['app', 'mini']], ]; return $rules; } public function afterValidate() { $this->user = User::findOne($this->user_id); $this->level = $this->getLevelData(); $this->address = $this->getAddressData(); $this->integral = [ 'forehead' => 0, 'forehead_integral' => 0, 'integration' => Option::get(OptionSetting::STORE_INTEGRATION,get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRATION,get_store_id(), 'store')['value']) ]; // parent::afterValidate(); } public function search() { if (!$this->validate()){ return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $cost_detail = $this->getCostDetail(); $AdoptSetting = AdoptSetting::findOne([ 'store_id' => $this->store_id,]); $free_preserve_time = $AdoptSetting->free_preserve_time; $preserve_day = $cost_detail['preserve_day']; $free_preserve_time_tips = '系统免费保管'.$free_preserve_time.'天,您当前已保管'.$preserve_day.'天'; return [ 'code' => 0, 'msg' => 'OOKK', 'data' => [ 'pay_type_list' => $this->getPayTypeList(), 'address' => $this->address, 'cost_detail' => $cost_detail, 'free_preserve_time_tips' => $free_preserve_time_tips ], ]; } public function getCostDetail() { $adopt_order = Order::find()->alias('o') ->where([ 'o.store_id' => $this->store_id, // 'o.user_id' => $this->user_id, 'o.id' => $this->order_id, 'o.is_delete' => 0, ]) ->leftJoin(AdoptOrderInfo::tableName() . ' aoi', "aoi.order_id = o.id ") ->leftJoin(OrderDetail::tableName() . ' od', "od.order_id = o.id ") ->leftJoin(Goods::tableName() . ' g', "od.goods_id = g.id ") ->andWhere([ 'aoi.is_delete' => 0, ]) ->select(['o.*', 'aoi.name as adopt_name', 'aoi.mobile as adopt_mobile', 'aoi.is_mature', 'aoi.mature_time', 'aoi.pick_method', 'aoi.pick_time', 'aoi.is_storage', 'aoi.storage_time', 'aoi.is_preserve', 'aoi.preserve_time', 'aoi.harvest_weight', 'aoi.billing_start_time', 'aoi.other_pick_fee', 'aoi.preserve_fee', 'aoi.harvest_weight', 'od.num', 'od.goods_id', 'g.freight', 'g.name', 'g.cover_pic', 'g.price' ]) ->asArray()->one(); $cost_list = []; if ($this->order_type == 0) { $cost_list = $adopt_order['other_pick_fee'] ? json_decode($adopt_order['other_pick_fee'], true) : []; } if ($this->order_type == 1) { $cost_list = $adopt_order['preserve_fee'] ? json_decode($adopt_order['preserve_fee'], true) : []; } $AdoptSetting = AdoptSetting::findOne(['store_id' => $this->store_id,]); $free_preserve_time = $AdoptSetting->free_preserve_time; $time = time(); $total_pay_price = 0; if ($time > ($adopt_order['preserve_time'] + $free_preserve_time * 86400)) { $preserve_billing_day = ceil(($time - ($adopt_order['preserve_time'] + $free_preserve_time * 86400)) / 86400); } else { $preserve_billing_day = 0; } $preserve_day =$adopt_order['preserve_time'] ? ceil(($time - $adopt_order['preserve_time']) / 86400) : 0; $this->weight = $this->weight ? $this->weight : 0; $billing_weight = $this->weight * 2; foreach ($cost_list as $k => $v) { $pay_price = 0; if ($v['is_day'] == 1 && $v['is_weight'] == 1) { if ($v['is_free'] == 1) { $pay_price += $preserve_billing_day * $billing_weight * $v['price']; $cost_list[$k]['calculation_method'] = "1天*"."1斤*".$v['price']."元"."(".$preserve_billing_day."天".$billing_weight."斤".")"; } else { $pay_price += $preserve_day * $billing_weight * $v['price']; $cost_list[$k]['calculation_method'] = "1天*"."1斤*".$v['price']."元"."(".$preserve_day."天".$billing_weight."斤".")"; } } elseif ($v['is_day'] == 1 && $v['is_weight'] == 0){ if ($v['is_free'] == 1) { $pay_price += $preserve_billing_day * $v['price']; $cost_list[$k]['calculation_method'] = "1天*".$v['price']."元"."(".$preserve_billing_day."天".")"; } else { $pay_price += $preserve_day * $v['price']; $cost_list[$k]['calculation_method'] = "1天*".$v['price']."元"."(".$preserve_day."天".")"; } } elseif ($v['is_day'] == 0 && $v['is_weight'] == 1){ $pay_price += $billing_weight * $v['price']; $cost_list[$k]['calculation_method'] = "1斤*".$v['price']."元"."(".$billing_weight."斤".")"; } else { $pay_price += $v['price']; $cost_list[$k]['calculation_method'] = ''; } $pay_price = sprintf('%.2f', $pay_price); $cost_list[$k]['total_price'] = $pay_price; $total_pay_price += $pay_price; } $cost_detail['total_price'] = $total_pay_price; $cost_detail['cost_list'] = $cost_list; $cost_detail['harvest_weight'] = $adopt_order['harvest_weight']; $goods = [ 'goods_id' => $adopt_order['goods_id'], 'num' => $adopt_order['num'], 'weight' => $this->weight * 1000, 'freight' => $adopt_order['freight'], 'name' => $adopt_order['name'], 'cover_pic' => $adopt_order['cover_pic'], 'price' => $adopt_order['price'], ]; $goods_list[] = $goods; $mch['mch_id'] = 0; $mch['total_price'] = $cost_detail['pay_price']; if ($this->order_type == 1) { $express_price = $this->getExpressPrice($goods_list, $mch); } else { $express_price = 0; } $cost_detail['express_price'] = $express_price > 0? $express_price : 0; $cost_detail['pay_price'] = sprintf('%.2f', $total_pay_price + $cost_detail['express_price']); $cost_detail['goods'] = $goods; $cost_detail['preserve_day'] = $preserve_day; return $cost_detail; } protected function getAddress() { if (!$this->address) { if ($this->address_id) { $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => get_saas_user_id(), 'is_delete' => 0]); } else { $this->address = Address::find()->where([ 'user_id' => get_saas_user_id(), 'is_default' => 1, 'is_delete' => 0, ])->limit(1)->one(); } } return (object)$this->address; } //获取收货地址,有address_id优先获取,没有则获取默认地址 public function getAddressData() { $address = $this->getAddress(); if (isset($address->id)) { return [ 'id' => $address->id, 'name' => $address->name, 'mobile' => $address->mobile, 'province_id' => $address->province_id, 'province' => $address->province, 'city_id' => $address->city_id, 'city' => $address->city, 'district_id' => $address->district_id, 'district' => $address->district, 'detail' => $address->detail, 'is_default' => $address->is_default, 'latitude' => $address->latitude, 'longitude' => $address->longitude, ]; } else { return null; } } //获取支付方式 protected function getPayTypeList() { $adopt_setting = AdoptSetting::findOne(['store_id' => get_store_id()]); $adopt_payment = $adopt_setting->adopt_payment; $adopt_pay_list = []; if (!empty($adopt_payment)) { $adopt_pay_type = Json::decode($adopt_payment); // $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store'); // $is_food_ok = false; // if (!empty($pay_type_list_json)) { // $pay_type_list = Json::decode($pay_type_list_json['value']); // foreach ($pay_type_list as $index => $pay_type) { // if ($index == 'friend' && $pay_type['value'] == 1) { // $is_food_ok = true; // break; // } // } // } foreach ($adopt_pay_type as &$value) { // if ($value['key'] == 'friend') { // if ($is_food_ok && $value['value'] == 1) { // $value['value'] = 1; // } else { // $value['value'] = 0; // } // } // if ($value['key'] == 'friend') { // $value['payment'] = 7; // } if ($value['key'] == 'alipay') { $value['payment'] = 4; } if ($value['key'] == 'wechat') { $value['payment'] = 1; } // if ($value['key'] == 'huodao') { // $value['payment'] = 2; // } } $adopt_pay_list = $adopt_pay_type; } foreach ($adopt_pay_list as $k => $val) { if ($val['payment'] == 4 && is_wechat_platform()) { unset($adopt_pay_list[$k]); } if ($val['payment'] == 1 && is_alipay_platform()) { unset($adopt_pay_list[$k]); } } $new_list = array_values($adopt_pay_list); return $new_list; } protected function getCouponList(&$mch) { if (!isset($mch['mch_id'])) { $mch['mch_id'] = 0; } $goods_total_price = isset($mch['total_price']) ? $mch['total_price'] : 0; $cat_ids = $this->getCatIdList($mch['goods_list'], $mch['mch_id']); $coupon_goods_id = $this->getGoodsIdList($mch['goods_list']); // Yii::error([$goods_total_price, $this->user_id, $this->store_id]); $list = UserCoupon::find()->alias('uc') ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id') ->leftJoin(['cas' => CouponAutoSend::tableName()], 'uc.coupon_auto_send_id=cas.id') ->where([ 'AND', ['uc.store_id' => $this->store_id], ['uc.is_delete' => 0], ['uc.is_use' => 0], ['uc.is_expire' => 0], ['uc.user_id' => $this->user_id], ['<=', 'c.min_price', $goods_total_price], ]) ->select('uc.id user_coupon_id,c.mch_id,uc.is_use,uc.is_expire,c.sub_price,c.min_price,cas.event,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list') ->asArray()->all(); $events = [ 0 => '平台发放', 1 => '分享红包', 2 => '购物返券', 3 => '领券中心', ]; $max_price = isset($mch['total_price']) ? $mch['total_price'] : 0; $new_list = []; foreach ($list as $i => $item) { if ($item['begin_time'] > (strtotime(date('Y-M-d')) + 86400) || $item['end_time'] < time()) { continue; } $list[$i]['status'] = 0; if ($item['is_use']) { $list[$i]['status'] = 1; } if ($item['is_expire']) { $list[$i]['status'] = 2; } $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用'; $list[$i]['begin_time'] = date('Y.m.d H:i', (int)$item['begin_time']); $list[$i]['end_time'] = date('Y.m.d H:i', (int)$item['end_time']); if (!$item['event']) { if ($item['type'] == 2) { $list[$i]['event'] = $item['event'] = 3; } else { $list[$i]['event'] = $item['event'] = 0; } } if ($list[$i]['mch_id'] > 0) { $list[$i]['event_desc'] = Mch::findOne($list[$i]['mch_id'])->name; }else { $list[$i]['event_desc'] = $events[$item['event']]; } $list[$i]['min_price'] = doubleval($item['min_price']); $list[$i]['sub_price'] = doubleval($item['sub_price']); if ($list[$i]['appoint_type'] == 1) { $list[$i]['cat_id_list'] = json_decode($list[$i]['cat_id_list']); if ($list[$i]['cat_id_list'] != null) { $goodsAdd = []; foreach ($mch['goods_list'] as $v2) { if (!in_array($v2['goods_id'], $goodsAdd)) { array_push($goodsAdd, $v2['goods_id']); } }; if ($mch['price'] < $list[$i]['min_price']) { unset($list[$i]); continue; } } } elseif ($list[$i]['appoint_type'] == 2) { $list[$i]['goods_id_list'] = json_decode($list[$i]['goods_id_list']); if ($list[$i]['goods_id_list'] != null) { $current = array_intersect($list[$i]['goods_id_list'], $coupon_goods_id); if ($current) { } else { unset($list[$i]); continue; } } } else { if ($max_price < $list[$i]['min_price']) { unset($list[$i]); continue; } } $new_list[] = $list[$i]; } $mch['coupon_list'] = $new_list; } protected function getLevelData() { $level = Level::find()->select([ 'name', 'level', 'discount', ])->where(['level' => $this->user->level, 'store_id' => $this->store_id, 'is_delete' => 0]) ->asArray()->one(); return $level; } protected function getExpressPrice($goods_list,$mch) { $expressPrice = 0; if ($this->address) { $address = $this->address; // //先计算单品满件包邮和满额包邮 // $resGoodsList = Goods::cutFull($mch['goods_list']); //再通过运费规则计算运费 $expressPrice = PostageRules::getExpressPriceMore($this->store_id, $address['city_id'], $goods_list, $address['province_id']); } $expressPrice = $this->getFreeDeliveryRules($mch, $expressPrice); return $expressPrice >= 0 ? $expressPrice : 0; } /** * 计算打包费 * @param $mch * @return int */ protected function getDabaoPrice($mch) { $goods_list = $mch['goods_list']; $dabao_price = 0; foreach ($goods_list as $value) { $dabao_price += $value['dabao_price']; } return $dabao_price >= 0 ? $dabao_price : 0; } // 获取门店列表 protected function getShopList() { $start = 0; $shop_table_name = Shop::tableName(); $user_table_name = User::tableName(); $address = Address::findOne($this->address['id']); $latitude = $address->latitude ? $address->latitude : 0; $longitude = $address->longitude ? $address->longitude : 0; $sql = "SELECT `s`.*, `u`.`nickname`, acos(cos({$latitude}*pi()/180 )*cos(s.latitude*pi()/180)*cos({$longitude}*pi()/180 -s.longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(s.latitude*pi()/180))*6370996.81 as distance FROM {$shop_table_name} `s` LEFT JOIN {$user_table_name} `u` ON s.id=u.shop_id WHERE ((`s`.`store_id`={$this->store_id}) AND (`s`.`is_delete`=0)) AND (`s`.`user_id` > 0) "; if ($this->keyword) { $sql .= " AND (`s`.`name` like '%{$this->keyword}%') "; } $sql .= "ORDER BY `distance` LIMIT {$start},30"; $list = \Yii::$app->db->createCommand($sql)->queryAll(); $shop = null; foreach ($list as $index => $item) { if ($item['is_default'] == 1) { $shop = $item; } $list[$index]['distance'] = round($item['distance']/1000, 2).'km'; } return [ 'list' => $list, 'shop' => $shop ]; } // 获取起送规则 protected function getOfferRule($mch) { $res = [ 'is_allowed' => 0, 'total_price' => 0, 'msg' => '' ]; if ($mch['mch_id'] > 0) { $res['msg'] = '商户不支持起送规则'; return $res; } if ($mch['plugin_type'] == 2) { $res['msg'] = '砍价不支持起送规则'; return $res; } if (!$this->address) { $res['msg'] = '请选择收货地址'; return $res; } $offerRule = OfferPrice::findOne(['store_id' => get_store_id(),'is_delete' => 0]); if (!$offerRule) { $res['msg'] = '起送规则不存在'; return $res; } if ($offerRule->is_enable == 0) { $res['msg'] = '起送规则未开启'; return $res; } $ruleList = Json::decode($offerRule->detail); $res['total_price'] = $offerRule->price; if (is_array($ruleList)) { foreach ($ruleList as $value) { foreach ($value['province_list'] as $item) { if ($item['id'] == $this->address['city_id']) { $res['total_price'] = $value['offer_price']; } } } } if ($mch['total_price'] >= $res['total_price']) { $res['is_allowed'] = 0; } else { $res['is_allowed'] = 1; } $value = round($res['total_price'] - $mch['total_price'], 2); $res['msg'] = "自营商品,还差{$value}元起送"; return $res; } protected function getTerritorialLimitation($mch) { $isArea = 0; if ($mch['mch_id'] > 0) { return $isArea; } if ($this->address) { $area = TerritorialLimitation::findOne([ 'store_id' => $this->store_id, 'is_delete' => 0, 'is_enable' => 1, ]); if ($area) { $city_id = []; //限制的地区ID $detail = json_decode($area->detail); if (!is_array($detail)) { $detail = []; } foreach ($detail as $key => $value) { foreach ($value->province_list as $key2 => $value2) { $city_id[] = $value2->id; } } $addressArr = [ $this->address['province_id'], $this->address['city_id'], $this->address['district_id'] ]; $addressArray = array_intersect($addressArr, $city_id); if (empty($addressArray)) { $isArea = 1; } } } return $isArea; } // 包邮规则 protected function getFreeDeliveryRules($mch, $expressPrice) { if ($expressPrice == 0) { return $expressPrice; } if ($mch['mch_id'] == 0) { $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'mch_id' => 0])->asArray()->all(); foreach ($free as $k => $v) { $city = json_decode($v['city'], true); foreach ($city as $v1) { if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) { $expressPrice = 0; break; } } } } else { $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id, 'mch_id' => $mch['mch_id'], 'is_delete' => 0])->asArray()->all(); foreach ($free as $k => $v) { $city = json_decode($v['city'], true); foreach ($city as $v1) { if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) { $expressPrice = 0; break; } } } } return $expressPrice; } }