DispatchService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Dispatch;
  4. use App\Model\DispatchSub;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeTeamPermission;
  7. use App\Model\Equipment;
  8. use App\Model\OrdersProduct;
  9. use App\Model\OrdersProductProcess;
  10. use App\Model\Process;
  11. use App\Model\Team;
  12. use Illuminate\Support\Facades\DB;
  13. class DispatchService extends Service
  14. {
  15. public function edit($data){}
  16. public function setOrderNO(){
  17. $str = date('Ymd',time());
  18. $order_number = Dispatch::where('dispatch_no','Like','%'. $str . '%')
  19. ->max('dispatch_no');
  20. if(empty($order_number)){
  21. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  22. $number = $str . $number;
  23. }else{
  24. $tmp = substr($order_number, -3);
  25. $tmp = $tmp + 1;
  26. //超过99999
  27. if(strlen($tmp) > 3) return '';
  28. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  29. $number = $str . $number;
  30. }
  31. return $number;
  32. }
  33. public function add($data,$user){
  34. //数据校验以及填充
  35. list($status,$msg) = $this->orderRule($data);
  36. if(!$status) return [$status,$msg];
  37. $dispatch_no = $this->setOrderNO();
  38. try{
  39. DB::beginTransaction();
  40. //主表
  41. Dispatch::insert(['dispatch_no' => $dispatch_no,'process_id' => $data['process_id'],'equipment_id' => $data['equipment_id'], 'team_id' => $data['team_id'],'dispatch_time_start' => $data['dispatch_time'][0],'dispatch_time_end' => $data['dispatch_time'][1], 'crt_time' => time()]);
  42. date_default_timezone_set("PRC");
  43. $process_model = new OrdersProductProcess(['channel' => date('Ymd',$data['out_order_no_time'][0])]);
  44. //生产数据的源数据
  45. $result = $msg;
  46. $time = time();
  47. foreach ($result as $key => $value){
  48. $quantity_tmp = $data['quantity'][$key];
  49. $result[$key]['dispatch_no'] = $dispatch_no;
  50. $result[$key]['process_id'] = $data['process_id'];
  51. $result[$key]['equipment_id'] = $data['equipment_id'];
  52. $result[$key]['team_id'] = $data['team_id'];
  53. $result[$key]['dispatch_time_start'] = $data['dispatch_time'][0];
  54. $result[$key]['dispatch_time_end'] = $data['dispatch_time'][1];
  55. $result[$key]['dispatch_quantity'] = $quantity_tmp;
  56. $result[$key]['crt_time'] = $time;
  57. $result[$key]['crt_id'] = $user['id'];
  58. $process_model->where('order_product_id',$value['order_product_id'])
  59. ->where('process_id',$data['process_id'])
  60. ->where('dispatch_no','')
  61. ->take($quantity_tmp)
  62. ->update(['dispatch_no' => $dispatch_no]);
  63. }
  64. DispatchSub::insert($result);
  65. DB::commit();
  66. }catch (\Exception $e){
  67. DB::rollBack();
  68. return [false,$e->getLine().':'.$e->getMessage()];
  69. }
  70. return [true,'保存成功!'];
  71. }
  72. public function del($data){
  73. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  74. return [true,'删除成功'];
  75. }
  76. public function orderDetail($data){
  77. return [200,''];
  78. }
  79. public function is_same_month($timestamp1,$timestamp2){
  80. date_default_timezone_set("PRC");
  81. // 格式化时间戳为年份和月份
  82. $year1 = date('Y', $timestamp1);
  83. $month1 = date('m', $timestamp1);
  84. $year2 = date('Y', $timestamp2);
  85. $month2 = date('m', $timestamp2);
  86. if ($year1 === $year2 && $month1 === $month2) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. public function orderList($data){
  93. list($status,$msg) = $this->orderListRule($data);
  94. if(! $status) return [false, $msg];
  95. $model = OrdersProduct::where('del_time',0)
  96. ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status','crt_id')
  97. ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
  98. ->whereIn('id',$msg)
  99. ->orderBy('id','desc');
  100. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  101. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  102. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  103. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  104. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  105. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  106. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  107. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  108. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  109. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  110. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  111. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  112. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  113. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  114. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);
  115. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  116. if(isset($data['status'])) $model->where('status',$data['status']);
  117. $list = $this->limit($model,'',$data);
  118. $list = $this->fillData($list);
  119. return [true,$list];
  120. }
  121. public function orderListRule($data){
  122. if(empty($data['process_id'])) return [false, '工序必须选择!'];
  123. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期必须选择'];
  124. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  125. if(! $bool) return [false,'制单日期必须同月!'];
  126. date_default_timezone_set("PRC");
  127. $process_model = new OrdersProductProcess(['process' => date('Ymd',$data['out_order_no_time'][0])]);
  128. if(! $process_model->is_table_isset()) return [true, []];//不存在process子表 返回空数据
  129. $process_array = $process_model->where('del_time',0)
  130. ->where('process_id',$data['process_id'])
  131. ->distinct()
  132. ->select('order_product_id')
  133. ->get()->toArray();
  134. $order_product_id = array_column($process_array,'order_product_id');
  135. if(empty($order_product_id)) return [true,[]];//process子表无数据 返回空数据
  136. return [true, $order_product_id];
  137. }
  138. public function orderRule($data){
  139. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  140. if($this->isEmpty($data,'quantity')) return [false,'请填写数量!'];
  141. if(empty($data['dispatch_time'][0]) || empty($data['dispatch_time'][1])) return [false,'计划生产时间!'];
  142. if($this->isEmpty($data,'process_id')) return [false,'工序不能为空!'];
  143. if($this->isEmpty($data,'equipment_id')) return [false,'设备不能为空!'];
  144. if($this->isEmpty($data,'team_id')) return [false,'班组不能为空!'];
  145. $result = OrdersProduct::whereIn('id',$data['id'])
  146. ->select('id as order_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark')
  147. ->get()->toArray();
  148. //已派工数据
  149. $map = $this->getDispatchQuantity($data['id']);
  150. foreach ($result as $key => $value){
  151. if(isset($map[$value['order_product_id']])){
  152. if($map[$value['order_product_id']] + $data['quantity'][$key] > $value['production_quantity']) return [false,'派单数量不能大于生产订单数量'];
  153. }else{
  154. if($data['quantity'][$key] > $value['production_quantity']) return [false,'派单数量不能大于生产订单数量'];
  155. }
  156. }
  157. return [true, $result];
  158. }
  159. public function fillData($data){
  160. if(empty($data['data'])) return $data;
  161. $map = $this->getDispatchQuantity(array_column($data['data'],'id'));
  162. $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))
  163. ->pluck('emp_name','id')
  164. ->toArray();
  165. date_default_timezone_set("PRC");
  166. foreach ($data['data'] as $key => $value){
  167. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  168. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  169. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  170. $data['data'][$key]['dispatch_quantity'] = $map[$value['id']] ?? 0;
  171. $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - ($map[$value['id']] ?? 0);
  172. $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
  173. }
  174. $data['production_quantity'] = array_sum(array_column($data['data'], 'production_quantity'));
  175. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  176. $data['not_dispatch_quantity'] = array_sum(array_column($data['data'], 'not_dispatch_quantity'));
  177. return $data;
  178. }
  179. //返回已派工数量
  180. public function getDispatchQuantity($order_product_id = []){
  181. if(empty($order_product_id)) return [];
  182. $result = DispatchSub::where('del_time',0)
  183. ->whereIn("order_product_id",$order_product_id)
  184. ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
  185. ->groupby('order_product_id')
  186. ->pluck('dispatch_quantity','order_product_id')
  187. ->toArray();
  188. return $result;
  189. }
  190. public function dispatchOrderList($data){
  191. $model = DispatchSub::where('del_time',0)
  192. ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','production_quantity','dispatch_no','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','crt_time')
  193. ->orderBy('id','desc');
  194. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  195. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  196. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  197. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  198. if(! empty($data['equipment_id'])) $model->where('equipment_id',$data['equipment_id'].'%');
  199. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  200. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  201. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  202. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  203. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  204. }
  205. if(! empty($data['emp_name'])) {
  206. $team_id = Employee::from('employee as a')
  207. ->left('employee_team_permission as b','b.employee_id','a.id')
  208. ->where('a.emp_name', 'LIKE', '%'.$data['emp_name'].'%')
  209. ->select('b.team_id')
  210. ->get()->toArray();
  211. $team_id = array_column($team_id,'team_id');
  212. $model->where('team_id',$team_id ?? []);
  213. }
  214. $list = $this->limit($model,'',$data);
  215. $list = $this->fillDispatchOrderListData($list);
  216. return [true,$list];
  217. }
  218. public function fillDispatchOrderListData($data){
  219. if(empty($data['data'])) return $data;
  220. $team = EmployeeTeamPermission::from('employee_team_permission as a')
  221. ->leftJoin('employee as b','b.id','a.employee_id')
  222. ->whereIn('a.team_id',array_column($data['data'],'team_id'))
  223. ->select('b.emp_name','a.team_id')
  224. ->get()
  225. ->toArray();
  226. $team_map = [];
  227. if(! empty($team)){
  228. foreach ($team as $value){
  229. if(isset($team_map[$value['team_id']])){
  230. $team_map[$value['team_id']] .= ','. $value['emp_name'];
  231. }else{
  232. $team_map[$value['team_id']] = $value['emp_name'];
  233. }
  234. }
  235. }
  236. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  237. ->pluck('title','id')
  238. ->toArray();
  239. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  240. ->pluck('title','id')
  241. ->toArray();
  242. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  243. ->pluck('title','id')
  244. ->toArray();
  245. date_default_timezone_set("PRC");
  246. foreach ($data['data'] as $key => $value){
  247. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  248. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  249. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  250. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  251. $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
  252. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  253. $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
  254. $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
  255. }
  256. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  257. return $data;
  258. }
  259. }