| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\order;
- use app\models\AccountLog;
- use app\models\Address;
- use app\models\Cart;
- use app\models\CashierActionLog;
- use app\models\common\CommonOrder;
- use app\models\FoodCart;
- use app\models\FoodFlag;
- use app\models\Goods;
- use app\models\HangingOrder;
- use app\models\HangingOrderDetail;
- use app\models\Md;
- use app\models\MdProfit;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\Store;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\VerifyCardSale;
- use app\models\VideoGoodsList;
- use app\models\VideoGoodsSetting;
- use app\models\VideoGoodsShare;
- use app\modules\client\models\OrderComplete;
- use app\modules\client\models\v1\order\OrderForm;
- use app\modules\common\models\NotifyForm;
- use app\plugins\adopt\models\AdoptGoods;
- use app\plugins\adopt\models\AdoptOrderInfo;
- use app\utils\CloudPrint;
- use app\utils\Delivery\Delivery;
- use app\utils\Notice\NoticeSend;
- use app\utils\OrderNo;
- use app\utils\PrintOrder;
- use app\utils\Tools;
- use yii\helpers\Json;
- class MchOrderSubmitForm extends MchOrderForm
- {
- public $user;
- public $payment;
- public $use_integral;
- public $actual_price;// 管理员实收金额
- public $sale_time;// 销售日期
- public $ready_money = ['money'=>0,'give_change'=>0];//现金收入相关
- public $remark = '';
- public $hanging_order_id = 0;
- public $address_id;
- public $is_combine = 0;//是否使用余额
- public $md_id;
- public $is_offline = 0;
- public function rules()
- {
- $rules = [
- [['payment', 'use_integral','hanging_order_id','is_combine', 'address_id', 'md_id', 'is_offline'], 'integer'],
- [['actual_price'],'number'],
- [['sale_time','ready_money','remark'],'string']
- ];
- return array_merge(parent::rules(), $rules);
- }
- public function save()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- try {
- $mchListData = $this->getMchListData(true);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '下单失败'
- ];
- }
- $order_id_list = [];
- $level = $this->level;
- $md_select = 0;
- /**
- * @var $address Address
- */
- //$address = (object)($this->address);
- $t = \Yii::$app->db->beginTransaction();
- foreach ($mchListData as &$mch) {
- if (isset($mch['use_integral']) && !$mch['use_integral']) {
- $mch['integral'] = ['forehead' => 0, 'forehead_integral' => 0];
- $mch['give'] = 0;
- }
- $mch_goods = $mch['goods_list'];
- if ($mch_goods) {
- $checkMchData = $this->checkMchData($mch);
- if ($checkMchData['code'] == 1) {
- $t->rollBack();
- return $checkMchData;
- }
- $level_price = 0;
- $total_price = 0;
- $is_have_express = false;
- $is_have_shop = $mch['is_shop'] == 1 ? 1 : 0;
- $order_type = 0;
- $is_have_delivery = 0;
- $total_integral = 0;
- foreach ($mch_goods as $v) {
- $order_type = 0;
- $level_price += $v['level_price'];
- $total_price += isset($v['total_price']) ? $v['total_price'] : $v['price'];
- }
- $mch['level_price'] = $level_price;
- $mch['total_price'] = $total_price;
- $mch['total_integral'] = $total_integral; //积分兑换
- // 开启使用积分抵扣
- if (isset($mch['use_integral']) && $mch['use_integral'] == 1) {
- if ($this->user->integral < $mch['integral']['forehead']) {
- return [
- 'code' => 1,
- 'msg' => '积分可抵扣支付数量不足'
- ];
- }
- }
- $payPrice = $this->getPayPrice($mch);
- $order = new Order();
- // 订单来源
- $order->order_origin = Order::ORDER_SOURCE_CASHIER;// 收银台订单
- $order->store_id = $this->store_id;
- $order->user_id = $this->user_id ?? 0;
- $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
- // 实际支付金额 按前台传过来的值
- $order->pay_price = $this->actual_price;
- $order->hanging_order_id = $this->hanging_order_id;
- if (isset($mch['picker_coupon']) && !empty($mch['picker_coupon'])) {
- $order->user_coupon_id = $mch['picker_coupon']['user_coupon_id'];
- $order->coupon_sub_price = $mch['picker_coupon']['sub_price'];
- // 查找优惠券优惠的商品
- $this->pickerGoods($mch);
- }
- // 订单类型
- $order->order_type = $order_type;
- // todo 积分兑换商品
- /*if (isset($mch['total_integral']) && $mch['total_integral'] > 0 ) {
- if (get_user()->integral < $mch['total_integral']) {
- //获取商城积分抵扣规则 x积分 = 1元
- $integral_rule = Option::find()->where(['store_id'=>get_store_id(),'name'=>'integral'])->select('value')->asArray()->one();
- $difference_price = sprintf("%.2f", ($mch['total_integral'] - get_user()->integral ) / $integral_rule['value'] );
- $order->pay_price += $difference_price;
- $order->total_price += $difference_price;
- $order->integral_price = get_user()->integral;
- $order->integral_difference_price = $difference_price;
- $mch['integral_type'] = 2; //积分不足
- $mch['integral_difference_price'] = $difference_price; //补的差额
- }else{
- $order->integral_price = $mch['total_integral'];
- $order->integral_difference_price = 0;
- $mch['integral_type'] = 1; //全部使用积分
- $mch['integral_difference_price'] = 0; //补的差额
- }
- //当用户有积分时
- if(get_user()->integral > 0){
- $upIntegral = get_user()->integral - $order->integral_price;
- //减用户积分
- if(! User::updateAll(['integral'=>$upIntegral],['id'=> get_user_id()]) ){
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '积分抵扣失败,稍后重试!'
- ];
- }
- }
- }*/
- // 是否是平台小程序订单
- $order->is_platform = 0;
- $order->md_id = $this->md_id;
- $order->created_at = time();
- $order->first_price = 0;
- $order->second_price = 0;
- $order->third_price = 0;
- $order->content = isset($mch['content']) ? $mch['content'] : '';
- $order->is_offline = $this->is_offline == 1 ? 1 : 0;
- $order->is_delivery = $is_have_delivery ? 1 : 0;
- if (isset($mch['use_integral']) && $mch['use_integral'] == 1) {
- $order->integral = json_encode($mch['integral'], JSON_UNESCAPED_UNICODE);
- } else {
- $order->integral = json_encode(['forehead' => 0, 'forehead_integral' => 0], JSON_UNESCAPED_UNICODE);
- }
- $order->version = cyy_version();
- $order->mch_id = $mch['mch_id'];
- $order->discount = isset($level['discount']) ? $level['discount'] : 10;
- $order->remark = $this->remark ?? '这是一个收银台过来的订单';
- // 微信支付
- if ($this->payment == 1) {
- $order->pay_type = 1;
- $order->is_pay = 0;
- }
- // 支付宝支付
- if ($this->payment == 4) {
- $order->pay_type = 4;
- $order->is_pay = 0;
- }
- // 线下支付
- if ($this->payment == 6) {
- $order->pay_type = 6;
- $order->is_pay = 0;
- }
- //货到付款
- if ($this->payment == 2) {
- $order->pay_type = 2;
- $order->is_pay = 0;
- if (!$order->user_id) {
- return [
- 'code' => 1,
- 'msg' => '下单失败,货到付款需选择用户'
- ];
- }
- }
- if ($this->address_id) {
- $address = Address::findOne([
- 'id' => $this->address_id
- ]);
- } else {
- $address = Address::findOne([
- 'is_default' => 1,
- 'user_id' => $this->saas_user->id,
- 'is_delete' => 0,
- ]);
- }
- if (!empty($address)) {
- $order->address = $address->province . $address->city . $address->district . $address->detail;
- $order->mobile = $address->mobile;
- $order->name = $address->name;
- $order->province_id = $address->province_id;
- $order->city_id = $address->city_id;
- $order->district_id = $address->district_id;
- $address_arr = json_encode([
- 'province' => trim($address->province),
- 'city' => trim($address->city),
- 'district' => trim($address->district),
- 'detail' => trim($address->detail),
- 'latitude' => $address->latitude,
- 'longitude' => $address->longitude,
- ], JSON_UNESCAPED_UNICODE);
- $order->address_data = $address_arr;
- }
- // 余额抵扣支付
- if ($this->is_combine) {
- if ($this->user->money >= $order->pay_price) {
- $order->pay_type = 3;
- $order->is_pay = 0;
- }
- }
- $order->total_price = $mch['total_price'];
- //获取代理配置信息
- $store = Store::findOne($this->store_id);
- $admin_profit = Option::get('admin_profit', 0, 'saas', 0)['value'];
- if (!empty($admin_profit) && !empty($store->transfer_profit)) {
- $order->ag_rebate = ($payPrice * $store->transfer_profit / 100) * ($admin_profit / 100);
- }
- // 配送订单预存数据
- $order->saas_id = 0;
- $order->integral_difference_price = 0;
- $order->sale_time = strtotime($this->sale_time);
- if(is_array($this->ready_money)){
- $order->ready_money = json_encode($this->ready_money);
- }else{
- $order->ready_money = $this->ready_money;
- }
- if (!$order->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($order->firstErrors))
- ];
- } else {
- // 绑定上下级
- $user = User::findOne($this->user_id);
- if ($user && $user->parent_user_id) {
- CommonOrder::saveParentId($user->parent_user_id);
- }
- // 处理订单生成之后其他相关数据
- $orderRes = $this->insertData($mch, $order);
- $this->reduceIntegral($order);
- if ($orderRes['code'] == 1) {
- $t->rollBack();
- return $orderRes;
- }
- if($order->hanging_order_id > 0){
- HangingOrder::updateAll(['is_delete'=>1],['id'=>$order->hanging_order_id]);
- HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$order->hanging_order_id]);
- }
- $printer_order = new PrintOrder($order->store_id, $order->id, 'order', 0, 0, 0, $order['mch_id']);
- $printer_order->print_order();
- /*// 订单提交完成发送消息
- NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name']);*/
- $order_id_list[] = $order->id;
- }
- }
- }
- if (count($order_id_list) > 0) {
- $t->commit();
- // 生成操作记录
- CashierActionLog::setLog($this->store_id,get_user_id(),CashierActionLog::CREATE_ORDER,'提交订单', $this->md_id);
- if (count($order_id_list) > 1) {//多个订单合并
- return [
- 'code' => 0,
- 'msg' => '订单提交成功',
- 'data' => (object)[
- 'order_id_list' => $order_id_list,
- 'add_time' => $order->created_at
- ],
- ];
- } else {//单个订单
- $order_id = $order_id_list[0];
- CloudPrint::doPrint($order_id, 0, $this->store_id);
- return [
- 'code' => 0,
- 'msg' => '订单提交成功',
- 'data' => (object)[
- 'order_id' => $order_id_list[0],
- 'add_time' => $order->created_at,
- 'order_price'=> $order->pay_price,
- 'pay_type' => $order->pay_type
- ],
- ];
- }
- } else {
- $t->rollBack();
- return [
- 'order_list' => $order_id_list,
- 'order_price'=> $order->pay_price
- ];
- }
- }
- // 使用积分抵扣,订单生成后扣除用户积分
- private function reduceIntegral($order) {
- $integral = Json::decode($order->integral);
- // 减去当前用户账户积分
- if ($integral['forehead_integral'] > 0) {
- $user = User::findOne($order->user_id);
- $before = $user->integral;
- $user->integral -= $integral['forehead_integral'];
- if ($user->save()) {
- $log = new AccountLog();
- $log->store_id = $order->store_id;
- $log->user_id = $user->id;
- $log->type = AccountLog::TYPE_INTEGRAL;
- $log->log_type = AccountLog::LOG_TYPE_EXPEND;
- $log->amount = $integral['forehead_integral'];
- $log->desc = "订单积分抵扣支付,订单号为:{$order->order_no}。";
- $log->before = $before;
- $log->after = $user->integral;
- $log->operator = '';
- $log->operator_id = 0;
- $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
- $log->created_at = time();
- $log->order_id = $order->id;
- $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
- $log->save();
- }
- }
- }
- // 检查数据
- private function checkMchData($mch)
- {
- if (empty($mch['goods_list'])) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在或已删除'
- ];
- }
- $checkCoupon = $this->checkCoupon($mch);
- if ($checkCoupon['code'] == 1) {
- return $checkCoupon;
- }
- $checkGoods = $this->checkGoods($mch);
- if ($checkGoods['code'] == 1) {
- return $checkGoods;
- }
- return ['code' => 0, 'msg' => 'success'];
- }
- // 优惠券可优惠的商品总额计算
- private function pickerGoods(&$mch)
- {
- $totalPrice = 0;
- $pickerCoupon = $mch['picker_coupon'];
- if (empty($pickerCoupon)) {
- return;
- }
- foreach ($mch['goods_list'] as $item) {
- if ($pickerCoupon['appoint_type'] == 1) {
- if ($pickerCoupon['cat_id_list'] !== null) {
- $catIdList = $pickerCoupon['cat_id_list'];
- if (array_intersect($item['cat_id'], $catIdList)) {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else if ($pickerCoupon['appoint_type'] == 2) {
- if ($pickerCoupon['goods_id_list'] !== null) {
- $goodsIdList = $pickerCoupon['goods_id_list'];
- if (in_array($item['goods_id'], $goodsIdList)) {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- }
- $mch['picker_coupon']['total_price'] = $totalPrice;
- }
- // 检测优惠券是否可使用
- private function checkCoupon($mch)
- {
- if (empty($mch['picker_coupon'])) {
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- $ok = false;
- foreach ($mch['coupon_list'] as $item) {
- if ($item['user_coupon_id'] == $mch['picker_coupon']['user_coupon_id']) {
- $ok = true;
- }
- }
- if (!$ok) {
- return [
- 'code' => 1,
- 'msg' => '该优惠券已过期'
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- }
- // 检测商品相关
- private function checkGoods($mch)
- {
- \Yii::warning($mch);
- foreach ($mch['goods_list'] as $goods) {
- $attr_id_list = [];
- foreach ($goods['attr_list'] as $item) {
- array_push($attr_id_list, $item['attr_id']);
- }
- $_goods = Goods::find()->where('id = ' . $goods['goods_id'] . ' for update')->one();
- //2砍价商品6当面付
- if ($_goods->type != 2 && $_goods->type != 6 && $_goods->product_type == Goods::GOODS_TYPE_NORMAL || $_goods->product_type == Goods::GOODS_TYPE_ADOPT) {
- if (!$_goods->numSub($attr_id_list, $goods['num'], $this->md_id)) {
- return [
- 'code' => 1,
- 'msg' => '订单提交失败,商品“' . $_goods->name . '”库存不足',
- 'attr_id_list' => $attr_id_list,
- 'attr_list' => $goods['attr_list'],
- ];
- }
- }
- if($_goods->product_type == Goods::GOODS_TYPE_DATE){
- $num = Goods::getGoodsNum($_goods)['data'];
- $attr = $goods['book'];
- foreach ($attr as $iattr) {
- $idate = $iattr['date'];
- foreach ($num as $inum) {
- $indate = $inum['date'];
- if(($idate == $indate) && ($inum['num'] < $goods['num'])){
- return ['code' => 1, 'msg' => $idate.'库存不足'];
- }
- }
- }
- }
- }
- return ['code' => 0, 'msg' => ''];
- }
- // 获得实际支付金额
- private function getPayPrice($mch)
- {
- $goods_list = $mch['goods_list'];
- $payPrice = $mch['level_price'];
- if (isset($mch['use_integral']) && $mch['use_integral']) {
- $payPrice -= $mch['integral']['forehead'];
- }
- if (isset($mch['picker_coupon'])) {
- $pickerCoupon = $mch['picker_coupon'];
- if ($pickerCoupon['sub_price'] > 0) {
- $coupon_price = 0;
- if ($pickerCoupon['appoint_type'] == 1 && $pickerCoupon['cat_id_list'] != null) {
- foreach ($goods_list as $goods) {
- foreach ($goods['cat_id'] as $v1) {
- if (in_array($v1, $pickerCoupon['cat_id_list'])) {
- $coupon_price += floatval($goods['level_price']);
- break;
- };
- };
- }
- } else if ($pickerCoupon['appoint_type'] == 2 && $pickerCoupon['goods_id_list'] != null) {
- foreach ($goods_list as $goods) {
- if (in_array($goods['goods_id'], $pickerCoupon['goods_id_list'])) {
- $coupon_price += floatval($goods['level_price']);
- };
- }
- };
- if ($pickerCoupon['sub_price'] > $coupon_price && $coupon_price > 0) {
- $payPrice -= $coupon_price;
- } else {
- $payPrice -= $pickerCoupon['sub_price'];
- }
- }
- }
- // Todo 此处有待确认算法是否正确 by Syan
- // $payPrice = $payPrice > 0 ? $payPrice : 0.01;
- /*if ($mch['express_price'] > 0) {
- if (isset($mch['send']) && $mch['send'] == 'express') {
- $payPrice += $mch['express_price'];
- } else {
- foreach ($goods_list as $goods) {
- if (isset($goods['send']) && $goods['send'] == 'express') {
- $payPrice += $mch['express_price'];
- break;
- }
- }
- }
- }*/
- return $payPrice > 0 ? $payPrice : 0;
- }
- /**
- * @param $mch
- * @param $order Order
- * @return array
- */
- private function insertData($mch, $order)
- {
- // 计算商品相关
- $goods_list = $mch['goods_list'];
- $goodsPrice = 0;
- // 计算商品分润
- $goods_profit = 0;
- $chain_goods_profit = 0;
- foreach ($goods_list as $goods) {
- if ($goods['product_type'] == Goods::GOODS_TYPE_ADOPT) {
- $adopt_order_info = new AdoptOrderInfo();
- $adopt_order_info->name = $this->adopt_name;
- $adopt_order_info->mobile = $this->adopt_mobile;
- $adopt_order_info->order_id = $order->id;
- $adopt_order_info->store_id = $this->store_id;
- $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods['id']])->one();
- $adopt_order_info->mature_time = time() + $adopt_goods->growth_cycle * 86400;
- if (!$adopt_order_info->save()) {
- return [
- 'code' => 1,
- 'msg' => '订单提交失败,请稍后再重试',
- 'error' => $adopt_order_info->errors
- ];
- }
- }
- // 存入下单表单
- if ($goods['form'] && $goods['form']['is_form'] == 1) {
- foreach ($goods['form']['list'] as $index => $value) {
- $order_form = new \app\models\OrderForm();
- $order_form->store_id = $this->store_id;
- $order_form->order_id = $order->id;
- $order_form->goods_id = $goods['id'];
- $order_form->key = $value['name'];
- $order_form->value = $value['default'];
- $order_form->type = $value['type'];
- $order_form->is_delete = 0;
- $order_form->save();
- }
- }
- // 删除购物车
- if (isset($goods['cart_id'])) {
- Cart::updateAll(['is_delete' => 1], ['id' => $goods['cart_id']]);
- }
- $order_detail = new OrderDetail();
- $order_detail->order_id = $order->id;
- $order_detail->goods_id = $goods['goods_id'];
- $order_detail->num = $goods['num'];
- // if ($goods['is_level'] && $goods['is_level'] == 1) {
- // $order_detail->is_level = (int)$goods['is_level'];
- // } else {
- $order_detail->is_level = 1;
- // }
- $payPrice = $goods['level_price'];
- if (isset($mch['picker_coupon']) && $mch['picker_coupon'] && !empty($mch['picker_coupon'])) {
- if (!empty($mch['picker_coupon']['goods_id_list']) && in_array($goods['goods_id'], \json_decode($mch['picker_coupon']['goods_id_list'], true)) && $mch['picker_coupon']['total_price'] > 0) {
- $pickerCouponSubPrice = doubleval($mch['picker_coupon']['sub_price'] * doubleval($goods['price']) / $mch['picker_coupon']['total_price']);
- $payPrice -= $pickerCouponSubPrice;
- }
- // 删除优惠券
- UserCoupon::updateAll(['is_use' => 1], ['id' => $mch['picker_coupon']['user_coupon_id']]);
- }
- if (isset($mch['use_integral']) && $mch['use_integral']) {
- if ($goods['resIntegral'] && $goods['resIntegral']['forehead'] > 0) {
- $payPrice -= $goods['resIntegral']['forehead'];
- }
- }
- $payPrice = $payPrice >= 0 ? sprintf('%.2f', $payPrice) : 0;
- $goodsPrice += $payPrice;
- $orderPrice = floatval($order->pay_price) - floatval($order->express_price);
- if ($goodsPrice > $orderPrice) {
- $payPrice = $payPrice - ($goodsPrice - $orderPrice);
- $goodsPrice = $orderPrice;
- }
- //$payPrice = $this->getBookPrice($payPrice, $goods_list);
- // $order_detail->supplier_price = $goods['supplier_price'];
- $order_detail->total_price = $payPrice;
- // 计算单个商品可分红金额
- $profit = $goods['rate_type'] == 0 ? floatval($payPrice * $goods['rate'] / 100) : $goods['rate'];
- $goods_profit += $profit;
- $order_detail->profit = $profit;
- // 链动
- $chain_profit = $goods['chain_rate_type'] == 0 ? floatval($payPrice * $goods['chain_rate'] / 100) : $goods['chain_rate'];
- $chain_goods_profit += $chain_profit;
- $order_detail->chain_profit = $chain_profit;
- $order_detail->is_delete = 0;
- if ($goods['product_type'] == 1) {
- $order_detail->attr = json_encode($goods['book'], JSON_UNESCAPED_UNICODE);
- } elseif ($goods['product_type'] == 2) {
- //$order_detail->attr = json_encode($goods['service'], JSON_UNESCAPED_UNICODE);
- $service = $goods['service'];
- $time = explode('-', $service['time']);
- $order_detail->attr = json_encode([
- 'start_date' => $service['date'] . ' ' . $time[0],
- 'end_date' => $service['date'] . ' ' . $time[1],
- 'time' => $service['time'],
- 'date' => $service['date'],
- 'price' => $service['price'],
- ], JSON_UNESCAPED_UNICODE);
- } else {
- $order_detail->attr = json_encode($goods['attr_list'], JSON_UNESCAPED_UNICODE);
- }
- $goods_info = Goods::findOne($goods['goods_id']);
- $order_detail->pic = $goods['goods_pic'] ? $goods['goods_pic'] : $goods_info->cover_pic;
- $order_detail->goods_name = $goods_info ? $goods_info->name : $goods['goods_name'];
- $order_detail->goods_info = $goods_info ? Json::encode($goods_info->toArray()) : json::encode([]);
- $order_detail->delivery_type = $mch['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : ($mch['send'] == 'delivery' ? OrderDetail::GOODS_DELIVERY_IM : OrderDetail::GOODS_DELIVERY_SHOP);
- if ($mch['send'] == 'shop' && empty(input_params('flag_id'))) {
- if(isset($goods['shop_id']) && $goods['shop_id'] > 0){
- $order_detail->shop_id = $goods['shop_id'];
- }else{
- $order_detail->shop_id = 0;
- }
- }
- if ($goods['give'] > 0) {
- $order_detail->integral = $goods['give'];
- } else {
- $order_detail->integral = 0;
- }
- $attr_id_list = [];
- foreach ($goods['attr_list'] as $item) {
- array_push($attr_id_list, $item['attr_id']);
- }
- $order_detail->batch_price_tips = $goods['current_batch_price_tips'];
- // 存入点餐的user_id
- if (!empty($goods['user_id'])) {
- $order_detail->user_id = $goods['user_id'];
- }
- // 积分兑换商品 1次只能兑换1个
- if ($order->order_type == 7){
- $order_detail->total_price = $order->total_price;
- $order_detail->integral_price = $order->integral_price;
- $order_detail->integral_difference_price = $order->integral_difference_price;
- }
- if (!$order_detail->save()) {
- return [
- 'code' => 1,
- 'msg' => '订单提交失败,请稍后再重试',
- 'error' => $order_detail->errors[0]
- ];
- }
- if (!empty($goods['videoId'])) {
- //获取抖品信息
- $video_goods = VideoGoodsList::findOne($goods['videoId']);
- //获取抖品配置信息
- $setting = VideoGoodsSetting::findOne(['store_id' => get_store_id()]);
- //获取商品配置信息
- $goods_info = Goods::findOne($goods['goods_id']);
- $profit = ($setting->profit / 100);
- if ($profit > 0 && $video_goods && $goods_info) {
- $video_share = new VideoGoodsShare();
- $video_share->order_id = $order->id;
- $video_share->goods_id = $goods['goods_id'];
- $video_share->num = $goods['num'];
- $video_share->order_detail = $order_detail->id;
- $video_share->video_goods_id = $goods['videoId'];
- $video_share->store_id = get_store_id();
- $video_share->author_user_id = $video_goods->user_id;
- $video_share->created_at = time();
- $video_share->profit = $setting->profit;
- $video_share->proportion = ($goods['level_price'] * $profit);
- if (!$video_share->save()) {
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($video_share->firstErrors))
- ];
- }
- }
- }
- }
- // goods_profit 计算之后存入订单表中
- $order->profit = $goods_profit;
- $order->chain_profit = $chain_goods_profit;
- $order->save();
- // 计算门店收益
- if (get_md_id() > 0 && !in_array($order->order_type, [1, 2])) {
- $result = OrderForm::findPrice($order->id);
- MdProfit::handleProfit($order->id, $result['original_price'], $result['md_price']);
- if (!empty(input_params('flag_id'))) {
- FoodCart::updateAll(['is_delete' => 1], ['flag_id' => input_params('flag_id')]);
- FoodFlag::updateAll(['status' => 1], ['id' => input_params('flag_id')]);
- }
- }else{
- if ($order->is_offline || in_array($order->order_type, [1, 2])) {
- MdProfit::handleProfit($order->id, $order->total_price, $order->total_price, $order->md_id);
- }
- }
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- }
|