|
@@ -570,7 +570,25 @@ class SalesOrderService extends Service
|
|
|
|
|
|
$customer = Customer::where('id',$sales['customer_id'])->value('title');
|
|
|
$sales['customer_title'] = $customer ?? '';
|
|
|
+ $customer_contact_id = $contact_type_id = 0;
|
|
|
+ $customer_contact = CustomerInfo::where('del_time',0)
|
|
|
+ ->where('customer_id',$sales['customer_id'])
|
|
|
+ ->where('contact_info', $sales['customer_contact'])
|
|
|
+ ->where('type',CustomerInfo::type_one)
|
|
|
+ ->first();
|
|
|
+ if(! empty($customer_contact)){
|
|
|
+ $customer_contact = $customer_contact->toArray();
|
|
|
+ $customer_contact_id = $customer_contact['id'];
|
|
|
+ $contact_type_id = $customer_contact['contact_type'];
|
|
|
+ }
|
|
|
+ $sales['customer_contact_id'] = $customer_contact_id;
|
|
|
+ $sales['contact_type_id'] = $contact_type_id;
|
|
|
+ $sales['contact_type_title'] = BasicType::where('id',$contact_type_id)->value('title');
|
|
|
+
|
|
|
$sales['sales_order_type_title'] = SalesOrder::$order_type[$sales['sales_order_type']] ?? '';
|
|
|
+
|
|
|
+ $sales['type_title'] = SalesOrder::$build_state[$sales['type']] ?? '';
|
|
|
+ $sales['is_dispatch'] = $sales['dispatch_time_second'] ? 1 : 0;
|
|
|
$sales['activity_product'] = $sales['file'] = $sales['employee_one'] = $sales['employee_two'] = $sales['employee_three'] = $sales['product'] = $sales['dispatch_depart_one'] = $sales['dispatch_employee'] = $sales['dispatch_depart_two'] = [];
|
|
|
$array = [
|
|
|
$sales['plat_type'],
|
|
@@ -746,10 +764,32 @@ class SalesOrderService extends Service
|
|
|
public function salesOrderCommon($data,$user){
|
|
|
$model = SalesOrder::Clear($user,$data);
|
|
|
$model = $model->where('del_time',0)
|
|
|
- ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no')
|
|
|
- ->orderby('id', 'desc')
|
|
|
- ->orderby('dispatch_time_second','desc');
|
|
|
+ ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no','type');
|
|
|
|
|
|
+ if(! empty($data['order_list'])){
|
|
|
+ if($data['order_list'] == 1){
|
|
|
+ $model = $model->orderby('id','desc');
|
|
|
+ //订单合同 安装件合同 状态未指派总社分社的 或者 快递件合同
|
|
|
+// $model->where('dispatch_time_second', 0);
|
|
|
+// $model->where('state', '<=', SalesOrder::State_three);
|
|
|
+ }elseif($data['order_list'] == 2){
|
|
|
+ $model = $model->orderby('dispatch_time_second','desc');
|
|
|
+ //派单合同 安装件合同 状态已指派总社分社的
|
|
|
+ $model = $model->where('dispatch_time_second', '>', 0);
|
|
|
+// $model->where('state', '>=', SalesOrder::State_four);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $model = $model->orderby('id', 'desc');
|
|
|
+ }
|
|
|
+ if(isset($data['is_dispatch'])) {
|
|
|
+ if($data['is_dispatch'] == 0){
|
|
|
+ //订单合同 安装件合同 状态未指派总社分社的 或者 快递件合同
|
|
|
+ $model->where('dispatch_time_second', 0);
|
|
|
+ }else{
|
|
|
+ //派单合同 安装件合同 状态已指派总社分社的
|
|
|
+ $model->where('dispatch_time_second', '>', 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
if(! empty($data['order_number'])) $model->where('order_number','LIKE', '%'.$data['order_number'].'%');
|
|
|
if(! empty($data['sales_order_type'])) $model->where('sales_order_type',$data['sales_order_type']);
|
|
|
if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
@@ -779,17 +819,6 @@ class SalesOrderService extends Service
|
|
|
// $model->where('state','<',SalesOrder::State_seven);
|
|
|
}
|
|
|
if(! empty($data['sales_order_id'])) $model->where('id',$data['sales_order_id']);
|
|
|
- if(! empty($data['order_list'])){
|
|
|
- if($data['order_list'] == 1){
|
|
|
- //订单合同 安装件合同 状态未指派总社分社的 或者 快递件合同
|
|
|
- $model->where('dispatch_time_second', 0);
|
|
|
-// $model->where('state', '<=', SalesOrder::State_three);
|
|
|
- }elseif($data['order_list'] == 2){
|
|
|
- //派单合同 安装件合同 状态已指派总社分社的
|
|
|
- $model->where('dispatch_time_second', '>', 0);
|
|
|
-// $model->where('state', '>=', SalesOrder::State_four);
|
|
|
- }
|
|
|
- }
|
|
|
if(isset($data['state'])) {
|
|
|
if($data['state'] == SalesOrder::special_status){
|
|
|
$model->whereRaw(SalesOrder::search1);
|
|
@@ -1126,7 +1155,13 @@ class SalesOrderService extends Service
|
|
|
//跟进记录
|
|
|
$record_array = (new FollowUpRecordService())->getVisitDataOfTime($data_id, FollowUpRecord::type_two);
|
|
|
|
|
|
+ //获取客户的信息
|
|
|
+ $customer_id = array_column($data['data'],'customer_id');
|
|
|
+ $customer_map = $this->getCustomer($customer_id);
|
|
|
+
|
|
|
foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['construction_model_type'] = SalesOrder::$build_for_con[$value['type']] ?? 1;
|
|
|
+ $data['data'][$key]['type_title'] = SalesOrder::$build_state[$value['type']] ?? '';
|
|
|
$data['data'][$key]['plat_type_title'] = $basic_map[$value['plat_type']] ?? '';
|
|
|
$data['data'][$key]['sales_order_type_title'] = SalesOrder::$order_type[$value['sales_order_type']] ?? '';
|
|
|
$data['data'][$key]['model_type_title'] = SalesOrder::$model_type_title[$value['model_type']] ?? '';
|
|
@@ -1136,10 +1171,16 @@ class SalesOrderService extends Service
|
|
|
$data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
|
|
|
$data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
|
|
|
$data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
|
|
|
+ $c_tmp = $customer_map[$value['customer_id'] . $value['customer_contact']] ?? [];
|
|
|
+ $data['data'][$key]['customer_contact_id'] = $c_tmp['customer_contact_id'] ?? '';
|
|
|
+ $data['data'][$key]['contact_type_id'] = $c_tmp['contact_type_id'] ?? '';
|
|
|
+ $data['data'][$key]['contact_type_title'] = $c_tmp['contact_type_title'] ?? '';
|
|
|
$data['data'][$key]['sign_time'] = $value['sign_time'] ? date('Y-m-d',$value['sign_time']) : '';
|
|
|
$data['data'][$key]['construction_time'] = $value['construction_time'] ? date('Y-m-d H:i:s',$value['construction_time']) : '';
|
|
|
$data['data'][$key]['handover_time'] = $value['handover_time'] ? date('Y-m-d H:i:s',$value['handover_time']) : '';
|
|
|
$data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['is_dispatch'] = $value['dispatch_time_second'] ? 1 : 0;
|
|
|
+ $data['data'][$key]['is_dispatch_title'] = $value['dispatch_time_second'] ? "已派单" : "未派单";
|
|
|
$data['data'][$key]['dispatch_time_second_time'] = $value['dispatch_time_second'] ? date('Y-m-d H:i:s',$value['dispatch_time_second']) : '';
|
|
|
$crt_name = $emp[$value['crt_id']] ?? '';
|
|
|
$data['data'][$key]['crt_name'] = $crt_name;
|
|
@@ -1202,6 +1243,33 @@ class SalesOrderService extends Service
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
+ public function getCustomer($customer_id){
|
|
|
+ $customer_contact_map = [];
|
|
|
+ $contact_type_id = [];
|
|
|
+ $customer_contact = CustomerInfo::where('del_time',0)
|
|
|
+ ->whereIn('customer_id', $customer_id)
|
|
|
+ ->where('type',CustomerInfo::type_one)
|
|
|
+ ->select('customer_id','contact_info','contact_type','id')
|
|
|
+ ->get()->toArray();
|
|
|
+ if(! empty($customer_contact)){
|
|
|
+ foreach ($customer_contact as $value){
|
|
|
+ if(! empty($value['customer_id']) && ! empty($value['contact_info'])){
|
|
|
+ $customer_contact_map[$value['customer_id'] . $value['contact_info']] = [
|
|
|
+ 'customer_contact_id' => $value['id'],
|
|
|
+ 'contact_type_id' => $value['contact_type'],
|
|
|
+ ];
|
|
|
+ $contact_type_id[] = $value['contact_type'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $contact_type_title = BasicType::whereIn('id', $contact_type_id)->pluck('title','id')->toArray();
|
|
|
+ foreach ($customer_contact_map as $key => $value){
|
|
|
+ $customer_contact_map[$key]['contact_type_title'] = $contact_type_title[$value['contact_type_id']] ?? "";
|
|
|
+ }
|
|
|
+
|
|
|
+ return $customer_contact_map;
|
|
|
+ }
|
|
|
+
|
|
|
public function countData($column = "", $data, $user){
|
|
|
if(empty($column) || empty($data['from_wx'])) return [0, 0];
|
|
|
|
|
@@ -1361,7 +1429,7 @@ class SalesOrderService extends Service
|
|
|
* @return array
|
|
|
*/
|
|
|
public function salesOrderDispatchCompany($data,$user){
|
|
|
- list($status,$msg) = $this->salesOrderDispatchCompanyRule($data);
|
|
|
+ list($status,$msg) = $this->salesOrderDispatchCompanyRule($data,$user);
|
|
|
if(! $status) return [false,$msg];
|
|
|
|
|
|
try {
|
|
@@ -1371,7 +1439,7 @@ class SalesOrderService extends Service
|
|
|
'state' => SalesOrder::State_four,
|
|
|
'dispatch_time_second' => $time,
|
|
|
'dispatch_time_second_id' => $user['id'],
|
|
|
- 'type' => $data['type'] ?? 0
|
|
|
+ 'type' => $data['type']
|
|
|
]);
|
|
|
|
|
|
SeeRange::where('del_time',0)
|
|
@@ -1404,6 +1472,23 @@ class SalesOrderService extends Service
|
|
|
$model->save();
|
|
|
}
|
|
|
|
|
|
+ if(isset($data['notify_id'])){
|
|
|
+ SalesOrder::where('del_time',0)->where('id',$msg['id'])->update([
|
|
|
+ 'dispatch_time_first' => $time, // 第一次派单时间也记录
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if(! empty($data['notify_id'])){ //门店负责人不为空
|
|
|
+ $insert[] = [
|
|
|
+ 'data_id' => $msg['id'],
|
|
|
+ 'data_type' => SeeRange::type_seven,
|
|
|
+ 'param_id' => $data['notify_id'],
|
|
|
+ 'type' => SeeRange::data_two,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ SeeRange::insert($insert);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
(new OrderOperationService())->add([
|
|
|
'order_number' => $msg['order_number'],
|
|
|
'msg' => OrderOperation::$type[OrderOperation::two],
|
|
@@ -1452,16 +1537,30 @@ class SalesOrderService extends Service
|
|
|
* @param $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function salesOrderDispatchCompanyRule(&$data){
|
|
|
+ public function salesOrderDispatchCompanyRule(&$data,$user){
|
|
|
if(empty($data['id'])) return [false,'请选择合同派单'];
|
|
|
+ if(empty($data['dispatch_depart_two'])) return [false, '请指派总社或分社'];
|
|
|
+ $depart_id = $data['dispatch_depart_two'][0];
|
|
|
+
|
|
|
+ if(! isset($data['other_fee_1'])) return [false,'分社安装金额不存在'];
|
|
|
+ $res = $this->checkNumber($data['other_fee_1']);
|
|
|
+ if(! isset($data['type']) || ! isset(SalesOrder::$build_state[$data['type']])) return [false, '合同安装类型不存在'];
|
|
|
+ if($data['type'] <= 0) $data['type'] = SalesOrder::build_zero;
|
|
|
+ if(! $res) return [false,'价格请输入不超过两位小数并且大于等于0的数值'];
|
|
|
+
|
|
|
$sale = SalesOrder::where('del_time',0)
|
|
|
->where('id',$data['id'])
|
|
|
->first();
|
|
|
if(empty($sale)) return [false,'合同不存在或已被删除'];
|
|
|
$sale = $sale->toArray();
|
|
|
if($sale['sales_order_type'] != SalesOrder::Order_type_one) return [false,'非安装件合同,操作失败'];
|
|
|
- if($sale['state'] < SalesOrder::State_three) return [false,'请确认合同状态,操作失败'];
|
|
|
if($sale['state'] > SalesOrder::State_four) return [false,'请确认合同状态,操作失败'];
|
|
|
+ if($sale['state'] < SalesOrder::State_two) return [false, '合同未审核通过,操作失败'];
|
|
|
+ if($sale['state'] < SalesOrder::State_three) {
|
|
|
+ //如果没有派销售过 找到门店负责人
|
|
|
+ $data['notify_id'] = Depart::where('id', $depart_id)->value('notify_id');
|
|
|
+ }
|
|
|
+
|
|
|
$product = SalesOrderProductInfo::where('del_time',0)
|
|
|
->where('sales_order_id',$data['id'])
|
|
|
->select('product_id','number')
|
|
@@ -1471,18 +1570,16 @@ class SalesOrderService extends Service
|
|
|
$bool = Construction::where('del_time',0)
|
|
|
->where('sales_order_id',$data['id'])
|
|
|
->exists();
|
|
|
- if($bool) return [false,'合同已下施工,操作失败'];
|
|
|
+ if($bool) return [false,'合同已生成施工单,操作失败'];
|
|
|
|
|
|
- if(empty($data['dispatch_depart_two'])) return [false, '请指派总社或分社'];
|
|
|
$product_id = array_unique(array_column($product,'product_id'));
|
|
|
$return = RangeService::productNotSeeRange($product_id);
|
|
|
|
|
|
//产品信息
|
|
|
$product_array = Product::whereIn('id',$product_id)->select('title','id','build_fee')->get()->toArray();
|
|
|
- $map = $map2 = [];
|
|
|
+ $map = [];
|
|
|
foreach ($product_array as $value){
|
|
|
$map[$value['id']] = $value['title'];
|
|
|
- $map2[$value['id']] = $value['build_fee'];
|
|
|
}
|
|
|
if(! empty($return)){
|
|
|
foreach ($data['dispatch_depart_two'] as $value){
|
|
@@ -1493,27 +1590,12 @@ class SalesOrderService extends Service
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //到店安装 原逻辑
|
|
|
- if(empty($data['type'])){
|
|
|
- if(empty($data['other_fee_1'])) return [false,'价格不能为空'];
|
|
|
- $res = $this->checkNumber($data['other_fee_1']);
|
|
|
- if(! $res) return [false,'到店安装价格请输入不超过两位小数并且大于0的数值'];
|
|
|
- }else{
|
|
|
- $other_fee_1 = 0;
|
|
|
- foreach ($product as $value){
|
|
|
- $build_fee = $map2[$value['product_id']] ?? 0;
|
|
|
- $fee = bcmul($value['number'],$build_fee,2);
|
|
|
- $other_fee_1 = bcadd($other_fee_1,$fee,2);
|
|
|
- }
|
|
|
- $data['other_fee_1'] = $other_fee_1;
|
|
|
- }
|
|
|
-
|
|
|
return [true, $sale];
|
|
|
}
|
|
|
|
|
|
public function salesOrderListsalesOrderWxList($data,$user){
|
|
|
$model = SalesOrder::where('del_time',0)
|
|
|
- ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no')
|
|
|
+ ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no','type')
|
|
|
->orderby('id', 'desc')
|
|
|
->orderby('dispatch_time_second','desc');
|
|
|
|
|
@@ -1687,4 +1769,328 @@ class SalesOrderService extends Service
|
|
|
|
|
|
return [true, ''];
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量推送公司
|
|
|
+ * @param $data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function salesOrderBatchDispatchCompany($data,$user){
|
|
|
+ list($status,$msg) = $this->salesOrderBatchDispatchCompanyRule($data,$user);
|
|
|
+ if(! $status) return [false,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ $time = time();
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $sales = $msg;
|
|
|
+ $sales_id = array_column($sales, 'id');
|
|
|
+
|
|
|
+ SeeRange::where('del_time',0)
|
|
|
+ ->whereIn('data_id', $sales_id)
|
|
|
+ ->where('data_type',SeeRange::type_seven)
|
|
|
+ ->where('type',SeeRange::data_three)
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+ SalesOrderOtherFee::where('del_time',0)
|
|
|
+ ->whereIn('sales_order_id',$sales_id)
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+ if(! empty($data['dispatch_depart_two'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['dispatch_depart_two'] as $value){
|
|
|
+ foreach ($sales_id as $v){
|
|
|
+ $insert[] = [
|
|
|
+ 'data_id' => $v,
|
|
|
+ 'data_type' => SeeRange::type_seven,
|
|
|
+ 'param_id' => $value,
|
|
|
+ 'type' => SeeRange::data_three,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SeeRange::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ $other_fee = $notify_id = [];
|
|
|
+ foreach ($sales as $value){
|
|
|
+ SalesOrder::where('del_time',0)->where('id', $value['id'])->update([
|
|
|
+ 'state' => SalesOrder::State_four,
|
|
|
+ 'dispatch_time_second' => $time,
|
|
|
+ 'dispatch_time_second_id' => $user['id'],
|
|
|
+ 'type' => $value['type']
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $other_fee[] = [
|
|
|
+ 'sales_order_id' => $value['id'],
|
|
|
+ 'other_fee_1' => $value['other_fee_1'],
|
|
|
+ 'type' => $value['type'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ if(! empty($value['notify_id'])){
|
|
|
+ //门店负责人不为空
|
|
|
+ $notify_id[] = [
|
|
|
+ 'data_id' => $value['id'],
|
|
|
+ 'data_type' => SeeRange::type_seven,
|
|
|
+ 'param_id' => $value['notify_id'],
|
|
|
+ 'type' => SeeRange::data_two,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($value['notify_id']) && empty($value['dispatch_time_first'])){
|
|
|
+ SalesOrder::where('del_time',0)->where('id',$value['id'])->update([
|
|
|
+ 'dispatch_time_first' => $time, // 第一次派单时间记录
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(! empty($other_fee)) SalesOrderOtherFee::insert($other_fee);
|
|
|
+ if(! empty($notify_id)) SeeRange::insert($notify_id);
|
|
|
+
|
|
|
+ (new OrderOperationService())->batchAdd([
|
|
|
+ 'order_number' => array_column($sales, 'order_number'),
|
|
|
+ 'msg' => OrderOperation::$type[OrderOperation::thirty_one],
|
|
|
+ 'type' => OrderOperation::thirty_one
|
|
|
+ ],$user);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ $depart_id = $data['dispatch_depart_two'][0] ?? 0;
|
|
|
+ $notify_id = Depart::where('id',$depart_id)->value('notify_id');
|
|
|
+ $crt_id = array_column($sales,'crt_id');
|
|
|
+ if($notify_id) $crt_id[] = $notify_id;
|
|
|
+ $emp_name = Employee::whereIn('id',$crt_id)->pluck('emp_name','id')->toArray();
|
|
|
+ foreach ($sales as $value){
|
|
|
+ if(! empty($depart_id) && $depart_id != $value['top_depart_id']){
|
|
|
+ //发送消息
|
|
|
+ if(! empty($notify_id)){
|
|
|
+ $send_data[] = [
|
|
|
+ 'employee_id' => $notify_id,
|
|
|
+ 'type' => 1,
|
|
|
+ 'state' => 1,
|
|
|
+ 'menu_id' => 37,
|
|
|
+ 'order_number' => $value['order_number'],
|
|
|
+ 'tmp_data' => [
|
|
|
+ $value['order_number'],
|
|
|
+ $emp_name[$value['crt_id']] ?? "",
|
|
|
+ $emp_name[$notify_id] ?? "" . "(派单通知)",
|
|
|
+ date('Y-m-d H:i:s'),
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ (new OaService())->sendWxOaCheckMessage($send_data);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //直接确认
|
|
|
+ SalesOrder::where('del_time',0)->where('id',$value['id'])->update([
|
|
|
+ 'is_confirm' => 1,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量推送公司规则
|
|
|
+ * @param $data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function salesOrderBatchDispatchCompanyRule(&$data,$user){
|
|
|
+ //是总社还是分社
|
|
|
+ if(empty($data['dispatch_depart_two'])) return [false, '请指派总社或分社'];
|
|
|
+ $depart_id = $data['dispatch_depart_two'][0];
|
|
|
+
|
|
|
+ //前端传参
|
|
|
+ if(empty($data['data'])) return [false,'请选择合同派单'];
|
|
|
+
|
|
|
+ $other_fee_1_map = [];
|
|
|
+ foreach ($data['data'] as $value){
|
|
|
+ if(empty($value['id'])) return [false, '合同ID不能为空'];
|
|
|
+ if(! isset($value['type']) || ! isset(SalesOrder::$build_state[$value['type']])) return [false, '合同安装类型不存在'];
|
|
|
+ $type = $value['type'];
|
|
|
+ if($type <= 0) $type = SalesOrder::build_zero;
|
|
|
+ if(! isset($value['other_fee_1'])) return [false, '合同安装金额不存在'];
|
|
|
+ $res = $this->checkNumber($value['other_fee_1']);
|
|
|
+ if(! $res) return [false,'金额请输入不超过两位小数并且大于等于0的数值'];
|
|
|
+ $other_fee_1_map[$value['id']] = [
|
|
|
+ 'other_fee_1' => $value['other_fee_1'],
|
|
|
+ 'type' => $type,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ //合同信息
|
|
|
+ $sale_id = array_column($data['data'], 'id');
|
|
|
+ $sale = SalesOrder::where('del_time',0)
|
|
|
+ ->whereIn('id', $sale_id)
|
|
|
+ ->get()->toArray();
|
|
|
+ if(empty($sale)) return [false,'合同不存在或已被删除'];
|
|
|
+ $notify_id = Depart::where('id', $depart_id)->value('notify_id');
|
|
|
+
|
|
|
+ //组织合同数据
|
|
|
+ foreach ($sale as $key => $value){
|
|
|
+ if($value['del_time'] > 0) return [false,'合同:' . $value['order_number'] . '不存在或已被删除'];
|
|
|
+ if($value['sales_order_type'] != SalesOrder::Order_type_one) return [false,'非安装件合同,操作失败'];
|
|
|
+ if($value['state'] > SalesOrder::State_four) return [false,'请确认合同状态,操作失败'];
|
|
|
+ if($value['state'] < SalesOrder::State_three) {
|
|
|
+ //如果没有派销售过 找到门店负责人
|
|
|
+ $sale[$key]['notify_id'] = $notify_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ //合同派单信息
|
|
|
+ $other_fee_1 = $other_fee_1_map[$value['id']] ?? [];
|
|
|
+ $sale[$key]['other_fee_1'] = $other_fee_1['other_fee_1'];
|
|
|
+ $sale[$key]['type'] = $other_fee_1['type'];
|
|
|
+ }
|
|
|
+
|
|
|
+ //校验产品
|
|
|
+ $p_id_map = array_column($sale, 'order_number', 'id');
|
|
|
+ $product = SalesOrderProductInfo::where('del_time',0)
|
|
|
+ ->whereIn('sales_order_id', $sale_id)
|
|
|
+ ->select('product_id','sales_order_id')
|
|
|
+ ->get()->toArray();
|
|
|
+ if(empty($product)) return [false,'合同产品不能为空'];
|
|
|
+ $p_map = array_column($product, null, 'sales_order_id');
|
|
|
+ foreach ($data['data'] as $value){
|
|
|
+ if(empty($p_map[$value['id']])){
|
|
|
+ $order_number = $p_id_map[$value['id']] ?? "";
|
|
|
+ return [false, '合同' . $order_number . '产品不能为空'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //校验施工
|
|
|
+ $bool = Construction::where('del_time',0)
|
|
|
+ ->whereIn('sales_order_id', $sale_id)
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'合同已生成施工单,操作失败'];
|
|
|
+
|
|
|
+ //产品不可见
|
|
|
+ $product_id = array_unique(array_column($product,'product_id'));
|
|
|
+ $return = RangeService::productNotSeeRange($product_id);
|
|
|
+
|
|
|
+ //产品信息
|
|
|
+ $product_array = Product::whereIn('id',$product_id)->select('title','id','build_fee')->get()->toArray();
|
|
|
+ $map = [];
|
|
|
+ foreach ($product_array as $value){
|
|
|
+ $map[$value['id']] = $value['title'];
|
|
|
+ }
|
|
|
+ if(! empty($return)){
|
|
|
+ foreach ($data['dispatch_depart_two'] as $value){
|
|
|
+ foreach ($return as $key => $values){
|
|
|
+ $tmp = $map[$key] ?? "";
|
|
|
+ if(in_array($value, $values)) return [false,'产品'. $tmp .'对于该总社或分社不可见'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $sale];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取安装价格
|
|
|
+ * @param $data
|
|
|
+ * @param $user
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function salesOrderGetBuildFee($data,$user){
|
|
|
+ if(empty($data['id'])) return [false,'请选择合同派单'];
|
|
|
+ if(empty($data['top_depart_id'])) return [false,'请选择派单分社'];
|
|
|
+ $depart = Depart::where('del_time',0)->where('id', $data['top_depart_id'])->first();
|
|
|
+ if(empty($depart)) return [false, '分社不存在或已被删除'];
|
|
|
+ $depart = $depart->toArray();
|
|
|
+
|
|
|
+ $sale = SalesOrder::where('del_time',0)
|
|
|
+ ->whereIn('id', $data['id'])
|
|
|
+ ->get()->toArray();
|
|
|
+ if(empty($sale)) return [false,'合同不存在或已被删除'];
|
|
|
+ $product = SalesOrderProductInfo::where('del_time',0)
|
|
|
+ ->whereIn('sales_order_id',$data['id'])
|
|
|
+ ->select('product_id','number','sales_order_id')
|
|
|
+ ->get()->toArray();
|
|
|
+ if(empty($product)) return [false,'合同产品不能为空'];
|
|
|
+ $product_array = Product::whereIn('id',array_unique(array_column($product, 'product_id')))
|
|
|
+ ->select('title','id','build_fee')
|
|
|
+ ->get()->toArray();
|
|
|
+ $map2 = [];
|
|
|
+ foreach ($product_array as $value){
|
|
|
+ $map2[$value['id']] = $value['build_fee'];
|
|
|
+ }
|
|
|
+ // 实时 带货安装包安装
|
|
|
+ $count = [];
|
|
|
+ $other_fee_1 = 0;
|
|
|
+ foreach ($product as $value){
|
|
|
+ $build_fee = $map2[$value['product_id']] ?? 0;
|
|
|
+ $fee = bcmul($value['number'],$build_fee,2);
|
|
|
+ $other_fee_1 = bcadd($other_fee_1,$fee,2);
|
|
|
+
|
|
|
+ if(isset($count[$value['sales_order_id']])){
|
|
|
+ $tmp = bcadd($count[$value['sales_order_id']], $other_fee_1,2);
|
|
|
+ $count[$value['sales_order_id']] = $tmp;
|
|
|
+ }else{
|
|
|
+ $count[$value['sales_order_id']] = $other_fee_1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $product_map = array_column($product,null,'sales_order_id');
|
|
|
+
|
|
|
+ $return_map = [];
|
|
|
+ foreach ($sale as $value){
|
|
|
+ if(! isset($product_map[$value['id']])) return [false, "合同:" . $value['order_number'] . '下没有产品信息'];
|
|
|
+ foreach (SalesOrder::$build_state as $key => $value_v){
|
|
|
+ if($key == SalesOrder::build_minus) continue;
|
|
|
+ if($key == SalesOrder::build_zero){
|
|
|
+ $rate = bcdiv($depart['rate'], 100,2);
|
|
|
+ $other_fee_1 = bcmul($value['contract_fee'], $rate,2);
|
|
|
+ }elseif($key == SalesOrder::build_one){
|
|
|
+ $other_fee_1 = $count[$value['id']] ?? "0.00";
|
|
|
+ }else{
|
|
|
+ $other_fee_1 = "0.00";
|
|
|
+ }
|
|
|
+ $return_map[$value['id']][$key] = [
|
|
|
+ 'id' => $value['id'],
|
|
|
+ 'order_number' => $value['order_number'],
|
|
|
+ 'type' => $key,
|
|
|
+ 'other_fee_1' => $other_fee_1,
|
|
|
+ 'is_choose' => 0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 已保存的
|
|
|
+ $save = SalesOrderOtherFee::where('del_time',0)
|
|
|
+ ->whereIn('sales_order_id', $data['id'])
|
|
|
+ ->pluck('other_fee_1','sales_order_id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ foreach ($sale as $value){// 填充已保存的
|
|
|
+ if(isset($save[$value['id']][$value['type']])){
|
|
|
+ $other_fee_1 = $save[$value['id']];
|
|
|
+ $return_map[$value['id']][$value['type']]['other_fee_1'] = $other_fee_1;
|
|
|
+ $return_map[$value['id']][$value['type']]['is_choose'] = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $return = [];
|
|
|
+ foreach ($return_map as $value){
|
|
|
+ foreach ($value as $v){
|
|
|
+ $tmp = [
|
|
|
+ 'type' => $v['type'],
|
|
|
+ 'other_fee_1' => $v['other_fee_1'],
|
|
|
+ 'is_choose' => $v['is_choose'],
|
|
|
+ ];
|
|
|
+ if(isset($return[$v['id']])){
|
|
|
+ $return[$v['id']]['data'][] = $tmp;
|
|
|
+ }else{
|
|
|
+ $return[$v['id']] = [
|
|
|
+ 'id' => $v['id'],
|
|
|
+ 'order_number' => $v['order_number'],
|
|
|
+ 'data' => [$tmp]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, array_values($return)];
|
|
|
+ }
|
|
|
}
|