WaybillService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(Request $request){
  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. ];
  32. $waybills = app(QueryService::class)->query($request->input(),$waybills,$columnQueryRules,"waybills");
  33. return $waybills;
  34. }
  35. public function paginate(Request $request){
  36. $waybills = $this->conditionQuery($request);
  37. return $waybills->paginate($request->paginate ?? 50);
  38. }
  39. public function get(Request $request){
  40. $waybills = $this->conditionQuery($request);
  41. return $waybills->get();
  42. }
  43. public function getSql(Request $request){
  44. $waybills = $this->conditionQuery($request);
  45. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  46. ->leftJoin('units as warehouse_weight_unit','warehouse_weight_unit.id','=','waybills.warehouse_weight_unit_id')
  47. ->selectRaw('warehouse_weight_unit.name warehouse_weight_unit_name')
  48. ->leftJoin('units as warehouse_weight_unit_other','warehouse_weight_unit_other.id','=','waybills.warehouse_weight_unit_id_other')
  49. ->selectRaw('warehouse_weight_unit_other.name warehouse_weight_unit_other_name')
  50. ->leftJoin('units as carrier_weight_unit','carrier_weight_unit.id','=','waybills.carrier_weight_unit_id')
  51. ->selectRaw('carrier_weight_unit.name carrier_weight_unit_name')
  52. ->leftJoin('units as carrier_weight_unit_other','carrier_weight_unit_other.id','=','waybills.carrier_weight_unit_id_other')
  53. ->selectRaw('carrier_weight_unit_other.name carrier_weight_unit_other_name')
  54. ->leftJoin('car_types','car_types.id','=','waybills.carType_id')
  55. ->selectRaw('car_types.name car_type_name')
  56. ->leftJoin('units as amount_unit','amount_unit.id','=','waybills.amount_unit_id')
  57. ->selectRaw('amount_unit.name amount_unit_name')
  58. ->leftJoin('carriers','carriers.id','=','waybills.carrier_id')
  59. ->selectRaw('carriers.name carrier_name')
  60. ->sql();
  61. }
  62. public function some(Request $request){
  63. return $waybills = Waybill::filterAuthorities()->with(['owner'])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  64. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  65. ->whereNull('waybill_on_tops.deleted_at')
  66. ->orderBy('waybill_on_tops.updated_at','desc')
  67. ->orderBy('waybills.id','desc')
  68. ->whereIn('waybills.id',explode(',',$request->data))
  69. ->get();
  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(PHP_EOL, ' ', $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. }