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' => '' ]; } }