|
|
@@ -0,0 +1,186 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Jobs;
|
|
|
+
|
|
|
+use App\Order;
|
|
|
+use App\OwnerFeeDetail;
|
|
|
+use App\OwnerFeeDetailLogistic;
|
|
|
+use App\Process;
|
|
|
+use App\Province;
|
|
|
+use App\Services\CacheService;
|
|
|
+use App\Services\OwnerPriceDirectLogisticService;
|
|
|
+use App\Services\OwnerPriceExpressService;
|
|
|
+use App\Services\OwnerPriceLogisticService;
|
|
|
+use App\Services\OwnerPriceOperationService;
|
|
|
+use App\Store;
|
|
|
+use App\Waybill;
|
|
|
+use Illuminate\Bus\Queueable;
|
|
|
+use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
+use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
+use Illuminate\Queue\InteractsWithQueue;
|
|
|
+use Illuminate\Queue\SerializesModels;
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
+
|
|
|
+class ResetInstantBill implements ShouldQueue
|
|
|
+{
|
|
|
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
+
|
|
|
+ /** @var \stdClass $detail */
|
|
|
+ private $detail;
|
|
|
+ /**
|
|
|
+ * Create a new job instance.
|
|
|
+ *
|
|
|
+ * @param OwnerFeeDetail $detail
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function __construct(OwnerFeeDetail $detail)
|
|
|
+ {
|
|
|
+ $this->detail = $detail;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Execute the job.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function handle()
|
|
|
+ {
|
|
|
+ switch ($this->detail->outer_table_name){
|
|
|
+ case "orders":
|
|
|
+ /** @var \stdClass $order */
|
|
|
+ $order = Order::query()->find($this->detail->outer_id);
|
|
|
+ $key = date("Y-m")."_".$order->owner_id;
|
|
|
+ if (Cache::has($key))Cache::increment($key);
|
|
|
+ else Cache::put($key,1,2678400);
|
|
|
+
|
|
|
+ $order->loadMissing(["logistic","shop","packages.commodities.commodity","batch"]);
|
|
|
+
|
|
|
+ /** @var OwnerPriceExpressService $service */
|
|
|
+ $service = app("OwnerPriceExpressService");
|
|
|
+ $logistic_fee = 0;
|
|
|
+ $commodities = [];
|
|
|
+ $amount = 0;
|
|
|
+ $volume = 0;
|
|
|
+ $weight = 0;
|
|
|
+ $logistic_bill = "";
|
|
|
+
|
|
|
+ if (!$order->logistic || $order->logistic->type != "快递")$logistic_fee = null;
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($order->packages as &$package){
|
|
|
+ $logistic_bill .= $package->logistic_number.",";
|
|
|
+ $volume += $package->bulk;
|
|
|
+ $weight += $package->weight;
|
|
|
+
|
|
|
+ // 四维转二维
|
|
|
+ $partAmount = 0;
|
|
|
+ foreach($package->commodities as &$commodity){
|
|
|
+ $commodity["commodity_name"] = $commodity->commodity ? $commodity->commodity->name : '';
|
|
|
+ $commodity["sku"] = $commodity->commodity ? $commodity->commodity->sku : '';
|
|
|
+ $partAmount += $commodity->amount;
|
|
|
+ }
|
|
|
+ $amount += $partAmount;
|
|
|
+ $commodities = array_merge($commodities,$package->commodities->toArray());
|
|
|
+
|
|
|
+ $provinceName = mb_substr($order->province,0,2);
|
|
|
+ $province = app(CacheService::class)->getOrExecute("province_".$provinceName,function ()use($provinceName){
|
|
|
+ return Province::query()->where("name","like",$provinceName."%")->first();
|
|
|
+ },86400);
|
|
|
+ if ($province){
|
|
|
+ $fee = $service->matching($package->weight, $order->owner_id, $order->logistic_id, $province->id);
|
|
|
+ }else{
|
|
|
+ $logistic_fee = null;
|
|
|
+ $fee = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $items[] = [
|
|
|
+ "amount" => $partAmount,
|
|
|
+ "logistic_bill" => $package->logistic_number,
|
|
|
+ "volume"=>$package->bulk,
|
|
|
+ "weight"=>$package->weight,
|
|
|
+ "logistic_fee" => $fee>0 ? $fee : null,
|
|
|
+ ];
|
|
|
+ if ($logistic_fee!==null){
|
|
|
+ if ($fee<0)$logistic_fee = null;
|
|
|
+ else $logistic_fee += $fee;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($logistic_fee!==null && $logistic_fee<0)$logistic_fee = null;
|
|
|
+
|
|
|
+ $object = ["commodities"=>$commodities,
|
|
|
+ "logistic_name"=>($order->logistic ? $order->logistic->name : ''),
|
|
|
+ "shop_name"=>($order->shop ? $order->shop->name : ''),
|
|
|
+ "order_type"=>$order->order_type,
|
|
|
+ "batch_type" => $order->batch ? $order->batch->wms_type : '',
|
|
|
+ "owner_id"=>$order->owner_id];
|
|
|
+ $mapping = ["packages"=>"commodities","商品名称"=>"commodity_name",
|
|
|
+ "承运商"=>"logistic_name","店铺类型"=>"shop_name","订单类型"=>"order_type",
|
|
|
+ "波次类型"=>"batch_type"];
|
|
|
+
|
|
|
+ /** @var OwnerPriceOperationService $service */
|
|
|
+ $service = app("OwnerPriceOperationService");
|
|
|
+ $result = $service->matching($object,$mapping,$order->owner_id,"出库");
|
|
|
+
|
|
|
+
|
|
|
+ $detail = $this->detail->update([
|
|
|
+ "work_fee" => is_array($result) ? ($result["money"]>0 ? $result["money"] : null) : null,
|
|
|
+ "logistic_fee" => $logistic_fee,
|
|
|
+ ]);
|
|
|
+ if ($detail){
|
|
|
+ foreach ($items as $item){
|
|
|
+ OwnerFeeDetailLogistic::query()->where("owner_fee_detail_id",$detail->id)
|
|
|
+ ->where("logistic_bill",$item["logistic_bill"])
|
|
|
+ ->update($item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "processes":
|
|
|
+ /** @var \stdClass $process */
|
|
|
+ $process = Process::query()->with("processStatistic")->find($this->detail->outer_id);
|
|
|
+ $this->detail->update([
|
|
|
+ "work_fee" => $process->processStatistic ? $process->processStatistic->revenue : null,
|
|
|
+ ]);
|
|
|
+ break;
|
|
|
+ case "waybills":
|
|
|
+ /** @var \stdClass $waybill */
|
|
|
+ $waybill = Waybill::query()->find($this->detail->outer_id);
|
|
|
+ $waybill->loadMissing(["destinationCity","order.owner"]);
|
|
|
+ if (!$waybill->destinationCity && !$waybill->order)break;
|
|
|
+
|
|
|
+ $owner_id = $waybill->order->owner_id ?? $waybill->owner_id;
|
|
|
+
|
|
|
+ if ($waybill->type == "专线"){
|
|
|
+ /** @var OwnerPriceLogisticService $service */
|
|
|
+ $service = app("OwnerPriceLogisticService");
|
|
|
+ $fee = $service->matching($waybill->carrier_weight_other,$owner_id,$waybill->logistic_id,
|
|
|
+ $waybill->carrier_weight_unit_id_other,$waybill->order ? app("RegionService")->getProvince($waybill->order->province) : $waybill->destinationCity->province_id,
|
|
|
+ $waybill->destination_city_id);
|
|
|
+ }else{
|
|
|
+ /** @var OwnerPriceDirectLogisticService $service */
|
|
|
+ $service = app("OwnerPriceDirectLogisticService");
|
|
|
+ $fee = $service->matching($waybill->mileage,$owner_id,$waybill->carType_id);
|
|
|
+ }
|
|
|
+ $this->detail->update([
|
|
|
+ "logistic_fee" => $fee ?? null,
|
|
|
+ ]);
|
|
|
+ break;
|
|
|
+ case "stores":
|
|
|
+ /** @var \stdClass $store */
|
|
|
+ $store = Store::query()->find($this->detail->outer_id);
|
|
|
+ $store->loadMissing("storeItems");
|
|
|
+
|
|
|
+ /** @var OwnerPriceOperationService $service */
|
|
|
+ $service = app("OwnerPriceOperationService");
|
|
|
+
|
|
|
+ $mapping = ["packages" => "storeItems", "商品名称" => "name", "订单类型" => "stored_method", "订单数"=>"amount"];
|
|
|
+
|
|
|
+ $result = $service->matching($store, $mapping, $store->owner_id, "入库");
|
|
|
+
|
|
|
+ $this->detail->update([
|
|
|
+ "work_fee" => is_array($result) ? ($result["money"]>0 ? $result["money"] : null) : null,
|
|
|
+ ]);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|