|
|
@@ -1,1085 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace App\Service;
|
|
|
-
|
|
|
-use App\Model\CustomerSupply;
|
|
|
-use App\Model\Employee;
|
|
|
-use App\Model\Order;
|
|
|
-use App\Model\OrderDetails;
|
|
|
-use App\Model\Device;
|
|
|
-use App\Model\ReminderDetails;
|
|
|
-use App\Model\ReminderRecord;
|
|
|
-use App\Model\ReminderRecordDetails;
|
|
|
-use App\Model\TodoList;
|
|
|
-use App\Model\WxEmployeeOfficial;
|
|
|
-use App\Service\Weixin\WxTemplateMessageService;
|
|
|
-use Illuminate\Support\Facades\DB;
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
-
|
|
|
-class OrderService extends Service
|
|
|
-{
|
|
|
- public function orderEdit($data,$user){
|
|
|
- list($status,$msg) = $this->orderRule($data, $user, false);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $model = Order::where('id',$data['id'])->first();
|
|
|
- $model->order_number = $data['order_number'] ?? '';
|
|
|
- $model->customer_id = $data['customer_id'] ?? 0;
|
|
|
- $model->supply_id = $data['supply_id'] ?? 0;
|
|
|
- $model->order_time = $data['order_time'] ?? 0;
|
|
|
- $model->quantity = $data['quantity'] ?? 0;
|
|
|
- $model->save();
|
|
|
-
|
|
|
- $time = time();
|
|
|
- OrderDetails::where('del_time',0)
|
|
|
- ->where('order_id', $model->id)
|
|
|
- ->update(['del_time' => $time]);
|
|
|
- $this->saveDetail($model->id, $time, $data);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function orderAdd($data,$user){
|
|
|
- list($status,$msg) = $this->orderRule($data, $user);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $model = new Order();
|
|
|
- $model->order_number = $data['order_number'] ?? '';
|
|
|
- $model->customer_id = $data['customer_id'] ?? 0;
|
|
|
- $model->supply_id = $data['supply_id'] ?? 0;
|
|
|
- $model->order_time = $data['order_time'] ?? 0;
|
|
|
- $model->quantity = $data['quantity'] ?? 0;
|
|
|
- $model->crt_id = $user['id'];
|
|
|
- $model->save();
|
|
|
-
|
|
|
- $this->saveDetail($model->id, time(), $data);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- private function saveDetail($id, $time, $data){
|
|
|
- if(! empty($data['details'])){
|
|
|
- $unit = [];
|
|
|
- foreach ($data['details'] as $value){
|
|
|
- $unit[] = [
|
|
|
- 'order_id' => $id,
|
|
|
- 'code' => $value['code'],
|
|
|
- 'title' => $value['title'],
|
|
|
- 'size' => $value['size'] ?? "",
|
|
|
- 'unit' => $value['unit'] ?? "",
|
|
|
- 'quantity' => $value['quantity'],
|
|
|
- 'crt_time' => $time,
|
|
|
- ];
|
|
|
- }
|
|
|
- if(! empty($unit)) OrderDetails::insert($unit);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private function getDetail($id){
|
|
|
- $data = OrderDetails::where('del_time',0)
|
|
|
- ->where('order_id', $id)
|
|
|
- ->get()->toArray();
|
|
|
-
|
|
|
- $unit = [];
|
|
|
- foreach ($data as $value){
|
|
|
- $unit[] = [
|
|
|
- 'code' => $value['code'],
|
|
|
- 'title' => $value['title'],
|
|
|
- 'size' => $value['size'],
|
|
|
- 'unit' => $value['unit'],
|
|
|
- 'quantity' => $value['quantity'],
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- $detail = [
|
|
|
- 'details' => $unit,
|
|
|
- ];
|
|
|
-
|
|
|
- foreach ($detail as $key => $value) {
|
|
|
- if (empty($value)) {
|
|
|
- $detail[$key] = (object)[]; // 转成 stdClass 对象
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $detail;
|
|
|
- }
|
|
|
-
|
|
|
- public function orderDel($data){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
- $time = time();
|
|
|
-
|
|
|
- Order::where('del_time',0)
|
|
|
- ->whereIn('id',$data['id'])
|
|
|
- ->update(['del_time' => $time]);
|
|
|
-
|
|
|
- OrderDetails::where('del_time',0)
|
|
|
- ->where('order_id', $data['id'])
|
|
|
- ->update(['del_time' => $time]);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function orderDetail($data, $user){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
- $customer = Order::where('del_time',0)
|
|
|
- ->where('id',$data['id'])
|
|
|
- ->first();
|
|
|
- if(empty($customer)) return [false,'订单不存在或已被删除'];
|
|
|
- $customer = $customer->toArray();
|
|
|
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
|
|
|
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
- $customer['customer_title'] = $customer['customer_id'] ? CustomerSupply::where('id',$customer['customer_id'])->value('title') : "";
|
|
|
- $customer['supply_title'] = $customer['supply_id'] ? CustomerSupply::where('id',$customer['supply_id'])->value('title') : "";
|
|
|
-
|
|
|
- $details = $this->getDetail($data['id']);
|
|
|
- $customer = array_merge($customer, $details);
|
|
|
-
|
|
|
- return [true, $customer];
|
|
|
- }
|
|
|
-
|
|
|
- public function orderCommon($data,$user, $field = []){
|
|
|
- if(empty($field)) $field = Order::$field;
|
|
|
-
|
|
|
- $model = Order::Clear($user,$data);
|
|
|
- $model = $model->where('del_time',0)
|
|
|
- ->select($field)
|
|
|
- ->orderby('id', 'desc');
|
|
|
-
|
|
|
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
- if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
|
|
|
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
|
|
|
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
- $model->where('crt_time','>=',$return[0]);
|
|
|
- $model->where('crt_time','<=',$return[1]);
|
|
|
- }
|
|
|
- if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])) {
|
|
|
- $return = $this->changeDateToTimeStampAboutRange($data['order_time']);
|
|
|
- $model->where('order_time','>=',$return[0]);
|
|
|
- $model->where('order_time','<=',$return[1]);
|
|
|
- }
|
|
|
-
|
|
|
- return $model;
|
|
|
- }
|
|
|
-
|
|
|
- public function orderList($data,$user){
|
|
|
- $model = $this->orderCommon($data, $user);
|
|
|
- $list = $this->limit($model,'',$data);
|
|
|
- $list = $this->fillData($list,$user,$data);
|
|
|
-
|
|
|
- $list['count'] = $this->countTotal($list['data'], $user['header_default']);
|
|
|
-
|
|
|
- return [true, $list];
|
|
|
- }
|
|
|
-
|
|
|
- public function orderRule(&$data, $user, $is_add = true){
|
|
|
- if(empty($data['order_number'])) return [false, '订单号不能为空'];
|
|
|
- if(empty($data['order_time'])) return [false, '订单日期不能为空'];
|
|
|
- $data['order_time'] = $this->changeDateToDate($data['order_time']);
|
|
|
- if(empty($data['details'])) return [false, '物料不能为空'];
|
|
|
- $total = 0;
|
|
|
- foreach ($data['details'] as $value){
|
|
|
- if(empty($value['code'])) return [false, '物料编码不能为空'];
|
|
|
- if(empty($value['title'])) return [false, '物料名称不能为空'];
|
|
|
- $res = $this->checkNumber($value['quantity'],2,'positive');
|
|
|
- if(! $res['valid']) return [false,'物料数量:' . $res['error']];
|
|
|
- $total = bcadd($total, $value['quantity'],2);
|
|
|
- }
|
|
|
- $data['quantity'] = $total;
|
|
|
- list($status, $msg) = $this->checkArrayRepeat($data['details'],'code','物料编码');
|
|
|
- if(! $status) return [false, $msg];
|
|
|
-
|
|
|
- if($is_add){
|
|
|
- $bool = Order::where('order_number',$data['order_number'])
|
|
|
- ->where('crt_id', $user['id'])
|
|
|
- ->where('del_time',0)
|
|
|
- ->exists();
|
|
|
- }else{
|
|
|
- if(empty($data['id'])) return [false,'ID不能为空'];
|
|
|
- $bool = Order::where('order_number',$data['order_number'])
|
|
|
- ->where('crt_id', $user['id'])
|
|
|
- ->where('id','<>',$data['id'])
|
|
|
- ->where('del_time',0)
|
|
|
- ->exists();
|
|
|
- }
|
|
|
- if($bool) return [false, '订单号已存在'];
|
|
|
-
|
|
|
- return [true, $data];
|
|
|
- }
|
|
|
-
|
|
|
- public function fillData($data, $user, $search){
|
|
|
- if(empty($data['data'])) return $data;
|
|
|
-
|
|
|
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
|
|
|
- foreach ($data['data'] as $key => $value){
|
|
|
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
- $data['data'][$key]['order_time'] = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
|
|
|
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
|
|
|
- $data['data'][$key]['customer_title'] = $emp_2[$value['customer_id']] ?? '';
|
|
|
- $data['data'][$key]['supply_title'] = $emp_2[$value['supply_id']] ?? '';
|
|
|
- }
|
|
|
-
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- //催单管理---------------------------------------------
|
|
|
-
|
|
|
- public function reminderEdit($data,$user){
|
|
|
- list($status,$msg) = $this->reminderRule($data, $user, false);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $model = Device::where('id',$data['id'])->first();
|
|
|
- $model->order_id = $data['order_id'] ?? 0;
|
|
|
- $model->order_no = $data['order_no'] ?? '';
|
|
|
- $model->product_code = $data['product_code'] ?? '';
|
|
|
- $model->product_size = $data['product_size'] ?? '';
|
|
|
- $model->product_unit = $data['product_unit'] ?? '';
|
|
|
- $model->product_title = $data['product_title'] ?? '';
|
|
|
- $model->quantity = $data['quantity'] ?? 0;
|
|
|
- $model->order_time = $data['order_time'] ?? 0;
|
|
|
- $model->lt_arrived_time = $data['lt_arrived_time'] ?? 0;
|
|
|
- $model->tl_arrived_time = $data['tl_arrived_time'] ?? 0;
|
|
|
- $model->tl_quantity = $data['tl_quantity'] ?? 0;
|
|
|
- $model->rule = $data['rule'] ?? 0;
|
|
|
- $model->df_type = $data['df_type'] ?? 0;
|
|
|
- $model->save();
|
|
|
-
|
|
|
- $time = time();
|
|
|
- ReminderDetails::where('del_time',0)
|
|
|
- ->where('reminder_id', $model->id)
|
|
|
- ->update(['del_time' => $time]);
|
|
|
- $this->saveDetail1($model->id, $time, $data);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderAdd($data,$user){
|
|
|
- list($status,$msg) = $this->reminderRule($data, $user);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $model = new Device();
|
|
|
- $model->order_number = $this->createOrderNumber();
|
|
|
- $model->order_id = $data['order_id'] ?? 0;
|
|
|
- $model->order_no = $data['order_no'] ?? '';
|
|
|
- $model->product_code = $data['product_code'] ?? '';
|
|
|
- $model->product_size = $data['product_size'] ?? '';
|
|
|
- $model->product_unit = $data['product_unit'] ?? '';
|
|
|
- $model->product_title = $data['product_title'] ?? '';
|
|
|
- $model->quantity = $data['quantity'] ?? 0;
|
|
|
- $model->order_time = $data['order_time'] ?? 0;
|
|
|
- $model->lt_arrived_time = $data['lt_arrived_time'] ?? 0;
|
|
|
- $model->tl_arrived_time = $data['tl_arrived_time'] ?? 0;
|
|
|
- $model->tl_quantity = $data['tl_quantity'] ?? 0;
|
|
|
- $model->df_type = $data['df_type'] ?? 0;
|
|
|
- $model->rule = $data['rule'] ?? 0;
|
|
|
- $model->crt_id = $user['id'];
|
|
|
- $model->save();
|
|
|
-
|
|
|
- $this->saveDetail1($model->id, time(), $data);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- private function saveDetail1($id, $time, $data){
|
|
|
- if(! empty($data['details'])){
|
|
|
- $unit = [];
|
|
|
- foreach ($data['details'] as $value){
|
|
|
- $unit[] = [
|
|
|
- 'reminder_id' => $id,
|
|
|
- 'customer_supply_id' => $value['customer_supply_id'],
|
|
|
- 'is_main' => $value['is_main'],
|
|
|
- 'crt_time' => $time,
|
|
|
- ];
|
|
|
- }
|
|
|
- if(! empty($unit)) ReminderDetails::insert($unit);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private function getDetail1($id, $user = []){
|
|
|
- $customer_supply_id = $user['id'] ?? 0;
|
|
|
-
|
|
|
- $data = ReminderDetails::from('reminder_details as a')
|
|
|
- ->leftJoin('customer_supply as b','b.id','a.customer_supply_id')
|
|
|
- ->where('a.del_time',0)
|
|
|
- ->where('a.reminder_id', $id)
|
|
|
- ->when(! empty($customer_supply_id), function ($query) use ($customer_supply_id) {
|
|
|
- return $query->where('a.customer_supply_id', $customer_supply_id);
|
|
|
- })
|
|
|
- ->select('a.customer_supply_id','a.is_main','b.title','a.status')
|
|
|
- ->get()->toArray();
|
|
|
-
|
|
|
- $unit = $unit2 = [];
|
|
|
- foreach ($data as $value){
|
|
|
- if(! empty($customer_supply_id)) {
|
|
|
- $unit2 = [
|
|
|
- 'status' => $value['status'],
|
|
|
- ];
|
|
|
- }else{
|
|
|
- $unit[] = [
|
|
|
- 'customer_supply_id' => $value['customer_supply_id'],
|
|
|
- 'customer_supply_title' => $value['title'],
|
|
|
- 'is_main' => $value['is_main'],
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
- if(! empty($unit2)) return $unit2;
|
|
|
-
|
|
|
- $detail = [
|
|
|
- 'details' => $unit,
|
|
|
- ];
|
|
|
-
|
|
|
- foreach ($detail as $key => $value) {
|
|
|
- if (empty($value)) {
|
|
|
- $detail[$key] = (object)[]; // 转成 stdClass 对象
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $detail;
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderDel($data){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
-
|
|
|
- $bool = Device::where('del_time',0)
|
|
|
- ->whereIn('id',$data['id'])
|
|
|
- ->where('status','>',Device::status_zero)
|
|
|
- ->exists();
|
|
|
- if($bool) return [false, '催单状态已变更,删除失败'];
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
- $time = time();
|
|
|
-
|
|
|
- Device::where('del_time',0)
|
|
|
- ->whereIn('id',$data['id'])
|
|
|
- ->update(['del_time' => $time]);
|
|
|
-
|
|
|
- ReminderDetails::where('del_time',0)
|
|
|
- ->where('reminder_id', $data['id'])
|
|
|
- ->update(['del_time' => $time]);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderDetail($data, $user){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
- $customer = Device::where('del_time',0)
|
|
|
- ->where('id',$data['id'])
|
|
|
- ->first();
|
|
|
- if(empty($customer)) return [false,'催单不存在或已被删除'];
|
|
|
- $customer = $customer->toArray();
|
|
|
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
|
|
|
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
- $customer['order_time'] = $customer['order_time'] ? date("Y-m-d",$customer['order_time']): '';
|
|
|
- $customer['lt_arrived_time'] = $customer['lt_arrived_time'] ? date("Y-m-d",$customer['lt_arrived_time']): '';
|
|
|
- $customer['tl_arrived_time'] = $customer['tl_arrived_time'] ? date("Y-m-d",$customer['tl_arrived_time']): '';
|
|
|
- $customer['df_type_title'] = Device::$df_type_name[$customer['df_type']] ?? "";
|
|
|
- $customer['rule_title'] = Device::$rule_name[$customer['rule']] ?? "";
|
|
|
- $customer['status_title'] = Device::$status_name[$customer['status']] ?? "";
|
|
|
-
|
|
|
- $details = $this->getDetail1($data['id']);
|
|
|
- $customer = array_merge($customer, $details);
|
|
|
-
|
|
|
- $details_s = $this->getDetailOut($data);
|
|
|
- $customer['cd_details'] = $details_s;
|
|
|
-
|
|
|
- return [true, $customer];
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderCommon($data,$user, $field = []){
|
|
|
- if(empty($field)) $field = Device::$field;
|
|
|
-
|
|
|
- $model = Device::Clear($user,$data);
|
|
|
- $model = $model->where('del_time',0)
|
|
|
- ->select($field)
|
|
|
- ->orderby('id', 'desc');
|
|
|
-
|
|
|
- if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
|
|
|
- if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
|
|
|
- if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
|
|
|
- if(! empty($data['product_code'])) $model->where('product_code', 'LIKE', '%'.$data['product_code'].'%');
|
|
|
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
|
|
|
- if(isset($data['status'])) $model->where('status', $data['status']);
|
|
|
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
- $model->where('crt_time','>=',$return[0]);
|
|
|
- $model->where('crt_time','<=',$return[1]);
|
|
|
- }
|
|
|
- if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])) {
|
|
|
- $return = $this->changeDateToTimeStampAboutRange($data['order_time']);
|
|
|
- $model->where('order_time','>=',$return[0]);
|
|
|
- $model->where('order_time','<=',$return[1]);
|
|
|
- }
|
|
|
-
|
|
|
- return $model;
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderList($data,$user){
|
|
|
- $model = $this->reminderCommon($data, $user);
|
|
|
- $list = $this->limit($model,'',$data);
|
|
|
- $list = $this->fillReminderData($list,$user,$data);
|
|
|
-
|
|
|
- $list['count'] = $this->countTotal($list['data'], $user['header_default']);
|
|
|
-
|
|
|
- return [true, $list];
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderRule(&$data, $user, $is_add = true){
|
|
|
- if(empty($data['order_id'])) return [false, '订单ID不能为空'];
|
|
|
- if(empty($data['order_no'])) return [false, '订单号不能为空'];
|
|
|
- if(empty($data['order_time'])) return [false, '订单日期不能为空'];
|
|
|
- $data['order_time'] = strtotime($data['order_time']);
|
|
|
- if(empty($data['product_code'])) return [false, '物料编码不能为空'];
|
|
|
- if(empty($data['product_title'])) return [false, '物料名称不能为空'];
|
|
|
- if(empty($data['quantity'])) return [false, '物料数量不能为空'];
|
|
|
- if(empty($data['lt_arrived_time'])) return [false, 'LT要求到货日期不能为空'];
|
|
|
- $data['lt_arrived_time'] = $this->changeDateToDate($data['lt_arrived_time']);
|
|
|
- if(empty($data['tl_arrived_time'])) return [false, '提拉到货日期不能为空'];
|
|
|
- $data['tl_arrived_time'] = $this->changeDateToDate($data['tl_arrived_time']);
|
|
|
- $res = $this->checkNumber($data['tl_quantity'],2,'positive');
|
|
|
- if(! $res['valid']) return [false,'提拉到货数量:' . $res['error']];
|
|
|
- if(empty($data['df_type'])) return [false, '要求答复时间不能为空'];
|
|
|
- if(! isset(Device::$df_type_name[$data['df_type']])) return [false, '要求答复时间错误'];
|
|
|
- if(empty($data['rule'])) return [false, '主要人员关系规则不能为空'];
|
|
|
- if(! isset(Device::$rule_name[$data['rule']])) return [false, '主要人员关系规则不存在'];
|
|
|
- if(empty($data['details'])) return [false, '催单提醒人员不能为空'];
|
|
|
-
|
|
|
- $is_bool = false;
|
|
|
- $main_ids = [];
|
|
|
- $sub_ids = [];
|
|
|
-
|
|
|
- foreach ($data['details'] as $value) {
|
|
|
- if (empty($value['customer_supply_id'])) return [false, '人员id不能为空'];
|
|
|
- if (empty($value['is_main'])) return [false, 'is_main不能为空'];
|
|
|
-
|
|
|
- $customer_id = $value['customer_supply_id'];
|
|
|
- $is_main_val = $value['is_main'];
|
|
|
-
|
|
|
- if ($is_main_val == ReminderDetails::is_main_one) {
|
|
|
- // 检查主要人员重复
|
|
|
- if (in_array($customer_id, $main_ids)) {
|
|
|
- return [false, "主要成员不能重复"];
|
|
|
- }
|
|
|
- $main_ids[] = $customer_id;
|
|
|
- $is_bool = true;
|
|
|
- } elseif ($is_main_val == ReminderDetails::is_main_two) {
|
|
|
- // 检查次要人员重复
|
|
|
- if (in_array($customer_id, $sub_ids)) {
|
|
|
- return [false, "次要成员不能重复"];
|
|
|
- }
|
|
|
- $sub_ids[] = $customer_id;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 检查主要成员至少一个
|
|
|
- if (!$is_bool) return [false, '主要成员必须选择'];
|
|
|
-
|
|
|
- // 检查主要与次要是否有重复
|
|
|
- $intersect = array_intersect($main_ids, $sub_ids);
|
|
|
- if (!empty($intersect)) return [false, "主要成员与次要成员不能重复"];
|
|
|
-
|
|
|
- if($is_add){
|
|
|
-
|
|
|
- }else{
|
|
|
- if(empty($data['id'])) return [false,'ID不能为空'];
|
|
|
- $order = Device::where('id',$data['id'])
|
|
|
- ->where('del_time',0)
|
|
|
- ->first();
|
|
|
- if(empty($order)) return [false, '催单不存在或已被删除'];
|
|
|
- $order = $order->toArray();
|
|
|
- if($order['status'] > Device::status_zero) return [false, '催单状态已变更,编辑失败'];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, $data];
|
|
|
- }
|
|
|
-
|
|
|
- public function fillReminderData($data, $user, $search){
|
|
|
- if(empty($data['data'])) return $data;
|
|
|
-
|
|
|
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
|
|
|
- foreach ($data['data'] as $key => $value){
|
|
|
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
- $data['data'][$key]['order_time'] = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
|
|
|
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
|
|
|
-
|
|
|
- $data['data'][$key]['lt_arrived_time'] = $value['lt_arrived_time'] ? date("Y-m-d",$value['lt_arrived_time']): '';
|
|
|
- $data['data'][$key]['tl_arrived_time'] = $value['tl_arrived_time'] ? date("Y-m-d",$value['tl_arrived_time']): '';
|
|
|
- $data['data'][$key]['df_type_title'] = Device::$df_type_name[$value['df_type']] ?? "";
|
|
|
- $data['data'][$key]['rule_title'] = Device::$rule_name[$value['rule']] ?? "";
|
|
|
- $data['data'][$key]['status_title'] = Device::$status_name[$value['status']] ?? "";
|
|
|
- }
|
|
|
-
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- public function createOrderNumber(){
|
|
|
- return date('YmdHis',time()) . rand(1000,9999);
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderSendWx($data, $user){
|
|
|
- if(empty($data['id'])) return [false, 'ID不能为空'];
|
|
|
- $appid = config("wx_msg.f_appid");
|
|
|
-
|
|
|
- $reminders = Device::where('del_time',0)
|
|
|
- ->whereIn('id',$data['id'])
|
|
|
- ->get()->toArray();
|
|
|
- if(empty($reminders)) return [false,'催单数据不存在或已被删除'];
|
|
|
- if(count($reminders) >= 10) return [false, '批量操作一次最多10条'];
|
|
|
-
|
|
|
- $details = ReminderDetails::from('reminder_details as a')
|
|
|
- ->leftjoin('wx_employee_official as b','b.employee_id','a.customer_supply_id')
|
|
|
- ->where('a.del_time',0)
|
|
|
- ->where('b.type',WxEmployeeOfficial::login_type_one)
|
|
|
- ->where('b.appid',$appid)
|
|
|
- ->whereIn('a.reminder_id', array_column($reminders,'id'))
|
|
|
- ->select('a.reminder_id','a.customer_supply_id','b.openid')
|
|
|
- ->get()->toArray();
|
|
|
- if (empty($details)) return [false, '未找到任何可发送的微信消息的供应商'];
|
|
|
-
|
|
|
- // 按 reminder_id 分组
|
|
|
- $detailMap = [];
|
|
|
- foreach ($details as $d) {
|
|
|
- if (! empty($d['openid'])) {
|
|
|
- $detailMap[$d['reminder_id']][] = [
|
|
|
- 'open_id' => $d['openid'],
|
|
|
- 'employee_id' => $d['customer_supply_id'],
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $wxService = new WxTemplateMessageService();
|
|
|
-
|
|
|
- $total = 0;
|
|
|
- $success = 0;
|
|
|
- $fail = 0;
|
|
|
- $failList = [];
|
|
|
-
|
|
|
- // ③ 循环发送每条催单的每个人
|
|
|
- foreach ($reminders as $reminder) {
|
|
|
- $reminderId = $reminder['id'];
|
|
|
- $d_array = $detailMap[$reminderId] ?? [];
|
|
|
- if (empty($d_array)) continue;
|
|
|
-
|
|
|
- $product_detail = $reminder['product_title'] . ',数量:' . $reminder['tl_quantity'];
|
|
|
- foreach ($d_array as $value) {
|
|
|
- $openid = $value['open_id'];
|
|
|
- $employee_id = $value['employee_id'];
|
|
|
- $params = [
|
|
|
- 'order_number' => $reminder['order_number'] ?? '',
|
|
|
- 'product_detail' => $product_detail ?? '暂无',
|
|
|
- 'order_no' => $reminder['order_no'] ?? '',
|
|
|
- ];
|
|
|
-
|
|
|
- // URL 可自定义跳转地址
|
|
|
- $options = [
|
|
|
- 'url' => 'https://xlkj.qingyaokeji.com/#/wxDetailGet?openid='.$openid.'&employee_id='.$employee_id.'&id='.$reminder['id'],
|
|
|
- 'openid' => $openid,
|
|
|
- ];
|
|
|
-
|
|
|
- $total++;
|
|
|
-
|
|
|
- try {
|
|
|
- // 调用微信发送
|
|
|
- [$res, $msg] = $wxService->sendTemplateMessage('supply_reminder', $params, $options);
|
|
|
-
|
|
|
- if ($res) {
|
|
|
- $success++;
|
|
|
- } else {
|
|
|
- $fail++;
|
|
|
- $failList[] = [
|
|
|
- 'reminder_id' => $reminderId,
|
|
|
- 'openid' => $openid,
|
|
|
- 'error' => $msg,
|
|
|
- ];
|
|
|
- }
|
|
|
- } catch (\Exception $e) {
|
|
|
- $fail++;
|
|
|
- $failList[] = [
|
|
|
- 'reminder_id' => $reminderId,
|
|
|
- 'openid' => $openid,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // ④ 结果统计返回
|
|
|
- $resultMsg = "共需发送 {$total} 条模板消息,成功 {$success} 条,失败 {$fail} 条。";
|
|
|
- if ($fail > 0) {
|
|
|
- $resultMsg .= ' 失败详情见日志。';
|
|
|
- Log::error('微信模板消息发送失败详情', $failList);
|
|
|
- }
|
|
|
-
|
|
|
- return [true, $resultMsg];
|
|
|
- }
|
|
|
- //催单管理---------------------------------------------
|
|
|
-
|
|
|
-
|
|
|
- //待办管理---------------------------------------------
|
|
|
-
|
|
|
- public function toDoEdit($data,$user){
|
|
|
- list($status,$msg) = $this->toDoRule($data, $user, false);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $model = TodoList::where('id',$data['id'])->first();
|
|
|
- $model->title = $data['title'] ?? "";
|
|
|
- $model->type = $data['type'] ?? '';
|
|
|
- $model->remind_start = $data['remind_start'] ?? '';
|
|
|
- $model->remind_interval = $data['remind_interval'] ?? 0;
|
|
|
- $model->last_remind_time = $data['last_remind_time'] ?? 0;
|
|
|
- $model->man_type = $data['man_type'] ?? '';
|
|
|
- $model->organization_name = $data['organization_name'] ?? '';
|
|
|
- $model->contact = $data['contact'] ?? '';
|
|
|
- $model->address = $data['address'] ?? '';
|
|
|
- $model->content = $data['content'] ?? '';
|
|
|
- $model->save();
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoAdd($data,$user){
|
|
|
- list($status,$msg) = $this->toDoRule($data, $user);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $model = new TodoList();
|
|
|
- $model->title = $data['title'] ?? "";
|
|
|
- $model->type = $data['type'] ?? '';
|
|
|
- $model->remind_start = $data['remind_start'] ?? '';
|
|
|
- $model->remind_interval = $data['remind_interval'] ?? 0;
|
|
|
- $model->last_remind_time = $data['last_remind_time'] ?? 0;
|
|
|
- $model->man_type = $data['man_type'] ?? '';
|
|
|
- $model->organization_name = $data['organization_name'] ?? '';
|
|
|
- $model->contact = $data['contact'] ?? '';
|
|
|
- $model->address = $data['address'] ?? '';
|
|
|
- $model->content = $data['content'] ?? '';
|
|
|
- $model->crt_id = $user['id'];
|
|
|
- $model->save();
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoDel($data){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
-
|
|
|
-// $bool = TodoList::where('del_time',0)
|
|
|
-// ->whereIn('id',$data['id'])
|
|
|
-// ->where('status','>',TodoList::status_zero)
|
|
|
-// ->exists();
|
|
|
-// if($bool) return [false, '待办状态已变更,删除失败'];
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
- $time = time();
|
|
|
-
|
|
|
- TodoList::where('del_time',0)
|
|
|
- ->whereIn('id',$data['id'])
|
|
|
- ->update(['del_time' => $time]);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false,$exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoDetail($data, $user){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
- $customer = TodoList::where('del_time',0)
|
|
|
- ->where('id',$data['id'])
|
|
|
- ->first();
|
|
|
- if(empty($customer)) return [false,'待办事项不存在或已被删除'];
|
|
|
- $customer = $customer->toArray();
|
|
|
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
|
|
|
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
- $customer['remind_start'] = $customer['remind_start'] ? date("Y-m-d H:i",$customer['remind_start']): '';
|
|
|
- $customer['type_title'] = TodoList::$type_name[$customer['type']] ?? "";
|
|
|
- $customer['status_title'] = TodoList::$status_name[$customer['status']] ?? "";
|
|
|
-
|
|
|
- return [true, $customer];
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoCommon($data,$user, $field = []){
|
|
|
- if(empty($field)) $field = TodoList::$field;
|
|
|
-
|
|
|
- $model = TodoList::Clear($user,$data);
|
|
|
- $model = $model->where('del_time',0)
|
|
|
- ->select($field)
|
|
|
- ->orderby('id', 'desc');
|
|
|
-
|
|
|
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
- if(! empty($data['id'])) {
|
|
|
- if(! is_array($data['id'])) $data['id'] = [$data['id']];
|
|
|
- $model->whereIn('id', $data['id']);
|
|
|
- }
|
|
|
- if(isset($data['status'])) $model->where('status', $data['status']);
|
|
|
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
- $model->where('crt_time','>=',$return[0]);
|
|
|
- $model->where('crt_time','<=',$return[1]);
|
|
|
- }
|
|
|
- if(! empty($data['remind_start'][0]) && ! empty($data['remind_start'][1])) {
|
|
|
- $return = $this->changeDateToTimeStampAboutRange($data['remind_start']);
|
|
|
- $model->where('remind_start','>=',$return[0]);
|
|
|
- $model->where('remind_start','<=',$return[1]);
|
|
|
- }
|
|
|
-
|
|
|
- return $model;
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoList($data,$user){
|
|
|
- $model = $this->toDoCommon($data, $user);
|
|
|
- $list = $this->limit($model,'',$data);
|
|
|
- $list = $this->fillToDoData($list,$user,$data);
|
|
|
-
|
|
|
- return [true, $list];
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoRule(&$data, $user, $is_add = true){
|
|
|
- if(empty($data['title'])) return [false, '待办标题不能为空'];
|
|
|
- if(empty($data['type'])) return [false, '间隔时长不能为空'];
|
|
|
- if(! isset(TodoList::$type_value[$data['type']])) return [false, '间隔时长错误'];
|
|
|
- $data['remind_interval'] = TodoList::$type_value[$data['type']];
|
|
|
- if(empty($data['remind_start'])) return [false, '提醒开始时间不能为空'];
|
|
|
- $data['remind_start'] = $this->changeDateToDateMin($data['remind_start']);
|
|
|
- $now = strtotime(date("Y-m-d H:i"));
|
|
|
- if ($data['remind_start'] - $now < 300) return [false, '提醒开始时间必须至少比当前时间晚5分钟'];
|
|
|
-
|
|
|
-// if(empty($data['man_type'])) return [false, '拜访人员类型不能为空'];
|
|
|
-// if(empty($data['organization_name'])) return [false, '组织名称不能为空'];
|
|
|
-// if(empty($data['contact'])) return [false, '联系人不能为空'];
|
|
|
-// if(empty($data['address'])) return [false, '地点不能为空'];
|
|
|
- if(empty($data['content'])) return [false, '内容不能为空'];
|
|
|
-
|
|
|
- if($is_add){
|
|
|
-
|
|
|
- }else{
|
|
|
- if(empty($data['id'])) return [false,'ID不能为空'];
|
|
|
- $order = TodoList::where('id',$data['id'])
|
|
|
- ->where('del_time',0)
|
|
|
- ->first();
|
|
|
- if(empty($order)) return [false, '待办事项不存在或已被删除'];
|
|
|
- $order = $order->toArray();
|
|
|
- if($order['status'] > TodoList::status_zero) return [false, '待办事项状态已变更,编辑失败'];
|
|
|
- if ($order['remind_start'] != $data['remind_start'] || $order['remind_interval'] != $data['remind_interval']) {
|
|
|
- $data['last_remind_time'] = 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return [true, $data];
|
|
|
- }
|
|
|
-
|
|
|
- public function toDoFinished($data, $user)
|
|
|
- {
|
|
|
- if (empty($data['id'])) return [false, 'ID不能为空'];
|
|
|
-
|
|
|
- // 确保 id 是数组
|
|
|
- $ids = is_array($data['id']) ? $data['id'] : [$data['id']];
|
|
|
- if (empty($ids)) return [false, 'ID不能为空'];
|
|
|
-
|
|
|
- // 查询待办事项
|
|
|
- $orders = TodoList::whereIn('id', $ids)
|
|
|
- ->where('del_time', 0)
|
|
|
- ->where('status', '<', TodoList::status_two)
|
|
|
- ->get();
|
|
|
-
|
|
|
- if ($orders->isEmpty()) return [false, '未找到可操作的待办事项'];
|
|
|
-
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- // 批量更新
|
|
|
- TodoList::whereIn('id', $orders->pluck('id'))
|
|
|
- ->update(['status' => TodoList::status_two]);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- } catch (\Exception $e) {
|
|
|
- DB::rollBack();
|
|
|
- return [false, $e->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- $count = count($orders);
|
|
|
- return [true, "成功完成 {$count} 条待办事项"];
|
|
|
- }
|
|
|
-
|
|
|
- public function fillToDoData($data, $user, $search){
|
|
|
- if(empty($data['data'])) return $data;
|
|
|
-
|
|
|
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
|
|
|
- foreach ($data['data'] as $key => $value){
|
|
|
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
- $data['data'][$key]['remind_start'] = $value['remind_start'] ? date('Y-m-d H:i',$value['remind_start']) : '';
|
|
|
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
|
|
|
-
|
|
|
- $data['data'][$key]['type_title'] = TodoList::$type_name[$value['type']] ?? "";
|
|
|
- $data['data'][$key]['status_title'] = TodoList::$status_name[$value['status']] ?? "";
|
|
|
- $data['data'][$key]['last_time_result'] = $value['last_time_result'] ?? "成功";
|
|
|
- }
|
|
|
-
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderDetail2($data, $user){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
- $customer = Device::where('del_time',0)
|
|
|
- ->where('id',$data['id'])
|
|
|
- ->first();
|
|
|
- if(empty($customer)) return [false,'催单不存在或已被删除'];
|
|
|
- $customer = $customer->toArray();
|
|
|
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
|
|
|
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
- $customer['order_time'] = $customer['order_time'] ? date("Y-m-d",$customer['order_time']): '';
|
|
|
- $customer['lt_arrived_time'] = $customer['lt_arrived_time'] ? date("Y-m-d",$customer['lt_arrived_time']): '';
|
|
|
- $customer['tl_arrived_time'] = $customer['tl_arrived_time'] ? date("Y-m-d",$customer['tl_arrived_time']): '';
|
|
|
- $customer['df_type_title'] = Device::$df_type_name[$customer['df_type']] ?? "";
|
|
|
- $customer['rule_title'] = Device::$rule_name[$customer['rule']] ?? "";
|
|
|
- $customer['status_title'] = Device::$status_name[$customer['status']] ?? "";
|
|
|
-
|
|
|
- $details_status = $this->getDetail1($data['id'], $user);
|
|
|
- $customer['reminder_detail_status'] = $details_status['status'];
|
|
|
-
|
|
|
- $details_s = $this->getDetailOut($data, $user);
|
|
|
- $customer['cd_details'] = $details_s;
|
|
|
-
|
|
|
- return [true, $customer];
|
|
|
- }
|
|
|
-
|
|
|
- private function getDetailOut($data, $user = []){
|
|
|
- $user_id = $user['id'] ?? 0;
|
|
|
-
|
|
|
- $record = ReminderRecord::where('del_time',0)
|
|
|
- ->where('reminder_id',$data['id'])
|
|
|
- ->when(! empty($user_id), function ($query) use ($user_id) {
|
|
|
- return $query->where('crt_id', $user_id);
|
|
|
- })
|
|
|
- ->select('id','crt_id','tl_type','crt_time','tl_result','tl_ways')
|
|
|
- ->orderby('id','desc')
|
|
|
- ->get()->toArray();
|
|
|
-
|
|
|
- $record_detail = ReminderRecordDetails::where('del_time',0)
|
|
|
- ->whereIn('reminder_record_id',array_column($record,'id'))
|
|
|
- ->select('reminder_record_id','quantity','tl_time')
|
|
|
- ->get()->toArray();
|
|
|
- $record_detail_map = [];
|
|
|
- foreach ($record_detail as $value){
|
|
|
- $record_detail_map[$value['reminder_record_id']][] = [
|
|
|
- 'quantity' => $value['quantity'],
|
|
|
- 'tl_time' => date("Y-m-d", $value['tl_time']),
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- foreach ($record as $key => $value){
|
|
|
- $record[$key]['crt_name'] = $emp_2[$value['crt_id']] ?? "";
|
|
|
- $record[$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']): '';
|
|
|
- $record[$key]['tl_type_title'] = ReminderRecord::$tl_type_name[$value['tl_type']] ?? "";
|
|
|
- $record[$key]['tl_ways_title'] = ReminderRecord::$tl_way_name[$value['tl_ways']] ?? "";
|
|
|
- $record[$key]['tl_result_title'] = ReminderRecord::$tl_result_name[$value['tl_result']] ?? "";
|
|
|
- $record[$key]['details'] = $record_detail_map[$value['id']] ?? (object)[];
|
|
|
- }
|
|
|
-
|
|
|
- return $record ?? (object)[];
|
|
|
- }
|
|
|
-
|
|
|
- public function reminderSave($data, $user){
|
|
|
- list($status, $msg) = $this->reminderSaveRule($data, $user);
|
|
|
- if(! $status) return [false, $msg];
|
|
|
-
|
|
|
- list($reminder, $reminder_details) = $msg;
|
|
|
- try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- $reminder_details->status = ReminderDetails::status_one;
|
|
|
- $reminder_details->save();
|
|
|
-
|
|
|
- $time = time();
|
|
|
- $model = new ReminderRecord();
|
|
|
- $model->reminder_id = $reminder->id;
|
|
|
- $model->tl_type = $data['tl_type'];
|
|
|
- $model->tl_result = $data['tl_result'];
|
|
|
- $model->tl_ways = $data['tl_ways'];
|
|
|
- $model->crt_id = $user['id'];
|
|
|
- $model->save();
|
|
|
-
|
|
|
- if(! empty($data['details'])){
|
|
|
- $insert = [];
|
|
|
- foreach ($data['details'] as $value){
|
|
|
- $insert[] = [
|
|
|
- 'reminder_record_id' => $model->id,
|
|
|
- 'quantity' => $value['quantity'],
|
|
|
- 'tl_time' => $value['tl_time'],
|
|
|
- 'crt_time' => $time,
|
|
|
- ];
|
|
|
- }
|
|
|
- if(! empty($insert)) ReminderRecordDetails::insert($insert);
|
|
|
- }
|
|
|
-
|
|
|
- $this->updateReminderStatus($reminder);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
- DB::rollBack();
|
|
|
- return [false, $exception->getMessage()];
|
|
|
- }
|
|
|
-
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
-
|
|
|
- private function reminderSaveRule(&$data, $user){
|
|
|
- if(empty($data['id'])) return [false, '催单id不能为空'];
|
|
|
- $reminder = Device::where('del_time',0)
|
|
|
- ->where('id', $data['id'])
|
|
|
- ->first();
|
|
|
- if(empty($reminder)) return [false, '催单不存在或已被删除'];
|
|
|
- list($status,$msg) = $this->limitingSendRequestBackgExpire("reminderSave" . $reminder->id);
|
|
|
- if(! $status) return [false, $msg];
|
|
|
- if($reminder->status == Device::status_one) return [false, '催单已完成,提交失败'];
|
|
|
- if($reminder->status == Device::status_two) return [false, '催单已结束,提交失败'];
|
|
|
- if(empty($data['tl_type'])) return [false, '提拉类型不能为空'];
|
|
|
- if(! isset(ReminderRecord::$tl_type_name[$data['tl_type']])) return [false, '提拉类型错误'];
|
|
|
- if(empty($data['tl_result'])) return [false, '提拉结果不能为空'];
|
|
|
- if(! isset(ReminderRecord::$tl_result_name[$data['tl_result']])) return [false, '提拉结果错误'];
|
|
|
- if($data['tl_result'] == ReminderRecord::tl_result_one){
|
|
|
- if(empty($data['tl_ways'])) return [false, '提拉方式不能为空'];
|
|
|
- if(! isset(ReminderRecord::$tl_way_name[$data['tl_ways']])) return [false, '提拉方式错误'];
|
|
|
- if(empty($data['details'])) return [false, '结果明细不能为空'];
|
|
|
- foreach ($data['details'] as $key => $value){
|
|
|
- $res = $this->checkNumber($value['quantity'],2,'positive');
|
|
|
- if(! $res['valid']) return [false,'提拉应答数量:' . $res['error']];
|
|
|
- if(empty($value['tl_time'])) return [false, '提拉应答日期不能为空'];
|
|
|
- $data['details'][$key]['tl_time'] = $this->changeDateToDate($value['tl_time']);
|
|
|
- }
|
|
|
- }
|
|
|
- $order = ReminderDetails::where('del_time',0)
|
|
|
- ->where('reminder_id', $data['id'])
|
|
|
- ->where('customer_supply_id', $user['id'])
|
|
|
- ->first();
|
|
|
- if(! $order) return [false, '该人员不在本次催单供应商人员内容'];
|
|
|
- if($order->status) return [false, '催单信息已回复,请勿重复操作'];
|
|
|
-
|
|
|
- return [true, [$reminder, $order]];
|
|
|
- }
|
|
|
-
|
|
|
- public function updateReminderStatus($reminder)
|
|
|
- {
|
|
|
- if (empty($reminder)) return;
|
|
|
-
|
|
|
- $reminder_id = $reminder->id;
|
|
|
-
|
|
|
- // 查询主要人员
|
|
|
- $main_users = ReminderDetails::where('reminder_id', $reminder_id)
|
|
|
- ->where('is_main', ReminderDetails::is_main_one)
|
|
|
- ->pluck('customer_supply_id')
|
|
|
- ->toArray();
|
|
|
-
|
|
|
- // 查询主要人员的最新回复结果
|
|
|
- $records = ReminderRecord::where('reminder_id', $reminder_id)
|
|
|
- ->whereIn('crt_id', $main_users)
|
|
|
- ->orderBy('id', 'desc')
|
|
|
- ->get()
|
|
|
- ->groupBy('crt_id')
|
|
|
- ->map(function ($items) {
|
|
|
- return $items->first(); // 每人最新一次回复
|
|
|
- });
|
|
|
-
|
|
|
- // 如果主要人员都还没回复,不更新
|
|
|
- if ($records->isEmpty()) return;
|
|
|
-
|
|
|
- // 统计回复结果
|
|
|
- $agree_count = $records->where('tl_result', ReminderRecord::tl_result_one)->count();
|
|
|
- $reject_count = $records->where('tl_result', ReminderRecord::tl_result_two)->count();
|
|
|
- $total_main = count($main_users);
|
|
|
-
|
|
|
- // 当前规则判断
|
|
|
- $new_status = null;
|
|
|
-
|
|
|
- if ($reminder->rule == Device::rule_one) {
|
|
|
- // 与规则:全同意 -> 1;有一人不同意 -> 2
|
|
|
- if ($reject_count > 0) {
|
|
|
- $new_status = Device::status_two; // 结束
|
|
|
- } elseif ($agree_count == $total_main) {
|
|
|
- $new_status = Device::status_one; // 完成
|
|
|
- }
|
|
|
- } elseif ($reminder->rule == Device::rule_two) {
|
|
|
- // 或规则:任意同意 -> 1;全不同意 -> 2
|
|
|
- if ($agree_count > 0) {
|
|
|
- $new_status = Device::status_one; // 完成
|
|
|
- } elseif ($reject_count == $total_main) {
|
|
|
- $new_status = Device::status_two; // 结束
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 更新状态
|
|
|
- if ($new_status !== null && $reminder->status != $new_status) {
|
|
|
- $reminder->status = $new_status;
|
|
|
- $reminder->save();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|