Przeglądaj źródła

客户管理-即时账单生成服务队列分发

Zhouzhendong 5 lat temu
rodzic
commit
d3995dcde7

+ 3 - 2
app/Console/Commands/CreateOwnerBillReport.php

@@ -46,7 +46,8 @@ class CreateOwnerBillReport extends Command
             $year--;
             $lastMonth = '12';
         }else $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
-        $bills = DB::select(DB::raw("select owner_id,SUM(work_fee)+SUM(logistic_fee) as total from owner_fee_details where worked_at like ? GROUP BY owner_id"),[$year."-".$lastMonth."%"]);
+        $sql = "SELECT owner_id,SUM(work_fee)+SUM(logistic_fee) AS total FROM owner_fee_details WHERE worked_at LIKE ? AND (type = '发货' AND logistic_fee IS NOT NULL AND work_fee IS NOT NULL) OR (type <> '发货' AND work_fee IS NOT NULL)  GROUP BY owner_id";
+        $billDetails = DB::select(DB::raw($sql),[$year."-".$lastMonth."%"]);
 
         $areas = OwnerAreaReport::query()->with("ownerStoragePriceModel")->where("counting_month","like",$year."-".$lastMonth."%")->get();
         $map = [];
@@ -62,7 +63,7 @@ class CreateOwnerBillReport extends Command
             }
         }
 
-        $chunks = array_chunk($bills,50);
+        $chunks = array_chunk($billDetails,50);
         foreach ($chunks as $bills){
             $date = date('Y-m-d H:i:s');
             $createOwnerBillReport = [];

+ 10 - 51
app/Http/Controllers/TestController.php

@@ -97,58 +97,17 @@ class TestController extends Controller
             app(BatchUpdateService::class)->batchUpdate('inventory_accounts',$updateParams);
         }
     }
-    public function mergeCarrier(){
-        $carriers = Carrier::query()->get();
-        $logistics = [];
-        $map = [];
-        foreach ($carriers as $carrier){
-            $map[$carrier->name] = $carrier->id;
-            $lo = Logistic::query()->where("name",$carrier->name)->first();
-            if ($lo){
-                if ($lo->type == '快递'){
-//                    $lo->update(["type"=>"全部"]);$lo->save();
-                }
-                continue;
-            };
-            $logistics[] = [
-                "name" => $carrier->name,
-                'mobile' => $carrier->mobile,
-                'delivery_fee'=> $carrier->delivery_fee,
-                'remark'=> $carrier->remark,
-                "type" => "物流"
-            ];
-        }
-        Logistic::query()->insert($logistics);
-        $ls = Logistic::query()->where("type","物流")->get();
-        $result = [];
-        foreach ($ls as $l)
-        {
-            if (isset($map[$l->name]))$result[$map[$l->name]] = $l->id;
-        }
-        LogService::log(__METHOD__,"同步承运商",json_encode($result));
-        foreach ($result as $tag => $val){
-            Waybill::query()->where("logistic_id",$tag)->update([
-                "logistic_id" => $val
-            ]);
-            WaybillPriceModel::query()->where("logistic_id",$tag)->update([
-                "logistic_id" => $val
-            ]);
-            DB::table("logistic_user")->where("logistic_id",$tag)->update([
-                "logistic_id" => $val
-            ]);
-        }
-    }
-
-    private function dd()
+    public static function zzd()
     {
-        StoreCheckingReceiveItem::query()->insert([[
-            "imported_amount" => 1,
-            "produced_at" => null,
-            "invalid_at" => null,
-            "batch_code" => null,
-            "unique_code" => null,
-            "commodity_id" => 36,
-            "store_checking_receive_id" => 2]]);
+        $year = (int)date('Y');
+        $month = (int)date('m');
+        if ($month == 1){
+            $year--;
+            $lastMonth = '12';
+        }else $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
+        $sql = "SELECT owner_id,SUM(work_fee)+SUM(logistic_fee) AS total FROM owner_fee_details WHERE created_at LIKE ? AND (type = '发货' AND logistic_fee IS NOT NULL AND work_fee IS NOT NULL) OR (type <> '发货' AND work_fee IS NOT NULL)  GROUP BY owner_id";
+        $billDetails = DB::select(DB::raw($sql),[$year."-".$lastMonth."%"]);
+dd($billDetails);
     }
 
     public function updateLaborRemark(){

+ 44 - 0
app/Jobs/ProcessCreateInstantBill.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Process;
+use App\Services\LogService;
+use App\Services\ProcessService;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class ProcessCreateInstantBill implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $process;
+    /**
+     * Create a new job instance.
+     *
+     * @param Process $process
+     * @return void
+     */
+    public function __construct(Process $process)
+    {
+        $this->process = $process;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @param ProcessService $service
+     * @return void
+     */
+    public function handle(ProcessService $service)
+    {
+        try{
+            $service->createInstantBill($this->process);
+        }catch (\Exception $e){
+            LogService::log(__METHOD__,"ERROR-入库生成即时账单",$this->process->toJson()." | ".$e->getMessage());
+        }
+    }
+}

+ 45 - 0
app/Jobs/StoreCreateInstantBill.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Services\LogService;
+use App\Services\StoreService;
+use App\Store;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class StoreCreateInstantBill implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+
+    protected $store;
+    /**
+     * Create a new job instance.
+     *
+     * @param Store $store
+     * @return void
+     */
+    public function __construct(Store $store)
+    {
+        $this->store = $store;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @param StoreService $service
+     * @return void
+     */
+    public function handle(StoreService $service)
+    {
+        try{
+            $service->createInstantBill($this->store);
+        }catch (\Exception $e){
+            LogService::log(__METHOD__,"ERROR-入库生成即时账单",$this->store->toJson()." | ".$e->getMessage());
+        }
+    }
+}

+ 7 - 10
app/Services/OrderService.php

@@ -1127,13 +1127,7 @@ class OrderService
     {
         //检查订单对象
         if (!$order || $order->wms_status != "订单完成")return false;
-        if (!$order->packages || !$order->packages[0]->commodities || !$order->packages[0]->commodities[0]->commodity)$order->load(["packages"=>function($query){
-            /** @var Builder $query */
-            $query->with(["commodities"=>function($query){
-                /** @var Builder $query */
-                $query->with("commodity");
-            }]);
-        }]);
+        $order->loadMissing(["logistic","packages.commodities.commodity"]);
 
         /** @var OwnerPriceExpressService $service */
         $service = app("OwnerPriceExpressService");
@@ -1143,6 +1137,9 @@ class OrderService
         $volume = 0;
         $weight = 0;
         $logistic_bill = "";
+
+        if (!$order->logistic || $order->logistic->type != "快递")$logistic_fee = null;
+
         foreach ($order->packages as &$package){
             $logistic_bill .= $package->logistic_number.",";
             $volume += $package->bulk;
@@ -1171,8 +1168,8 @@ class OrderService
         }
         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 : '')];
