ProductionOrderService.php 19 KB

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