| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174 |
- <?php
- namespace app\api\service;
- use app\common\model\ActivityProductModel;
- use app\common\model\AdminModel;
- use app\common\model\ConfigModel;
- use app\common\model\CreditCardConfigModel;
- use app\common\model\CustomerModel;
- use app\common\model\CustomerZueCoinModel;
- use app\common\model\OrderAnnualFeeModel;
- use app\common\model\OrderModel;
- use app\common\model\OrderPaymentModel;
- use app\common\model\OrderProceedsModel;
- use app\common\model\OrderProductModel;
- 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
- {
- private $storeModel;
- private $orderModel;
- private $productModel;
- private $orderAnnualFeeModel;
- private $orderPaymentModel;
- private $orderProductModel;
- private $orderProceedsModel;
- private $activityProductModel;
- private $storeProductModel;
- private $configModel;
- private $adminModel;
- private $customerModel;
- private $paymentChannelModel;
- private $creditCardModel;
- private $zueCoinModel;
- private $zueCoinRecordModel;
- public function __construct()
- {
- $this->storeModel = new StoreModel();
- $this->orderModel = new OrderModel();
- $this->productModel = new ProductModel();
- $this->configModel = new ConfigModel();
- $this->storeProductModel = new StoreProductModel();
- $this->orderAnnualFeeModel = new OrderAnnualFeeModel();
- $this->orderPaymentModel = new OrderPaymentModel();
- $this->orderProductModel = new OrderProductModel();
- $this->orderProceedsModel = new OrderProceedsModel();
- $this->activityProductModel = new ActivityProductModel();
- $this->adminModel = new AdminModel();
- $this->customerModel = new CustomerModel();
- $this->paymentChannelModel = new PaymentChannelModel();
- $this->creditCardModel = new CreditCardConfigModel();
- $this->zueCoinModel = new CustomerZueCoinModel();
- $this->zueCoinRecordModel = new ZueCoinRecordModel();
- }
- /**
- * @param $params
- * @param array $store_products_params
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function create($params, array $store_products_params) {
- // 客户
- $customer = $this->customerModel->findById($params['customer_id']);
- if (!$customer)
- return $this->fail(lang("The Customer does not exist"));
- // 门店
- $store = $this->storeModel->findById($params['store_id']);
- if (!$store)
- return $this->fail(lang("The store does not exist"));
- // 顾问 or 老师 ids
- $user_ids = [];
- // 门店商品ids
- $store_product_ids = [];
- for ($i = 0; $i < count($store_products_params); $i++) {
- $store_products_param = $store_products_params[$i];
- $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_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);
- }
- // Adviser|Teacher
- $users = $this->adminModel->findByIds($user_ids)->toArray();
- if(count($users) != count($user_ids))
- return $this->fail(lang("The Adviser|Teacher does not exist"));
- $fmt_users = array_reduce($users, function ($result, $item) {
- $result[$item['id']] = $item;
- return $result;
- }, []);
- // 门店商品
- $store_products = $this->storeProductModel->fetchRelationsByOrder($store_product_ids)->toArray();
- if(count($store_products) != count($store_product_ids))
- return $this->fail(lang("The Product does not exist"));
- // 商品信息
- $products = array_map(function ($p) {
- return $p['product'] ? $p['product'] : null;
- }, $store_products);
- 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();
- $fmt_store_products = [];
- foreach ($store_products as &$item) {
- $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
- return $aProduct['product_id'] == $item['product_id'];
- });
- $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;
- $fmt_store_products[$item['id']] = $item;
- }
- $order_products = [];
- //项目总额
- $rental_amount = 0;
- $product_amount = 0;
- // 总消费税
- $total_sales_tax = 0;
- $advisor_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_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, [
- 'order_id' => 0,
- 'order_no' => $order_no,
- 'customer_id' => $customer->id,
- 'product_id' => $store_product['product']['id'],
- 'product_category_id' => $store_product['product']['category_id'],
- 'is_serve' => $store_product['product']['is_serve'],
- 'store_product_id' => $store_product['id'],
- 'product_name' => $store_product['product']['name'],
- 'adviser_1_id' => isset($store_products_param->adviser_1_id) ? $store_products_param->adviser_1_id : null,
- '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_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,
- 'is_gather_annuity' => $store_product['product']['is_gather_annuity'],
- 'annuity' => $store_product['product']['annuity'],
- 'real_price' => $store_product['product']['real_price'],
- 'service_charge' => 0,
- 'reduce_price' => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
- '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' => fixed2Float($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']),
- 'zue_coin' => 0,
- 'create_time' => time(),
- 'update_time' => time(),
- ]);
- $product_amount += $store_product['product']['real_price'];
- $total_sales_tax += $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_zh}",
- 'obj_names' => join(',',array_unique($product_names)),
- 'rental_amount' => $rental_amount,
- 'receivable_amount' => $rental_amount,
- 'receive_amount' => 0,
- 'imposed_amount' => $rental_amount,
- '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(',', array_unique($advisor_ids)),
- 'create_time' => time(),
- 'update_time' => time()
- ]);
- foreach ($order_products as &$order_product) $order_product['order_id'] = $order->id;
- Db::startTrans();
- try {
- Db::table('erp_order_product')->insertAll($order_products);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- Db::table('erp_order')->delete($order->id);
- return $this->fail(lang($e->getMessage()));
- }
- return $this->ok($order);
- }
- /**
- * @param $params
- * @param $channels
- * @return \SResult
- * @throws \app\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function payment($params, $channels) {
- $order = $this->orderModel->findById($params);
- if(!$order)
- return $this->fail(lang("Order does not exist"));
- 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;
- 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;
- },[]);
- // 本次总计支付费用
- $total_fee = 0;
- // 本次信用卡支付手续费
- $service_charge = 0;
- // 本次御龙币
- $zue_coin = 0;
- $order_payments = [];
- $record_channel_names = [];
- 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'].']');
- $item = [
- 'order_id' => $order->id,
- 'channel_id' => $channel->channel_id,
- 'channel_name' => $fmt_pay_channel['name'],
- 'create_time' => time(),
- 'update_time' => time(),
- ];
- if ($fmt_pay_channel['type'] == 1) { // 正常支付
- predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
- $item['fee'] = $channel->fee;
- $total_fee += $channel->fee;
- if ($fmt_pay_channel['is_upload_code']) {
- predicate(isset($channel->code),'必须上传付款编号');
- $item['code'] = $channel->code;
- }
- array_push($order_payments, $item);
- } else if($fmt_pay_channel['type'] == 2) { // 御龙币支付
- predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
- $item['fee'] = $channel->fee;
- // TODO: 御龙币需求未明确
- array_push($order_payments, $item);
- $zue_coin += $channel->fee;
- } else {
- for ($j = 0; $j < count($channel->credit_card); $j++) {
- $credit_card = $channel->credit_card[$j];
- $fmt_credit_card_config = $fmt_credit_card_configs[$credit_card->credit_card_id];
- if (!$fmt_credit_card_config)
- return $this->fail("信用卡支付渠道错误!");
- predicate(isset($credit_card->fee) && $credit_card->fee > 0, '支付费用未填写');
- $total_fee += $credit_card->fee;
- $el = [
- 'order_id' => $order->id,
- 'channel_id' => $channel->channel_id,
- 'channel_name' => $fmt_pay_channel['name'],
- 'fee' => $credit_card->fee,
- 'credit_card_id' => $credit_card->credit_card_id,
- 'credit_card_name' => $fmt_credit_card_config['bank'],
- 'create_time' => time(),
- 'update_time' => time(),
- ];
- if ($fmt_pay_channel['is_upload_code']) {
- predicate(isset($credit_card->code),'必须填写付款编号');
- $el['code'] = $credit_card->code;
- }
- if ($fmt_credit_card_config['is_stage'] == 1) {
- predicate(isset($credit_card->stage_num),'必须选择分期数');
- $el['stage_num'] = $credit_card->stage_num;
- $config = [
- '6' => $fmt_credit_card_config['stage_6'],
- '9' => $fmt_credit_card_config['stage_9'],
- '12' => $fmt_credit_card_config['stage_12'],
- '24' => $fmt_credit_card_config['stage_24'],
- '36' => $fmt_credit_card_config['stage_36'],
- ];
- $now_config = $config[$credit_card->stage_num];
- predicate($now_config[0] == 1,'该分期未启用!');
- if ($now_config[1] > 0) { // 收取手续费
- $el['service_charge_rate'] = $now_config[1];
- $now_service_charge = $el['fee'] * ($now_config[1] / 100);
- $el['service_charge'] = $now_service_charge;
- $service_charge += $now_service_charge;
- }
- }
- array_push($order_payments, $el);
- }
- }
- }
- // 信用卡手续费
- $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);
- $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);
- 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'];
- if(!$zue_coin_config)
- return $this->fail("御龙币设置不存在!");
- $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
- $zue_coin_consume_rate = $zue_coin_config['zue_coin_consume_rate'] ?? 0; // 报销比例
- if($zue_coin > 0) {
- if($zue_coin_exchange_rate == 0 || $zue_coin_consume_rate == 0) {
- return $this->fail("该笔订单不支持御龙币支付");
- }
- // 1.计算出该订单能使用多少御龙币
- // 2.结合之前已使用的御龙币 算出还能使用多少御龙币
- // 3.判断当前御龙币是否符合兑换数额
- // 4.进行扣除并记录
- // 根据御龙币的设置 总共能报销这么多钱
- $max_consume_amount = fixed2Float($imposed_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;
- // 当前最多还能使用多少御龙币
- $now_can_consume_zue_coin = fixed2Float($now_max_consume_amount / $zue_coin_exchange_rate);
- // 判断当前还能使用
- if ($now_can_consume_zue_coin - $zue_coin < 0)
- return $this->fail("当前剩余可使用的: {$now_can_consume_zue_coin}");
- // 总共使用了这么多御龙币
- $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,
- 'order_id' => $order->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;
- // 总计实收金额
- $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})");
- $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();
- $fmt_store_products = array_reduce($store_products, function ($result, $item) {
- $result[$item['id']] = $item;
- return $result;
- },[]);
- // 商品本身的价格 | 年费 | 消费税
- $total_ = $order->product_amount + $order->total_annual_fee + $order->total_sales_tax;
- // 信用卡手续费 每一块钱所占的比例
- $item_service_charge_rate = ($order->service_charge_amount + $service_charge) / $total_;
- // 每一个御龙币所占的比例
- $item_zue_coin_rate = $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;
- if (isset($update_store_products[$order_product['store_product_id']])) {
- $update_store_products[$order_product['store_product_id']]['reduce_num'] += 1;
- } else {
- $update_store_products[$order_product['store_product_id']] = ['id' => $order_product['store_product_id'], 'reduce_num' => 1];
- }
- if ($order_product['is_gather_annuity']) {
- array_push($order_annual_fees, [
- 'order_id' => $order->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' => 1,
- 'start_time' => time(),
- 'end_time' => time() + (365 * 24 * 60 * 60),
- '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'];
- $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);
- 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,
- 'is_pay' => 1
- ]);
- }
- }
- Db::startTrans();
- try {
- $update_order = [
- 'rental_amount' => $imposed_amount + $order_use_zue_coin_amount,
- 'receivable_amount' => $imposed_amount,
- 'receive_amount' => $receive_amount,
- 'imposed_amount' => $imposed_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']
- ];
- if (round($receive_amount) == round($imposed_amount)) { // 支付满了
- $update_order['type'] = 1;
- if(count($update_order_products) > 0) {
- foreach ($update_order_products as $item) {
- Db::table('erp_order_product')->where('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)->update();
- Db::table('erp_store_product')->where('id', $update['id'])->inc('sale_stock',1)->update();
- }
- }
- }
- Db::table('erp_order_proceeds')->save([
- 'order_id' => $order->id,
- 'channels' => join(' ',$record_channel_names),
- 'fee' => $total_fee,
- 'admin_id' => $params['admin_id'],
- 'admin_name' => $params['admin_name'],
- '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 [
- "order_id" => isset($data['order_id']) ? $data['order_id'] : null,
- "channel_id" => isset($data['channel_id']) ? $data['channel_id'] : null,
- "channel_name" => isset($data['channel_name']) ? $data['channel_name']: null,
- "fee" => isset($data['fee']) ? $data['fee'] : null,
- "credit_card_id" => isset($data['credit_card_id']) ? $data['credit_card_id'] : null,
- "credit_card_name" => isset($data['credit_card_name']) ? $data['credit_card_name'] : null,
- "create_time" => isset($data['create_time']) ? $data['create_time'] : null,
- "update_time" => isset($data['update_time']) ? $data['update_time'] : null,
- "code" => isset($data['code']) ? $data['code'] : null,
- "stage_num" => isset($data['stage_num']) ? $data['stage_num'] : null,
- "service_charge_rate" => isset($data['service_charge_rate']) ? $data['service_charge_rate'] : null,
- "service_charge" => isset($data['service_charge']) ? $data['service_charge'] : null
- ];
- }, $order_payments));
- }
- Db::table('erp_order')->where('id', $order->id)->update($update_order);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return $this->fail(lang($e->getMessage()));
- }
- 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->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);
- }
- 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);
- 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
- * @param array $order_product_ids
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function returnOrderProduct($admin_id, $order_id, array $order_product_ids) {
- // $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;
- },[]);
- $zue_coin_config = $this->paymentChannelModel->findById(11);
- if(!$zue_coin_config)
- return $this->fail("御龙币设置不存在!");
- $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
- //商品本金
- $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 = $order->zue_coin_amount > 0 && $order->zue_coin > 0 ? fixed2Float($order->zue_coin_amount / $order->zue_coin) : $zue_coin_exchange_rate;
- // 总计退还多少钱
- $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 {
- // 退还御龙币
- $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 {
- 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();
- return $this->fail(lang($e->getMessage()));
- }
- return $this->ok(true);
- }
- /**
- * @param $admin_id
- * @param $admin_name
- * @param $order_id
- * @param $origin_order_product_ids
- * @param $append_store_product_ids
- * @return \SResult
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function exchange($admin_id, $admin_name, $order_id, $origin_order_product_ids, $append_store_product_ids) {
- // $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($origin_order_product_ids)->toArray();
- if (count($origin_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($origin_order_product_ids) {
- if(!in_array($item['id'], $origin_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;
- },[]);
- $zue_coin_config = $this->paymentChannelModel->findById(11);
- if(!$zue_coin_config)
- return $this->fail("御龙币设置不存在!");
- $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
- //商品本金
- $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 = $order->zue_coin_amount > 0 && $order->zue_coin > 0 ? fixed2Float($order->zue_coin_amount / $order->zue_coin) : $zue_coin_exchange_rate;
- // 差价
- $diff_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'];
- $diff_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;
- }
- }
- /////////////// 加操作
- // 客户
- $customer = $this->customerModel->findById($order->customer_id);
- if (!$customer)
- return $this->fail(lang("The Customer does not exist"));
- // 门店商品
- $store_products = $this->storeProductModel->fetchRelationsByOrder($append_store_product_ids)->toArray();
- if(count($store_products) != count($append_store_product_ids))
- return $this->fail(lang("The Product does not exist"));
- // 商品信息
- $products = array_map(function ($p) {
- return $p['product'] ? $p['product'] : null;
- }, $store_products);
- 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();
- $fmt_store_products = [];
- foreach ($store_products as &$item) {
- $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
- return $aProduct['product_id'] == $item['product_id'];
- });
- $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;
- $fmt_store_products[$item['id']] = $item;
- }
- $order_products = [];
- $order_annual_fees = [];
- for ($j = 0; $j < count($store_products); $j++) {
- $store_product = $store_products[$j];
- array_push($obj_names, $store_product['product']['name']);
- array_push($advisor_ids, $admin_id);
- array_push($order_products, [
- 'order_id' => $order->id,
- 'order_no' => $order->no,
- 'customer_id' => $order->customer_id,
- 'product_id' => $store_product['product']['id'],
- 'product_category_id' => $store_product['product']['category_id'],
- 'is_serve' => $store_product['product']['is_serve'],
- 'store_product_id' => $store_product['id'],
- 'product_name' => $store_product['product']['name'],
- 'adviser_1_id' => $admin_id,
- 'adviser_1_name' => $admin_name,
- 'adviser_2_id' => null,
- 'adviser_2_name' => null,
- 'teacher_1_id' => null,
- 'teacher_1_name' => null,
- 'teacher_2_id' => null,
- 'teacher_2_name' => null,
- 'is_upload_numerology' => $store_product['product']['is_upload_numerology'],
- 'is_upload' => 0,
- 'report' => null,
- 'is_gather_annuity' => $store_product['product']['is_gather_annuity'],
- 'annuity' => $store_product['product']['annuity'],
- 'real_price' => $store_product['product']['real_price'],
- 'service_charge' => 0,
- 'reduce_price' => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
- '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' => fixed2Float($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']),
- 'zue_coin' => 0,
- 'is_pay' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- ]);
- array_push($order_annual_fees, $store_product['product']['is_gather_annuity'] ? [
- 'order_id' => $order->id,
- 'customer_id' => $order->customer_id,
- 'customer_name' => $order->customer_name,
- 'customer_mobile' => $customer->mobile,
- 'customer_create_username' => $customer->create_username,
- 'index' => 1,
- 'order_product_id' => -1,
- 'order_product_name' => $store_product['product']['name'],
- 'fee' => $store_product['product']['name'],
- 'is_pay' => 1,
- 'start_time' => time(),
- 'end_time' => time() + (365 * 24 * 60 * 60),
- 'order_create_time' => strtotime($order->create_time),
- 'adviser_1_id' => $store_product['product']['annuity'],
- 'store_id' => $order->store_id,
- 'create_time' => time(),
- 'update_time' => time()
- ] : null);
- $total_product_amount += $store_product['product']['real_price'];
- $total_sales_tax += $store_product['product']['sales_tax'];
- if($store_product['product']['is_gather_annuity'] == 1)
- $total_annual_fee += $store_product['product']['annuity'];
- $diff_amount += fixed2Float($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']);
- }
- $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($origin_order_product_ids);
- Db::startTrans();
- try {
- // 退还御龙币
- $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()]);
- // 插入退款记录
- Db::table('erp_order_payment')->save([
- 'order_id' => $order->id,
- 'channel_id' => 0,
- 'channel_name' => "Exchange a purchase",
- 'fee' => $diff_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', $origin_order_product_ids)->update(['is_delete' => 1, 'delete_time' => time()]);
- // 新增商品
- for ($i = 0; $i < count($order_products); $i++) {
- $order_product = $order_products[$i];
- $order_annual_fee = $order_annual_fees[$i];
- $last_id = Db::table('erp_order_product')->insert($order_product, true);
- if($order_annual_fee != null) {
- $order_annual_fee['order_product_id'] = $last_id;
- Db::table('erp_order_annual_fee')->save($order_annual_fee);
- }
- Db::table("erp_store_product")
- ->where('id', $order_product['store_product_id'])
- ->dec('now_stock', 1)
- ->inc('sale_stock', 1)
- ->update();
- }
- // 更新订单
- 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::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return $this->fail(lang($e->getMessage()));
- }
- return $this->ok(true);
- }
- }
|