WaybillService.php 12 KB

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