| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 | <?phpnamespace App\Service;use App\Model\Dispatch;use App\Model\DispatchEmpSub;use App\Model\DispatchSub;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 = DispatchSub::where('del_time',0)            ->whereIn('id',$id)            ->where('finished_num','>',0)            ->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                        ]);                    DispatchEmpSub::where('order_product_id',$value['order_product_id'])                        ->where('dispatch_no',$value['dispatch_no'])                        ->update([                            'del_time' => time()                        ]);                }            }            //已派工数量            (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){        $result = DispatchSub::whereIn('id',$id)            ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title')            ->orderBy('id','desc')            ->get()->toArray();        try {            DB::beginTransaction();            date_default_timezone_set('PRC');            foreach ($result as $value){                SaleOrdersProduct::where('id',$value['sale_orders_product_id'])->decrement('finished_num', $value['finished_num']);                $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);                $process_model->where('order_product_id',$value['order_product_id'])                    ->where('process_id',$value['process_id'])                    ->where('dispatch_no',$value['dispatch_no'])                    ->take($value['finished_num'])                    ->update([                        'finished_time' => 0,                        'status' => 1,                        'finished_id' => 0,                        'team_id' => 0,                        'equipment_id' => 0                    ]);                $process_model->where('order_product_id',$value['order_product_id'])                    ->where('process_id',$value['process_id'])                    ->where('dispatch_no',$value['dispatch_no'])                    ->where('status',4)                    ->update([                        'del_time' => time()                    ]);            }            DispatchSub::whereIn('id',$id)->update([                'finished_num' => 0,                'waste_num' => 0            ]);            //出库单生成  TODO            DB::commit();        }catch (\Throwable $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,''];    }}
 |