WaybillService.php 10.0 KB

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