-        $mapping = ["packages"=>"commodities","商品名称"=>"commodity_name","承运商"=>"logistic_name","店铺类型"=>"shop_name"];
+        $object = ["commodities"=>$commodities,"logistic_name"=>($order->logistic ? $order->logistic->name : ''),"shop_name"=>($order->shop ? $order->shop->name : ''),"order_type"=>$order->order_type];
+        $mapping = ["packages"=>"commodities","商品名称"=>"commodity_name","承运商"=>"logistic_name","店铺类型"=>"shop_name","订单类型"=>"order_type"];
 
         /** @var OwnerPriceOperationService $service */
         $service = app("OwnerPriceOperationService");
@@ -1184,7 +1181,7 @@ class OrderService
             "worked_at"         => $order->wms_edittime,
             "type"              => "发货",
             "shop_id"           => $order->shop_id,
-            "operation_bill"    => $order->client_code,
+            "operation_bill"    => $order->code,
             "consignee_name"    => $order->consignee_name,
             "consignee_phone"   => $order->consignee_phone,
             "commodity_amount"  => $amount,

+ 15 - 0
app/Services/OwnerFeeDetailService.php

@@ -60,4 +60,19 @@ Class OwnerFeeDetailService
         return OwnerFeeDetail::query()->create($params);
     }
 
+    public function first(array $params)
+    {
+        $query = OwnerFeeDetail::query();
+        foreach ($params as $column=>$param){
+            if (is_array($param))$query->whereIn($column,$param);
+            else $query->where($column,$param);
+        }
+        return $query->first();
+    }
+
+    public function updateFind(OwnerFeeDetail $detail, array $values)
+    {
+        return $detail->update($values);
+    }
+
 }

+ 20 - 0
app/Services/ProcessService.php

@@ -94,4 +94,24 @@ Class ProcessService
                 ->selectRaw('sign_commodity_barcode.mark sign_commodity_barcode_mark')
             ->sql();
     }
