1557492053 %!s(int64=3) %!d(string=hai) anos
pai
achega
ae99fb22ca

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

@@ -118,8 +118,25 @@ class Order extends \app\BaseController
         return $this->ok($res->data);
     }
 
+    public function fetchWaitGatherAnnualFee() {
+        $params = $this->request->param();
+        return $this->ok($this->service->fetchWaitGatherAnnualFee(
+            $params['admin_id'],
+            format_string($params['text'] ?? null),
+            isset($params['start_time']) && $params['start_time'] > 0 ? $params['start_time'] : null,
+            isset($params['end_time']) && $params['end_time'] > 0 ? $params['end_time'] : null,
+            $params['page'] ?? 1,
+            $params['size'] ?? 20
+        ));
+    }
 
-
+    public function paymentAnnualFee() {
+        $params = $this->request->param();
+        predicate(isset($params['annual_fee_id']) && $params['annual_fee_id'] > 0, '待收取年费的ID错误!');
+        $r = $this->service->paymentAnnualFee($params['annual_fee_id']);
+        predicate($r->bool,$r->message);
+        return $this->ok($r->data);
+    }
 
 
 

+ 2 - 0
app/api/route/app.php

@@ -29,6 +29,8 @@ Route::group('api/order', function () {
     Route::rule('fetch', 'api/order/fetch');
     Route::rule('fetchByCustomerId', 'api/order/fetchByCustomerId');
     Route::rule('upload_numerology', 'api/order/upload_numerology');
+    Route::rule('fetchWaitGatherAnnualFee', 'api/order/fetchWaitGatherAnnualFee');
+    Route::rule('paymentAnnualFee', 'api/order/paymentAnnualFee');
 })->middleware(\app\common\middleware\VerifySessionToken::class);
 
 Route::group('api/payment', function () {

+ 17 - 14
app/api/service/AdminService.php

@@ -70,27 +70,30 @@ class AdminService extends \app\BaseService
         $admin = $this->adminModel->loadByLogin($username, $password);
         if(!$admin) return $this->fail(lang("Account password error"));
         $counts = $this->orderProductModel->countByAdviser($admin->id, $start_time, $end_time);
-        $res = [
-            'total_sales' => fixed2Float($counts[0]),
-            'numerology' => "{$counts[1]}/{$counts[2]}",
-            'crape_myrtle'  =>  rand(1000,100000),
-            'geomancy_serve' =>  rand(1000,100000),
-        ];
         $items = [
+            ['key' => '销售总额', 'value' => fixed2Float($counts[0])],
+            ['key' => '上传命理报告数', 'value' =>  "{$counts[1]}/{$counts[2]}"],
+            ['key' => '紫薇斗数', 'value' => 0],
+            ['key' => '风水服务', 'value' => 0],
             ['key' => '商品销售总额', 'value' => fixed2Float($counts[3])],
             ['key' => '大商品销售总额', 'value' => fixed2Float($counts[4])],
-            ['key' => '服务销售总额', 'value' => fixed2Float($counts[5])],
+            ['key' => '服务销售总额', 'value' => fixed2Float($counts[5])]
         ];
         $categorys = $this->productCategoryModel->findAll();
         foreach ($categorys as $category) {
-            array_push($items, [
-                'key' => $category->name,
-                'value' => rand(1,999),
-            ]);
+            $value = $this->orderProductModel->countByAdviserProductCategoryIds($admin->id, [$category->id], $start_time, $end_time);
+            if($category->name == '紫薇斗数') {
+                $res[3]['value'] = $value;
+            } else if ($category->name == '风水服务') {
+                $res[4]['value'] = $value;
+            } else {
+                array_push($items, [
+                    'key' => $category->name,
+                    'value' => $value
+                ]);
+            }
         }
-        $res['items'] = $items;
-
-        return $this->ok($res);
+        return $this->ok($items);
     }
 
 

+ 4 - 0
app/api/service/AuthService.php

@@ -17,6 +17,10 @@ class AuthService extends \app\BaseService
 //    }
 
     public static function is_look_all($admin_id, $data_rule_id) {
+
+        // 定金订单查看全部
+        if (in_array($data_rule_id, [10004]))
+            return true;
         $admin = (new AdminModel())->findById($admin_id);
         if(!$admin || !$admin->access || !$admin->access->group) return false;
         return in_array($data_rule_id,$admin->access->group->data_rules ? explode(',', $admin->access->group->data_rules) : []);

+ 47 - 17
app/api/service/OrderService.php

@@ -131,10 +131,13 @@ class OrderService extends \app\BaseService
         $total_sales_tax = 0;
         $advisor_1_ids = [];
         $product_names = [];
+        $total_annual_fee = 0;
         $order_no = $this->orderModel->genOrderNo($store->id, $store->abbr);
         for ($i = 0; $i < count($store_products_params); $i++) {
             $store_products_param = $store_products_params[$i];
             $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);
             for ($j = 0; $j < $store_products_param->quantity; $j++) {
                 array_push($product_names, $store_product['product']['name']);
@@ -169,13 +172,18 @@ class OrderService extends \app\BaseService
                 ]);
                 $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']);
+                if($store_product['product']['is_gather_annuity'] == 1) {
+                    $total_annual_fee += $store_product['product']['annuity'];
+                    $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']);
+                } else {
+                    $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax']);
+                }
             }
         }
         $order = $this->orderModel->create([
             'no' => $order_no,
             'customer_id' => $customer->id,
-            'customer_name' => $customer->name_en,
+            'customer_name' => "{$customer->name_en}|{$customer->name_zh}",
             'obj_names' =>  join(',',array_unique($product_names)),
             'rental_amount' => $rental_amount,
             'receivable_amount' =>  $rental_amount,
@@ -184,6 +192,7 @@ class OrderService extends \app\BaseService
             'product_amount'    =>  $product_amount,
             'total_sales_tax'   =>  $total_sales_tax,
             'service_charge_amount' =>  0,
+            'total_annual_fee' => $total_annual_fee,
             'type'  =>  2,
             'store_id'  =>  $store->id,
             'advisor_ids' => join(',', $advisor_1_ids),
@@ -218,7 +227,10 @@ class OrderService extends \app\BaseService
             return $this->fail('订单不存在!');
         if($order->type == 1)
             return $this->fail('订单状态为历史订单!不支持付款');
-
+        // 客户
+        $customer = $this->customerModel->findById($order->customer_id);
+        if (!$customer)
+            return $this->fail(lang("The Customer does not exist"));
         $pay_channels = $this->paymentChannelModel->where('is_delete', 0)->select()->toArray();
         $fmt_pay_channels = array_reduce($pay_channels, function ($result, $item) {
             $result[$item['id']] = $item;
@@ -310,12 +322,13 @@ class OrderService extends \app\BaseService
             }
         }
 
-        // 总共需要收取这么多费用; (商品的价格 + 手续费 + 税费) - 御龙币抵扣的费用
-        $imposed_amount = fixed2Float($order->product_amount + ($order->service_charge_amount + $service_charge) + $order->total_sales_tax);
+        // 信用卡手续费
+        $total_service_charge_amount = ($order->service_charge_amount + $service_charge);
+        // 总共需要收取这么多费用; (商品的价格 + 商品税费 + 年费 + 信用卡手续费) - 御龙币抵扣的费用
+        $imposed_amount = fixed2Float($order->product_amount + $order->total_sales_tax + $order->total_annual_fee + $total_service_charge_amount);
         // 总计实收金额
         $receive_amount = fixed2Float($order->receive_amount + $total_fee);
-        if (round($receive_amount) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 应该支付 {$imposed_amount}");
-
+        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 = [];
         if (round($receive_amount) == round($imposed_amount)) { // 减库存
@@ -336,14 +349,22 @@ class OrderService extends \app\BaseService
                 if ($order_product['is_gather_annuity']) {
                     array_push($order_annual_fees, [
                         'order_id' => $order->id,
-                        'order_product_id'  => $order_product['product_id'],
+                        'customer_id'   => $customer->id,
+                        'customer_name'   => "{$customer->name_en}|{$customer->name_zh}",
+                        'customer_mobile'  => $customer->mobile,
+                        'customer_create_username' => $customer->create_username,
+                        'index' => 1,
+                        'order_product_id'  => $order_product['id'],
                         'order_product_name' => $order_product['product_name'],
                         'fee' => $order_product['annuity'],
-                        'is_pay' => 0,
+                        'is_pay' => 1,
                         'start_time' => time(),
                         'end_time' => time() + (365 * 24 * 60 * 60),
+                        'order_create_time' => $order->create_time,
+                        'adviser_1_id'  =>  $order_product['adviser_1_id'],
+                        'store_id' => $order->store_id,
                         'create_time'   =>  time(),
-                        'update_time'   =>  time(),
+                        'update_time'   =>  time()
                     ]);
                 }
             }
@@ -406,7 +427,6 @@ class OrderService extends \app\BaseService
         return $this->ok(true);
     }
 
-
     public function fetch($params) {
         $is_look_all = AuthService::is_look_all($params['admin_id'],$params['type'] == 1 ? 10003 : 10004);
         $res = $this->orderModel->search($params, $is_look_all);
@@ -435,8 +455,6 @@ class OrderService extends \app\BaseService
         return [$items, $res['total']];
     }
 
-
-
     public function upload_numerology($order_product_id, $report) {
         $order_product = $this->orderProductModel->findById($order_product_id);
         if (!$order_product) return $this->fail('订单商品不存在,无法上传命理报告!');
@@ -447,11 +465,23 @@ class OrderService extends \app\BaseService
         return $this->ok($order_product);
     }
 
+    public function fetchWaitGatherAnnualFee($admin_id, $text = null, $start_time = null, $end_time = null, $page = 1, $size = 10) {
+        $res = $this->orderAnnualFeeModel->fetchByAdviser($admin_id, $text, $start_time, $end_time, $page, $size);
+        return [$res->items(),$res->total()];
+    }
 
-
-
-
-
+    public function paymentAnnualFee($annual_fee_id) {
+        $annual_fee = $this->orderAnnualFeeModel->findById($annual_fee_id);
+        if(!$annual_fee_id) {
+            return $this->fail('年费记录不存在!');
+        }
+        if($annual_fee->is_pay == 1)
+            return $this->fail('该条年费记录已经支付!');
+        $annual_fee->is_pay = 1;
+        $annual_fee->update_time = time();
+        $annual_fee->save();
+        return $this->ok($annual_fee);
+    }
 
 }
 

+ 15 - 0
app/common/model/OrderAnnualFeeModel.php

@@ -21,4 +21,19 @@ class OrderAnnualFeeModel extends BaseModel
             ->select()->toArray();
     }
 
