ResetInstantBill.php 11 KB

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