singleton('waybillService',WaybillService::class); } /** * @param Request $request * @param OwnerService $ownerService * @param LogisticService $logisticService * @return void */ public function index(Request $request,OwnerService $ownerService,LogisticService $logisticService) { if(!Gate::allows('运输管理-查询')){ return view("waybill.authorityMenu"); } $paginateParams = $request->input(); $waybills=app('waybillService')->paginate($request->input()); return view('waybill.index', [ 'waybills' => $waybills, 'logistics' => $logisticService->getSelection(["id","name"],"物流"), 'owners' => $ownerService->getIntersectPermitting(), 'paginateParams'=>$paginateParams, 'uriType'=>$request->uriType??'']); } public function create(Request $request,OwnerService $ownerService) { if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); } $type=$request->type ?? ""; if ($type==='ZF')$type='直发车'; if ($type==='ZX')$type='专线'; return view('waybill.create',['owners'=>$ownerService->getIntersectPermitting(),'type'=>$type]); } public function store(Request $request) { if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); } $id=false; $this->validatorWaybill($request,$id)->validate(); /** @var WaybillService */ $waybill=app('waybillService')->store($request); app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return redirect('waybill/index')->with('successTip','新运单“'.$waybill->waybill_number.'”录入成功'); } public function edit($id,LogisticService $logisticService,CarTypeService $carTypeService, CityService $cityService,UnitService $unitService) { if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); } /** @var WaybillService */ $waybill = app('waybillService')->find($id); if (!$waybill)return view("exception.default",["code"=>"500","message"=>"数据已被删除或丢失"]); if ($waybill->deliver_at){ $waybill->deliver_at_date=Carbon::parse($waybill->deliver_at)->format('Y-m-d'); $waybill->deliver_at_time=Carbon::parse($waybill->deliver_at)->format('H:i:s'); } $cities=$cityService->getSelection(); $units=$unitService->getSelection(); $carTypes=$carTypeService->getSelection(); return view('waybill/edit',['waybill'=>$waybill,'logistics'=>$logisticService->getSelection(["id","name"],"物流"),'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]); } public function update(Request $request, $id,WaybillPriceModelService $waybillPriceModelService, LogisticService $logisticService,WaybillPayoffService $waybillPayoffService) { if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); } if (!$request->warehouse_weight && $request->warehouse_weight_unit_id){ $request->offsetUnset('warehouse_weight_unit_id'); } if (!$request->warehouse_weight_other && $request->warehouse_weight_unit_id_other){ $request->offsetUnset('warehouse_weight_unit_id_other'); } if (!$request->carrier_weight && $request->carrier_weight_unit_id){ $request->offsetUnset('carrier_weight_unit_id'); } if (!$request->carrier_weight_other && $request->carrier_weight_unit_id_other){ $request->offsetUnset('carrier_weight_unit_id_other'); } $this->validatorWaybillDispatch($request,$id)->validate(); $waybillPayoffParam = []; $waybillPayoffParam['total_receivable']=0; /** @var WaybillService */ $waybill = app('waybillService')->update($request,$id); if ($waybill->type=="直发车"){ if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge); elseif ($waybill->collect_fee)$waybillPayoffParam['total_receivable'] = ($waybill->collect_fee); $waybillPayoffParam['total_expense'] = ($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee); }else { $waybillPriceModel_id=$request->input('waybillPriceModel'); if ($waybillPriceModel_id){ $carrier_weight=$request->input('carrier_weight'); $waybillPriceModel=$waybillPriceModelService->find($waybillPriceModel_id); $logistic=$logisticService->find($waybill->logistic_id); if ($carrier_weight<$waybillPriceModel->initial_weight){ $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$logistic->delivery_fee; }else{ $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$logistic->delivery_fee; } if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){ $fee=$waybillPriceModel->base_fee; } $waybill->fee=$fee; $waybill->waybill_price_model_id=$waybillPriceModel_id; } $waybill->save(); if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge); elseif($waybill->collect_fee) { $waybillPayoffParam['total_receivable'] = $waybill->collect_fee; } $waybillPayoffParam['total_expense'] = ($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee); } if ($waybillPayoffParam['total_receivable'] > 0){ $waybillPayoffParam['waybill_id'] = $id; $waybillPayoffParam['gross_margin'] = $waybillPayoffParam['total_receivable'] - $waybillPayoffParam['total_expense']; $waybillPayoffParam['gross_profit_rate'] = $waybillPayoffParam['gross_margin']/$waybillPayoffParam['total_receivable']; $waybillPayoffService->updateOrCreate($waybillPayoffParam); } app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功'); } public function checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){ //确保承运商计数与计数单位为一个数组且长度2 if(!$logistic_id)return false; if(!$destination_city_id)return false; if(!$carrier_weight)return false; if(!$carrier_weight_unit_id)return false; //多个计数标准,计算价格,取最贵 if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){ //城市价格区间不为空 $waybillPriceModelOne=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id) ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0]) ->where('unit_id',$carrier_weight_unit_id[0])->first(); $waybillPriceModelTwo=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id) ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1]) ->where('unit_id',$carrier_weight_unit_id[1])->first(); if ($waybillPriceModelOne&&$waybillPriceModelTwo){ if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){ return $waybillPriceModelOne->id; }else{ return $waybillPriceModelTwo->id; } } if ($waybillPriceModelOne)return $waybillPriceModelOne->id; if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id; //价格区间为空 $waybillPriceModelRangeOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$logistic_id,$destination_city_id,$carrier_weight_unit_id[0]])->first(); $waybillPriceModelRangeTwo=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$logistic_id,$destination_city_id,$carrier_weight_unit_id[1]])->first(); if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){ if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){ return $waybillPriceModelRangeOne->id; }else{ return $waybillPriceModelRangeTwo->id; } } if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id; if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id; //城市为空 $city=City::query()->where('id',$destination_city_id)->select('province_id')->first(); $waybillPriceModelProvinceOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL', [$logistic_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first(); $waybillPriceModelProvinceTwo=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL', [$logistic_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first(); if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){ if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){ return $waybillPriceModelProvinceOne->id; }else{ return $waybillPriceModelProvinceTwo->id; } } if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id; if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id; //城市价格区间都为空 $waybillPriceModelProvinceRangeOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL', [$logistic_id,$city->province_id,$carrier_weight_unit_id[0]])->first(); $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL', [$logistic_id,$city->province_id,$carrier_weight_unit_id[1]])->first(); if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){ if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){ return $waybillPriceModelProvinceRangeOne->id; }else{ return $waybillPriceModelProvinceRangeOne->id; } } if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id; if ($waybillPriceModelProvinceRangeTwo)return $waybillPriceModelProvinceRangeTwo->id; }; for ($i=0;$iwhere('logistic_id',$logistic_id)->where('city_id',$destination_city_id) ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i]) ->where('unit_id',$carrier_weight_unit_id[$i])->first(); if($waybillPriceModel)return $waybillPriceModel->id; //价格区间为空 $waybillPriceModelRange=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$logistic_id,$destination_city_id,$carrier_weight_unit_id[$i]])->first(); if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;} //城市为空 $city=City::where('id',$destination_city_id)->select('province_id')->first(); $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL', [$logistic_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first(); if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;} //城市价格区间都为空 $waybillPriceModelProvinceRange=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL', [$logistic_id,$city->province_id,$carrier_weight_unit_id[$i]])->first(); if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;} } } return false; } /*三层条件:无优先级,找到第一个直接返回 * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T * 计数一与计数二同时存在取最贵价格: * 计数一存在,二不存在: * 计数二存在,一不存在: * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空 * */ public function isWaybillPriceModel(Request $request){ $logistic_id=$request->input('logistic_id'); $destination_city_id=$request->input('destination_city_id'); $carrier_weight=$request->input('carrier_weight'); $carrier_weight_unit_id=$request->input('carrier_weight_unit_id'); $validatorData=["logistic_id"=>$logistic_id,"destination_city_id"=>$destination_city_id, 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0], "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]]; $errors=Validator::make($validatorData,[ 'logistic_id'=>'required|integer', 'destination_city_id'=>'required|integer', 'carrier_weight'=>'nullable|min:0|numeric|max:999999', 'carrier_weight_unit_id'=>'required_with:carrier_weight', 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999', 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other', ],[ 'required'=>':attribute 为必填项', 'max'=>':attribute 字符过多或输入值过大', 'min'=>':attribute 不得为负', 'numeric'=>':attribute 应为数字', 'unique'=>':attribute 已存在', 'required_with'=>':attribute 未填', 'integer'=>':attribute 必须为数字', ],[ 'carrier_weight'=>'承运商计数(抛)', 'logistic_id'=>'承运商', 'destination_city_id'=>'目的市', 'carrier_weight_unit_id'=>'承运商计数单位', 'carrier_weight_other'=>'承运商计数二', 'carrier_weight_unit_id_other'=>'承运商计数单位二', ])->errors(); if (count($errors)>0)return ['error'=>$errors]; $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id); if (!$result){ //单位为kg,T时 $unitKG=Unit::query()->where('name','kg')->first(); $unitT=Unit::query()->where('name','T')->first(); if ($carrier_weight_unit_id[0]==$unitKG->id){ $carrier_weight_unit_id[0]=$unitT->id; $carrier_weight[0]=$carrier_weight[0]/1000; $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id); if ($result)return ['success'=>$result]; } if ($carrier_weight_unit_id[1]==$unitKG->id){ $carrier_weight_unit_id[1]=$unitT->id; $carrier_weight[1]=$carrier_weight[1]/1000; $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id); if ($result)return ['success'=>$result]; } if ($carrier_weight_unit_id[0]==$unitT->id){ $carrier_weight_unit_id[0]=$unitKG->id; $carrier_weight[0]=$carrier_weight[0]*1000; $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id); if ($result)return ['success'=>$result]; } if ($carrier_weight_unit_id[1]==$unitT->id){ $carrier_weight_unit_id[1]=$unitKG->id; $carrier_weight[1]=$carrier_weight[1]*1000; $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id); if ($result)return ['success'=>$result]; } } return ['success'=>$result]; } public function waybillUpdate(Request $request, $id){ if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); } $this->validatorWaybill($request,$id)->validate(); $data=$request->input(); $waybill=app('waybillService')->find($id); $waybill->fill($data); if ($waybill->save()){ app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功'); } } public function waybillAudit(Request $request){ if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); } $id=$request->input('id'); $waybill=app('waybillService')->find($id); $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first(); if (empty($isAudit)){ $waybillAuditLog=new WaybillAuditLog([ 'waybill_id'=>$id, 'audit_stage'=>'运单阶段', 'user_id'=>Auth::id(), ]); $waybillAuditLog->save(); $waybillAuditLog['user']=Auth::user(); $waybill->status='已审核'; $result=$waybill->save(); app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog]; } return ['exception'=>'请勿重复审核!']; } public function waybillEdit($id){ if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); } $waybill=app('waybillService')->find($id); $owners=Owner::get(); return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]); } public function waybillRetreatAudit(Request $request){ if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); } $id=$request->input('id'); $waybill=app('waybillService')->find($id); WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete(); $waybill->status='待重审'; $result=$waybill->save(); app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']); return ['success'=>$result,'status'=>$waybill->status]; } public function waybillEndAudit(Request $request){ if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); } $id=$request->input('id'); $waybill=Waybill::query()->with(["owner","logistic","originationCity","destinationCity","carType",'priceModel',"amountUnit", "warehouseWeightUnit","carrierWeightUnit","warehouseWeightUnitOther","carrierWeightUnitOther"])->find($id); if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!']; if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!']; if ($waybill->type=='专线'){ if (!$waybill->carrier_weight_other||$waybill->carrier_weight_other==0)return ['exception'=>'承运商计重未填或为0!']; if (!$waybill->carrier_weight_unit_id_other)return ['exception'=>'承运商计重单位未选!']; } $isAudit=WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first(); if (empty($isAudit)){ $waybillAuditLog=new WaybillAuditLog([ 'waybill_id'=>$id, 'audit_stage'=>'调度阶段', 'user_id'=>Auth::id(), ]); $waybillAuditLog->save(); $waybillAuditLog['user']=Auth::user(); if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){ $waybill->status='已完结'; $result=$waybill->save(); $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first(); $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE); WaybillFinancialSnapshot::query()->create([ 'waybill_id'=>$id, 'json_content'=>$waybillPayoffJson, ]); }else{ $waybill->status='无模型'; $result=$waybill->save(); $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first(); if ($waybillPayoff){ $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE); WaybillFinancialExcepted::query()->create([ 'waybill_id'=>$id, 'json_content'=>$waybillPayoffJson, ]); } } app('LogService')->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::id()); return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog]; } return ['exception'=>'请勿重复审核!']; } //生成报表数据 private function createReportData($waybill,$waybillPayoff){ return [ "type"=>$waybill->type, "waybill_number"=>$waybill->waybill_number, "owner_name"=>$waybill->owner ? $waybill->owner->name : null, "wms_bill_number"=>$waybill->wms_bill_number, "source_bill"=>$waybill->source_bill, "origination"=>$waybill->origination, "destination"=>$waybill->destination, "recipient"=>$waybill->recipient, "recipient_mobile"=>$waybill->recipient_mobile, "charge"=>$waybill->charge, "collect_fee"=>$waybill->collect_fee, "ordering_remark"=>$waybill->ordering_remark, "carrier_name"=>$waybill->logistic ? $waybill->logistic->name : null, "carrier_bill"=>$waybill->carrier_bill, "origination_city_name"=>$waybill->originationCity ? $waybill->originationCity->name : null, "destination_city_name"=>$waybill->destinationCity ? $waybill->destinationCity->name : null, "warehouse_weight"=>$waybill->warehouse_weight.($waybill->warehouseWeightUnit ? $waybill->warehouseWeightUnit->name : ''), "carrier_weight"=>$waybill->carrier_weight.($waybill->carrierWeightUnit ? $waybill->carrierWeightUnit->name : ''), "warehouse_weight_other"=>$waybill->warehouse_weight_other.($waybill->warehouseWeightUnitOther ? $waybill->warehouseWeightUnitOther->name : ''), "carrier_weight_other"=>$waybill->carrier_weight_other.($waybill->carrierWeightUnitOther ? $waybill->carrierWeightUnitOther->name : ''), "car_type_name"=>$waybill->carType ? $waybill->carType->name : null, "fee"=>$waybill->fee, "pick_up_fee"=>$waybill->pick_up_fee, "other_fee"=>$waybill->other_fee, "dispatch_remark"=>$waybill->dispatch_remark, "price_model_range_min"=>$waybill->priceModel ? $waybill->priceModel->range_min : null, "price_model_range_max"=>$waybill->priceModel ? $waybill->priceModel->range_max : null, "price_model_unit_price"=>$waybill->priceModel ? $waybill->priceModel->unit_price : null, "price_model_base_fee"=>$waybill->priceModel ? $waybill->priceModel->base_fee : null, "price_model_initial_weight"=>$waybill->priceModel ? $waybill->priceModel->initial_weight : null, "car_owner_info"=>$waybill->car_owner_info, "status"=>$waybill->status, "mileage"=>$waybill->mileage, 'amount'=>$waybill->amount.($waybill->amountUnit ? $waybill->amountUnit->name : ''), "inquire_tel"=>$waybill->inquire_tel, "other_charge"=>$waybill->other_charge, "other_charge_remark"=>$waybill->other_charge_remark, "deliver_at"=>$waybill->deliver_at, "created_at"=>$waybill->created_at, "auditLog_user_name"=>Auth::user()['name'], "total_expense"=>$waybillPayoff->total_expense, "total_receivable"=>$waybillPayoff->total_receivable, "gross_margin"=>$waybillPayoff->gross_margin, "gross_profit_rate"=>$waybillPayoff->gross_profit_rate, ]; } public function upload(Request $request){ if(!Gate::allows('运输管理-图片上传')){ return '没有权限'; } $file=$request->file('file'); $waybill_number=$request->input('waybill_number'); $waybill=Waybill::query()->where('waybill_number',$waybill_number)->first(); if (!$waybill){ return ['success'=>false,'error'=>"未找到该运单!"]; } if ($waybill->upload_file_url){ return ['success'=>false,'error'=>"该运单已存在照片!"]; } if (!$file){ return ['success'=>false,'error'=>"照片不得为空!"]; } if (!$file->isValid()){ return ['success'=>false,'error'=>"找不到照片!"]; } $tmpFile = $file->getRealPath(); if (! is_uploaded_file($tmpFile)) { return ['success'=>false,'error'=>"文件错误!"]; } $fileExtension=$file->getClientOriginalExtension(); // 5.存储, 生成一个随机文件名 $fileName = date('ymd').'-'.Uuid::uuid1();//thumbnail common bulky $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$fileExtension); $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$fileExtension); $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$fileExtension); $result=move_uploaded_file ($tmpFile ,$bulkyName); if ($result){ $img=Image::make($bulkyName); if ($img->height() > $img->width()) $img->heighten(250)->save($commonName); else $img->widen(250)->save($commonName); $img->heighten(28)->save($thumbnailName); $uploadFile=new UploadFile([ "table_name"=>"waybills", "table_id"=>$waybill->id, "url"=>'/files/'.$fileName, "type"=>$fileExtension, ]); if ($uploadFile->save()) app('LogService')->log(__METHOD__,'图片上传',json_encode($request),Auth::user()['id']); $uploadFile->url=asset('/storage'.$uploadFile->url); return ['success'=>true,'data'=>$uploadFile]; } return ['success'=>false,'error'=>"图片保存失败!"]; } //批量上传图片 public function batchUploadImages() { $this->gate("运输管理-图片上传"); $images = request("images"); $errors = []; $number = []; $mapping = []; $type = ["jpg","png","gif","jfif","pjpeg","jpeg","webp"]; foreach ($images as $index => $image){ $suffix = $image->getClientOriginalExtension(); $name = $image->getClientOriginalName(); if (array_search(strtolower($suffix),$type) === false){ $errors[] = "“".$name."”格式错误"; unset($images[$index]); continue; } $num = trim(rtrim($name,".".$suffix)); $number[] = $num; $mapping[$num] = $index; } $waybills = Waybill::query()->select("id","source_bill")->doesntHave("uploadFile")->whereIn('source_bill',$number)->get(); foreach (array_diff($number,array_column($waybills->toArray(),"source_bill")) as $diff){ $errors[] = "“".$diff."”不存在运单或已存在照片"; unset($images[$mapping[$diff]]); } $insert = []; foreach ($waybills as $waybill){ $image = $images[$mapping[$waybill->source_bill]]; $fileName = date('ymd').'-'.Uuid::uuid1(); $suffix = $image->getClientOriginalExtension(); $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$suffix); $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$suffix); $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$suffix); move_uploaded_file ($image->getRealPath() ,$bulkyName); $img=Image::make($bulkyName); if ($img->height() > $img->width()) $img->heighten(250)->save($commonName); else $img->widen(250)->save($commonName); $img->heighten(28)->save($thumbnailName); $insert[] = [ "table_name"=>"waybills", "table_id"=>$waybill->id, "url"=>'/files/'.$fileName, "type"=>strtolower($suffix), ]; } if ($insert)UploadFile::query()->insert($insert); $waybills->load("uploadFile"); $this->success(["errors"=>$errors,"data"=>$waybills]); } //删除照片 public function deleteImg(Request $request){ if(!Gate::allows('运输管理-图片删除')){ return '没有权限'; } $ids=$request->input('ids'); $uploadFiles=UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->get(); foreach ($uploadFiles as $uploadFile){ $bulky=storage_path('app/public/'.$uploadFile->url.'-bulky.'.$uploadFile->type); $common=storage_path('app/public/'.$uploadFile->url.'-common.'.$uploadFile->type); $thumbnail=storage_path('app/public/'.$uploadFile->url.'-thumbnail.'.$uploadFile->type); if (file_exists($bulky) && file_exists($common) && file_exists($thumbnail)){ unlink($bulky);unlink($common);unlink($thumbnail); } } UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->delete(); app('LogService')->log(__METHOD__,'图片删除',json_encode($request),Auth::user()['id']); return ['success'=>true]; } public function export(Request $request){ if(!Gate::allows('运输管理-查询')){ return '没有权限'; } if ($request->checkAllSign){ $param = $request->input(); unset($param['checkAllSign']); $sql = app('waybillService')->getSql($param); }else $sql = app('waybillService')->getSql(['id'=>$request->data]); $post = Http::post(config('go.export.url'),['type'=>'waybill','sql'=>$sql]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=运单列表-".date('ymdHis').'.xlsx', ]); } public function deliveringExport(Request $request){ if ($request->checkAllSign){ $param = $request->input(); unset($param['checkAllSign']); $sql = app('waybillService')->getDeliveringSql($param); }else{ $sql = app('waybillService')->getDeliveringSql(['id'=>$request->data]); } $post = Http::post(config('go.export.url'),['type'=>'waybillDelivering','sql'=>$sql]); if ($post->status() == 500){ throw new Exception($post->header("Msg")); } return response($post,200, [ "Content-type"=>"application/octet-stream", "Content-Disposition"=>"attachment; filename=发运列表-".date('ymdHis').'.xlsx', ]); } //发运 public function delivering(Request $request){ if (!Auth::user())return view('exception.login'); $waybills= app('waybillService')->paginate($request->input()); if (!Auth::user()->isSuperAdmin()){ $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get(); $carrierIds=array_column($carriersUsers->toArray(),'logistic_id'); $waybills=$waybills->whereIn("logistic_id",$carrierIds); } return view('waybill.delivering',compact('waybills')); } //承运商提交 public function storeCarrierBill(Request $request){ $errors=Validator::make($request->input(),[ 'id'=>'required|integer', 'carrier_bill'=>'required', 'inquire_tel'=>'nullable', 'amount'=>'nullable|integer', 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric', 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric', ],[ 'required'=>':attribute 为必填项', 'integer'=>':attribute 应为整数', 'numeric'=>':attribute 应为数字', 'required_with'=>':attribute 重量与体积至少存在一项', ],[ 'carrier_bill'=>'专线运单号', 'inquire_tel'=>'查件电话', 'amount'=>'件数', 'carrier_weight'=>'体积', 'carrier_weight_other'=>'重量', ])->errors(); if (count($errors)>0)return ["errors"=>$errors]; $waybill=Waybill::query()->find($request->input('id')); if (!$waybill)return ["error"=>"未找到该运单!"]; $waybill->fill($request->input()); $waybill->update(); return $waybill; } protected function validatorWaybill(Request $request,$id){ if ($id){$wms_bill_number=$id;}; $validator=Validator::make($request->input(),[ 'owner_id'=>'required', 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'], 'origination'=>'required|max:255', 'destination'=>'required|max:255', 'recipient'=>'required|max:50', 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'], 'charge'=>'nullable|min:0|max:999999|numeric', 'collect_fee'=>'nullable|min:0|numeric', ],[ 'required'=>':attribute 为必填项', 'alpha_num'=>':attribute 应为字母或数字', 'max'=>':attribute 字符过多或输入值过大', 'regex'=>':attribute 输入有误', 'integer'=>':attribute 应为整数', 'min'=>':attribute 不得为负', 'numeric'=>':attribute 应为数字', 'unique'=>':attribute 已存在', ],[ 'owner_id'=>'货主', 'wms_bill_number'=>'WMS单号', 'origination'=>'始发地', 'destination'=>'目的地', 'recipient'=>'收件人', 'recipient_mobile'=>'收件人电话', 'charge'=>'收费', 'collect_fee'=>'到付金额', ]); return $validator; } protected function validatorWaybillDispatch(Request $request,$id){ $rule=[ 'logistic_id'=>'required|integer', 'carrier_bill'=>"sometimes|nullable|max:50|unique:waybills,carrier_bill,$id", 'fee'=>'sometimes|nullable|min:0|numeric|max:999999', 'other_fee'=>'sometimes|nullable|min:0|numeric|max:999999', 'charge'=>'sometimes|nullable|min:0|numeric|max:999999', 'mileage'=>'nullable|numeric|min:0', 'amount'=>'nullable|numeric|min:0', 'amount_unit_id'=>'required', 'origination_city_id'=>'sometimes|required|integer', 'destination_city_id'=>'sometimes|required|integer', 'warehouse_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999', 'warehouse_weight_unit_id_other'=>'sometimes|required_with:warehouse_weight_other|nullable|integer', 'pick_up_fee'=>'sometimes|nullable|min:0|numeric|max:999999', 'warehouse_weight'=>'sometimes|nullable|min:0|numeric|max:999999', 'warehouse_weight_unit_id'=>'sometimes|required_with:warehouse_weight|nullable|integer', 'carrier_weight'=>'sometimes|nullable|min:0|numeric|max:999999', 'carrier_weight_unit_id'=>'sometimes|required_with:carrier_weight', 'carrier_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999', 'carrier_weight_unit_id_other'=>'sometimes|required_with:carrier_weight_other', ]; if ($request->type == '专线'){ $rule['origination_city_id']='required|integer'; $rule['destination_city_id']='required|integer'; } $validator=Validator::make($request->input(),$rule,[ 'required'=>':attribute 为必填项', 'alpha_num'=>':attribute 应为字母或数字', 'max'=>':attribute 字符过多或输入值过大', 'min'=>':attribute 不得为负', 'numeric'=>':attribute 应为数字', 'unique'=>':attribute 已存在', 'required_with'=>':attribute 未填', 'integer'=>':attribute 必须为数字', ],[ 'logistic_id'=>'承运商', 'carrier_bill'=>'承运商单号', 'fee'=>'运费', 'other_fee'=>'其他费用', 'charge'=>'收费', 'mileage'=>'里程数', 'amount'=>'计数', 'amount_unit_id'=>'计数单位', 'warehouse_weight'=>'仓库计数(抛)', 'carrier_weight'=>'承运商计数(抛)', 'pick_up_fee'=>'提货费', 'destination_city_id'=>'目的市', 'carrier_weight_unit_id'=>'承运商计数单位', 'warehouse_weight_unit_id'=>'仓库计数单位', 'warehouse_weight_other'=>'仓库计数二', 'carrier_weight_other'=>'承运商计数二', 'warehouse_weight_unit_id_other'=>'仓库技数单位二', 'carrier_weight_unit_id_other'=>'承运商计数单位二', ]); return $validator; } public function addCounty(Request $request){ if ($request->destination_city =='undefined')$request->offsetUnset('destination_city'); $rule =[ 'destination_city' => ['required', 'string','unique:cities,name'], ]; $messages=[ 'destination_city.required'=>'市/县不能为空', 'destination_city.string'=>'市/县必须是字符串', 'destination_city.unique'=>'市/县已存在', ]; $validator = Validator::make($request->all(),$rule,$messages); if ($validator->fails()) { return $validator->errors(); } $city=new City([ 'name'=>$request->input('destination_city'), 'type'=>3, ]); $city->save(); return $city; } // 运单删除 软删除 public function destroy(int $id){ if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];} if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];} $result = Waybill::where('id',$id)->delete(); return ['success'=>$result,'status'=>$result]; } // 回收站 public function recycle(Request $request){ if(!Gate::allows('运输管理-删除')){return redirect('/');} $paginate = $request->input('paginate')??50; /** @var Collection $waybills */ $waybills = Waybill::query()->with(['owner','logistic','amountUnit','warehouseWeightUnit','carrierWeightUnit', 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','waybillAuditLogs' => function ($query) { /** @var Builder $query */ $query->with('user'); }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50); $total = $waybills->count(); $paginateParams = []; $paginateParams['paginate'] = $paginate; return view('waybill.recycle',compact('waybills','total','paginateParams')); } // 软删除恢复 public function apiRestoreSelected(Request $request){ if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];} $ids = $request->input('ids')??''; if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];} $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get(); $waybills->each(function (Waybill $waybill){ $waybill->restore(); }); app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return ['success'=>'true','waybills'=>$waybills]; } // 修改运费 public function changeFee(Request $request){ if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];} $wayBillId = $request->input('id'); $waybillFee = $request->input('fee'); if(is_null($wayBillId) or is_null($waybillFee)){ return ['success'=>'false','fail_info'=>'参数异常']; } $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]); app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return ['success'=>$result,'status'=>$result]; } // 修改运输收费 public function changeCharge(Request $request){ if(!Gate::allows('运输管理-运单编辑')){return ['success'=>'false','fail_info'=>'没有权限'];} $wayBillId = $request->id; $waybillCharge = $request->input('charge'); if(is_null($wayBillId) or is_null($waybillCharge)){ return ['success'=>'false','fail_info'=>'参数异常']; } $result = Waybill::where('id',$wayBillId)->update(['charge'=>$waybillCharge]); app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return ['success'=>$result,'status'=>$result]; } // 置顶 public function waybillOnTop(Request $request){ $id = $request->input('id'); $detail = $request->input('detail'); if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];} if(is_null($id)){ return ['success'=>'false','fail_info'=>'传参错误']; } $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id); if(count($wayontop->get()) == 0){ $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]); $result = $wayontop->save(); }else{ $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore(); } return ['success'=>$result,'status'=>$result]; } // 取消置顶 public function cancelOnTop(Request $request){ $id = $request->input('id'); if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];} if(is_null($id)){ return ['success'=>'false','fail_info'=>'传参错误']; } $result = WaybillOnTop::where('waybill_id',$id)->forceDelete(); return ['success'=>$result,'status'=>$result]; } //同步刷新仓库计重 public function refreshWaveHouseWeight(Request $request){ $wms_bill_number=$request->input('wms_bill_number'); if(is_null($wms_bill_number)) return ['success'=>false,'fail_info'=>'传参错误']; $waybills=DB::connection('oracle')->table('DOC_ORDER_DETAILS')->where('orderno',$wms_bill_number)->get(); if($waybills->isEmpty()) return ['success'=>false,'fail_info'=>'传参错误']; $warehouseWeight=0; foreach ($waybills as $waybill){ if ($waybill->grossweight) $warehouseWeight += $waybill->grossweight; if (!$waybill->grossweight&& $waybill->netweight) $warehouseWeight +=$waybill->netweight; } $warehouseWeight=round($warehouseWeight,2); $waybill=Waybill::where('wms_bill_number',$wms_bill_number)->first(); if ($warehouseWeight!=0){ if ($waybill['warehouse_weight_other']!=$warehouseWeight){ $waybill['warehouse_weight_other']=$warehouseWeight; $waybill->update(); app('LogService')->log(__METHOD__,'刷新仓库计重'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); } }else{ $warehouseWeight=$waybill['warehouse_weight_other']; } return ['success'=>true,'warehouseWeight'=>$warehouseWeight]; } }