1557492053 лет назад: 3
Родитель
Сommit
595a3bd91d

+ 1 - 1
.env

@@ -18,7 +18,7 @@ prefix = erp_
 default_lang = zh-cn
 
 [cache]
-driver = file
+driver = redis
 
 [REDIS]
 host = 118.195.195.200

+ 1 - 0
app/api/lang/zh-cn.php

@@ -44,6 +44,7 @@ return [
     "The store does not exist"  =>  "门店不存在",
     "The Adviser|Teacher does not exist"    =>  "顾问或者老师不存在!",
     "Stores do not exist"           =>  "门店不存在",
+    "inventory shortage"            =>  "库存不足",
 ];
 
 

+ 90 - 25
app/api/service/OrderService.php

@@ -18,6 +18,7 @@ use app\common\model\PaymentChannelModel;
 use app\common\model\ProductModel;
 use app\common\model\StoreModel;
 use app\common\model\StoreProductModel;
+use think\cache\driver\Redis;
 use think\facade\Db;
 
 class OrderService extends \app\BaseService
@@ -126,6 +127,7 @@ class OrderService extends \app\BaseService
         $order_products = [];
         //项目总额
         $rental_amount = 0;
+        $product_amount = 0;
         // 总消费税
         $total_sales_tax = 0;
         $advisor_1_ids = [];
@@ -137,6 +139,7 @@ class OrderService extends \app\BaseService
                 array_push($order_products, [
                     'order_id' => 0,
                     'product_id' => $store_product['product']['id'],
+                    'store_product_id' => $store_product['id'],
                     'product_name' => $store_product['product']['name'],
                     'adviser_1_id' => isset($store_products_param->adviser_1_id) ?? null,
                     'adviser_1_name'  => isset($store_products_param->adviser_1_id) ? $fmt_users[$store_products_param->adviser_1_id]['nickname'] : null,
@@ -144,7 +147,11 @@ class OrderService extends \app\BaseService
                     'adviser_2_name'  => isset($store_products_param->adviser_2_id) ? $fmt_users[$store_products_param->adviser_2_id]['nickname'] : null,
                     'teacher_id' => isset($store_products_param->teacher_id) ?? null,
                     'teacher_name'  => isset($store_products_param->teacher_id) ? $fmt_users[$store_products_param->teacher_id]['nickname'] : null,
+                    'is_upload_numerology' => $store_product['product']['is_upload_numerology'],
+                    'is_upload' => 0,
                     'report'    => null,
+                    'is_gather_annuity' => $store_product['product']['is_gather_annuity'],
+                    'annuity' => $store_product['product']['annuity'],
                     'real_price'    =>  $store_product['product']['real_price'],
                     'service_charge'    => 0,
                     'reduce_price'    => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
@@ -154,8 +161,9 @@ class OrderService extends \app\BaseService
                     'create_time' => time(),
                     'update_time' => time(),
                 ]);
-                $rental_amount += $store_product['product']['real_price'];
+                $product_amount += $store_product['product']['real_price'];
                 $total_sales_tax += $store_product['product']['sales_tax'];
