ResetInstantBill.php 7.9 KB

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