|
@@ -19,6 +19,8 @@ use app\common\model\PaymentChannelModel;
|
|
|
use app\common\model\ProductModel;
|
|
use app\common\model\ProductModel;
|
|
|
use app\common\model\StoreModel;
|
|
use app\common\model\StoreModel;
|
|
|
use app\common\model\StoreProductModel;
|
|
use app\common\model\StoreProductModel;
|
|
|
|
|
+use app\common\model\ZueCoinRecordModel;
|
|
|
|
|
+use app\exception\BaseException;
|
|
|
use think\facade\Db;
|
|
use think\facade\Db;
|
|
|
|
|
|
|
|
class OrderService extends \app\BaseService
|
|
class OrderService extends \app\BaseService
|
|
@@ -38,6 +40,7 @@ class OrderService extends \app\BaseService
|
|
|
private $paymentChannelModel;
|
|
private $paymentChannelModel;
|
|
|
private $creditCardModel;
|
|
private $creditCardModel;
|
|
|
private $zueCoinModel;
|
|
private $zueCoinModel;
|
|
|
|
|
+ private $zueCoinRecordModel;
|
|
|
|
|
|
|
|
public function __construct()
|
|
public function __construct()
|
|
|
{
|
|
{
|
|
@@ -56,6 +59,7 @@ class OrderService extends \app\BaseService
|
|
|
$this->paymentChannelModel = new PaymentChannelModel();
|
|
$this->paymentChannelModel = new PaymentChannelModel();
|
|
|
$this->creditCardModel = new CreditCardConfigModel();
|
|
$this->creditCardModel = new CreditCardConfigModel();
|
|
|
$this->zueCoinModel = new CustomerZueCoinModel();
|
|
$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);
|
|
$order_use_zue_coin_amount = fixed2Float($order_use_zue_coin * $zue_coin_exchange_rate);
|
|
|
$change_zue_coin_record = [
|
|
$change_zue_coin_record = [
|
|
|
'zue_coin_id' => $zue_coin_model->id,
|
|
'zue_coin_id' => $zue_coin_model->id,
|
|
|
|
|
+ 'order_id' => $order->id,
|
|
|
'origin' => $zue_coin_model->zue_coin,
|
|
'origin' => $zue_coin_model->zue_coin,
|
|
|
- 'change' => $zue_coin,
|
|
|
|
|
|
|
+ 'change' => -$zue_coin,
|
|
|
'after' => $zue_coin_model->zue_coin - $zue_coin,
|
|
'after' => $zue_coin_model->zue_coin - $zue_coin,
|
|
|
'reason' => 1,
|
|
'reason' => 1,
|
|
|
'create_time' => time(),
|
|
'create_time' => time(),
|
|
@@ -451,12 +456,14 @@ class OrderService extends \app\BaseService
|
|
|
$now = $order_product['real_price'] + $order_product['annuity'] + $order_product['sales_tax'];
|
|
$now = $order_product['real_price'] + $order_product['annuity'] + $order_product['sales_tax'];
|
|
|
$item_service_charge = fixed2Float($now * $item_service_charge_rate);
|
|
$item_service_charge = fixed2Float($now * $item_service_charge_rate);
|
|
|
$item_zue_coin = fixed2Float($now * $item_zue_coin_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, [
|
|
array_push($update_order_products, [
|
|
|
'id' => $order_product['id'],
|
|
'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
|
|
'is_pay' => 1
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
@@ -689,6 +696,14 @@ class OrderService extends \app\BaseService
|
|
|
return [$res->items(),$res->total()];
|
|
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 $admin_id
|
|
|
* @param $order_id
|
|
* @param $order_id
|
|
@@ -702,21 +717,130 @@ class OrderService extends \app\BaseService
|
|
|
// $verify = AuthService::verify($admin_id, 10005);
|
|
// $verify = AuthService::verify($admin_id, 10005);
|
|
|
$order = $this->orderModel->findById($order_id);
|
|
$order = $this->orderModel->findById($order_id);
|
|
|
if (!$order) return $this->fail(lang("Order does not exist"));
|
|
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();
|
|
$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"));
|
|
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();
|
|
$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();
|
|
Db::startTrans();
|
|
|
try {
|
|
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]);
|
|
Db::table('erp_order')->where('id', $order->id)->update(['is_delete' => 1]);
|
|
|
} else {
|
|
} 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();
|
|
Db::commit();
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
Db::rollback();
|
|
Db::rollback();
|
|
@@ -725,13 +849,6 @@ class OrderService extends \app\BaseService
|
|
|
return $this->ok(true);
|
|
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) {
|
|
public function exchange($admin_id, $order_id, $origin_order_product_ids, $append_product_ids) {
|
|
|
return $this->ok(true);
|
|
return $this->ok(true);
|
|
|
}
|
|
}
|