Explorar el Código

return order product

1557492053 hace 2 años
padre
commit
e4021d89f7

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

@@ -50,6 +50,7 @@ return [
     "Please select the item to return"  =>  "请选择需要退掉的商品!",
     "Order does not exist"  =>  "订单不存在",
     "The order item was incorrectly selected"   =>  "订单商品选择有误!",
+    "The status of the order does not support return or exchange"   =>  "订单状态不支持退换货!",
 ];
 
 

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

@@ -19,6 +19,8 @@ use app\common\model\PaymentChannelModel;
 use app\common\model\ProductModel;
 use app\common\model\StoreModel;
 use app\common\model\StoreProductModel;
+use app\common\model\ZueCoinRecordModel;
+use app\exception\BaseException;
 use think\facade\Db;
 
 class OrderService extends \app\BaseService
@@ -38,6 +40,7 @@ class OrderService extends \app\BaseService
     private $paymentChannelModel;
     private $creditCardModel;
     private $zueCoinModel;
+    private $zueCoinRecordModel;
 
     public function __construct()
     {
@@ -56,6 +59,7 @@ class OrderService extends \app\BaseService
         $this->paymentChannelModel = new PaymentChannelModel();
         $this->creditCardModel = new CreditCardConfigModel();
         $this->zueCoinModel = new CustomerZueCoinModel();
+        $this->zueCoinRecordModel = new ZueCoinRecordModel();
     }
 
     /**
@@ -388,8 +392,9 @@ class OrderService extends \app\BaseService
             $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,
+                'order_id'      =>  $order->id,
                 'origin'        =>  $zue_coin_model->zue_coin,
-                'change'        =>  $zue_coin,
+                'change'        =>  -$zue_coin,
                 'after'        =>  $zue_coin_model->zue_coin - $zue_coin,
                 'reason'        =>  1,
                 'create_time'   =>  time(),
@@ -451,12 +456,14 @@ class OrderService extends \app\BaseService
                 $now = $order_product['real_price'] + $order_product['annuity'] + $order_product['sales_tax'];
                 $item_service_charge = fixed2Float($now * $item_service_charge_rate);
                 $item_zue_coin = fixed2Float($now * $item_zue_coin_rate);
-                $transaction_price = fixed2Float($now + $item_service_charge - ($item_zue_coin * $zue_coin_exchange_rate));
+                $item_zue_coin_amount = fixed2Float($item_zue_coin * $zue_coin_exchange_rate);
+                $transaction_price = fixed2Float($now + $item_service_charge - $item_zue_coin_amount);
                 array_push($update_order_products, [
                     'id'    =>  $order_product['id'],
-                    'service_charge'    => $item_service_charge,
-                    'zue_coin'          => $item_zue_coin,
-                    'transaction_price' => $transaction_price,
+                    'service_charge'    =>  $item_service_charge,
+                    'zue_coin'          =>  $item_zue_coin,
+                    'zue_coin_amount'   =>  $item_zue_coin_amount,
+                    'transaction_price' =>  $transaction_price,
                     'is_pay' => 1
                 ]);
             }
@@ -689,6 +696,14 @@ class OrderService extends \app\BaseService
         return [$res->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
      * @param $order_id
@@ -702,21 +717,130 @@ class OrderService extends \app\BaseService
 //        $verify = AuthService::verify($admin_id, 10005);
         $order = $this->orderModel->findById($order_id);
         if (!$order) return $this->fail(lang("Order does not exist"));
+        if ($order->type == 2) return $this->fail(lang("The status of the order does not support return or exchange"));
         $return_order_products = $this->orderProductModel->findByIds($order_product_ids)->toArray();
         if (count($order_product_ids) != count($return_order_products)) return $this->fail(lang("The order item was incorrectly selected"));
         $all_order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
+        $merge_order_products = array_reduce($all_order_products,
+            function ($result, $item) use($order_product_ids) {
+                if(!in_array($item['id'],$order_product_ids))
+                    array_push($result, $item);
+                return $result;
+            }, []);
+        $obj_names = array_map(function ($data) {return $data['product_name'];}, $merge_order_products);
+        $advisor_ids = array_reduce($merge_order_products, function ($result, $item) {
+            if ($item['adviser_1_id'])
+                array_push($result, $item['adviser_1_id']);
+            if ($item['adviser_2_id'])
+                array_push($result, $item['adviser_2_id']);
+            return $result;
+        },[]);
+        //商品本金
+        $total_product_amount = $order->product_amount;
+        // 整体税费
+        $total_sales_tax = $order->total_sales_tax;
+        // 总体信用卡服务费
+        $total_service_amount = $order->service_charge_amount;
+        // 总体年费
+        $total_annual_fee = $order->total_annual_fee;
+        // 总体御龙币
+        $total_zue_coin = $order->zue_coin;
+        // 御龙币兑换的金额
+        $total_zue_coin_amount = $order->zue_coin_amount;
+        // 御龙币当时的兑换比例
+        $zue_coin_exchange_rate = fixed2Float($order->zue_coin_amount / $order->zue_coin);
+        // 总计退还多少钱
+        $total_return_amount = 0;
+
+        // 商品库存
+        $return_now_stocks = [];
+        foreach ($return_order_products as $return_order_product) {
+            // 减少御龙币
+            $total_zue_coin -= $return_order_product['zue_coin'];
+            $total_zue_coin_amount -= ($return_order_product['zue_coin'] * $zue_coin_exchange_rate);
+            // 减少年费
+            $total_annual_fee -= $return_order_product['annuity'];
+            // 减少信用卡服务费
+            $total_service_amount -= $return_order_product['service_charge'];
+            // 减少税费
+            $total_sales_tax -= $return_order_product['sales_tax'];
+            // 减少本金
+            $total_product_amount -= $return_order_product['real_price'];
+            $total_return_amount += $return_order_product['transaction_price'];
+            if(isset($return_now_stocks[$return_order_product['store_product_id']])) {
+                $return_now_stocks[$return_order_product['store_product_id']] += 1;
+            } else {
+                $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_receive_amount = $total_receivable_amount;
 
+        $annual_fees = $this->orderAnnualFeeModel->fetchByOrderProductId($order_product_ids);
         Db::startTrans();
         try {
-            if(count($return_order_products) == count($all_order_products)) {
-                Db::table('erp_order')->where('id', $order->id)->update(['is_delete' => 1]);
-                Db::table('erp_order_product')->where('order_id', $order->id)->update(['is_delete' => 1]);
+            // 退还御龙币
+            $return_zue_coin = $order->zue_coin - $total_zue_coin;
+            if ($return_zue_coin > 0) {
+                $customer_zue_coin = $this->zueCoinModel->findByCustomerId($order->customer_id);
+                if(!$customer_zue_coin) {
+                    throw new BaseException('御龙币账户记录不存在',1012);
+                }
+                Db::table('erp_customer_zue_coin')->where('id', '=', $customer_zue_coin->id)->inc('zue_coin', $return_zue_coin)->update();
+                Db::table('erp_zue_coin_record')->save([
+                    'zue_coin_id'   =>  $customer_zue_coin->id,
+                    'order_id'   =>  $order->id,
+                    'origin'        =>  $customer_zue_coin->zue_coin,
+                    'change'        =>  $return_zue_coin,
+                    'after'        =>  $customer_zue_coin->zue_coin + $return_zue_coin,
+                    'reason'        =>  3,
+                    'create_time'   =>  time(),
+                    'update_time'   =>  time(),
+                ]);
+            }
+            // 退还年费
+            if (count($annual_fees) > 0)
+                Db::table('erp_order_annual_fee')->where('id', 'in', array_map(function ($data) {return $data['id'];}, $annual_fees))->update(['is_delete' => 1, 'delete_time' => time()]);
+
+            // 更新订单
+            if (count($return_order_products) == count($all_order_products)){
                 Db::table('erp_order')->where('id', $order->id)->update(['is_delete' => 1]);
             } else {
-                foreach ($return_order_products as $return_order_product) {
-//                    $return_order_product['']
-                }
+                Db::table('erp_order')->where('id', $order->id)->update([
+                    'rental_amount' => $total_order_rental_amount,
+                    '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,
+                    'obj_names' => join(',', array_unique($obj_names)),
+                    'advisor_ids' => join(',', array_unique($advisor_ids)),
+                ]);
+            }
+            // 插入退款记录
+            Db::table('erp_order_payment')->save([
+                'order_id' => $order->id,
+                'channel_id' => 0,
+                'channel_name' => "Exchange a purchase",
+                'fee'   => -$total_return_amount,
+                'create_time' => time(),
+                'update_time' => time(),
+            ]);
+            // 退还商品库存
+            $return_stock_keys = array_keys($return_now_stocks);
+            foreach ($return_stock_keys as $key) {
+                Db::table("erp_store_product")
+                    ->where('id', $key)
+                    ->inc('now_stock', $return_now_stocks[$key])
+                    ->dec('sale_stock', $return_now_stocks[$key])
+                    ->update();
             }
+            // 删除订单商品
+            Db::table('erp_order_product')->where('id', 'in', $order_product_ids)->update(['is_delete' => 1, 'delete_time' => time()]);
             Db::commit();
         } catch (\Exception $e) {
             Db::rollback();
@@ -725,13 +849,6 @@ class OrderService extends \app\BaseService
         return $this->ok(true);
     }
 
-    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 exchange($admin_id, $order_id, $origin_order_product_ids, $append_product_ids) {
         return $this->ok(true);
     }

+ 31 - 2
app/api/service/ProductService.php

@@ -27,7 +27,7 @@ class ProductService extends BaseService
         $this->activityProductModel = new ActivityProductModel();
     }
 
-    public function search($store_id,$bar_code = null, $product_name = null, $page = 1, $size = 10) {
+    public function search($store_id, $bar_code = null, $product_name = null, $page = 1, $size = 10) {
         $storeProducts = $this->storeProductModel->search([
             'store_id' => $store_id,
             'bar_code' => $bar_code,
@@ -54,4 +54,33 @@ class ProductService extends BaseService
         }
         return [$items, $storeProducts->total()];
     }
-}
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

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

@@ -28,7 +28,7 @@ abstract class BaseModel extends Model
 
 
     public function deleteByIds(array $ids) {
-        return $this->whereIn('id',$ids)->update(['is_delete' => 1]);
+        return $this->whereIn('id',$ids)->update(['is_delete' => 1, 'delete_time' => time()]);
     }
 
     /**

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

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

+ 10 - 1
app/common/model/OrderAnnualFeeModel.php

@@ -25,7 +25,7 @@ class OrderAnnualFeeModel extends BaseModel
         $where = [
             ['is_delete', '=', 0],
             ['is_pay', '=', 0],
-            ['adviser_1_id', '=', $admin_id],
+            ['adviser_1_id|adviser_2_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]);
@@ -36,4 +36,13 @@ class OrderAnnualFeeModel extends BaseModel
 
     }
 
+    public function fetchByOrderProductId(array $o_p_ids) {
+        $where = [
+            ['is_delete', '=', 0],
+            ['order_product_id', 'in', $o_p_ids],
+        ];
+        return $this->where($where)
+            ->select()->toArray();
+    }
+
 }

+ 0 - 1
app/common/model/OrderModel.php

@@ -75,7 +75,6 @@ class OrderModel extends BaseModel
                 'products',
                 'payments',
                 'payments.payment_channel',
-//                'payments.card_config',
                 'annuals',
                 'proceeds'
             ])->find($id);

+ 11 - 0
app/common/model/OrderPaymentModel.php

@@ -21,4 +21,15 @@ class OrderPaymentModel extends BaseModel
     {
         // TODO: Implement genSchema() method.
     }
+
+    public function findByOrderId($o_id) {
+        return $this->where([
+            ['order_id', '=', $o_id],
+            ['is_delete', '=', 0],
+            ['channel_id', '=', 11],
+        ])->order('id', 'desc')->find();
+    }
+
+
+
 }

+ 8 - 0
app/common/model/ZueCoinRecordModel.php

@@ -14,4 +14,12 @@ class ZueCoinRecordModel extends BaseModel
         // TODO: Implement genSchema() method.
     }
 
+    public function findByOrderId($o_id) {
+        return $this->where([
+            ['is_delete', '=', 0],
+            ['order_id', '=', $o_id],
+        ])->order('id','desc')->find();
+    }
+
+
 }