ResetInstantBill.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\RejectedBill;
  10. use App\Services\CacheService;
  11. use App\Services\OwnerPriceDirectLogisticService;
  12. use App\Services\OwnerPriceExpressService;
  13. use App\Services\OwnerPriceLogisticService;
  14. use App\Services\OwnerPriceOperationService;
  15. use App\Store;
  16. use App\StoreRejected;
  17. use App\Waybill;
  18. use Illuminate\Bus\Queueable;
  19. use Illuminate\Contracts\Queue\ShouldQueue;
  20. use Illuminate\Foundation\Bus\Dispatchable;
  21. use Illuminate\Queue\InteractsWithQueue;
  22. use Illuminate\Queue\SerializesModels;
  23. use Illuminate\Support\Facades\Cache;
  24. class ResetInstantBill implements ShouldQueue
  25. {
  26. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  27. /** @var \stdClass $detail */
  28. private $detail;
  29. /**
  30. * Create a new job instance.
  31. *
  32. * @param OwnerFeeDetail $detail
  33. *
  34. * @return void
  35. */
  36. public function __construct(OwnerFeeDetail $detail)
  37. {
  38. $this->detail = $detail;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. switch ($this->detail->outer_table_name){
  48. case "orders":
  49. /** @var \stdClass $order */
  50. //检查订单对象
  51. $order = Order::query()->find($this->detail->outer_id);
  52. if (!$order || $order->wms_status != "订单完成")break;
  53. $order->loadMissing(["logistic","shop","packages.commodities.commodity","batch"]);
  54. /** @var OwnerPriceExpressService $service */
  55. $service = app("OwnerPriceExpressService");
  56. $logistic_fee = 0;
  57. $amount = 0;
  58. $volume = 0;
  59. $weight = 0;
  60. $logistic_bill = "";
  61. if (!$order->logistic || $order->logistic->type == "物流")$logistic_fee = null;
  62. $items = [];
  63. $isBunched = $order->logistic && $order->logistic->is_bunched=='Y';
  64. $weightExceptionMark = false;
  65. $provinceId = null;
  66. $logisticTaxFee = 0;
  67. foreach ($order->packages as &$package){
  68. $tax = 0;
  69. $logistic_bill .= $package->logistic_number.",";
  70. $volume += $package->bulk;
  71. $weight += $package->weight;
  72. if (!$weightExceptionMark && (!$package->weight || $package->weight<0))$weightExceptionMark = true;
  73. $partAmount = 0;
  74. foreach($package->commodities as $commodity){
  75. $partAmount += $commodity->amount;
  76. }
  77. $amount += $partAmount;
  78. $provinceName = mb_substr($order->province,0,2);
  79. $province = app(CacheService::class)->getOrExecute("province_".$provinceName,function ()use($provinceName){
  80. return Province::query()->where("name","like",$provinceName."%")->first();
  81. },86400);
  82. $fee = null;
  83. if ($province){
  84. if (!$provinceId)$provinceId = $province->id;
  85. else if ($provinceId!=$province->id)$weightExceptionMark = true;
  86. if (!$isBunched)list($fee,$tax) = $service->matching($package->weight, $order->owner_id, $order->logistic_id, $province->id);
  87. }else $logistic_fee = null;
  88. $items[] = [
  89. "amount" => $partAmount,
  90. "logistic_bill" => $package->logistic_number,
  91. "volume"=>$package->bulk,
  92. "weight"=>$package->weight,
  93. "logistic_fee" => $fee,
  94. "tax_fee" => $tax,
  95. ];
  96. if ($logistic_fee!==null){
  97. if (!$fee || $fee<0)$logistic_fee = null;
  98. else $logistic_fee += $fee;
  99. }
  100. $logisticTaxFee += $tax;
  101. }
  102. /* 为字母单 且 重量与省份完整 且 重量大于0 且 省份存在 进入子母单计算 */
  103. if ($isBunched && !$weightExceptionMark && $weight>0 && $provinceId)list($logistic_fee,$logisticTaxFee) = $service->matching($weight, $order->owner_id, $order->logistic_id, $provinceId);
  104. if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
  105. /** @var OwnerPriceOperationService $service */
  106. $service = app("OwnerPriceOperationService");
  107. list($id,$money,$workTaxFee) = $service->matching($order,Feature::MAPPING["order"],$order->owner_id,"出库");
  108. $this->detail->update([
  109. "owner_id" => $order->owner_id,
  110. "worked_at" => $order->wms_edittime ?? $order->updated_at,
  111. "shop_id" => $order->shop_id,
  112. "operation_bill" => $order->code,
  113. "consignee_name" => $order->consignee_name,
  114. "consignee_phone" => $order->consignee_phone,
  115. "commodity_amount" => $amount,
  116. "logistic_bill" => rtrim($logistic_bill,","),
  117. "volume" => $volume,
  118. "weight" => $weight,
  119. "logistic_id" => $order->logistic_id,
  120. "work_fee" => $money,
  121. "owner_price_operation_id" => $id,
  122. "logistic_fee" => $logistic_fee,
  123. "work_tax_fee" => $workTaxFee,
  124. "logistic_tax_fee" => $logistic_fee ? $logisticTaxFee : null,
  125. ]);
  126. OwnerFeeDetailLogistic::query()->where("owner_fee_detail_id",$this->detail->id)->delete();
  127. foreach ($items as &$item)$item["owner_fee_detail_id"] = $this->detail->id;
  128. if (count($items)>1)OwnerFeeDetailLogistic::query()->insert($items);
  129. app("OrderService")->setOrderQuantity($order->owner_id,$order->logistic_id);
  130. break;
  131. case "processes":
  132. /** @var \stdClass $process */
  133. $process = Process::query()->with("processStatistic")->find($this->detail->outer_id);
  134. $this->detail->update([
  135. "work_fee" => $process->processStatistic ? $process->processStatistic->revenue : null,
  136. ]);
  137. break;
  138. case "waybills":
  139. /** @var \stdClass $waybill */
  140. $waybill = Waybill::query()->find($this->detail->outer_id);
  141. $waybill->loadMissing(["destinationCity","order.owner"]);
  142. if (!$waybill->destinationCity && !$waybill->order)break;
  143. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  144. $detail = OwnerFeeDetail::query()->where("type","发货")
  145. ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first();
  146. if ($detail && $detail->logistic_fee !== null)break;
  147. if ($waybill->type == "专线"){
  148. /** @var OwnerPriceLogisticService $service */
  149. $service = app("OwnerPriceLogisticService");
  150. list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  151. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  152. $waybill->destination_city_id);
  153. }else{
  154. /** @var OwnerPriceDirectLogisticService $service */
  155. $service = app("OwnerPriceDirectLogisticService");
  156. list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  157. }
  158. $this->detail->update([
  159. "logistic_fee" => $fee,
  160. "logistic_tax_fee" => $taxFee,
  161. ]);
  162. break;
  163. case "stores":
  164. /** @var \stdClass $store */
  165. $store = Store::query()->find($this->detail->outer_id);
  166. if (!$store || $store->status != "已入库") break;
  167. $store->loadMissing(["storeItems.commodity","warehouse"]);
  168. /** @var OwnerPriceOperationService $service */
  169. $service = app("OwnerPriceOperationService");
  170. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库");
  171. $this->detail->update([
  172. "owner_id" => $store->owner_id,
  173. "worked_at" => $store->updated_at,
  174. "operation_bill" => $store->asn_code,
  175. "commodity_amount" => array_sum(array_column($store->storeItems->toArray(), "amount")),
  176. "work_fee" => $money,
  177. "owner_price_operation_id" => $id,
  178. "work_tax_fee" => $taxFee
  179. ]);
  180. break;
  181. case "rejected_bills":
  182. $rejectedBill = RejectedBill::query()->find($this->detail->outer_id);
  183. /** @var \stdClass $rejectedBill */
  184. $number = array_column(StoreRejected::query()->where("logistic_number_return",$rejectedBill->logistic_number_return)->get()->toArray(),"store_id");
  185. foreach (Store::query()->with("storeItems")->whereIn("id",$number)->get() as $store){
  186. /** @var OwnerPriceOperationService $service */
  187. $service = app("OwnerPriceOperationService");
  188. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库",0);
  189. $bill = OwnerFeeDetail::query()->where("outer_id",$store->id)->where("outer_table_name","stores")->first();
  190. if ($bill) $bill->update([
  191. "work_fee" => $money,
  192. "owner_price_operation_id" => $id,
  193. "outer_id" => $rejectedBill->id,
  194. "outer_table_name" => "rejected_bills",
  195. ]); else app("OwnerFeeDetailService")->create([
  196. "owner_id" => $store->owner_id,
  197. "worked_at" => $store->created_at,
  198. "type" => "收货",
  199. "operation_bill" => $store->asn_code,
  200. "commodity_amount" => array_sum(array_column($store->storeItems->toArray(), "amount")),
  201. "work_fee" => $money,
  202. "owner_price_operation_id" => $id,
  203. "created_at" => date('Y-m-d H:i:s'),
  204. "outer_id" => $rejectedBill->id,
  205. "outer_table_name" => "rejected_bills",
  206. "work_tax_fee" => $taxFee,
  207. ]);
  208. }
  209. }
  210. }
  211. }