with(['owner','logistic','originationCity','destinationCity.parent', 'uploadFiles','amountUnit','warehouseWeightUnit','carrierWeightUnit','district','order', 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','uploadFiles','waybillAuditLogs.user']) ->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update') ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id') ->whereNull('waybill_on_tops.deleted_at') ->orderBy('waybill_on_tops.updated_at','desc') ->orderBy('waybills.id','desc'); if ($param["owner"] ?? false){ $ownerIds = explode(",",$param["owner"]); $waybills->where(function ($query)use($ownerIds){ /** @var Builder $query */ $query->whereIn("waybills.owner_id",$ownerIds)->orWhereHas("order",function ($query)use($ownerIds){ /** @var Builder $query */ $query->whereIn("owner_id",$ownerIds); }); }); unset($param["owner"]); } if ($param["destination"] ?? false){ $destination = $param["destination"]; $waybills->where(function ($query)use($destination){ /** @var Builder $query */ $query->where("waybills.destination","like",$destination."%")->orWhereHas("order",function ($query)use($destination){ /** @var Builder $query */ $query->where("address",'like',$destination."%"); }); }); unset($param["destination"]); } if ($param["recipient"] ?? false){ $recipient = $param["recipient"]; $waybills->where(function ($query)use($recipient){ if (strpos($recipient, ',') || strpos($recipient, ',') || strpos($recipient, ' ')) { $arr = array_filter(preg_split('/[,, ]+/is', $recipient)); /** @var Builder $query */ $query->whereIn("waybills.recipient",$arr)->orWhereHas("order",function ($query)use($arr){ /** @var Builder $query */ $query->whereIn("consignee_name",$arr); }); } else { /** @var Builder $query */ $query->where("waybills.recipient","like",$recipient."%")->orWhereHas("order",function ($query)use($recipient){ /** @var Builder $query */ $query->where("consignee_name",'like',$recipient."%"); }); } }); unset($param["recipient"]); } if ($param["recipient_mobile"] ?? false){ $recipientMobile = $param["recipient_mobile"]; $waybills->where(function ($query)use($recipientMobile){ if (strpos($recipientMobile, ',') || strpos($recipientMobile, ',') || strpos($recipientMobile, ' ')) { $arr = array_filter(preg_split('/[,, ]+/is', $recipientMobile)); /** @var Builder $query */ $query->whereIn("waybills.recipient_mobile",$arr)->orWhereHas("order",function ($query)use($arr){ /** @var Builder $query */ $query->whereIn("consignee_phone",$arr); }); } else { /** @var Builder $query */ $query->where("waybills.recipient_mobile","like",$recipientMobile."%")->orWhereHas("order",function ($query)use($recipientMobile){ /** @var Builder $query */ $query->where("consignee_phone",'like',$recipientMobile."%"); }); } }); unset($param["recipient_mobile"]); } if (($param["updated_at_start"] ?? false) || ($param["updated_at_end"] ?? false)){ $waybills->whereIn("waybills.status",['已完结','无模型']); } if ($param["mileage"] ?? false){ $param["mileage"] = explode("-",$param["mileage"]); $waybills->whereBetween("mileage",[(int)$param["mileage"][0] ?? 0,(int)$param["mileage"][1]] ?? 0); unset($param["mileage"]); } if ($param["warehouse_weight_other"] ?? false){ $param["warehouse_weight_other"] = explode("-",$param["warehouse_weight_other"]); $waybills->whereBetween("warehouse_weight_other",[(int)$param["warehouse_weight_other"][0] ?? 0,(int)$param["warehouse_weight_other"][1]] ?? 0); unset($param["warehouse_weight_other"]); } $columnQueryRules=[ 'waybill_number' => ['batch' => ''], 'carrier_bill' => ['batch' => ''], 'wms_bill_number' => ['batch' => ''], 'origination' => ['batch' => ''], 'source_bill' => ['batch' => ''], 'car_owner_info' => ['batch' => ''], 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ':00'], 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ':59'], 'updated_at_start' => ['alias' => 'updated_at' , 'startDate' => ':00'], 'updated_at_end' => ['alias' => 'updated_at' , 'endDate' => ':59'], 'deliver_at_start' => ['alias' => 'deliver_at' , 'startDate' => ':00'], 'deliver_at_end' => ['alias' => 'deliver_at' , 'endDate' => ':59'], 'uriType' => ['alias' => 'type'], 'id' => ['multi' => ','], 'logistic' => ['alias' => 'logistic_id', 'multi' => ','], 'ordering_remark' => ['like' => '%'], ]; return app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills"); } public function paginate(array $param){ $waybills = $this->conditionQuery($param); return $waybills->paginate($param['paginate'] ?? 50); } public function get(array $param){ $waybills = $this->conditionQuery($param); return $waybills->get(); } public function dailyBilling(array $param, bool $isOnlyLogistic = false){ if (($param['screenDate']??false)==false || ($param['billing']??false)==false) return 0; //入参错误 $waybills = Waybill::query()->with(['owner','logistic','originationCity','destinationCity.parent', 'uploadFiles','amountUnit','warehouseWeightUnit','carrierWeightUnit','district','order', 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','waybillAuditLogs.user']) ->selectRaw('waybills.* ,waybill_on_tops.id top_id,waybill_on_tops.remark,waybill_on_tops.updated_at top_update') ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id') ->whereNull('waybill_on_tops.deleted_at') ->orderBy('waybill_on_tops.updated_at','desc') ->orderBy('waybills.id','desc') ->where('waybills.type','专线') ->where('waybills.amount','>',0) ->where('waybills.deliver_at','like',$param['screenDate'].'%'); if ($isOnlyLogistic)$waybills->whereIn("logistic_id",app("LogisticService")->getQuery()); $waybills = $waybills->get(); if ($waybills->isEmpty()) return 1;//无数据 foreach ($waybills as $waybill){ if (!$waybill['carrier_weight_other'] && !$waybill['carrier_weight']) return null; if (!$waybill['carrier_weight_other'] || $waybill['carrier_weight_other']<=0) $waybill['carrier_weight_other']=($waybill['carrier_weight']/0.25*100); } $daily_total_weight=$waybills->sum('carrier_weight_other'); $updateParams = [['id','pick_up_fee','updated_at']]; foreach ($waybills as $waybill){ $waybill['pick_up_fee']=round(($waybill->carrier_weight_other/$daily_total_weight)*$param['billing']); $updateParams[] = [ 'id' => $waybill->id, 'pick_up_fee' =>(($waybill->carrier_weight_other/$daily_total_weight)*$param['billing']), 'updated_at' => Carbon::now()->toDateTimeString(), ]; } if (count($updateParams)>0)app(BatchUpdateService::class)->batchUpdate('waybills', $updateParams); return $waybills; } public function store(Request $request){ return DB::transaction(function ()use($request){ $waybill=new Waybill(); $inputs = $request->all(); $inputs['status']='未审核'; $inputs['waybill_number']=Uuid::uuid1(); $waybill->fill($inputs); if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee; $waybill->save(); $number_id=$waybill->id; if ($request->type=='直发车') $waybill_number='BSZF'; else $waybill_number='BSZX'; $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT); $waybill->update(['waybill_number' => $waybill_number ]); WaybillAuditLog::query()->create([ 'waybill_id'=>$waybill->id, 'audit_stage'=>'创建', 'user_id'=>Auth::id() ?? 0, ]); return $waybill; }); } public function find($id){ return Waybill::query()->find($id); } /** * 修改物流单 * * @param Waybill|\stdClass $waybill * @param array $param * * @return Model */ public function update(Waybill $waybill,array $param) { //替换换行符 if ($param["dispatch_remark"] ?? false)$param["dispatch_remark"] = str_replace(["\n","\r"], ' ', $param["dispatch_remark"]); if (!($param["destination"] ?? false)) $param["destination"] = $waybill->destination; $waybill->fill($param); $waybill->update(); return $waybill; } public function getDeliveringSql(array $param){ $waybills = $this->conditionQuery($param); if (!Auth::user()->isSuperAdmin()){ $users=DB::table('logistic_user')->where('user_id',Auth::id())->get(); $userIds=array_column($users->toArray(),'logistic_id'); $waybills=$waybills->whereIn("waybills.logistic_id",$userIds); } return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name') ->leftJoin('logistics','logistics.id','=','waybills.logistic_id') ->selectRaw('logistics.name carrier_name') ->sql(); } public function createInstantBill(Waybill $waybill) :bool { /** @var \stdClass $waybill */ if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false; $waybill->loadMissing(["destinationCity","order.owner"]); if (!$waybill->destinationCity && !$waybill->order)return false; $owner_id = $waybill->order->owner_id ?? $waybill->owner_id; $detail = OwnerFeeDetail::query()->where("type","发货") ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first(); if ($detail && $detail->logistic_fee !== null)return false; if ($waybill->type == "专线"){ $provinceId = $waybill->order ? app("ProvinceService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id; $cityId = $waybill->destination_city_id; $consigneeName = $waybill->order ? $waybill->order->consignee_name : $waybill->recipient; $consigneePhone = $waybill->order ? $waybill->order->consignee_phone : $waybill->recipient_mobile; $GLOBALS["FEE_INFO"] = [ "province_id" => $provinceId, "owner_id" => $waybill->owner_id, "city_id" => $cityId, "logistic_id" => $waybill->logistic_id, "order_number" => $waybill->wms_bill_number, "recipient_name"=>$consigneeName, "recipient_phone"=>$consigneePhone, "quantity"=>$waybill->carrier_weight_other, "unit_id"=>$waybill->carrier_weight_unit_id_other, "remark"=>$waybill->ordering_remark, "created_at"=>$waybill->updated_at, ]; /** @var OwnerPriceLogisticService $service */ $service = app("OwnerPriceLogisticService"); list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id, $waybill->carrier_weight_unit_id_other,$provinceId, $cityId); $this->buildWaybillFeeInfo(); }else{ /** @var OwnerPriceDirectLogisticService $service */ $service = app("OwnerPriceDirectLogisticService"); list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id); } $obj = [ "owner_id" => $owner_id, "worked_at"=> $waybill->updated_at, "type" => "发货", "operation_bill" => $waybill->waybill_number, "province" => $waybill->order_id ? ($waybill->order->province ?? '') : ($waybill->destinationCity->parent->name ?? ''), "consignee_name" => $waybill->recipient, "consignee_phone" => $waybill->recipient_mobile, "commodity_amount" => $waybill->amount, "logistic_bill" => $waybill->carrier_bill, "volume" =>$waybill->carrier_weight ?? $waybill->warehouse_weight, "weight" => $waybill->carrier_weight_other ?? $waybill->warehouse_weight_other, "logistic_id" => $waybill->logistic_id, "logistic_fee" => $fee, "outer_id" => $waybill->id, "outer_table_name" => "waybills", "logistic_tax_fee" => $taxFee, ]; if ($detail)app("OwnerFeeDetailService")->updateFind($detail,$obj); else OwnerFeeDetail::query()->create($obj); return true; } /** * 构建运输费用信息 */ public function buildWaybillFeeInfo() { if (!isset($GLOBALS["FEE_INFO"]))return; OwnerFeeLogistic::query()->create($GLOBALS["FEE_INFO"]); } /** * 生成德邦单据 * * @param Order|\stdClass $order * * @return void */ public function createDbBill(Order $order) { $order->loadMissing("logistic"); if (!$order->logistic || substr($order->logistic->code,0,2) != 'DB')return; if (Waybill::query()->selectRaw("1")->where("wms_bill_number",$order->code)->first())return; $waybill = Waybill::query()->create([ 'type'=> "德邦物流", 'waybill_number'=> Uuid::uuid1(), 'owner_id'=> $order->owner_id, 'wms_bill_number'=> $order->code, 'destination'=> $order->address, 'recipient'=> $order->consignee_name, 'recipient_mobile'=>$order->consignee_phone, 'source_bill'=> $order->client_code, 'is_to_pay'=> strstr($order->logistic->code,'DF')===false ? 0 : 1, 'destination_city_id'=>$order->city ? app(RegionService::class)->getCity($order->city) : null, 'order_id'=> $order->id, ]); $waybill->update([ "waybill_number" => 'BSDB'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT), ]); WaybillAuditLog::query()->create([ 'waybill_id'=>$waybill->id, 'audit_stage'=>'创建', 'user_id'=>Auth::id() ?? 0, ]); } /** * 通知FLUX新单号 * * @param Model|\stdClass $waybill * * @return bool */ public function notifyFlux($waybill):bool { /** @var Waybill|\stdClass $w */ $w = new Waybill(); $w->wms_bill_number = $waybill->wms_bill_number; $w->waybill_number = $waybill->carrier_bill; $controller = new WaybillController(); if ($controller->accomplishToWMS($w)){ $sql = <<update($sql,[ $waybill->carrier_bill,$waybill->carrier_bill,$waybill->wms_bill_number ]); if ($updateRow==0)Log::warning("德邦单号修改分配失败",["param"=>$w,"row"=>$updateRow]); return true; } return false; } /** * 获取调度数据 * */ public function getDispatchQuery():Builder { return Waybill::query()->with(["order:id,address","logistic:id,name","amountUnit:id,name"]) ->select("id","order_id","logistic_id","amount_unit_id","deliver_at", "waybill_number","destination","recipient","recipient_mobile", "carrier_bill","warehouse_weight","carrier_weight","inquire_tel", "warehouse_weight_other","carrier_weight_other","amount","origination")->where(function ($query){ /** @var Builder $query */ $query->whereIn("owner_id",app("OwnerService")->getQuery()) ->orWhereIn("logistic_id",app("LogisticService")->getQuery()); })->whereNotNull("deliver_at")->whereIn("status",["已审核","待终审"]); } public function autoGetDBBill(Waybill $waybill){ if ($waybill->type=='德邦物流'){ if ($waybill->carrier_bill)return; $waybill->load("order"); if (!$waybill->order){ Log::error("德邦单号获取失败",["no"=>$waybill->waybill_number,"info"=>"运单无绑定订单"]); return; } list($update,$waybill) = $this->paramDefault($waybill); $bill = app('DbOpenService')->getDbOrderNo($waybill); if (!$bill || $bill["result"]=="false"){ Log::error("德邦单号获取失败",["no"=>$waybill->toArray(),"info"=>$bill]); return; } $update["carrier_bill"] = $bill['mailNo']; $update["waybill_number"] = $bill['mailNo']; $update["station_no"] = $bill['stationNo']; $update["arrived_org_simple_name"] = $bill['arrivedOrgSimpleName']; $update["much_higher_delivery"] = $bill['muchHigherDelivery']; $logistic = Logistic::query()->where("code","DBWL")->first(); $update["logistic_id"] = $logistic->id ?? null; $waybill->update($update); if (!app("WaybillService")->notifyFlux($waybill)){ Log::error("德邦单号回传FLUX失败",["no"=>$waybill->waybill_number,"info"=>$bill]); return; } } } private function paramDefault($waybill):array { $update = []; if (!$waybill->order_type){ $update["order_type"] = $waybill->order_type = Waybill::ORDER_TYPE_DEFAULT; } if (!$waybill->transport_type){ $update["transport_type"] = $waybill->transport_type = "JZKH"; } if (!$waybill->cargo_name){ $update["cargo_name"] = $waybill->cargo_name = "补货"; } if (!$waybill->total_number){ $update["total_number"] = $waybill->total_number = 1; } if (!$waybill->total_weight){ $update["total_weight"] = $waybill->total_weight = 1; } if (!$waybill->package_service){ $update["package_service"] = $waybill->package_service = '托膜'; } if (!$waybill->deliveryType_id){ $update["deliveryType_id"] = $waybill->deliveryType_id = 3; } if (!$waybill->pay_type){ $update["pay_type"] = $waybill->pay_type = Waybill::PAY_TYPE_DEFAULT; } if (!$waybill->back_sign_bill){ $update["back_sign_bill"] = $waybill->back_sign_bill = Waybill::BACK_SIGN_BILL_DEFAULT; } return array($update,$waybill); } }