DeleteOrderService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Dispatch;
  4. use App\Model\DispatchSub;
  5. use App\Model\FinishedOrder;
  6. use App\Model\FinishedOrderScrapp;
  7. use App\Model\FinishedOrderSub;
  8. use App\Model\Orders;
  9. use App\Model\OrdersProduct;
  10. use App\Model\OrdersProductBom;
  11. use App\Model\OrdersProductMain;
  12. use App\Model\OrdersProductProcess;
  13. use App\Model\SaleOrdersProduct;
  14. use Illuminate\Support\Facades\DB;
  15. class DeleteOrderService extends Service
  16. {
  17. public function del($data){
  18. if($this->isEmpty($data,'id')) return [false,'数据必须选择!'];
  19. if($this->isEmpty($data,'type')) return [false,'单据类型不能为空!'];
  20. switch ($data['type']){
  21. case 1:
  22. list($status,$msg) = $this->delSaleOrdersProduct($data['id']);
  23. break;
  24. case 2:
  25. list($status,$msg) = $this->delOrdersProduct($data['id']);
  26. break;
  27. case 3:
  28. list($status,$msg) = $this->delDispatch($data['id']);
  29. break;
  30. case 4:
  31. list($status,$msg) = $this->delFinished($data['id']);
  32. break;
  33. default:
  34. list($status,$msg) = [false,'删除失败!'];
  35. }
  36. return [$status,$msg];
  37. }
  38. //销售订单删除
  39. public function delSaleOrdersProduct($id){
  40. $bool = OrdersProduct::where('del_time',0)
  41. ->whereIn('sale_orders_product_id',$id)
  42. ->exists();
  43. if($bool) return [false,'销售订单已生成生产订单,删除失败!'];
  44. try {
  45. DB::beginTransaction();
  46. //销售订单
  47. SaleOrdersProduct::whereIn('id',$id)->update([
  48. 'del_time' => time()
  49. ]);
  50. //内部订单号与销售订单关联表
  51. $subquery = DB::table('sale_orders_product')
  52. ->select('order_no')
  53. ->whereIn('id',$id);
  54. $order_no = DB::table('sale_orders_product')
  55. ->select('order_no','del_time')
  56. ->joinSub($subquery, 'sub', function ($join) {
  57. $join->on('sale_orders_product.order_no', '=', 'sub.order_no');
  58. })->get()->toArray();
  59. $update_order = [];
  60. if(! empty($order_no)){
  61. $tmp = [];
  62. foreach ($order_no as $value){
  63. if($value['del_time'] == 0){
  64. $tmp[] = $value['order_no'];
  65. }else{
  66. $update_order[] = $value['order_no'];
  67. }
  68. }
  69. $tmp = array_unique($tmp);
  70. $update_order = array_unique($update_order);
  71. $update_order = array_diff($update_order, $tmp);
  72. }
  73. if(! empty($update_order)) Orders::whereIn('order_no',$update_order)->update(['del_time' => time()]);
  74. DB::commit();
  75. }catch (\Throwable $e){
  76. DB::rollBack();
  77. return [false,$e->getMessage()];
  78. }
  79. return [true,''];
  80. }
  81. //生产订单删除
  82. public function delOrdersProduct($id){
  83. $bool = DispatchSub::where('del_time',0)
  84. ->whereIn('order_product_id',$id)
  85. ->exists();
  86. if($bool) return [false,'生产订单已生成派工单,删除失败!'];
  87. try {
  88. DB::beginTransaction();
  89. //生产订单
  90. OrdersProduct::whereIn('id',$id)->update([
  91. 'del_time' => time()
  92. ]);
  93. //生产订单主表
  94. $subquery = DB::table('orders_product')
  95. ->select('production_no')
  96. ->whereIn('id',$id);
  97. $order_no = DB::table('orders_product')
  98. ->select('production_no','del_time')
  99. ->joinSub($subquery, 'sub', function ($join) {
  100. $join->on('orders_product.production_no', '=', 'sub.production_no');
  101. })->get()->toArray();
  102. $update_order = [];
  103. if(! empty($order_no)){
  104. $tmp = [];
  105. foreach ($order_no as $value){
  106. if($value['del_time'] == 0){
  107. $tmp[] = $value['production_no'];
  108. }else{
  109. $update_order[] = $value['production_no'];
  110. }
  111. }
  112. $tmp = array_unique($tmp);
  113. $update_order = array_unique($update_order);
  114. $update_order = array_diff($update_order, $tmp);
  115. }
  116. if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]);
  117. //生产订单子表
  118. $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray();
  119. $time = array_unique(array_column($message,'out_order_no_time'));
  120. $arr_time = [];
  121. if(! empty($time)){
  122. date_default_timezone_set("PRC");
  123. foreach ($time as $value){
  124. $time_tmp = date("Ymd", $value);
  125. if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp;
  126. }
  127. }
  128. if(! empty($arr_time)){
  129. foreach ($arr_time as $value){
  130. $modelBom = new OrdersProductBom(['channel'=> $value]);
  131. $modelBom->where('order_product_id',$id)->update(['del_time' => time()]);
  132. $modelProcess = new OrdersProductProcess(['channel' => $value]);
  133. $modelProcess->where('order_product_id',$id)->update(['del_time' => time()]);
  134. }
  135. }
  136. //销售订单里的已生产数量
  137. (new ProductionOrderService())->writeProductionQuantity(array_column($message,'sale_orders_product_id'));
  138. DB::commit();
  139. }catch (\Throwable $e){
  140. DB::rollBack();
  141. return [false,$e->getMessage()];
  142. }
  143. return [true,''];
  144. }
  145. //派工单删除
  146. public function delDispatch($id){
  147. $bool = FinishedOrderSub::where('del_time',0)
  148. ->whereIn('dispatch_id',$id)
  149. ->exists();
  150. if($bool) return [false,'工序派工单已生成工序完工单,删除失败!'];
  151. try {
  152. DB::beginTransaction();
  153. //工序派工单
  154. DispatchSub::whereIn('id',$id)->update([
  155. 'del_time' => time()
  156. ]);
  157. //工序派工单主表
  158. $subquery = DB::table('dispatch_sub')
  159. ->select('dispatch_no')
  160. ->whereIn('id',$id);
  161. $order_no = DB::table('dispatch_sub')
  162. ->select('dispatch_no','del_time')
  163. ->joinSub($subquery, 'sub', function ($join) {
  164. $join->on('dispatch_sub.dispatch_no', '=', 'sub.dispatch_no');
  165. })->get()->toArray();
  166. $update_order = [];
  167. if(! empty($order_no)){
  168. $tmp = [];
  169. foreach ($order_no as $value){
  170. if($value['del_time'] == 0){
  171. $tmp[] = $value['dispatch_no'];
  172. }else{
  173. $update_order[] = $value['dispatch_no'];
  174. }
  175. }
  176. $tmp = array_unique($tmp);
  177. $update_order = array_unique($update_order);
  178. $update_order = array_diff($update_order, $tmp);
  179. }
  180. if(! empty($update_order)) Dispatch::whereIn('dispatch_no',$update_order)->update(['del_time' => time()]);
  181. //工序表
  182. $message = DispatchSub::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id','dispatch_no','dispatch_quantity','order_product_id','process_id')->get()->toArray();
  183. if(! empty($message)){
  184. date_default_timezone_set("PRC");
  185. foreach ($message as $value){
  186. $tmp_time = date('Ymd',$value['out_order_no_time']);
  187. $modelProcess = new OrdersProductProcess(['channel' => $tmp_time]);
  188. $modelProcess->where('order_product_id',$value['order_product_id'])
  189. ->where('process_id',$value['process_id'])
  190. ->where('dispatch_no',$value['dispatch_no'])
  191. ->take($value['dispatch_quantity'])
  192. ->update([
  193. 'dispatch_no' => '',
  194. 'status' => 0
  195. ]);
  196. }
  197. }
  198. //已派工数量
  199. (new DispatchService())->writeDispatchQuantity(array_column($message,'order_product_id'));
  200. DB::commit();
  201. }catch (\Throwable $e){
  202. DB::rollBack();
  203. return [false,$e->getMessage()];
  204. }
  205. return [true,''];
  206. }
  207. //完工单删除
  208. public function delFinished($id){
  209. try {
  210. DB::beginTransaction();
  211. //工序完工单
  212. FinishedOrderSub::whereIn('id',$id)->update([
  213. 'del_time' => time()
  214. ]);
  215. //工序完工单主表
  216. $subquery = DB::table('finished_order_sub')
  217. ->select('finished_no')
  218. ->whereIn('id',$id);
  219. $order_no = DB::table('finished_order_sub')
  220. ->select('finished_no','del_time')
  221. ->joinSub($subquery, 'sub', function ($join) {
  222. $join->on('finished_order_sub.finished_no', '=', 'sub.finished_no');
  223. })->get()->toArray();
  224. $update_order = [];
  225. if(! empty($order_no)){
  226. $tmp = [];
  227. foreach ($order_no as $value){
  228. if($value['del_time'] == 0){
  229. $tmp[] = $value['finished_no'];
  230. }else{
  231. $update_order[] = $value['finished_no'];
  232. }
  233. }
  234. $tmp = array_unique($tmp);
  235. $update_order = array_unique($update_order);
  236. $update_order = array_diff($update_order, $tmp);
  237. }
  238. if(! empty($update_order)) {
  239. FinishedOrder::from('finished_order as a')
  240. ->leftJoin('finished_order_scrapp as b','b.finished_order_id','a.id')
  241. ->whereIn('a.finished_no',$update_order)
  242. ->update(['a.del_time' => time(),'b.del_time' => time()]);
  243. }
  244. //工序表
  245. $message = FinishedOrderSub::whereIn('id',$id)->select('out_order_no_time','finished_no','finished_num','order_product_id','process_id','dispatch_no','sale_orders_product_id')->get()->toArray();
  246. if(! empty($message)){
  247. date_default_timezone_set("PRC");
  248. foreach ($message as $value){
  249. $tmp_time = date('Ymd',$value['out_order_no_time']);
  250. $modelProcess = new OrdersProductProcess(['channel' => $tmp_time]);
  251. $modelProcess->where('order_product_id',$value['order_product_id'])
  252. ->where('process_id',$value['process_id'])
  253. ->where('finished_no',$value['finished_no'])
  254. ->where('dispatch_no',$value['dispatch_no'])
  255. ->take($value['finished_num'])
  256. ->update([
  257. 'finished_no' => '',
  258. 'status' => 1
  259. ]);
  260. }
  261. }
  262. //已完工数量
  263. (new FinishedOrderService())->writeFinishedQuantity(array_column($message,'sale_orders_product_id'));
  264. DB::commit();
  265. }catch (\Throwable $e){
  266. DB::rollBack();
  267. return [false,$e->getMessage()];
  268. }
  269. return [true,''];
  270. }
  271. }