| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 | <?phpnamespace App\Service\Box;use App\Model\Box;use App\Model\BoxDetail;use App\Model\Header_ext;use App\Model\OrdersProduct;use App\Model\SaleOrdersProduct;use App\Model\Team;use App\Service\Service;use Illuminate\Support\Facades\DB;/** * 包装相关 * @package App\Models */class BoxService extends Service{    protected static $instance;    protected static $box_header;    protected static $box_detail_header;    protected static $box_hook;    public $lock_key = 'box';    public function __construct()    {        self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();        self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();        self::$box_hook = BoxHookService::getInstance();    }    /**     * 包装     * @param $data     * @return array     */    public function boxIn($data)    {//        if(!isset($data['params'])) return [false,'not found params'];//        $param = $data['params'];//        $param = [//            [//                'id' => 716,//                'order_no' => 716,//                'param' => [//                    '1',//                    '1'//                ],//            ], [//                'id' => 723,//                'param' => [//                    '40',//                    '40'//                ],//            ],//        ];        $ids = [];        $sale_ids = [];//        $key_list = [];        $top_order_no = $data['order_no'];        $data = $data['param'];        foreach ($data as $v) {            if($v['id'] < 0)  $sale_ids[] = -$v['id'];            else $ids[] = $v['id'];            if($v['id'] < 0) $v['id'] = -$v['id'];            $total = 0;            foreach ($v['params'] as $vv) {                $total += $vv;            }            $key_list[$v['id']] = [                'detail' => $v['params'],                'total' => $total,                'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,            ];        }        $insert = [];//        var_dump($ids);//        var_dump($sale_ids);die;        try{            DB::beginTransaction();            $key = $this->lock_key.'_'.$v['id'];            $lock_status = $this->isLock($key);            if(!$lock_status) return [false,'操作过于频繁'];            $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();//            var_dump($product_list);die;            $sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray();            $box_insert = [];            foreach ($product_list as $v) {                $num_list = $key_list[$v['id']];                $total = $num_list['total'];                $detail = $num_list['detail'];                $team_id = $num_list['team_id'];                $un_box_num = $v['dispatch_complete_quantity'] - $v['box_num'];                if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];                $ext_1 = $v['customer_no'];                $ext_2 = $v['customer_name'];                $ext_3 = $v['product_no'];                $ext_4 = $v['product_title'];                $ext_5 = $v['product_size'];                $out_order_no = $insert['out_order_no'] = $v['out_order_no'];                $top_id = $v['sale_orders_product_id'];                foreach ($detail as $vv){                    $box_insert[] = [                        'out_order_no' => $out_order_no,                        'top_id' => $top_id,                        'ext_1' => $ext_1,                        'ext_2' => $ext_2,                        'ext_3' => $ext_3,                        'ext_4' => $ext_4,                        'ext_5' => $ext_5,                        'num' => $vv,                        'box_type' => 0,                        'team_id' => $team_id,                    ];                }                SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([                    'box_num' => DB::raw('box_num + '.$total),                ]);                OrdersProduct::where('id',$v['id'])->update([                    'box_num' => DB::raw('box_num + '.$total),                ]);            }            foreach ($sale_product_list as $v) {                if($v['id'] < 0)  $v['id'] = -$v['id'];                $num_list = $key_list[$v['id']];                $total = $num_list['total'];                $detail = $num_list['detail'];                $team_id = $num_list['team_id'];                $box_detail = new BoxDetail(['channel'=>$top_order_no]);                $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('box_type',1)->sum('num');                if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];                $ext_1 = $v['customer_no'];                $ext_2 = $v['customer_name'];                $ext_3 = $v['product_no'];                $ext_4 = $v['product_title'];                $ext_5 = $v['product_size'];                $out_order_no = $insert['out_order_no'] = $v['out_order_no'];                $top_id = $v['id'];                foreach ($detail as $vv){                    $box_insert[] = [                        'out_order_no' => $out_order_no,                        'top_id' => $top_id,                        'ext_1' => $ext_1,                        'ext_2' => $ext_2,                        'ext_3' => $ext_3,                        'ext_4' => $ext_4,                        'ext_5' => $ext_5,                        'num' => $vv,                        'box_type' => 1,                        'team_id' => $team_id,                    ];                }                SaleOrdersProduct::where('id',$v['id'])->update([                    'box_num' => DB::raw('box_num + '.$total),                ]);            }            $insert['detail'] = $box_insert;            $insert['top_order_no'] = $top_order_no;//            var_dump($insert);die;            list($status,$msg) = self::$box_hook->boxInsert($insert);            if(!$status) {                $this->delLock($key);                DB::rollBack();                return [false,$msg];            }            $this->delLock($key);            DB::commit();            return [true,''];        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getLine().':'.$e->getMessage()];        }    }    /**     * 包装详情1用于包装前     * @param $data     * @return array     */    public function boxDetail($data)    {        list($status, $data) = self::$box_hook->boxDetail($data);        if (!$status) return [false, $data];        $sale_orders_product_ids = [];        foreach ($data as $v){            $sale_orders_product_ids[] = $v['top_id'];        }        $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();        $key_list = [];        foreach ($list as $v){            $key_list[$v['id']] = $v;        }        foreach ($data as &$v){            $detail =  $key_list[$v['top_id']];            $v['process'] = $detail['technology_material'];            $v['process_title'] = $detail['technology_name'];            $v['material_name'] = $detail['wood_name'];            $v['process_mark'] = $detail['process_mark'];        }        return [true, $data];    }    public function boxProductList($data){        if(!isset($data['id'])) return [false,'id not found'];        $sale_order_ids = $data['id'];        $model = SaleOrdersProduct::where('del_time',0)->wherein('id',$sale_order_ids)            ->select('id','out_order_no','order_no','customer_no','customer_name','product_no','product_title','product_size','crt_time as production_time','id as sale_orders_product_id','finished_num as dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time','order_quantity','technology_material','technology_name','wood_name','process_mark')            ->orderBy('id','desc')->get()->toArray();        $product_list = ordersProduct::wherein('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time','production_quantity','technology_material','technology_name','wood_name','process_mark')->get()->toArray();        $model_key_list = [];        foreach ($model as $v){            $model_key_list[$v['id']] = $v;        }        $product_key_num_list = [];        foreach ($product_list as $v){            if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']]))$product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [                'product_num' => 0 ,                'box_num' => 0 ,            ];            $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];            $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];            $detail = $model_key_list[$v['sale_orders_product_id']];            $detail['box_type'] = 1;            $detail['id'] = $v['id'];            $detail['box_num'] = $v['box_num'];            $detail['un_box_num'] = $v['dispatch_complete_quantity'] - $v['box_num'];            $detail['type'] = 1;            $model[] = $detail;        }        $return = [];        $product_key_list = [];        foreach ($model as $v){            if(!isset($v['box_type'])) {                $product_num = 0;                $box_num = 0;                if(isset($product_key_num_list[$v['id']])){                    foreach ($product_key_num_list[$v['id']] as $vv){                        $product_num += $vv['product_num'];                        $box_num += $vv['box_num'];                    }                }//                $product_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['product_num'] : 0;//                $box_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['box_num'] : 0;                if(($v['order_quantity'] - $product_num) === 0) continue;//                var_dump($v['order_quantity']);//                var_dump($v['order_quantity']);                $product_key_list[$v['sale_orders_product_id']] = [                    'out_order_no' => $v['out_order_no'],                    'order_no' => $v['order_no'],                    'production_time' => '未下生产',                    'customer_no' => $v['customer_no'],                    'customer_name' => $v['customer_name'],                    'product_no' => $v['product_no'],                    'product_title' => $v['product_title'],                    'product_size' => $v['product_size'],                    'id' => -$v['sale_orders_product_id'],                    'technology_material' => $v['technology_material'],                    'technology_name' => $v['technology_name'],                    'wood_name' => $v['wood_name'],                    'process_mark' => $v['process_mark'],                    'type' => '2',                    'is_box_num' => $v['box_num'] - $box_num,                    'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,                    'sale_num' => $v['order_quantity'],                ];            }else{                $return[] = [                    'id' => $v['id'],                    'technology_material' => $v['technology_material'],                    'technology_name' => $v['technology_name'],                    'wood_name' => $v['wood_name'],                    'order_no' => $v['order_no'],                    'process_mark' => -$v['process_mark'],                    'out_order_no' => $v['out_order_no'],                    'production_time' => date('Y-m-d',$v['crt_time']),                    'customer_no' => $v['customer_no'],                    'customer_name' => $v['customer_name'],                    'product_no' => $v['product_no'],                    'product_title' => $v['product_title'],                    'product_size' => $v['product_size'],                    'type' => '1',                    'is_box_num' => $v['box_num'],                    'un_box_num' => $v['un_box_num'],                    'sale_num' => $v['order_quantity'],                ];            }        }        foreach ($product_key_list as $v){            $return[] = $v;        }//        $data = [//            [//                'id' => 1,//                'out_order_no' => '销售订单号',//                'production_time' => '下生产时间',//                'customer_no' => '客户编码',//                'customer_name' => '客户名称',//                'product_no' => '产品编码',//                'product_title' => '产品名称',//                'product_size' => '产品规格',//                'type' => '1生产包装2备用包装',//                'is_box_num' => '已包装数量',//                'un_box_num' => '未包装',//                'sale_num' => '销售数量',//            ]//        ];        return [true,$return];    }    /**     * 包装详情2用于包装后     * @param $data     * @return array     */    public function boxOrderDetail($data)    {        list($status, $data) = self::$box_hook->boxDetail($data);//        var_dump($data);die;        $sale_orders_product_ids = [];        foreach ($data as $v){            $sale_orders_product_ids[] = $v['top_id'];        }        $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();        $key_list = [];        foreach ($list as $v){            $key_list[$v['id']] = $v;        }        foreach ($data as &$v){            $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];            $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];            $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];            $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];        }        if (!$status) return [false, $data];        return [true, $data];    }    /**     * 通过销售单号获取包装单     * @param $data     * @return array     */    public function boxOrderDetailByTop($data){        $top_order_no = $data['top_order_no'];        $list = new Box();        $list = $list->where('top_order_no',$top_order_no)->select('top_order_no','order_no','crt_time')->get()->toArray();        return [true,$list];    }    /**     * 通过包装单获取包装详情     * @param $data     * @return array     */    public function boxOrderDetailByOrderNo($data){        $order_nos = $data['order_nos'];        $top_order_no = $data['top_order_no'];        $list = new BoxDetail(['channel'=>$top_order_no]);        $list = $list->wherein('order_no',$order_nos)->get()->toArray();        $ids = [];        foreach ($list as $v){            $ids[] = $v['top_id'];        }        $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();        $sale_orders_key_product = [];        foreach ($sale_orders_product as $v){            $sale_orders_key_product[$v['id']] = $v;        }        $team_key_list = Team::pluck('title','id')->toArray();        foreach ($list as &$v){            $detail = $sale_orders_key_product[$v['top_id']];//            var_dump($detail);die;            $v['customer_name'] = $detail['customer_name'];            $v['technology_material'] = $detail['technology_material'];            $v['wood_name'] = $detail['wood_name'];            $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;        }        return [true,$list];    }}
 |