1557492053 3 yıl önce
ebeveyn
işleme
1bcc58c575

+ 1 - 1
app/api/controller/Order.php

@@ -35,7 +35,7 @@ class Order extends \app\BaseController
 
         for ($i = 0; $i < count($products); $i++) {
             $product = $products[$i];
-            predicate(isset($product->store_product_id) && $product->store_product_id > 0, '商品ID错误!');
+            predicate(isset($product->store_product_id) && $product->store_product_id > 0, '门店商品ID错误!');
             predicate($product->quantity > 0, '商品数量错误!');
             predicate($product->adviser_1_id > 0, '销售顾问1必须存在!');
             if(isset($product->adviser_2_id)) predicate($product->adviser_2_id > 0, '销售顾问2不符合规则!');

+ 57 - 18
app/api/service/OrderService.php

@@ -84,7 +84,8 @@ class OrderService extends \app\BaseService
             $ids = [];
             if(isset($store_products_param->adviser_1_id)) array_push($ids, $store_products_param->adviser_1_id);
             if(isset($store_products_param->adviser_2_id)) array_push($ids, $store_products_param->adviser_2_id);
-            if(isset($store_products_param->teacher_id)) array_push($ids, $store_products_param->teacher_id);
+            if(isset($store_products_param->teacher_1_id)) array_push($ids, $store_products_param->teacher_1_id);
+            if(isset($store_products_param->teacher_2_id)) array_push($ids, $store_products_param->teacher_2_id);
             $user_ids = array_unique(array_merge($user_ids, $ids));
             array_push($store_product_ids, $store_products_param->store_product_id);
         }
@@ -132,7 +133,7 @@ class OrderService extends \app\BaseService
         $product_amount = 0;
         // 总消费税
         $total_sales_tax = 0;
-        $advisor_1_ids = [];
+        $advisor_ids = [];
         $product_names = [];
         $total_annual_fee = 0;
         $order_no = $this->orderModel->genOrderNo($store->id, $store->abbr);
@@ -141,7 +142,11 @@ class OrderService extends \app\BaseService
             $store_product = $fmt_store_products[$store_products_param->store_product_id];
             if($store_product['now_stock'] < $store_products_param->quantity)
                 return $this->fail(lang("inventory shortage"));
-            array_push($advisor_1_ids, $store_products_param->adviser_1_id);
+            array_push($advisor_ids, $store_products_param->adviser_1_id);
+            if(isset($store_products_param->adviser_2_id)) {
+                array_push($advisor_ids, $store_products_param->adviser_2_id);
+            }
+
             for ($j = 0; $j < $store_products_param->quantity; $j++) {
                 array_push($product_names, $store_product['product']['name']);
                 array_push($order_products, [
@@ -157,8 +162,10 @@ class OrderService extends \app\BaseService
                     'adviser_1_name'  => isset($store_products_param->adviser_1_id) ? $fmt_users[$store_products_param->adviser_1_id]['nickname'] : null,
                     'adviser_2_id' => isset($store_products_param->adviser_2_id) ? $store_products_param->adviser_2_id : null,
                     '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) ? $store_products_param->teacher_id : null,
-                    'teacher_name'  => isset($store_products_param->teacher_id) ? $fmt_users[$store_products_param->teacher_id]['nickname'] : null,
+                    'teacher_1_id' => isset($store_products_param->teacher_1_id) ? $store_products_param->teacher_1_id : null,
+                    'teacher_1_name'  => isset($store_products_param->teacher_1_id) ? $fmt_users[$store_products_param->teacher_1_id]['nickname'] : null,
+                    'teacher_2_id' => isset($store_products_param->teacher_2_id) ? $store_products_param->teacher_2_id : null,
+                    'teacher_2_name'  => isset($store_products_param->teacher_2_id) ? $fmt_users[$store_products_param->teacher_2_id]['nickname'] : null,
                     'is_upload_numerology' => $store_product['product']['is_upload_numerology'],
                     'is_upload' => 0,
                     'report'    => null,
@@ -170,6 +177,8 @@ class OrderService extends \app\BaseService
                     'reduce_type'    => $store_product['activity'] ? $store_product['activity']['type'] : 0,
                     'sales_tax_rate' => $store_product['product']['sales_tax_rate'],
                     'sales_tax' => $store_product['product']['sales_tax'],
+                    'transaction_price' =>  0,
+                    'zue_coin'  =>  0,
                     'create_time' => time(),
                     'update_time' => time(),
                 ]);
@@ -198,7 +207,7 @@ class OrderService extends \app\BaseService
             'total_annual_fee' => $total_annual_fee,
             'type'  =>  2,
             'store_id'  =>  $store->id,
-            'advisor_ids' => join(',', $advisor_1_ids),
+            'advisor_ids' => join(',', $advisor_ids),
             'create_time' => time(),
             'update_time' => time()
         ]);
