WaybillService.php 6.9 KB

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