ProductionOrderService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\Exports;
  4. use App\Model\OrdersProduct;
  5. use App\Model\OrdersProductBom;
  6. use App\Model\OrdersProductMain;
  7. use App\Model\OrdersProductProcess;
  8. use App\Model\Process;
  9. use App\Model\SaleOrdersProduct;
  10. use App\Model\Technology;
  11. use Illuminate\Support\Facades\DB;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. /**
  14. * bom相关
  15. * @package App\Models
  16. */
  17. class ProductionOrderService extends Service
  18. {
  19. public function edit($data){}
  20. public function setOrderNO(){
  21. $str = date('Ymd',time());
  22. $order_number = OrdersProductMain::where('production_no','Like','%'. $str . '%')
  23. ->max('production_no');
  24. if(empty($order_number)){
  25. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  26. $number = $str . $number;
  27. }else{
  28. $tmp = substr($order_number, -3);
  29. $tmp = $tmp + 1;
  30. //超过99999
  31. if(strlen($tmp) > 3) return '';
  32. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  33. $number = $str . $number;
  34. }
  35. return $number;
  36. }
  37. public function add($data,$user){
  38. //数据校验以及填充
  39. list($status,$msg) = $this->orderRule($data);
  40. if(!$status) return [$status,$msg];
  41. $production_no = $this->setOrderNO();
  42. if(empty($production_no)) return [false,'单据号生成失败!'];
  43. //工序
  44. $process_arr = $data['process_id'];
  45. try{
  46. DB::beginTransaction();
  47. $time = time();
  48. //主表数据写入
  49. OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]);
  50. //生产数据的源数据
  51. $result = $msg[0];
  52. $quantity_map = $msg[1];
  53. $boom = $process = [];
  54. foreach ($result as $key => $value){
  55. $result[$key]['process_id'] = implode(',', $process_arr);
  56. $result[$key]['production_no'] = $production_no;
  57. $result[$key]['production_quantity'] = $quantity_map[$value['sale_orders_product_id']];
  58. $result[$key]['production_time'] = $time;
  59. $result[$key]['crt_id'] = $user['id'];
  60. }
  61. OrdersProduct::insert($result);
  62. //获取上一次插入订单的所有id
  63. $last_insert_id = OrdersProduct::where('production_no',$production_no)->select('id')->get()->toArray();
  64. $last_insert_id = array_column($last_insert_id,'id');
  65. //组织process bom的数据 写入与主表的关联id
  66. $time_arr = [];
  67. foreach ($result as $key => $value){
  68. $quantity_tmp = $quantity_map[$value['sale_orders_product_id']];
  69. $time_tmp = date("Ymd", $value['out_order_no_time']);
  70. if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
  71. for ($i = 1; $i <= $quantity_tmp; $i++) {
  72. $boom[$time_tmp][] = [
  73. 'order_product_id' => $last_insert_id[$key],
  74. 'production_no' => $production_no,
  75. 'order_no' => $value['order_no'],
  76. 'out_order_no' => $value['out_order_no'],
  77. 'product_no' => $value['product_no'],
  78. 'product_title' => $value['product_title'],
  79. 'crt_time' => $time
  80. ];
  81. foreach ($process_arr as $k => $v){
  82. $process[$time_tmp][] = [
  83. 'order_product_id' => $last_insert_id[$key],
  84. 'production_no' => $production_no,
  85. 'process_id' => $v,
  86. 'order_no' => $value['order_no'],
  87. 'out_order_no' => $value['out_order_no'],
  88. 'product_no' => $value['product_no'],
  89. 'product_title' => $value['product_title'],
  90. 'crt_time' => $time,
  91. 'sort' => $k,
  92. ];
  93. }
  94. }
  95. }
  96. //反写已经生产数量
  97. $this->writeProductionQuantity($data['id']);
  98. foreach ($time_arr as $t){
  99. $boom_model = new OrdersProductBom(['channel'=> $t]);
  100. $boom_model->insert($boom[$t]);
  101. $process_model = new OrdersProductProcess(['channel' => $t]);
  102. $process_model->insert($process[$t]);
  103. }
  104. unset($boom);unset($process);
  105. DB::commit();
  106. }catch (\Exception $e){
  107. DB::rollBack();
  108. return [false,$e->getLine().':'.$e->getMessage()];
  109. }
  110. return [true, ''];
  111. }
  112. public function del($data){
  113. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  114. return [true,'删除成功'];
  115. }
  116. public function orderDetail($data){
  117. return [200,''];
  118. }
  119. public function orderList($data){
  120. $model = OrdersProduct::where('del_time',0)
  121. ->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','sale_orders_product_id','dispatch_complete_quantity','pre_shipment_time')
  122. ->orderBy('id','desc');
  123. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  124. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  125. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  126. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  127. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  128. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  129. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  130. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  131. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  132. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  133. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  134. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  135. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  136. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  137. 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]]);
  138. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  139. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  140. if(! empty($data['pre_shipment_time'][0]) && ! empty($data['pre_shipment_time'][1])) $model->whereBetween('pre_shipment_time',[$data['pre_shipment_time'][0],$data['pre_shipment_time'][1]]);
  141. if(isset($data['status'])) $model->where('status',$data['status']);
  142. if(isset($data['is_create'])) {
  143. if($data['is_create']){
  144. $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity');
  145. }else{
  146. $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity');
  147. }
  148. }
  149. $list = $this->limit($model,'',$data);
  150. $list = $this->fillData($list);
  151. return [200,$list];
  152. }
  153. public function orderRule(&$data){
  154. if($this->isEmpty($data,'process_id')) return [false, '工序不能为空!'];
  155. //工序
  156. $process = Process::whereIn('id', $data['process_id'])->get()->toArray();
  157. if(empty($process)) return [false,'工序不存在或已被删除!'];
  158. $process_id = [];
  159. foreach ($process as $value){
  160. if($value['del_time'] > 0) return [false, '工序:' . $value['title'] . '不存在或已被删除!'];
  161. $process_id[] = $value['id'];
  162. }
  163. $data['process_id'] = $process_id;
  164. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  165. if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!'];
  166. if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true)) return [false,'数量不能为空!'];
  167. $map = [];
  168. foreach ($data['id'] as $key => $value){
  169. $map[$value] = $data['quantity'][$key];
  170. }
  171. $result = SaleOrdersProduct::whereIn('id',$data['id'])
  172. ->select('id as sale_orders_product_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','price','pre_shipment_time')
  173. ->orderBy('id','asc')
  174. ->get()->toArray();
  175. foreach ($result as $key => $value){
  176. $quantity_tmp = $map[$value['sale_orders_product_id']] ?? 0;
  177. if($value['production_quantity'] + $quantity_tmp > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
  178. unset($result[$key]['production_quantity']);//删除销售订单的已生产数量
  179. }
  180. return [true, [$result,$map]];
  181. }
  182. public function fillData($data){
  183. if(empty($data['data'])) return $data;
  184. $sale_orders_product_id = array_unique(array_column($data['data'],'sale_orders_product_id'));
  185. $production_map = $this->getProductionOrderQuantity($sale_orders_product_id);
  186. foreach ($data['data'] as $key => $value){
  187. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  188. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  189. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  190. $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  191. $data['data'][$key]['not_production'] = $value['order_quantity'] - ($production_map[$value['sale_orders_product_id']] ?? 0);
  192. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  193. $data['data'][$key]['is_create'] = 1;
  194. }else{
  195. $data['data'][$key]['is_create'] = 0;
  196. }
  197. }
  198. return $data;
  199. }
  200. //反写已生产数量(添加)
  201. public function writeProductionQuantity($sale_orders_product_id){
  202. if(empty($sale_orders_product_id)) return;
  203. $result = OrdersProduct::where('del_time',0)
  204. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  205. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  206. ->groupby('sale_orders_product_id')
  207. ->pluck('production_quantity','sale_orders_product_id')
  208. ->toArray();
  209. if(empty($result)) return;
  210. foreach ($result as $key => $value){
  211. SaleOrdersProduct::where('id',$key)->update([
  212. 'production_quantity' => $value
  213. ]);
  214. }
  215. }
  216. //反写已生产数量(删除)
  217. public function writeProductionQuantityDEL($sale_orders_product_id){
  218. if(empty($sale_orders_product_id)) return;
  219. $result = OrdersProduct::where('del_time',0)
  220. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  221. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  222. ->groupby('sale_orders_product_id')
  223. ->pluck('production_quantity','sale_orders_product_id')
  224. ->toArray();
  225. foreach ($sale_orders_product_id as $value){
  226. $quantity = $result[$value] ?? 0;
  227. SaleOrdersProduct::where('id',$value)->update([
  228. 'production_quantity' => $quantity
  229. ]);
  230. }
  231. }
  232. //返回已生产数量
  233. public function getProductionOrderQuantity($data){
  234. if(empty($data)) return [];
  235. $result = OrdersProduct::where('del_time',0)
  236. ->whereIn('sale_orders_product_id',$data)
  237. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  238. ->groupby('sale_orders_product_id')
  239. ->pluck('production_quantity','sale_orders_product_id')
  240. ->toArray();
  241. return $result;
  242. }
  243. public function productionExport($data){
  244. if(empty($data['id']) || ! is_array($data['id'])) return [false, '请选择导出的数据'];
  245. $model = OrdersProduct::where('del_time',0)
  246. ->whereIn('id',$data['id'])
  247. ->select('production_time', 'production_no', 'out_order_no_time','out_order_no','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','sale_orders_product_id')
  248. ->orderBy('id','desc');
  249. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  250. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  251. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  252. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  253. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  254. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  255. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  256. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  257. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  258. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  259. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  260. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  261. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  262. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  263. 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]]);
  264. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  265. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  266. if(isset($data['status'])) $model->where('status',$data['status']);
  267. $list = $model->get()->toArray();
  268. //组织数据
  269. $data = $this->fillExportData($list);
  270. //保存导出数据
  271. $name = $this->saveExportData($data);
  272. return [true,$name];
  273. }
  274. public function fillExportData($data){
  275. if(empty($data)) return $data;
  276. $sale_orders_product_id = array_unique(array_column($data,'sale_orders_product_id'));
  277. $production_map = $this->getProductionOrderQuantity($sale_orders_product_id);
  278. $exportFields = ['production_time', 'production_no', 'out_order_no_time','out_order_no','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','production_quantity','not_production','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man']; // 指定要导出的字段
  279. foreach ($data as $key => $value){
  280. $data[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  281. $data[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  282. $data[$key]['not_production'] = $value['order_quantity'] - ($production_map[$value['sale_orders_product_id']] ?? 0);
  283. unset($data[$key]['sale_orders_product_id']);
  284. }
  285. return $data;
  286. }
  287. public function saveExportData($data){
  288. $file_name = time(). '_' . rand(1000,9999);
  289. $filename = $file_name.'.' . 'xlsx';
  290. $headers = [
  291. ['生产订单日期','生产订单号','销售订单日期','销售订单编号','客户编码','客户名称','表头备注','产品编码','产品名称','规格型号','计量单位','订单数量','生产数量','未下生产数量','工艺/材质','工艺名称','木皮名称','加工备注','表体备注','制单人']];
  292. $bool = Excel::store(new Exports($data,'production_order',$headers),"/public/productionOrder/{$filename}", null, 'Xlsx', []);
  293. return $filename;
  294. }
  295. }