| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace App\Services;
- use App\Services\common\QueryService;
- use App\Waybill;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Ramsey\Uuid\Uuid;
- Class WaybillService
- {
- /**
- * @param array $param
- * @return Builder
- */
- private function conditionQuery(array $param){
- $waybills = Waybill::filterAuthorities()->with(['owner','logistic','originationCity','destinationCity'=>function($query){
- $query->with('province');
- },'uploadFile','amountUnit','priceModel','warehouseWeightUnit','carrierWeightUnit',
- 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','uploadFile','waybillAuditLogs' => function ($query) {
- $query->with('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');
- $columnQueryRules=[
- 'waybill_number' => ['like' => ''],
- 'carrier_bill' => ['like' => ''],
- 'owner_id' => ['multi' => ','],
- 'wms_bill_number' => ['like' => ''],
- 'origination' => ['like' => ''],
- 'destination' => ['like' => ''],
- 'source_bill' => ['like' => ''],
- 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ':00'],
- 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ':59'],
- 'uriType' => ['alias' => 'type'],
- 'id' => ['multi' => ','],
- ];
- $waybills = app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills");//dd($waybills->sql());
- return $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 getSql(array $param){
- $waybills = $this->conditionQuery($param)->whereNull('waybills.deleted_at');
- return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
- ->leftJoin('units as warehouse_weight_unit','warehouse_weight_unit.id','=','waybills.warehouse_weight_unit_id')
- ->selectRaw('warehouse_weight_unit.name warehouse_weight_unit_name')
- ->leftJoin('units as warehouse_weight_unit_other','warehouse_weight_unit_other.id','=','waybills.warehouse_weight_unit_id_other')
- ->selectRaw('warehouse_weight_unit_other.name warehouse_weight_unit_other_name')
- ->leftJoin('units as carrier_weight_unit','carrier_weight_unit.id','=','waybills.carrier_weight_unit_id')
- ->selectRaw('carrier_weight_unit.name carrier_weight_unit_name')
- ->leftJoin('units as carrier_weight_unit_other','carrier_weight_unit_other.id','=','waybills.carrier_weight_unit_id_other')
- ->selectRaw('carrier_weight_unit_other.name carrier_weight_unit_other_name')
- ->leftJoin('car_types','car_types.id','=','waybills.carType_id')
- ->selectRaw('car_types.name car_type_name')
- ->leftJoin('units as amount_unit','amount_unit.id','=','waybills.amount_unit_id')
- ->selectRaw('amount_unit.name amount_unit_name')
- ->leftJoin('logistics','logistics.id','=','waybills.logistic_id')
- ->selectRaw('logistics.name carrier_name')
- ->sql();
- }
- 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 ]);
- return $waybill;
- });
- }
- public function find($id){
- return Waybill::query()->find($id);
- }
- public function update(Request $request,$id)
- {
- $waybill = $this->find($id);
- //替换换行符
- if ($request->dispatch_remark) {
- $request->offsetSet('dispatch_remark', str_replace(["\n","\r"], ' ', $request->dispatch_remark));
- }
- if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
- if ($request->destination_city_id && $waybill->destination_city_id != $request->destination_city_id) {
- $city = app(CityService::class)->find($request->destination_city_id);
- if ($city && $city->province_name && (mb_strpos($request->destination, $city->name) === false || mb_strpos($request->destination, $city->province_name) === false)) {
- if (mb_strpos($request->destination, $city->name) === false && mb_strpos($request->destination, $city->province_name) === false) {
- $request->offsetSet('destination', $city->province_name . $city->name . $request->destination);
- goto sign;
- }
- if (mb_strpos($request->destination, $city->province_name) === false) {
- $request->offsetSet('destination', $city->province_name . $request->destination);
- }
- if (mb_strpos($request->destination, $city->name) === false) {
- $province_name = $city->province_name;
- $start_index = mb_strpos($request->destination, $city->province_name . '省');
- if ($start_index === false) $start_index = mb_strpos($request->destination, $city->province_name);
- else $province_name = $province_name . '省';
- $strBefore = mb_substr($request->destination, $start_index, mb_strlen($province_name));
- $strAfter = mb_substr($request->destination, $start_index + mb_strlen($province_name));
- $request->offsetSet('destination', $strBefore . $city->name . $strAfter);
- }
- }
- }
- sign:
- $waybill->fill($request->input());
- $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
- {
- if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false;
- $waybill->loadMissing("destinationCity");
- if (!$waybill->destinationCity)return false;
- $detail = app("OwnerFeeDetailService")->first([
- "type" => "发货","owner_id" => $waybill->owner_id,"operation_bill"=>$waybill->wms_bill_number
- ]);
- if (!$detail || $detail->logistic_fee !== null)return false;
- if ($waybill->type == "专线"){
- /** @var OwnerPriceLogisticService $service */
- $service = app("OwnerPriceLogisticService");
- $fee = $service->matching($waybill->carrier_weight_other,$waybill->owner_id,$waybill->logistic_id,
- $waybill->carrier_weight_unit_id_other,$waybill->destinationCity->province_id,
- $waybill->destination_city_id);
- }else{
- /** @var OwnerPriceDirectLogisticService $service */
- $service = app("OwnerPriceDirectLogisticService");
- $fee = $service->matching($waybill->mileage,$waybill->owner_id,$waybill->carType_id);
- }
- if ($fee >= 0){
- app("OwnerFeeDetailService")->updateFind($detail,[
- "owner_id" => $waybill->owner_id,
- "worked_at"=> $waybill->updated_at,
- "type" => "发货",
- "operation_bill" => $waybill->waybill_number,
- "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",
- ]);
- }
- }
- }
|