WaybillService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerFeeDetail;
  4. use App\Services\common\QueryService;
  5. use App\Traits\ModelSearchWay;
  6. use App\Waybill;
  7. use App\WaybillAuditLog;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\DB;
  12. use Ramsey\Uuid\Uuid;
  13. use App\Traits\ServiceAppAop;
  14. class WaybillService
  15. {
  16. use ServiceAppAop;
  17. use ModelSearchWay;
  18. protected $modelClass=Waybill::class;
  19. /**
  20. * @param array $param
  21. * @return Builder
  22. */
  23. private function conditionQuery(array $param){
  24. $waybills = Waybill::filterAuthorities()->with(['owner','logistic','originationCity','destinationCity.parent',
  25. 'uploadFile','amountUnit','warehouseWeightUnit','carrierWeightUnit','district',
  26. 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','uploadFile','waybillAuditLogs.user'])
  27. ->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  28. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  29. ->whereNull('waybill_on_tops.deleted_at')
  30. ->orderBy('waybill_on_tops.updated_at','desc')
  31. ->orderBy('waybills.id','desc');
  32. if ($param["owner_id"] ?? false){
  33. $ownerIds = explode(",",$param["owner_id"]);
  34. $waybills->where(function ($query)use($ownerIds){
  35. /** @var Builder $query */
  36. $query->whereIn("waybills.owner_id",$ownerIds)->orWhereHas("order",function ($query)use($ownerIds){
  37. /** @var Builder $query */
  38. $query->whereIn("owner_id",$ownerIds);
  39. });
  40. });
  41. unset($param["owner_id"]);
  42. }
  43. if ($param["destination"] ?? false){
  44. $destination = $param["destination"];
  45. $waybills->where(function ($query)use($destination){
  46. /** @var Builder $query */
  47. $query->where("waybills.destination","like",$destination."%")->orWhereHas("order",function ($query)use($destination){
  48. /** @var Builder $query */
  49. $query->where("address",'like',$destination."%");
  50. });
  51. });
  52. unset($param["destination"]);
  53. }
  54. if ($param["recipient"] ?? false){
  55. $recipient = $param["recipient"];
  56. $waybills->where(function ($query)use($recipient){
  57. /** @var Builder $query */
  58. $query->where("waybills.recipient","like",$recipient."%")->orWhereHas("order",function ($query)use($recipient){
  59. /** @var Builder $query */
  60. $query->where("consignee_name",'like',$recipient."%");
  61. });
  62. });
  63. unset($param["recipient"]);
  64. }
  65. if ($param["recipient_mobile"] ?? false){
  66. $recipientMobile = $param["recipient_mobile"];
  67. $waybills->where(function ($query)use($recipientMobile){
  68. /** @var Builder $query */
  69. $query->where("waybills.recipient_mobile","like",$recipientMobile."%")->orWhereHas("order",function ($query)use($recipientMobile){
  70. /** @var Builder $query */
  71. $query->where("consignee_phone",'like',$recipientMobile."%");
  72. });
  73. });
  74. unset($param["recipient_mobile"]);
  75. }
  76. if($param['carrier_bill'] ?? false){ // 承运商单号
  77. $this->searchWay($waybills,$param['carrier_bill'],'waybills.carrier_bill');
  78. unset($param['carrier_bill']);
  79. }
  80. if($param['waybill_number'] ?? false) { // 运单号
  81. $this->searchWay($waybills,$param['waybill_number'],'waybills.waybill_number');
  82. unset($param['waybill_number']);
  83. }
  84. if($param['source_bill'] ?? false) {// 上游单号
  85. $this->searchWay($waybills,$param['source_bill'],'waybills.source_bill');
  86. unset($param['source_bill']);
  87. }
  88. if($param['wms_bill_number']?? false){ // wms单号
  89. $this->searchWay($waybills,$param['wms_bill_number'],'waybills.wms_bill_number');
  90. unset($param['wms_bill_number']);
  91. }
  92. $columnQueryRules=[
  93. // 'waybill_number' => ['like' => ''],
  94. // 'carrier_bill' => ['like' => ''],
  95. // 'wms_bill_number' => ['like' => ''],
  96. 'origination' => ['like' => ''],
  97. // 'source_bill' => ['like' => ''],
  98. 'car_owner_info' => ['like' => ''],
  99. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ':00'],
  100. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ':59'],
  101. 'uriType' => ['alias' => 'type'],
  102. 'id' => ['multi' => ','],
  103. ];
  104. $waybills = app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills");
  105. return $waybills;
  106. }
  107. public function paginate(array $param){
  108. $waybills = $this->conditionQuery($param);
  109. return $waybills->paginate($param['paginate'] ?? 50);
  110. }
  111. public function get(array $param){
  112. $waybills = $this->conditionQuery($param);
  113. return $waybills->get();
  114. }
  115. public function store(Request $request){
  116. return DB::transaction(function ()use($request){
  117. $waybill=new Waybill();
  118. $inputs = $request->all();
  119. $inputs['status']='未审核';
  120. $inputs['waybill_number']=Uuid::uuid1();
  121. $waybill->fill($inputs);
  122. if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee;
  123. $waybill->save();
  124. $number_id=$waybill->id;
  125. if ($request->type=='直发车') $waybill_number='BSZF';
  126. else $waybill_number='BSZX';
  127. $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  128. $waybill->update(['waybill_number' => $waybill_number ]);
  129. WaybillAuditLog::query()->create([
  130. 'waybill_id'=>$waybill->id,
  131. 'audit_stage'=>'创建',
  132. 'user_id'=>Auth::id() ?? 0,
  133. ]);
  134. return $waybill;
  135. });
  136. }
  137. public function find($id){
  138. return Waybill::query()->find($id);
  139. }
  140. public function update(Request $request,$id)
  141. {
  142. $waybill = $this->find($id);
  143. //替换换行符
  144. if ($request->dispatch_remark) {
  145. $request->offsetSet('dispatch_remark', str_replace(["\n","\r"], ' ', $request->dispatch_remark));
  146. }
  147. if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
  148. $waybill->fill($request->input());
  149. $waybill->update();
  150. return $waybill;
  151. }
  152. public function getDeliveringSql(array $param){
  153. $waybills = $this->conditionQuery($param);
  154. if (!Auth::user()->isSuperAdmin()){
  155. $users=DB::table('logistic_user')->where('user_id',Auth::id())->get();
  156. $userIds=array_column($users->toArray(),'logistic_id');
  157. $waybills=$waybills->whereIn("waybills.logistic_id",$userIds);
  158. }
  159. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  160. ->leftJoin('logistics','logistics.id','=','waybills.logistic_id')
  161. ->selectRaw('logistics.name carrier_name')
  162. ->sql();
  163. }
  164. public function createInstantBill(Waybill $waybill) :bool
  165. {
  166. /** @var \stdClass $waybill */
  167. if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false;
  168. $waybill->loadMissing(["destinationCity","order.owner"]);
  169. if (!$waybill->destinationCity && !$waybill->order)return false;
  170. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  171. $detail = OwnerFeeDetail::query()->where("type","发货")
  172. ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first();
  173. if ($detail && $detail->logistic_fee !== null)return false;
  174. if ($waybill->type == "专线"){
  175. /** @var OwnerPriceLogisticService $service */
  176. $service = app("OwnerPriceLogisticService");
  177. list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  178. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  179. $waybill->destination_city_id);
  180. }else{
  181. /** @var OwnerPriceDirectLogisticService $service */
  182. $service = app("OwnerPriceDirectLogisticService");
  183. list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  184. }
  185. $obj = [
  186. "owner_id" => $owner_id,
  187. "worked_at"=> $waybill->updated_at,
  188. "type" => "发货",
  189. "operation_bill" => $waybill->waybill_number,
  190. "consignee_name" => $waybill->recipient,
  191. "consignee_phone" => $waybill->recipient_mobile,
  192. "commodity_amount" => $waybill->amount,
  193. "logistic_bill" => $waybill->carrier_bill,
  194. "volume" =>$waybill->carrier_weight ?? $waybill->warehouse_weight,
  195. "weight" => $waybill->carrier_weight_other ?? $waybill->warehouse_weight_other,
  196. "logistic_id" => $waybill->logistic_id,
  197. "logistic_fee" => $fee,
  198. "outer_id" => $waybill->id,
  199. "outer_table_name" => "waybills",
  200. "logistic_tax_fee" => $taxFee,
  201. ];
  202. if ($detail)app("OwnerFeeDetailService")->updateFind($detail,$obj);
  203. else OwnerFeeDetail::query()->create($obj);
  204. return true;
  205. }
  206. }