CommandService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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){dd(1);
  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. //是否自动审核
  60. $em = new EmployeeService();
  61. $auto = $em->is_auto($user, "sc_auto");
  62. $boom = $process = $other_map = [];
  63. foreach ($result as $key => $value){
  64. $not_production_num = bcsub($value['order_quantity'], $value['production_quantity'], 3);
  65. $production_num = $quantity_map[$value['sale_orders_product_id']];
  66. if($production_num > $not_production_num) $other_map[$value['sale_orders_product_id']] = bcsub($production_num, $not_production_num, 3) * 1000;
  67. $result[$key]['process_id'] = implode(',', $process_arr);
  68. $result[$key]['production_no'] = $production_no;
  69. $result[$key]['production_quantity'] = $production_num;
  70. $result[$key]['production_time'] = $time;
  71. $result[$key]['crt_id'] = $user['id'];
  72. $result[$key]['status'] = $auto;
  73. }
  74. OrdersProduct::insert($result);
  75. //反写销售订单中 已经生产数量
  76. $service->writeProductionQuantity($data['id']);
  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. $size = 0;
  81. //组织process bom的数据 写入与主表的关联id
  82. $time_arr = [];
  83. foreach ($result as $key => $value){
  84. $quantity_tmp = $quantity_map[$value['sale_orders_product_id']];
  85. $quantity_tmp = $quantity_tmp * 1000;
  86. $time_tmp = date("Ymd", $value['out_order_no_time']);
  87. if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
  88. $count = $other_map[$value['sale_orders_product_id']] ?? 0;
  89. for ($i = 1; $i <= $quantity_tmp; $i++) {
  90. if($size == 100){
  91. $this->batchInsertData($boom,$process,$time_arr);
  92. unset($boom);unset($process);
  93. $size = 0;
  94. echo "100条写入成功";
  95. }
  96. $boom[$time_tmp][] = [
  97. 'order_product_id' => $last_insert_id[$key],
  98. 'production_no' => $production_no,
  99. 'order_no' => $value['order_no'],
  100. 'out_order_no' => $value['out_order_no'],
  101. 'product_no' => $value['product_no'],
  102. 'product_title' => $value['product_title'],
  103. 'crt_time' => $time
  104. ];
  105. $is_more_production = 0;
  106. if($count>= $i) $is_more_production = 1;
  107. foreach ($process_arr as $k => $v){
  108. $process[$time_tmp][] = [
  109. 'order_product_id' => $last_insert_id[$key],
  110. 'production_no' => $production_no,
  111. 'process_id' => $v,
  112. 'order_no' => $value['order_no'],
  113. 'out_order_no' => $value['out_order_no'],
  114. 'product_no' => $value['product_no'],
  115. 'product_title' => $value['product_title'],
  116. 'crt_time' => $time,
  117. 'sort' => $k,
  118. 'is_more_production' => $is_more_production
  119. ];
  120. }
  121. $size ++;
  122. }
  123. }
  124. if($size > 0){
  125. $this->batchInsertData($boom, $process, $time_arr);
  126. echo "剩余 " . $size . " 条写入成功\n";
  127. }
  128. CommandList::where('id', $id)
  129. ->update([
  130. 'mark' => '',
  131. 'del_time' => time(),
  132. 'is_use' => 2
  133. ]);
  134. (new ProductionOrderService())->delBoxLock();
  135. DB::commit();
  136. }catch (\Exception $e){
  137. (new ProductionOrderService())->delBoxLock();
  138. DB::rollBack();
  139. CommandList::where('id', $id)
  140. ->update([
  141. 'mark' => $e->getLine().':'.$e->getMessage(),
  142. 'del_time' => 1,
  143. ]);
  144. }
  145. }
  146. function delList($result){
  147. if(empty($result)) return;
  148. foreach ($result as $value){
  149. $key = "productionAdd" . $value['sale_orders_product_id'];
  150. $this->dellimitingSendRequestBackgNeed($key);
  151. }
  152. }
  153. function batchInsertData(array $boom, array $process, array $time_arr)
  154. {
  155. foreach ($time_arr as $t) {
  156. if (isset($boom[$t]) && count($boom[$t]) > 0) {
  157. $boom_model = new OrdersProductBom(['channel'=> $t]);
  158. $boom_model->insert($boom[$t]);
  159. }
  160. if (isset($process[$t]) && count($process[$t]) > 0) {
  161. $process_model = new OrdersProductProcess(['channel' => $t]);
  162. $process_model->insert($process[$t]);
  163. }
  164. }
  165. }
  166. public function productionDel($data,$command_id){
  167. $id = $data['id'];
  168. try {
  169. DB::beginTransaction();
  170. //生产订单
  171. OrdersProduct::whereIn('id',$id)->update([
  172. 'del_time' => time()
  173. ]);
  174. //生产订单主表
  175. $subquery = DB::table('orders_product')
  176. ->select('production_no')
  177. ->whereIn('id',$id);
  178. $order_no = DB::table('orders_product')
  179. ->select('orders_product.production_no','orders_product.del_time')
  180. ->joinSub($subquery, 'sub', function ($join) {
  181. $join->on('orders_product.production_no', '=', 'sub.production_no');
  182. })->get()->toArray();
  183. $update_order = [];
  184. if(! empty($order_no)){
  185. $tmp = [];
  186. foreach ($order_no as $value){
  187. $value = (array)$value;
  188. if($value['del_time'] == 0){
  189. $tmp[] = $value['production_no'];
  190. }else{
  191. $update_order[] = $value['production_no'];
  192. }
  193. }
  194. $tmp = array_unique($tmp);
  195. $update_order = array_unique($update_order);
  196. $update_order = array_diff($update_order, $tmp);
  197. }
  198. if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]);
  199. //生产订单子表
  200. $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray();
  201. //销售订单里的已生产数量
  202. (new ProductionOrderService())->writeProductionQuantityDEL(array_column($message,'sale_orders_product_id'));
  203. $time = array_unique(array_column($message,'out_order_no_time'));
  204. $arr_time = [];
  205. if(! empty($time)){
  206. foreach ($time as $value){
  207. $time_tmp = date("Ymd", $value);
  208. if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp;
  209. }
  210. }
  211. if(! empty($arr_time)){
  212. foreach ($arr_time as $value){
  213. $modelBom = new OrdersProductBom(['channel'=> $value]);
  214. $modelBom->whereIn('order_product_id',$id)->update(['del_time' => time()]);
  215. $modelProcess = new OrdersProductProcess(['channel' => $value]);
  216. $modelProcess->whereIn('order_product_id',$id)->update(['del_time' => time()]);
  217. }
  218. }
  219. DB::commit();
  220. }catch (\Throwable $e){
  221. DB::rollBack();
  222. CommandList::where('id', $command_id)
  223. ->update([
  224. 'mark' => $e->getLine().':'.$e->getMessage(),
  225. 'del_time' => 1,
  226. ]);
  227. }
  228. }
  229. }