WaybillService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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','carrier','originationCity','destinationCity'=>function($query){
  18. $query->with('province');
  19. },'uploadFile','amountUnit','priceModel','warehouseWeightUnit','carrierWeightUnit',
  20. 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','uploadFile',
  21. 'wmsCommodities','waybillAuditLogs' => function ($query) {
  22. $query->with('user');
  23. }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  24. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  25. ->whereNull('waybill_on_tops.deleted_at')
  26. ->orderBy('waybill_on_tops.updated_at','desc')
  27. ->orderBy('waybills.id','desc');
  28. $columnQueryRules=[
  29. 'waybill_number' => ['like' => ''],
  30. 'carrier_bill' => ['like' => ''],
  31. 'owner_id' => ['multi' => ','],
  32. 'wms_bill_number' => ['like' => ''],
  33. 'origination' => ['like' => ''],
  34. 'destination' => ['like' => ''],
  35. 'source_bill' => ['like' => ''],
  36. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ':00'],
  37. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ':59'],
  38. 'uriType' => ['alias' => 'type'],
  39. 'id' => ['multi' => ','],
  40. ];
  41. $waybills = app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills");//dd($waybills->sql());
  42. return $waybills;
  43. }
  44. public function paginate(array $param){
  45. $waybills = $this->conditionQuery($param);
  46. return $waybills->paginate($param['paginate'] ?? 50);
  47. }
  48. public function get(array $param){
  49. $waybills = $this->conditionQuery($param);
  50. return $waybills->get();
  51. }
  52. public function getSql(array $param){
  53. $waybills = $this->conditionQuery($param)->whereNull('waybills.deleted_at');
  54. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  55. ->leftJoin('units as warehouse_weight_unit','warehouse_weight_unit.id','=','waybills.warehouse_weight_unit_id')
  56. ->selectRaw('warehouse_weight_unit.name warehouse_weight_unit_name')
  57. ->leftJoin('units as warehouse_weight_unit_other','warehouse_weight_unit_other.id','=','waybills.warehouse_weight_unit_id_other')
  58. ->selectRaw('warehouse_weight_unit_other.name warehouse_weight_unit_other_name')
  59. ->leftJoin('units as carrier_weight_unit','carrier_weight_unit.id','=','waybills.carrier_weight_unit_id')
  60. ->selectRaw('carrier_weight_unit.name carrier_weight_unit_name')
  61. ->leftJoin('units as carrier_weight_unit_other','carrier_weight_unit_other.id','=','waybills.carrier_weight_unit_id_other')
  62. ->selectRaw('carrier_weight_unit_other.name carrier_weight_unit_other_name')
  63. ->leftJoin('car_types','car_types.id','=','waybills.carType_id')
  64. ->selectRaw('car_types.name car_type_name')
  65. ->leftJoin('units as amount_unit','amount_unit.id','=','waybills.amount_unit_id')
  66. ->selectRaw('amount_unit.name amount_unit_name')
  67. ->leftJoin('carriers','carriers.id','=','waybills.carrier_id')
  68. ->selectRaw('carriers.name carrier_name')
  69. ->sql();
  70. }
  71. public function store(Request $request){
  72. return DB::transaction(function ()use($request){
  73. $waybill=new Waybill();
  74. $inputs = $request->all();
  75. $inputs['status']='未审核';
  76. $inputs['waybill_number']=Uuid::uuid1();
  77. $waybill->fill($inputs);
  78. if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee;
  79. $waybill->save();
  80. $number_id=$waybill->id;
  81. if ($request->type=='直发车') $waybill_number='BSZF';
  82. else $waybill_number='BSZX';
  83. $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  84. $waybill->update(['waybill_number' => $waybill_number ]);
  85. return $waybill;
  86. });
  87. }
  88. public function find($id){
  89. return Waybill::query()->find($id);
  90. }
  91. public function update(Request $request,$id)
  92. {
  93. $waybill = $this->find($id);
  94. //替换换行符
  95. if ($request->dispatch_remark) {
  96. $request->offsetSet('dispatch_remark', str_replace(["\n","\r"], ' ', $request->dispatch_remark));
  97. }
  98. if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
  99. if ($request->destination_city_id && $waybill->destination_city_id != $request->destination_city_id) {
  100. $city = app(CityService::class)->find($request->destination_city_id);
  101. if ($city && $city->province_name && (mb_strpos($request->destination, $city->name) === false || mb_strpos($request->destination, $city->province_name) === false)) {
  102. if (mb_strpos($request->destination, $city->name) === false && mb_strpos($request->destination, $city->province_name) === false) {
  103. $request->offsetSet('destination', $city->province_name . $city->name . $request->destination);
  104. goto sign;
  105. }
  106. if (mb_strpos($request->destination, $city->province_name) === false) {
  107. $request->offsetSet('destination', $city->province_name . $request->destination);
  108. }
  109. if (mb_strpos($request->destination, $city->name) === false) {
  110. $province_name = $city->province_name;
  111. $start_index = mb_strpos($request->destination, $city->province_name . '省');
  112. if ($start_index === false) $start_index = mb_strpos($request->destination, $city->province_name);
  113. else $province_name = $province_name . '省';
  114. $strBefore = mb_substr($request->destination, $start_index, mb_strlen($province_name));
  115. $strAfter = mb_substr($request->destination, $start_index + mb_strlen($province_name));
  116. $request->offsetSet('destination', $strBefore . $city->name . $strAfter);
  117. }
  118. }
  119. }
  120. sign:
  121. $waybill->fill($request->input());
  122. $waybill->update();
  123. return $waybill;
  124. }
  125. public function getDeliveringSql(array $param){
  126. $waybills = $this->conditionQuery($param);
  127. if (!Auth::user()->isSuperAdmin()){
  128. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  129. $carrierIds=array_column($carriersUsers->toArray(),'carrier_id');
  130. $waybills=$waybills->whereIn("waybills.carrier_id",$carrierIds);
  131. }
  132. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  133. ->leftJoin('carriers','carriers.id','=','waybills.carrier_id')
  134. ->selectRaw('carriers.name carrier_name')
  135. ->sql();
  136. }
  137. }