validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } try { $mch_list = $this->getStoreListData(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'], ]; } $delivery_time = $mch['delivery_time'];//送出时间 } if($this->order_no){ $existOrder = Order::findOne(['order_no' => $this->order_no]); if($existOrder){ return [ 'code' => 1, 'msg' => '单号错误0,请后退到商品页重新下单', ]; } $order_no = $this->order_no; }else{ $order_no = cache()->get('delivery_order_no_' . get_saas_user_id()); } $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, $this->store_id, 'store')['value']; $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, $this->store_id, 'pay', $local_type); if($local_type['value'] == 'keloop') { if(KeloopForm::isSaasOpen()){ $preAddOrder = KeloopForm::preAddOrder($this->store_id, $order_no, $this->address['longitude'], $this->address['latitude']); if ($preAddOrder['code']) { return $preAddOrder; } return [ 'code' => 0, 'msg' => 'ok', 'data' => [ "resultcode" => 0, 'fee' => (float)$preAddOrder['data']['fee'], 'fee_data' => $preAddOrder['data']['fee_data'], ], 'order_no' => $order_no, ]; } return [ 'code' => 1, 'msg' => '快跑者配送没有配置', ]; } if($local_type['value'] == 'maiyatian') {//麦芽田 if(MaiyatianForm::isopen($this->store_id)){ $getFreight = LocalDeliveryFreight::getFreight($this->store_id, $this->address, $delivery_time, LocalDeliveryFreight::TYPE_MAIYATIAN, $goods_list); if($getFreight['code'] != 0){ return $getFreight; } $freight = $getFreight['data']; $preAddOrder = MaiyatianForm::preAddOrder($this->store_id, $order_no, $freight); return [ 'code' => 0, 'msg' => 'ok', 'data' => [ "resultcode" => 0, // 'fee' => (float)MaiyatianForm::getConf($this->store_id, 'cyy_delivery_fee'), 'fee' => (float)$freight, ], 'order_no' => $order_no, ]; return $preAddOrder; } return [ 'code' => 1, 'msg' => '麦芽田没有配置', ]; } elseif($local_type['value'] == 'aggregate') {//聚合配送 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 array_merge($res, ['order_no' => $order_no]); } $store = Store::findOne($this->store_id); if (intval($store->wechat_delivery_type) === 0) { return array_merge(Delivery::preAddOrder($goods_list, $order_no), ['order_no' => $order_no]); } else { $form = new WechatNewDelivery(); $result = $form->preAddOrder([ 'address' => ArrayHelper::toArray($this->address), 'goods_list' => $goods_list, 'store_id' => $this->store_id, 'md_id' => get_md_id() ]); if ($result['code'] === 0) { DeliveryInfo::deleteAll(['order_no' => $order_no, 'store_id' => get_store_id(), 'status' => 0]); // 存预下单表 $delivery_info = new DeliveryInfo(); $delivery_info->store_id = get_store_id(); $delivery_info->order_no = $order_no; $delivery_info->fee = bcdiv($result['data']['fee'], 100, 2); $delivery_info->delivery_type = 0; $delivery_info->created_at = time(); $delivery_info->is_local = DeliveryInfo::IS_LOCAL_NO; $serial_num = DeliveryInfo::find()->where(['>','created_at',strtotime(date('Y-m-d'))])->orderBy('serial_num desc')->asArray()->one()['serial_num']; $delivery_info->serial_num = $serial_num > 0 ? $serial_num + 1 : 1; $delivery_info->md_id = get_md_id(); $delivery_info->wechat_delivery_type = 1; $delivery_info->save(); return array_merge($result, ['order_no' => $order_no]); } else { return $result; } // return array_merge($form->preAddOrder([ // 'address' => ArrayHelper::toArray($this->address), // 'goods_list' => $goods_list, // 'store_id' => $this->store_id, // 'md_id' => 0 // ]), ['order_no' => $order_no]); } } elseif(in_array($local_type['value'], ['self', 'self_store'])) {//平台自配 $type = LocalDeliveryFreight::TYPE_LOCAL; if ($local_type['value'] === 'self_store') { $type = LocalDeliveryFreight::TYPE_STORE_LOCAL; } $getFreight = LocalDeliveryFreight::getFreight($this->store_id, $this->address, $delivery_time, $type, $goods_list); if($getFreight['code'] != 0){ return $getFreight; } $freight = $getFreight['data']; DeliveryInfo::deleteAll(['order_no' => $order_no, 'store_id' => get_store_id(), 'status' => 0]); // 存预下单表 $delivery_info = new DeliveryInfo(); $delivery_info->store_id = get_store_id(); $delivery_info->order_no = $order_no; $delivery_info->fee = $freight; $delivery_info->delivery_type = 0; $delivery_info->created_at = time(); $delivery_info->is_local = DeliveryInfo::IS_LOCAL_YSE; $delivery_info->is_store_delivery_type = intval($local_type['value'] === 'self_store'); $serial_num = DeliveryInfo::find()->where(['>','created_at',strtotime(date('Y-m-d'))])->orderBy('serial_num desc')->asArray()->one()['serial_num']; $delivery_info->serial_num = $serial_num > 0 ? $serial_num + 1 : 1; $delivery_info->save(); return [ 'code' => 0, 'msg' => 'success', 'order_no' => $order_no, 'data' => $getFreight['info'], ]; /* end */ // return [ // 'code' => 0, // 'data' => [ // 'goods_list' => $goods_list, // 'order_no' => $order_no, // 'test' => [ // 'address' => $address, // 'store' => $store, // 'distance' => $distance, // 'delivery_time' => $delivery_time, // 'delivery_time_stamp' => $delivery_time_stamp, // 'LocalDeliveryFreight' => $data, // 'freight_detail' => $freight_detail, // 'freight' => $freight, // ] // ], // 'msg' => '平台自配' // ]; } } 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->getStoreListData(true); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } $order_id_list = []; $md_select = 0; /** * @var $address Address */ $saas_user = get_saas_user(); $this->user = User::findOne(['binding' => $saas_user->mobile,'store_id' => get_store_id(),'is_delete'=>0]); $address = (object)($this->address); $t = \Yii::$app->db->beginTransaction(); foreach ($mchListData as &$mch) { $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']; } } $is_delivery = OfferPrice::findOne([ 'store_id' => get_store_id(), 'is_delete' => 0 ]); $store_data = Store::findOne(get_store_id()); if ($mch['send'] == 'delivery') { $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value']; $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type); $is_have_express = true; $delivery_rules = json_decode($is_delivery->delivery_rules, true); if ($is_delivery->delivery_type == 1 && !empty($delivery_rules)) { $conf = [ 'shop_tag' => $store_data->coordinate, 'deliveryRules' => $delivery_rules ]; $deliveryRules = $this->canDeliver($address, $conf); if (!$deliveryRules['can_deliver']) { return [ 'code' => 1, 'msg' => '当前订单超出配送距离' ]; } } if ($local_type['value'] == 'keloop') { //快跑者配送 if (!KeloopForm::isSaasOpen()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '商城未设置有效配送方式' ]; } } if($local_type['value'] == 'maiyatian') {//麦芽田 $maiyatian = MaiyatianForm::isopen($this->store_id); if (!$maiyatian) { $t->rollBack(); return [ 'code' => 1, 'msg' => '商城未设置有效配送方式' ]; } } elseif($local_type['value'] == 'aggregate') {//聚合配送 $store = Store::findOne($this->store_id); if (intval($store->wechat_delivery_type) === 0) { if ($store->delivery_type == 0) { $t->rollBack(); return [ 'code' => 1, 'msg' => '商城未设置有效配送方式' ]; } $res = Delivery::getBindAccount(); // 下单时判断是否可以同城配送 if (empty($res['data']['shop_list'])) { $t->rollBack(); return [ 'code' => 1, 'msg' => '当前未绑定运力账号,暂不能保存选择同城配送方式' ]; } $shop_list = $res['data']['shop_list']; $delivery_arr = array_column($shop_list, 'delivery_id'); $delivery_arr[] = 'TEST'; $delivery_arr_id = array_column($shop_list, null, 'delivery_id'); if (!in_array(Delivery::$deliveryIdArr[$store->delivery_type], $delivery_arr) || $delivery_arr_id[Delivery::$deliveryIdArr[$store->delivery_type]]['audit_result'] != 0) { $t->rollBack(); return [ 'code' => 1, 'msg' => '当前账号未在微信后台绑定或者并未审核通过' ]; } } } elseif(in_array($local_type['value'], ['self', 'self_store'])) {//平台自配 if(empty($mch['delivery_time'])){ $store_id = $local_type['value'] === 'self_store' ? $this->store_id : 0; $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, $store_id, '', '{}')['value'], true); $mch['delivery_time'] = date('Y-m-d H:i:s', time() + 90 * 60); if(!empty($setting['default_time']) && !empty($setting['default_time']['value'])){ $mch['delivery_time'] = date('Y-m-d H:i:s', time() + ($setting['default_time']['value'] * 60)); } } } $is_have_delivery = true; } // 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['total_price'] = $total_price; // $mch['total_integral'] = $total_integral; //积分兑换 $payPrice = $this->getPayPrice($mch); $order = new PtActivityOrder(); $order->is_platform = 1; // 订单来源 if (is_h5()) { $order->order_origin = Order::ORDER_SOURCE_WEB; } $order->store_id = get_store_id(); $user = User::findOne(['binding' => get_saas_user()->mobile, 'is_delete' => 0, 'store_id' => get_store_id()]); $order->user_id = $this->user_id ?: $user->id; $order->saas_id = get_saas_user_id(); if ($is_have_delivery) { if($this->order_no){ $order->order_no = $this->order_no; }else{ $order->order_no = cache()->get('delivery_order_no_' . get_saas_user_id()); } $existOrder = Order::findOne(['order_no' => $order->order_no]); if($existOrder){ $t->rollBack(); return [ 'code' => 1, 'msg' => '单号错误1,请后退到商品页重新下单', ]; } } else { $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; } if($this->send_price){ $order->send_price = $this->send_price; } if($this->take_price){ $order->take_price = $this->take_price; } $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']; } //获取代理配置信息 $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); } if ($is_have_delivery) { $cacheK = 'is_have_delivery_' . $order->order_no; $cacheV = cache()->get($cacheK); $deliveryInfo = DeliveryInfo::find()->where(['order_no' => $order->order_no])->one(); if(!$deliveryInfo){ $t->rollBack(); if($cacheV){ return [ 'code' => 1, 'msg' => '配送单不存在,请返回商品详情页重新下单。' . $cacheV . $cacheK ]; } cache()->set($cacheK, 1, 180); $this->order_no = ''; $preOrder = $this->preOrder(); if($preOrder['code'] != 0){ return $preOrder; }else{ $this->order_no = ''; return $this->save(); } } else { if (intval($deliveryInfo->wechat_delivery_type) === 1) { $goods_list = []; //foreach ($mch as $mch_item) { foreach ($mch['goods_list'] as $goods) { $goods_list[] = [ 'name' => $goods['goods_name'], 'price' => $goods['price'], 'pic' => $goods['goods_pic'], 'num' => $goods['num'], 'weight' => $goods['weight'], ]; } //} $form = new WechatNewDelivery(); $result = $form->preAddOrder([ 'address' => ArrayHelper::toArray($this->address), 'goods_list' => $goods_list, 'store_id' => $this->store_id, 'md_id' => get_md_id() ]); // $result = $form->addOrder([ // 'address' => ArrayHelper::toArray($this->address), // 'goods_list' => $goods_list, // 'store_id' => $order->store_id, // 'md_id' => $order->md_id ?: 0, // 'order_no' => $order->order_no, // 'store_user' => ArrayHelper::toArray(get_user()), // 'order_seq' => $deliveryInfo->serial_num, // 'order_detail_path' => '/order/order-detail/order-detail?id=' . $order->id, // ]); if ($result['code'] === 0) { $deliveryInfo = DeliveryInfo::findOne(['order_no' => $order->order_no]); if (floatval($deliveryInfo->fee) !== floatval(bcdiv($result['data']['fee'], 100, 2))) { $t->rollBack(); return [ 'code' => 1, 'msg' => '配送费错误,请重新下单' ]; } } else { $t->rollBack(); return $result; } } } $order->total_price += $deliveryInfo->fee; $order->pay_price += $deliveryInfo->fee; } 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]; if($order->take_price > 0){ if (($order->pay_price - $order->take_price) >= 0) { $order->pay_price -= $order->take_price; $order->total_price -= $order->take_price; } else { $order->take_price -= $order->pay_price; $order->total_price = $order->pay_price = 0; } $order->save(); } 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(); } } } /** * 判断是否可以配送并计算费用 */ private function canDeliver($address, $conf) { // 解析商家地址的经纬度 list($shopLon, $shopLat) = explode(',', $conf['shop_tag']); // 计算距离 $distance = Tools::getDistance((float)$shopLat, (float)$shopLon, $address->latitude ?: $address['latitude'], $address->longitude ?: $address['longitude']); if (strpos($distance, 'km') !== false) { $distance = floatval(explode('km', $distance)[0]); } elseif (strpos($distance, 'm') !== false) { $distance = floatval(explode('m', $distance)[0]) / 1000; } foreach ($conf['deliveryRules'] as $rule) { if ($rule['start'] <= $distance && $distance <= $rule['end']) { return [ 'can_deliver' => true, 'distance' => $distance, 'delivery_price' => $rule['price'], 'delivery_range' => [$rule['start'], $rule['end']] ]; } } return ['can_deliver' => false, 'distance' => $distance, 'delivery_price' => null, 'delivery_range' => null]; } 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([]); $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']); } 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' => '' ]; } }