DispatchService.php 14 KB

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