|
@@ -0,0 +1,306 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace App\Service;
|
|
|
|
+
|
|
|
|
+use App\Model\Dispatch;
|
|
|
|
+use App\Model\DispatchSub;
|
|
|
|
+use App\Model\FinishedOrder;
|
|
|
|
+use App\Model\FinishedOrderScrapp;
|
|
|
|
+use App\Model\FinishedOrderSub;
|
|
|
|
+use App\Model\Orders;
|
|
|
|
+use App\Model\OrdersProduct;
|
|
|
|
+use App\Model\OrdersProductBom;
|
|
|
|
+use App\Model\OrdersProductMain;
|
|
|
|
+use App\Model\OrdersProductProcess;
|
|
|
|
+use App\Model\SaleOrdersProduct;
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
+
|
|
|
|
+class DeleteOrderService extends Service
|
|
|
|
+{
|
|
|
|
+ public function del($data){
|
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'数据必须选择!'];
|
|
|
|
+ if($this->isEmpty($data,'type')) return [false,'单据类型不能为空!'];
|
|
|
|
+
|
|
|
|
+ switch ($data['type']){
|
|
|
|
+ case 1:
|
|
|
|
+ list($status,$msg) = $this->delSaleOrdersProduct($data['id']);
|
|
|
|
+ break;
|
|
|
|
+ case 2:
|
|
|
|
+ list($status,$msg) = $this->delOrdersProduct($data['id']);
|
|
|
|
+ break;
|
|
|
|
+ case 3:
|
|
|
|
+ list($status,$msg) = $this->delDispatch($data['id']);
|
|
|
|
+ break;
|
|
|
|
+ case 4:
|
|
|
|
+ list($status,$msg) = $this->delFinished($data['id']);
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ list($status,$msg) = [false,'删除失败!'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [$status,$msg];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //销售订单删除
|
|
|
|
+ public function delSaleOrdersProduct($id){
|
|
|
|
+ $bool = OrdersProduct::where('del_time',0)
|
|
|
|
+ ->whereIn('sale_orders_product_id',$id)
|
|
|
|
+ ->exists();
|
|
|
|
+ if($bool) return [false,'销售订单已生成生产订单,删除失败!'];
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+
|
|
|
|
+ //销售订单
|
|
|
|
+ SaleOrdersProduct::whereIn('id',$id)->update([
|
|
|
|
+ 'del_time' => time()
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ //内部订单号与销售订单关联表
|
|
|
|
+ $subquery = DB::table('sale_orders_product')
|
|
|
|
+ ->select('order_no')
|
|
|
|
+ ->whereIn('id',$id);
|
|
|
|
+ $order_no = DB::table('sale_orders_product')
|
|
|
|
+ ->select('order_no','del_time')
|
|
|
|
+ ->joinSub($subquery, 'sub', function ($join) {
|
|
|
|
+ $join->on('sale_orders_product.order_no', '=', 'sub.order_no');
|
|
|
|
+ })->get()->toArray();
|
|
|
|
+ $update_order = [];
|
|
|
|
+ if(! empty($order_no)){
|
|
|
|
+ $tmp = [];
|
|
|
|
+ foreach ($order_no as $value){
|
|
|
|
+ if($value['del_time'] == 0){
|
|
|
|
+ $tmp[] = $value['order_no'];
|
|
|
|
+ }else{
|
|
|
|
+ $update_order[] = $value['order_no'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $tmp = array_unique($tmp);
|
|
|
|
+ $update_order = array_unique($update_order);
|
|
|
|
+ $update_order = array_diff($update_order, $tmp);
|
|
|
|
+ }
|
|
|
|
+ if(! empty($update_order)) Orders::whereIn('order_no',$update_order)->update(['del_time' => time()]);
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ }catch (\Throwable $e){
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ return [false,$e->getMessage()];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true,''];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //生产订单删除
|
|
|
|
+ public function delOrdersProduct($id){
|
|
|
|
+ $bool = DispatchSub::where('del_time',0)
|
|
|
|
+ ->whereIn('order_product_id',$id)
|
|
|
|
+ ->exists();
|
|
|
|
+ if($bool) return [false,'生产订单已生成派工单,删除失败!'];
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+
|
|
|
|
+ //生产订单
|
|
|
|
+ OrdersProduct::whereIn('id',$id)->update([
|
|
|
|
+ 'del_time' => time()
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ //生产订单主表
|
|
|
|
+ $subquery = DB::table('orders_product')
|
|
|
|
+ ->select('production_no')
|
|
|
|
+ ->whereIn('id',$id);
|
|
|
|
+ $order_no = DB::table('orders_product')
|
|
|
|
+ ->select('production_no','del_time')
|
|
|
|
+ ->joinSub($subquery, 'sub', function ($join) {
|
|
|
|
+ $join->on('orders_product.production_no', '=', 'sub.production_no');
|
|
|
|
+ })->get()->toArray();
|
|
|
|
+ $update_order = [];
|
|
|
|
+ if(! empty($order_no)){
|
|
|
|
+ $tmp = [];
|
|
|
|
+ foreach ($order_no as $value){
|
|
|
|
+ if($value['del_time'] == 0){
|
|
|
|
+ $tmp[] = $value['production_no'];
|
|
|
|
+ }else{
|
|
|
|
+ $update_order[] = $value['production_no'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $tmp = array_unique($tmp);
|
|
|
|
+ $update_order = array_unique($update_order);
|
|
|
|
+ $update_order = array_diff($update_order, $tmp);
|
|
|
|
+ }
|
|
|
|
+ if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]);
|
|
|
|
+
|
|
|
|
+ //生产订单子表
|
|
|
|
+ $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray();
|
|
|
|
+ $time = array_unique(array_column($message,'out_order_no_time'));
|
|
|
|
+ $arr_time = [];
|
|
|
|
+ if(! empty($time)){
|
|
|
|
+ date_default_timezone_set("PRC");
|
|
|
|
+ foreach ($time as $value){
|
|
|
|
+ $time_tmp = date("Ymd", $value);
|
|
|
|
+ if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(! empty($arr_time)){
|
|
|
|
+ foreach ($arr_time as $value){
|
|
|
|
+ $modelBom = new OrdersProductBom(['channel'=> $value]);
|
|
|
|
+ $modelBom->where('order_product_id',$id)->update(['del_time' => time()]);
|
|
|
|
+
|
|
|
|
+ $modelProcess = new OrdersProductProcess(['channel' => $value]);
|
|
|
|
+ $modelProcess->where('order_product_id',$id)->update(['del_time' => time()]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //销售订单里的已生产数量
|
|
|
|
+ (new ProductionOrderService())->writeProductionQuantity(array_column($message,'sale_orders_product_id'));
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ }catch (\Throwable $e){
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ return [false,$e->getMessage()];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true,''];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //派工单删除
|
|
|
|
+ public function delDispatch($id){
|
|
|
|
+ $bool = FinishedOrderSub::where('del_time',0)
|
|
|
|
+ ->whereIn('dispatch_id',$id)
|
|
|
|
+ ->exists();
|
|
|
|
+ if($bool) return [false,'工序派工单已生成工序完工单,删除失败!'];
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+
|
|
|
|
+ //工序派工单
|
|
|
|
+ DispatchSub::whereIn('id',$id)->update([
|
|
|
|
+ 'del_time' => time()
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ //工序派工单主表
|
|
|
|
+ $subquery = DB::table('dispatch_sub')
|
|
|
|
+ ->select('dispatch_no')
|
|
|
|
+ ->whereIn('id',$id);
|
|
|
|
+ $order_no = DB::table('dispatch_sub')
|
|
|
|
+ ->select('dispatch_no','del_time')
|
|
|
|
+ ->joinSub($subquery, 'sub', function ($join) {
|
|
|
|
+ $join->on('dispatch_sub.dispatch_no', '=', 'sub.dispatch_no');
|
|
|
|
+ })->get()->toArray();
|
|
|
|
+ $update_order = [];
|
|
|
|
+ if(! empty($order_no)){
|
|
|
|
+ $tmp = [];
|
|
|
|
+ foreach ($order_no as $value){
|
|
|
|
+ if($value['del_time'] == 0){
|
|
|
|
+ $tmp[] = $value['dispatch_no'];
|
|
|
|
+ }else{
|
|
|
|
+ $update_order[] = $value['dispatch_no'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $tmp = array_unique($tmp);
|
|
|
|
+ $update_order = array_unique($update_order);
|
|
|
|
+ $update_order = array_diff($update_order, $tmp);
|
|
|
|
+ }
|
|
|
|
+ if(! empty($update_order)) Dispatch::whereIn('dispatch_no',$update_order)->update(['del_time' => time()]);
|
|
|
|
+
|
|
|
|
+ //工序表
|
|
|
|
+ $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();
|
|
|
|
+ if(! empty($message)){
|
|
|
|
+ date_default_timezone_set("PRC");
|
|
|
|
+ foreach ($message as $value){
|
|
|
|
+ $tmp_time = date('Ymd',$value['out_order_no_time']);
|
|
|
|
+ $modelProcess = new OrdersProductProcess(['channel' => $tmp_time]);
|
|
|
|
+ $modelProcess->where('order_product_id',$value['order_product_id'])
|
|
|
|
+ ->where('process_id',$value['process_id'])
|
|
|
|
+ ->where('dispatch_no',$value['dispatch_no'])
|
|
|
|
+ ->take($value['dispatch_quantity'])
|
|
|
|
+ ->update([
|
|
|
|
+ 'dispatch_no' => '',
|
|
|
|
+ 'status' => 0
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //已派工数量
|
|
|
|
+ (new DispatchService())->writeDispatchQuantity(array_column($message,'order_product_id'));
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ }catch (\Throwable $e){
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ return [false,$e->getMessage()];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true,''];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //完工单删除
|
|
|
|
+ public function delFinished($id){
|
|
|
|
+ try {
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+
|
|
|
|
+ //工序完工单
|
|
|
|
+ FinishedOrderSub::whereIn('id',$id)->update([
|
|
|
|
+ 'del_time' => time()
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ //工序完工单主表
|
|
|
|
+ $subquery = DB::table('finished_order_sub')
|
|
|
|
+ ->select('finished_no')
|
|
|
|
+ ->whereIn('id',$id);
|
|
|
|
+ $order_no = DB::table('finished_order_sub')
|
|
|
|
+ ->select('finished_no','del_time')
|
|
|
|
+ ->joinSub($subquery, 'sub', function ($join) {
|
|
|
|
+ $join->on('finished_order_sub.finished_no', '=', 'sub.finished_no');
|
|
|
|
+ })->get()->toArray();
|
|
|
|
+ $update_order = [];
|
|
|
|
+ if(! empty($order_no)){
|
|
|
|
+ $tmp = [];
|
|
|
|
+ foreach ($order_no as $value){
|
|
|
|
+ if($value['del_time'] == 0){
|
|
|
|
+ $tmp[] = $value['finished_no'];
|
|
|
|
+ }else{
|
|
|
|
+ $update_order[] = $value['finished_no'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $tmp = array_unique($tmp);
|
|
|
|
+ $update_order = array_unique($update_order);
|
|
|
|
+ $update_order = array_diff($update_order, $tmp);
|
|
|
|
+ }
|
|
|
|
+ if(! empty($update_order)) {
|
|
|
|
+ FinishedOrder::from('finished_order as a')
|
|
|
|
+ ->leftJoin('finished_order_scrapp as b','b.finished_order_id','a.id')
|
|
|
|
+ ->whereIn('a.finished_no',$update_order)
|
|
|
|
+ ->update(['a.del_time' => time(),'b.del_time' => time()]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //工序表
|
|
|
|
+ $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();
|
|
|
|
+ if(! empty($message)){
|
|
|
|
+ date_default_timezone_set("PRC");
|
|
|
|
+ foreach ($message as $value){
|
|
|
|
+ $tmp_time = date('Ymd',$value['out_order_no_time']);
|
|
|
|
+ $modelProcess = new OrdersProductProcess(['channel' => $tmp_time]);
|
|
|
|
+ $modelProcess->where('order_product_id',$value['order_product_id'])
|
|
|
|
+ ->where('process_id',$value['process_id'])
|
|
|
|
+ ->where('finished_no',$value['finished_no'])
|
|
|
|
+ ->where('dispatch_no',$value['dispatch_no'])
|
|
|
|
+ ->take($value['finished_num'])
|
|
|
|
+ ->update([
|
|
|
|
+ 'finished_no' => '',
|
|
|
|
+ 'status' => 1
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //已完工数量
|
|
|
|
+ (new FinishedOrderService())->writeFinishedQuantity(array_column($message,'sale_orders_product_id'));
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ }catch (\Throwable $e){
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ return [false,$e->getMessage()];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true,''];
|
|
|
|
+ }
|
|
|
|
+}
|