WaybillService.php 12 KB

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