| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 | <?phpnamespace App\Service;use App\Model\ApplyOrder;use App\Model\ApplyOrderDetail;use App\Model\Box;use App\Model\BoxDetail;use App\Model\DispatchSub;use App\Model\Employee;use App\Model\EmployeeTeamPermission;use App\Model\InOutRecord;use App\Model\Process;use App\Model\ReportWorking;use App\Model\ReportWorkingDetail;use App\Model\SaleOrdersProduct;use App\Model\Team;use Illuminate\Support\Facades\DB;class ApplyOrderService extends Service{    public function MaterialEdit($data, $user){        list($status,$msg) = $this->MaterialRule($data,false);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ApplyOrder();            $model = $model->where('id',$data['id'])->first();            $model->apply_id = $data['apply_id'];            $model->apply_time = $data['apply_time'] ;            $model->mark = $data['mark'] ?? "";            $model->storehouse_id = $data['storehouse_id'];            $model->storehouse_title = $data['storehouse_title'] ?? "";            $model->type = $data['type'];            $model->save();            $id = $model->id;            ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([                'del_time' => $time            ]);            $detail_insert = [];            foreach ($data['order_data'] as $v){                $detail_insert[] = [                    'apply_order_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'product_no' => $v['product_no'] ?? "",                    'product_title' => $v['product_title'] ?? "",                    'product_size' => $v['product_size'] ?? "",                    'product_unit' => $v['product_unit'] ?? "",                    'technology_name' => $v['technology_name'] ?? "", //颜色                    'top_product_title' => $v['top_product_title'] ?? "",                    'top_product_no' => $v['top_product_title'] ?? "",                    'type' => $data['type'],                    'storehouse_id' => $data['storehouse_id'],                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,''];    }    public function MaterialAdd($data,$user){        list($status,$msg) = $this->MaterialRule($data);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ApplyOrder();            $model->order_number = $data['order_number'];            $model->apply_id = $data['apply_id'];            $model->apply_time = $data['apply_time'] ;            $model->mark = $data['mark'] ?? "";            $model->storehouse_id = $data['storehouse_id'];            $model->storehouse_title = $data['storehouse_title'] ?? "";            $model->type = $data['type'];            $model->crt_id = $user['id'];            $model->save();            $id = $model->id;            $detail_insert = [];            foreach ($data['order_data'] as $v){                $detail_insert[] = [                    'apply_order_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'product_no' => $v['product_no'] ?? "",                    'product_title' => $v['product_title'] ?? "",                    'product_size' => $v['product_size'] ?? "",                    'product_unit' => $v['product_unit'] ?? "",                    'technology_name' => $v['technology_name'] ?? "", //颜色                    'top_product_title' => $v['top_product_title'] ?? "",                    'top_product_no' => $v['top_product_title'] ?? "",                    'type' => $data['type'],                    'storehouse_id' => $data['storehouse_id'],                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    public function MaterialDel($data){        if($this->isEmpty($data,'id')) return [false,'ID必须!'];        $apply = ApplyOrder::where('id',$data['id'])->first();        if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];        if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];        $time = time();        try {            DB::beginTransaction();            $apply->del_time = $time;            $apply->save();            ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([                'del_time'=>time()            ]);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true,''];    }    public function MaterialList($data){        $model = ApplyOrder::where('del_time',0)            ->select('*')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(! empty($data['type'])) $model->where('type', $data['type']);        if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);        if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);        if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_no'])){            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ApplyOrderDetail::where('del_time',0)                ->where('type', $data['type'])                ->whereIn('data_id', $dispatch_id)                ->select('apply_order_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));        }        if(! empty($data['box_no'])){            $box_id = Box::where('del_time',0)                ->where('order_no', 'LIKE', '%'.$data['box_no'].'%')                ->select('id')                ->get()->toArray();            $box_id = array_column($box_id,'id');            $detail = ApplyOrderDetail::where('del_time',0)                ->where('type', $data['type'])                ->whereIn('data_id', $box_id)                ->select('apply_order_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));        }        if(! empty($data['sale_order_number'])){            if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $dispatch_id = DispatchSub::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('id')                    ->get()->toArray();                $dispatch_id = array_column($dispatch_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $dispatch_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }else{                $no = SaleOrdersProduct::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('order_no')                    ->get()->toArray();                $no = array_unique(array_column($no,'order_no'));                $box_id = Box::where('del_time',0)                    ->whereIn('top_order_no', $no)                    ->select('id')                    ->get()->toArray();                $box_id = array_column($box_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $box_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }        }        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true,$list];    }    public function fillData($data){        if(empty($data['data'])) return $data;        $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))            ->pluck('emp_name','id')            ->toArray();        $array1 = $array2 = [];        foreach ($data['data'] as $value){            if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $array1[] = $value['id'];            }else{                $array2[] = $value['id'];            }        }        $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])            ->whereIn('apply_order_id', $array1)            ->select('data_id','apply_order_id')            ->get()->toArray();        $order1_map = [];        foreach ($order1 as $value){            $order1_map[$value['apply_order_id']][] = $value['data_id'];        }        $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])            ->whereIn('apply_order_id', $array2)            ->select('data_id','apply_order_id')            ->get()->toArray();        $order2_map = [];        foreach ($order2 as $value){            $order2_map[$value['apply_order_id']][] = $value['data_id'];        }        $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))            ->pluck('out_order_no','id')            ->toArray();        $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id')))            ->pluck('top_order_no','id')            ->toArray();        $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))            ->pluck('out_order_no','order_no')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';            $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';            $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';            if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $order_tmp = $order1_map[$value['id']] ?? [];                $order_tmp = array_unique($order_tmp);                $sale_order_number = [];                foreach ($order_tmp as $v){                    $str = $map1[$v] ?? '';                    if(empty($str)) continue;                    $sale_order_number[] = $str;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data['data'][$key]['sale_order_number'] = $sale_order_number;            }else{                $order_tmp = $order2_map[$value['id']] ?? [];                $order_tmp = array_unique($order_tmp);                $sale_order_number = [];                foreach ($order_tmp as $v){                    $m2_t = $map2[$v] ?? '';                    $m2_t = $map2_fin[$m2_t] ?? "";                    if(empty($m2_t)) continue;                    $sale_order_number[] = $m2_t;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data['data'][$key]['sale_order_number'] = $sale_order_number;            }        }        return $data;    }    public function MaterialDetail($data){        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];        $id = $data['id'];        $detail = ApplyOrder::where('del_time',0)            ->where('id',$id)            ->first();        if(empty($detail)) return [false,'申请单不存在或已被删除'];        $detail = $detail->toArray();        $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');        $apply_d = ApplyOrderDetail::where('del_time', 0)            ->where('apply_order_id', $id)            ->get()->toArray();        $apply_id = array_column($apply_d, 'data_id');        $d = [];        if($detail['type'] == ApplyOrder::type_one){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($detail['type'] == ApplyOrder::type_two){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($detail['type'] == ApplyOrder::type_three){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }elseif ($detail['type'] == ApplyOrder::type_four){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }        $d_tmp = array_column($d,null,'id');        $return = [];        foreach ($apply_d as $t){            $tmp = $d_tmp[$t['data_id']] ?? [];            $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";            $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";            $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";            $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";            if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){                $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);            }else{                $order_no = $tmp['order_no'] ?? "";            }            if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){                $product_unit = "吨";            }else{                $product_unit = $t['product_unit'] ?? "";            }            $return[] = [                'id' => $t['data_id'] ?? 0,                'quantity' => $t['quantity'] ?? 0,                'product_no' => $t['product_no'] ?? "",                'product_title' => $t['product_title'] ?? "",                'product_size' => $t['product_size'] ?? "",                'product_unit' => $product_unit,                'top_product_title' => $top_product_title,                'top_product_no' => $top_product_no,                'technology_name' => $technology_name, //颜色                'wood_name' => $wood_name,                'order_no' => $order_no,            ];        }        $detail['order_data'] = $return;        return [true, $detail];    }    public function MaterialRule(&$data,$is_add = true){        if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];        if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];        if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];        if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];        if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];        foreach ($data['order_data'] as $value){            if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];            if($data['type'] != ApplyOrder::type_three){                if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];            }        }        $id = array_unique(array_column($data['order_data'],'id'));        if($data['type'] == ApplyOrder::type_one){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'dispatch_no', 'status')                ->get()->toArray();            if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_two){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'dispatch_no', 'status')                ->get()->toArray();            if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_three){            $box = Box::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'order_no', 'status')                ->get()->toArray();            if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_four){            $box = Box::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'order_no', 'status')                ->get()->toArray();            if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];        }else{            return [false, '申请单类型错误'];        }        if($is_add){            $data['order_number'] = $this->setOrderNO($data['type']);            if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];        }else{            if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];            if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];            $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();            if(empty($apply)) return [false, '申请单不存在或已被删除'];            if($apply->status != 0) return [false, '申请单已审核,编辑失败'];        }        return [true,''];    }    public function setOrderNO($type = ""){        $str = date('Ymd',time());        $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')            ->where('type', $type)            ->max('order_number');        if(empty($order_number)){            $number = str_pad(1,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }else{            $tmp = substr($order_number, -3);            $tmp = $tmp + 1;            //超过999            if(strlen($tmp) > 3) return '';            $number = str_pad($tmp,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }        return $number;    }    public function createSQ($data, $user, $type = 0, $status = 1){        if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];        if($type == ApplyOrder::type_one){            $storehouse_id = "001";            $storehouse_title = "原料仓";        }elseif ($type == ApplyOrder::type_two){            $storehouse_id = "004";            $storehouse_title = "待清洗原材料仓";        }elseif ($type == ApplyOrder::type_three){            $storehouse_id = "002";            $storehouse_title = "产成品仓";        }else{            $storehouse_id = "003";            $storehouse_title = "包材、辅料仓" ;        }        try{            DB::beginTransaction();            $order_number = $this->setOrderNO($type);//            list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);//            if(! $status) return [false, $msg];            $time = time();            $model = new ApplyOrder();            $model->order_number = $order_number;            $model->apply_id = $user['id'];            $model->apply_time = time();            $model->storehouse_id = $storehouse_id;            $model->storehouse_title = $storehouse_title;            $model->type = $type;            $model->status = $status; //是否审核            $model->save();            $id = $model->id;            $detail_insert = [];            foreach ($data as $v){                $detail_insert[] = [                    'apply_order_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'product_no' => $v['product_no'] ?? "",                    'product_title' => $v['product_title'] ?? "",                    'product_size' => $v['product_size'] ?? "",                    'product_unit' => $v['product_unit'] ?? "",                    'technology_name' => $v['technology_name'] ?? "",                    'wood_name' => $v['wood_name'] ?? "",                    'top_product_no' => $v['top_product_no'] ?? "",                    'top_product_title' => $v['top_product_title'] ?? "",                    'type' => $type,                    'storehouse_id' => $storehouse_id,                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, $id];    }    //流水    public function createRecord($id){        $record = ApplyOrderDetail::where('apply_order_id', $id)            ->where('del_time',0)            ->get()->toArray();        if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];        try{            DB::beginTransaction();            $time = time();            $insert = [];            foreach ($record as $value){                if(empty($value['product_no'])) continue;                if($value['type'] == ApplyOrder::type_one){                    $number = - $value['quantity'];                }else{                    $number = $value['quantity'];                }                $insert[] = [                    'apply_order_id' => $id,                    'product_no' => $value['product_no'],                    'number' => $number,                    'type' => $value['type'],                    'storehouse_id' => $value['storehouse_id'],                    'crt_time' => $time,                ];            }            if(! empty($insert)) InOutRecord::insert($insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    //报工    public function reportWorkingEdit($data, $user){        list($status,$msg) = $this->reportWorkingRule($data,false);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ReportWorking();            $model = $model->where('id',$data['id'])->first();            $model->report_time = $data['report_time'];            $model->mark = $data['mark']?? "";            $model->save();            $id = $model->id;            ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([                'del_time' => $time            ]);            //班组下的人            $team_man = [];            $model_t = new EmployeeTeamPermission();            $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();            foreach ($team_array as $v){                $team_man[$v['team_id']][] = $v['employee_id'];            }            $detail_insert = [];            foreach ($data['order_data'] as $v){                $t = $team_man[$v['team_id']] ?? [];                foreach ($t as $t_v){                    $detail_insert[] = [                        'report_working_id' => $id,                        'data_id' => $v['id'],                        'quantity' => $v['quantity'] ?? 0,                        'process_id' => $v['process_id'] ?? 0,                        'finished_id' => $t_v,                        'team_id' => $v['team_id'],                        'crt_time' => $time,                    ];                }            }            ReportWorkingDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,''];    }    public function reportWorkingAdd($data,$user){        list($status,$msg) = $this->reportWorkingRule($data);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ReportWorking();            $model->order_number = $data['order_number'];            $model->report_time = $data['report_time'];            $model->mark = $data['mark']?? "";            $model->crt_id = $user['id'];            $model->save();            $id = $model->id;            //班组下的人            $team_man = [];            $model_t = new EmployeeTeamPermission();            $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();            foreach ($team_array as $v){                $team_man[$v['team_id']][] = $v['employee_id'];            }            $detail_insert = [];            foreach ($data['order_data'] as $v){                $t = $team_man[$v['team_id']] ?? [];                foreach ($t as $t_v){                    $detail_insert[] = [                        'report_working_id' => $id,                        'data_id' => $v['id'],                        'quantity' => $v['quantity'] ?? 0,                        'process_id' => $v['process_id'] ?? 0,                        'finished_id' => $t_v,                        'team_id' => $v['team_id'],                        'crt_time' => $time,                    ];                }            }            ReportWorkingDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    public function reportWorkingDel($data){        if($this->isEmpty($data,'id')) return [false,'ID必须!'];        $apply = ReportWorking::where('id',$data['id'])->first();        if($apply->del_time > 0) return [false,'报工单不存在或已被删除'];        if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败'];        $time = time();        try {            DB::beginTransaction();            $apply->del_time = $time;            $apply->save();            ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([                'del_time'=>time()            ]);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true,''];    }    public function reportWorkingList($data){        $model = ReportWorking::where('del_time',0)            ->select('*')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);        if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]);        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_no'])) {            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ReportWorkingDetail::where('del_time',0)                ->whereIn('data_id', $dispatch_id)                ->select('report_working_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));        }        $list = $this->limit($model,'',$data);        $list = $this->reportWorkingfillData($list);        return [true,$list];    }    public function reportWorkingfillData($data){        if(empty($data['data'])) return $data;        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))            ->pluck('emp_name','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d H:i:s',$value['report_time']) : '';            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';            $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';        }        return $data;    }    public function reportWorkingDetail($data){        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];        $id = $data['id'];        $detail = ReportWorking::where('del_time',0)            ->where('id',$id)            ->first();        if(empty($detail)) return [false,'报工单不存在或已被删除'];        $detail = $detail->toArray();        $apply_d = ReportWorkingDetail::where('del_time', 0)            ->where('report_working_id', $id)            ->get()->toArray();        $apply_id = array_column($apply_d, 'data_id');        //派工单        $d = DispatchSub::where('del_time',0)            ->whereIn('id', $apply_id)            ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size')            ->get()->toArray();        $d_tmp = array_column($d,null,'id');        $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))            ->pluck('title','id')            ->toArray();        $team_map = Team::pluck('title','id')->toArray();        $return = [];        foreach ($apply_d as $t){            $tmp = $d_tmp[$t['data_id']] ?? [];            $str = $t['data_id'] . $t['process_id'];            if(! isset($return[$str])){                $return[$str] = [                    'id' => $t['data_id'] ?? 0,                    'quantity' => $t['quantity'] ?? 0,                    'product_no' => $tmp['product_no'] ?? "",                    'product_title' => $tmp['product_title'] ?? "",                    'product_size' => $tmp['product_size'] ?? "",                    'product_unit' => $tmp['product_unit'] ?? "",                    'technology_name' => $tmp['technology_name'] ?? "",                    'wood_name' => $tmp['wood_name'] ?? "",                    'order_no' => $tmp['order_no'] ?? "",                    'process_id' => $tmp['process_id'] ?? 0,                    'process_name' => $process_map[$tmp['process_id']] ?? "",                    'team_id' => $t['team_id'] ?? 0,                    'team_title' => $team_map[$t['team_id']] ?? '',                ];            }        }        $detail['order_data'] = array_values($return);        return [true, $detail];    }    public function reportWorkingRule(&$data,$is_add = true){        if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];        if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];        foreach ($data['order_data'] as $value){            if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];            if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];            if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];            if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];        }        $id = array_unique(array_column($data['order_data'],'id'));        $dispatch = DispatchSub::where('del_time',0)            ->whereIn('id', $id)            ->select('id', 'dispatch_no', 'status')            ->get()->toArray();        if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];        if($is_add){            $data['order_number'] = $this->setOrderNO2();            if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];        }else{            if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];            if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];            $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();            if(empty($apply)) return [false, '报工单不存在或已被删除'];            if($apply->status != 0) return [false, '报工单已审核,编辑失败'];        }        return [true,''];    }    public function setOrderNO2(){        $str = date('Ymd',time());        $model = new ReportWorking();        $order_number = $model->where('order_number','Like','%'. $str . '%')            ->max('order_number');        if(empty($order_number)){            $number = str_pad(1,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }else{            $tmp = substr($order_number, -3);            $tmp = $tmp + 1;            //超过999            if(strlen($tmp) > 3) return '';            $number = str_pad($tmp,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }        return $number;    }    public function applyOrderReportList($data){        if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];        if(empty($data['type'])) return [false, 'TYPE不能为空!'];        $model = ApplyOrder::where('del_time',0)            ->where('apply_time',">=",$data['apply_time'][0])            ->where('apply_time',"<=",$data['apply_time'][1])            ->where('type',$data['type'])            ->select('id','order_number','apply_time','mark','status')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){            $model->where('crt_time',">=",$data['crt_time'][0]);            $model->where('crt_time',"<=",$data['crt_time'][1]);        }        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');        if(! empty($data['sale_order_number'])){            if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $dispatch_id = DispatchSub::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('id')                    ->get()->toArray();                $dispatch_id = array_column($dispatch_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $dispatch_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }else{                $no = SaleOrdersProduct::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('order_no')                    ->get()->toArray();                $no = array_unique(array_column($no,'order_no'));                $box_id = Box::where('del_time',0)                    ->whereIn('top_order_no', $no)                    ->select('id')                    ->get()->toArray();                $box_id = array_column($box_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $box_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }        }        if(! empty($data['dispatch_no'])){            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ApplyOrderDetail::where('del_time',0)                ->where('type', $data['type'])                ->whereIn('data_id', $dispatch_id)                ->select('apply_order_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));        }        $list = $model->get()->toArray();        if(empty($list)) return [true, []];        $apply_id = array_column($list, 'id');        //明细数据        $result = ApplyOrderDetail::where('del_time',0)            ->whereIn('apply_order_id', $apply_id)            ->get()->toArray();        if(empty($result)) return [true, []];        //组织主表需要的数据        $list_map = $this->makeListMap($list, $result, $data['type']);        $data_id = array_unique(array_column($result, 'data_id'));        list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);        $return = [];        $d_tmp = array_column($d,null,'id');        foreach ($result as $t){            $tmp = $d_tmp[$t['data_id']] ?? [];            $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";            $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";            $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";            $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";            if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){                $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);            }else{                $order_no = $tmp['order_no'] ?? "";            }            if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){                $product_unit = "吨";            }elseif($t['type'] == ApplyOrder::type_four){                $product_unit = "只";            }else{                $product_unit = $t['product_unit'] ?? "";            }            $tmp = $list_map[$t['apply_order_id']] ?? [];            $return[] = [                'order_number' => $tmp['order_number'],                'sale_order_number' => $tmp['sale_order_number'],                'apply_time' => $tmp['apply_time'],                'mark' => $tmp['mark'],                'status_title' => $tmp['status_title'],                'id' => $t['data_id'] ?? 0,//                'main_id' => $t['id'],                'quantity' => $t['quantity'] ?? 0,                'product_no' => $t['product_no'] ?? "",                'product_title' => $t['product_title'] ?? "",                'product_size' => $t['product_size'] ?? "",                'product_unit' => $product_unit,                'top_product_title' => $top_product_title,                'top_product_no' => $top_product_no,                'technology_name' => $technology_name, //颜色                'wood_name' => $wood_name,                'order_no' => $order_no,            ];        }        return [true, array_values($return)];    }    private function getDetailList($apply_id, $type){        $d = $d_no_map = [];        if($type == ApplyOrder::type_one){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($type == ApplyOrder::type_two){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($type == ApplyOrder::type_three){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }elseif ($type == ApplyOrder::type_four){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }        return [$d, $d_no_map];    }    private function makeListMap($data, $order, $type){        if(empty($data)) return [];        //申请单 所有的数据id        $order_map = $data_id = [];        foreach ($order as $value){            $order_map[$value['apply_order_id']][] = $value['data_id'];            $data_id[] = $value['data_id'];        }        if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){            $map1 = DispatchSub::whereIn('id', array_unique($data_id))                ->pluck('out_order_no','id')                ->toArray();        }else{            $map2 = Box::whereIn('id', array_unique($data_id))                ->pluck('top_order_no','id')                ->toArray();            $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))                ->pluck('out_order_no','order_no')                ->toArray();        }        foreach ($data as $key => $value){            $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';            $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";            $order_tmp = $order_map[$value['id']] ?? [];            $order_tmp = array_unique($order_tmp);            $sale_order_number = [];            if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){                foreach ($order_tmp as $v){                    $str = $map1[$v] ?? '';                    if(empty($str)) continue;                    $sale_order_number[] = $str;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data[$key]['sale_order_number'] = $sale_order_number;            }else{                foreach ($order_tmp as $v){                    $m2_t = $map2[$v] ?? '';                    $m2_t = $map2_fin[$m2_t] ?? "";                    if(empty($m2_t)) continue;                    $sale_order_number[] = $m2_t;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data[$key]['sale_order_number'] = $sale_order_number;            }        }        return array_column($data,null,'id');    }}
 |