DispatchService.php 16 KB

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