ProductionOrderService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 $lock_key = 'hc_production_add';
  21. public function edit($data){}
  22. public function setOrderNO(){
  23. $str = date('Ymd',time());
  24. $order_number = OrdersProductMain::where('production_no','Like','%'. $str . '%')
  25. ->max('production_no');
  26. if(empty($order_number)){
  27. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  28. $number = $str . $number;
  29. }else{
  30. $tmp = substr($order_number, -3);
  31. $tmp = $tmp + 1;
  32. //超过99999
  33. if(strlen($tmp) > 3) return '';
  34. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  35. $number = $str . $number;
  36. }
  37. return $number;
  38. }
  39. public function add($data,$user){
  40. //数据校验以及填充
  41. list($status,$msg) = $this->orderRule($data);
  42. if(!$status) return [$status,$msg];
  43. list($status,$msg) = $this->limitingSendRequestBackgNeed($this->lock_key);
  44. if(! $status) return [false, '销售订单正在生成生产订单数据,请稍后尝试!'];
  45. $insert_data = [
  46. 'data' => $data,
  47. 'user' => $user
  48. ];
  49. CommandList::insert([
  50. 'data' => json_encode($insert_data),
  51. 'function' => 'productionAdd'
  52. ]);
  53. return [true, '生成订单数据后台生成中......'];
  54. //以下用不到-----------------------------
  55. return;
  56. $production_no = $this->setOrderNO();
  57. if(empty($production_no)) return [false,'单据号生成失败!'];
  58. //工序
  59. $process_arr = $data['process_id'];
  60. try{
  61. DB::beginTransaction();
  62. $time = time();
  63. //主表数据写入
  64. OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]);
  65. //生产数据的源数据
  66. $result = $msg[0];
  67. $quantity_map = $msg[1];
  68. $boom = $process = [];
  69. foreach ($result as $key => $value){
  70. $result[$key]['process_id'] = implode(',', $process_arr);
  71. $result[$key]['production_no'] = $production_no;
  72. $result[$key]['production_quantity'] = $quantity_map[$value['sale_orders_product_id']];
  73. $result[$key]['production_time'] = $time;
  74. $result[$key]['crt_id'] = $user['id'];
  75. }
  76. OrdersProduct::insert($result);
  77. //获取上一次插入订单的所有id
  78. $last_insert_id = OrdersProduct::where('production_no',$production_no)->select('id')->get()->toArray();
  79. $last_insert_id = array_column($last_insert_id,'id');
  80. //组织process bom的数据 写入与主表的关联id
  81. $time_arr = [];
  82. foreach ($result as $key => $value){
  83. $quantity_tmp = $quantity_map[$value['sale_orders_product_id']];
  84. $time_tmp = date("Ymd", $value['out_order_no_time']);
  85. if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
  86. for ($i = 1; $i <= $quantity_tmp; $i++) {
  87. $boom[$time_tmp][] = [
  88. 'order_product_id' => $last_insert_id[$key],
  89. 'production_no' => $production_no,
  90. 'order_no' => $value['order_no'],
  91. 'out_order_no' => $value['out_order_no'],
  92. 'product_no' => $value['product_no'],
  93. 'product_title' => $value['product_title'],
  94. 'crt_time' => $time
  95. ];
  96. foreach ($process_arr as $k => $v){
  97. $process[$time_tmp][] = [
  98. 'order_product_id' => $last_insert_id[$key],
  99. 'production_no' => $production_no,
  100. 'process_id' => $v,
  101. 'order_no' => $value['order_no'],
  102. 'out_order_no' => $value['out_order_no'],
  103. 'product_no' => $value['product_no'],
  104. 'product_title' => $value['product_title'],
  105. 'crt_time' => $time,
  106. 'sort' => $k,
  107. ];
  108. }
  109. }
  110. }
  111. //反写已经生产数量
  112. $this->writeProductionQuantity($data['id']);
  113. foreach ($time_arr as $t){
  114. $boom_model = new OrdersProductBom(['channel'=> $t]);
  115. $boom_model->insert($boom[$t]);
  116. $process_model = new OrdersProductProcess(['channel' => $t]);
  117. $process_model->insert($process[$t]);
  118. }
  119. unset($boom);unset($process);
  120. DB::commit();
  121. }catch (\Exception $e){
  122. DB::rollBack();
  123. return [false,$e->getLine().':'.$e->getMessage()];
  124. }
  125. return [true, ''];
  126. //以下用不到-----------------------------
  127. }
  128. public function delBoxLock(){
  129. $this->dellimitingSendRequestBackgNeed($this->lock_key);
  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. $quantities = array_map('floatval', $data['quantity']);
  186. if(in_array(false, $data['quantity'], true) || in_array(0.0, $quantities, true) || in_array('', $data['quantity'], true)) return [false,'数量不能为空!'];
  187. $map = [];
  188. foreach ($data['id'] as $key => $value){
  189. $map[$value] = $data['quantity'][$key];
  190. }
  191. $result = SaleOrdersProduct::whereIn('id',$data['id'])
  192. ->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')
  193. ->orderBy('id','asc')
  194. ->get()->toArray();
  195. // foreach ($result as $key => $value){
  196. // $quantity_tmp = $map[$value['sale_orders_product_id']] ?? 0;
  197. // if($value['production_quantity'] + $quantity_tmp > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
  198. // unset($result[$key]['production_quantity']);//删除销售订单的已生产数量
  199. // }
  200. return [true, [$result,$map]];
  201. }
  202. public function fillData($data){
  203. if(empty($data['data'])) return $data;
  204. $sale_orders_product_id = array_unique(array_column($data['data'],'sale_orders_product_id'));
  205. $production_map = $this->getProductionOrderQuantity($sale_orders_product_id);
  206. foreach ($data['data'] as $key => $value){
  207. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  208. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  209. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  210. $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  211. $all_production = $production_map[$value['sale_orders_product_id']] ?? 0;
  212. $not_production = bcsub($value['order_quantity'] , $all_production,3);
  213. if($not_production < 0) $not_production = "0.000";
  214. $data['data'][$key]['not_production'] = $not_production;
  215. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  216. $data['data'][$key]['is_create'] = 1;
  217. }else{
  218. $data['data'][$key]['is_create'] = 0;
  219. }
  220. $data['data'][$key]['status_title'] = OrdersProduct::$status_name[$value['status']] ?? "";
  221. }
  222. return $data;
  223. }
  224. //反写已生产数量(添加)
  225. public function writeProductionQuantity($sale_orders_product_id){
  226. if(empty($sale_orders_product_id)) return;
  227. $result = OrdersProduct::where('del_time',0)
  228. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  229. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  230. ->groupby('sale_orders_product_id')
  231. ->pluck('production_quantity','sale_orders_product_id')
  232. ->toArray();
  233. if(empty($result)) return;
  234. foreach ($result as $key => $value){
  235. SaleOrdersProduct::where('id',$key)->update([
  236. 'production_quantity' => $value
  237. ]);
  238. }
  239. }
  240. //反写已生产数量(删除)
  241. public function writeProductionQuantityDEL($sale_orders_product_id){
  242. if(empty($sale_orders_product_id)) return;
  243. $result = OrdersProduct::where('del_time',0)
  244. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  245. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  246. ->groupby('sale_orders_product_id')
  247. ->pluck('production_quantity','sale_orders_product_id')
  248. ->toArray();
  249. foreach ($sale_orders_product_id as $value){
  250. $quantity = $result[$value] ?? 0;
  251. SaleOrdersProduct::where('id',$value)->update([
  252. 'production_quantity' => $quantity
  253. ]);
  254. }
  255. }
  256. //返回已生产数量
  257. public function getProductionOrderQuantity($data){
  258. if(empty($data)) return [];
  259. $result = OrdersProduct::where('del_time',0)
  260. ->whereIn('sale_orders_product_id',$data)
  261. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  262. ->groupby('sale_orders_product_id')
  263. ->pluck('production_quantity','sale_orders_product_id')
  264. ->toArray();
  265. return $result;
  266. }
  267. //销售订单的 已生产数量
  268. public function getSaleProductProductionQuantity($data){
  269. if(empty($data)) return [];
  270. $result = SaleOrdersProduct::where('del_time',0)
  271. ->whereIn('id',$data)
  272. ->select(DB::raw("sum(production_quantity) as production_quantity"),'id')
  273. ->groupby('id')
  274. ->pluck('production_quantity','id')
  275. ->toArray();
  276. return $result;
  277. }
  278. public function productionExport($data){
  279. if(empty($data['id']) || ! is_array($data['id'])) return [false, '请选择导出的数据'];
  280. $model = OrdersProduct::where('del_time',0)
  281. ->whereIn('id',$data['id'])
  282. ->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')
  283. ->orderBy('id','desc');
  284. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  285. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  286. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  287. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  288. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  289. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  290. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  291. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  292. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  293. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  294. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  295. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  296. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  297. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  298. 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]]);
  299. 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]]);
  300. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  301. if(isset($data['status'])) $model->where('status',$data['status']);
  302. $list = $model->get()->toArray();
  303. //组织数据
  304. $data = $this->fillExportData($list);
  305. //保存导出数据
  306. $name = $this->saveExportData($data);
  307. return [true,$name];
  308. }
  309. public function fillExportData($data){
  310. if(empty($data)) return $data;
  311. $sale_orders_product_id = array_unique(array_column($data,'sale_orders_product_id'));
  312. $production_map = $this->getProductionOrderQuantity($sale_orders_product_id);
  313. $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']; // 指定要导出的字段
  314. foreach ($data as $key => $value){
  315. $data[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  316. $data[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  317. $data[$key]['not_production'] = $value['order_quantity'] - ($production_map[$value['sale_orders_product_id']] ?? 0);
  318. unset($data[$key]['sale_orders_product_id']);
  319. }
  320. return $data;
  321. }
  322. public function saveExportData($data){
  323. $file_name = time(). '_' . rand(1000,9999);
  324. $filename = $file_name.'.' . 'xlsx';
  325. $headers = [
  326. ['生产订单日期','生产订单号','销售订单日期','销售订单编号','客户编码','客户名称','表头备注','产品编码','产品名称','规格型号','计量单位','订单数量','生产数量','未下生产数量','工艺/材质','工艺名称','木皮名称','加工备注','表体备注','制单人']];
  327. $bool = Excel::store(new Exports($data,'production_order',$headers),"/public/productionOrder/{$filename}", null, 'Xlsx', []);
  328. return $filename;
  329. }
  330. }