ResetInstantBill.php 10 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\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. //检查订单对象
  50. $order = Order::query()->find($this->detail->outer_id);
  51. if (!$order || $order->wms_status != "订单完成")break;
  52. $order->loadMissing(["logistic","shop","packages.commodities.commodity","batch"]);
  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. if ($isBunched && !$weightExceptionMark && $weight>0 && $provinceId)list($logistic_fee,$logisticTaxFee) = $service->matching($weight, $order->owner_id, $order->logistic_id, $provinceId);
  101. if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
  102. $service = app("OwnerPriceOperationService");
  103. list($id,$money,$workTaxFee) = $service->matching($order,Feature::MAPPING["order"],$order->owner_id,"出库");
  104. $this->detail->update([
  105. "owner_id" => $order->owner_id,
  106. "worked_at" => $order->wms_edittime ?? $order->updated_at,
  107. "shop_id" => $order->shop_id,
  108. "operation_bill" => $order->code,
  109. "consignee_name" => $order->consignee_name,
  110. "consignee_phone" => $order->consignee_phone,
  111. "commodity_amount" => $amount,
  112. "logistic_bill" => rtrim($logistic_bill,","),
  113. "volume" => $volume,
  114. "weight" => $weight,
  115. "logistic_id" => $order->logistic_id,
  116. "work_fee" => $money,
  117. "owner_price_operation_id" => $id,
  118. "logistic_fee" => $logistic_fee,
  119. "work_tax_fee" => $workTaxFee,
  120. "logistic_tax_fee" => $logistic_fee ? $logisticTaxFee : null,
  121. ]);
  122. OwnerFeeDetailLogistic::query()->where("owner_fee_detail_id",$this->detail->id)->delete();
  123. foreach ($items as &$item)$item["owner_fee_detail_id"] = $this->detail->id;
  124. if (count($items)>1)OwnerFeeDetailLogistic::query()->insert($items);
  125. app("OrderService")->setOrderQuantity($order->owner_id,$order->logistic_id);
  126. break;
  127. case "processes":
  128. $process = Process::query()->with("processStatistic")->find($this->detail->outer_id);
  129. $this->detail->update([
  130. "work_fee" => $process->processStatistic ? $process->processStatistic->revenue : null,
  131. ]);
  132. break;
  133. case "waybills":
  134. $waybill = Waybill::query()->find($this->detail->outer_id);
  135. $waybill->loadMissing(["destinationCity","order.owner"]);
  136. if (!$waybill->destinationCity && !$waybill->order)break;
  137. $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
  138. $detail = OwnerFeeDetail::query()->where("type","发货")
  139. ->where("owner_id",$owner_id)->whereIn("operation_bill",[$waybill->wms_bill_number,$waybill->waybill_number])->first();
  140. if ($detail && $detail->logistic_fee !== null)break;
  141. if ($waybill->type == "专线"){
  142. $service = app("OwnerPriceLogisticService");
  143. list($fee,$taxFee) = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
  144. $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
  145. $waybill->destination_city_id);
  146. }else{
  147. $service = app("OwnerPriceDirectLogisticService");
  148. list($fee,$taxFee) = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
  149. }
  150. $this->detail->update([
  151. "logistic_fee" => $fee,
  152. "logistic_tax_fee" => $taxFee,
  153. ]);
  154. break;
  155. case "stores":
  156. $store = Store::query()->find($this->detail->outer_id);
  157. if (!$store || $store->status != "已入库") break;
  158. $store->loadMissing(["storeItems.commodity","warehouse"]);
  159. $service = app("OwnerPriceOperationService");
  160. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库");
  161. $this->detail->update([
  162. "owner_id" => $store->owner_id,
  163. "worked_at" => $store->updated_at,
  164. "operation_bill" => $store->asn_code,
  165. "commodity_amount" => array_sum(array_column($store->storeItems->toArray(), "amount")),
  166. "work_fee" => $money,
  167. "owner_price_operation_id" => $id,
  168. "work_tax_fee" => $taxFee
  169. ]);
  170. break;
  171. case "rejected_bills":
  172. $rejectedBill = RejectedBill::query()->find($this->detail->outer_id);
  173. $number = array_column(StoreRejected::query()->where("logistic_number_return",$rejectedBill->logistic_number_return)->get()->toArray(),"store_id");
  174. foreach (Store::query()->with("storeItems")->whereIn("id",$number)->get() as $store){
  175. $service = app("OwnerPriceOperationService");
  176. list($id,$money,$taxFee) = $service->matching($store, Feature::MAPPING["store"], $store->owner_id, "入库",0);
  177. $bill = OwnerFeeDetail::query()->where("outer_id",$store->id)->where("outer_table_name","stores")->first();
  178. if ($bill) $bill->update([
  179. "work_fee" => $money,
  180. "owner_price_operation_id" => $id,
  181. "outer_id" => $rejectedBill->id,
  182. "outer_table_name" => "rejected_bills",
  183. ]); else app("OwnerFeeDetailService")->create([
  184. "owner_id" => $store->owner_id,
  185. "worked_at" => $store->created_at,
  186. "type" => "收货",
  187. "operation_bill" => $store->asn_code,
  188. "commodity_amount" => array_sum(array_column($store->storeItems->toArray(), "amount")),
  189. "work_fee" => $money,
  190. "owner_price_operation_id" => $id,
  191. "created_at" => date('Y-m-d H:i:s'),
  192. "outer_id" => $rejectedBill->id,
  193. "outer_table_name" => "rejected_bills",
  194. "work_tax_fee" => $taxFee,
  195. ]);
  196. }
  197. }*/
  198. }
  199. }