ResetInstantBill.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Jobs;
  3. use App\Order;
  4. use App\OwnerFeeDetail;
  5. use App\OwnerFeeDetailLogistic;
  6. use App\Process;
  7. use App\Province;
  8. use App\Services\CacheService;
  9. use App\Services\OwnerPriceDirectLogisticService;
  10. use App\Services\OwnerPriceExpressService;
  11. use App\Services\OwnerPriceLogisticService;
  12. use App\Services\OwnerPriceOperationService;
  13. use App\Store;
  14. use App\Waybill;
  15. use Illuminate\Bus\Queueable;
  16. use Illuminate\Contracts\Queue\ShouldQueue;
  17. use Illuminate\Foundation\Bus\Dispatchable;
  18. use Illuminate\Queue\InteractsWithQueue;
  19. use Illuminate\Queue\SerializesModels;
  20. use Illuminate\Support\Facades\Cache;
  21. class ResetInstantBill implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. /** @var \stdClass $detail */
  25. private $detail;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @param OwnerFeeDetail $detail
  30. *
  31. * @return void
  32. */
  33. public function __construct(OwnerFeeDetail $detail)
  34. {
  35. $this->detail = $detail;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. switch ($this->detail->outer_table_name){
  45. case "orders":
  46. /** @var \stdClass $order */
  47. $order = Order::query()->find($this->detail->outer_id);
  48. $key = date("Y-m")."_".$order->owner_id;
  49. if (Cache::has($key))Cache::increment($key);
  50. else Cache::put($key,1,2678400);
  51. $order->loadMissing(["logistic","shop","packages.commodities.commodity","batch"]);
  52. /** @var OwnerPriceExpressService $service */
  53. $service = app("OwnerPriceExpressService");
  54. $logistic_fee = 0;
  55. $commodities = [];
  56. $amount = 0;
  57. $volume = 0;
  58. $weight = 0;
  59. $logistic_bill = "";
  60. if (!$order->logistic || $order->logistic->type != "快递")$logistic_fee = null;
  61. $items = [];
  62. foreach ($order->packages as &$package){
  63. $logistic_bill .= $package->logistic_number.",";
  64. $volume += $package->bulk;
  65. $weight += $package->weight;
  66. // 四维转二维
  67. $partAmount = 0;
  68. foreach($package->commodities as &$commodity){
  69. $commodity["commodity_name"] = $commodity->commodity ? $commodity->commodity->name : '';
  70. $commodity["sku"] = $commodity->commodity ? $commodity->commodity->sku : '';
  71. $partAmount += $commodity->amount;
  72. }
  73. $amount += $partAmount;
  74. $commodities = array_merge($commodities,$package->commodities->toArray());
  75. $provinceName = mb_substr($order->province,0,2);
  76. $province = app(CacheService::class)->getOrExecute("province_".$provinceName,function ()use($provinceName){
  77. return Province::query()->where("name","like",$provinceName."%")->first();
  78. },86400);
  79. if ($province){
  80. $fee = $service->matching($package->weight, $order->owner_id, $order->logistic_id, $province->id);
  81. }else{
  82. $logistic_fee = null;
  83. $fee = null;
  84. }
  85. $items[] = [
  86. "amount" => $partAmount,
  87. "logistic_bill" => $package->logistic_number,
  88. "volume"=>$package->bulk,
  89. "weight"=>$package->weight,
  90. "logistic_fee" => $fee>0 ? $fee : null,
  91. ];
  92. if ($logistic_fee!==null){
  93. if ($fee<0)$logistic_fee = null;
  94. else $logistic_fee += $fee;
  95. }
  96. }
  97. if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
  98. $object = ["commodities"=>$commodities,
  99. "logistic_name"=>($order->logistic ? $order->logistic->name : ''),
  100. "shop_name"=>($order->shop ? $order->shop->name : ''),
  101. "order_type"=>$order->order_type,
  102. "batch_type" => $order->batch ? $order->batch->wms_type : '',
  103. "owner_id"=>$order->owner_id];
  104. $mapping = ["packages"=>"commodities","商品名称"=>"commodity_name",
  105. "承运商"=>"logistic_name","店铺类型"=>"shop_name","订单类型"=>"order_type",
  106. "波次类型"=>"batch_type"];
  107. /** @var OwnerPriceOperationService $service */
  108. $service = app("OwnerPriceOperationService");
  109. $result = $service->matching($object,$mapping,$order->owner_id,"出库");
  110. $detail = $this->detail->update([
  111. "work_fee" => is_array($result) ? ($result["money"]>0 ? $result["money"] : null) : null,
  112. "logistic_fee" => $logistic_fee,
  113. ]);
  114. if ($detail){
  115. foreach ($items as $item){
  116. OwnerFeeDetailLogistic::query()->where("owner_fee_detail_id",$detail->id)
  117. ->where("logistic_bill",$item["logistic_bill"])
  118. ->update($item);
  119. }
  120. }
  121. break;
  122. case "processes":
  123. /** @var \stdClass $process */
  124. $process = Process::query()->with("processStatistic")->find($this->detail->outer_id);
  125. $this->detail->update([
  126. "work_fee" => $process->processStatistic ? $process->processStatistic->revenue : null,
  127. ]);
  128. break;
  129. case "waybills":
  130. /** @var \stdClass $waybill */
  131. $waybill = Waybill::query()->find($this->detail->outer_id);
  132. $waybill->loadMissing(["destinationCity","order.owner"]);
  133. if (!$waybill->destinationCity && !$waybill->order)break;
  134. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  135. if ($waybill->type == "专线"){
  136. /** @var OwnerPriceLogisticService $service */
  137. $service = app("OwnerPriceLogisticService");
  138. $fee = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  139. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  140. $waybill->destination_city_id);
  141. }else{
  142. /** @var OwnerPriceDirectLogisticService $service */
  143. $service = app("OwnerPriceDirectLogisticService");
  144. $fee = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  145. }
  146. $this->detail->update([
  147. "logistic_fee" => $fee ?? null,
  148. ]);
  149. break;
  150. case "stores":
  151. /** @var \stdClass $store */
  152. $store = Store::query()->find($this->detail->outer_id);
  153. $store->loadMissing("storeItems");
  154. /** @var OwnerPriceOperationService $service */
  155. $service = app("OwnerPriceOperationService");
  156. $mapping = ["packages" => "storeItems", "商品名称" => "name", "订单类型" => "stored_method", "订单数"=>"amount"];
  157. $result = $service->matching($store, $mapping, $store->owner_id, "入库");
  158. $this->detail->update([
  159. "work_fee" => is_array($result) ? ($result["money"]>0 ? $result["money"] : null) : null,
  160. ]);
  161. break;
  162. }
  163. }
  164. }