MchOrderForm.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\order;
  8. use app\constants\OptionSetting;
  9. use app\models\Address;
  10. use app\models\Attr;
  11. use app\models\AttrGroup;
  12. use app\models\Cat;
  13. use app\models\Coupon;
  14. use app\models\CouponAutoSend;
  15. use app\models\Goods;
  16. use app\models\GoodsCat;
  17. use app\models\GoodsFullMinus;
  18. use app\models\Level;
  19. use app\models\MchGoodsCat;
  20. use app\models\MdGoods;
  21. use app\models\Option;
  22. use app\models\SaasUser;
  23. use app\models\SeckillActivity;
  24. use app\models\SeckillActivityGoods;
  25. use app\models\Store;
  26. use app\models\User;
  27. use app\models\UserCoupon;
  28. use app\plugins\adopt\models\AdoptGoods;
  29. use app\utils\Delivery\Alipay\ADelivery;
  30. use app\utils\Delivery\Delivery;
  31. use yii\base\Model;
  32. use yii\helpers\Json;
  33. /**
  34. * 收银台订单生成结算模块
  35. * User: hankaige
  36. * DATE TIME: 2022/12/7 09:56
  37. */
  38. class MchOrderForm extends Model
  39. {
  40. public $hanging_order_id = 0;// 如果有挂单ID的话 要处理挂单的信息
  41. public $mch_list;
  42. public $user_id;
  43. public $md_id;
  44. public $store_id;
  45. public $address_id;
  46. protected $user;
  47. protected $level;
  48. protected $address;
  49. protected $integral;
  50. protected $saas_user;
  51. public function rules()
  52. {
  53. $rules = [
  54. [['mch_list','store_id'], 'required'],
  55. [['hanging_order_id','user_id','store_id', 'address_id', 'md_id'], 'integer'],
  56. ['mch_list', function ($attr, $params) {
  57. $data = Json::decode($this->mch_list);
  58. if (!$data) {
  59. $this->addError($attr, "{$attr}数据格式错误。");
  60. }
  61. $this->mch_list = $data;
  62. }],
  63. ];
  64. return $rules;
  65. }
  66. // 走过验证后 查询相应数据
  67. public function afterValidate()
  68. {
  69. $this->user = User::findOne($this->user_id);
  70. $this->level = $this->getLevelData();
  71. $this->integral = [
  72. 'forehead' => 0,
  73. 'forehead_integral' => 0,
  74. 'integration' => Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'store')['value'])['value']
  75. ];
  76. $this->saas_user = SaasUser::findOne(['mobile' => $this->user->binding]);
  77. parent::afterValidate();
  78. }
  79. protected function getLevelData()
  80. {
  81. $level = Level::find()->select([
  82. 'name', 'level', 'discount',
  83. ])->where(['level' => $this->user->level, 'store_id' => $this->store_id, 'is_delete' => 0])
  84. ->asArray()->one();
  85. return $level;
  86. }
  87. /**
  88. * @throws \Exception
  89. */
  90. protected function getMchListData($submit = false)
  91. {
  92. foreach ($this->mch_list as $i => &$mch) {
  93. $mch['goods_list'] = $this->getGoodsList($mch['goods_list'], $submit);
  94. if (empty($mch['goods_list'])) {
  95. throw new \Exception($mch['goods_list']['msg'], 1);
  96. }
  97. if ($mch['goods_list']['code'] === 1) {
  98. throw new \Exception($mch['goods_list']['msg'], 1);
  99. }
  100. $is_integral = false;
  101. if( isset($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_INTEGRAL){
  102. $mch['name'] = '积分商品';
  103. $is_integral = true;
  104. }else{
  105. $mch['name'] = '平台自营';
  106. }
  107. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
  108. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
  109. $send_type = $send_type ? Json::decode($send_type['value']) : [];
  110. $send_type_arr = [];
  111. if (isset($send_type['express']['value']) && $send_type['express']['value']) {
  112. $send_type_arr[] = 'express';
  113. }
  114. if (isset($send_type['shop']['value']) && $send_type['shop']['value']) {
  115. $send_type_arr[] = 'shop';
  116. }
  117. if ((isset($send_type['delivery']['value']) && $send_type['delivery']['value']) && $submit == false) {
  118. if(is_alipay_platform()){
  119. $send_type_arr[] = 'delivery';
  120. try{
  121. $conf = ADelivery::getConf($this->store_id, get_mini_id(), 1);
  122. $check = $conf['check'];
  123. if($check['code'] == 0){
  124. $send_type_arr[] = 'delivery';
  125. }
  126. } catch (\Exception $e) {
  127. \Yii::error([__METHOD__, $this->store_id, get_mini_id(), $e]);
  128. }
  129. }else{
  130. $res = Delivery::getBindAccount();
  131. // 下单时判断是否可以同城配送
  132. if (!empty($res['data']['shop_list'])) {
  133. $store = Store::findOne($this->store_id);
  134. if ($store->delivery_type > 0) {
  135. $shop_list = $res['data']['shop_list'];
  136. $delivery_arr = array_column($shop_list, 'delivery_id');
  137. $delivery_arr[] = 'TEST';
  138. $delivery_arr_id = array_column($shop_list, null, 'delivery_id');
  139. if (in_array(Delivery::$deliveryIdArr[$store->delivery_type], $delivery_arr) && $delivery_arr_id[Delivery::$deliveryIdArr[$store->delivery_type]]['audit_result'] == 0) {
  140. $send_type_arr[] = 'delivery';
  141. }
  142. }
  143. }
  144. }
  145. }
  146. $mch['send_type'] = $send_type_arr;
  147. $mch['shop_list'] = [];
  148. !isset($mch['is_shop']) && $mch['is_shop'] = '';
  149. $total_price = 0;
  150. $level_price = 0;
  151. $integral = [
  152. 'forehead' => 0,
  153. 'forehead_integral' => 0
  154. ];
  155. $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0;
  156. foreach ($mch['goods_list'] as &$_goods) {
  157. if (in_array($_goods['product_type'], [1, 2])) {
  158. $mch['send_type'] = ['shop'];
  159. }
  160. if (in_array($_goods['product_type'], [1, 2]) && $submit == true) {
  161. $res = $this->bookCheckGoodsNum($_goods, $_goods['product_type']);
  162. if ($res['code'] != 0) {
  163. throw new \Exception($res['msg'], 1);
  164. }
  165. }
  166. $_goods['form'] = [];
  167. $total_price += doubleval($_goods['price']);
  168. if ((int)$_goods['product_type'] === 2) {
  169. $_goods['level_price'] = sprintf('%.2f', $mch['list']['price']);
  170. $_goods['price'] = $mch['list']['price'];
  171. }
  172. $level_price += doubleval($_goods['level_price']) > 0 ? doubleval($_goods['level_price']) : doubleval($_goods['price']);
  173. $_goods['integral'] = [
  174. 'forehead' => doubleval($_goods['resIntegral']['forehead']),
  175. 'forehead_integral' => doubleval($_goods['resIntegral']['forehead_integral'])
  176. ];
  177. $integral['forehead'] += doubleval($_goods['resIntegral']['forehead']);
  178. $integral['forehead_integral'] += doubleval($_goods['resIntegral']['forehead_integral']);
  179. unset($_goods['is_form']);
  180. unset($_goods['form_name']);
  181. }
  182. $integral['forehead'] = round($integral['forehead'], 2);
  183. $mch['total_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $total_price);
  184. $mch['level_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $level_price);
  185. $mch['integral'] = !empty($this->verify_card_id) ? [] : $integral;
  186. if($is_integral){
  187. //积分兑换商品不能使用优惠券 或 秒杀活动中不允许使用优惠券则所有商品不允许使用优惠券
  188. $mch['coupon_list'] = [];
  189. }else{
  190. $this->getCouponList($mch);
  191. }
  192. $mch['express_price'] = 0;
  193. $mch['offer_rule'] = $this->getOfferRule($mch);
  194. if(empty(input_params('flag_id'))){
  195. $mch['is_area'] = 0;
  196. }
  197. // 如果是点餐的商品,结构需要变一下
  198. /*if (get_md_id() && (!empty(input_params('flag_id')) || !empty(input_params('table_num'))) && !$submit) {
  199. $user_list = [];
  200. $user_ids = array_unique(array_column($mch['goods_list'], 'user_id'));
  201. foreach ($user_ids as $user_id) {
  202. $goods_list = [];
  203. foreach ($mch['goods_list'] as $item) {
  204. if ($item['user_id'] == $user_id) {
  205. $goods_list[] = $item;
  206. }
  207. }
  208. $saas_user = SaasUser::findOne(['mobile' => User::findOne($user_id)->binding]);
  209. $user_list[] = [
  210. 'name' => $saas_user->name,
  211. 'avatar' => $saas_user->avatar,
  212. 'user_id' => $user_id,
  213. 'goods_list' => $goods_list
  214. ];
  215. }
  216. $mch['goods_list'] = $user_list;
  217. }*/
  218. }
  219. return $this->mch_list;
  220. }
  221. protected function getGoodsList($goods_list, $submit)
  222. {
  223. // 获取卡券绑定的运费模板id
  224. $goodsIds = [];
  225. foreach ($goods_list as $i => &$item) {
  226. if(!isset($item['goods_id'])){
  227. unset($goods_list[$i]);
  228. continue;
  229. }
  230. $attr_id_list = [];
  231. foreach ($item['attr'] as $_a) {
  232. array_push($attr_id_list, $_a['attr_id']);
  233. }
  234. $goods = Goods::findOne([
  235. 'store_id' => get_store_id(),
  236. 'id' => $item['goods_id'],
  237. ]);
  238. if (!$goods) {
  239. unset($goods_list[$i]);
  240. continue;
  241. }
  242. if (($goods->confine_count && $goods->confine_count > 0)) {
  243. $goodsNum = Goods::getBuyNum($this->user, $goods->id);
  244. if ($goodsNum) {
  245. } else {
  246. $goodsNum = 0;
  247. }
  248. $goodsTotalNum = intval($goodsNum + $item['num']);
  249. if ($goodsTotalNum > $goods->confine_count) {
  250. throw new \Exception('商品:' . $goods->name . ' 只允许购买' . $goods->confine_count . $goods->unit, 1);
  251. }
  252. }
  253. $attr_info = $goods->getAttrInfo($attr_id_list);
  254. //判断是否为云仓商品
  255. // if (!empty($goods->cloud_goods_id) && !$goods->is_wholesale) {
  256. // $goods_url = "/goods/getAttrParams";
  257. // $param = [
  258. // 'id' => $goods->cloud_goods_id
  259. // ];
  260. // //请求接口
  261. // $domain = (new OptionSetting)->getCloudDomainName();
  262. // $goodsInfo = cloud_post($domain.$goods_url,$param);
  263. // $goodsInfo = json_decode($goodsInfo, true);
  264. // $goodsInfo = $goodsInfo['data'];
  265. //
  266. // //如果没有上架或审核未通过则不允许购买
  267. // if ((int)$goodsInfo['status'] === 0 || (int)$goodsInfo['is_audit'] !== 2) {
  268. // //商品下架
  269. // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
  270. // unset($goods_list[$i]);
  271. // continue;
  272. // }
  273. //
  274. // //如果使用规格
  275. // if (!empty($goodsInfo['use_attr']) && (int)$goodsInfo['use_attr'] === 1) {
  276. // $goods_2 = new Goods();
  277. // $goods_2->attr = $goods->attr;
  278. // $res_attr = $goods_2->getAttrGroupList();
  279. //
  280. // if ($res_attr) {
  281. // foreach ($res_attr as &$attr_item) {
  282. // unset($attr_item->attr_group_id);
  283. // foreach ($attr_item->attr_list as &$list_item) {
  284. // unset($list_item->attr_id);
  285. // }
  286. // }
  287. // }
  288. // //判断规格名称是否一致
  289. // $res_attr = json_decode(json_encode($res_attr), true);
  290. // if ($res_attr != $goodsInfo['AttrGroupList']) {
  291. // //商品下架
  292. // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
  293. // unset($goods_list[$i]);
  294. // continue;
  295. // }
  296. // $attrs = json_decode($goodsInfo['attrs'], true);
  297. // foreach ($attrs as $attr_item) {
  298. // //如果供货商的商品规格包含购买的
  299. // if (in_array($attr_item['attr_id'], $attr_id_list)) {
  300. // //判断库存无 或者 供货商库存小于购买的商品数量
  301. // if ((int)$attr_item['num'] <= 0) {
  302. // //商品下架
  303. // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
  304. // unset($goods_list[$i]);
  305. // continue;
  306. // }
  307. // }
  308. // }
  309. // } else {
  310. // //如果不使用规格
  311. // if ((int)$goodsInfo['num'] <= 0 || $goodsInfo['name'] != $goods->name) {
  312. // Goods::updateAll(['status' => 0, 'store_id' => get_store_id()],['cloud_goods_id' => $goods->cloud_goods_id]);
  313. // unset($goods_list[$i]);
  314. // continue;
  315. // }
  316. // }
  317. // }
  318. $attr_list = Attr::find()->alias('a')
  319. ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
  320. ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
  321. ->where(['a.id' => $attr_id_list, 'ag.store_id' => get_store_id(),])
  322. ->asArray()->all();
  323. $item['user_id'] = !empty($this->user) ? $this->user->id : 0;
  324. $item['attr_list'] = $attr_list;
  325. $item['goods_id'] = $goods->id;
  326. $item['mch_id'] = $goods->mch_id;
  327. $item['rate'] = $goods->rate;
  328. $item['rate_type'] = $goods->rate_type;
  329. $item['goods_name'] = $goods->name;
  330. $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic;
  331. //如果没有使用规格,则使用商品售价 否则使用规格售价
  332. $item['price'] = sprintf('%.2f', (($goods->use_attr ? $attr_info['price'] : $goods->price) * $item['num']));
  333. if ($item['new_price'] > 0) {
  334. $goods->price = $attr_info['price'] = $item['price'] = $item['new_price'];
  335. }
  336. $item['single_price'] = !empty($this->verify_card_id) ? 0 : sprintf('%.2f', $attr_info['price']);
  337. $item['weight'] = $goods->weight;
  338. if (!empty($this->verify_card_id)) {
  339. $item['integral'] = 0;
  340. } else {
  341. $item['integral'] = $goods->integral ? $goods->integral : 0;
  342. }
  343. $item['integral_price'] = $goods->integral_price;
  344. $item['freight'] = !empty($VerifyCard['freight_id']) ? $VerifyCard['freight_id'] : $goods->freight;
  345. $item['full_cut'] = $goods->full_cut;
  346. $item['goods_cat_id'] = $goods->cat_id;
  347. $item['id'] = $goods->id;
  348. $item['verify_card_id'] = [];
  349. $item['type'] = $goods->type;
  350. $item['delivery_type'] = Json::decode($goods->delivery_type);
  351. /* if (get_md_id() && empty(input_params('flag_id'))) {
  352. $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
  353. if ($md_goods) {
  354. $item['delivery_type'] = Json::decode($md_goods->delivery_type);
  355. }
  356. }*/
  357. // 当前选择的规格
  358. $attrIdArr = [];
  359. foreach ($item['attr_list'] as $attrListItem) {
  360. $attrIdArr[] = $attrListItem['attr_id'];
  361. }
  362. $GoodsFullMinus = GoodsFullMinus::find()
  363. ->select('*')
  364. ->where(['store_id' => get_store_id(), 'goods_id' => $goods->id, 'is_delete' => 0])
  365. ->orderBy('full_minus_num ASC')->asArray()->all();
  366. // 门店逻辑
  367. $attr = $goods->attr;
  368. $price = $goods->price;
  369. // 门店逻辑
  370. if (!empty($this->md_id)) {
  371. $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => $this->md_id]);
  372. if ($md_goods) {
  373. $attr = $md_goods->attr;
  374. $price = $md_goods->price;
  375. }
  376. }
  377. if ($item['new_price'] > 0) {
  378. $mch_attr_id = array_column($item['attr'], 'attr_id');
  379. sort($mch_attr_id);
  380. $attr_ = json_decode($attr, true);
  381. foreach ($attr_ as &$goods_attr_item) {
  382. $goods_attr_id = array_column($goods_attr_item['attr_list'], 'attr_id');
  383. sort($goods_attr_id);
  384. if (!array_diff($goods_attr_id, $mch_attr_id)) {
  385. $goods_attr_item['price'] = $item['new_price'];
  386. }
  387. }
  388. $attr = json_encode($attr_, JSON_UNESCAPED_UNICODE);
  389. $price = $item['new_price'];
  390. $goods->is_level = 0;
  391. }
  392. /*if (get_md_id() && empty(input_params('flag_id'))) {
  393. $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
  394. if ($md_goods) {
  395. $attr = $md_goods->attr;
  396. $price = $md_goods->price;
  397. }
  398. }*/
  399. $otherData = [];
  400. $this->user && $otherData = ['user'=> $this->user];
  401. $other['form'] = 'cashier';
  402. $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([
  403. 'attr' => $attr,
  404. 'price' => $price,
  405. 'is_level' => $goods->is_level,
  406. 'mch_id' => $goods->mch_id,
  407. 'full_minus' => $GoodsFullMinus,
  408. 'unit' => $goods->unit,
  409. 'use_attr' => $goods->use_attr,
  410. 'md_id' => $this->md_id,
  411. ], $attrIdArr, $otherData, $item['num']);
  412. $item['batch_price_tips'] = $res['batch_price_tips'];
  413. $item['current_batch_price_tips'] = $res['current_batch_price_tips'];
  414. $item['price'] = sprintf('%.2f', ($res['price'] * $item['num']));
  415. $item['level_price'] = sprintf('%.2f', ($res['level_price'] * $item['num']));
  416. $item['is_level'] = $res['is_level'];
  417. $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'];
  418. $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);
  419. $item['give'] = $integralArr['give'];
  420. $item['resIntegral'] = $integralArr['resIntegral'];
  421. $goodsIds[] = $goods->id;
  422. $item['goods_card_list'] = Goods::getGoodsCard($goods->id);
  423. $item['is_form'] = $goods->is_form;
  424. $item['form_name'] = $goods->form_name;
  425. $item['product_type'] = $goods->product_type;
  426. /*$res = $this->getSceKillGoodsPrice($goods, $attr_id_list, $submit, $item['num']);
  427. if ($res['code'] === 0) {
  428. $item['is_level'] = 0;
  429. if ($res['data'] > 0) {
  430. $item['price'] = $item['level_price'] = sprintf('%.2f', ($res['data'] * $item['num']));
  431. }
  432. } else {
  433. return $res;
  434. }*/
  435. if ($goods->product_type == Goods::GOODS_TYPE_ADOPT) {
  436. $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods->id,'is_delete' => 0])->one();
  437. $item['desc'] = $adopt_goods->desc;
  438. }
  439. }
  440. // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题
  441. return array_merge($goods_list, []);
  442. }
  443. //积分计算
  444. /**
  445. * @param $goods_item object 重新编写的goods_item
  446. * @param $store_integral int 商城设置的积分规则
  447. * @param $goods_id array 已设置积分的商品id数组
  448. * @return array
  449. */
  450. protected function getIntegral($goods_item, $store_integral, $goods_id = array(), $send_integral_profit = 0)
  451. {
  452. $integral = json_decode($goods_item->integral, true);
  453. $resIntegral = [
  454. 'forehead' => 0,
  455. 'forehead_integral' => 0,
  456. ];
  457. if ($integral) {
  458. // 赠送积分计算
  459. $give = $integral['give'];
  460. if (strpos($give, '%') !== false) {
  461. // 百分比
  462. $give = trim($give, '%');
  463. $goods_item->give = (int)($goods_item->price * ($give / 100));
  464. } else {
  465. // 固定积分
  466. $goods_item->give = (int)($give * $goods_item->num);
  467. }
  468. if ($give <= 0 && $send_integral_profit > 0) {
  469. $goods_item->give = (int)(intval($goods_item->price) * ($send_integral_profit / 100));
  470. }
  471. // 抵扣积分计算
  472. $forehead = $integral['forehead'];
  473. if ($forehead) {
  474. if (strpos($forehead, '%') !== false) {//百分比积分抵扣计算
  475. if ($forehead >= 100) {
  476. $forehead = 100;
  477. }
  478. if ($integral['more'] == '1') {//多件累计计算
  479. $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->price * $store_integral);
  480. } else {
  481. if (!in_array($goods_item->id, $goods_id)) { //不允许多件累计 同id商品值计算一次积分抵扣
  482. $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->single_price * $store_integral);
  483. }
  484. }
  485. } else {
  486. // if ($integral['more'] == '1') {
  487. // $resIntegral['forehead_integral'] = (int)($store_integral * $goods_item->price);
  488. // if ($goods_item->price > ($forehead * $goods_item->num)) {
  489. // $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
  490. // }
  491. // } else {
  492. // if (!in_array($goods_item->id, $goods_id)) {
  493. // $goodsPrice = $goods_item->single_price;
  494. // $resIntegral['forehead_integral'] = (int)($store_integral * $goodsPrice);
  495. // if ($goodsPrice > $forehead) {
  496. // if ($forehead > 0) {
  497. // $resIntegral['forehead_integral'] = (int)($forehead * $store_integral);
  498. // } else {
  499. // $resIntegral['forehead_integral'] = 0;
  500. // }
  501. // }
  502. // }
  503. // }
  504. // 累计
  505. $resIntegral['forehead_integral'] = (int)($store_integral * $goods_item->price);
  506. if ($goods_item->price > ($forehead * $goods_item->num)) {
  507. $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
  508. }
  509. }
  510. }
  511. if ($this->integral['forehead_integral'] < $this->user->integral) {
  512. $resetIntegral = $this->user->integral - $this->integral['forehead_integral'];
  513. $resIntegral['forehead_integral'] = $resIntegral['forehead_integral'] >= $resetIntegral ? $resetIntegral : $resIntegral['forehead_integral'];
  514. $resIntegral['forehead'] = $store_integral ? sprintf("%.2f", ($resIntegral['forehead_integral'] / $store_integral)) : 0;
  515. $this->integral['forehead_integral'] += $resIntegral['forehead_integral'];
  516. $this->integral['forehead'] += $resIntegral['forehead'];
  517. } else {
  518. $resIntegral['forehead_integral'] = 0;
  519. $resIntegral['forehead'] = 0;
  520. }
  521. }
  522. return [
  523. 'resIntegral' => $resIntegral,
  524. 'give' => $goods_item->give ?? ''
  525. ];
  526. }
  527. protected function getCouponList(&$mch)
  528. {
  529. // if (get_md_id()) {
  530. // $mch['coupon_list'] = [];
  531. // return;
  532. // }
  533. if (!isset($mch['mch_id'])) {
  534. $mch['mch_id'] = 0;
  535. }
  536. $goods_total_price = $mch['total_price'];
  537. $cat_ids = $this->getCatIdList($mch['goods_list'],$mch['mch_id']);
  538. $coupon_goods_id = $this->getGoodsIdList($mch['goods_list']);
  539. $list = UserCoupon::find()->alias('uc')
  540. ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
  541. ->leftJoin(['cas' => CouponAutoSend::tableName()], 'uc.coupon_auto_send_id=cas.id')
  542. ->where([
  543. 'AND',
  544. ['uc.is_delete' => 0],
  545. ['uc.is_use' => 0],
  546. ['c.mch_id' => $mch['mch_id']],
  547. ['uc.is_expire' => 0],
  548. ['uc.user_id' => $this->user_id],
  549. ['<=', 'c.min_price', $goods_total_price],
  550. ])
  551. ->andWhere($mch['plugin_type'] == 6 ? ['c.appoint_type' => 3] : [
  552. 'OR',
  553. ['<>', 'c.appoint_type', 3],
  554. 'c.appoint_type IS NULL'
  555. ])
  556. ->select('c.name, c.expire_day, c.id, c.expire_type, c.discount_type, c.discount, c.discount_max_price, uc.id user_coupon_id,c.mch_id,c.rule,c.sub_price,c.min_price,cas.event,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list')
  557. ->asArray()->all();
  558. $events = [
  559. 0 => '平台发放',
  560. 1 => '分享红包',
  561. 2 => '购物返券',
  562. 3 => '领券中心',
  563. ];
  564. $max_price = 0;
  565. if ($mch['plugin_type'] == 6) {
  566. $max_price = $mch['total_price'];
  567. } else {
  568. foreach ($mch['goods_list'] as $v) {
  569. $max_price += $v['price'];
  570. }
  571. }
  572. $new_list = [];
  573. foreach ($list as $i => $item) {
  574. if ($item['expire_type'] == 1) {
  575. if ($item['end_time'] > 0) {
  576. if (time() > $item['end_time']) {
  577. continue;
  578. }
  579. }
  580. }
  581. if ($item['expire_type'] == 2) {
  582. if (time() > $item['end_time'] || time() < $item['begin_time']) {
  583. continue;
  584. }
  585. }
  586. $list[$i]['status'] = 0;
  587. if (isset($item['is_use']) && $item['is_use']) {
  588. $list[$i]['status'] = 1;
  589. }
  590. if (isset($item['is_expire']) && $item['is_expire']) {
  591. $list[$i]['status'] = 2;
  592. }
  593. $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
  594. $list[$i]['begin_time'] = date('Y.m.d H:i', (int)$item['begin_time']);
  595. $list[$i]['end_time'] = date('Y.m.d H:i', (int)$item['end_time']);
  596. $list[$i]['discount_type'] = $item['discount_type'];
  597. $list[$i]['discount'] = $item['discount'];
  598. if (!$item['event']) {
  599. if ($item['type'] == 2) {
  600. $list[$i]['event'] = $item['event'] = 3;
  601. } else {
  602. $list[$i]['event'] = $item['event'] = 0;
  603. }
  604. }
  605. $list[$i]['event_desc'] = $events[$item['event']] ?: '领券中心';
  606. $list[$i]['min_price'] = doubleval($item['min_price']);
  607. if ($list[$i]['discount_type'] == 1) {
  608. $list[$i]['sub_price'] = round($mch['total_price'] - doubleval($mch['total_price'] * $list[$i]['discount'] / 10), 2);
  609. if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){
  610. $list[$i]['sub_price'] = $item['discount_max_price'];
  611. }
  612. } else {
  613. $list[$i]['sub_price'] = doubleval($item['sub_price']);
  614. }
  615. if ($list[$i]['appoint_type'] == 1) {
  616. $list[$i]['cat_id_list'] = json_decode($list[$i]['cat_id_list']);
  617. if ($list[$i]['cat_id_list'] != null) {
  618. $current = array_intersect($list[$i]['cat_id_list'], $cat_ids);
  619. if ($current) {
  620. $goodsAdd = [];
  621. $price = 0;
  622. foreach ($current as $v) {
  623. foreach ($mch['goods_list'] as $v2) {
  624. if (in_array($v, $v2['cat_id']) && !in_array($v2['goods_id'], $goodsAdd)) {
  625. $price += $v2['price'];
  626. array_push($goodsAdd, $v2['goods_id']);
  627. }
  628. };
  629. }
  630. if ($price < $list[$i]['min_price']) {
  631. unset($list[$i]);
  632. continue;
  633. }
  634. } else {
  635. unset($list[$i]);
  636. continue;
  637. }
  638. }
  639. } elseif ($list[$i]['appoint_type'] == 2) {
  640. $list[$i]['goods_id_list'] = json_decode($list[$i]['goods_id_list']);
  641. if ($list[$i]['goods_id_list'] != null) {
  642. $current = array_intersect($list[$i]['goods_id_list'], $coupon_goods_id);
  643. if ($current) {
  644. $goodsAdd = [];
  645. $price = 0;
  646. foreach ($current as $v) {
  647. foreach ($mch['goods_list'] as $v2) {
  648. if ($v == $v2['goods_id'] && !in_array($v2['goods_id'], $goodsAdd)) {
  649. $price += $v2['price'];
  650. }
  651. }
  652. }
  653. if ($price < $list[$i]['min_price']) {
  654. unset($list[$i]);
  655. continue;
  656. }
  657. } else {
  658. unset($list[$i]);
  659. continue;
  660. }
  661. }
  662. } else {
  663. if ($max_price < $list[$i]['min_price']) {
  664. unset($list[$i]);
  665. continue;
  666. }
  667. }
  668. $new_list[] = $list[$i];
  669. }
  670. if (!empty($this->verify_card_id)) {
  671. $mch['coupon_list'] = [];
  672. } else {
  673. $mch['coupon_list'] = $new_list;
  674. }
  675. }
  676. protected function getCatIdList(&$goods_list,$mch_id = 0)
  677. {
  678. $cat_id_list = [];
  679. foreach ($goods_list as &$goods) {
  680. if ($goods['goods_cat_id'] == 0) {
  681. if ($mch_id > 0) {
  682. $goods_cat_list = MchGoodsCat::find()
  683. ->select('cat_id')->where([
  684. 'goods_id' => $goods['goods_id'],
  685. ])->all();
  686. }else {
  687. $goods_cat_list = GoodsCat::find()
  688. ->select('cat_id')->where([
  689. 'goods_id' => $goods['goods_id'],
  690. 'is_delete' => 0,
  691. ])->all();
  692. }
  693. foreach ($goods_cat_list as $goods_cat) {
  694. $cat_id_list[] = $goods_cat->cat_id;
  695. $goods['cat_id'][] = $goods_cat->cat_id;
  696. }
  697. } else {
  698. $cat_id_list[] = $goods['goods_cat_id'];
  699. $goods['cat_id'][] = $goods['goods_cat_id'];
  700. }
  701. $cat_parent_list = Cat::find()->select('parent_id')
  702. ->andWhere(['id' => $goods['cat_id'], 'store_id' => $this->store_id, 'is_delete' => 0])->andWhere(['>', 'parent_id', 0])
  703. ->column();
  704. $cat_id_list = array_merge($cat_parent_list, $cat_id_list);
  705. $goods['cat_id'] = array_merge($cat_parent_list, is_array($goods['cat_id']) ? $goods['cat_id'] : []);
  706. }
  707. unset($goods);
  708. return array_unique($cat_id_list);
  709. }
  710. protected function getGoodsIdList($goods_list)
  711. {
  712. $goods_id_list = [];
  713. foreach ($goods_list as $goods) {
  714. $goods_id_list[] = $goods['goods_id'];
  715. }
  716. return $goods_id_list;
  717. }
  718. // 获取起送规则
  719. protected function getOfferRule($mch)
  720. {
  721. $res = [
  722. 'is_allowed' => 0,
  723. 'total_price' => 0,
  724. 'msg' => ''
  725. ];
  726. return $res;
  727. }
  728. protected function getAddress()
  729. {
  730. $saas_user = $this->saas_user;
  731. $this->address = Address::find()->where([
  732. 'user_id' => $saas_user->id,
  733. 'is_delete' => 0,
  734. ])->asArray()->all();
  735. return $this->address;
  736. }
  737. }