+
+    public function createInstantBill(Process $process)
+    {
+        if (!$process || $process->status!="交接完成")return false;
+        if (!$process->processStatistic)$process->load("processStatistic");
+
+        if (app("OwnerFeeDetailService")->create([
+            "owner_id"          => $process->owner_id,
+            "worked_at"         => $process->processStatistic ? $process->processStatistic->ended_at : '',
+            "type"              => "增值服务",
+            "operation_bill"    => $process->code,
+            "commodity_amount"  => $process->completed_amount,
+            "process_method_id" => $process->process_method_id,
+            "work_fee"          => $process->processStatistic ? $process->processStatistic->revenue : null,
+            "created_at"        => date('Y-m-d H:i:s'),
+            "outer_id"          => $process->id,
+            "outer_table_name"  => "processes",
+        ]))return true;
+        return false;
+    }
 }

+ 28 - 0
app/Services/StoreService.php

@@ -183,4 +183,32 @@ Class StoreService
         $asn_nos = array_unique(data_get($asnHerders,'*.asnno'));
         return Store::query()->whereIn('asn_code',$asn_nos)->get();
     }
+
+    public function createInstantBill(Store $store):bool
+    {
+        if (!$store || $store->status!="已入库")return false;
+        if (!$store->storeItems)$store->load("storeItems");
+
+
+        /** @var OwnerPriceOperationService $service */
+        $service = app("OwnerPriceOperationService");
+
+        $mapping = ["packages"=>"storeItems","商品名称"=>"name","订单类型"=>"stored_method"];
+
+        $work_fee = $service->matchRule($store,$mapping,$store->owner_id,"入库");
+        if ($work_fee < 0)$work_fee = null;
+
+        if (app("OwnerFeeDetailService")->create([
+            "owner_id"          => $store->owner_id,
+            "worked_at"         => $store->created_at,
+            "type"              => "收货",
+            "operation_bill"    => $store->asn_code,
+            "commodity_amount"  => array_sum(array_column($store->storeItems->toArray(),"amount")),
+            "work_fee"          => $work_fee,
+            "created_at"        => date('Y-m-d H:i:s'),
+            "outer_id"          => $store->id,
+            "outer_table_name"  => "stores",
+        ]))return true;
+        return false;
+    }
 }

+ 43 - 0
app/Services/WaybillService.php

@@ -143,4 +143,47 @@ Class WaybillService
             ->selectRaw('logistics.name carrier_name')
             ->sql();
     }
+
+    public function createInstantBill(Waybill $waybill) :bool
+    {
+        if (!$waybill || $waybill->status != "已完结" || !$waybill->wms_bill_number || !$waybill->logistic_id)return false;
+        $waybill->loadMissing("destinationCity");
+        if (!$waybill->destinationCity)return false;
+
+        $detail = app("OwnerFeeDetailService")->first([
+            "type" => "发货","owner_id" => $waybill->owner_id,"operation_bill"=>$waybill->wms_bill_number
+        ]);
+        if (!$detail || $detail->logistic_fee !== null)return false;
+
+        if ($waybill->type == "专线"){
+            /** @var OwnerPriceLogisticService $service */
+            $service = app("OwnerPriceLogisticService");
+            $fee = $service->matching($waybill->carrier_weight_other,$waybill->owner_id,$waybill->logistic_id,
+                $waybill->carrier_weight_unit_id_other,$waybill->destinationCity->province_id,
+                $waybill->destination_city_id);
+        }else{
+            /** @var OwnerPriceDirectLogisticService $service */
+            $service = app("OwnerPriceDirectLogisticService");
+            $fee = $service->matching($waybill->mileage,$waybill->owner_id,$waybill->carType_id);
+        }
+
+        if ($fee >= 0){
+            app("OwnerFeeDetailService")->updateFind($detail,[
+                "owner_id" => $waybill->owner_id,
+                "worked_at"=> $waybill->updated_at,
+                "type" => "发货",
+                "operation_bill" => $waybill->waybill_number,
+                "consignee_name" => $waybill->recipient,
+                "consignee_phone" => $waybill->recipient_mobile,
+                "commodity_amount" => $waybill->amount,
+                "logistic_bill" => $waybill->carrier_bill,
+                "volume" =>$waybill->carrier_weight ?? $waybill->warehouse_weight,
+                "weight" => $waybill->carrier_weight_other ?? $waybill->warehouse_weight_other,
+                "logistic_id" => $waybill->logistic_id,
+                "logistic_fee" => $fee,
+                "outer_id" => $waybill->id,
+                "outer_table_name" => "waybills",
+            ]);
+        }
+    }
 }

+ 1 - 1
package-lock.json

@@ -6254,7 +6254,7 @@
         },
         "minimist": {
             "version": "1.2.0",
-            "resolved": "https://registry.npm.taobao.org/minimist/download/minimist-1.2.0.tgz",
+            "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
             "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=",
             "dev": true
         },