| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\pt\order;
- use app\constants\OptionSetting;
- use app\models\Address;
- use app\models\Admin;
- use app\models\AgentGoodsBindGoods;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Cat;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\Form;
- use app\models\FreeDeliveryRules;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\IntegralAppreciationGoods;
- use app\models\IntegralAppreciationUser;
- use app\models\Level;
- use app\models\Mch;
- use app\models\MchGoodsCat;
- use app\models\MdGoods;
- use app\models\OfferPrice;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\PostageRules;
- use app\models\PtActivity;
- use app\models\PtActivityGoods;
- use app\models\PtActivityOrder;
- use app\models\PtActivityOrderDetail;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\SeckillActivityOrderLog;
- use app\models\Shop;
- use app\models\Store;
- use app\models\TerritorialLimitation;
- use app\models\User;
- use app\models\UserCoupon;
- use app\utils\Delivery\Delivery;
- use app\utils\Delivery\Alipay\ADelivery;
- 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;
- public $verify_card_id;
- /** @var User $user */
- protected $user;
- protected $address;
- protected $level;
- protected $integral;
- protected $keyword;
- public $order_type; //1认养商品
- public $goods_count_activity_new_user = [];
- public $pt_activity_id;
- public $pt_number;
- public $head_integral;
- public function rules()
- {
- $rules = [
- ['mch_list', 'required'],
- ['address_id', 'integer'],
- ['verify_card_id', 'integer'],
- ['order_type', 'integer'],
- [['order_type',], 'default', 'value' => 0],
- ['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', 'h5', 'official']],
- [['pt_activity_id', 'pt_number'], 'integer']
- ];
- 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'])['value']
- ];
- parent::afterValidate();
- }
- /**
- * @throws \Exception
- */
- protected function getMchListData($submit = false)
- {
- foreach ($this->mch_list as $i => &$mch) {
- $mch['pt_number'] = $mch['pt_number'] ?? $this->pt_number;
- $mch['goods_list'] = $this->getGoodsList($mch['goods_list']);
- 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);
- }
- //拼团信息检测
- $pt_activity = PtActivity::findOne($this->pt_activity_id);
- if (!$pt_activity) {
- throw new \Exception("活动不存在或已结束");
- }
- $this->head_integral = 0;
- if (!empty($mch['pt_number'])) {
- $pt_order = PtActivityOrder::findOne($mch['pt_number']);
- if ($pt_order) {
- if ((int)$pt_order->is_pay === 0) {
- throw new \Exception("当前团暂未开放");
- }
- //error
- $check_pt_order = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1])
- ->andWhere(['OR', ['id' => $mch['pt_number']], ['pt_number' => $mch['pt_number']]])
- ->select('id, is_winner, user_id')->asArray()->all();
- //判断是否到解散时间
- $end_time = (($pt_activity->split_time * 60 * 60) + ($pt_order->pay_time));
- if ($end_time <= time()) {
- throw new \Exception("当前团已解散");
- }
- //判断是否开奖
- $open = false;
- foreach ($check_pt_order as $item) {
- if ((int)$item['is_winner'] > 0) {
- $open = true;
- }
- if($pt_activity->party_type == 0){
- if ((int)$item['user_id'] === get_user_id()) {
- throw new \Exception("已加入,不可重复操作");
- }
- }
- }
- if ($open) {
- throw new \Exception("当前团已开奖");
- }
- if($pt_activity->party_type == 0){
- if ($pt_activity->party_size <= (count($check_pt_order))) {
- throw new \Exception("当前团已达上限");
- }
- }
- if($pt_activity->party_type == 1){
- $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
- ->andWhere(['OR' , ['po.id' => $mch['pt_number']], ['po.pt_number' => $mch['pt_number']]]);
- $goodsCount = $goodsCountQuery->sum('pod.num');
- if ($pt_activity->party_goods_count <= (int)$goodsCount) {
- throw new \Exception("当前团商品数量已达上限");
- }
- $buyCount = 0;
- foreach ($mch['goods_list'] as $_goods) {
- $buyCount += (int)$_goods['num'];
- if ($pt_activity->party_goods_count < ((int)$goodsCount + $buyCount)) {
- throw new \Exception("当前购买商品数量已超活动限制");
- }
- }
- }
- } else {
- //判断团长是否积分充足
- $head_integral = $pt_activity->head_integral;
- if ($head_integral > 0) {
- if ($head_integral > get_user()->integral) {
- throw new \Exception("开团积分不足");
- }
- $this->head_integral = $head_integral;
- }
- }
- } else {
- //判断团长是否积分充足
- $head_integral = $pt_activity->head_integral;
- if ($head_integral > 0) {
- if ($head_integral > get_user()->integral) {
- throw new \Exception("开团积分不足");
- }
- $this->head_integral = $head_integral;
- }
- }
- if($pt_activity->party_type == 1){
- $buyCount = 0;
- foreach ($mch['goods_list'] as $_goods) {
- $buyCount += (int)$_goods['num'];
- if ($pt_activity->party_goods_count < $buyCount) {
- throw new \Exception("当前购买商品数量已超活动限制" . $pt_activity->party_goods_count);
- }
- }
- } else {
- $buyCount = 0;
- foreach ($mch['goods_list'] as $_goods) {
- $buyCount += (int)$_goods['num'];
- if ($pt_activity->order_goods_limit < $buyCount) {
- throw new \Exception("当前购买商品数量已超活动限制" . $pt_activity->order_goods_limit);
- }
- }
- }
- if ($mch['mch_id'] == 0) {
- $mch['name'] = '平台自营';
- // if ($submit == false) {
- // $mch['form'] = $this->getFormData();
- // } else {
- // $mch['form'] = $this->getForm($mch['form']);
- // }
- $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 = !empty($send_type['value']) ? Json::decode($send_type['value']) : ["express" => ["text" => "快递", "value" => 1]];
- $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';
- // }
- // }
- // }
- // }
- // }
- } else {
- $_mch = Mch::findOne([
- 'store_id' => $this->store_id,
- 'id' => $mch['mch_id'],
- ]);
- if (!$_mch) {
- unset($this->mch_list[$i]);
- continue;
- }
- $send_type_arr = [];
- if ($_mch['express_delivery']) {
- $send_type_arr[] = 'express';
- }
- if ($_mch['self_delivery']) {
- $send_type_arr[] = 'shop';
- }
- $mch['name'] = $_mch->name;
- $mch['form'] = null;
- // $mch['shop_list'] = [(object)[
- // 'address' => $_mch->address,
- // 'distance' => '',
- // 'id' => $_mch->id,
- // 'is_default' => 1,
- // 'latitude' => $_mch->latitude,
- // 'longitude' => $_mch->longitude,
- // 'mobile' => $_mch->tel,
- // 'name' => $_mch->name,
- // ]];
- }
- $mch['send_type'] = $send_type_arr;
- if (in_array('shop', $send_type_arr)) {
- $shopArr = $this->getShopList();
- $mch['is_shop'] = $shopArr['shop'];
- $mch['shop_list'] = $shopArr['list'];
- } else {
- $mch['shop_list'] = [];
- $mch['is_shop'] = '';
- }
- $total_price = 0;
- $level_price = 0;
- $next_level_price = 0;
- $integral = [
- 'forehead' => 0,
- 'forehead_integral' => 0
- ];
- $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0;
- $open = false;
- // foreach ($mch['goods_list'] as &$mch_goods) {
- // $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $mch_goods['goods_id']])
- // ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id')
- // ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => get_store_id()]])
- // ->select('sg.is_use_coupon')->asArray()->one();
- // if ($seckill_activity_goods && (int)$seckill_activity_goods['is_use_coupon'] === 0) {
- // $open = true;
- // }
- // }
- 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);
- // }
- // }
- if ($submit == false) {
- $_goods = array_merge($_goods, [
- 'form' => []
- ]);
- $_goods['form'] = $this->getNewFormData($_goods);
- } else {
- $_goods['form'] = $this->getForm($_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' => 0,
- 'forehead_integral' => 0
- ];
- $integral['forehead'] = 0;
- $integral['forehead_integral'] = 0;
- unset($_goods['is_form']);
- unset($_goods['form_name']);
- }
- // $integral['forehead'] = round($integral['forehead'], 2);
- $mch['total_price'] = sprintf('%.2f', $total_price);
- $mch['level_price'] = sprintf('%.2f', $level_price);
- $mch['diy_send_type'] = [
- [
- 'key' => 'express',
- 'name' => Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'store', '快递配送')['value'] ?: '快递配送')['value']
- ],
- [
- 'key' => 'shop',
- 'name' => Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'store', '自提配送')['value'] ?: '自提配送')['value']
- ],
- [
- 'key' => 'delivery',
- 'name' => Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'store', '同城配送')['value'] ?: '同城配送')['value']
- ]
- ];
- $mch['integral'] = [
- "forehead" => 0,
- "forehead_integral" => 0
- ];
- $mch['coupon_list'] = [];
- $mch['express_price'] = $this->getExpressPrice($mch);
- $mch['offer_rule'] = null;
- // 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)
- {
- $goodsIds = [];
- // $cards = [];
- $goods_integral_amount = 0;
- foreach ($goods_list as $i => &$item) {
- $food_ext_goods = [];
- if ($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;
- }
- // $delivery_rules = DeliveryRules::find()->where([
- // 'id' => $goods->delivery_rules_id,
- // 'is_delete' => 0,
- // 'status' => 1,
- // 'store_id' => get_store_id()
- // ])->select('type, times, days, name')->asArray()->one();
- //
- // if ($delivery_rules) {
- // $days = "下单" . $delivery_rules['days'] . "天后";
- // $delivery_rules['times'] =
- // (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']);
- // $delivery_rules['type'] .= '';
- // }
- // $item['delivery_rules'] = $delivery_rules;
- // 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);
- // }
- // }
- //增值积分活动商品检测
- /* $integralAppreciationGoods = IntegralAppreciationGoods::findOne(['goods_id' => $goods->id, 'is_delete' => 0]);
- if ($integralAppreciationGoods && $integralAppreciationGoods->integral_amount > 0) {
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', get_store_id(), 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
- $integral_custom_name = $integral_appreciation_setting['integral_custom_name'] ?: '增值积分';
- $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $this->user_id]);
- if (!$integralAppreciationUser) {
- return [
- 'code' => 1,
- 'msg' => "用户{$integral_custom_name}数量不足"
- ];
- }
- $goods_integral_amount = bcadd(bcmul($integralAppreciationGoods->integral_amount, $item['num'], 2), $goods_integral_amount, 2);
- if ($goods_integral_amount > $integralAppreciationUser->integral) {
- return [
- 'code' => 1,
- 'msg' => "用户{$integral_custom_name}数量不足"
- ];
- }
- }*/
- $cat_id_arr = GoodsCat::find()->where(['goods_id' => $goods->id, 'is_delete' => 0])->select('cat_id')->column();
- $shop_count = Cat::find()->where(['id' => $cat_id_arr])->andWhere(['>', 'shop_count', 0])->select('shop_count')->min('shop_count') ?: 0;
- if ($shop_count > 0 && $shop_count > $item['num']) {
- return [
- 'code' => 1,
- 'msg' => $goods->name . "起购数量为" . $shop_count,
- ];
- }
- foreach ($attr_id_list as $index => $value) {
- if ($value < 0) {
- unset($attr_id_list[$index]);
- }
- }
- $attr = json_decode($goods->attr, true);
- $attr_info = $goods->getAttrInfo($attr_id_list);
- //获取拼团价格
- $pt_activity_goods = PtActivityGoods::find()->alias('pag')->where(['pag.goods_id' => $goods->id])
- ->leftJoin(['pa' => PtActivity::tableName()], 'pag.activity_id = pa.id')
- ->andWhere(['AND', ['pa.is_delete' => 0], ['>', 'pa.end_time', time()], ['<', 'start_time', time()], ['pag.is_delete' => 0]])
- ->select('pa.id, pa.party_size, pa.party_type, pa.party_goods_count, pa.join_num, pa.start_time, pa.end_time, pag.attr, pag.pt_price, pag.use_attr, pag.sale_num, pag.virtual_sales, pag.activity_id')->asArray()->one();
- if ($pt_activity_goods) {
- $pt_goods_attr = json_decode($pt_activity_goods['attr'], true);
- foreach ($pt_goods_attr as $pt_attr) {
- $pt_attr_id = array_column($pt_attr['attr_list'], 'attr_id');
- sort($pt_attr_id);
- sort($attr_id_list);
- if (empty(array_diff($pt_attr_id, $attr_id_list))) {
- $attr_info['price'] = $pt_attr['pt_price'];
- $goods->price = $pt_activity_goods['pt_price'] = $pt_attr['pt_price'];
- }
- foreach ($attr as &$g_attr) {
- $g_attr_id = array_column($g_attr['attr_list'], 'attr_id');
- sort($g_attr_id);
- if (empty(array_diff($pt_attr_id, $g_attr_id))) {
- $g_attr['price'] = $pt_attr['pt_price'];
- }
- }
- }
- $attr = json_encode($attr);
- $this->pt_activity_id = $pt_activity_goods['activity_id'];
- $join_num = $pt_activity_goods['join_num'];
- if($pt_activity_goods['party_type'] == 0){
- $s_today_time = strtotime(date('Y-m-d'));
- $e_today_time = (strtotime(date('Y-m-d')) + 3600 * 24);
- $join_count = PtActivityOrderDetail::find()->alias('od')
- ->leftJoin(['o' => PtActivityOrder::tableName()], 'od.order_id = o.id')
- ->where(['od.goods_id' => $goods->id, 'o.user_id' => get_user_id(), 'is_pay' => 1])
- ->andWhere(['AND', ['>', 'od.created_at', $s_today_time], ['<', 'od.created_at', $e_today_time]])->select('od.id')->count();
- if ($join_num > 0 && $join_count >= $join_num) {
- return [
- 'code' => 1,
- 'msg' => "今日参加此商品的拼团次数已达上限"
- ];
- }
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => "拼团活动不存在"
- ];
- }
- $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($user_id) ? $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']));
- $item['single_price'] = sprintf('%.2f', $attr_info['price']);
- $item['weight'] = $goods->weight;
- $item['integral'] = 0;
- $item['integral'] = $goods->integral ? $goods->integral : 0;
- $item['freight'] = 0;
- $item['full_cut'] = $goods->full_cut;
- $item['goods_cat_id'] = $goods->cat_id;
- $item['id'] = $goods->id;
- $item['verify_card_id'] = 0;
- $item['type'] = $goods->type;
- $item['delivery_type'] = Json::decode($goods->delivery_type);
- if (in_array('delivery', $item['delivery_type'])) {
- foreach ($item['delivery_type'] as $delivery_index => $delivery_item) {
- if ($delivery_item == 'delivery') {
- unset($item['delivery_type'][$delivery_index]);
- }
- }
- }
- // 当前选择的规格
- $attrIdArr = [];
- foreach ($item['attr_list'] as $attrListItem) {
- $attrIdArr[] = $attrListItem['attr_id'];
- }
- // 门店逻辑
- $price = $goods->price;
- if ($pt_activity_goods) {
- $price = $pt_activity_goods['pt_price'];
- }
- $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([
- 'attr' => $attr,
- 'price' => $price,
- 'is_level' => 0,
- 'mch_id' => $goods->mch_id,
- 'unit' => $goods->unit,
- 'use_attr' => $goods->use_attr,
- 'food_ext_goods' => $food_ext_goods,
- ], $attrIdArr, [], $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['next_level_price'] = sprintf('%.2f', ($res['next_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'] = null;
- $item['is_form'] = $goods->is_form;
- $item['form_name'] = $goods->form_name;
- $item['product_type'] = $goods->product_type;
- }
- // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题
- return array_merge($goods_list, []);
- }
- //秒杀商品处理
- public function getSceKillGoodsPrice($goods, $attr, $submit, $num)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- //秒杀订单
- $sql = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods->id])
- ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id')
- ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => get_store_id()]])
- ->select('sag.id sag_id, sg.id, sg.end_time, sag.attr, sag.use_attr, sag.seckill_num, sag.seckill_price, sag.sale_num, sg.order_limit_num, sg.self_limit_num')->createCommand()->getRawSql();
- $sql = $sql . ' FOR UPDATE';
- $seckill_activity_goods = Yii::$app->db->createCommand($sql)->queryOne();
- $price = 0;
- $id = 0;
- if (!empty($seckill_activity_goods)) {
- $sum = SeckillActivityOrderLog::find()
- ->where(['activity_goods_id' => $seckill_activity_goods['sag_id'], 'store_id' => get_store_id(), 'user_id' => get_user_id(), 'is_delete' => 0])
- ->count('num');
- if ($seckill_activity_goods['self_limit_num'] > 0 && ($sum * 1 + $num) > $seckill_activity_goods['self_limit_num']) {
- throw new \Exception("购买数量超出限购数量");
- }
- if ($seckill_activity_goods['order_limit_num'] > 0 && $num > $seckill_activity_goods['order_limit_num']) {
- throw new \Exception("购买数量超出订单限购数量");
- }
- //如果商品使用规格 且 已经查找到活动
- // if ((int)$goods->use_attr === 1 && !empty($seckill_activity_goods['attr'])) {
- //解码
- $seckill_activity_goods['attr'] = $sag_attrs = json_decode($seckill_activity_goods['attr'], true);
- //获取设置的商品秒杀规格id数组与购物车添加的规格id数组比较
- // $sag_attrs = array_column($sag_attrs, 'attr_list');
- //假设没有对应的规格信息
- $open = false;
- foreach ($sag_attrs as $sag_attr) {
- $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id');
- // $sag_attr['seckill_num'] -= $num;
- //与购物车添加的规格id数组比较
- if (empty(array_diff($sag_attr_id, $attr))) {
- if ((int)$sag_attr['seckill_num'] >= $num) {
- $price = $sag_attr['seckill_price'];
- $id = $seckill_activity_goods['sag_id'];
- } else {
- throw new \Exception("当前秒杀商品库存不足");
- }
- }
- }
- // } else {
- // if ($seckill_activity_goods['seckill_num'] >= ($seckill_activity_goods['sale_num'] + $num)) {
- // $price = $seckill_activity_goods['seckill_price'];
- // $id = $seckill_activity_goods['sag_id'];
- // } else {
- // throw new \Exception("当前秒杀商品库存不足");
- // }
- // }
- if ($submit && $id > 0) {
- $activity_goods = SeckillActivityGoods::findOne($id);
- $activity_goods_attr = json_decode($activity_goods->attr, true);
- foreach ($activity_goods_attr as &$sag_attr) {
- $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id');
- // $sag_attr['seckill_num'] -= $num;
- //与购物车添加的规格id数组比较
- if (empty(array_diff($sag_attr_id, $attr))) {
- $sag_attr['seckill_num'] = $sag_attr['seckill_num'] - $num;
- }
- }
- $activity_goods->attr = json_encode($activity_goods_attr);
- $activity_goods->sale_num = ($activity_goods->sale_num * 1 + $num);
- $activity_goods->seckill_num = ($activity_goods->seckill_num - $num);
- if (!$activity_goods->save()) {
- throw new \Exception(json_encode($activity_goods->errors));
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $price,
- 'id' => $id
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //自定义表单
- protected function getFormData()
- {
- $new_list = [];
- $new_list['is_form'] = Option::get('is_form', $this->store_id, 'store', 0)['value'];
- $form_list = [];
- if ($new_list['is_form'] == 1) {
- $new_list['name'] = Option::get('form_name', $this->store_id, 'store', '表单信息')['value'];
- $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) {
- if ($value['type'] == 'radio') {
- $form_list[$index]['default'] = $v;
- } else {
- $form_list[$index]['default'] = [$v];
- }
- $default_list[$k]['checked'] = true;
- } else {
- $default_list[$k]['checked'] = false;
- }
- }
- $form_list[$index]['default_list'] = $default_list;
- }
- }
- }
- $new_list['list'] = $form_list;
- return $new_list;
- }
- //自定义表单
- protected function getNewFormData($goods)
- {
- $new_list = [];
- $new_list['is_form'] = $goods['is_form'];
- $form_list = [];
- if ($new_list['is_form'] == 1) {
- $new_list['name'] = $goods['form_name'];
- $form_list = Form::find()->where([
- 'store_id' => $this->store_id, 'is_delete' => 0, 'goods_id' => $goods['id']
- ])->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) {
- if ($value['type'] == 'radio') {
- $form_list[$index]['default'] = $v;
- } else {
- $form_list[$index]['default'] = [$v];
- }
- $default_list[$k]['checked'] = true;
- } else {
- $default_list[$k]['checked'] = false;
- }
- }
- $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' => 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优先获取,没有则获取默认地址
- 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;
- }
- }
- //获取支付方式
- public 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']);
- $user_pay_list_json = Option::get('payment_' . get_user_id(), get_store_id(), 'user')['value'];
- if ($user_pay_list_json) {
- $user_pay_list_arr = Json::decode($user_pay_list_json);
- foreach ($pay_type_list as $key => &$value) {
- if (isset($user_pay_list_arr[$key]) && $user_pay_list_arr[$key]['value'] == 0 && $value['value'] == 1) {
- $value['value'] = 0;
- }
- }
- }
- $new_list = [];
- foreach ($pay_type_list as $index => $pay_type_value) {
- if ((is_wechat_platform() || is_h5() || is_app_platform()) && $index == 'wechat' && $pay_type_value['value'] == 1) {
- $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() || is_h5() || is_app_platform()) && $index == 'alipay' && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => '支付宝支付',
- 'payment' => 4,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
- ];
- }
- //如果是微信且商城已配置通联支付进件数据
- if (is_wechat_platform() && $index == Order::PAY_TYPE_KEY_YUNST_WECHAT_PAY && $pay_type_value['value'] == 1) {
- $store = Store::findOne(get_store_id());
- if (!empty($store->cusid)) {
- $new_list[] = [
- 'name' => '通联支付',
- 'payment' => Order::PAY_TYPE_YUNST_WECHAT_PAY,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.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)) {
- $default = '{"wechat":{"value":1}}';
- if (is_alipay_platform()) {
- $default = '{"alipay":{"value":1}}';
- }
- if (is_toutiao_platform()) {
- $default = '{"toutiao":{"value":1}}';
- }
- $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', $default)['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 && is_wechat_platform()) {
- $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 == 'toutiao' && $value['value'] == 1 && is_toutiao_platform()) {
- $pay_type_list[] = [
- 'name' => '线上支付',
- 'payment' => 5,
- '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 || is_alipay_platform()) {
- 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) {
- if (is_wechat_platform()) {
- $pay_type_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if (is_toutiao_platform()) {
- $pay_type_list[] = [
- 'name' => '线上支付',
- 'payment' => 5,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
- ];
- }
- }
- return $pay_type_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(), $send_integral_profit = 0)
- {
- $integral = json_decode($goods_item->integral, true);
- $resIntegral = [
- 'forehead' => 0,
- 'forehead_integral' => 0,
- ];
- // 赠送积分计算
- if ($integral) {
- // // 赠送积分计算
- $give = (int)$integral['give'];
- if (strpos($give, '%') !== false) {
- // 百分比
- $give = trim($give, '%');
- $goods_item->give = (int)(intval($goods_item->price) * ($give / 100));
- } else {
- // 固定积分
- $goods_item->give = (intval($give) * intval($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 {
- // 累计
- $resIntegral['forehead_integral'] = bcmul($store_integral, $goods_item->price);
- if ($goods_item->price > ($forehead * $goods_item->num)) {
- $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
- }
- }
- }
- $user_integral = $this->user->integral;
- $user_integral = floor_num($user_integral, 0);
- if ($this->integral['forehead_integral'] < $user_integral) {
- $resetIntegral = $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 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;
- }
- // 获取门店列表
- protected function getShopList()
- {
- $start = 0;
- $shop_table_name = Shop::tableName();
- $latitude = 0;
- $longitude = 0;
- if ($this->address && isset($this->address['id'])) {
- $address = Address::findOne($this->address['id']);
- if ($address) {
- $latitude = $address->latitude ? $address->latitude : 0;
- $longitude = $address->longitude ? $address->longitude : 0;
- }
- }
- $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as
- distance FROM {$shop_table_name} WHERE ((`store_id`={$this->store_id}) AND (`is_delete`=0)) ";
- if ($this->keyword) {
- $sql .= " AND (`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 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['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']
- ];
- }
- }
- }
- if ($value['type'] == 'checkbox') {
- $formList[$index]['default'] = is_array($value['default']) ? implode(',', $value['default']) : $value['default'];
- }
- }
- }
- return $form;
- }
- protected function goodsCardList()
- {
- $list = [];
- foreach ($this->mch_list as $mch) {
- if($mch['mch_id'] == 0) {
- foreach ($mch['goods_list'] as $goods) {
- if (!empty($goods['goods_list'])) {
- foreach ($goods['goods_list'] as $val) {
- if(!$val['goods_card_list']) {
- $val['goods_card_list'] = [];
- }
- $list = array_merge($list, $val['goods_card_list']);
- }
- }
- }
- }
- }
- return $list;
- }
- /**
- * @param $goods
- * @param $type
- */
- public function bookCheckGoodsNum($goods, $type) {
- // 酒店预约
- if ($type == 1) {
- // $date_book = GoodsBook::findOne(['goods_id' => $goods['id']])->date_book;
- // $date_book = Json::decode($date_book);
- // if (!$date_book) {
- // return [
- // 'code' => 1,
- // 'msg' => '数据异常'
- // ];
- // }
- // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num'));
- //
- // foreach ($goods['book'] as $value) {
- // if ($data_config[$value['date']] < $goods['num']) {
- // return [
- // 'code' => 1,
- // 'msg' => '选中日期' . $value['date'] . '内暂无房源'
- // ];
- // }
- // }
- }
- // 服务预约
- if ($type == 2) {
- $service_book = GoodsBook::findOne(['goods_id' => $goods['id']])->service_book;
- $service_book = Json::decode($service_book);
- if (!$service_book) {
- return [
- 'code' => 1,
- 'msg' => '数据异常'
- ];
- }
- $date_data = $this->getDateByInterval(7);
- $count = 0;
- $new_arr = [
- 'data' => []
- ];
- $order = Order::find()->alias('o')
- ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id')
- ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods['id']])
- ->andWhere(['>', 'o.created_at', strtotime(date("Y-m-d"))])
- ->select('od.attr, od.num')->asArray()->all();
- foreach ($date_data as $index => $datum) {
- $new_arr['data'][$index]['date'] = $datum;
- $new_arr['data'][$index]['time'] = $service_book['data'][0]['time'];
- }
- if (!$new_arr) {
- return [
- 'code' => 1,
- 'msg' => '数据异常'
- ];
- }
- $service_book = $new_arr['data'];
- $service_book_date = array_column($service_book, NULL, 'date');
- $service_book_time = $service_book_date[$goods['service']['date']]['time'];
- $num = 0;
- $price = 0;
- foreach ($order as $item) {
- $item['attr'] = json_decode($item['attr'], true);
- if ($item['attr']['date'] === $goods['service']['date']) {
- if ($goods['service']['time'] === $item['attr']['time']) {
- $num += $item['num'];
- }
- }
- }
- foreach ($service_book_time as $m) {
- foreach ($m['times'] as $n) {
- if ($n['time'] == $goods['service']['time']) {
- if (($goods['num'] + $num) > $n['num']) {
- return [
- 'code' => 1,
- 'msg' => $n['time'] . '时间段内暂不可预约'
- ];
- }
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'price' => $price
- ]
- ];
- }
- /**
- * 计算初始价格
- * @param $order_id
- * @return array
- */
- public static function findPrice($order_id) {
- $order_detail = OrderDetail::findAll(['order_id' => $order_id]);
- $original_price = 0;
- $md_price = 0;
- foreach ($order_detail as $detail) {
- $goods = Goods::findOne($detail['goods_id']);
- if ($goods->use_attr > 0) {
- $attrs = Json::decode($goods->attr);
- // 当前选择的规格
- $attrIdArr = [];
- foreach (Json::decode($detail['attr']) as $attrListItem) {
- $attrIdArr[] = $attrListItem['attr_id'];
- }
- foreach ($attrs as $attr) {
- $attrIds = [];
- foreach ($attr['attr_list'] as $item) {
- $attrIds[] = $item['attr_id'];
- }
- sort($attrIds);
- sort($attrIdArr);
- // 找出当前规格信息
- if (implode($attrIds) === implode($attrIdArr)) {
- $original_price += $attr['price'];
- }
- }
- } else {
- $original_price += $goods->price;
- }
- }
- foreach ($order_detail as $detail) {
- $md_goods = MdGoods::findOne(['goods_id' => $detail['goods_id'], 'md_id' => get_md_id()]);
- $goods = Goods::findOne($detail['goods_id']);
- $goods_attr = $goods->attr;
- if ($md_goods) {
- $goods_attr = $md_goods->attr;
- }
- if ($goods->use_attr == 1) {
- $attrs = Json::decode($goods_attr);
- // 当前选择的规格
- $attrIdArr = [];
- foreach (Json::decode($detail['attr']) as $attrListItem) {
- $attrIdArr[] = $attrListItem['attr_id'];
- }
- foreach ($attrs as $attr) {
- $attrIds = [];
- foreach ($attr['attr_list'] as $item) {
- $attrIds[] = $item['attr_id'];
- }
- sort($attrIds);
- sort($attrIdArr);
- // 找出当前规格信息
- if (implode($attrIds) === implode($attrIdArr)) {
- $md_price += $attr['price'];
- }
- }
- } else {
- if ($md_goods) {
- $md_price += $md_goods->price;
- } else {
- $md_price += $goods->price;
- }
- }
- }
- return [
- 'original_price' => $original_price > 0 ? $original_price : $md_price,
- 'md_price' => $md_price
- ];
- }
- public function getDateByInterval(int $num) :array
- {
- //var_dump($st, $et);die;
- $returnData = [];
- $i = 0;
- do {
- $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d'))));
- $returnData[] = $temp;
- $i++;
- } while ($i < $num);
- return $returnData;
- }
- //判断是否需为代理配送且要上门安装
- public function goodsIsInstall($goods_id) {
- try {
- $goods = Goods::findOne($goods_id);
- $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);
- if ((int)$goodsInfo['code'] === 0) {
- $goodsInfo = $goodsInfo['data'];
- //需要安装
- if ((int)$goodsInfo['send_type'] === 3 && (int)$goodsInfo['is_need_install'] === 1) {
- return 1;
- }
- //不需要安装
- if ((int)$goodsInfo['send_type'] === 3) {
- return 3;
- }
- }
- return 2;
- } catch (\Exception $e) {
- return 2;
- }
- }
- //判断是否存在代理
- public function goodsIsGoodsAgent($address, $goods_id) {
- try {
- if (empty($address) || !is_array($address)) {
- throw new \Exception('缺少地址信息');
- }
- $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0]);
- if ((!empty($goods) && empty($goods->cloud_goods_id) || empty($goods) )) {
- return [
- 'code' => 0,
- 'msg' => '非云仓商品'
- ];
- }
- $goods_agent_admin = AgentGoodsBindGoods::find()->where(['cloud_goods_id' => $goods->cloud_goods_id, 'status' => 1, 'is_delete' => 0])
- ->select('goods_agent_admin_id')->column();
- $where = [
- 'province_id' => $address['province_id'],
- 'type' => 'goods_agent',
- 'is_delete' => 0,
- 'id' => $goods_agent_admin
- ];
- $admin = Admin::findOne(array_merge(
- [
- 'area_level' => 1,
- 'city_id' => $address['city_id'],
- 'district_id' => $address['district_id'],
- ], $where));
- if (!empty($admin)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $admin
- ];
- }
- $admin = Admin::findOne(array_merge(
- [
- 'area_level' => 2,
- 'city_id' => $address['city_id'],
- ], $where));
- if (!empty($admin)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $admin
- ];
- }
- $admin = Admin::findOne(array_merge(
- [
- 'area_level' => 3,
- ], $where));
- if (!empty($admin)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $admin
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '该区域未存在产品代理,无法下单'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- // 检测拼团信息
- public function checkPtActivity($pt_number)
- {
- $pt_order = PtActivityOrder::findOne(['id' => $pt_number]);
- if ($pt_order) {
- if ((int)$pt_order->is_pay === 0) {
- return [
- 'code' => 1,
- 'msg' => "当前团暂未开放"
- ];
- }
- //error
- $check_pt_order = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1])
- ->andWhere(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]])
- ->select('id')->column();
- $pt_activity = PtActivity::findOne($this->pt_activity_id);
- if (!$this->pt_activity_id) {
- return [
- 'code' => 1,
- 'msg' => "活动信息不存在或已结束"
- ];
- }
- if($pt_activity->party_type == 0){
- if ($pt_activity->party_size <= (count($check_pt_order))) {
- return [
- 'code' => 1,
- 'msg' => "当前团已达上限"
- ];
- }
- }
- if($pt_activity->party_type == 1){
- $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
- ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
- $goodsCount = $goodsCountQuery->sum('pod.num');
- if($pt_activity->party_goods_count <= (int)$goodsCount){
- return [
- 'code' => 1,
- 'msg' => "当前团商品数量已达上限"
- ];
- }
- }
- }
- return ['code' => 0, 'msg' => ''];
- }
- }
|