WaybillService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 'logistic_id' => ['multi' => ','],
  122. ];
  123. $waybills = app(QueryService::class)->query($param,$waybills,$columnQueryRules,"waybills");
  124. return $waybills;
  125. }
  126. public function paginate(array $param){
  127. $waybills = $this->conditionQuery($param);
  128. return $waybills->paginate($param['paginate'] ?? 50);
  129. }
  130. public function get(array $param){
  131. $waybills = $this->conditionQuery($param);
  132. return $waybills->get();
  133. }
  134. public function store(Request $request){
  135. return DB::transaction(function ()use($request){
  136. $waybill=new Waybill();
  137. $inputs = $request->all();
  138. $inputs['status']='未审核';
  139. $inputs['waybill_number']=Uuid::uuid1();
  140. $waybill->fill($inputs);
  141. if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee;
  142. $waybill->save();
  143. $number_id=$waybill->id;
  144. if ($request->type=='直发车') $waybill_number='BSZF';
  145. else $waybill_number='BSZX';
  146. $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  147. $waybill->update(['waybill_number' => $waybill_number ]);
  148. WaybillAuditLog::query()->create([
  149. 'waybill_id'=>$waybill->id,
  150. 'audit_stage'=>'创建',
  151. 'user_id'=>Auth::id() ?? 0,
  152. ]);
  153. return $waybill;
  154. });
  155. }
  156. public function find($id){
  157. return Waybill::query()->find($id);
  158. }
  159. public function update(Request $request,$id)
  160. {
  161. $waybill = $this->find($id);
  162. //替换换行符
  163. if ($request->dispatch_remark) {
  164. $request->offsetSet('dispatch_remark', str_replace(["\n","\r"], ' ', $request->dispatch_remark));
  165. }
  166. if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
  167. $waybill->fill($request->input());
  168. $waybill->update();
  169. return $waybill;
  170. }
  171. public function getDeliveringSql(array $param){
  172. $waybills = $this->conditionQuery($param);
  173. if (!Auth::user()->isSuperAdmin()){
  174. $users=DB::table('logistic_user')->where('user_id',Auth::id())->get();
  175. $userIds=array_column($users->toArray(),'logistic_id');
  176. $waybills=$waybills->whereIn("waybills.logistic_id",$userIds);
  177. }
  178. return $waybills->leftJoin('owners','owners.id','=','waybills.owner_id')->selectRaw('owners.name owner_name')
  179. ->leftJoin('logistics','logistics.id','=','waybills.logistic_id')
  180. ->selectRaw('logistics.name carrier_name')
  181. ->sql();
  182. }
  183. public function createInstantBill(Waybill $waybill) :bool
  184. {
  185. /** @var \stdClass $waybill */
  186. if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false;
  187. $waybill->loadMissing(["destinationCity","order.owner"]);
  188. if (!$waybill->destinationCity && !$waybill->order)return false;
  189. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  190. $detail = OwnerFeeDetail::query()->where("type","发货")
  191. ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first();
  192. if ($detail && $detail->logistic_fee !== null)return false;
  193. if ($waybill->type == "专线"){
  194. /** @var OwnerPriceLogisticService $service */
  195. $service = app("OwnerPriceLogisticService");
  196. list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  197. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  198. $waybill->destination_city_id);
  199. }else{
  200. /** @var OwnerPriceDirectLogisticService $service */
  201. $service = app("OwnerPriceDirectLogisticService");
  202. list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  203. }
  204. $obj = [
  205. "owner_id" => $owner_id,
  206. "worked_at"=> $waybill->updated_at,
  207. "type" => "发货",
  208. "operation_bill" => $waybill->waybill_number,
  209. "province" => $waybill->order_id ? ($waybill->order->province ?? '') : ($waybill->destinationCity->parent->name ?? ''),
  210. "consignee_name" => $waybill->recipient,
  211. "consignee_phone" => $waybill->recipient_mobile,
  212. "commodity_amount" => $waybill->amount,
  213. "logistic_bill" => $waybill->carrier_bill,
  214. "volume" =>$waybill->carrier_weight ?? $waybill->warehouse_weight,
  215. "weight" => $waybill->carrier_weight_other ?? $waybill->warehouse_weight_other,
  216. "logistic_id" => $waybill->logistic_id,
  217. "logistic_fee" => $fee,
  218. "outer_id" => $waybill->id,
  219. "outer_table_name" => "waybills",
  220. "logistic_tax_fee" => $taxFee,
  221. ];
  222. if ($detail)app("OwnerFeeDetailService")->updateFind($detail,$obj);
  223. else OwnerFeeDetail::query()->create($obj);
  224. return true;
  225. }
  226. }