WaybillService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if (strpos($recipient, ',') || strpos($recipient, ',') || strpos($recipient, ' ')) {
  58. $arr = array_filter(preg_split('/[,, ]+/is', $recipient));
  59. /** @var Builder $query */
  60. $query->whereIn("waybills.recipient",$arr)->orWhereHas("order",function ($query)use($arr){
  61. /** @var Builder $query */
  62. $query->whereIn("consignee_name",$arr);
  63. });
  64. } else {
  65. /** @var Builder $query */
  66. $query->where("waybills.recipient","like",$recipient."%")->orWhereHas("order",function ($query)use($recipient){
  67. /** @var Builder $query */
  68. $query->where("consignee_name",'like',$recipient."%");
  69. });
  70. }
  71. });
  72. unset($param["recipient"]);
  73. }
  74. if ($param["recipient_mobile"] ?? false){
  75. $recipientMobile = $param["recipient_mobile"];
  76. $waybills->where(function ($query)use($recipientMobile){
  77. if (strpos($recipientMobile, ',') || strpos($recipientMobile, ',') || strpos($recipientMobile, ' ')) {
  78. $arr = array_filter(preg_split('/[,, ]+/is', $recipientMobile));
  79. /** @var Builder $query */
  80. $query->whereIn("waybills.recipient_mobile",$arr)->orWhereHas("order",function ($query)use($arr){
  81. /** @var Builder $query */
  82. $query->whereIn("consignee_phone",$arr);
  83. });
  84. } else {
  85. /** @var Builder $query */
  86. $query->where("waybills.recipient_mobile","like",$recipientMobile."%")->orWhereHas("order",function ($query)use($recipientMobile){
  87. /** @var Builder $query */
  88. $query->where("consignee_phone",'like',$recipientMobile."%");
  89. });
  90. }
  91. });
  92. unset($param["recipient_mobile"]);
  93. }
  94. // if($param['carrier_bill'] ?? false){ // 承运商单号
  95. // $this->searchWay($waybills,$param['carrier_bill'],'waybills.carrier_bill');
  96. // unset($param['carrier_bill']);
  97. // }
  98. // if($param['waybill_number'] ?? false) { // 运单号
  99. // $this->searchWay($waybills,$param['waybill_number'],'waybills.waybill_number');
  100. // unset($param['waybill_number']);
  101. // }
  102. // if($param['source_bill'] ?? false) {// 上游单号
  103. // $this->searchWay($waybills,$param['source_bill'],'waybills.source_bill');
  104. // unset($param['source_bill']);
  105. // }
  106. // if($param['wms_bill_number']?? false){ // wms单号
  107. // $this->searchWay($waybills,$param['wms_bill_number'],'waybills.wms_bill_number');
  108. // unset($param['wms_bill_number']);
  109. // }
  110. $columnQueryRules=[
  111. 'waybill_number' => ['batch' => ''],
  112. 'carrier_bill' => ['batch' => ''],
  113. 'wms_bill_number' => ['batch' => ''],
  114. 'origination' => ['batch' => ''],
  115. 'source_bill' => ['batch' => ''],
  116. 'car_owner_info' => ['batch' => ''],
  117. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ':00'],
  118. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ':59'],
  119. 'uriType' => ['alias' => 'type'],
  120. 'id' => ['multi' => ','],
  121. ];
  122. $waybills = app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills");
  123. return $waybills;
  124. }
  125. public function paginate(array $param){
  126. $waybills = $this->conditionQuery($param);
  127. return $waybills->paginate($param['paginate'] ?? 50);
  128. }
  129. public function get(array $param){
  130. $waybills = $this->conditionQuery($param);
  131. return $waybills->get();
  132. }
  133. public function store(Request $request){
  134. return DB::transaction(function ()use($request){
  135. $waybill=new Waybill();
  136. $inputs = $request->all();
  137. $inputs['status']='未审核';
  138. $inputs['waybill_number']=Uuid::uuid1();
  139. $waybill->fill($inputs);
  140. if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee;
  141. $waybill->save();
  142. $number_id=$waybill->id;
  143. if ($request->type=='直发车') $waybill_number='BSZF';
  144. else $waybill_number='BSZX';
  145. $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  146. $waybill->update(['waybill_number' => $waybill_number ]);
  147. WaybillAuditLog::query()->create([
  148. 'waybill_id'=>$waybill->id,
  149. 'audit_stage'=>'创建',
  150. 'user_id'=>Auth::id() ?? 0,
  151. ]);
  152. return $waybill;
  153. });
  154. }
  155. public function find($id){
  156. return Waybill::query()->find($id);
  157. }
  158. public function update(Request $request,$id)
  159. {
  160. $waybill = $this->find($id);
  161. //替换换行符
  162. if ($request->dispatch_remark) {
  163. $request->offsetSet('dispatch_remark', str_replace(["\n","\r"], ' ', $request->dispatch_remark));
  164. }
  165. if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
  166. $waybill->fill($request->input());
  167. $waybill->update();
  168. return $waybill;
  169. }
  170. public function getDeliveringSql(array $param){
  171. $waybills = $this->conditionQuery($param);
  172. if (!Auth::user()->isSuperAdmin()){
  173. $users=DB::table('logistic_user')->where('user_id',Auth::id())->get();
  174. $userIds=array_column($users->toArray(),'logistic_id');
  175. $waybills=$waybills->whereIn("waybills.logistic_id",$userIds);
  176. }
  177. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  178. ->leftJoin('logistics','logistics.id','=','waybills.logistic_id')
  179. ->selectRaw('logistics.name carrier_name')
  180. ->sql();
  181. }
  182. public function createInstantBill(Waybill $waybill) :bool
  183. {
  184. /** @var \stdClass $waybill */
  185. if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false;
  186. $waybill->loadMissing(["destinationCity","order.owner"]);
  187. if (!$waybill->destinationCity && !$waybill->order)return false;
  188. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  189. $detail = OwnerFeeDetail::query()->where("type","发货")
  190. ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first();
  191. if ($detail && $detail->logistic_fee !== null)return false;
  192. if ($waybill->type == "专线"){
  193. /** @var OwnerPriceLogisticService $service */
  194. $service = app("OwnerPriceLogisticService");
  195. $fee = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  196. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  197. $waybill->destination_city_id);
  198. }else{
  199. /** @var OwnerPriceDirectLogisticService $service */
  200. $service = app("OwnerPriceDirectLogisticService");
  201. $fee = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  202. }
  203. $obj = [
  204. "owner_id" => $owner_id,
  205. "worked_at"=> $waybill->updated_at,
  206. "type" => "发货",
  207. "operation_bill" => $waybill->waybill_number,
  208. "consignee_name" => $waybill->recipient,
  209. "consignee_phone" => $waybill->recipient_mobile,
  210. "commodity_amount" => $waybill->amount,
  211. "logistic_bill" => $waybill->carrier_bill,
  212. "volume" =>$waybill->carrier_weight ?? $waybill->warehouse_weight,
  213. "weight" => $waybill->carrier_weight_other ?? $waybill->warehouse_weight_other,
  214. "logistic_id" => $waybill->logistic_id,
  215. "logistic_fee" => $fee ?? null,
  216. "outer_id" => $waybill->id,
  217. "outer_table_name" => "waybills",
  218. ];
  219. if ($detail)app("OwnerFeeDetailService")->updateFind($detail,$obj);
  220. else OwnerFeeDetail::query()->create($obj);
  221. return true;
  222. }
  223. }