CommandService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CommandList;
  4. use App\Model\OrdersProduct;
  5. use App\Model\OrdersProductBom;
  6. use App\Model\OrdersProductMain;
  7. use App\Model\OrdersProductProcess;
  8. use Illuminate\Support\Facades\DB;
  9. class CommandService extends Service
  10. {
  11. public function getForSettle(){
  12. $command = CommandList::where('del_time',0)
  13. ->where('is_use',0)
  14. ->first();
  15. if(! $command) return;
  16. $command->is_use = 1;
  17. $command->save();
  18. if($command->function == "productionAdd"){
  19. $args = json_decode($command->data, true);
  20. $this->productionAdd($args['data'], $args['user'],$command->id);
  21. }
  22. // if($command->function == "productionDel"){
  23. // $args = json_decode($command->data, true);
  24. // $this->productionDel($args['data'],$command->id);
  25. // }
  26. }
  27. public function productionAdd($data,$user,$id){
  28. $service = new ProductionOrderService();
  29. list($status,$msg) = $service->orderRule($data);
  30. //生产数据的源数据
  31. $result = $msg[0];
  32. $quantity_map = $msg[1];
  33. if(! $status) {
  34. CommandList::where('id', $id)
  35. ->update([
  36. 'mark' => $msg,
  37. 'del_time' => 1,
  38. ]);
  39. (new ProductionOrderService())->delBoxLock();
  40. return;
  41. }
  42. $production_no = $service->setOrderNO();
  43. if(empty($production_no)) {
  44. CommandList::where('id', $id)
  45. ->update([
  46. 'mark' => '单据号生成失败',
  47. 'del_time' => 1,
  48. ]);
  49. (new ProductionOrderService())->delBoxLock();
  50. return;
  51. }
  52. //工序
  53. $process_arr = $data['process_id'];
  54. try{
  55. DB::beginTransaction();
  56. $time = time();
  57. //主表数据写入
  58. OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]);
  59. $boom = $process = $other_map = [];
  60. foreach ($result as $key => $value){
  61. $not_production_num = bcsub($value['order_quantity'], $value['production_quantity'], 3);
  62. $production_num = $quantity_map[$value['sale_orders_product_id']];
  63. if($production_num > $not_production_num) $other_map[$value['sale_orders_product_id']] = bcsub($production_num, $not_production_num, 3) * 1000;
  64. $result[$key]['process_id'] = implode(',', $process_arr);
  65. $result[$key]['production_no'] = $production_no;
  66. $result[$key]['production_quantity'] = $production_num;
  67. $result[$key]['production_time'] = $time;
  68. $result[$key]['crt_id'] = $user['id'];
  69. }
  70. OrdersProduct::insert($result);
  71. //反写销售订单中 已经生产数量
  72. $service->writeProductionQuantity($data['id']);
  73. //获取上一次插入订单的所有id
  74. $last_insert_id = OrdersProduct::where('production_no',$production_no)->select('id')->get()->toArray();
  75. $last_insert_id = array_column($last_insert_id,'id');
  76. $size = 0;
  77. //组织process bom的数据 写入与主表的关联id
  78. $time_arr = [];
  79. foreach ($result as $key => $value){
  80. $quantity_tmp = $quantity_map[$value['sale_orders_product_id']];
  81. $quantity_tmp = $quantity_tmp * 1000;
  82. $time_tmp = date("Ymd", $value['out_order_no_time']);
  83. if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
  84. $count = $other_map[$value['sale_orders_product_id']] ?? 0;
  85. for ($i = 1; $i <= $quantity_tmp; $i++) {
  86. if($size == 100){
  87. $this->batchInsertData($boom,$process,$time_arr);
  88. unset($boom);unset($process);
  89. $size = 0;
  90. echo "100条写入成功";
  91. }
  92. $boom[$time_tmp][] = [
  93. 'order_product_id' => $last_insert_id[$key],
  94. 'production_no' => $production_no,
  95. 'order_no' => $value['order_no'],
  96. 'out_order_no' => $value['out_order_no'],
  97. 'product_no' => $value['product_no'],
  98. 'product_title' => $value['product_title'],
  99. 'crt_time' => $time
  100. ];
  101. $is_more_production = 0;
  102. if($count>= $i) $is_more_production = 1;
  103. foreach ($process_arr as $k => $v){
  104. $process[$time_tmp][] = [
  105. 'order_product_id' => $last_insert_id[$key],
  106. 'production_no' => $production_no,
  107. 'process_id' => $v,
  108. 'order_no' => $value['order_no'],
  109. 'out_order_no' => $value['out_order_no'],
  110. 'product_no' => $value['product_no'],
  111. 'product_title' => $value['product_title'],
  112. 'crt_time' => $time,
  113. 'sort' => $k,
  114. 'is_more_production' => $is_more_production
  115. ];
  116. }
  117. $size ++;
  118. }
  119. }
  120. if($size > 0){
  121. $this->batchInsertData($boom, $process, $time_arr);
  122. echo "剩余 " . $size . " 条写入成功\n";
  123. }
  124. CommandList::where('id', $id)
  125. ->update([
  126. 'mark' => '',
  127. 'del_time' => time(),
  128. 'is_use' => 2
  129. ]);
  130. (new ProductionOrderService())->delBoxLock();
  131. DB::commit();
  132. }catch (\Exception $e){
  133. (new ProductionOrderService())->delBoxLock();
  134. DB::rollBack();
  135. CommandList::where('id', $id)
  136. ->update([
  137. 'mark' => $e->getLine().':'.$e->getMessage(),
  138. 'del_time' => 1,
  139. ]);
  140. }
  141. }
  142. function delList($result){
  143. if(empty($result)) return;
  144. foreach ($result as $value){
  145. $key = "productionAdd" . $value['sale_orders_product_id'];
  146. $this->dellimitingSendRequestBackgNeed($key);
  147. }
  148. }
  149. function batchInsertData(array $boom, array $process, array $time_arr)
  150. {
  151. foreach ($time_arr as $t) {
  152. if (isset($boom[$t]) && count($boom[$t]) > 0) {
  153. $boom_model = new OrdersProductBom(['channel'=> $t]);
  154. $boom_model->insert($boom[$t]);
  155. }
  156. if (isset($process[$t]) && count($process[$t]) > 0) {
  157. $process_model = new OrdersProductProcess(['channel' => $t]);
  158. $process_model->insert($process[$t]);
  159. }
  160. }
  161. }
  162. public function productionDel($data,$command_id){
  163. $id = $data['id'];
  164. try {
  165. DB::beginTransaction();
  166. //生产订单
  167. OrdersProduct::whereIn('id',$id)->update([
  168. 'del_time' => time()
  169. ]);
  170. //生产订单主表
  171. $subquery = DB::table('orders_product')
  172. ->select('production_no')
  173. ->whereIn('id',$id);
  174. $order_no = DB::table('orders_product')
  175. ->select('orders_product.production_no','orders_product.del_time')
  176. ->joinSub($subquery, 'sub', function ($join) {
  177. $join->on('orders_product.production_no', '=', 'sub.production_no');
  178. })->get()->toArray();
  179. $update_order = [];
  180. if(! empty($order_no)){
  181. $tmp = [];
  182. foreach ($order_no as $value){
  183. $value = (array)$value;
  184. if($value['del_time'] == 0){
  185. $tmp[] = $value['production_no'];
  186. }else{
  187. $update_order[] = $value['production_no'];
  188. }
  189. }
  190. $tmp = array_unique($tmp);
  191. $update_order = array_unique($update_order);
  192. $update_order = array_diff($update_order, $tmp);
  193. }
  194. if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]);
  195. //生产订单子表
  196. $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray();
  197. //销售订单里的已生产数量
  198. (new ProductionOrderService())->writeProductionQuantityDEL(array_column($message,'sale_orders_product_id'));
  199. $time = array_unique(array_column($message,'out_order_no_time'));
  200. $arr_time = [];
  201. if(! empty($time)){
  202. foreach ($time as $value){
  203. $time_tmp = date("Ymd", $value);
  204. if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp;
  205. }
  206. }
  207. if(! empty($arr_time)){
  208. foreach ($arr_time as $value){
  209. $modelBom = new OrdersProductBom(['channel'=> $value]);
  210. $modelBom->whereIn('order_product_id',$id)->update(['del_time' => time()]);
  211. $modelProcess = new OrdersProductProcess(['channel' => $value]);
  212. $modelProcess->whereIn('order_product_id',$id)->update(['del_time' => time()]);
  213. }
  214. }
  215. DB::commit();
  216. }catch (\Throwable $e){
  217. DB::rollBack();
  218. CommandList::where('id', $command_id)
  219. ->update([
  220. 'mark' => $e->getLine().':'.$e->getMessage(),
  221. 'del_time' => 1,
  222. ]);
  223. }
  224. }
  225. }