| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\order;
- use app\constants\OptionSetting;
- use app\models\Address;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Cat;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\Level;
- use app\models\MchGoodsCat;
- use app\models\MdGoods;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\Store;
- use app\models\User;
- use app\models\UserCoupon;
- use app\plugins\adopt\models\AdoptGoods;
- use app\utils\Delivery\Alipay\ADelivery;
- use app\utils\Delivery\Delivery;
- use yii\base\Model;
- use yii\helpers\Json;
- /**
- * 收银台订单生成结算模块
- * User: hankaige
- * DATE TIME: 2022/12/7 09:56
- */
- class MchOrderForm extends Model
- {
- public $hanging_order_id = 0;// 如果有挂单ID的话 要处理挂单的信息
- public $mch_list;
- public $user_id;
- public $md_id;
- public $store_id;
- public $address_id;
- protected $user;
- protected $level;
- protected $address;
- protected $integral;
- protected $saas_user;
- public function rules()
- {
- $rules = [
- [['mch_list','store_id'], 'required'],
- [['hanging_order_id','user_id','store_id', 'address_id', 'md_id'], 'integer'],
- ['mch_list', function ($attr, $params) {
- $data = Json::decode($this->mch_list);
- if (!$data) {
- $this->addError($attr, "{$attr}数据格式错误。");
- }
- $this->mch_list = $data;
- }],
- ];
- return $rules;
- }
- // 走过验证后 查询相应数据
- public function afterValidate()
- {
- $this->user = User::findOne($this->user_id);
- $this->level = $this->getLevelData();
- $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'])['value']
- ];
- $this->saas_user = SaasUser::findOne(['mobile' => $this->user->binding]);
- parent::afterValidate();
- }
- 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;
- }
- /**
- * @throws \Exception
- */
- protected function getMchListData($submit = false)
- {
- foreach ($this->mch_list as $i => &$mch) {
- $mch['goods_list'] = $this->getGoodsList($mch['goods_list'], $submit);
- if (empty($mch['goods_list'])) {
- throw new \Exception($mch['goods_list']['msg'], 1);
- }
- if ($mch['goods_list']['code'] === 1) {
- throw new \Exception($mch['goods_list']['msg'], 1);
- }
- $is_integral = false;
- if( isset($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_INTEGRAL){
- $mch['name'] = '积分商品';
- $is_integral = true;
- }else{
- $mch['name'] = '平台自营';
- }
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
- $send_type = $send_type ? Json::decode($send_type['value']) : [];
- $send_type_arr = [];
- if (isset($send_type['express']['value']) && $send_type['express']['value']) {
- $send_type_arr[] = 'express';
- }
- if (isset($send_type['shop']['value']) && $send_type['shop']['value']) {
- $send_type_arr[] = 'shop';
- }
- if ((isset($send_type['delivery']['value']) && $send_type['delivery']['value']) && $submit == false) {
- if(is_alipay_platform()){
- $send_type_arr[] = 'delivery';
- try{
- $conf = ADelivery::getConf($this->store_id, get_mini_id(), 1);
- $check = $conf['check'];
- if($check['code'] == 0){
- $send_type_arr[] = 'delivery';
- }
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $this->store_id, get_mini_id(), $e]);
- }
- }else{
- $res = Delivery::getBindAccount();
- // 下单时判断是否可以同城配送
- if (!empty($res['data']['shop_list'])) {
- $store = Store::findOne($this->store_id);
- if ($store->delivery_type > 0) {
- $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) {
- $send_type_arr[] = 'delivery';
- }
- }
- }
- }
- }
- $mch['send_type'] = $send_type_arr;
- $mch['shop_list'] = [];
- !isset($mch['is_shop']) && $mch['is_shop'] = '';
- $total_price = 0;
- $level_price = 0;
- $integral = [
- 'forehead' => 0,
- 'forehead_integral' => 0
- ];
- $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0;
- foreach ($mch['goods_list'] as &$_goods) {
- if (in_array($_goods['product_type'], [1, 2])) {
- $mch['send_type'] = ['shop'];
- }
- if (in_array($_goods['product_type'], [1, 2]) && $submit == true) {
- $res = $this->bookCheckGoodsNum($_goods, $_goods['product_type']);
- if ($res['code'] != 0) {
- throw new \Exception($res['msg'], 1);
- }
- }
- $_goods['form'] = [];
- $total_price += doubleval($_goods['price']);
- if ((int)$_goods['product_type'] === 2) {
- $_goods['level_price'] = sprintf('%.2f', $mch['list']['price']);
- $_goods['price'] = $mch['list']['price'];
- }
- $level_price += doubleval($_goods['level_price']) > 0 ? doubleval($_goods['level_price']) : doubleval($_goods['price']);
- $_goods['integral'] = [
- 'forehead' => doubleval($_goods['resIntegral']['forehead']),
- 'forehead_integral' => doubleval($_goods['resIntegral']['forehead_integral'])
- ];
- $integral['forehead'] += doubleval($_goods['resIntegral']['forehead']);
- $integral['forehead_integral'] += doubleval($_goods['resIntegral']['forehead_integral']);
- unset($_goods['is_form']);
- unset($_goods['form_name']);
- }
- $integral['forehead'] = round($integral['forehead'], 2);
- $mch['total_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $total_price);
- $mch['level_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $level_price);
- $mch['integral'] = !empty($this->verify_card_id) ? [] : $integral;
- if($is_integral){
- //积分兑换商品不能使用优惠券 或 秒杀活动中不允许使用优惠券则所有商品不允许使用优惠券
- $mch['coupon_list'] = [];
- }else{
- $this->getCouponList($mch);
- }
- $mch['express_price'] = 0;
- $mch['offer_rule'] = $this->getOfferRule($mch);
- if(empty(input_params('flag_id'))){
- $mch['is_area'] = 0;
- }
- // 如果是点餐的商品,结构需要变一下
- /*if (get_md_id() && (!empty(input_params('flag_id')) || !empty(input_params('table_num'))) && !$submit) {
- $user_list = [];
- $user_ids = array_unique(array_column($mch['goods_list'], 'user_id'));
- foreach ($user_ids as $user_id) {
- $goods_list = [];
- foreach ($mch['goods_list'] as $item) {
- if ($item['user_id'] == $user_id) {
- $goods_list[] = $item;
- }
- }
- $saas_user = SaasUser::findOne(['mobile' => User::findOne($user_id)->binding]);
- $user_list[] = [
- 'name' => $saas_user->name,
- 'avatar' => $saas_user->avatar,
- 'user_id' => $user_id,
- 'goods_list' => $goods_list
- ];
- }
- $mch['goods_list'] = $user_list;
- }*/
- }
- return $this->mch_list;
- }
- protected function getGoodsList($goods_list, $submit)
- {
- // 获取卡券绑定的运费模板id
- $goodsIds = [];
- foreach ($goods_list as $i => &$item) {
- if(!isset($item['goods_id'])){
- unset($goods_list[$i]);
- continue;
- }
- $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'],
- ]);
- 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 . ' 只允许购买' . $goods->confine_count . $goods->unit, 1);
- }
- }
- $attr_info = $goods->getAttrInfo($attr_id_list);
- //判断是否为云仓商品
- // if (!empty($goods->cloud_goods_id) && !$goods->is_wholesale) {
- // $goods_url = "/goods/getAttrParams";
- // $param = [
- // 'id' => $goods->cloud_goods_id
- // ];
- // //请求接口
- // $domain = (new OptionSetting)->getCloudDomainName();
- // $goodsInfo = cloud_post($domain.$goods_url,$param);
- // $goodsInfo = json_decode($goodsInfo, true);
- // $goodsInfo = $goodsInfo['data'];
- //
- // //如果没有上架或审核未通过则不允许购买
- // if ((int)$goodsInfo['status'] === 0 || (int)$goodsInfo['is_audit'] !== 2) {
- // //商品下架
- // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
- // unset($goods_list[$i]);
- // continue;
- // }
- //
- // //如果使用规格
- // if (!empty($goodsInfo['use_attr']) && (int)$goodsInfo['use_attr'] === 1) {
- // $goods_2 = new Goods();
- // $goods_2->attr = $goods->attr;
- // $res_attr = $goods_2->getAttrGroupList();
- //
- // if ($res_attr) {
- // foreach ($res_attr as &$attr_item) {
- // unset($attr_item->attr_group_id);
- // foreach ($attr_item->attr_list as &$list_item) {
- // unset($list_item->attr_id);
- // }
- // }
- // }
- // //判断规格名称是否一致
- // $res_attr = json_decode(json_encode($res_attr), true);
- // if ($res_attr != $goodsInfo['AttrGroupList']) {
- // //商品下架
- // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
- // unset($goods_list[$i]);
- // continue;
- // }
- // $attrs = json_decode($goodsInfo['attrs'], true);
- // foreach ($attrs as $attr_item) {
- // //如果供货商的商品规格包含购买的
- // if (in_array($attr_item['attr_id'], $attr_id_list)) {
- // //判断库存无 或者 供货商库存小于购买的商品数量
- // if ((int)$attr_item['num'] <= 0) {
- // //商品下架
- // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
- // unset($goods_list[$i]);
- // continue;
- // }
- // }
- // }
- // } else {
- // //如果不使用规格
- // if ((int)$goodsInfo['num'] <= 0 || $goodsInfo['name'] != $goods->name) {
- // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
- // 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['user_id'] = !empty($this->user) ? $this->user->id : 0;
- $item['attr_list'] = $attr_list;
- $item['goods_id'] = $goods->id;
- $item['mch_id'] = $goods->mch_id;
- $item['rate'] = $goods->rate;
- $item['rate_type'] = $goods->rate_type;
- $item['goods_name'] = $goods->name;
- $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic;
- //如果没有使用规格,则使用商品售价 否则使用规格售价
- $item['price'] = sprintf('%.2f', (($goods->use_attr ? $attr_info['price'] : $goods->price) * $item['num']));
- if ($item['new_price'] > 0) {
- $goods->price = $attr_info['price'] = $item['price'] = $item['new_price'];
- }
- $item['single_price'] = !empty($this->verify_card_id) ? 0 : sprintf('%.2f', $attr_info['price']);
- $item['weight'] = $goods->weight;
- if (!empty($this->verify_card_id)) {
- $item['integral'] = 0;
- } else {
- $item['integral'] = $goods->integral ? $goods->integral : 0;
- }
- $item['integral_price'] = $goods->integral_price;
- $item['freight'] = !empty($VerifyCard['freight_id']) ? $VerifyCard['freight_id'] : $goods->freight;
- $item['full_cut'] = $goods->full_cut;
- $item['goods_cat_id'] = $goods->cat_id;
- $item['id'] = $goods->id;
- $item['verify_card_id'] = [];
- $item['type'] = $goods->type;
- $item['delivery_type'] = Json::decode($goods->delivery_type);
- /* if (get_md_id() && empty(input_params('flag_id'))) {
- $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $item['delivery_type'] = Json::decode($md_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, 'is_delete' => 0])
- ->orderBy('full_minus_num ASC')->asArray()->all();
- // 门店逻辑
- $attr = $goods->attr;
- $price = $goods->price;
- // 门店逻辑
- if (!empty($this->md_id)) {
- $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => $this->md_id]);
- if ($md_goods) {
- $attr = $md_goods->attr;
- $price = $md_goods->price;
- }
- }
- if ($item['new_price'] > 0) {
- $mch_attr_id = array_column($item['attr'], 'attr_id');
- sort($mch_attr_id);
- $attr_ = json_decode($attr, true);
- foreach ($attr_ as &$goods_attr_item) {
- $goods_attr_id = array_column($goods_attr_item['attr_list'], 'attr_id');
- sort($goods_attr_id);
- if (!array_diff($goods_attr_id, $mch_attr_id)) {
- $goods_attr_item['price'] = $item['new_price'];
- }
- }
- $attr = json_encode($attr_, JSON_UNESCAPED_UNICODE);
- $price = $item['new_price'];
- $goods->is_level = 0;
- }
- /*if (get_md_id() && empty(input_params('flag_id'))) {
- $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $attr = $md_goods->attr;
- $price = $md_goods->price;
- }
- }*/
- $otherData = [];
- $this->user && $otherData = ['user'=> $this->user];
- $other['form'] = 'cashier';
- $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([
- 'attr' => $attr,
- 'price' => $price,
- 'is_level' => $goods->is_level,
- 'mch_id' => $goods->mch_id,
- 'full_minus' => $GoodsFullMinus,
- 'unit' => $goods->unit,
- 'use_attr' => $goods->use_attr,
- 'md_id' => $this->md_id,
- ], $attrIdArr, $otherData, $item['num']);
- $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'];
- $send_integral_profit = Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, get_store_id(), 'gift', Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, get_store_id(), 'store')['value'])['value'];
- $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, $send_integral_profit);
- $item['give'] = $integralArr['give'];
- $item['resIntegral'] = $integralArr['resIntegral'];
- $goodsIds[] = $goods->id;
- $item['goods_card_list'] = Goods::getGoodsCard($goods->id);
- $item['is_form'] = $goods->is_form;
- $item['form_name'] = $goods->form_name;
- $item['product_type'] = $goods->product_type;
- /*$res = $this->getSceKillGoodsPrice($goods, $attr_id_list, $submit, $item['num']);
- if ($res['code'] === 0) {
- $item['is_level'] = 0;
- if ($res['data'] > 0) {
- $item['price'] = $item['level_price'] = sprintf('%.2f', ($res['data'] * $item['num']));
- }
- } else {
- return $res;
- }*/
- if ($goods->product_type == Goods::GOODS_TYPE_ADOPT) {
- $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods->id,'is_delete' => 0])->one();
- $item['desc'] = $adopt_goods->desc;
- }
- }
- // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题
- return array_merge($goods_list, []);
- }
- //积分计算
- /**
- * @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(), $send_integral_profit = 0)
- {
- $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);
- }
- if ($give <= 0 && $send_integral_profit > 0) {
- $goods_item->give = (int)(intval($goods_item->price) * ($send_integral_profit / 100));
- }
- // 抵扣积分计算
- $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;
- // }
- // }
- // }
- // }
- // 累计
- $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);
- }
- }
- }
- 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 ? 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 getCouponList(&$mch)
- {
- // if (get_md_id()) {
- // $mch['coupon_list'] = [];
- // return;
- // }
- if (!isset($mch['mch_id'])) {
- $mch['mch_id'] = 0;
- }
- $goods_total_price = $mch['total_price'];
- $cat_ids = $this->getCatIdList($mch['goods_list'],$mch['mch_id']);
- $coupon_goods_id = $this->getGoodsIdList($mch['goods_list']);
- $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.is_delete' => 0],
- ['uc.is_use' => 0],
- ['c.mch_id' => $mch['mch_id']],
- ['uc.is_expire' => 0],
- ['uc.user_id' => $this->user_id],
- ['<=', 'c.min_price', $goods_total_price],
- ])
- ->andWhere($mch['plugin_type'] == 6 ? ['c.appoint_type' => 3] : [
- 'OR',
- ['<>', 'c.appoint_type', 3],
- 'c.appoint_type IS NULL'
- ])
- ->select('c.name, c.expire_day, c.id, c.expire_type, c.discount_type, c.discount, c.discount_max_price, uc.id user_coupon_id,c.mch_id,c.rule,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 = 0;
- if ($mch['plugin_type'] == 6) {
- $max_price = $mch['total_price'];
- } else {
- foreach ($mch['goods_list'] as $v) {
- $max_price += $v['price'];
- }
- }
- $new_list = [];
- foreach ($list as $i => $item) {
- if ($item['expire_type'] == 1) {
- if ($item['end_time'] > 0) {
- if (time() > $item['end_time']) {
- continue;
- }
- }
- }
- if ($item['expire_type'] == 2) {
- if (time() > $item['end_time'] || time() < $item['begin_time']) {
- continue;
- }
- }
- $list[$i]['status'] = 0;
- if (isset($item['is_use']) && $item['is_use']) {
- $list[$i]['status'] = 1;
- }
- if (isset($item['is_expire']) && $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']);
- $list[$i]['discount_type'] = $item['discount_type'];
- $list[$i]['discount'] = $item['discount'];
- if (!$item['event']) {
- if ($item['type'] == 2) {
- $list[$i]['event'] = $item['event'] = 3;
- } else {
- $list[$i]['event'] = $item['event'] = 0;
- }
- }
- $list[$i]['event_desc'] = $events[$item['event']] ?: '领券中心';
- $list[$i]['min_price'] = doubleval($item['min_price']);
- if ($list[$i]['discount_type'] == 1) {
- $list[$i]['sub_price'] = round($mch['total_price'] - doubleval($mch['total_price'] * $list[$i]['discount'] / 10), 2);
- if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){
- $list[$i]['sub_price'] = $item['discount_max_price'];
- }
- } else {
- $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) {
- $current = array_intersect($list[$i]['cat_id_list'], $cat_ids);
- if ($current) {
- $goodsAdd = [];
- $price = 0;
- foreach ($current as $v) {
- foreach ($mch['goods_list'] as $v2) {
- if (in_array($v, $v2['cat_id']) && !in_array($v2['goods_id'], $goodsAdd)) {
- $price += $v2['price'];
- array_push($goodsAdd, $v2['goods_id']);
- }
- };
- }
- if ($price < $list[$i]['min_price']) {
- unset($list[$i]);
- continue;
- }
- } else {
- 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) {
- $goodsAdd = [];
- $price = 0;
- foreach ($current as $v) {
- foreach ($mch['goods_list'] as $v2) {
- if ($v == $v2['goods_id'] && !in_array($v2['goods_id'], $goodsAdd)) {
- $price += $v2['price'];
- }
- }
- }
- if ($price < $list[$i]['min_price']) {
- unset($list[$i]);
- continue;
- }
- } else {
- unset($list[$i]);
- continue;
- }
- }
- } else {
- if ($max_price < $list[$i]['min_price']) {
- unset($list[$i]);
- continue;
- }
- }
- $new_list[] = $list[$i];
- }
- if (!empty($this->verify_card_id)) {
- $mch['coupon_list'] = [];
- } else {
- $mch['coupon_list'] = $new_list;
- }
- }
- protected function getCatIdList(&$goods_list,$mch_id = 0)
- {
- $cat_id_list = [];
- foreach ($goods_list as &$goods) {
- 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 getOfferRule($mch)
- {
- $res = [
- 'is_allowed' => 0,
- 'total_price' => 0,
- 'msg' => ''
- ];
- return $res;
- }
- protected function getAddress()
- {
- $saas_user = $this->saas_user;
- $this->address = Address::find()->where([
- 'user_id' => $saas_user->id,
- 'is_delete' => 0,
- ])->asArray()->all();
- return $this->address;
- }
- }
|