1557492053 2 years ago
parent
commit
151a517b71

File diff suppressed because it is too large
+ 1329 - 0
app/api/service/OrderService-backups.php


+ 314 - 276
app/api/service/OrderService.php

@@ -4,6 +4,7 @@
 namespace app\api\service;
 
 
+use app\api\controller\Product;
 use app\common\model\ActivityProductModel;
 use app\common\model\AdminModel;
 use app\common\model\ConfigModel;
@@ -62,6 +63,34 @@ class OrderService extends \app\BaseService
         $this->zueCoinRecordModel = new ZueCoinRecordModel();
     }
 
+    // 计算单个商品消费税
+    private function calculate_sales_tax($product_real_price, $product_sales_tax_rate = 0)
+    {
+        $config = $this->configModel->findConfig();
+        $sales_tax_rate =  $config ? $config->sales_tax_rate : 0;
+        $sales_tax_rate = $product_sales_tax_rate > 0 ? $product_sales_tax_rate : $sales_tax_rate;
+        if ($product_sales_tax_rate == 0 || $product_real_price == 0) {
+            return ['sales_tax_rate' => 0, 'sales_tax' => 0];
+        }
+        return [
+            'sales_tax_rate' => $sales_tax_rate,
+            'sales_tax' => fixed2Float($product_real_price * ($sales_tax_rate / 100))
+        ];
+    }
+
+    // 计算单个商品总费用
+    private function calculate_product_amount()
+    {
+
+    }
+
+    // 生成单个订单商品
+    private function genOrderProduct($product, $order_product_id = null, $order = null, $order_no = null)
+    {
+
+    }
+
+
     /**
      * @param $params
      * @param array $store_products_params
@@ -113,7 +142,6 @@ class OrderService extends \app\BaseService
         if (in_array(null, $products))
             return $this->fail(lang("The Product does not exist"));
 
-        $config = $this->configModel->findConfig();
         $activityProducts = $this->activityProductModel->fetchByProductIds(array_map(function ($data) {
             return $data['id'];
         }, $products))->toArray();
@@ -125,11 +153,10 @@ class OrderService extends \app\BaseService
             $res = compare($item['product']['real_price'] ?? 0, $aProducts);
             $item['activity'] = $res['item'];
             $item['product']['origin_price'] = $item['product']['real_price'];
-            $item['product']['real_price'] = fixed2Float($res['min_num'] > 0 ? $res['min_num'] : 0);
-            $sales_tax_rate = $item['product']['sales_tax_rate'] > 0 ? $item['product']['sales_tax_rate'] : $config->sales_tax_rate;
-            $sales_tax = fixed2Float($sales_tax_rate > 0 && $item['product']['real_price'] > 0 ? ($item['product']['real_price'] * ($sales_tax_rate / 100)) : 0);
-            $item['product']['sales_tax_rate'] = $sales_tax_rate;
-            $item['product']['sales_tax'] = $sales_tax;
+            $item['product']['real_price'] = $res['min_num'];
+            $tax = $this->calculate_sales_tax($item['product']['real_price'], $item['product']['sales_tax_rate']);
+            $item['product']['sales_tax_rate'] = $tax['sales_tax_rate'];
+            $item['product']['sales_tax'] = $tax['sales_tax'];
             $fmt_store_products[$item['id']] = $item;
         }
         $order_products = [];
@@ -151,7 +178,6 @@ class OrderService extends \app\BaseService
             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, [
@@ -229,6 +255,31 @@ class OrderService extends \app\BaseService
         return $this->ok($order);
     }
 
+
+    private function fetch_fmt_pay_channels()
+    {
+        $pay_channels = $this->paymentChannelModel->where('is_delete', 0)->select()->toArray();
+        return array_reduce($pay_channels, function ($result, $item) {
+            $result[$item['id']] = $item;
+            return $result;
+        }, []);
+    }
+
+    private function fetch_fmt_credit_card_configs()
+    {
+        $credit_card_configs = $this->creditCardModel->findAll()->toArray();
+        return array_reduce($credit_card_configs, function ($result, $item) {
+            $result[$item['id']] = $item;
+            return $result;
+        }, []);
+    }
+
+    private function findCustomerZueCoin($customer_id)
+    {
+        $zue_coin_model = $this->zueCoinModel->findByCustomerId($customer_id);
+        return $zue_coin_model ? $zue_coin_model->zue_coin : 0;
+    }
+
     /**
      * @param $params
      * @param $channels
@@ -243,23 +294,14 @@ class OrderService extends \app\BaseService
         $order = $this->orderModel->findById($params['order_id']);
         if (!$order)
             return $this->fail(lang("Order does not exist"));
-        if ($order->type == 1)
-            return $this->fail('订单状态为历史订单!不支持付款');
+        if ($order->imposed_amount <= 0)
+            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;
-            return $result;
-        }, []);
-        $credit_card_configs = $this->creditCardModel->findAll()->toArray();
-        $fmt_credit_card_configs = array_reduce($credit_card_configs, function ($result, $item) {
-            $result[$item['id']] = $item;
-            return $result;
-        }, []);
-
+        $fmt_pay_channels = $this->fetch_fmt_pay_channels();
+        $fmt_credit_card_configs = $this->fetch_fmt_credit_card_configs();
         // 本次总计支付费用
         $total_fee = 0;
         // 本次信用卡支付手续费
@@ -268,12 +310,16 @@ class OrderService extends \app\BaseService
         $zue_coin = 0;
         $order_payments = [];
         $record_channel_names = [];
+        $channel_ids = array_map(function ($data) {
+            return $data['channel_id'];
+        }, $this->orderPaymentModel->findByOrderId($order->id)->toArray());
         for ($i = 0; $i < count($channels); $i++) {
             $channel = $channels[$i];
             $fmt_pay_channel = $fmt_pay_channels[$channel->channel_id];
             if (!$fmt_pay_channel)
                 return $this->fail("支付渠道错误!");
             array_push($record_channel_names, '[' . $fmt_pay_channel['name'] . ']');
+            array_push($channel_ids, $channel->channel_id);
             $item = [
                 'order_id' => $order->id,
                 'channel_id' => $channel->channel_id,
@@ -343,15 +389,10 @@ class OrderService extends \app\BaseService
             }
         }
 
-        // 信用卡手续费
-        $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);
-
-
+        // 总共需要收取这么多费用; (商品的价格 + 商品税费 + 年费) - 御龙币抵扣的费用
+        $receivable_amount = fixed2Float($order->product_amount + $order->total_sales_tax + $order->total_annual_fee);
         $order_use_zue_coin = $order->zue_coin;
         $order_use_zue_coin_amount = $order->zue_coin_amount;
-
         $change_zue_coin_record = null;
         $zue_coin_model = $this->zueCoinModel->findByCustomerId($customer->id);
         $zue_coin_exchange_rate = 0;
@@ -372,15 +413,13 @@ class OrderService extends \app\BaseService
             // 2.结合之前已使用的御龙币 算出还能使用多少御龙币
             // 3.判断当前御龙币是否符合兑换数额
             // 4.进行扣除并记录
-
             // 根据御龙币的设置 总共能报销这么多钱
-            $max_consume_amount = fixed2Float($imposed_amount * ($zue_coin_consume_rate / 100));
+            $max_consume_amount = fixed2Float($receivable_amount * ($zue_coin_consume_rate / 100));
             // 根据御龙币的设置 最大的报销的御龙币
             $max_consume_zue_coin = fixed2Float($max_consume_amount / $zue_coin_exchange_rate);
             if ($max_consume_zue_coin - ($order->zue_coin + $zue_coin) < 0) {
                 return $this->fail("最多能使用御龙币: {$max_consume_zue_coin}, 当前已使用{$order->zue_coin}");
             }
-
             // 根据当前订单支付情况 得出现在最大的报销金额;
             $now_max_consume_amount = $order->imposed_amount < $max_consume_amount ? $order->imposed_amount : $max_consume_amount;
             // 当前最多还能使用多少御龙币
@@ -388,7 +427,6 @@ class OrderService extends \app\BaseService
             // 判断当前还能使用
             if ($now_can_consume_zue_coin - $zue_coin < 0)
                 return $this->fail("当前剩余可使用的: {$now_can_consume_zue_coin}");
-
             // 总共使用了这么多御龙币
             $order_use_zue_coin += $zue_coin;
             // 总共兑换了这么多钱
@@ -405,16 +443,19 @@ class OrderService extends \app\BaseService
             ];
         }
         // 总计费用 - 御龙币抵消费用;
-        $imposed_amount -= $order_use_zue_coin_amount;
+        $receivable_amount -= $order_use_zue_coin_amount;
 
         // 总计实收金额
         $receive_amount = fixed2Float($order->receive_amount + $total_fee);
-        if (round($receive_amount) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 当前已付款: {$order->receive_amount} / {$order->receivable_amount} (商品总价:{$order->product_amount} + 商品总消费税:{$order->total_sales_tax} + 第一年年费:{$order->total_annual_fee} + 信用卡总手续费:{$order->service_charge_amount})");
+        if (round($receive_amount) > round($receivable_amount)) return $this->fail("实收金额大于待收金额! 当前已付款: {$order->receive_amount} / {$order->receivable_amount} (商品总价:{$order->product_amount} + 商品总消费税:{$order->total_sales_tax} + 第一年年费:{$order->total_annual_fee} + 信用卡总手续费:{$order->service_charge_amount})");
         $order_annual_fees = [];
         $update_store_products = [];
         $update_order_products = [];
 
-        if (round($receive_amount) == round($imposed_amount)) { // 减库存
+        // 是否全部支付
+        $is_all_in_pay = round($receive_amount) == round($receivable_amount);
+        if ($is_all_in_pay) {
+            // 减库存
             $order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
             $store_products = $this->storeProductModel->findByIds(array_map(function ($r) {
                 return $r['store_product_id'];
@@ -423,6 +464,7 @@ class OrderService extends \app\BaseService
                 $result[$item['id']] = $item;
                 return $result;
             }, []);
+
             // 商品本身的价格 | 年费 | 消费税
             $total_ = $order->product_amount + $order->total_annual_fee + $order->total_sales_tax;
             // 信用卡手续费 每一块钱所占的比例
@@ -469,13 +511,14 @@ class OrderService extends \app\BaseService
                 $item_service_charge = fixed2Float($now * $item_service_charge_rate);
                 $item_zue_coin = fixed2Float($now * $item_zue_coin_rate);
                 $item_zue_coin_amount = fixed2Float($item_zue_coin * $zue_coin_exchange_rate);
-                $transaction_price = fixed2Float($now + $item_service_charge - $item_zue_coin_amount);
+                $transaction_price = fixed2Float($now - $item_zue_coin_amount);
                 array_push($update_order_products, [
                     'id' => $order_product['id'],
                     'service_charge' => $item_service_charge,
                     'zue_coin' => $item_zue_coin,
                     'zue_coin_amount' => $item_zue_coin_amount,
                     'transaction_price' => $transaction_price,
+                    'channel_ids' => join(',', array_unique($channel_ids)),
                     'is_pay' => 1
                 ]);
             }
@@ -483,18 +526,15 @@ class OrderService extends \app\BaseService
         Db::startTrans();
         try {
             $update_order = [
-                'rental_amount' => $imposed_amount + $order_use_zue_coin_amount,
-                'receivable_amount' => $imposed_amount,
+                'receivable_amount' => $receivable_amount,
                 'receive_amount' => $receive_amount,
-                'imposed_amount' => $imposed_amount - $receive_amount,
+                'imposed_amount' => $receivable_amount - $receive_amount,
                 'service_charge_amount' => $order->service_charge_amount + $service_charge,
                 'zue_coin' => $order_use_zue_coin,
                 'zue_coin_amount' => $order_use_zue_coin_amount,
                 'remarks' => $params['remarks'] ?? null
             ];
-
-            if (round($receive_amount) == round($imposed_amount)) { // 支付满了
-                $update_order['type'] = 1;
+            if ($is_all_in_pay) { // 支付满了
                 if (count($update_order_products) > 0) {
                     foreach ($update_order_products as $item) {
                         Db::table('erp_order_product')->where('id', $item['id'])->update($item);
@@ -550,216 +590,6 @@ class OrderService extends \app\BaseService
         return $this->ok(true);
     }
 
-    /**
-     * @param $params
-     * @return array
-     * @throws \think\db\exception\DbException
-     */
-    public function fetch($params)
-    {
-        $verify = AuthService::verify($params['admin_id'], $params['type'] == 1 ? 10003 : 10004);
-        $res = $this->orderModel->search($params, $verify);
-        $items = $res['data'];
-        return [array_map(function ($item) {
-            return [
-                "id" => $item['id'],
-                "no" => $item['no'],
-                "customer_id" => $item['customer_id'],
-                "customer_name" => $item['customer_name'],
-                "obj_names" => $item['obj_names'],
-                "rental_amount" => $item['rental_amount'],
-                "receivable_amount" => $item['receivable_amount'],
-                "receive_amount" => $item['receive_amount'],
-                "imposed_amount" => $item['imposed_amount'],
-                "product_amount" => $item['product_amount'],
-                "total_sales_tax" => $item['total_sales_tax'],
-                "service_charge_amount" => $item['service_charge_amount'],
-                "total_annual_fee" => $item['total_annual_fee'],
-                "zue_coin" => $item['zue_coin'],
-                "zue_coin_amount" => $item['zue_coin_amount'],
-                "type" => $item['type'],
-                "store_id" => $item['store_id'],
-                "advisor_ids" => $item['advisor_ids'],
-                "remarks" => $item['remarks'],
-                "create_time" => $item['create_time'],
-                "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
-                    array_push($result, $item['adviser_1_name']);
-                    if ($item['adviser_2_id'] > 0)
-                        array_push($result, $item['adviser_2_name']);
-                    return $result;
-                }, []))),
-                "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
-                    if ($item['teacher_1_id'] > 0)
-                        array_push($result, $item['teacher_1_name']);
-                    if ($item['teacher_2_id'] > 0)
-                        array_push($result, $item['teacher_2_name']);
-                    return $result;
-                }, []))),
-                "payments" => join(' ', array_unique(array_reduce($item['payments'], function ($result, $item) {
-                    array_push($result, '[' . $item['channel_name'] . ']');
-                    return $result;
-                }, []))),
-                "stage_num" => array_reduce($item['payments'], function ($result, $item) {
-                    if ($item['stage_num'] && $item['stage_num'] > $result) {
-                        $result = $item['stage_num'];
-                    }
-                    return $result;
-                }, 0),
-                'is_need_upload' => $item['type'] == 1 && in_array(true, array_map(function ($product) {
-                        return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
-                    }, $item['products'])),
-            ];
-        }, $items), $res['total']];
-    }
-
-
-    /**
-     * @param $params
-     * @return array
-     * @throws \think\db\exception\DbException
-     */
-    public function fetchByCustomerId($params)
-    {
-        $res = $this->orderModel->fetchByCustomerId($params['customer_id'], $params['page'] ?? 1, $params['size'] ?? 20);
-        $items = $res['data'];
-        return [array_map(function ($item) {
-            return [
-                "id" => $item['id'],
-                "no" => $item['no'],
-                "customer_id" => $item['customer_id'],
-                "customer_name" => $item['customer_name'],
-                "obj_names" => $item['obj_names'],
-                "rental_amount" => $item['rental_amount'],
-                "receivable_amount" => $item['receivable_amount'],
-                "receive_amount" => $item['receive_amount'],
-                "imposed_amount" => $item['imposed_amount'],
-                "product_amount" => $item['product_amount'],
-                "total_sales_tax" => $item['total_sales_tax'],
-                "service_charge_amount" => $item['service_charge_amount'],
-                "total_annual_fee" => $item['total_annual_fee'],
-                "zue_coin" => $item['zue_coin'],
-                "zue_coin_amount" => $item['zue_coin_amount'],
-                "type" => $item['type'],
-                "store_id" => $item['store_id'],
-                "advisor_ids" => $item['advisor_ids'],
-                "remarks" => $item['remarks'],
-                "create_time" => $item['create_time'],
-                "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
-                    array_push($result, $item['adviser_1_name']);
-                    if ($item['adviser_2_id'] > 0)
-                        array_push($result, $item['adviser_2_name']);
-                    return $result;
-                }, []))),
-                "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
-                    if ($item['teacher_1_id'] > 0)
-                        array_push($result, $item['teacher_1_name']);
-                    if ($item['teacher_2_id'] > 0)
-                        array_push($result, $item['teacher_2_name']);
-                    return $result;
-                }, []))),
-                "stage_num" => array_reduce($item['payments'], function ($result, $item) {
-                    if ($item['stage_num'] && $item['stage_num'] > $result) {
-                        $result = $item['stage_num'];
-                    }
-                    return $result;
-                }, 0),
-                'is_need_upload' => $item['type'] == 1 && in_array(true, array_map(function ($product) {
-                        return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
-                    }, $item['products'])),
-            ];
-        }, $items), $res['total']];
-    }
-
-    public function fetchOrderProducts($order_id, $page = 1, $size = 10)
-    {
-        $res = $this->orderProductModel->fetchByOrderId($order_id, $page, $size);
-        return [$res->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('订单商品不存在,无法上传命理报告!');
-        if (!$order_product->is_pay) return $this->fail('订单未支付,无法上传商品');
-        $order_product->is_upload = 1;
-        $order_product->report = $report;
-        $order_product->save();
-        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[0]->items(), $res[0]->total(), $res[1]];
-    }
-
-    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);
-    }
-
-    public function fetchOrderByReturn($admin_id, $store_id, $no = null, $customer_name = null, $page = 1, $size = 10)
-    {
-        $verify = AuthService::verify($admin_id, 10005);
-        $res = $this->orderModel->fetchOrderByReturn($admin_id, $store_id, $no, $customer_name, $page, $size, $verify)->toArray();
-        $items = $res['data'];
-        return [array_map(function ($item) {
-            return [
-                "id" => $item['id'],
-                "no" => $item['no'],
-                "customer_id" => $item['customer_id'],
-                "customer_name" => $item['customer_name'],
-                "obj_names" => $item['obj_names'],
-                "rental_amount" => $item['rental_amount'],
-                "receivable_amount" => $item['receivable_amount'],
-                "receive_amount" => $item['receive_amount'],
-                "imposed_amount" => $item['imposed_amount'],
-                "product_amount" => $item['product_amount'],
-                "total_sales_tax" => $item['total_sales_tax'],
-                "service_charge_amount" => $item['service_charge_amount'],
-                "total_annual_fee" => $item['total_annual_fee'],
-                "zue_coin" => $item['zue_coin'],
-                "zue_coin_amount" => $item['zue_coin_amount'],
-                "type" => $item['type'],
-                "store_id" => $item['store_id'],
-                "advisor_ids" => $item['advisor_ids'],
-                "remarks" => $item['remarks'],
-                "create_time" => $item['create_time'],
-                "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
-                    array_push($result, $item['adviser_1_name']);
-                    if ($item['adviser_2_id'] > 0)
-                        array_push($result, $item['adviser_2_name']);
-                    return $result;
-                }, []))),
-                "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
-                    if ($item['teacher_1_id'] > 0)
-                        array_push($result, $item['teacher_1_name']);
-                    if ($item['teacher_2_id'] > 0)
-                        array_push($result, $item['teacher_2_name']);
-                    return $result;
-                }, [])))
-            ];
-        }, $items), $res['total']];
-    }
-
-
-    public function findByOrderNo($admin_id, $order_no)
-    {
-        $order = $this->orderModel->findByOrderNo($admin_id, $order_no);
-        if (!$order)
-            return $this->fail(lang("Order does not exist"));
-        return $this->ok($order);
-    }
 
     /**
      * @param $admin_id
@@ -837,8 +667,8 @@ class OrderService extends \app\BaseService
                 $return_now_stocks[$return_order_product['store_product_id']] = 1;
             }
         }
-        $total_order_rental_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_service_amount + $total_annual_fee);
-        $total_receivable_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_service_amount + $total_annual_fee - ($total_zue_coin * $zue_coin_exchange_rate));
+        $total_order_rental_amount = fixed2Float($total_product_amount + $total_sales_tax  + $total_annual_fee);
+        $total_receivable_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_annual_fee - ($total_zue_coin * $zue_coin_exchange_rate));
         $total_receive_amount = $total_receivable_amount;
 
         $annual_fees = $this->orderAnnualFeeModel->fetchByOrderProductId($order_product_ids);
@@ -1145,14 +975,14 @@ class OrderService extends \app\BaseService
             Db::table('erp_order_product')->where('id', 'in', $origin_order_product_ids)->update(['is_delete' => 1, 'delete_time' => time()]);
 
             $imposed_amount = fixed2Float($total_receivable_amount - $order->receive_amount); // 应收金额 - 实收金额
-            if($imposed_amount == 0) {// 应收金额 == 代收金额;
+            if ($imposed_amount == 0) {// 应收金额 == 代收金额;
                 // 新增商品
                 for ($i = 0; $i < count($order_products); $i++) {
                     $order_product = $order_products[$i];
                     $order_product['is_pay'] = 1;
                     $order_annual_fee = $order_annual_fees[$i];
                     $last_id = Db::table('erp_order_product')->insert($order_product, true);
-                    if($order_annual_fee != null) {
+                    if ($order_annual_fee != null) {
                         $order_annual_fee['order_product_id'] = $last_id;
                         $order_annual_fee['is_pay'] = 1;
                         Db::table('erp_order_annual_fee')->save($order_annual_fee);
@@ -1169,11 +999,11 @@ class OrderService extends \app\BaseService
                     'receivable_amount' => $total_receivable_amount,
                     'receive_amount' => $total_receive_amount,
                     'product_amount' => $total_product_amount,
-                    'total_sales_tax'   =>  $total_sales_tax,
-                    'service_charge_amount' =>  $total_service_amount,
-                    'total_annual_fee'  =>  $total_annual_fee,
-                    'zue_coin'  =>  $total_zue_coin,
-                    'zue_coin_amount'   =>  $total_zue_coin_amount,
+                    'total_sales_tax' => $total_sales_tax,
+                    'service_charge_amount' => $total_service_amount,
+                    'total_annual_fee' => $total_annual_fee,
+                    'zue_coin' => $total_zue_coin,
+                    'zue_coin_amount' => $total_zue_coin_amount,
                     'obj_names' => join(',', array_unique($obj_names)),
                     'advisor_ids' => join(',', array_unique($advisor_ids)),
                 ]);
@@ -1204,28 +1034,18 @@ class OrderService extends \app\BaseService
         return $this->ok($diff_amount);
     }
 
-
-    public function findOrderByOrderId($order_id)
-    {
-        return $this->orderModel->findById($order_id);
-    }
-
-    public function fetchOrderPayments($order_id)
+    public function paymentDone($admin_id, $order_id, $remarks = null)
     {
-        return $this->orderPaymentModel->findByOrderId($order_id);
-    }
-
-    public function paymentDone($admin_id, $order_id, $remarks = null) {
         $order = $this->orderModel->findById($order_id);
         if (!$order)
             return $this->fail(lang("Order does not exist"));
-        if ($order->type == 1)
+        if ($order->imposed_amount < 0)
             return $this->fail('订单状态为历史订单!不支持付款');
         // 客户
         $customer = $this->customerModel->findById($order->customer_id);
         if (!$customer)
             return $this->fail(lang("The Customer does not exist"));
-        $no_pay_order_products = $this->orderProductModel->findByOrderId($order->id,0)->toArray();
+        $no_pay_order_products = $this->orderProductModel->findByOrderIdAndPay($order->id, 0)->toArray();
         Db::startTrans();
         try {
             $update_order = [
@@ -1237,10 +1057,10 @@ class OrderService extends \app\BaseService
             $order_annual_fees = [];
             for ($i = 0; $i < count($no_pay_order_products); $i++) {
                 $order_product = $no_pay_order_products[$i];
-                Db::table('erp_order_product')->where('id',$order_product['id'])->update([
+                Db::table('erp_order_product')->where('id', $order_product['id'])->update([
                     'is_pay' => 1
                 ]);
-                if($order_product['is_gather_annuity'] && $order_product['annuity'] > 0) {
+                if ($order_product['is_gather_annuity'] && $order_product['annuity'] > 0) {
                     array_push($order_annual_fees, [
                         'order_id' => $order->id,
                         'customer_id' => $order->customer_id,
@@ -1250,7 +1070,7 @@ class OrderService extends \app\BaseService
                         'index' => 1,
                         'order_product_id' => $order_product['id'],
                         'order_product_name' => $order_product['product_name'],
-                        'fee' => $order_product['annuity'] ,
+                        'fee' => $order_product['annuity'],
                         'is_pay' => 1,
                         'start_time' => time(),
                         'end_time' => time() + (365 * 24 * 60 * 60),
@@ -1288,6 +1108,224 @@ class OrderService extends \app\BaseService
         return $this->ok(true);
     }
 
+    /**
+     * @param $params
+     * @return array
+     * @throws \think\db\exception\DbException
+     */
+    public function fetch($params)
+    {
+        $verify = AuthService::verify($params['admin_id'], $params['type'] == 1 ? 10003 : 10004);
+        $res = $this->orderModel->search($params, $verify);
+        $items = $res['data'];
+        return [array_map(function ($item) {
+            return [
+                "id" => $item['id'],
+                "no" => $item['no'],
+                "customer_id" => $item['customer_id'],
+                "customer_name" => $item['customer_name'],
+                "obj_names" => $item['obj_names'],
+                "rental_amount" => $item['rental_amount'],
+                "receivable_amount" => $item['receivable_amount'],
+                "receive_amount" => $item['receive_amount'],
+                "imposed_amount" => $item['imposed_amount'],
+                "product_amount" => $item['product_amount'],
+                "total_sales_tax" => $item['total_sales_tax'],
+                "service_charge_amount" => $item['service_charge_amount'],
+                "total_annual_fee" => $item['total_annual_fee'],
+                "zue_coin" => $item['zue_coin'],
+                "zue_coin_amount" => $item['zue_coin_amount'],
+                "type" => $item['type'],
+                "store_id" => $item['store_id'],
+                "advisor_ids" => $item['advisor_ids'],
+                "remarks" => $item['remarks'],
+                "create_time" => $item['create_time'],
+                "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
+                    array_push($result, $item['adviser_1_name']);
+                    if ($item['adviser_2_id'] > 0)
+                        array_push($result, $item['adviser_2_name']);
+                    return $result;
+                }, []))),
+                "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
+                    if ($item['teacher_1_id'] > 0)
+                        array_push($result, $item['teacher_1_name']);
+                    if ($item['teacher_2_id'] > 0)
+                        array_push($result, $item['teacher_2_name']);
+                    return $result;
+                }, []))),
+                "payments" => join(' ', array_unique(array_reduce($item['payments'], function ($result, $item) {
+                    array_push($result, '[' . $item['channel_name'] . ']');
+                    return $result;
+                }, []))),
+                "stage_num" => array_reduce($item['payments'], function ($result, $item) {
+                    if ($item['stage_num'] && $item['stage_num'] > $result) {
+                        $result = $item['stage_num'];
+                    }
+                    return $result;
+                }, 0),
+                'is_need_upload' => $item['type'] == 1 && in_array(true, array_map(function ($product) {
+                        return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
+                    }, $item['products'])),
+            ];
+        }, $items), $res['total']];
+    }
+
+    /**
+     * @param $params
+     * @return array
+     * @throws \think\db\exception\DbException
+     */
+    public function fetchByCustomerId($params)
+    {
+        $res = $this->orderModel->fetchByCustomerId($params['customer_id'], $params['page'] ?? 1, $params['size'] ?? 20);
+        $items = $res['data'];
+        return [array_map(function ($item) {
+            return [
+                "id" => $item['id'],
+                "no" => $item['no'],
+                "customer_id" => $item['customer_id'],
+                "customer_name" => $item['customer_name'],
+                "obj_names" => $item['obj_names'],
+                "rental_amount" => $item['rental_amount'],
+                "receivable_amount" => $item['receivable_amount'],
+                "receive_amount" => $item['receive_amount'],
+                "imposed_amount" => $item['imposed_amount'],
+                "product_amount" => $item['product_amount'],
+                "total_sales_tax" => $item['total_sales_tax'],
+                "service_charge_amount" => $item['service_charge_amount'],
+                "total_annual_fee" => $item['total_annual_fee'],
+                "zue_coin" => $item['zue_coin'],
+                "zue_coin_amount" => $item['zue_coin_amount'],
+                "type" => $item['type'],
+                "store_id" => $item['store_id'],
+                "advisor_ids" => $item['advisor_ids'],
+                "remarks" => $item['remarks'],
+                "create_time" => $item['create_time'],
+                "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
+                    array_push($result, $item['adviser_1_name']);
+                    if ($item['adviser_2_id'] > 0)
+                        array_push($result, $item['adviser_2_name']);
+                    return $result;
+                }, []))),
+                "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
+                    if ($item['teacher_1_id'] > 0)
+                        array_push($result, $item['teacher_1_name']);
+                    if ($item['teacher_2_id'] > 0)
+                        array_push($result, $item['teacher_2_name']);
+                    return $result;
+                }, []))),
+                "stage_num" => array_reduce($item['payments'], function ($result, $item) {
+                    if ($item['stage_num'] && $item['stage_num'] > $result) {
+                        $result = $item['stage_num'];
+                    }
+                    return $result;
+                }, 0),
+                'is_need_upload' => $item['type'] == 1 && in_array(true, array_map(function ($product) {
+                        return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
+                    }, $item['products'])),
+            ];
+        }, $items), $res['total']];
+    }
+
+    public function fetchOrderProducts($order_id, $page = 1, $size = 10)
+    {
+        $res = $this->orderProductModel->fetchByOrderId($order_id, $page, $size);
+        return [$res->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('订单商品不存在,无法上传命理报告!');
+        if (!$order_product->is_pay) return $this->fail('订单未支付,无法上传商品');
+        $order_product->is_upload = 1;
+        $order_product->report = $report;
+        $order_product->save();
+        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[0]->items(), $res[0]->total(), $res[1]];
+    }
+
+    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);
+    }
+
+    public function fetchOrderByReturn($admin_id, $store_id, $no = null, $customer_name = null, $page = 1, $size = 10)
+    {
+        $verify = AuthService::verify($admin_id, 10005);
+        $res = $this->orderModel->fetchOrderByReturn($admin_id, $store_id, $no, $customer_name, $page, $size, $verify)->toArray();
+        $items = $res['data'];
+        return [array_map(function ($item) {
+            return [
+                "id" => $item['id'],
+                "no" => $item['no'],
+                "customer_id" => $item['customer_id'],
+                "customer_name" => $item['customer_name'],
+                "obj_names" => $item['obj_names'],
+                "rental_amount" => $item['rental_amount'],
+                "receivable_amount" => $item['receivable_amount'],
+                "receive_amount" => $item['receive_amount'],
+                "imposed_amount" => $item['imposed_amount'],
+                "product_amount" => $item['product_amount'],
+                "total_sales_tax" => $item['total_sales_tax'],
+                "service_charge_amount" => $item['service_charge_amount'],
+                "total_annual_fee" => $item['total_annual_fee'],
+                "zue_coin" => $item['zue_coin'],
+                "zue_coin_amount" => $item['zue_coin_amount'],
+                "type" => $item['type'],
+                "store_id" => $item['store_id'],
+                "advisor_ids" => $item['advisor_ids'],
+                "remarks" => $item['remarks'],
+                "create_time" => $item['create_time'],
+                "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
+                    array_push($result, $item['adviser_1_name']);
+                    if ($item['adviser_2_id'] > 0)
+                        array_push($result, $item['adviser_2_name']);
+                    return $result;
+                }, []))),
+                "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
+                    if ($item['teacher_1_id'] > 0)
+                        array_push($result, $item['teacher_1_name']);
+                    if ($item['teacher_2_id'] > 0)
+                        array_push($result, $item['teacher_2_name']);
+                    return $result;
+                }, [])))
+            ];
+        }, $items), $res['total']];
+    }
+
+    public function findByOrderNo($admin_id, $order_no)
+    {
+        $order = $this->orderModel->findByOrderNo($admin_id, $order_no);
+        if (!$order)
+            return $this->fail(lang("Order does not exist"));
+        return $this->ok($order);
+    }
+
+    public function findOrderByOrderId($order_id)
+    {
+        return $this->orderModel->findById($order_id);
+    }
+
+    public function fetchOrderPayments($order_id)
+    {
+        return $this->orderPaymentModel->findByOrderId($order_id);
+    }
+
 }
 
 

