ProductionOrderService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Service;
  3. use App\Model\OrdersProduct;
  4. use App\Model\OrdersProductBom;
  5. use App\Model\OrdersProductProcess;
  6. use App\Model\Process;
  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 = OrdersProduct::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. $result = $msg;
  49. $boom = $process = [];
  50. $time = time();
  51. foreach ($result as $key => $value){
  52. $quantity_tmp = $data['quantity'][$key];
  53. $result[$key]['production_no'] = $production_no;
  54. $result[$key]['production_quantity'] = $quantity_tmp;
  55. $result[$key]['production_time'] = $time;
  56. for ($i = 1; $i <= $quantity_tmp; $i++) {
  57. $boom[$value['order_no']][] = [
  58. 'production_no' => $production_no,
  59. 'order_no' => $value['order_no'],
  60. 'out_order_no' => $value['out_order_no'],
  61. 'product_no' => $value['product_no'],
  62. 'product_title' => $value['product_title'],
  63. 'crt_time' => $time
  64. ];
  65. foreach ($process_arr as $v){
  66. $process[$value['order_no']][] = [
  67. 'production_no' => $production_no,
  68. 'process_id' => $v,
  69. 'order_no' => $value['order_no'],
  70. 'out_order_no' => $value['out_order_no'],
  71. 'product_no' => $value['product_no'],
  72. 'product_title' => $value['product_title'],
  73. 'crt_time' => $time
  74. ];
  75. }
  76. }
  77. }
  78. OrdersProduct::insert($result);
  79. foreach ($boom as $key => $value){
  80. $boom_model = new OrdersProductBom(['channel'=> $key]);
  81. $boom_model->insert($value);
  82. }unset($boom);
  83. foreach ($process as $key => $value){
  84. $process_model = new OrdersProductProcess(['channel' => $key]);
  85. $process_model->insert($value);
  86. }unset($process);
  87. DB::commit();
  88. }catch (\Exception $e){
  89. DB::rollBack();
  90. return [false,$e->getLine().':'.$e->getMessage()];
  91. }
  92. return [true,'保存成功!'];
  93. }
  94. public function del($data){
  95. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  96. return [true,'删除成功'];
  97. }
  98. public function orderDetail($data){
  99. return [200,''];
  100. }
  101. public function orderList($data){
  102. $model = OrdersProduct::where('del_time',0)
  103. ->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')
  104. ->orderBy('id','desc');
  105. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  106. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  107. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  108. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  109. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  110. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  111. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  112. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  113. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  114. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  115. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  116. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  117. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  118. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  119. 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]]);
  120. 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]]);
  121. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  122. if(isset($data['status'])) $model->where('status',$data['status']);
  123. $list = $this->limit($model,'',$data);
  124. $list = $this->fillData($list);
  125. return [200,$list];
  126. }
  127. public function orderRule($data){
  128. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  129. if($this->isEmpty($data,'quantity')) return [false,'数量不能为空!'];
  130. if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!'];
  131. $result = SaleOrdersProduct::whereIn('id',$data['id'])
  132. ->select('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')->get()->toArray();
  133. $map = $this->getProductionOrderQuantity($result);
  134. foreach ($result as $key => $value){
  135. $keys = $value['order_no'] . $value['technology_name'] . $value['wood_name'];
  136. if(isset($map[$keys])){
  137. if($map[$keys] + $data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
  138. }else{
  139. if($data['quantity'][$key] > $value['order_quantity']) return [false,'生产数量不能大于订单数量'];
  140. }
  141. }
  142. return [true, $result];
  143. }
  144. public function fillData($data){
  145. if(empty($data['data'])) return $data;
  146. $production_map = $this->getProductionOrderQuantity($data['data']);
  147. date_default_timezone_set("PRC");
  148. foreach ($data['data'] as $key => $value){
  149. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  150. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  151. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  152. $keys = $value['order_no'] . $value['technology_name'] . $value['wood_name'];
  153. $data['data'][$key]['not_production'] = $value['order_quantity'] - ($production_map[$keys] ?? 0);
  154. }
  155. return $data;
  156. }
  157. //返回已生产数量
  158. public function getProductionOrderQuantity($data){
  159. if(empty($data)) return [];
  160. $args = '';
  161. foreach ($data as $value){
  162. $args .= "(order_no = '{$value['order_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
  163. }
  164. $args = rtrim($args,'OR ');
  165. $result = OrdersProduct::where('del_time',0)
  166. ->whereRaw("($args)")
  167. ->select(DB::raw("sum(production_quantity) as production_quantity"),'order_no','technology_name','wood_name')
  168. ->groupby('order_no','technology_name','wood_name')
  169. ->get()->toArray();
  170. $map = [];
  171. foreach ($result as $value){
  172. $map[$value['order_no'] . $value['technology_name'] . $value['wood_name']] = $value['production_quantity'];
  173. }
  174. return $map;
  175. }
  176. }