WaybillService.php 10 KB

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