File diff suppressed because it is too large
+ 28 - 21
app/common.php


+ 9 - 2
app/common/model/OrderProductModel.php

@@ -28,13 +28,19 @@ class OrderProductModel extends BaseModel
 
     /**
      * @param $o_id
-     * @param int $is_pay
      * @return OrderProductModel[]|array|\think\Collection
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function findByOrderId($o_id, $is_pay = 1)
+    public function findByOrderId($o_id)
+    {
+        return $this->where('is_delete', 0)
+            ->where('order_id', $o_id)
+            ->select();
+    }
+
+    public function findByOrderIdAndPay($o_id, $is_pay)
     {
         return $this->where('is_delete', 0)
             ->where('order_id', $o_id)
@@ -42,6 +48,7 @@ class OrderProductModel extends BaseModel
             ->select();
     }
 
+
     /**
      * @param $o_id
      * @param int $page

File diff suppressed because it is too large
+ 1 - 0
public/storage/numerology/20230111/3a064091d32b072ba52ea56cc5ddfd7a.svg


BIN
public/storage/numerology/20230111/4d6124213daf68c9446c79c96bd4c0d0.jpg


BIN
public/storage/numerology/20230111/63e8d70e3a18d5ffa894962ed16c528b.jpg


File diff suppressed because it is too large
+ 1 - 0
public/storage/numerology/20230111/cb79946e8760b4e4320b3d6834c6137f.svg


BIN
public/storage/numerology/20230111/e9b5c71bdf824ac6cbfd91c9d9f14204.jpg