WaybillService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Services;
  3. use App\Services\common\QueryService;
  4. use App\Waybill;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Ramsey\Uuid\Uuid;
  10. Class WaybillService
  11. {
  12. /**
  13. * @param array $param
  14. * @return Builder
  15. */
  16. private function conditionQuery(array $param){
  17. $waybills = Waybill::filterAuthorities()->with(['owner','logistic','originationCity','destinationCity'=>function($query){
  18. $query->with('province');
  19. },'uploadFile','amountUnit','priceModel','warehouseWeightUnit','carrierWeightUnit',
  20. 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','uploadFile','waybillAuditLogs' => function ($query) {
  21. $query->with('user');
  22. }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  23. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  24. ->whereNull('waybill_on_tops.deleted_at')
  25. ->orderBy('waybill_on_tops.updated_at','desc')
  26. ->orderBy('waybills.id','desc');
  27. $columnQueryRules=[
  28. 'waybill_number' => ['like' => ''],
  29. 'carrier_bill' => ['like' => ''],
  30. 'owner_id' => ['multi' => ','],
  31. 'wms_bill_number' => ['like' => ''],
  32. 'origination' => ['like' => ''],
  33. 'destination' => ['like' => ''],
  34. 'source_bill' => ['like' => ''],
  35. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ':00'],
  36. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ':59'],
  37. 'uriType' => ['alias' => 'type'],
  38. 'id' => ['multi' => ','],
  39. ];
  40. $waybills = app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills");//dd($waybills->sql());
  41. return $waybills;
  42. }
  43. public function paginate(array $param){
  44. $waybills = $this->conditionQuery($param);
  45. return $waybills->paginate($param['paginate'] ?? 50);
  46. }
  47. public function get(array $param){
  48. $waybills = $this->conditionQuery($param);
  49. return $waybills->get();
  50. }
  51. public function getSql(array $param){
  52. $waybills = $this->conditionQuery($param)->whereNull('waybills.deleted_at');
  53. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  54. ->leftJoin('units as warehouse_weight_unit','warehouse_weight_unit.id','=','waybills.warehouse_weight_unit_id')
  55. ->selectRaw('warehouse_weight_unit.name warehouse_weight_unit_name')
  56. ->leftJoin('units as warehouse_weight_unit_other','warehouse_weight_unit_other.id','=','waybills.warehouse_weight_unit_id_other')
  57. ->selectRaw('warehouse_weight_unit_other.name warehouse_weight_unit_other_name')
  58. ->leftJoin('units as carrier_weight_unit','carrier_weight_unit.id','=','waybills.carrier_weight_unit_id')
  59. ->selectRaw('carrier_weight_unit.name carrier_weight_unit_name')
  60. ->leftJoin('units as carrier_weight_unit_other','carrier_weight_unit_other.id','=','waybills.carrier_weight_unit_id_other')
  61. ->selectRaw('carrier_weight_unit_other.name carrier_weight_unit_other_name')
  62. ->leftJoin('car_types','car_types.id','=','waybills.carType_id')
  63. ->selectRaw('car_types.name car_type_name')
  64. ->leftJoin('units as amount_unit','amount_unit.id','=','waybills.amount_unit_id')
  65. ->selectRaw('amount_unit.name amount_unit_name')
  66. ->leftJoin('logistics','logistics.id','=','waybills.logistic_id')
  67. ->selectRaw('logistics.name carrier_name')
  68. ->sql();
  69. }
  70. public function store(Request $request){
  71. return DB::transaction(function ()use($request){
  72. $waybill=new Waybill();
  73. $inputs = $request->all();
  74. $inputs['status']='未审核';
  75. $inputs['waybill_number']=Uuid::uuid1();
  76. $waybill->fill($inputs);
  77. if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee;
  78. $waybill->save();
  79. $number_id=$waybill->id;
  80. if ($request->type=='直发车') $waybill_number='BSZF';
  81. else $waybill_number='BSZX';
  82. $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  83. $waybill->update(['waybill_number' => $waybill_number ]);
  84. return $waybill;
  85. });
  86. }
  87. public function find($id){
  88. return Waybill::query()->find($id);
  89. }
  90. public function update(Request $request,$id)
  91. {
  92. $waybill = $this->find($id);
  93. //替换换行符
  94. if ($request->dispatch_remark) {
  95. $request->offsetSet('dispatch_remark', str_replace(["\n","\r"], ' ', $request->dispatch_remark));
  96. }
  97. if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
  98. if ($request->destination_city_id && $waybill->destination_city_id != $request->destination_city_id) {
  99. $city = app(CityService::class)->find($request->destination_city_id);
  100. if ($city && $city->province_name && (mb_strpos($request->destination, $city->name) === false || mb_strpos($request->destination, $city->province_name) === false)) {
  101. if (mb_strpos($request->destination, $city->name) === false && mb_strpos($request->destination, $city->province_name) === false) {
  102. $request->offsetSet('destination', $city->province_name . $city->name . $request->destination);
  103. goto sign;
  104. }
  105. if (mb_strpos($request->destination, $city->province_name) === false) {
  106. $request->offsetSet('destination', $city->province_name . $request->destination);
  107. }
  108. if (mb_strpos($request->destination, $city->name) === false) {
  109. $province_name = $city->province_name;
  110. $start_index = mb_strpos($request->destination, $city->province_name . '省');
  111. if ($start_index === false) $start_index = mb_strpos($request->destination, $city->province_name);
  112. else $province_name = $province_name . '省';
  113. $strBefore = mb_substr($request->destination, $start_index, mb_strlen($province_name));
  114. $strAfter = mb_substr($request->destination, $start_index + mb_strlen($province_name));
  115. $request->offsetSet('destination', $strBefore . $city->name . $strAfter);
  116. }
  117. }
  118. }
  119. sign:
  120. $waybill->fill($request->input());
  121. $waybill->update();
  122. return $waybill;
  123. }
  124. public function getDeliveringSql(array $param){
  125. $waybills = $this->conditionQuery($param);
  126. if (!Auth::user()->isSuperAdmin()){
  127. $users=DB::table('logistic_user')->where('user_id',Auth::id())->get();
  128. $userIds=array_column($users->toArray(),'logistic_id');
  129. $waybills=$waybills->whereIn("waybills.logistic_id",$userIds);
  130. }
  131. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  132. ->leftJoin('logistics','logistics.id','=','waybills.logistic_id')
  133. ->selectRaw('logistics.name carrier_name')
  134. ->sql();
  135. }
  136. public function createInstantBill(Waybill $waybill) :bool
  137. {
  138. if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false;
  139. $waybill->loadMissing("destinationCity");
  140. if (!$waybill->destinationCity)return false;
  141. $detail = app("OwnerFeeDetailService")->first([
  142. "type" => "发货","owner_id" => $waybill->owner_id,"operation_bill"=>$waybill->wms_bill_number
  143. ]);
  144. if (!$detail || $detail->logistic_fee !== null)return false;
  145. if ($waybill->type == "专线"){
  146. /** @var OwnerPriceLogisticService $service */
  147. $service = app("OwnerPriceLogisticService");
  148. $fee = $service->matching($waybill->carrier_weight_other,$waybill->owner_id,$waybill->logistic_id,
  149. $waybill->carrier_weight_unit_id_other,$waybill->destinationCity->province_id,
  150. $waybill->destination_city_id);
  151. }else{
  152. /** @var OwnerPriceDirectLogisticService $service */
  153. $service = app("OwnerPriceDirectLogisticService");
  154. $fee = $service->matching($waybill->mileage,$waybill->owner_id,$waybill->carType_id);
  155. }
  156. if ($fee >= 0){
  157. app("OwnerFeeDetailService")->updateFind($detail,[
  158. "owner_id" => $waybill->owner_id,
  159. "worked_at"=> $waybill->updated_at,
  160. "type" => "发货",
  161. "operation_bill" => $waybill->waybill_number,
  162. "consignee_name" => $waybill->recipient,
  163. "consignee_phone" => $waybill->recipient_mobile,
  164. "commodity_amount" => $waybill->amount,
  165. "logistic_bill" => $waybill->carrier_bill,
  166. "volume" =>$waybill->carrier_weight ?? $waybill->warehouse_weight,
  167. "weight" => $waybill->carrier_weight_other ?? $waybill->warehouse_weight_other,
  168. "logistic_id" => $waybill->logistic_id,
  169. "logistic_fee" => $fee,
  170. "outer_id" => $waybill->id,
  171. "outer_table_name" => "waybills",
  172. ]);
  173. }
  174. }
  175. }