@@ -335,8 +344,11 @@ class OrderService extends \app\BaseService
         $order_use_zue_coin = $order->zue_coin;
         $order_use_zue_coin_amount = $order->zue_coin_amount;
 
+        $change_zue_coin_record = null;
         if($zue_coin > 0) {
             $zue_coin_model = $this->zueCoinModel->findByCustomerId($customer->id);
+            if(!$zue_coin_model)
+                return $this->fail("当前客户御龙币账户余额: 0");
             if ($zue_coin > $zue_coin_model->zue_coin)
                 return $this->fail("当前客户御龙币账户余额:{$zue_coin_model->zue_coin}");
             $zue_coin_config = $fmt_pay_channels['11'];
@@ -361,16 +373,20 @@ class OrderService extends \app\BaseService
             $now_can_consume_zue_coin = $total_can_zue_coin - $order_use_zue_coin;
             // 判断当前还能使用
             if ($now_can_consume_zue_coin - $zue_coin < 0)
-                return $this->fail("
-                    当前已使用御龙币数额: {$order_use_zue_coin}/{$total_can_zue_coin},
-                    当前已兑换金额: {$order_use_zue_coin_amount}/{$total_consume_amount},
-                    御龙币兑换比例: {$zue_coin_exchange_rate},
-                    御龙币报销比例:{$zue_coin_consume_rate}
-                ");
+                return $this->fail("当前已使用御龙币数额: {$order_use_zue_coin}/{$total_can_zue_coin},当前已兑换金额: {$order_use_zue_coin_amount}/{$total_consume_amount},御龙币兑换比例: {$zue_coin_exchange_rate},御龙币报销比例:{$zue_coin_consume_rate}");
             // 总共使用了这么多御龙币
             $order_use_zue_coin += $zue_coin;
             // 总共兑换了这么多钱
             $order_use_zue_coin_amount = fixed2Float($order_use_zue_coin * $zue_coin_exchange_rate);
+            $change_zue_coin_record = [
+                'zue_coin_id'   =>  $zue_coin_model->id,
+                'origin'        =>  $zue_coin_model->zue_coin,
+                'change'        =>  $zue_coin,
+                'after'        =>  $zue_coin_model->zue_coin - $zue_coin,
+                'reason'        =>  1,
+                'create_time'   =>  time(),
+                'update_time'   =>  time(),
+            ];
         }
         // 总计费用 - 御龙币抵消费用;
         $imposed_amount -= $order_use_zue_coin_amount;
@@ -380,6 +396,7 @@ class OrderService extends \app\BaseService
         if (round($receive_amount) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 应该支付 {$imposed_amount} (商品总价:{$order->product_amount} + 商品总消费税:{$order->total_sales_tax} + 第一年年费:{$order->total_annual_fee} + 信用卡总手续费:{$total_service_charge_amount})");
         $order_annual_fees = [];
         $update_store_products = [];
+        $update_order_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();
@@ -387,6 +404,13 @@ class OrderService extends \app\BaseService
                 $result[$item['id']] = $item;
                 return $result;
             },[]);
