ResetInstantBill.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $isBunched = $order->logistic && $order->logistic->is_bunched=='Y';
  62. $weightExceptionMark = false;
  63. $provinceId = null;
  64. $logisticTaxFee = 0;
  65. foreach ($order->packages as &$package){
  66. $tax = 0;
  67. $logistic_bill .= $package->logistic_number.",";
  68. $volume += $package->bulk;
  69. $weight += $package->weight;
  70. if (!$weightExceptionMark && (!$package->weight || $package->weight<0))$weightExceptionMark = true;
  71. $partAmount = 0;
  72. foreach($package->commodities as $commodity){
  73. $partAmount += $commodity->amount;
  74. }
  75. $amount += $partAmount;
  76. $provinceName = mb_substr($order->province,0,2);
  77. $province = app(CacheService::class)->getOrExecute("province_".$provinceName,function ()use($provinceName){
  78. return Province::query()->where("name","like",$provinceName."%")->first();
  79. },86400);
  80. $fee = null;
  81. if ($province){
  82. if (!$provinceId)$provinceId = $province->id;
  83. else if ($provinceId!=$province->id)$weightExceptionMark = true;
  84. if (!$isBunched)list($fee,$tax) = $service->matching($package->weight, $order->owner_id, $order->logistic_id, $province->id);
  85. }else $logistic_fee = null;
  86. $items[] = [
  87. "amount" => $partAmount,
  88. "logistic_bill" => $package->logistic_number,
  89. "volume"=>$package->bulk,
  90. "weight"=>$package->weight,
  91. "logistic_fee" => $fee,
  92. "tax_fee" => $tax,
  93. ];
  94. if ($logistic_fee!==null){
  95. if (!$fee || $fee<0)$logistic_fee = null;
  96. else $logistic_fee += $fee;
  97. }
  98. $logisticTaxFee += $tax;
  99. }
  100. /* 为字母单 且 重量与省份完整 且 重量大于0 且 省份存在 进入子母单计算 */
  101. if ($isBunched && !$weightExceptionMark && $weight>0 && $provinceId)list($logistic_fee,$logisticTaxFee) = $service->matching($weight, $order->owner_id, $order->logistic_id, $provinceId);
  102. if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
  103. /** @var OwnerPriceOperationService $service */
  104. $service = app("OwnerPriceOperationService");
  105. list($id,$money,$workTaxFee) = $service->matching($order,Feature::MAPPING["order"],$order->owner_id,"出库");
  106. $this->detail->update([
  107. "owner_id" => $order->owner_id,
  108. "worked_at" => $order->wms_edittime ?? $order->updated_at,
  109. "shop_id" => $order->shop_id,
  110. "operation_bill" => $order->code,
  111. "consignee_name" => $order->consignee_name,
  112. "consignee_phone" => $order->consignee_phone,
  113. "commodity_amount" => $amount,
  114. "logistic_bill" => rtrim($logistic_bill,","),
  115. "volume" => $volume,
  116. "weight" => $weight,
  117. "logistic_id" => $order->logistic_id,
  118. "work_fee" => $money,
  119. "owner_price_operation_id" => $id,
  120. "logistic_fee" => $logistic_fee,
  121. "work_tax_fee" => $workTaxFee,
  122. "logistic_tax_fee" => $logistic_fee ? $logisticTaxFee : null,
  123. ]);
  124. OwnerFeeDetailLogistic::query()->where("owner_fee_detail_id",$this->detail->id)->delete();
  125. foreach ($items as &$item)$item["owner_fee_detail_id"] = $this->detail->id;
  126. if (count($items)>1)OwnerFeeDetailLogistic::query()->insert($items);
  127. app("OrderService")->setOrderQuantity($order->owner_id,$order->logistic_id);
  128. break;
  129. case "processes":
  130. /** @var \stdClass $process */
  131. $process = Process::query()->with("processStatistic")->find($this->detail->outer_id);
  132. $this->detail->update([
  133. "work_fee" => $process->processStatistic ? $process->processStatistic->revenue : null,
  134. ]);
  135. break;
  136. case "waybills":
  137. /** @var \stdClass $waybill */
  138. $waybill = Waybill::query()->find($this->detail->outer_id);
  139. $waybill->loadMissing(["destinationCity","order.owner"]);
  140. if (!$waybill->destinationCity && !$waybill->order)break;
  141. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  142. $detail = OwnerFeeDetail::query()->where("type","发货")
  143. ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first();
  144. if ($detail && $detail->logistic_fee !== null)break;
  145. if ($waybill->type == "专线"){
  146. /** @var OwnerPriceLogisticService $service */
  147. $service = app("OwnerPriceLogisticService");
  148. list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  149. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  150. $waybill->destination_city_id);
  151. }else{
  152. /** @var OwnerPriceDirectLogisticService $service */
  153. $service = app("OwnerPriceDirectLogisticService");
  154. list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  155. }
  156. $this->detail->update([
  157. "logistic_fee" => $fee,
  158. "logistic_tax_fee" => $taxFee,
  159. ]);
  160. break;
  161. case "stores":
  162. /** @var \stdClass $store */
  163. $store = Store::query()->find($this->detail->outer_id);
  164. if (!$store || $store->status != "已入库") break;
  165. $store->loadMissing(["storeItems.commodity","warehouse"]);
  166. /** @var OwnerPriceOperationService $service */
  167. $service = app("OwnerPriceOperationService");
  168. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库");
  169. $this->detail->update([
  170. "owner_id" => $store->owner_id,
  171. "worked_at" => $store->updated_at,
  172. "operation_bill" => $store->asn_code,
  173. "commodity_amount" => array_sum(array_column($store->storeItems->toArray(), "amount")),
  174. "work_fee" => $money,
  175. "owner_price_operation_id" => $id,
  176. "work_tax_fee" => $taxFee
  177. ]);
  178. break;
  179. }
  180. }
  181. }