+    public function fetchByAdviser($admin_id, $text = null, $start_time = null, $end_time = null, $page = 1, $size = 10) {
+        $where = [
+            ['is_delete', '=', 0],
+            ['is_pay', '=', 0],
+            ['adviser_1_id', '=', $admin_id],
+        ];
+        if ($text) array_push($where, ['customer_name|customer_mobile', 'like', "%".$text."%"]);
+        if ($start_time) array_push($where, ['order_create_time', '>=', $start_time]);
+        if ($end_time) array_push($where, ['order_create_time', '<=', $end_time]);
+        return $this->where($where)
+            ->page($page, $size)
+            ->paginate();
+
+    }
+
 }

+ 15 - 24
app/common/model/OrderProductModel.php

@@ -46,10 +46,11 @@ class OrderProductModel extends BaseModel
             ->count();
     }
 
-    public function countByAdviser($adviser_1_id,$start_time = null, $end_time = null) {
+    public function countByAdviser($adviser_1_id, $start_time = null, $end_time = null) {
         $where = [
             ['is_delete', '=', 0],
             ['adviser_1_id', '=', $adviser_1_id],
+            ['is_pay' , '=', 1]
         ];
         if($start_time) array_push($where, ['create_time', '>=', $start_time]);
         if($end_time) array_push($where, ['create_time', '<=', $end_time]);
@@ -59,34 +60,24 @@ class OrderProductModel extends BaseModel
                 ['is_upload_numerology', '=', 1],
                 ['is_upload', '=', 1]
             ]))->count(),
