ResetInstantBill.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\OwnerFeeExpress;
  8. use App\OwnerFeeLogistic;
  9. use App\Process;
  10. use App\RejectedBill;
  11. use App\Services\OwnerPriceDirectLogisticService;
  12. use App\Services\OwnerPriceLogisticService;
  13. use App\Services\OwnerPriceOperationService;
  14. use App\Store;
  15. use App\StoreRejected;
  16. use App\Waybill;
  17. use Illuminate\Bus\Queueable;
  18. use Illuminate\Contracts\Queue\ShouldQueue;
  19. use Illuminate\Foundation\Bus\Dispatchable;
  20. use Illuminate\Queue\InteractsWithQueue;
  21. class ResetInstantBill implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable;
  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. $order = Order::query()->find($this->detail->outer_id);
  47. if (!$order)break;
  48. //加载所需数据
  49. $order->loadMissing(["logistic","shop","packages.commodities.commodity","batch"]);
  50. //获取运输费
  51. $logisticInfo = app("OrderService")->getLogisticFeeInfo($order);
  52. //获取作业费
  53. /** @var OwnerPriceOperationService $service */
  54. $service = app("OwnerPriceOperationService");
  55. $GLOBALS["FEE_INFO"] = [];
  56. list($id,$money,$workTaxFee) = $service->matching($order,Feature::MAPPING["order"],$order->owner_id);
  57. //数据组装
  58. OwnerFeeDetail::query()->where("id",$this->detail->id)->update([
  59. "commodity_amount" => $logisticInfo["amount"],
  60. "logistic_bill" => $logisticInfo["logisticBill"],
  61. "volume" => $logisticInfo["volume"],
  62. "weight" => $logisticInfo["weight"],
  63. "logistic_id" => $order->logistic_id,
  64. "work_fee" => $money,
  65. "owner_price_operation_id" => $id,
  66. "logistic_fee" => $logisticInfo["logisticFee"],
  67. "created_at" => date('Y-m-d H:i:s'),
  68. "outer_id" => $order->id,
  69. "outer_table_name" => "orders",
  70. "work_tax_fee" => $workTaxFee,
  71. "logistic_tax_fee" => $logisticInfo["logisticTaxFee"],
  72. ]);
  73. if ($logisticInfo["logisticFee"] && $logisticInfo["fee_info"])OwnerFeeExpress::query()->insert($logisticInfo["fee_info"]);
  74. if ($money>0)app("StoreService")->constructFeeInfo([
  75. "worked_at" => $order->wms_edittime ?: $order->updated_at,
  76. "owner_id" => $order->owner_id,
  77. "model_id" => $id,
  78. "source_number"=> $order->client_code,
  79. "doc_number" => $order->code,
  80. "commodity_id" => 0,
  81. "total_fee" =>0,
  82. "tax_rate" =>0,
  83. "fee_description"=>'',
  84. ]);
  85. //后续处理
  86. OwnerFeeDetailLogistic::query()->where("owner_fee_detail_id",$this->detail->id)->delete();
  87. foreach ($logisticInfo["items"] as &$item)$item["owner_fee_detail_id"] = $this->detail->id;
  88. if (count($logisticInfo["items"])>1)OwnerFeeDetailLogistic::query()->insert($logisticInfo["items"]);
  89. break;
  90. case "processes":
  91. $process = Process::query()->with("processStatistic")->find($this->detail->outer_id);
  92. $this->detail->update([
  93. "work_fee" => $process->processStatistic ? $process->processStatistic->revenue : null,
  94. ]);
  95. break;
  96. case "waybills":
  97. /** @var \stdClass $waybill */
  98. $waybill = Waybill::query()->find($this->detail->outer_id);
  99. if (!$waybill)break;
  100. $waybill->loadMissing(["destinationCity","order.owner"]);
  101. if (!$waybill->destinationCity && !$waybill->order)break;
  102. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  103. if ($waybill->type == "专线"){
  104. $provinceId = $waybill->order ? app("ProvinceService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id;
  105. $cityId = $waybill->destination_city_id;
  106. $consigneeName = $waybill->order ? $waybill->order->consignee_name : $waybill->recipient;
  107. $consigneePhone = $waybill->order ? $waybill->order->consignee_phone : $waybill->recipient_mobile;
  108. $GLOBALS["FEE_INFO"] = [
  109. "province_id" => $provinceId,
  110. "owner_id" => $waybill->owner_id,
  111. "city_id" => $cityId,
  112. "logistic_id" => $waybill->logistic_id,
  113. "order_number" => $waybill->wms_bill_number,
  114. "recipient_name"=>$consigneeName,
  115. "recipient_phone"=>$consigneePhone,
  116. "quantity"=>$waybill->carrier_weight_other,
  117. "unit_id"=>$waybill->carrier_weight_unit_id_other,
  118. "remark"=>$waybill->ordering_remark,
  119. "created_at"=>$waybill->updated_at,
  120. ];
  121. /** @var OwnerPriceLogisticService $service */
  122. $service = app("OwnerPriceLogisticService");
  123. list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  124. $waybill->carrier_weight_unit_id_other,$provinceId, $cityId);
  125. if (isset($GLOBALS["FEE_INFO"])) OwnerFeeLogistic::query()->create($GLOBALS["FEE_INFO"]);
  126. }else{
  127. /** @var OwnerPriceDirectLogisticService $service */
  128. $service = app("OwnerPriceDirectLogisticService");
  129. list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  130. }
  131. $obj = [
  132. "logistic_fee" => $fee,
  133. "logistic_tax_fee" => $taxFee,
  134. ];
  135. app("OwnerFeeDetailService")->updateFind($this->detail,$obj);
  136. break;
  137. case "stores":
  138. /** @var \stdClass $store */
  139. $store = Store::query()->find($this->detail->outer_id);
  140. if (!$store)break;
  141. $store->loadMissing(["storeItems.commodity","warehouse"]);
  142. /** @var OwnerPriceOperationService $service */
  143. $service = app("OwnerPriceOperationService");
  144. $GLOBALS["FEE_INFO"] = [];
  145. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库");
  146. $amount = 0;
  147. if ($store->storeItems)foreach ($store->storeItems as $item)$amount += $item->amount;
  148. if (($this->detail->commodity_amount - $amount)!==0)app("StoreService")->setStoreAmount($store->owner_id,$this->detail->commodity_amount - $amount);
  149. $this->detail->update([
  150. "commodity_amount" => $amount,
  151. "work_fee" => $money,
  152. "owner_price_operation_id" => $id,
  153. "work_tax_fee" => $taxFee,
  154. ]);
  155. if ($money>0)app("StoreService")->constructFeeInfo([
  156. "worked_at" => $store->updated_at,
  157. "owner_id" => $store->owner_id,
  158. "model_id" => $id,
  159. "source_number"=> null,
  160. "doc_number" => $store->asn_code,
  161. "commodity_id" => 0,
  162. "total_fee" =>0,
  163. "tax_rate" =>0,
  164. "fee_description"=>'',
  165. ]);
  166. break;
  167. case "rejected_bills":
  168. /** @var \stdClass $rejectedBill */
  169. $number = array_column(StoreRejected::query()->where("logistic_number_return",$rejectedBill->logistic_number_return)->get()->toArray(),"store_id");
  170. $stores = Store::query()->with("storeItems")->whereIn("id",$number)->get();
  171. /** @var OwnerPriceOperationService $service */
  172. $service = app("OwnerPriceOperationService");
  173. foreach ($stores as $store){
  174. $GLOBALS["FEE_INFO"] = [];
  175. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库",0);
  176. app("StoreService")->clearFeeInfo($store->asn_code);
  177. $this->detail->update([
  178. "work_fee" => $money,
  179. "owner_price_operation_id" => $id,
  180. "work_tax_fee" => $taxFee,
  181. ]);
  182. app("StoreService")->constructFeeInfo([
  183. "worked_at" => $rejectedBill->updated_at,
  184. "owner_id" => $rejectedBill->id_owner,
  185. "model_id" => $id,
  186. "source_number"=> null,
  187. "doc_number" => $store->asn_code,
  188. "commodity_id" => 0,
  189. "total_fee" =>0,
  190. "tax_rate" =>0,
  191. "fee_description"=>'',
  192. ]);
  193. }
  194. if ($stores->count())return;
  195. $rejectedBill = RejectedBill::query()->find($this->detail->outer_id);
  196. $rejectedBill->loadMissing("items");
  197. $GLOBALS["FEE_INFO"] = [];
  198. list($id,$money,$taxFee) = $service->matching($rejectedBill, Feature::MAPPING["rejected_bill"], $rejectedBill->id_owner, "入库",0);
  199. $this->detail->update([
  200. "commodity_amount" => $rejectedBill->goods_amount,
  201. "work_fee" => $money,
  202. "owner_price_operation_id" => $id,
  203. "work_tax_fee" => $taxFee,
  204. ]);
  205. app("StoreService")->constructFeeInfo([
  206. "worked_at" => $rejectedBill->updated_at,
  207. "owner_id" => $rejectedBill->id_owner,
  208. "model_id" => $id,
  209. "source_number"=> null,
  210. "doc_number" => null,
  211. "commodity_id" => 0,
  212. "total_fee" =>0,
  213. "tax_rate" =>0,
  214. "fee_description"=>'',
  215. ]);
  216. }
  217. }
  218. }