ProductionOrderService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Service;
  3. use App\Model\OrdersProduct;
  4. use App\Model\OrdersProductBom;
  5. use App\Model\OrdersProductMain;
  6. use App\Model\OrdersProductProcess;
  7. use App\Model\SaleOrdersProduct;
  8. use App\Model\Technology;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * bom相关
  12. * @package App\Models
  13. */
  14. class ProductionOrderService extends Service
  15. {
  16. public function edit($data){}
  17. public function setOrderNO(){
  18. $str = date('Ymd',time());
  19. $order_number = OrdersProductMain::where('production_no','Like','%'. $str . '%')
  20. ->max('production_no');
  21. if(empty($order_number)){
  22. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  23. $number = $str . $number;
  24. }else{
  25. $tmp = substr($order_number, -3);
  26. $tmp = $tmp + 1;
  27. //超过99999
  28. if(strlen($tmp) > 3) return '';
  29. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  30. $number = $str . $number;
  31. }
  32. return $number;
  33. }
  34. public function add($data,$user){
  35. //数据校验以及填充
  36. list($status,$msg) = $this->orderRule($data);
  37. if(!$status) return [$status,$msg];
  38. $production_no = $this->setOrderNO();
  39. //工艺 -> 工序
  40. $technology = Technology::where('del_time',0)
  41. ->orderBy('id','desc')
  42. ->first();
  43. if(empty($technology)) return [false,'工艺不存在!'];
  44. $process_arr = explode(',',$technology->process_id);
  45. try{
  46. DB::beginTransaction();
  47. //主表数据写入
  48. OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => time(),'crt_id' => $user['id']]);
  49. //生产数据的源数据
  50. $result = $msg;
  51. $boom = $process = [];
  52. $time = time();
  53. foreach ($result as $key => $value){
  54. $quantity_tmp = $data['quantity'][$key];
  55. $result[$key]['production_no'] = $production_no;
  56. $result[$key]['production_quantity'] = $quantity_tmp;
  57. $result[$key]['production_time'] = $time;
  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. date_default_timezone_set("PRC");
  66. foreach ($result as $key => $value){
  67. $quantity_tmp = $data['quantity'][$key];
  68. $time_tmp = date("Ymd", $value['out_order_no_time']);
  69. if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
  70. for ($i = 1; $i <= $quantity_tmp; $i++) {
  71. $boom[$time_tmp][] = [
  72. 'order_product_id' => $last_insert_id[$key],
  73. 'production_no' => $production_no,
  74. 'order_no' => $value['order_no'],
  75. 'out_order_no' => $value['out_order_no'],
  76. 'product_no' => $value['product_no'],
  77. 'product_title' => $value['product_title'],
  78. 'crt_time' => $time
  79. ];
  80. foreach ($process_arr as $v){
  81. $process[$time_tmp][] = [
  82. 'order_product_id' => $last_insert_id[$key],
  83. 'production_no' => $production_no,
  84. 'process_id' => $v,
  85. 'order_no' => $value['order_no'],
  86. 'out_order_no' => $value['out_order_no'],
  87. 'product_no' => $value['product_no'],
  88. 'product_title' => $value['product_title'],
  89. 'crt_time' => $time
  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')
  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(isset($data['status'])) $model->where('status',$data['status']);
  139. $list = $this->limit($model,'',$data);
  140. $list = $this->fillData($list);
  141. return [200,$list];
  142. }
  143. public function orderRule($data){
  144. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  145. if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!'];
  146. if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!'];
  147. $result = SaleOrdersProduct::whereIn('id',$data['id'])
  148. ->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')->get()->toArray();
  149. foreach ($result as $key => $value){
  150. if($value['production_quantity'] + $data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
  151. unset($result[$key]['production_quantity']);//删除销售订单的已生产数量
  152. }
  153. return [true, $result];
  154. }
  155. public function fillData($data){
  156. if(empty($data['data'])) return $data;
  157. $sale_orders_product_id = array_unique(array_column($data['data'],'sale_orders_product_id'));
  158. $production_map = $this->getProductionOrderQuantity($sale_orders_product_id);
  159. date_default_timezone_set("PRC");
  160. foreach ($data['data'] as $key => $value){
  161. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  162. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  163. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  164. $data['data'][$key]['not_production'] = $value['order_quantity'] - ($production_map[$value['sale_orders_product_id']] ?? 0);
  165. }
  166. return $data;
  167. }
  168. //反写已生产数量
  169. public function writeProductionQuantity($sale_orders_product_id){
  170. if(empty($sale_orders_product_id)) return;
  171. $result = OrdersProduct::where('del_time',0)
  172. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  173. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  174. ->groupby('sale_orders_product_id')
  175. ->pluck('production_quantity','sale_orders_product_id')
  176. ->toArray();
  177. if(empty($result)) return;
  178. foreach ($result as $key => $value){
  179. SaleOrdersProduct::where('id',$key)->update([
  180. 'production_quantity' => $value
  181. ]);
  182. }
  183. }
  184. //返回已生产数量
  185. public function getProductionOrderQuantity($data){
  186. if(empty($data)) return [];
  187. $result = OrdersProduct::where('del_time',0)
  188. ->whereIn('sale_orders_product_id',$data)
  189. ->select(DB::raw("sum(production_quantity) as production_quantity"),'sale_orders_product_id')
  190. ->groupby('sale_orders_product_id')
  191. ->pluck('production_quantity','sale_orders_product_id')
  192. ->toArray();
  193. return $result;
  194. }
  195. }