-            $this->where([
-                ['is_delete', '=', 0],
-                ['adviser_1_id', '=', $adviser_1_id],
-                ['is_upload_numerology', '=', 1],
-            ])->count(),
-            $this->where([
-                ['is_delete', '=', 0],
-                ['adviser_1_id', '=', $adviser_1_id],
-                ['is_serve', '=', 0],
-            ])->sum('real_price'),
-            $this->where([
-                ['is_delete', '=', 0],
-                ['adviser_1_id', '=', $adviser_1_id],
-                ['teacher_id', '>', 0],
-            ])->sum('real_price'),
-            $this->where([
-                ['is_delete', '=', 0],
-                ['adviser_1_id', '=', $adviser_1_id],
-                ['is_serve', '=', 1],
-            ])->sum('real_price'),
+            $this->where(array_merge($where,[['is_upload_numerology', '=', 1]]))->count(),
+            $this->where(array_merge($where, [['is_serve', '=', 0]]))->sum('real_price'),
+            $this->where(array_merge($where, [['teacher_id', '>', 0]]))->sum('real_price'),
+            $this->where(array_merge($where, [['is_serve', '=', 1]]))->sum('real_price'),
         ];
     }
 
-    public function groupByAdviser($adviser_1_id, $start_time = null, $end_time = null) {
-        return $this->where([
+    public function countByAdviserProductCategoryIds($adviser_1_id, $category_ids = [], $start_time = null, $end_time = null) {
+        if (count($category_ids) == 0) return 0;
+        $where = [
             ['is_delete', '=', 0],
             ['adviser_1_id', '=', $adviser_1_id],
-        ])->sum('real_price');
+            ['is_pay' , '=', 1],
+            ['product_category_id', 'in', $category_ids]
+        ];
+        if($start_time) array_push($where, ['create_time', '>=', $start_time]);
+        if($end_time) array_push($where, ['create_time', '<=', $end_time]);
+        return $this->where($where)->sum('real_price');
     }