+                $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax']);
             }
         }
         $order = $this->orderModel->create([
@@ -166,7 +174,9 @@ class OrderService extends \app\BaseService
             'receivable_amount' =>  $rental_amount,
             'receive_amount'    =>  0,
             'imposed_amount'    =>  $rental_amount,
+            'product_amount'    =>  $product_amount,
             'total_sales_tax'   =>  $total_sales_tax,
+            'service_charge_amount' =>  0,
             'type'  =>  2,
             'store_id'  =>  $store->id,
             'advisor_ids' => join(',', $advisor_1_ids),
@@ -186,7 +196,6 @@ class OrderService extends \app\BaseService
         return $this->ok($order);
     }
 
-
     /**
      * @param $params
      * @param $channels
@@ -214,12 +223,11 @@ class OrderService extends \app\BaseService
             return $result;
         },[]);
 
-
-        // 总计支付费用
+        // 本次总计支付费用
         $total_fee = 0;
-        // 信用卡支付手续费
+        // 本次信用卡支付手续费
         $service_charge = 0;
-        // 御龙币
+        // 本次御龙币
         $zue_coin = 0;
         $order_payments = [];
         $record_channel_names = [];
@@ -295,14 +303,67 @@ class OrderService extends \app\BaseService
             }
         }
 
-        // 总共需要收取这么多费用;
-        $imposed_amount = fixed2Float($order->imposed_amount + $service_charge + $order->total_sales_tax);
-        $total_fee = fixed2Float($total_fee);
-        if (round($total_fee) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 应该支付 {$imposed_amount}");
-        return $this->ok($total_fee);
+        // 总共需要收取这么多费用; (商品的价格 + 手续费 + 税费) - 御龙币抵扣的费用
+        $imposed_amount = fixed2Float($order->product_amount + ($order->service_charge_amount + $service_charge) + $order->total_sales_tax);
+        // 总计实收金额
+        $receive_amount = fixed2Float($order->receive_amount + $total_fee);
+        if (round($receive_amount) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 应该支付 {$imposed_amount}");
+
+        $order_annual_fees = [];
+        $update_store_products = [];
+        if (round($receive_amount) == round($imposed_amount)) { // 减库存
+            $order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
+            $store_products = $this->storeProductModel->findByIds(array_map(function ($r) {return $r['store_product_id'];}, $order_products))->toArray();
+            $fmt_store_products = array_reduce($store_products, function ($result, $item) {
+                $result[$item['id']] = $item;
+                return $result;
+            },[]);
+            foreach ($order_products as $order_product) {
+                predicate($fmt_store_products[$order_product['store_product_id']]['now_stock'] > 0, lang('inventory shortage'));
+                $fmt_store_products[$order_product['store_product_id']]['now_stock'] -= 1;
+                if (isset($update_store_products[$order_product['store_product_id']])) {
+                    $update_store_products[$order_product['store_product_id']]['reduce_num'] += 1;
+                } else {
+                    $update_store_products[$order_product['store_product_id']] = ['id' => $order_product['store_product_id'], 'reduce_num' => 1];
+                }
+                if ($order_product['is_gather_annuity']) {
+                    array_push($order_annual_fees, [
+                        'order_id' => $order->id,
+                        'order_product_id'  => $order_product['product_id'],
+                        'order_product_name' => $order_product['product_name'],
+                        'fee' => $order_product['annuity'],
+                        'is_pay' => 0,
+                        'start_time' => time(),
+                        'end_time' => time() + (365 * 24 * 60 * 60),
+                        'create_time'   =>  time(),
+                        'update_time'   =>  time(),
+                    ]);
+                }
+            }
+        }
+
         Db::startTrans();
         try {
-            Db::table('erp_order_payment')->insertAll($order_payments);
+            $update_order = [
+                'rental_amount' => $imposed_amount,
+                'receivable_amount' => $imposed_amount,
+                'receive_amount'    =>  $receive_amount,
+                'imposed_amount'    =>  $imposed_amount - $receive_amount,
+                'service_charge_amount' =>  $order->service_charge_amount + $service_charge,
+            ];
+            if (round($receive_amount) == round($imposed_amount)) { // 支付满了
+                $update_order['type'] = 2;
+                Db::table('erp_order_product')->where('order_id', $order->id)->update(['is_pay' => 1]);
+                if(count($order_annual_fees) > 0) {
+                    Db::table('erp_order_annual_fee')->insertAll($order_annual_fees);
+                }
+                if(count($update_store_products) > 0) {
+                    foreach ($update_store_products as $update) {
+                        Db::table('erp_store_product')->where('id', $update['id'])->dec('now_stock',1);
+                        Db::table('erp_store_product')->where('id', $update['id'])->inc('sale_stock',1);
+                    }
+                }
+            }
             Db::table('erp_order_proceeds')->save([
                 'order_id' => $order->id,
                 'channels' => join(' ',$record_channel_names),
@@ -312,17 +373,24 @@ class OrderService extends \app\BaseService
                 'create_time' => time(),
                 'update_time' => time(),
             ]);
-            $update_order = [
-                'service_charge_amount' => $service_charge,
-                'receive_amount' => $order->receive_amount + $total_fee,
-                'imposed_amount' => $order->imposed_amount - $total_fee,
-            ];
-            if (round($total_fee) < round($imposed_amount)) { // 未支付满
-//                DOTO:
-            } else {
-                $update_order['type'] = 2;
+            if(count($order_payments) > 0) {
+                Db::table('erp_order_payment')->insertAll(array_map(function ($data) {
+                    return  [
+                        "order_id" => isset($data['order_id']) ? $data['order_id'] : null,
+                        "channel_id" => isset($data['channel_id']) ? $data['channel_id'] : null,
+                        "fee" => isset($data['fee']) ? $data['fee'] : null,
+                        "credit_card_id" => isset($data['credit_card_id']) ? $data['credit_card_id'] : null,
+                        "credit_card_name" => isset($data['credit_card_name']) ? $data['credit_card_name'] : null,
+                        "create_time" => isset($data['create_time']) ? $data['create_time'] : null,
+                        "update_time" => isset($data['update_time']) ? $data['update_time'] : null,
+                        "code" => isset($data['code']) ? $data['code'] : null,
+                        "stage_num" => isset($data['stage_num']) ? $data['stage_num'] : null,
+                        "service_charge_rate" => isset($data['service_charge_rate']) ? $data['service_charge_rate'] : null,
+                        "service_charge" => isset($data['service_charge']) ? $data['service_charge'] : null
+                    ];
+                },$order_payments));
             }
-            Db::table('erp_order')->where('id',$order->id)->update($update_order);
+            Db::table('erp_order')->where('id', $order->id)->update($update_order);
             Db::commit();
         } catch (\Exception $e) {
             Db::rollback();
@@ -331,9 +399,6 @@ class OrderService extends \app\BaseService
         return $this->ok(true);
     }
 
-
-
-
 }
 
 

+ 10 - 0
app/common/model/OrderProductModel.php

@@ -12,4 +12,14 @@ class OrderProductModel extends BaseModel
     {
         // TODO: Implement genSchema() method.
     }
+
+    public function findByOrderId($o_id) {
+        return $this->where('is_delete', 0)
+            ->where('order_id',$o_id)
+            ->select();
+    }
+
+
+
+
 }

+ 9 - 0
app/common/model/StoreProductModel.php

@@ -92,6 +92,15 @@ class StoreProductModel extends BaseModel
             ->select();
     }
 
+    public function fetchByStoreIdAndProductIds($s_id, $p_ids) {
+        $where = [
+            ['product_id', 'in', $p_ids],
+            ['store_id', '=', $s_id],
+            ['is_delete', '=', 0]
+        ];
+        return $this->where($where)
+            ->select();
+    }
 
 
 }