DispatchService.php 17 KB

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