ProductionOrderService.php 18 KB

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