WaybillService.php 9.6 KB

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