+            // 商品本身的价格 | 年费 | 消费税 |  信用卡支付的服务费 | 御龙币使用的御龙币退还
+            $total_ = $imposed_amount - ($order->service_charge_amount + $service_charge);
+
+            // 每一块钱所占的比例
+            $item_service_charge = ($order->service_charge_amount + $service_charge) / $total_;
+            // 每一个御龙币所占的比例
+            $item_zue_coin = $order_use_zue_coin / $total_;
             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;
@@ -409,17 +433,23 @@ class OrderService extends \app\BaseService
                         'is_pay' => 1,
                         'start_time' => time(),
                         'end_time' => time() + (365 * 24 * 60 * 60),
-                        'order_create_time' => $order->create_time,
+                        'order_create_time' => strtotime($order->create_time),
                         'adviser_1_id'  =>  $order_product['adviser_1_id'],
                         'store_id' => $order->store_id,
                         'create_time'   =>  time(),
                         'update_time'   =>  time()
                     ]);
                 }
+                $now = $order_product['real_price'] + $order_product['annuity'] + $order_product['sales_tax'];
+                array_push($update_order_products, [
+                    'id'    =>  $order_product['id'],
+                    'service_charge'    =>  $now * $item_service_charge,
+                    'zue_coin'          =>  $now * $item_zue_coin,
+                    'transaction_price' =>  $now + ($now * $item_service_charge),
+                    'is_pay' => 1
+                ]);
             }
         }
-        return $this->ok(true);
-
         Db::startTrans();
         try {
             $update_order = [
@@ -432,16 +462,21 @@ class OrderService extends \app\BaseService
                 'zue_coin_amount' => $order_use_zue_coin_amount,
                 'remarks'   =>  $params['remarks']
             ];
+
             if (round($receive_amount) == round($imposed_amount)) { // 支付满了
                 $update_order['type'] = 1;
-                Db::table('erp_order_product')->where('order_id', $order->id)->update(['is_pay' => 1]);
+                if(count($update_order_products) > 0) {
+                    foreach ($update_order_products as $item) {
+                        Db::table('erp_order_product')->where('order_id', $item['id'])->update($item);
+                    }
+                }
                 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_store_product')->where('id', $update['id'])->dec('now_stock',1)->update();
+                        Db::table('erp_store_product')->where('id', $update['id'])->inc('sale_stock',1)->update();
                     }
                 }
             }
@@ -454,6 +489,10 @@ class OrderService extends \app\BaseService
                 'create_time' => time(),
                 'update_time' => time(),
             ]);
+            if ($zue_coin > 0 && $change_zue_coin_record != null) {
+                Db::table('erp_zue_coin_record')->save($change_zue_coin_record);
+                Db::table('erp_customer_zue_coin')->where('id', $change_zue_coin_record['zue_coin_id'])->dec('zue_coin', $zue_coin)->update();
+            }
             if(count($order_payments) > 0) {
                 Db::table('erp_order_payment')->insertAll(array_map(function ($data) {
                     return  [

+ 0 - 6
app/api/service/ProductService.php

@@ -54,10 +54,4 @@ class ProductService extends BaseService
         }
         return [$items, $storeProducts->total()];
     }
-
-    // 订单总额 = 商品价格 + 商品年费 + 商品消费税
-    // 应收金额 = 商品的活动价 + 商品年费 + 商品消费税
-    // 优惠金额 = 订单总额 - 应收金额 + 商品消费税
-    // 消费税 = 商品的消费税
-
 }

+ 1 - 1
app/common/model/CustomerZueCoinModel.php

@@ -52,7 +52,7 @@ class CustomerZueCoinModel extends BaseModel
     public function findByCustomerId($c_id) {
         return $this->where([
             ['is_delete', '=', 0],
-            ['lose_time', '<=', time()],
+            ['lose_time', '>=', time()],
         ])->order('lose_time', 'Asc')->find();
     }