OrderForm.php 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\order;
  8. use app\constants\OptionSetting;
  9. use app\models\ActivityCutPriceGoods;
  10. use app\models\ActivityCutPriceOrder;
  11. use app\models\ActivityNewUser;
  12. use app\models\ActivityNewUserGoods;
  13. use app\models\Address;
  14. use app\models\Attr;
  15. use app\models\AttrGroup;
  16. use app\models\Cart;
  17. use app\models\BusinessCart;
  18. use app\models\Cat;
  19. use app\models\common\CommonGoods;
  20. use app\models\Coupon;
  21. use app\models\CouponAutoSend;
  22. use app\models\FoodCart;
  23. use app\models\FoodFlag;
  24. use app\models\Form;
  25. use app\models\FreeDeliveryRules;
  26. use app\models\Goods;
  27. use app\models\GoodsBook;
  28. use app\models\GoodsCat;
  29. use app\models\GoodsFullMinus;
  30. use app\models\Level;
  31. use app\models\Mch;
  32. use app\models\MchGoodsCat;
  33. use app\models\Md;
  34. use app\models\MdGoods;
  35. use app\models\OfferPrice;
  36. use app\models\Option;
  37. use app\models\Order;
  38. use app\models\OrderDetail;
  39. use app\models\PostageRules;
  40. use app\models\SaasUser;
  41. use app\models\SeckillActivity;
  42. use app\models\SeckillActivityGoods;
  43. use app\models\SeckillActivityOrderLog;
  44. use app\models\Shop;
  45. use app\models\Store;
  46. use app\models\TerritorialLimitation;
  47. use app\models\User;
  48. use app\models\UserCoupon;
  49. use app\models\SaasCoupon;
  50. use app\models\CartOffline;
  51. use app\models\District;
  52. use app\utils\Delivery\Delivery;
  53. use app\utils\OrderNo;
  54. use app\models\VerifyCard;
  55. use app\models\VerifyCardSale;
  56. use app\models\BusinessMember;
  57. use app\models\BusinessMemberOrder;
  58. use app\utils\Tools;
  59. use Yii;
  60. use yii\base\Model;
  61. use yii\helpers\Json;
  62. class OrderForm extends Model
  63. {
  64. public $mch_list;
  65. public $store_list;
  66. public $address_id;
  67. public $_from;
  68. public $longitude;
  69. public $latitude;
  70. public $send_price;
  71. public $take_price;
  72. public $store_id;
  73. public $store;
  74. public $user_id;
  75. public $saas_id;
  76. public $verify_card_id;
  77. /** @var User $user */
  78. protected $user;
  79. protected $address;
  80. protected $level;
  81. protected $integral;
  82. protected $keyword;
  83. public $order_type; //1认养商品
  84. public $goods_count_activity_new_user = [];
  85. public $goods_count_activity_cut_price = [];
  86. public $activity_cut_price_order_id = 0;
  87. public $order_no = '';
  88. public $is_seckill;
  89. public function reInitMchList()
  90. {
  91. if ($this->activity_cut_price_order_id) {
  92. $activityOrder = ActivityCutPriceOrder::findOne($this->activity_cut_price_order_id);
  93. $this->store_list = json_decode($activityOrder['order_mch_list'], true);
  94. if ($activityOrder) {
  95. $this->store_id = $activityOrder->store_id;
  96. }
  97. }
  98. }
  99. public function rules()
  100. {
  101. $rules = [
  102. ['store_list', 'required'],
  103. ['address_id', 'integer'],
  104. ['verify_card_id', 'integer'],
  105. ['order_type', 'integer'],
  106. [['order_type',], 'default', 'value' => 0],
  107. ['store_list', function ($attr, $params) {
  108. if(!is_array($this->store_list)){
  109. $data = Json::decode($this->store_list);
  110. if (!$data) {
  111. $this->addError($attr, "{$attr}数据格式错误。");
  112. }
  113. $this->store_list = $data;
  114. }
  115. }],
  116. ['store_list', function ($attr, $params) {
  117. foreach ($this->store_list as $i => &$store) {
  118. if (!is_array($store['goods_list'])) {
  119. $this->addError($attr, "{$attr}[{$i}]['goods_list']必须是一个数组。");
  120. return;
  121. }
  122. }
  123. }],
  124. [['longitude', 'latitude', '_from'], 'trim'],
  125. [['_from'], 'in', 'range' => ['app', 'mini']],
  126. [['activity_cut_price_order_id', 'is_seckill', 'order_no'], 'safe'],
  127. ];
  128. return $rules;
  129. }
  130. public function afterValidate()
  131. {
  132. //$this->user = User::findOne($this->user_id);
  133. $saas_user = get_saas_user();
  134. $this->user = User::findOne(['binding'=>$saas_user->mobile,'store_id'=>get_store_id(),'is_delete'=>0]);
  135. //$this->level = $this->getLevelData();
  136. $this->address = $this->getAddressData();
  137. $this->integral = [
  138. 'forehead' => 0,
  139. 'forehead_integral' => 0,
  140. 'integration' => Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'store')['value'])['value']
  141. ];
  142. parent::afterValidate();
  143. }
  144. protected function getStoreListData($submit = false)
  145. {
  146. $submit || $this->reInitMchList();
  147. //处理多店铺订单,前端实现比较困难先注释 start
  148. // $goods_list = $this->getGoodsList($this->store_list[0]['goods_list'], $submit, 1);
  149. //
  150. // $store_list = [];
  151. // foreach ($goods_list as $goods_index => $goods_item) {
  152. // $store_list[$goods_index] = ['mch_id' => 0, 'store_id' => $goods_item['store_id']];
  153. // foreach ($goods_item['goods_list'] as $goods_item_) {
  154. // $store_list[$goods_index]['goods_list'][] = ['goods_id' => $goods_item_['goods_id'], 'attr' => $goods_item_['attr_list'], 'num' => $goods_item_['num']];
  155. // }
  156. // }
  157. // $this->store_list = $store_list;
  158. //End
  159. foreach ($this->store_list as $i => &$mch) {
  160. $mch['goods_list'] = $this->getGoodsList($mch['goods_list'], $submit);
  161. if (isset($mch['goods_list']['code'])) {
  162. throw new \Exception($mch['goods_list']['msg'], 1);
  163. }
  164. $shop_count_result = $this->checkCatShopCount($mch['goods_list']);
  165. if ($shop_count_result['code'] !== 0) {
  166. return $shop_count_result;
  167. }
  168. if ($this->store_list[0]['store_id']) {
  169. $storeInfo = Store::find()->where(['id'=>$this->store_list[0]['store_id'],'is_delete'=>0])->asArray()->one();
  170. if(!$storeInfo){
  171. throw new \Exception('商城信息有误', 1);
  172. }
  173. } else {
  174. $storeInfo['name'] = '平台';
  175. }
  176. $mch['name'] = $storeInfo['name'];
  177. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
  178. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
  179. // $send_type = $send_type ? Json::decode($send_type['value']) : [];
  180. $send_type = !empty($send_type['value']) ? Json::decode($send_type['value']) : ["express" => ["text" => "快递", "value" => 1]];
  181. $send_type_arr = [];
  182. if (isset($send_type['express']['value']) && $send_type['express']['value']) {
  183. $send_type_arr[] = 'express';
  184. }
  185. //商盟没有 店铺配送和同城配送
  186. if (isset($send_type['shop']['value']) && $send_type['shop']['value']) {
  187. $send_type_arr[] = 'shop';
  188. }
  189. if ((isset($send_type['delivery']['value']) && $send_type['delivery']['value']) && $submit == false) {
  190. $send_type_arr[] = 'delivery';
  191. }
  192. //商盟没有 店铺配送和同城配送
  193. if (in_array($mch['product_type'], [Goods::GOODS_TYPE_DATE, Goods::GOODS_TYPE_TIME])) {
  194. $send_type_arr = ['shop'];
  195. }
  196. //Yii::error($send_type_arr);
  197. $mch['send_type'] = $send_type_arr;
  198. if (in_array('shop', $send_type_arr)) {
  199. $shopArr = $this->getShopList();
  200. $mch['is_shop'] = $shopArr['shop'];
  201. $mch['shop_list'] = $shopArr['list'];
  202. } else {
  203. $mch['shop_list'] = [];
  204. $mch['is_shop'] = '';
  205. }
  206. $total_price = 0;
  207. $level_price = 0;
  208. $integral = [
  209. 'forehead' => 0,
  210. 'forehead_integral' => 0
  211. ];
  212. $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0;
  213. $open = false;
  214. if ($this->is_seckill) {
  215. foreach ($mch['goods_list'] as &$mch_goods) {
  216. $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $mch_goods['goods_id']])
  217. ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id')
  218. ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => get_store_id()]])
  219. ->select('sg.is_use_coupon')->asArray()->one();
  220. if ($seckill_activity_goods && (int)$seckill_activity_goods['is_use_coupon'] === 0) {
  221. $open = true;
  222. }
  223. }
  224. }
  225. $mch['express_price'] = 0;
  226. foreach ($mch['goods_list'] as &$_goods) {
  227. // if (in_array($_goods['product_type'], [1, 2]) && $submit == true) {
  228. // $res = $this->bookCheckGoodsNum($_goods, $_goods['product_type']);
  229. // if ($res['code'] != 0) {
  230. // throw new \Exception($res['msg'], 1);
  231. // }
  232. // }
  233. if ($submit == false) {
  234. $_goods['form'] = $this->getNewFormData($_goods);
  235. } else {
  236. $_goods['form'] = $this->getForm($_goods['form']);
  237. }
  238. $total_price += doubleval($_goods['price']);
  239. $level_price += doubleval($_goods['level_price']) > 0 ? doubleval($_goods['level_price']) : doubleval($_goods['price']);
  240. $_goods['integral'] = [
  241. 'forehead' => doubleval($_goods['resIntegral']['forehead']),
  242. 'forehead_integral' => doubleval($_goods['resIntegral']['forehead_integral'])
  243. ];
  244. $integral['forehead'] += doubleval($_goods['resIntegral']['forehead']);
  245. $integral['forehead_integral'] += doubleval($_goods['resIntegral']['forehead_integral']);
  246. unset($_goods['is_form']);
  247. unset($_goods['form_name']);
  248. //如果是酒店就检测商品数量
  249. $goods_ = Goods::findOne($_goods['goods_id']);
  250. if ($goods_->product_type == Goods::GOODS_TYPE_DATE) {
  251. //判断是否有库存
  252. $num = Goods::getGoodsNum($goods_)['data'];
  253. if (isset($mch['list'])) {
  254. foreach ($mch['list'] as $iattr) {
  255. $idate = strtotime($iattr['date']);
  256. foreach ($num as $inum) {
  257. $indate = strtotime($inum['date']);
  258. if(($idate == $indate) && ($inum['num'] < $_goods['num'])){
  259. return ['code' => 1, 'msg' => $iattr['date'].'库存不足'];
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. $mch['total_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $total_price);
  267. $mch['level_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $level_price);
  268. $mch['diy_send_type'] = [
  269. [
  270. 'key' => 'express',
  271. 'name' => Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'store', '快递配送')['value'] ?: '快递配送')['value']
  272. ],
  273. [
  274. 'key' => 'shop',
  275. 'name' => Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'store', '自提配送')['value'] ?: '自提配送')['value']
  276. ],
  277. [
  278. 'key' => 'delivery',
  279. 'name' => Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'store', '同城配送')['value'] ?: '同城配送')['value']
  280. ]
  281. ];
  282. $integral['forehead'] = round($integral['forehead'], 2);
  283. if ($integral['forehead'] > $mch['level_price']) {
  284. $integral['forehead'] = 0;
  285. $integral['forehead_integral'] = 0;
  286. }
  287. $mch['integral'] = !empty($this->verify_card_id) ? [] : $integral;
  288. $this->getCouponList($mch);
  289. $mch['express_price'] = $this->getExpressPrice($mch);
  290. if($open){
  291. //积分兑换商品不能使用优惠券 或 秒杀活动中不允许使用优惠券则所有商品不允许使用优惠券
  292. $mch['coupon_list'] = [];
  293. }else{
  294. $this->getCouponList($mch);
  295. }
  296. $mch['offer_rule'] = $this->getOfferRule($mch);
  297. $mch['is_area'] = $this->getTerritorialLimitation($mch);
  298. // 如果是点餐的商品,结构需要变一下
  299. if ( (!empty(input_params('flag_id')) || !empty(input_params('table_num'))) && !$submit) {
  300. $saas_list = [];
  301. $saas_ids = array_unique(array_column($mch['goods_list'], 'saas_id'));
  302. foreach ($saas_ids as $saas_id) {
  303. $goods_list = [];
  304. foreach ($mch['goods_list'] as $item) {
  305. if ($item['saas_id'] == $saas_id) {
  306. $goods_list[] = $item;
  307. }
  308. }
  309. //$saas_user = SaasUser::findOne(['mobile' => User::findOne($user_id)->binding]);
  310. $saas_user = SaasUser::findOne(['id'=>$saas_id]);
  311. $saas_list[] = [
  312. 'name' => $saas_user->name,
  313. 'avatar' => $saas_user->avatar,
  314. 'user_id' => 0,
  315. 'saas_id' => $saas_id,
  316. 'goods_list' => $goods_list
  317. ];
  318. }
  319. $mch['goods_list'] = $saas_list;
  320. }
  321. }
  322. return $this->store_list;
  323. }
  324. protected function getGoodsList($goods_list, $submit, $type = 0)
  325. {
  326. // 获取卡券绑定的运费模板id
  327. $VerifyCardSale = VerifyCardSale::findOne(['id' => $this->verify_card_id]);
  328. $VerifyCard = null;
  329. if ($VerifyCardSale) {
  330. $VerifyCard = VerifyCard::findOne(['id' => $VerifyCardSale['verify_card_id']]);
  331. }
  332. $goodsIds = [];
  333. $cards = [];
  334. $levelDiscount = 10;
  335. $saasInfo = get_saas_user();
  336. $time = time();
  337. // //如果是会员获取会员商品折扣
  338. // $levelInfo = BusinessMemberOrder::find()->where(['is_pay'=>1,'is_delete'=>0,'saas_id'=>$saasInfo['id']])->andWhere([">=","begin_time",$time])->andWhere(["<","end_time",$time])->asArray()->all();
  339. // if(count($levelInfo) > 0){
  340. // $memberIds = $levelType = [];
  341. // foreach($levelInfo as $val){
  342. // if(!in_array($val['member_id'], $memberIds)){
  343. // $memberIds[] = $val['member_id'];
  344. // }
  345. // $levelType[$val['member_id'].'_'.$val['member_type']] = 1000;
  346. // }
  347. // $memberInfo = BusinessMember::find()->where(['id'=>$memberIds,'is_delete'=>0,'status'=>1])->asArray()->all();
  348. // if(count($memberInfo)>0){
  349. // foreach($memberInfo as $val){
  350. // if($val['sale_info']){
  351. // $saleInfo = json_decode($val['sale_info'],true);
  352. // foreach($saleInfo as $v){
  353. // if($levelType[$val['id'].'_'.$v['type']] == 1000){
  354. // $levelType[$val['id'].'_'.$v['type']] = $v['discount'];
  355. // }
  356. // }
  357. // }
  358. // }
  359. // foreach($levelType as $v){
  360. // if( $v < $levelDiscount) $levelDiscount = $v;
  361. // }
  362. // }
  363. // }
  364. $take_price = 0;
  365. $send_price = 0;
  366. foreach ($goods_list as $i => &$item) {
  367. if (isset($item['cart_id'])) {
  368. $cart = BusinessCart::findOne([
  369. // 'store_id' => get_store_id(),
  370. 'id' => $item['cart_id'],
  371. 'is_delete' => 0
  372. ]);
  373. if (!$cart) {
  374. unset($goods_list[$i]);
  375. continue;
  376. }
  377. $item['num'] = $cart->num;
  378. $attr_id_list = (array)Json::decode($cart->attr);
  379. $goods = Goods::findOne($cart->goods_id);
  380. } elseif (isset($item['food_cart_id'])) {
  381. $cart = FoodCart::findOne([
  382. 'id' => $item['food_cart_id'],
  383. 'status' => 1,
  384. 'is_delete' => 0
  385. ]);
  386. if (!$cart) {
  387. unset($goods_list[$i]);
  388. continue;
  389. }
  390. $item['num'] = $cart->num;
  391. $attr_id_list = (array)Json::decode($cart->attr);
  392. $goods = Goods::findOne($cart->goods_id);
  393. $user_id = $cart->user_id;
  394. $saas_id = $cart->saas_id;
  395. } elseif (isset($item['goods_id'])) {
  396. $attr_id_list = [];
  397. foreach ($item['attr'] as $_a) {
  398. if ($_a['attr_id'] > 0) {
  399. array_push($attr_id_list, $_a['attr_id']);
  400. }
  401. }
  402. // $store_id = [0, -1];
  403. // if (get_store_id() > 0) {
  404. // $store_id = get_store_id();
  405. // }
  406. $goods = Goods::findOne([
  407. // 'store_id' => $store_id,
  408. 'id' => $item['goods_id'],
  409. ]);
  410. } else {
  411. unset($goods_list[$i]);
  412. continue;
  413. }
  414. if (!$goods) {
  415. unset($goods_list[$i]);
  416. continue;
  417. }
  418. // $cat_id_arr = GoodsCat::find()->where(['goods_id' => $goods->id, 'is_delete' => 0])->select('cat_id')->column();
  419. // $shop_count = Cat::find()->where(['id' => $cat_id_arr])->andWhere(['>', 'shop_count', 0])->select('shop_count')->min('shop_count') ?: 0;
  420. // if ($shop_count > 0 && $shop_count > $item['num']) {
  421. // return [
  422. // 'code' => 1,
  423. // 'msg' => $goods->name . "起购数量为" . $shop_count,
  424. // ];
  425. // }
  426. //商品限购数量
  427. if (($goods->confine_count && $goods->confine_count > 0)) {
  428. $goodsNum = Goods::getSaasBuyNum($saasInfo, $goods->id);
  429. if ($goodsNum) {
  430. } else {
  431. $goodsNum = 0;
  432. }
  433. $goodsTotalNum = intval($goodsNum + $item['num']);
  434. if ($goodsTotalNum > $goods->confine_count) {
  435. throw new \Exception('商品:' . $goods->name . ' 只允许购买' . $goods->confine_count . $goods->unit, 1);
  436. }
  437. }
  438. //起订数量
  439. if (($goods['order_min_count'] && $goods['order_min_count'] > 0)) {
  440. if ($item['num'] < $goods['order_min_count']) {
  441. if($submit){
  442. throw new \Exception('商品:' . $goods->name . ' 起订数量' . $goods['order_min_count'], 1);
  443. }else{
  444. $item['errors']['order_min_count'] = $goods['order_min_count'];
  445. }
  446. }
  447. }
  448. $attr_info = $goods->getAttrInfo($attr_id_list);
  449. // 排除当面付检测库存
  450. if ((in_array($goods->type, [2, 6]) == false && !in_array($goods->product_type, [1, 2]) && $item['num'] > $attr_info['num']) || $item['num'] <= 0) { //库存不足
  451. unset($goods_list[$i]);
  452. continue;
  453. }
  454. $cards = [];
  455. if (!empty($goods->verify_card_id)) {
  456. $ids = explode(',', $goods->verify_card_id);
  457. $cards = Tools::getVerifyList($ids);
  458. } else {
  459. $cards = [];
  460. }
  461. $store_id = $goods->store_id;
  462. if ($store_id <= 0) {
  463. $store_id = [0, -1];
  464. }
  465. $attr_list = Attr::find()->alias('a')
  466. ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
  467. ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
  468. ->where(['a.id' => $attr_id_list, 'ag.store_id' => $store_id])
  469. ->asArray()->all();
  470. $item['cost_price'] = $attr_info['cost_price'];
  471. $item['user_id'] = !empty($user_id) ? $user_id : 0;
  472. $item['saas_id'] = !empty($saas_id) ? $saas_id : 0;
  473. $item['attr_list'] = $attr_list;
  474. $item['goods_id'] = $goods->id;
  475. $item['store_id'] = $goods->store_id;
  476. $item['mch_id'] = $goods->mch_id;
  477. $item['rate'] = $goods->rate;
  478. $item['rate_type'] = $goods->rate_type;
  479. $item['chain_rate_type'] = $goods->chain_rate_type;
  480. $item['chain_rate'] = $goods->chain_rate;
  481. $item['goods_name'] = $goods->name;
  482. $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic;
  483. //如果没有使用规格,则使用商品售价 否则使用规格售价
  484. $item['price'] = sprintf('%.2f', (($goods->use_attr ? $attr_info['price'] : $goods->price) * $item['num']));
  485. //$item['level_price'] = sprintf('%.2f', ( $item['price'] * ($levelDiscount / 10) ) );
  486. $item['level_price'] = $item['price']; //商盟的会员价 全部通过返利的方式发放
  487. $item['single_price'] = !empty($this->verify_card_id) ? 0 : sprintf('%.2f', $attr_info['price']);
  488. $item['weight'] = $goods->weight;
  489. if (!empty($this->verify_card_id)) {
  490. $item['integral'] = 0;
  491. } else {
  492. $item['integral'] = $goods->integral ? $goods->integral : 0;
  493. }
  494. $item['integral_price'] = $goods->integral_price;
  495. $item['freight'] = !empty($VerifyCard['freight_id']) ? $VerifyCard['freight_id'] : $goods->freight;
  496. $item['full_cut'] = $goods->full_cut;
  497. $item['goods_cat_id'] = $goods->cat_id;
  498. $item['id'] = $goods->id;
  499. $item['verify_card_id'] = $cards;
  500. $item['type'] = $goods->type;
  501. $item['delivery_type'] = Json::decode($goods->delivery_type);
  502. if (in_array($goods->product_type, [Goods::GOODS_TYPE_DATE, Goods::GOODS_TYPE_TIME])) {
  503. $item['delivery_type'] = ['shop'];
  504. }
  505. // // 当前选择的规格
  506. $attrIdArr = [];
  507. foreach ($item['attr_list'] as $attrListItem) {
  508. $attrIdArr[] = $attrListItem['attr_id'];
  509. }
  510. $GoodsFullMinus = GoodsFullMinus::find()
  511. ->select('*')
  512. ->where(['goods_id' => $goods->id, 'is_delete' => 0])
  513. ->orderBy('full_minus_num ASC')->asArray()->all();
  514. // // 门店逻辑
  515. $attr = $goods->attr;
  516. $price = $goods->price;
  517. // if (get_md_id() && empty(input_params('flag_id'))) {
  518. // $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
  519. // if ($md_goods) {
  520. // $attr = $md_goods->attr;
  521. // $price = $md_goods->price;
  522. // }
  523. // }
  524. $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([
  525. 'attr' => $attr,
  526. 'price' => $price,
  527. 'is_level' => $goods->is_level,
  528. 'mch_id' => $goods->mch_id,
  529. 'full_minus' => $GoodsFullMinus,
  530. 'unit' => $goods->unit,
  531. 'use_attr' => $goods->use_attr
  532. ], $attrIdArr, [], $item['num']);
  533. // $item['batch_price_tips'] = $res['batch_price_tips'];
  534. // $item['current_batch_price_tips'] = $res['current_batch_price_tips'];
  535. $item['price'] = sprintf('%.2f', ($res['price'] * $item['num']));
  536. $item['level_price'] = sprintf('%.2f', ($res['level_price'] * $item['num']));
  537. $item['is_level'] = $res['is_level'];
  538. if(isset($this->user->id) && $this->user->id > 0){
  539. $integralArr = $this->getIntegral((object)$item, Option::get(OptionSetting::STORE_INTEGRAL, $goods->store_id, 'gift', Option::get(OptionSetting::STORE_INTEGRAL, $goods->store_id, 'store')['value'])['value'], $goodsIds);
  540. $item['give'] = $integralArr['give'];
  541. $item['resIntegral'] = $integralArr['resIntegral'];
  542. }else{
  543. $item['give'] = 0;
  544. $item['resIntegral'] = ['forehead'=>0,'forehead_integral'=>0];
  545. }
  546. $goodsIds[] = $goods->id;
  547. $item['goods_card_list'] = Goods::getGoodsCard($goods->id);
  548. $item['is_form'] = $goods->is_form;
  549. $item['form_name'] = $goods->form_name;
  550. $item['product_type'] = $goods->product_type;
  551. $item['send_price'] = $item['take_price'] = 0;
  552. if ($store_id > 0) {
  553. $store = Store::findOne($store_id);
  554. $item['send_price'] = bcmul($goods->goods_send_profit * 0.01, $item['level_price'], 2);
  555. $item['take_price'] = bcmul($goods->goods_take_price * 0.01, $item['level_price'], 2);
  556. $saas_user = SaasUser::findOne($saasInfo->id);
  557. $user_league_price = 0;
  558. if ($saas_user) {
  559. $user_league_price = $saas_user->league_price;
  560. }
  561. if(bcadd($take_price, $item['take_price'], 2) > $user_league_price){
  562. $item['take_price'] = 0;
  563. }
  564. if (bcadd($send_price, $item['send_price'], 2) > $store->league_price) {
  565. $item['send_price'] = 0;
  566. }
  567. $send_price += $item['send_price'];
  568. $take_price += $item['take_price'];
  569. }
  570. $res_acnu = $this->getActivityNewUserGoodsPrice($res['price'], $goods, $attr_id_list, $item['num']);
  571. if ($res_acnu['code'] === 0) {
  572. $item['is_level'] = 0;
  573. if ($res_acnu['price'] > 0) {
  574. $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_acnu['price']));
  575. $item['price_str'] = $res_acnu['price_str'];
  576. }
  577. } else {
  578. return $res_acnu;
  579. }
  580. if($this->activity_cut_price_order_id){
  581. $res_accp = $this->getActivityCutPriceGoodsPrice($goods, $attr_id_list, $item['num']);
  582. // var_dump($res_accp);die;
  583. if ($res_accp['code'] === 0) {
  584. $item['is_level'] = 0;
  585. if ($res_accp['price'] > 0) {
  586. $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_accp['price']));
  587. $item['price_str'] = $res_accp['price_str'];
  588. }
  589. } else {
  590. return $res_accp;
  591. }
  592. }
  593. if ($this->is_seckill) {
  594. $res = $this->getSceKillGoodsPrice($goods, $attr_id_list, $submit, $item['num']);
  595. if ($res['code'] === 0) {
  596. $item['is_level'] = 0;
  597. if ($res['data'] > 0) {
  598. $item['price'] = $item['level_price'] = sprintf('%.2f', ($res['data'] * $item['num']));
  599. }
  600. } else {
  601. return $res;
  602. }
  603. }
  604. }
  605. // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题
  606. if ($type) {
  607. $goods_store_id = array_column($goods_list, 'store_id');
  608. $goods_store_id = array_unique($goods_store_id);
  609. $new_mch = [];
  610. foreach ($goods_store_id as $store_id_item) {
  611. foreach ($goods_list as $goods_item) {
  612. if ($goods_item['store_id'] == $store_id_item) {
  613. $new_mch[$store_id_item]['store_id'] = $store_id_item;
  614. $new_mch[$store_id_item]['goods_list'][] = $goods_item;
  615. }
  616. }
  617. }
  618. return array_values($new_mch);
  619. }
  620. return array_merge($goods_list, []);
  621. }
  622. //砍价商品处理
  623. public function getActivityCutPriceGoodsPrice($goods, $attr, $num)
  624. {
  625. try {
  626. $price_str = [];
  627. $activityOrder = ActivityCutPriceOrder::findOne($this->activity_cut_price_order_id);
  628. if(!$activityOrder){
  629. throw new \Exception('砍价活动订单信息不存在');
  630. }
  631. $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $activityOrder->activity_id, 'goods_id' => $goods->id, 'is_delete' => 0]);
  632. if(!$goods_ext){
  633. throw new \Exception('砍价商品信息不存在');
  634. }
  635. $ext_attrs = json_decode($goods_ext['attr'], true);
  636. foreach ($ext_attrs as $ext_attr) {
  637. $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
  638. if (empty(array_diff($ext_attr_id, $attr))) {
  639. if($ext_attr['num'] < $num){
  640. throw new \Exception('砍价商品库存不足');
  641. }
  642. }
  643. }
  644. //全部享受优惠
  645. $price = $activityOrder->pay_price;
  646. $price_str[] = '砍价优惠价¥' . sprintf('%.2f', $price);
  647. return [
  648. 'code' => 0,
  649. 'msg' => '获取成功',
  650. 'price' => $price,
  651. 'price_str' => implode('+', $price_str),
  652. 'id' => $activityOrder->id,
  653. ];
  654. } catch (\Exception $e) {
  655. return [
  656. 'code' => 1,
  657. 'msg' => $e->getMessage()
  658. ];
  659. }
  660. }
  661. //新人专享商品处理
  662. public function getActivityNewUserGoodsPrice($level_price, $goods, $attr, $num)
  663. {
  664. try {
  665. $price = 0;
  666. $price_str = [];
  667. $activity = ActivityNewUser::activityAt($goods->store_id);
  668. $open = false;
  669. if($activity){
  670. $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $goods->id, 'is_delete' => 0]);
  671. if($goods_ext){
  672. $ext_attrs = json_decode($goods_ext['attr'], true);
  673. foreach ($ext_attrs as $ext_attr) {
  674. $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
  675. //与购物车添加的规格id数组比较
  676. if (empty(array_diff($ext_attr_id, $attr))) {
  677. $ext_price = $ext_attr['price'];
  678. $id = $goods_ext['id'];
  679. $open = true;
  680. }
  681. }
  682. if ($open) {
  683. $buy_limit = $activity->buy_limit;
  684. if (($buy_limit && $buy_limit > 0)) {
  685. // $goodsNum = 0;
  686. //部分享受优惠
  687. $user = User::findOne(['binding' => get_saas_user()->mobile, 'store_id' => $goods->store_id, 'is_delete' => 0]);
  688. $goodsNum = intval(Goods::getBuyNum($user, $goods->id));
  689. $diff_num = $buy_limit - $goodsNum;//1 - 1
  690. if ($diff_num > 0) {
  691. $buy_num = $num - $diff_num;
  692. if ($buy_num > 0) {
  693. $price += ($diff_num * $ext_price) + ($buy_num * $level_price);
  694. $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $diff_num;
  695. } else {
  696. $price += $num * $ext_price;
  697. $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num;
  698. }
  699. }
  700. }else{
  701. //全部享受优惠
  702. $price = $ext_price * $num;
  703. $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num;
  704. }
  705. }
  706. }
  707. }
  708. return [
  709. 'code' => 0,
  710. 'msg' => '获取成功',
  711. 'price' => $price,
  712. 'price_str' => implode('+', $price_str),
  713. 'id' => $id
  714. ];
  715. } catch (\Exception $e) {
  716. return [
  717. 'code' => 1,
  718. 'msg' => $e->getMessage()
  719. ];
  720. }
  721. }
  722. //秒杀商品处理
  723. public function getSceKillGoodsPrice($goods, $attr, $submit, $num)
  724. {
  725. $t = \Yii::$app->db->beginTransaction();
  726. try {
  727. //秒杀订单
  728. $sql = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods->id])
  729. ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id')
  730. ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => $goods->store_id]])
  731. ->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();
  732. $sql = $sql . ' FOR UPDATE';
  733. $seckill_activity_goods = Yii::$app->db->createCommand($sql)->queryOne();
  734. $price = 0;
  735. $id = 0;
  736. if (!empty($seckill_activity_goods)) {
  737. $user = User::findOne(['binding' => get_saas_user()->mobile, 'store_id' => $goods->store_id, 'is_delete' => 0]);
  738. $sum = SeckillActivityOrderLog::find()
  739. ->where(['activity_goods_id' => $seckill_activity_goods['sag_id'], 'store_id' => $goods->store_id, 'user_id' => $user->id, 'is_delete' => 0])
  740. ->count('num');
  741. if ($seckill_activity_goods['self_limit_num'] > 0 && ($sum * 1 + $num) > $seckill_activity_goods['self_limit_num']) {
  742. throw new \Exception("购买数量超出限购数量");
  743. }
  744. if ($seckill_activity_goods['order_limit_num'] > 0 && $num > $seckill_activity_goods['order_limit_num']) {
  745. throw new \Exception("购买数量超出订单限购数量");
  746. }
  747. //如果商品使用规格 且 已经查找到活动
  748. // if ((int)$goods->use_attr === 1 && !empty($seckill_activity_goods['attr'])) {
  749. //解码
  750. $seckill_activity_goods['attr'] = $sag_attrs = json_decode($seckill_activity_goods['attr'], true);
  751. //获取设置的商品秒杀规格id数组与购物车添加的规格id数组比较
  752. // $sag_attrs = array_column($sag_attrs, 'attr_list');
  753. //假设没有对应的规格信息
  754. $open = false;
  755. foreach ($sag_attrs as $sag_attr) {
  756. $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id');
  757. // $sag_attr['seckill_num'] -= $num;
  758. //与购物车添加的规格id数组比较
  759. if (empty(array_diff($sag_attr_id, $attr))) {
  760. if ((int)$sag_attr['seckill_num'] >= $num) {
  761. $price = $sag_attr['seckill_price'];
  762. $id = $seckill_activity_goods['sag_id'];
  763. } else {
  764. throw new \Exception("当前秒杀商品库存不足");
  765. }
  766. }
  767. }
  768. // } else {
  769. // if ($seckill_activity_goods['seckill_num'] >= ($seckill_activity_goods['sale_num'] + $num)) {
  770. // $price = $seckill_activity_goods['seckill_price'];
  771. // $id = $seckill_activity_goods['sag_id'];
  772. // } else {
  773. // throw new \Exception("当前秒杀商品库存不足");
  774. // }
  775. // }
  776. if ($submit && $id > 0) {
  777. $activity_goods = SeckillActivityGoods::findOne($id);
  778. $activity_goods_attr = json_decode($activity_goods->attr, true);
  779. foreach ($activity_goods_attr as &$sag_attr) {
  780. $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id');
  781. // $sag_attr['seckill_num'] -= $num;
  782. //与购物车添加的规格id数组比较
  783. if (empty(array_diff($sag_attr_id, $attr))) {
  784. $sag_attr['seckill_num'] = $sag_attr['seckill_num'] - $num;
  785. }
  786. }
  787. $activity_goods->attr = json_encode($activity_goods_attr);
  788. $activity_goods->sale_num = ($activity_goods->sale_num * 1 + $num);
  789. $activity_goods->seckill_num = ($activity_goods->seckill_num - $num);
  790. if (!$activity_goods->save()) {
  791. throw new \Exception(json_encode($activity_goods->errors));
  792. }
  793. }
  794. }
  795. $t->commit();
  796. return [
  797. 'code' => 0,
  798. 'msg' => '获取成功',
  799. 'data' => $price,
  800. 'id' => $id
  801. ];
  802. } catch (\Exception $e) {
  803. $t->rollBack();
  804. return [
  805. 'code' => 1,
  806. 'msg' => $e->getMessage()
  807. ];
  808. }
  809. }
  810. //自定义表单
  811. protected function getFormData()
  812. {
  813. $new_list = [];
  814. $new_list['is_form'] = Option::get('is_form', $this->store_id, 'store', 0)['value'];
  815. $form_list = [];
  816. if ($new_list['is_form'] == 1) {
  817. $new_list['name'] = Option::get('form_name', $this->store_id, 'store', '表单信息')['value'];
  818. $form_list = Form::find()->where([
  819. 'store_id' => $this->store_id, 'is_delete' => 0,
  820. ])->orderBy(['sort' => SORT_ASC])->asArray()->all();
  821. foreach ($form_list as $index => $value) {
  822. if (in_array($value['type'], ['radio', 'checkbox'])) {
  823. $default = str_replace(",", ",", $value['default']);
  824. $list = explode(',', $default);
  825. $default_list = [];
  826. foreach ($list as $k => $v) {
  827. $default_list[$k]['name'] = $v;
  828. if ($k == 0) {
  829. if ($value['type'] == 'radio') {
  830. $form_list[$index]['default'] = $v;
  831. } else {
  832. $form_list[$index]['default'] = [$v];
  833. }
  834. $default_list[$k]['checked'] = true;
  835. } else {
  836. $default_list[$k]['checked'] = false;
  837. }
  838. }
  839. $form_list[$index]['default_list'] = $default_list;
  840. }
  841. }
  842. }
  843. $new_list['list'] = $form_list;
  844. return $new_list;
  845. }
  846. //自定义表单
  847. protected function getNewFormData($goods)
  848. {
  849. $new_list = [];
  850. $new_list['is_form'] = $goods['is_form'];
  851. $form_list = [];
  852. $store_id = $this->store_id;
  853. if ($this->store_id <= 0) {
  854. $store_id = [0, -1];
  855. }
  856. if ($new_list['is_form'] == 1) {
  857. $new_list['name'] = $goods['form_name'];
  858. $form_list = Form::find()->where([
  859. 'store_id' => $store_id, 'is_delete' => 0, 'goods_id' => $goods['id']
  860. ])->orderBy(['sort' => SORT_ASC])->asArray()->all();
  861. foreach ($form_list as $index => $value) {
  862. if (in_array($value['type'], ['radio', 'checkbox'])) {
  863. $default = str_replace(",", ",", $value['default']);
  864. $list = explode(',', $default);
  865. $default_list = [];
  866. foreach ($list as $k => $v) {
  867. $default_list[$k]['name'] = $v;
  868. if ($k == 0) {
  869. if ($value['type'] == 'radio') {
  870. $form_list[$index]['default'] = $v;
  871. } else {
  872. $form_list[$index]['default'] = [$v];
  873. }
  874. $default_list[$k]['checked'] = true;
  875. } else {
  876. $default_list[$k]['checked'] = false;
  877. }
  878. }
  879. $form_list[$index]['default_list'] = $default_list;
  880. }
  881. }
  882. }
  883. $new_list['list'] = $form_list;
  884. return $new_list;
  885. }
  886. protected function getAddress()
  887. {
  888. if (!$this->address) {
  889. if ($this->address_id) {
  890. $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => get_saas_user_id(), 'is_delete' => 0]);
  891. } else {
  892. $this->address = Address::find()->where([
  893. 'user_id' => get_saas_user_id(),
  894. 'is_default' => 1,
  895. 'is_delete' => 0,
  896. ])->limit(1)->one();
  897. }
  898. }
  899. return (object)$this->address;
  900. }
  901. //获取收货地址,有address_id优先获取,没有则获取默认地址
  902. protected function getAddressData()
  903. {
  904. $address = $this->getAddress();
  905. if (isset($address->id)) {
  906. return [
  907. 'id' => $address->id,
  908. 'name' => $address->name,
  909. 'mobile' => $address->mobile,
  910. 'province_id' => $address->province_id,
  911. 'province' => $address->province,
  912. 'city_id' => $address->city_id,
  913. 'city' => $address->city,
  914. 'district_id' => $address->district_id,
  915. 'district' => $address->district,
  916. 'detail' => $address->detail,
  917. 'is_default' => $address->is_default,
  918. 'latitude' => $address->latitude,
  919. 'longitude' => $address->longitude,
  920. ];
  921. } else {
  922. return null;
  923. }
  924. }
  925. //获取支付方式
  926. public function getPayTypeList()
  927. {
  928. $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']);
  929. $pay_type_list = Json::decode($pay_type_list_json['value']);
  930. $new_list = [];
  931. $ok = true;
  932. $is_virtual = false;
  933. // foreach ($this->store_list as $mch) {
  934. // if ($mch['goods_list'][0]['product_type'] == 3) {
  935. // $is_virtual = true;
  936. // }
  937. // if (isset($mch['mch_id']) && $mch['mch_id'] == 0) {
  938. // continue;
  939. // } else {
  940. // $ok = false;
  941. // break;
  942. // }
  943. // }
  944. if (is_wechat_platform() || $this->_from == 'app') {
  945. $new_list[] = [
  946. 'name' => '微信支付',
  947. 'payment' => 1,
  948. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  949. ];
  950. }
  951. //如果是微信且商城已配置通联支付进件数据
  952. if (is_wechat_platform()) {
  953. $store = Store::findOne(get_store_id());
  954. if (!empty($store->cusid)) {
  955. $new_list[] = [
  956. 'name' => '通联支付',
  957. 'payment' => Order::PAY_TYPE_YUNST_WECHAT_PAY,
  958. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  959. ];
  960. }
  961. }
  962. if (is_alipay_platform() || $this->_from == 'app') {
  963. $new_list[] = [
  964. 'name' => '支付宝支付',
  965. 'payment' => 4,
  966. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
  967. ];
  968. }
  969. foreach ($new_list as $index => $item) {
  970. foreach ($pay_type_list as $pay_type_index => $pay_type_item) {
  971. if ($item['payment'] === 1 && $pay_type_index === 'wechat') {
  972. if (intval($pay_type_item['value']) === 0) {
  973. unset($new_list[$index]);
  974. }
  975. }
  976. if ($item['payment'] === 4 && $pay_type_index === 'alipay') {
  977. if (intval($pay_type_item['value']) === 0) {
  978. unset($new_list[$index]);
  979. }
  980. }
  981. if ($item['payment'] === Order::PAY_TYPE_YUNST_WECHAT_PAY && $pay_type_index === Order::PAY_TYPE_KEY_YUNST_WECHAT_PAY) {
  982. if (intval($pay_type_item['value']) === 0) {
  983. unset($new_list[$index]);
  984. }
  985. }
  986. }
  987. }
  988. // if (is_toutiao_platform()) {
  989. // $new_list[] = [
  990. // 'name' => '线上支付',
  991. // 'payment' => 5,
  992. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  993. // ];
  994. // }
  995. // foreach ($pay_type_list as $index => $value) {
  996. // if ($index == 'huodao' && $value['value'] == 1 && $ok && !$is_virtual) {
  997. // $new_list[] = [
  998. // 'name' => '货到付款',
  999. // 'payment' => 2,
  1000. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
  1001. // ];
  1002. // }
  1003. // if ($index == 'friend' && $value['value'] == 1) {
  1004. // $new_list[] = [
  1005. // 'name' => '朋友代付',
  1006. // 'payment' => 7,
  1007. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
  1008. // ];
  1009. // }
  1010. // }
  1011. return array_values($new_list);;
  1012. }
  1013. /**
  1014. * 支付方式
  1015. * @param array $is_payment //支付方式
  1016. * @param array $ignore //忽略的支付方式
  1017. * @return array
  1018. */
  1019. public static function getPayType($is_payment = array(), $ignore = array())
  1020. {
  1021. if (!$is_payment || empty($is_payment)) {
  1022. $default = '{"wechat":{"value":1}}';
  1023. if (is_alipay_platform()) {
  1024. $default = '{"alipay":{"value":1}}';
  1025. }
  1026. if (is_toutiao_platform()) {
  1027. $default = '{"toutiao":{"value":1}}';
  1028. }
  1029. $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', $default)['value']);
  1030. $is_payment = Json::decode($pay_str['value']);
  1031. }
  1032. $pay_type_list = [];
  1033. foreach ($is_payment as $index => $value) {
  1034. if (in_array($index, $ignore)) {
  1035. continue;
  1036. }
  1037. if ($index == 'wechat' && $value['value'] == 1 && is_wechat_platform()) {
  1038. $pay_type_list[] = [
  1039. 'name' => '微信支付',
  1040. 'payment' => 1,
  1041. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png'
  1042. ];
  1043. }
  1044. // if ($index == 'toutiao' && $value['value'] == 1 && is_toutiao_platform()) {
  1045. // $pay_type_list[] = [
  1046. // 'name' => '线上支付',
  1047. // 'payment' => 5,
  1048. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png'
  1049. // ];
  1050. // }
  1051. // if ($index == 'huodao' && $value['value'] == 1) {
  1052. // $pay_type_list[] = [
  1053. // 'name' => '货到付款',
  1054. // 'payment' => 2,
  1055. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-huodao.png'
  1056. // ];
  1057. // }
  1058. if (get_params('_from') == OrderPayDataForm::PAY_FROM_APP || is_alipay_platform()) {
  1059. if ($index == 'alipay' && $value['value'] == 1) {
  1060. $pay_type_list[] = [
  1061. 'name' => '支付宝支付',
  1062. 'payment' => 4,
  1063. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png'
  1064. ];
  1065. }
  1066. }
  1067. // if ($index == 'balance' && $value['value'] == 1) {
  1068. // $pay_type_list[] = [
  1069. // 'name' => '账户余额支付',
  1070. // 'payment' => 3,
  1071. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-balance.png'
  1072. // ];
  1073. // }
  1074. }
  1075. if (!$pay_type_list) {
  1076. if (is_wechat_platform()) {
  1077. $pay_type_list[] = [
  1078. 'name' => '微信支付',
  1079. 'payment' => 1,
  1080. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
  1081. ];
  1082. }
  1083. if (is_toutiao_platform()) {
  1084. $pay_type_list[] = [
  1085. 'name' => '线上支付',
  1086. 'payment' => 5,
  1087. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
  1088. ];
  1089. }
  1090. }
  1091. return $pay_type_list;
  1092. }
  1093. protected function getCouponList(&$mch)
  1094. {
  1095. if (get_md_id()) {
  1096. $mch['coupon_list'] = [];
  1097. return;
  1098. }
  1099. if (!isset($mch['mch_id'])) {
  1100. $mch['mch_id'] = 0;
  1101. }
  1102. $goods_total_price = $mch['total_price'];
  1103. $cat_ids = $this->getCatIdList($mch['goods_list'],$mch['mch_id']);
  1104. $coupon_goods_id = $this->getGoodsIdList($mch['goods_list']);
  1105. $list = SaasCoupon::find()->alias('uc')
  1106. ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
  1107. //->leftJoin(['cas' => CouponAutoSend::tableName()], 'uc.coupon_auto_send_id=cas.id')
  1108. ->where([
  1109. 'AND',
  1110. ['uc.is_delete' => 0],
  1111. ['uc.is_use' => 0],
  1112. ['c.mch_id' => $mch['mch_id']],
  1113. ['uc.is_expire' => 0],
  1114. ['uc.saas_id' => get_saas_user_id()],
  1115. ['<=', 'c.min_price', $goods_total_price],
  1116. ['c.store_id'=>get_store_id()],
  1117. ])
  1118. ->andWhere($mch['plugin_type'] == 6 ? ['c.appoint_type' => 3] : [
  1119. 'OR',
  1120. ['<>', 'c.appoint_type', 3],
  1121. 'c.appoint_type IS NULL'
  1122. ])
  1123. //cas.event, 暂时去掉
  1124. ->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,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list')
  1125. ->asArray()->all();
  1126. // $events = [
  1127. // 0 => '平台发放',
  1128. // 1 => '分享红包',
  1129. // 2 => '购物返券',
  1130. // 3 => '领券中心',
  1131. // ];
  1132. $max_price = 0;
  1133. if ($mch['plugin_type'] == 6) {
  1134. $max_price = $mch['total_price'];
  1135. } else {
  1136. foreach ($mch['goods_list'] as $v) {
  1137. $max_price += $v['price'];
  1138. }
  1139. }
  1140. $new_list = [];
  1141. foreach ($list as $i => $item) {
  1142. if ($item['expire_type'] == 1) {
  1143. if ($item['end_time'] > 0) {
  1144. if (time() > $item['end_time']) {
  1145. continue;
  1146. }
  1147. }
  1148. }
  1149. if ($item['expire_type'] == 2) {
  1150. if (time() > $item['end_time'] || time() < $item['begin_time']) {
  1151. continue;
  1152. }
  1153. }
  1154. $list[$i]['status'] = 0;
  1155. if (isset($item['is_use']) && $item['is_use']) {
  1156. $list[$i]['status'] = 1;
  1157. }
  1158. if (isset($item['is_expire']) && $item['is_expire']) {
  1159. $list[$i]['status'] = 2;
  1160. }
  1161. $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
  1162. $list[$i]['begin_time'] = date('Y.m.d H:i', (int)$item['begin_time']);
  1163. $list[$i]['end_time'] = date('Y.m.d H:i', (int)$item['end_time']);
  1164. $list[$i]['discount_type'] = $item['discount_type'];
  1165. $list[$i]['discount'] = $item['discount'];
  1166. // if (!$item['event']) {
  1167. // if ($item['type'] == 2) {
  1168. // $list[$i]['event'] = $item['event'] = 3;
  1169. // } else {
  1170. // $list[$i]['event'] = $item['event'] = 0;
  1171. // }
  1172. // }
  1173. // $list[$i]['event_desc'] = $events[$item['event']] ?: '领券中心';
  1174. $list[$i]['min_price'] = doubleval($item['min_price']);
  1175. if ($list[$i]['discount_type'] == 1) {
  1176. $list[$i]['sub_price'] = round($mch['total_price'] - doubleval($mch['total_price'] * $list[$i]['discount'] / 10), 2);
  1177. if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){
  1178. $list[$i]['sub_price'] = $item['discount_max_price'];
  1179. }
  1180. } else {
  1181. $list[$i]['sub_price'] = doubleval($item['sub_price']);
  1182. }
  1183. if ($list[$i]['appoint_type'] == 1) {
  1184. $list[$i]['cat_id_list'] = json_decode($list[$i]['cat_id_list']);
  1185. if ($list[$i]['cat_id_list'] != null) {
  1186. $current = array_intersect($list[$i]['cat_id_list'], $cat_ids);
  1187. if ($current) {
  1188. $goodsAdd = [];
  1189. $price = 0;
  1190. foreach ($current as $v) {
  1191. foreach ($mch['goods_list'] as $v2) {
  1192. if (in_array($v, $v2['cat_id']) && !in_array($v2['goods_id'], $goodsAdd)) {
  1193. $price += $v2['price'];
  1194. array_push($goodsAdd, $v2['goods_id']);
  1195. }
  1196. };
  1197. }
  1198. if ($price < $list[$i]['min_price']) {
  1199. unset($list[$i]);
  1200. continue;
  1201. }
  1202. } else {
  1203. unset($list[$i]);
  1204. continue;
  1205. }
  1206. }
  1207. } elseif ($list[$i]['appoint_type'] == 2) {
  1208. $list[$i]['goods_id_list'] = json_decode($list[$i]['goods_id_list']);
  1209. if ($list[$i]['goods_id_list'] != null) {
  1210. $current = array_intersect($list[$i]['goods_id_list'], $coupon_goods_id);
  1211. if ($current) {
  1212. $goodsAdd = [];
  1213. $price = 0;
  1214. foreach ($current as $v) {
  1215. foreach ($mch['goods_list'] as $v2) {
  1216. if ($v == $v2['goods_id'] && !in_array($v2['goods_id'], $goodsAdd)) {
  1217. $price += $v2['price'];
  1218. }
  1219. }
  1220. }
  1221. if ($price < $list[$i]['min_price']) {
  1222. unset($list[$i]);
  1223. continue;
  1224. }
  1225. } else {
  1226. unset($list[$i]);
  1227. continue;
  1228. }
  1229. }
  1230. } else {
  1231. if ($max_price < $list[$i]['min_price']) {
  1232. unset($list[$i]);
  1233. continue;
  1234. }
  1235. }
  1236. $new_list[] = $list[$i];
  1237. }
  1238. if (!empty($this->verify_card_id)) {
  1239. $mch['coupon_list'] = [];
  1240. } else {
  1241. $mch['coupon_list'] = $new_list;
  1242. }
  1243. }
  1244. protected function getCatIdList(&$goods_list,$mch_id = 0)
  1245. {
  1246. $cat_id_list = [];
  1247. foreach ($goods_list as &$goods) {
  1248. if ($goods['goods_cat_id'] == 0) {
  1249. // if ($mch_id > 0) {
  1250. // $goods_cat_list = MchGoodsCat::find()
  1251. // ->select('cat_id')->where([
  1252. // 'goods_id' => $goods['goods_id'],
  1253. // ])->all();
  1254. // }else {
  1255. $goods_cat_list = GoodsCat::find()
  1256. ->select('cat_id')->where([
  1257. 'goods_id' => $goods['goods_id'],
  1258. 'is_delete' => 0,
  1259. ])->all();
  1260. //}
  1261. foreach ($goods_cat_list as $goods_cat) {
  1262. $cat_id_list[] = $goods_cat->cat_id;
  1263. $goods['cat_id'][] = $goods_cat->cat_id;
  1264. }
  1265. } else {
  1266. $cat_id_list[] = $goods['goods_cat_id'];
  1267. $goods['cat_id'][] = $goods['goods_cat_id'];
  1268. }
  1269. $cat_parent_list = Cat::find()->select('parent_id')
  1270. ->andWhere(['id' => $goods['cat_id'], 'is_delete' => 0])->andWhere(['>', 'parent_id', 0])
  1271. ->column();
  1272. $cat_id_list = array_merge($cat_parent_list, $cat_id_list);
  1273. $goods['cat_id'] = array_merge($cat_parent_list, is_array($goods['cat_id']) ? $goods['cat_id'] : []);
  1274. }
  1275. unset($goods);
  1276. return array_unique($cat_id_list);
  1277. }
  1278. protected function getGoodsIdList($goods_list)
  1279. {
  1280. $goods_id_list = [];
  1281. foreach ($goods_list as $goods) {
  1282. $goods_id_list[] = $goods['goods_id'];
  1283. }
  1284. return $goods_id_list;
  1285. }
  1286. protected function getLevelData()
  1287. {
  1288. $level = Level::find()->select([
  1289. 'name', 'level', 'discount',
  1290. ])->where(['level' => $this->user->level, 'store_id' => $this->store_id, 'is_delete' => 0])
  1291. ->asArray()->one();
  1292. return $level;
  1293. }
  1294. //积分计算
  1295. /**
  1296. * @param $goods_item object 重新编写的goods_item
  1297. * @param $store_integral int 商城设置的积分规则
  1298. * @param $goods_id array 已设置积分的商品id数组
  1299. * @return array
  1300. */
  1301. protected function getIntegral($goods_item, $store_integral, $goods_id = array())
  1302. {
  1303. $integral = json_decode($goods_item->integral, true);
  1304. $resIntegral = [
  1305. 'forehead' => 0,
  1306. 'forehead_integral' => 0,
  1307. ];
  1308. if ($integral) {
  1309. // 赠送积分计算
  1310. $give = $integral['give'];
  1311. if (strpos($give, '%') !== false) {
  1312. // 百分比
  1313. $give = trim($give, '%');
  1314. $goods_item->give = (int)($goods_item->price * ($give / 100));
  1315. } else {
  1316. // 固定积分
  1317. $goods_item->give = (int)($give * $goods_item->num);
  1318. }
  1319. // 抵扣积分计算
  1320. $forehead = $integral['forehead'];
  1321. if ($forehead) {
  1322. if (strpos($forehead, '%') !== false) {//百分比积分抵扣计算
  1323. if ($forehead >= 100) {
  1324. $forehead = 100;
  1325. }
  1326. if ($integral['more'] == '1') {//多件累计计算
  1327. $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->price * $store_integral);
  1328. } else {
  1329. if (!in_array($goods_item->id, $goods_id)) { //不允许多件累计 同id商品值计算一次积分抵扣
  1330. $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->single_price * $store_integral);
  1331. }
  1332. }
  1333. } else {
  1334. // if ($integral['more'] == '1') {
  1335. // $resIntegral['forehead_integral'] = (int)($store_integral * $goods_item->price);
  1336. // if ($goods_item->price > ($forehead * $goods_item->num)) {
  1337. // $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
  1338. // }
  1339. // } else {
  1340. // if (!in_array($goods_item->id, $goods_id)) {
  1341. // $goodsPrice = $goods_item->single_price;
  1342. // $resIntegral['forehead_integral'] = (int)($store_integral * $goodsPrice);
  1343. // if ($goodsPrice > $forehead) {
  1344. // if ($forehead > 0) {
  1345. // $resIntegral['forehead_integral'] = (int)($forehead * $store_integral);
  1346. // } else {
  1347. // $resIntegral['forehead_integral'] = 0;
  1348. // }
  1349. // }
  1350. // }
  1351. // }
  1352. // 累计
  1353. $resIntegral['forehead_integral'] = bcmul($store_integral, $goods_item->price);
  1354. if ($goods_item->price > ($forehead * $goods_item->num)) {
  1355. $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
  1356. }
  1357. }
  1358. }
  1359. if ($this->integral['forehead_integral'] < $this->user->integral) {
  1360. $resetIntegral = $this->user->integral - $this->integral['forehead_integral'];
  1361. $resIntegral['forehead_integral'] = $resIntegral['forehead_integral'] >= $resetIntegral ? $resetIntegral : $resIntegral['forehead_integral'];
  1362. $resIntegral['forehead'] = $store_integral ? sprintf("%.2f", ($resIntegral['forehead_integral'] / $store_integral)) : 0;
  1363. $this->integral['forehead_integral'] += $resIntegral['forehead_integral'];
  1364. $this->integral['forehead'] += $resIntegral['forehead'];
  1365. } else {
  1366. $resIntegral['forehead_integral'] = 0;
  1367. $resIntegral['forehead'] = 0;
  1368. }
  1369. }
  1370. return [
  1371. 'resIntegral' => $resIntegral,
  1372. 'give' => ($goods_item->give)?$goods_item->give:0
  1373. ];
  1374. }
  1375. public function getExpressPrice($mch)
  1376. {
  1377. $expressPrice = 0;
  1378. if ($this->address) {
  1379. $address = $this->address;
  1380. //先计算单品满件包邮和满额包邮
  1381. $resGoodsList = Goods::cutFull($mch['goods_list']);
  1382. //再通过运费规则计算运费
  1383. $store_id = $mch['store_id'] ?: $this->store_id;
  1384. if ($store_id <= 0) {
  1385. $store_id = [0, -1];
  1386. }
  1387. $expressPrice = PostageRules::getExpressPriceMore($store_id, $address['city_id'], $resGoodsList, $address['province_id']);
  1388. }
  1389. $expressPrice = $this->getFreeDeliveryRules($mch, $expressPrice);
  1390. return $expressPrice >= 0 ? $expressPrice : 0;
  1391. }
  1392. /**
  1393. * 计算打包费
  1394. * @param $mch
  1395. * @return int
  1396. */
  1397. protected function getDabaoPrice($mch)
  1398. {
  1399. $goods_list = $mch['goods_list'];
  1400. $dabao_price = 0;
  1401. foreach ($goods_list as $value) {
  1402. $dabao_price += $value['dabao_price'];
  1403. }
  1404. return $dabao_price >= 0 ? $dabao_price : 0;
  1405. }
  1406. // 获取门店列表
  1407. protected function getShopList()
  1408. {
  1409. $start = 0;
  1410. $shop_table_name = Shop::tableName();
  1411. $latitude = 0;
  1412. $longitude = 0;
  1413. if ($this->address && isset($this->address['id'])) {
  1414. $address = Address::findOne($this->address['id']);
  1415. if ($address) {
  1416. $latitude = $address->latitude ? $address->latitude : 0;
  1417. $longitude = $address->longitude ? $address->longitude : 0;
  1418. }
  1419. }
  1420. $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
  1421. distance FROM {$shop_table_name} WHERE ((`store_id`={$this->store_id}) AND (`is_delete`=0)) ";
  1422. if ($this->keyword) {
  1423. $sql .= " AND (`name` like '%{$this->keyword}%') ";
  1424. }
  1425. $sql .= "ORDER BY `distance` LIMIT {$start},30";
  1426. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1427. $shop = null;
  1428. foreach ($list as $index => $item) {
  1429. if ($item['is_default'] == 1) {
  1430. $shop = $item;
  1431. }
  1432. $list[$index]['distance'] = round($item['distance']/1000, 2).'km';
  1433. }
  1434. return [
  1435. 'list' => $list,
  1436. 'shop' => $shop
  1437. ];
  1438. }
  1439. // 获取起送规则
  1440. protected function getOfferRule($mch)
  1441. {
  1442. $res = [
  1443. 'is_allowed' => 0,
  1444. 'total_price' => 0,
  1445. 'msg' => ''
  1446. ];
  1447. if (isset($mch['mch_id']) && $mch['mch_id'] > 0) {
  1448. $res['msg'] = '商户不支持起送规则';
  1449. return $res;
  1450. }
  1451. if (!$this->address) {
  1452. $res['msg'] = '请选择收货地址';
  1453. return $res;
  1454. }
  1455. $offerRule = OfferPrice::findOne(['store_id' => get_store_id(),'is_delete' => 0]);
  1456. if (!$offerRule) {
  1457. $res['msg'] = '起送规则不存在';
  1458. return $res;
  1459. }
  1460. if ($offerRule->is_enable == 0) {
  1461. $res['msg'] = '起送规则未开启';
  1462. return $res;
  1463. }
  1464. $ruleList = Json::decode($offerRule->detail);
  1465. $res['total_price'] = $offerRule->price;
  1466. if (is_array($ruleList)) {
  1467. foreach ($ruleList as $value) {
  1468. foreach ($value['province_list'] as $item) {
  1469. if (isset($item['districtAll'])) {
  1470. if ($item['id'] == $this->address['city_id']) {
  1471. $res['total_price'] = $value['offer_price'];
  1472. }
  1473. } else if (isset($item['cityAll'])){
  1474. if ($item['id'] == $this->address['province_id']) {
  1475. $res['total_price'] = $value['offer_price'];
  1476. }
  1477. } else {
  1478. if ($item['id'] == $this->address['district_id']) {
  1479. $res['total_price'] = $value['offer_price'];
  1480. }
  1481. }
  1482. }
  1483. }
  1484. }
  1485. if ($mch['total_price'] >= $res['total_price']) {
  1486. $res['is_allowed'] = 0;
  1487. } else {
  1488. $res['is_allowed'] = 1;
  1489. }
  1490. $value = round($res['total_price'] - $mch['total_price'], 2);
  1491. $res['msg'] = "自营商品,还差{$value}元起送";
  1492. return $res;
  1493. }
  1494. protected function getTerritorialLimitation($mch)
  1495. {
  1496. $isArea = 0;
  1497. if (isset($mch['mch_id']) && $mch['mch_id'] > 0) {
  1498. return $isArea;
  1499. }
  1500. if ($this->address) {
  1501. $area = TerritorialLimitation::findOne([
  1502. 'store_id' => $this->store_id,
  1503. 'is_delete' => 0,
  1504. 'is_enable' => 1,
  1505. ]);
  1506. if ($area) {
  1507. $city_id = []; //限制的地区ID
  1508. $detail = json_decode($area->detail);
  1509. if (!is_array($detail)) {
  1510. $detail = [];
  1511. }
  1512. foreach ($detail as $key => $value) {
  1513. foreach ($value->province_list as $key2 => $value2) {
  1514. $city_id[] = $value2->id;
  1515. }
  1516. }
  1517. $addressArr = [
  1518. $this->address['province_id'],
  1519. $this->address['city_id'],
  1520. $this->address['district_id']
  1521. ];
  1522. $addressArray = array_intersect($addressArr, $city_id);
  1523. if (empty($addressArray)) {
  1524. $isArea = 1;
  1525. }
  1526. }
  1527. }
  1528. return $isArea;
  1529. }
  1530. // 包邮规则
  1531. protected function getFreeDeliveryRules($mch, $expressPrice)
  1532. {
  1533. if ($expressPrice == 0) {
  1534. return $expressPrice;
  1535. }
  1536. $free = FreeDeliveryRules::find()->where(['store_id' => $mch['store_id'], 'is_delete' => 0])->asArray()->all();
  1537. foreach ($free as $k => $v) {
  1538. $city = json_decode($v['city'], true);
  1539. foreach ($city as $v1) {
  1540. if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) {
  1541. $expressPrice = 0;
  1542. break;
  1543. }
  1544. }
  1545. }
  1546. return $expressPrice;
  1547. }
  1548. // 获取用户填写的自定义表单
  1549. protected function getForm(&$form)
  1550. {
  1551. if ($form['is_form'] == 1) {
  1552. $formList = &$form['list'];
  1553. foreach ($formList as $index => $value) {
  1554. if ($value['required'] == 1) {
  1555. if (in_array($value['type'], ['radio', 'checkbox'])) {
  1556. $is_true = false;
  1557. foreach ($value['default_list'] as $k => $v) {
  1558. if ($v['checked'] == true) {
  1559. $is_true = true;
  1560. }
  1561. }
  1562. if (!$is_true) {
  1563. return [
  1564. 'code' => 1,
  1565. 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
  1566. 'name' => $value['name']
  1567. ];
  1568. }
  1569. } else {
  1570. if (empty($value['default']) && strlen($value['default']) == 0) {
  1571. return [
  1572. 'code' => 1,
  1573. 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
  1574. 'name' => $value['name']
  1575. ];
  1576. }
  1577. }
  1578. }
  1579. if ($value['type'] == 'checkbox') {
  1580. $formList[$index]['default'] = is_array($value['default']) ? implode(',', $value['default']) : $value['default'];
  1581. }
  1582. }
  1583. }
  1584. return $form;
  1585. }
  1586. protected function goodsCardList()
  1587. {
  1588. $list = [];
  1589. foreach ($this->mch_list as $mch) {
  1590. if($mch['mch_id'] == 0) {
  1591. foreach ($mch['goods_list'] as $goods) {
  1592. if (!empty($goods['goods_list'])) {
  1593. foreach ($goods['goods_list'] as $val) {
  1594. if(!$val['goods_card_list']) {
  1595. $val['goods_card_list'] = [];
  1596. }
  1597. $list = array_merge($list, $val['goods_card_list']);
  1598. }
  1599. }
  1600. }
  1601. }
  1602. }
  1603. return $list;
  1604. }
  1605. public function getDateByInterval(int $num) :array
  1606. {
  1607. //var_dump($st, $et);die;
  1608. $returnData = [];
  1609. $i = 0;
  1610. do {
  1611. $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d'))));
  1612. $returnData[] = $temp;
  1613. $i++;
  1614. } while ($i < $num);
  1615. return $returnData;
  1616. }
  1617. /**
  1618. * @param $goods
  1619. * @param $type
  1620. */
  1621. public function bookCheckGoodsNum($goods, $type)
  1622. {
  1623. // 酒店预约
  1624. if ($type == 1) {
  1625. // $date_book = GoodsBook::findOne(['goods_id' => $goods['id']])->date_book;
  1626. // $date_book = Json::decode($date_book);
  1627. // if (!$date_book) {
  1628. // return [
  1629. // 'code' => 1,
  1630. // 'msg' => '数据异常'
  1631. // ];
  1632. // }
  1633. // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num'));
  1634. //
  1635. // foreach ($goods['book'] as $value) {
  1636. // if ($data_config[$value['date']] < $goods['num']) {
  1637. // return [
  1638. // 'code' => 1,
  1639. // 'msg' => '选中日期' . $value['date'] . '内暂无房源'
  1640. // ];
  1641. // }
  1642. // }
  1643. }
  1644. // 服务预约
  1645. if ($type == 2) {
  1646. $service_book = GoodsBook::findOne(['goods_id' => $goods['id']])->service_book;
  1647. $service_book = Json::decode($service_book);
  1648. if (!$service_book) {
  1649. return [
  1650. 'code' => 1,
  1651. 'msg' => '数据异常'
  1652. ];
  1653. }
  1654. $service_book = $service_book['data'];
  1655. $service_book_date = array_column($service_book, NULL, 'date');
  1656. $service_book_time = $service_book_date[$goods['service']['date']]['time'];
  1657. foreach ($service_book_time as $m) {
  1658. foreach ($m['times'] as $n) {
  1659. if ($n['time'] == $goods['service']['time']) {
  1660. if ($goods['num'] > $n['num']) {
  1661. return [
  1662. 'code' => 1,
  1663. 'msg' => $n['time'] . '时间段内暂不可预约'
  1664. ];
  1665. }
  1666. }
  1667. }
  1668. }
  1669. }
  1670. return [
  1671. 'code' => 0,
  1672. 'msg' => 'success'
  1673. ];
  1674. }
  1675. /**
  1676. * 计算初始价格
  1677. * @param $order_id
  1678. * @return array
  1679. */
  1680. public static function findPrice($order_id)
  1681. {
  1682. $order_detail = OrderDetail::findAll(['order_id' => $order_id]);
  1683. $original_price = 0;
  1684. foreach ($order_detail as $detail) {
  1685. $goods = Goods::findOne($detail['goods_id']);
  1686. if ($goods->use_attr > 0) {
  1687. $attrs = Json::decode($goods->attr);
  1688. // 当前选择的规格
  1689. $attrIdArr = [];
  1690. foreach (Json::decode($detail['attr']) as $attrListItem) {
  1691. $attrIdArr[] = $attrListItem['attr_id'];
  1692. }
  1693. foreach ($attrs as $attr) {
  1694. $attrIds = [];
  1695. foreach ($attr['attr_list'] as $item) {
  1696. $attrIds[] = $item['attr_id'];
  1697. }
  1698. sort($attrIds);
  1699. sort($attrIdArr);
  1700. // 找出当前规格信息
  1701. if (implode($attrIds) === implode($attrIdArr)) {
  1702. $original_price += $attr['price'];
  1703. }
  1704. }
  1705. } else {
  1706. $original_price += $goods->price;
  1707. }
  1708. }
  1709. return [
  1710. 'original_price' => $original_price,
  1711. 'md_price' => $original_price
  1712. ];
  1713. }
  1714. //分类起购数量 修正为当前分类下所有商品数量未达到数量后警告
  1715. private function checkCatShopCount($goodsList)
  1716. {
  1717. $new_goods_list = [];
  1718. foreach ($goodsList as $item) {
  1719. $cat_id_arr = GoodsCat::find()->where(['goods_id' => $item['goods_id'], 'is_delete' => 0])->select('cat_id')->column();
  1720. foreach ($cat_id_arr as $goods_cat_id) {
  1721. $open = true;
  1722. foreach ($new_goods_list as $new_goods_item) {
  1723. if ($new_goods_item['cat_id'] === $goods_cat_id) {
  1724. $new_goods_list[$goods_cat_id]['goods_list'][] = [
  1725. 'goods_id' => $item['goods_id'],
  1726. 'num' => $item['num']
  1727. ];
  1728. $open = false;
  1729. }
  1730. }
  1731. if ($open) {
  1732. $new_goods_list[$goods_cat_id]['cat_id'] = $goods_cat_id;
  1733. $cat = Cat::find()->where(['id' => $goods_cat_id])
  1734. ->select('shop_count, name')->asArray()->one();
  1735. $new_goods_list[$goods_cat_id]['shop_count'] = $cat['shop_count'] ?: 0;
  1736. $new_goods_list[$goods_cat_id]['name'] = $cat['name'] ?: '';
  1737. $new_goods_list[$goods_cat_id]['goods_list'][] = [
  1738. 'goods_id' => $item['goods_id'],
  1739. 'num' => $item['num']
  1740. ];
  1741. }
  1742. }
  1743. }
  1744. foreach ($new_goods_list as $index => $goods_item) {
  1745. if ($goods_item['shop_count'] > 0) {
  1746. $goods_num_arr = array_column($goods_item['goods_list'], 'num');
  1747. if (array_sum($goods_num_arr) < $goods_item['shop_count']) {
  1748. return [
  1749. 'code' => 1,
  1750. 'msg' => '分类' . $goods_item['name'] . "起购数量为" . $goods_item['shop_count'] . ',当前购买数量为' . array_sum($goods_num_arr),
  1751. ];
  1752. }
  1753. }
  1754. }
  1755. return [
  1756. 'code' => 0,
  1757. 'msg' => '检测起购数量完成',
  1758. ];
  1759. }
  1760. }