WaybillService.php 12 KB

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