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