validate()) return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; try { $mch_list = $this->getMchListData(true); $goods_list = []; foreach ($mch_list as $mch) { foreach ($mch['goods_list'] as $goods) { $goods_list[] = [ 'name' => $goods['goods_name'], 'price' => $goods['price'], 'pic' => $goods['goods_pic'], 'num' => $goods['num'], ]; } } $order_no = cache()->get('delivery_order_no_' . get_user_id()); if(is_alipay_platform()){ $this->address_id || $this->address_id = 220; $res = ADelivery::createOrder(1, $this->store_id, get_mini_id(), get_saas_user()->ali_user_id, $goods_list, $order_no, $this->address_id);; return $res; } return Delivery::preAddOrder($goods_list, $order_no); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } 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' => $e->getMessage() ]; } $order_id_list = []; $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 = false; $is_have_delivery = 0; if ($mch['send'] == 'express') { $is_have_express = true; } if ($mch['send'] == 'shop') {// || get_md_id() $is_have_shop = true; // if (!empty($mch['shop']) && !empty($mch['shop']['id'])) { // $md_select = $mch['shop']['id']; // } $md = Md::findOne(['id' => get_md_id(), 'is_delete' => 0, 'store_id' => $this->store_id]); if (empty($md)) { return [ 'code' => 1, 'msg' => '到店自提请选择对应门店' ]; } } if (!in_array($mch['send'], ['express', 'shop'])) { return [ 'code' => 1, 'msg' => '请选择快递或到店自提' ]; } foreach ($mch_goods as $v) { $level_price += $v['level_price']; $total_price += isset($v['total_price']) ? $v['total_price'] : $v['price']; } $mch['level_price'] = $level_price; $mch['level_diff_price'] = sprintf('%.2f',$mch['next_level']['diff_price']); $mch['total_price'] = $total_price; // $mch['total_integral'] = $total_integral; //积分兑换 $payPrice = $this->getPayPrice($mch); $order = new PtActivityOrder(); // 订单来源 if (is_h5()) { $order->order_origin = Order::ORDER_SOURCE_WEB; } $order->store_id = $this->store_id; $order->user_id = $this->user_id; $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_PT); $order->pay_price = $payPrice; $order->pt_number = $mch['pt_number']; if ($this->head_integral > 0) { if ($this->head_integral > get_user()->integral) { return [ 'code' => 1, 'msg' => '开团积分不足' ]; } $order->head_integral = $this->head_integral; } // 订单类型 // 是否是平台小程序订单 if (get_md_id()) { $order->md_id = get_md_id(); } if($md_select > 0){ $order->md_id = $md_select; } $order->created_at = time(); $order->content = $mch['content'] ?? ''; $order->is_offline = $is_have_shop ? 1 : 0; $order->remark = $mch['remark']; if ($this->payment == 2) { $order->pay_type = 2; $order->is_pay = 0; } if ($this->payment == 3) { $order->pay_type = 3; $order->is_pay = 0; } // 线下支付 if ($this->payment == 6) { $order->pay_type = 6; $order->is_pay = 0; } if ($is_have_express || get_md_id()) { $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; $order->address_data = 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->express_price = $mch['express_price']; $order->total_price = $mch['total_price'] + $mch['express_price']; } if ($is_have_shop) { if (get_md_id()) { $md_info = Md::findOne(get_md_id()); $order->province_id = $md_info->province; $order->city_id = $md_info->city; $order->district_id = $md_info->district; } $order->express_price = 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($store->transfer_profit)) { $order->ag_rebate = $payPrice * ($store->transfer_profit / 100); } $order->saas_id = 0; if ($order->save()) { // 绑定上下级 $user = User::findOne($this->user_id); if ($user && $user->parent_user_id) { CommonOrder::saveParentId($user->parent_user_id); } // 处理订单生成之后其他相关数据 $orderRes = $this->insertData($mch, $order); if ($orderRes['code'] == 1) { $t->rollBack(); return $orderRes; } $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'], 0, $order->store_id); $order_id_list[] = $order->id; } else { $t->rollBack(); return [ 'code' => 0, 'msg' => implode(';', array_values($order->firstErrors)) ]; } } } if (count($order_id_list) > 0) { $t->commit(); $store = Store::findOne($this->store_id); $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, 'douyin_url' => $store->douyin_url, ], ]; } 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(); } } } public function getNo($attrStr,$searchAttrIds){ $attr = json_decode($attrStr,true); foreach ($attr as $item){ $attrIds = array_column($item['attr_list'],'attr_id'); if ($attrIds == $searchAttrIds){ return $item['no']; } } } // 获得实际支付金额 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; } } } } if ($this->buy_level_id) { $level = Level::findOne($this->buy_level_id); $payPrice += ($level->price * 1); } return $payPrice > 0 ? $payPrice : 0; } // 检查数据 private function checkMchData($mch) { if (empty($mch['goods_list'])) { return [ 'code' => 1, 'msg' => '商品不存在或已删除' ]; } $checkFormData = $this->checkFormData($mch); if ($checkFormData['code'] == 1) { return $checkFormData; } $checkSendType = $this->checkSendType($mch); if ($checkSendType['code'] == 1) { return $checkSendType; } // $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 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 checkSendType($mch) { if ($mch['mch_id'] == 0) { if (!$this->address) { return [ 'coe' => 1, 'msg' => '收货地址不存在' ]; } if ($mch['offer_rule'] && $mch['offer_rule']['is_allowed'] == 1) { return [ 'code' => 1, 'msg' => $mch['offer_rule']['msg'] ]; } if ($mch['is_area'] == 1 && $mch['send'] == 'express') { return [ 'code' => 1, 'msg' => '所选地区自营商品暂时无货' ]; } } return ['code' => 0, 'msg' => '']; } // 检测自定义表单 private function checkFormData($mch) { foreach ($mch['goods_list'] as $goods) { if (isset($goods['form']['code'])) { return $goods['form']; } if (!isset($goods['form'])) { continue; } $form = $goods['form']; if ($form['is_form'] == 1) { $form_list = $form['list']; foreach ($form_list as $index => $value) { if ($value['required'] == 1) { if (in_array($value['type'], ['radio', 'checkbox'])) { $is_true = false; foreach ($value['default_list'] as $k => $v) { if ($v['checked'] == true) { $is_true = true; } } if (!$is_true) { return [ 'code' => 1, 'msg' => '请填写' . $form['name'] . ',加“*”为必填项', 'name' => $value['name'] ]; } } else { if (empty($value['default']) && strlen($value['default']) == 0) { return [ 'code' => 1, 'msg' => '请填写' . $form['name'] . ',加“*”为必填项', 'name' => $value['name'] ]; } } } } } } 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(); $is_install = $this->goodsIsInstall($goods['goods_id']); if ($is_install !== 2) { $store = Store::findOne(get_store_id()); $store_address = [ 'province_id' => $store->province_id, 'city_id' => $store->city_id, 'district_id' => $store->district_id, ]; $address = $is_install === 1 ? $this->address : $store_address; $result = $this->goodsIsGoodsAgent($address, $goods['goods_id']); if ($result['code'] !== 0) { return $result; } } } return ['code' => 0, 'msg' => '']; } public function getOrderNo() { $order_no = null; while (true) { $order_no = date('YmdHis') . mt_rand(100000, 999999); $exist_order_no = Order::find()->where(['order_no' => $order_no])->exists(); if (!$exist_order_no) { break; } } return $order_no; } // 优惠券可优惠的商品总额计算 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; } /** * @param $mch * @param $order PtActivityOrder * @return array */ private function insertData($mch, $order) { // 计算商品相关 $goods_list = $mch['goods_list']; $goodsPrice = 0; // 计算商品分润 $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 PtActivityOrderDetail(); $order_detail->order_id = $order->id; $order_detail->goods_id = $goods['goods_id']; $order_detail->num = $goods['num']; $order_detail->is_level = 0; $order_detail->activity_id = $this->pt_activity_id; $payPrice = $goods['level_price']; $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; } $order_detail->total_price = $payPrice; // 计算单个商品可分红金额 $profit = $goods['rate_type'] == 0 ? floatval($payPrice * $goods['rate'] / 100) : floatval($goods['rate']) * $goods['num']; $goods_profit += $profit; $order_detail->profit = $profit; $order_detail->is_delete = 0; $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([]); if ($goods['give'] > 0) { $order_detail->integral = $goods['give']; } else { $order_detail->integral = 0; } $order_detail->delivery_type = $mch['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : OrderDetail::GOODS_DELIVERY_SHOP; $attr_id_list = []; foreach ($goods['attr_list'] as $item) { array_push($attr_id_list, $item['attr_id']); } //增值积分下单扣减用户积分 /*$integralAppreciationGoods = IntegralAppreciationGoods::findOne(['goods_id' => $goods['goods_id'], 'is_delete' => 0]); if ($integralAppreciationGoods && $integralAppreciationGoods->integral_amount > 0) { $integral_appreciation_setting = Option::get('integral_appreciation_setting', $order->store_id, 'integral_appreciation')['value']; $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true); $integral_custom_name = $integral_appreciation_setting['integral_custom_name'] ?: '增值积分'; $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $this->user_id]); if (!$integralAppreciationUser) { return [ 'code' => 1, 'msg' => "用户{$integral_custom_name}数量不足" ]; } $integral_amount = bcmul($integralAppreciationGoods->integral_amount, $goods['num'], 2); if ($integral_amount > $integralAppreciationUser->integral) { return [ 'code' => 1, 'msg' => "用户{$integral_custom_name}数量不足" ]; } $integralAppreciationUser->integral = bcsub($integralAppreciationUser->integral, $integral_amount, 2); $integralAppreciationUser->save(); $result = IntegralAppreciationUserIntegralLog::saveIntegralLog( $integralAppreciationUser->id, $integral_amount, IntegralAppreciationUserIntegralLog::TYPE_EXPEND, IntegralAppreciationUserIntegralLog::SOURCE_TYPE_BUY_GOODS, "购买商品 订单号{$order->order_no}", $order->id, IntegralAppreciationUserIntegralLog::ORDER_TYPE_PT); if ($result['code']) { return $result; } }*/ if (!$order_detail->save()) { return [ 'code' => 1, 'msg' => '订单提交失败,请稍后再重试', 'error' => $order_detail->errors[0] ]; } } // goods_profit 计算之后存入订单表中 $order->profit = $goods_profit; $order->save(); // 计算门店收益 if ($order->is_offline) { // MdProfit::handleProfit($order->id, $order->total_price, $order->total_price, $order->md_id); } return [ 'code' => 0, 'msg' => '' ]; } public function getBookPrice($payPrice, $mch_goods) { $price = $payPrice; foreach ($mch_goods as $goods) { if (0 && $goods['product_type'] == 1) { // $goods_book = GoodsBook::findOne(['goods_id' => $goods['id']]); // $date_book = Json::decode($goods_book->date_book); // $book_config_data = array_column($date_book, NULL, 'date'); // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'price')); // $num_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num')); $price = 0; if (empty($goods['book'])) { return [ 'code' => 1, 'msg' => '预约日期数据为空' ]; } foreach ($goods['book'] as $value) { // if ($num_config[$value['date']] < $goods['num']) { // return [ // 'code' => 1, // 'msg' => '选中日期' . $value['date'] . '内暂无房源' // ]; // } // $price += $data_config[$value['date']]; $price += $goodsInfo['price']; } $price = $price * $goods['num']; } if ($goods['product_type'] == 2) { $price = 0; $goods_book = GoodsBook::findOne(['goods_id' => $goods['id']]); $service_book = Json::decode($goods_book->service_book); //$service_book = $service_book['data']; $date_data = $this->getDateByInterval(7); $new_arr = [ 'data' => [] ]; foreach ($date_data as $index => $datum) { $new_arr['data'][$index]['date'] = $datum; $new_arr['data'][$index]['time'] = $service_book['data'][0]['time']; } if (!$new_arr) { return [ 'code' => 1, 'msg' => '数据异常' ]; } $price = 0; $service_book = $new_arr['data']; $service_book_date = array_column($service_book, NULL, 'date'); $service_book_time = $service_book_date[$goods['service']['date']]['time']; foreach ($service_book_time as $m) { foreach ($m['times'] as $n) { if ($n['time'] == $goods['service']['time']) { $price = $goods['service']['price'] * $goods['num']; break; } } if ($price > 0) { break; } } } } \Yii::error($price); return $price; } /** * 检查当前时间是否为指定时间范围内 * @param $begin 20:00:00 * @param $end 08:00:00 * @param $date * @return bool true 时间范围内 */ private function checkTimeBeginEnd ($begin, $end, $date = null) { if (empty($begin) && empty($end)) { return true;// } $date = is_null ($date) ? date ('H:i:s') : $date; $curTime = strtotime ($date); // 当前时分 $assignTime1 = strtotime ($begin); // 获得指定分钟时间戳,00:00 $assignTime2 = strtotime ($end); // 获得指定分钟时间戳,01:00 $result = false; if ($assignTime1 > $assignTime2) { //跨24点 if ($curTime > $assignTime1 || $curTime < $assignTime2) { $result = true; } } if ($curTime > $assignTime1 && $curTime < $assignTime2) { $result = true; } if ($assignTime1 == $assignTime2) { $result = true; } return $result; } }