| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\scanCodePay\models\form;
- use app\constants\OptionSetting;
- use app\models\Address;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Cart;
- use app\models\Cat;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\FreeDeliveryRules;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\Level;
- use app\models\Mch;
- use app\models\MchGoodsCat;
- use app\models\OfferPrice;
- use app\models\Option;
- use app\models\Order;
- use app\models\PostageRules;
- use app\models\Shop;
- use app\models\TerritorialLimitation;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\CartOffline;
- use app\models\District;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Json;
- class OrderForm extends Model
- {
- public $mch_list;
- public $address_id;
- public $_from;
- public $longitude;
- public $latitude;
- public $store_id;
- public $store;
- public $user_id;
- /** @var User $user */
- protected $user;
- protected $address;
- protected $level;
- protected $integral;
- protected $keyword;
- public function rules()
- {
- $rules = [
- ['mch_list', 'required'],
- ['address_id', 'integer'],
- ['mch_list', function ($attr, $params) {
- $data = Json::decode($this->mch_list);
- if (!$data) {
- $this->addError($attr, "{$attr}数据格式错误。");
- }
- $this->mch_list = $data;
- }],
- ['mch_list', function ($attr, $params) {
- foreach ($this->mch_list as $i => &$mch) {
- if (!is_array($mch['goods_list'])) {
- $this->addError($attr, "{$attr}[{$i}]['goods_list']必须是一个数组。");
- return;
- }
- }
- }],
- [['longitude', 'latitude', '_from'], 'trim'],
- [['_from'], 'in', 'range' => ['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();
- }
- protected function getMchListData($submit = false)
- {
- foreach ($this->mch_list as $i => &$mch) {
- $mch['goods_list'] = $this->getGoodsList($mch['goods_list']);
- if (empty($mch['goods_list'])) {
- throw new \Exception('商品不存在', 1);
- }
- $this->getCouponList($mch);
- }
- return $this->mch_list;
- }
- protected function getGoodsList($goods_list)
- {
- $goodsIds = [];
- foreach ($goods_list as $i => &$item) {
- if (isset($item['cart_id'])) {
- $cart = Cart::findOne([
- 'store_id' => get_store_id(),
- 'id' => $item['cart_id'],
- 'is_delete' => 0
- ]);
- if (!$cart) {
- unset($goods_list[$i]);
- continue;
- }
- $item['num'] = $cart->num;
- $attr_id_list = (array)Json::decode($cart->attr);
- $goods = Goods::findOne($cart->goods_id);
- } elseif ($item['goods_id']) {
- $attr_id_list = [];
- foreach ($item['attr'] as $_a) {
- array_push($attr_id_list, $_a['attr_id']);
- }
- $goods = Goods::findOne([
- 'store_id' => get_store_id(),
- 'id' => $item['goods_id'],
- ]);
- } else {
- unset($goods_list[$i]);
- continue;
- }
- if (!$goods) {
- unset($goods_list[$i]);
- continue;
- }
- if (($goods->confine_count && $goods->confine_count > 0)) {
- $goodsNum = Goods::getBuyNum($this->user, $goods->id);
- if ($goodsNum) {
- } else {
- $goodsNum = 0;
- }
- $goodsTotalNum = intval($goodsNum + $item['num']);
- if ($goodsTotalNum > $goods->confine_count) {
- throw new \Exception('商品:' . $goods->name . ' 超出购买数量', 1);
- }
- }
- $attr_info = $goods->getAttrInfo($attr_id_list);
- // 排除当面付检测库存
- if ((in_array($goods->type, [2, 6]) == false && $item['num'] > $attr_info['num']) || $item['num'] <= 0) { //库存不足
- unset($goods_list[$i]);
- continue;
- }
- $attr_list = Attr::find()->alias('a')
- ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
- ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
- ->where(['a.id' => $attr_id_list, 'ag.store_id' => get_store_id(),])
- ->asArray()->all();
- $item['attr_list'] = $attr_list;
- $item['goods_id'] = $goods->id;
- $item['mch_id'] = $goods->mch_id;
- $item['goods_name'] = $goods->name;
- $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic;
- $item['price'] = sprintf('%.2f', ($attr_info['price'] * $item['num']));
- $item['single_price'] = sprintf('%.2f', $attr_info['price']);
- $item['weight'] = $goods->weight;
- $item['integral'] = $goods->integral ? $goods->integral : 0;
- $item['freight'] = $goods->freight;
- $item['full_cut'] = $goods->full_cut;
- $item['goods_cat_id'] = $goods->cat_id;
- $item['id'] = $goods->id;
- $item['type'] = $goods->type;
- $item['delivery_type'] = Json::decode($goods->delivery_type);
- // 当前选择的规格
- // $attrIdArr = [];
- // foreach ($item['attr_list'] as $attrListItem) {
- // $attrIdArr[] = $attrListItem['attr_id'];
- // }
- // $GoodsFullMinus = GoodsFullMinus::find()
- // ->select('*')
- // ->where(['store_id' => get_store_id(),'goods_id' => $goods['id']])
- // ->orderBy('full_minus_num ASC')->asArray()->all();
- $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([
- 'attr' => $goods['attr'],
- 'price' => $goods->type == 2 ? $item['price'] : $goods['price'],
- 'is_level' => $goods['is_level'],
- 'mch_id' => $goods['mch_id'],
- // 'full_minus' => $GoodsFullMinus,
- 'unit' => $goods['unit'],
- ], [1], [], $item['num']);
- // \Yii::error();
- // $item['batch_price_tips'] = $res['batch_price_tips'];
- // $item['current_batch_price_tips'] = $res['current_batch_price_tips'];
- $item['price'] = sprintf('%.2f', ($res['price'] * $item['num']));
- $item['level_price'] = sprintf('%.2f', ($res['level_price'] * $item['num']));
- $item['is_level'] = $res['is_level'];
- // 砍价不享受会员折扣
- if(isset($item['bargain_price'])) {
- $item['level_price'] = $item['price'];
- $item['is_level'] = 0;
- }
- $integralArr = $this->getIntegral((object)$item, Option::get(OptionSetting::STORE_INTEGRAL,get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRAL,get_store_id(), 'store')['value'])['value'], $goodsIds);
- $item['give'] = $integralArr['give'];
- $item['resIntegral'] = $integralArr['resIntegral'];
- $goodsIds[] = $goods->id;
- $item['goods_card_list'] = Goods::getGoodsCard($goods->id);
- }
- // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题
- return array_merge($goods_list, []);
- }
- //自定义表单
- protected function getFormData()
- {
- $new_list = [];
- $new_list['is_form'] = Option::get('is_form', $this->store_id, 'admin', 0);
- $form_list = [];
- if ($new_list['is_form'] == 1) {
- $new_list['name'] = Option::get('form_name', $this->store_id, 'admin', '表单信息');
- $form_list = Form::find()->where([
- 'store_id' => $this->store_id, 'is_delete' => 0,
- ])->orderBy(['sort' => SORT_ASC])->asArray()->all();
- foreach ($form_list as $index => $value) {
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- $default = str_replace(",", ",", $value['default']);
- $list = explode(',', $default);
- $default_list = [];
- foreach ($list as $k => $v) {
- $default_list[$k]['name'] = $v;
- if ($k == 0) {
- $default_list[$k]['is_selected'] = 1;
- } else {
- $default_list[$k]['is_selected'] = 0;
- }
- }
- $form_list[$index]['default_list'] = $default_list;
- }
- }
- }
- $new_list['list'] = $form_list;
- return $new_list;
- }
- protected function getAddress()
- {
- if (!$this->address) {
- if ($this->address_id) {
- $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => $this->user_id, 'is_delete' => 0]);
- } else {
- $this->address = Address::find()->where([
- 'user_id' => $this->user_id,
- 'is_default' => 1,
- 'is_delete' => 0,
- ])->limit(1)->one();
- }
- }
- return (object)$this->address;
- }
- //获取收货地址,有address_id优先获取,没有则获取默认地址
- protected 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()
- {
- $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']);
- $pay_type_list = Json::decode($pay_type_list_json['value']);
- $new_list = [];
- $ok = true;
- foreach ($this->mch_list as $mch) {
- if ($mch['mch_id'] == 0) {
- continue;
- } else {
- $ok = false;
- break;
- }
- }
- if (is_wechat_platform()) {
- $new_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if (is_alipay_platform()) {
- $new_list[] = [
- 'name' => '支付宝支付',
- 'payment' => 4,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png'
- ];
- }
- foreach ($pay_type_list as $index => $value) {
- if ($index == 'huodao' && $value['value'] == 1 && $ok) {
- $new_list[] = [
- 'name' => '货到付款',
- 'payment' => 2,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
- ];
- }
- }
- return $new_list;
- }
- /**
- * 支付方式
- * @param array $is_payment //支付方式
- * @param array $ignore //忽略的支付方式
- * @return array
- */
- public static function getPayType($is_payment = array(), $ignore = array())
- {
- if (!$is_payment || empty($is_payment)) {
- $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']);
- $is_payment = Json::decode($pay_str['value']);
- }
- $pay_type_list = [];
- foreach ($is_payment as $index => $value) {
- if (in_array($index, $ignore)) {
- continue;
- }
- if ($index == 'wechat' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png'
- ];
- }
- if ($index == 'huodao' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '货到付款',
- 'payment' => 2,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-huodao.png'
- ];
- }
- if (get_params('_from') == OrderPayDataForm::PAY_FROM_APP) {
- if ($index == 'alipay' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '支付宝支付',
- 'payment' => 4,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png'
- ];
- }
- }
- if ($index == 'balance' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '账户余额支付',
- 'payment' => 3,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-balance.png'
- ];
- }
- }
- if (!$pay_type_list) {
- $pay_type_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
- ];
- }
- return $pay_type_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 getCatIdList(&$goods_list, $mch_id = 0)
- {
- $cat_id_list = [];
- foreach ($goods_list as &$goods) {
- $goods['cat_id'] = [];
- if ($goods['goods_cat_id'] == 0) {
- if ($mch_id > 0) {
- $goods_cat_list = MchGoodsCat::find()
- ->select('cat_id')->where([
- 'goods_id' => $goods['goods_id'],
- ])->all();
- } else {
- $goods_cat_list = GoodsCat::find()
- ->select('cat_id')->where([
- 'goods_id' => $goods['goods_id'],
- 'is_delete' => 0,
- ])->all();
- }
- foreach ($goods_cat_list as $goods_cat) {
- $cat_id_list[] = $goods_cat->cat_id;
- $goods['cat_id'][] = $goods_cat->cat_id;
- }
- } else {
- $cat_id_list[] = $goods['goods_cat_id'];
- $goods['cat_id'][] = $goods['goods_cat_id'];
- }
- $cat_parent_list = Cat::find()->select('parent_id')
- ->andWhere(['id' => $goods['cat_id'], 'store_id' => $this->store_id, 'is_delete' => 0])->andWhere(['>', 'parent_id', 0])
- ->column();
- $cat_id_list = array_merge($cat_parent_list, $cat_id_list);
- $goods['cat_id'] = array_merge($cat_parent_list, is_array($goods['cat_id']) ? $goods['cat_id'] : []);
- }
- unset($goods);
- return array_unique($cat_id_list);
- }
- protected function getGoodsIdList($goods_list)
- {
- $goods_id_list = [];
- foreach ($goods_list as $goods) {
- $goods_id_list[] = $goods['goods_id'];
- }
- return $goods_id_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;
- }
- //积分计算
- /**
- * @param $goods_item object 重新编写的goods_item
- * @param $store_integral int 商城设置的积分规则
- * @param $goods_id array 已设置积分的商品id数组
- * @return array
- */
- protected function getIntegral($goods_item, $store_integral, $goods_id = array())
- {
- $integral = json_decode($goods_item->integral, true);
- $resIntegral = [
- 'forehead' => 0,
- 'forehead_integral' => 0,
- ];
- if ($integral) {
- //赠送积分计算
- $give = $integral['give'];
- if (strpos($give, '%') !== false) {
- // 百分比
- $give = trim($give, '%');
- $goods_item->give = (int)($goods_item->price * ($give / 100));
- } else {
- // 固定积分
- $goods_item->give = (int)($give * $goods_item->num);
- }
- //抵扣积分计算
- $forehead = $integral['forehead'];
- if ($forehead) {
- if (strpos($forehead, '%') !== false) {//百分比积分抵扣计算
- if ($forehead >= 100) {
- $forehead = 100;
- }
- if ($integral['more'] == '1') {//多件累计计算
- $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->price * $store_integral);
- } else {
- if (!in_array($goods_item->id, $goods_id)) { //不允许多件累计 同id商品值计算一次积分抵扣
- $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->single_price * $store_integral);
- }
- }
- } else {
- if ($integral['more'] == '1') {
- $resIntegral['forehead_integral'] = (int)($store_integral * $goods_item->price);
- if ($goods_item->price > ($forehead * $goods_item->num)) {
- $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
- }
- } else {
- if (!in_array($goods_item->id, $goods_id)) {
- $goodsPrice = $goods_item->single_price;
- $resIntegral['forehead_integral'] = (int)($store_integral * $goodsPrice);
- if ($goodsPrice > $forehead) {
- if ($forehead > 0) {
- $resIntegral['forehead_integral'] = (int)($forehead * $store_integral);
- } else {
- $resIntegral['forehead_integral'] = 0;
- }
- }
- }
- }
- }
- }
- if ($this->integral['forehead_integral'] < $this->user->integral) {
- $resetIntegral = $this->user->integral - $this->integral['forehead_integral'];
- $resIntegral['forehead_integral'] = $resIntegral['forehead_integral'] >= $resetIntegral ? $resetIntegral : $resIntegral['forehead_integral'];
- $resIntegral['forehead'] = $store_integral > 0 ? sprintf("%.2f", ($resIntegral['forehead_integral'] / $store_integral)) : 0;
- $this->integral['forehead_integral'] += $resIntegral['forehead_integral'];
- $this->integral['forehead'] += $resIntegral['forehead'];
- } else {
- $resIntegral['forehead_integral'] = 0;
- $resIntegral['forehead'] = 0;
- }
- }
- return [
- 'resIntegral' => $resIntegral,
- 'give' => $goods_item->give
- ];
- }
- protected function getExpressPrice($mch)
- {
- $expressPrice = 0;
- if ($this->address) {
- $address = $this->address;
- //先计算单品满件包邮和满额包邮
- $resGoodsList = Goods::cutFull($mch['goods_list']);
- //再通过运费规则计算运费
- $expressPrice = PostageRules::getExpressPriceMore($this->store_id, $address['city_id'], $resGoodsList, $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;
- }
- // 获取用户填写的自定义表单
- protected function getForm(&$form)
- {
- if ($form['is_form'] == 1) {
- $formList = &$form['list'];
- foreach ($formList 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['is_selected'] == 1) {
- $is_true = true;
- }
- }
- if (!$is_true) {
- return [
- 'code' => 1,
- 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
- 'name' => $value['name']
- ];
- }
- } else {
- if (!$value['default'] && $value['default'] != 0) {
- return [
- 'code' => 1,
- 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
- 'name' => $value['name']
- ];
- }
- }
- }
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- $d = [];
- foreach ($value['default_list'] as $k => $v) {
- if ($v['is_selected'] == 1) {
- $d[] = $v['name'];
- }
- }
- $formList[$index]['default'] = implode(',', $d);
- }
- }
- }
- return $form;
- }
- protected function goodsCardList()
- {
- $list = [];
- foreach ($this->mch_list as $mch) {
- if($mch['mch_id'] == 0) {
- foreach ($mch['goods_list'] as $goods) {
- if(!$goods['goods_card_list']) {
- $goods['goods_card_list'] = [];
- }
- $list = array_merge($list, $goods['goods_card_list']);
- }
- }
- }
- return $list;
- }
- }
|