Buy_1.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 逻辑层模型
  13. */
  14. class Buy_1
  15. {
  16. public $lock = false; //是否加锁
  17. /**
  18. * 取得商品最新的属性及促销[购物车]
  19. * @param unknown $cart_list
  20. */
  21. public function getGoodsCartList($cart_list)
  22. {
  23. $cart_list = $this->_getOnlineCartList($cart_list);
  24. //优惠套装
  25. $this->_getBundlingCartList($cart_list);
  26. //会员等级折扣
  27. $this->getMgdiscountCartList($cart_list);
  28. //抢购
  29. $this->getGroupbuyCartList($cart_list);
  30. //秒杀
  31. $this->getXianshiCartList($cart_list);
  32. //预售
  33. $this->getPresellCartList($cart_list);
  34. //批发
  35. $this->getWholesaleCartList($cart_list);
  36. //赠品
  37. $this->_getGiftCartList($cart_list);
  38. return $cart_list;
  39. }
  40. /**
  41. * 取得商品最新的属性及促销[立即购买]
  42. * @param type $goods_id
  43. * @param type $quantity
  44. * @param type $extra
  45. * @return array
  46. */
  47. public function getGoodsOnlineInfo($goods_id, $quantity, $extra = array(), $member_id = 0)
  48. {
  49. $goods_info = $this->_getGoodsOnlineInfo($goods_id, $quantity);
  50. if (isset($extra['pintuan_id']) && intval($extra['pintuan_id']) > 0) {
  51. //如果是特定拼团商品,则只按照拼团的规则进行处理
  52. $this->getPintuanInfo($goods_info, $goods_info['goods_num'], $extra, $member_id);
  53. } else if (isset($extra['bargainorder_id']) && intval($extra['bargainorder_id']) > 0) {
  54. $this->getBargainInfo($goods_info, $goods_info['goods_num'], $extra);
  55. } else {
  56. //会员等级折扣
  57. $this->getMgdiscountInfo($goods_info);
  58. //抢购
  59. $this->getGroupbuyInfo($goods_info, $goods_info['goods_num']);
  60. //秒杀
  61. $this->getXianshiInfo($goods_info, $goods_info['goods_num']);
  62. //预售
  63. $this->getPresellInfo($goods_info, $goods_info['goods_num']);
  64. //批发
  65. $this->getWholesaleInfo($goods_info, $goods_info['goods_num']);
  66. //赠品
  67. $this->_getGoodsgiftList($goods_info);
  68. }
  69. return $goods_info;
  70. }
  71. /**
  72. * 商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
  73. * @param unknown $store_cart_list 以店铺ID分组的购物车商品信息
  74. * @return array
  75. */
  76. public function calcCartList($store_cart_list)
  77. {
  78. if (empty($store_cart_list) || !is_array($store_cart_list))
  79. return array($store_cart_list, array(), 0);
  80. //存放每个店铺的商品总金额
  81. $store_goods_total = array();
  82. //存放本次下单所有店铺商品总金额
  83. $order_goods_total = 0;
  84. foreach ($store_cart_list as $store_id => $store_cart) {
  85. $tmp_amount = 0;
  86. $tmp_amount2 = 0;
  87. $tmp_amount3 = 0;
  88. foreach ($store_cart as $key => $cart_info) {
  89. $store_cart[$key]['goods_original_total'] = ds_price_format($cart_info['goods_original_price'] * $cart_info['goods_num']); //商品原价
  90. $store_cart[$key]['goods_total'] = ds_price_format($cart_info['goods_price'] * $cart_info['goods_num']);
  91. $store_cart[$key]['goods_discount_total'] = ds_price_format($store_cart[$key]['goods_original_total'] - $store_cart[$key]['goods_total']); //优惠金额
  92. $store_cart[$key]['goods_image_url'] = goods_cthumb($store_cart[$key]['goods_image']);
  93. $tmp_amount += $store_cart[$key]['goods_total'];
  94. $tmp_amount2 += $store_cart[$key]['goods_original_total'];
  95. $tmp_amount3 += $store_cart[$key]['goods_discount_total'];
  96. }
  97. $store_cart_list[$store_id] = $store_cart;
  98. $store_goods_total[$store_id] = ds_price_format($tmp_amount);
  99. $store_goods_original_total[$store_id] = ds_price_format($tmp_amount2);
  100. $store_goods_discount_total[$store_id] = ds_price_format($tmp_amount3);
  101. }
  102. return array($store_cart_list, $store_goods_total, $store_goods_original_total, $store_goods_discount_total);
  103. }
  104. /**
  105. * 取得店铺级优惠 - 跟据商品金额返回每个店铺当前符合的一条活动规则,如果有赠品,则自动追加到购买列表,价格为0
  106. * @param unknown $store_goods_total 每个店铺的商品金额小计,以店铺ID为下标
  107. * @return array($premiums_list,$mansong_rule_list) 分别为赠品列表[下标自增],店铺满送规则列表[店铺ID为下标]
  108. */
  109. public function getMansongruleCartListByTotal($store_goods_total)
  110. {
  111. if (!config('ds_config.promotion_allow') || empty($store_goods_total) || !is_array($store_goods_total))
  112. return array(array(), array());
  113. $pmansong_model = model('pmansong');
  114. //定义赠品数组,下标为店铺ID
  115. $premiums_list = array();
  116. //定义满送活动数组,下标为店铺ID
  117. $mansong_rule_list = array();
  118. foreach ($store_goods_total as $store_id => $goods_total) {
  119. $rule_info = $pmansong_model->getMansongruleByStoreID($store_id, $goods_total);
  120. if (is_array($rule_info) && !empty($rule_info)) {
  121. //即不减金额,也找不到促销商品时(已下架),此规则无效
  122. if (empty($rule_info['mansongrule_discount']) && empty($rule_info['mansong_goods_name'])) {
  123. continue;
  124. }
  125. $rule_info['desc'] = $this->_parseMansongruleDesc($rule_info);
  126. $rule_info['discount'] = ds_price_format($rule_info['mansongrule_discount']);
  127. $mansong_rule_list[$store_id] = $rule_info;
  128. //如果赠品在售,有库存,则追加到购买列表
  129. if (!empty($rule_info['mansong_goods_name']) && !empty($rule_info['goods_storage'])) {
  130. $data = array();
  131. $data['goods_id'] = $rule_info['goods_id'];
  132. $data['goods_name'] = $rule_info['mansong_goods_name'];
  133. $data['goods_num'] = 1;
  134. $data['goods_price'] = 0.00;
  135. $data['goods_image'] = $rule_info['goods_image'];
  136. $data['goods_image_url'] = goods_cthumb($rule_info['goods_image']);
  137. $data['goods_storage'] = $rule_info['goods_storage'];
  138. $premiums_list[$store_id][] = $data;
  139. }
  140. }
  141. }
  142. return array($premiums_list, $mansong_rule_list);
  143. }
  144. /**
  145. * 重新计算每个店铺最终商品总金额(最初计算金额减去各种优惠/加运费)
  146. * @param array $store_goods_total 店铺商品总金额
  147. * @param array $preferential_array 店铺优惠活动内容
  148. * @param string $preferential_type 优惠类型,目前只有一个 'mansong'
  149. * @return array 返回扣除优惠后的店铺商品总金额
  150. */
  151. public function reCalcGoodsTotal($store_goods_total, $preferential_array, $preferential_type, $goods_list = array())
  152. {
  153. $deny = empty($store_goods_total) || !is_array($store_goods_total) || empty($preferential_array) || !is_array($preferential_array);
  154. if ($deny)
  155. return $store_goods_total;
  156. switch ($preferential_type) {
  157. case 'mansong':
  158. if (!config('ds_config.promotion_allow'))
  159. return $store_goods_total;
  160. foreach ($preferential_array as $store_id => $rule_info) {
  161. if (is_array($rule_info) && $rule_info['discount'] > 0) {
  162. $store_goods_total[$store_id] -= $rule_info['discount'];
  163. }
  164. }
  165. break;
  166. case 'voucher':
  167. if (!config('ds_config.voucher_allow'))
  168. return $store_goods_total;
  169. foreach ($preferential_array as $store_id => $voucher_info) {
  170. $store_goods_total[$store_id] -= $voucher_info['voucher_price'];
  171. }
  172. break;
  173. case 'mallvoucher':
  174. if (!config('ds_config.voucher_allow'))
  175. return $store_goods_total;
  176. $mallvouchergoodstotal = 0;
  177. //获取购物车里面的平台代金券商品
  178. $mallvouchergoodslist = array();
  179. foreach ($goods_list as $k => $v) {
  180. $gc_id = ',' . $v['gc_id'] . ',';
  181. if (strpos($preferential_array['mallvouchertemplate_gcidarr'], $gc_id) !== false) {
  182. $mallvouchergoodslist[] = $v;
  183. }
  184. };
  185. //获取代金券商品总价
  186. foreach ($mallvouchergoodslist as $k => $v) {
  187. $mallvouchergoodstotal += $v['goods_price'] * $v['goods_num'];
  188. }
  189. //获取店铺商品代金券占比
  190. $storearray = array();
  191. foreach ($mallvouchergoodslist as $k => $v) {
  192. if (isset($storearray[$v['store_id']])) {
  193. $proportion = sprintf("%.2f", ($v['goods_price'] * $v['goods_num']) / $mallvouchergoodstotal);
  194. $storearray[$v['store_id']]['proportion'] = $storearray[$v['store_id']]['proportion'] + $proportion;
  195. } else {
  196. $storearray[$v['store_id']]['proportion'] = sprintf("%.2f", ($v['goods_price'] * $v['goods_num']) / $mallvouchergoodstotal);
  197. }
  198. }
  199. foreach ($storearray as $key => $val) {
  200. $mallvoucherprice = $val['proportion'] * $preferential_array['mallvoucheruser_price'];
  201. $store_goods_total[$key] = $store_goods_total[$key] - $mallvoucherprice;
  202. }
  203. break;
  204. case 'freight':
  205. foreach ($preferential_array as $store_id => $freight_total) {
  206. $store_goods_total[$store_id] += $freight_total;
  207. }
  208. break;
  209. }
  210. return $store_goods_total;
  211. }
  212. /**
  213. * 取得店铺可用的代金券
  214. * @param array $store_goods_total array(店铺ID=>商品总金额)
  215. * @return array
  216. */
  217. public function getStoreAvailableVoucherList($store_goods_total, $member_id)
  218. {
  219. if (!config('ds_config.voucher_allow'))
  220. return array();
  221. $voucher_list = array();
  222. $voucher_model = model('voucher');
  223. foreach ($store_goods_total as $store_id => $goods_total) {
  224. $condition = array();
  225. $condition[] = array('voucher_store_id', '=', $store_id);
  226. $condition[] = array('voucher_owner_id', '=', $member_id);
  227. $voucher_list[$store_id] = $voucher_model->getCurrentAvailableVoucher($condition, $goods_total);
  228. }
  229. return $voucher_list;
  230. }
  231. /**
  232. * 取得可用的平台代金券
  233. * @param array $store_goods_total array(店铺ID=>商品总金额)
  234. * @return array
  235. */
  236. public function getAvailableMallVoucherUserList($goods_list, $member_id)
  237. {
  238. //获取购物车里面的商品分类ID
  239. $goodsclassarray = array();
  240. foreach ($goods_list as $key => $value) {
  241. $goodsclassarray[] = ',' . $value['gc_id'] . ',';
  242. }
  243. if (!config('ds_config.voucher_allow'))
  244. return array();
  245. $mallvoucher_list = array();
  246. //获取商品分类关联的平台代金券模板
  247. $mallvouchertemplate_list = array();
  248. $mallvouchertemplate_model = model('mallvouchertemplate');
  249. foreach ($goodsclassarray as $key => $val) {
  250. $gccondition = array();
  251. $gccondition[] = array('mallvouchertemplate_gcidarr', 'like', "%$val%");
  252. $mallvouchertemplatelist = $mallvouchertemplate_model->getMallvouchertemplateList($gccondition);
  253. $mallvouchertemplate_list = array_merge($mallvouchertemplate_list, $mallvouchertemplatelist);
  254. }
  255. if (!empty($mallvouchertemplate_list)) {
  256. //获取购物车商品相关平台代金券ID
  257. foreach ($mallvouchertemplate_list as $key => $value) {
  258. $mallvouchergoodstotal = 0;
  259. //获取代金券关联商品分类的商品
  260. $mallvouchergoodslist = array();
  261. foreach ($goods_list as $k => $v) {
  262. $gc_id = ',' . $v['gc_id'] . ',';
  263. if (strpos($value['mallvouchertemplate_gcidarr'], $gc_id) !== false) {
  264. $mallvouchergoodslist[] = $v;
  265. }
  266. };
  267. foreach ($mallvouchergoodslist as $k => $v) {
  268. $mallvouchergoodstotal += $v['goods_price'] * $v['goods_num'];
  269. }
  270. $mallvoucherusercondition = array();
  271. $mallvoucherusercondition[] = array('mallvouchertemplate_id', '=', $value['mallvouchertemplate_id']);
  272. $mallvoucherusercondition[] = array('mallvoucheruser_ownerid', '=', $member_id);
  273. $mallvoucherarr_list = $mallvouchertemplate_model->getCurrentAvailableMallVoucherUser($mallvoucherusercondition, $mallvouchergoodstotal);
  274. if (!empty($mallvoucherarr_list)) {
  275. $mallvoucher_list = array_merge($mallvoucher_list, $mallvoucherarr_list);
  276. }
  277. }
  278. }
  279. $mallvoucher_list = ds_change_arraykey($mallvoucher_list, 'mallvouchertemplate_id');
  280. return $mallvoucher_list;
  281. }
  282. /**
  283. * 验证传过来的代金券是否可用有效,如果无效,直接删除
  284. * @param array $input_mallvoucher_list 代金券列表
  285. * @param array $store_goods_total (店铺ID=>商品总金额)
  286. * @return array
  287. */
  288. public function reParseMallVoucherList($input_mallvoucher_list = array(), $goods_list = array(), $member_id)
  289. {
  290. if (empty($input_mallvoucher_list) || !is_array($input_mallvoucher_list))
  291. return array();
  292. $mall_voucher_list = $this->getAvailableMallVoucherUserList($goods_list, $member_id);
  293. foreach ($mall_voucher_list as $key => $value) {
  294. if ($mall_voucher_list[$key]['mallvouchertemplate_id'] == $input_mallvoucher_list['mallvouchertemplate_id']) {
  295. $input_mallvoucher_list['mallvoucheruser_price'] = $value['mallvoucheruser_price'];
  296. $input_mallvoucher_list['mallvoucheruser_id'] = $value['mallvoucheruser_id'];
  297. $input_mallvoucher_list['mallvoucheruser_ownerid'] = $value['mallvoucheruser_ownerid'];
  298. $input_mallvoucher_list['mallvouchertemplate_gcidarr'] = $value['mallvouchertemplate_gcidarr'];
  299. $input_mallvoucher_list['mallvoucheruser_code'] = $value['mallvoucheruser_code'];
  300. }
  301. }
  302. return $input_mallvoucher_list;
  303. }
  304. /**
  305. * 验证传过来的代金券是否可用有效,如果无效,直接删除
  306. * @param array $input_voucher_list 代金券列表
  307. * @param array $store_goods_total (店铺ID=>商品总金额)
  308. * @return array
  309. */
  310. public function reParseVoucherList($input_voucher_list = array(), $store_goods_total = array(), $member_id)
  311. {
  312. if (empty($input_voucher_list) || !is_array($input_voucher_list))
  313. return array();
  314. $store_voucher_list = $this->getStoreAvailableVoucherList($store_goods_total, $member_id);
  315. foreach ($input_voucher_list as $store_id => $voucher) {
  316. $tmp = $store_voucher_list[$store_id];
  317. if (is_array($tmp) && isset($tmp[$voucher['vouchertemplate_id']])) {
  318. $input_voucher_list[$store_id]['voucher_price'] = $tmp[$voucher['vouchertemplate_id']]['voucher_price'];
  319. $input_voucher_list[$store_id]['voucher_id'] = $tmp[$voucher['vouchertemplate_id']]['voucher_id'];
  320. $input_voucher_list[$store_id]['voucher_code'] = $tmp[$voucher['vouchertemplate_id']]['voucher_code'];
  321. $input_voucher_list[$store_id]['voucher_owner_id'] = $tmp[$voucher['vouchertemplate_id']]['voucher_owner_id'];
  322. } else {
  323. unset($input_voucher_list[$store_id]);
  324. }
  325. }
  326. return $input_voucher_list;
  327. }
  328. /**
  329. * 判断商品是不是秒杀中,如果购买数量若>=规定的下限,按折扣价格计算,否则按原价计算
  330. * @param array $goods_info
  331. * @param number $quantity 购买数量
  332. */
  333. public function getXianshiInfo(&$goods_info, $quantity)
  334. {
  335. if (empty($quantity))
  336. $quantity = 1;
  337. if (!config('ds_config.promotion_allow') || empty($goods_info['xianshi_info']))
  338. return;
  339. $goods_info['xianshi_info']['down_price'] = ds_price_format($goods_info['goods_price'] - $goods_info['xianshi_info']['xianshigoods_price']);
  340. if ($quantity >= $goods_info['xianshi_info']['xianshigoods_lower_limit']) {
  341. $goods_info['goods_price'] = $goods_info['xianshi_info']['xianshigoods_price'];
  342. $goods_info['promotions_id'] = $goods_info['xianshi_info']['xianshi_id'];
  343. $goods_info['ifxianshi'] = true;
  344. }
  345. }
  346. /**
  347. * 判断商品是不是预售
  348. * @param array $goods_info
  349. * @param number $quantity 购买数量
  350. */
  351. public function getPresellInfo(&$goods_info, $quantity)
  352. {
  353. if (empty($quantity))
  354. $quantity = 1;
  355. if (!config('ds_config.promotion_allow') || empty($goods_info['presell_info']))
  356. return;
  357. $goods_info['presell_info']['down_price'] = ds_price_format($goods_info['goods_price'] - $goods_info['presell_info']['presell_price']);
  358. $goods_info['goods_price'] = $goods_info['presell_info']['presell_price'];
  359. $goods_info['promotions_id'] = $goods_info['presell_info']['presell_id'];
  360. $goods_info['ifpresell'] = true;
  361. }
  362. /**
  363. * 判断商品是不是批发中,如果购买数量若>=规定的下限,按批发价格计算,否则按原价计算
  364. * @param array $goods_info
  365. * @param number $quantity 购买数量
  366. */
  367. public function getWholesaleInfo(&$goods_info, $quantity)
  368. {
  369. if (empty($quantity))
  370. $quantity = 1;
  371. if (!config('ds_config.promotion_allow') || empty($goods_info['wholesale_info']))
  372. return;
  373. $if_wholesale = false;
  374. foreach ($goods_info['wholesale_info']['wholesalegoods_price'] as $val) {
  375. if ($val['num'] <= $quantity) {
  376. $if_wholesale = true;
  377. $goods_info['wholesale_info']['down_price'] = ds_price_format($goods_info['goods_price'] - $val['price']);
  378. $goods_info['goods_price'] = $val['price'];
  379. }
  380. }
  381. if ($if_wholesale) {
  382. $goods_info['ifwholesale'] = true;
  383. $goods_info['promotions_id'] = $goods_info['wholesale_info']['wholesale_id'];
  384. }
  385. }
  386. /**
  387. * 判断商品是不是砍价中,如果购买数量若>=规定的下限,则报错
  388. * @param array $goods_info
  389. * @param number $quantity 购买数量
  390. */
  391. public function getBargainInfo(&$goods_info, $quantity, $extra)
  392. {
  393. $pbargain_model = model('pbargain');
  394. $pbargainorder_model = model('pbargainorder');
  395. //是否正在进行的砍价活动
  396. $pbargainorder_info = $pbargainorder_model->getOnePbargainorder(array('bargainorder_id' => $extra['bargainorder_id'], 'bargainorder_state' => 2), true);
  397. if (!$pbargainorder_info) {
  398. return;
  399. }
  400. $bargain_info = $pbargain_model->getOnlineBargainInfoByID($pbargainorder_info['bargain_id']);
  401. if (!$bargain_info) {
  402. return;
  403. }
  404. //之前是否已添加过订单
  405. if (model('order')->getOrdergoodsInfo(array('goods_type' => 8, 'promotions_id' => $pbargainorder_info['bargainorder_id']))) {
  406. return;
  407. }
  408. //购买数量是否超过限制数量
  409. if ($quantity > $bargain_info['bargain_limit']) {
  410. return;
  411. }
  412. $goods_info['goods_price'] = $pbargainorder_info['bargainorder_current_price'];
  413. $goods_info['promotions_id'] = $pbargainorder_info['bargainorder_id'];
  414. $goods_info['ifbargain'] = true;
  415. }
  416. /**
  417. * 判断商品是不是拼团中,如果购买数量若>=规定的下限,则报错
  418. * @param array $goods_info
  419. * @param number $quantity 购买数量
  420. */
  421. public function getPintuanInfo(&$goods_info, $quantity, $extra, $member_id)
  422. {
  423. if (empty($quantity))
  424. $quantity = 1;
  425. //超过了购买限制按照原价
  426. if ($goods_info['pintuan_info'] && ($quantity > $goods_info['pintuan_info']['pintuan_limit_quantity'])) {
  427. return;
  428. }
  429. $pintuangroup_id = intval($extra['pintuangroup_id']);
  430. if ($pintuangroup_id > 0) {
  431. //判断非开团拼团订单,判断参团人数是否满足
  432. $pintuangroup = model('ppintuangroup')->getOnePpintuangroup(array('pintuangroup_id' => $pintuangroup_id));
  433. if (empty($pintuangroup) || $pintuangroup['pintuangroup_state'] != 1) {
  434. //不存在,或者拼团活动状态 不为1
  435. return;
  436. }
  437. //当开团拼团订单表中参团人数大于成团人团则按照正常价格购买
  438. if ($pintuangroup['pintuangroup_joined'] >= $pintuangroup['pintuangroup_limit_number']) {
  439. return;
  440. }
  441. }
  442. //不可以重复参加
  443. $order_id_list = Db::name('ppintuanorder')->where(array(array('pintuan_id', '=', $goods_info['pintuan_info']['pintuan_id']), array('pintuanorder_state', '<>', 0)))->column('order_id');
  444. if ($order_id_list) {
  445. if (!$goods_info['is_virtual']) {
  446. if (Db::name('order')->where('buyer_id', $member_id)->where('order_id', 'in', $order_id_list)->value('order_id')) {
  447. return;
  448. }
  449. } else {
  450. if (Db::name('vrorder')->where('buyer_id', $member_id)->where('order_id', 'in', $order_id_list)->value('order_id')) {
  451. return;
  452. }
  453. }
  454. }
  455. $goods_info['pintuan_info']['down_price'] = ds_price_format($goods_info['goods_price'] * (1 - $goods_info['pintuan_info']['pintuan_zhe']));
  456. $goods_info['goods_price'] = round(($goods_info['pintuan_info']['pintuan_zhe'] * $goods_info['goods_price']) / 10, 2);
  457. $goods_info['promotions_id'] = $goods_info['pintuan_info']['pintuan_id'];
  458. $goods_info['ifpintuan'] = true;
  459. }
  460. public function updatePintuan($post_data, $goods_info, $order, $order_type, $member_id)
  461. {
  462. $res = array();
  463. //拼团订单的特殊性,还需要额外的进行处理.
  464. $pintuangroup_id = intval($post_data['pintuangroup_id']);
  465. $pintuan_id = intval($post_data['pintuan_id']);
  466. $res['pintuangroup_id'] = $pintuangroup_id;
  467. $pintuanorder_isfirst = 0; #是否为首团订单
  468. if ($pintuangroup_id == 0) {
  469. //首团订单新增
  470. $data = array(
  471. 'pintuan_id' => $pintuan_id,
  472. 'pintuangroup_goods_id' => $goods_info['goods_id'],
  473. 'pintuangroup_is_virtual' => $goods_info['is_virtual'],
  474. 'pintuangroup_joined' => 0,
  475. 'pintuangroup_limit_number' => $goods_info['pintuan_info']['pintuan_limit_number'],
  476. 'pintuangroup_limit_hour' => $goods_info['pintuan_info']['pintuan_limit_hour'],
  477. 'pintuangroup_headid' => $member_id,
  478. 'pintuangroup_starttime' => TIMESTAMP,
  479. );
  480. $pintuangroup_id = model('ppintuangroup')->addPpintuangroup($data);
  481. $res['pintuangroup_id'] = $pintuangroup_id;
  482. $pintuanorder_isfirst = 1;
  483. }
  484. //新增订单
  485. $data = array(
  486. 'pintuan_id' => $pintuan_id,
  487. 'pintuangroup_id' => $pintuangroup_id,
  488. 'order_id' => $order['order_id'],
  489. 'order_sn' => $order['order_sn'],
  490. 'pintuanorder_isfirst' => $pintuanorder_isfirst,
  491. 'pintuanorder_type' => $order_type,
  492. );
  493. model('ppintuanorder')->addPpintuanorder($data);
  494. //下单后清除缓存
  495. model('ppintuan')->_dGoodsPintuanCache($goods_info['pintuan_info']['pintuan_goods_commonid']);
  496. return $res;
  497. }
  498. /**
  499. * 判断商品是不是享受会员等级折扣,
  500. * @param array $goods_info
  501. * @param number $level 会员等级
  502. */
  503. public function getMgdiscountInfo(&$goods_info)
  504. {
  505. $member_model = model('member');
  506. $member_info = $member_model->getMemberInfoByID(session('member_id'));
  507. if (!$member_info) {
  508. return;
  509. }
  510. $member_gradeinfo = $member_model->getOneMemberGrade(intval($member_info['member_exppoints']));
  511. if (!$member_gradeinfo) {
  512. return;
  513. }
  514. $level = intval($member_gradeinfo['level']);
  515. if (!config('ds_config.mgdiscount_allow') || empty($goods_info['mgdiscount_info']) || $level <= 0) {
  516. return;
  517. }
  518. $mgdiscount = $goods_info['mgdiscount_info'][$level];
  519. if (empty($mgdiscount)) {
  520. return;
  521. } else {
  522. if ($mgdiscount['level_discount'] < 10) {
  523. $goods_info['goods_price'] = round(($mgdiscount['level_discount'] * $goods_info['goods_price']) / 10, 2);
  524. $goods_info['mgdiscount_desc'] = '会员享受' . $mgdiscount['level_discount'] . '折';
  525. $goods_info['ifmgdiscount'] = true;
  526. }
  527. }
  528. }
  529. /**
  530. * 输出有货到付款时,在线支付和货到付款及每种支付下商品数量和详细列表
  531. * @param $buy_list 商品列表
  532. * @return 返回 以支付方式为下标分组的商品列表
  533. */
  534. public function getOfflineGoodsPay($buy_list)
  535. {
  536. //以支付方式为下标,存放购买商品
  537. $buy_goods_list = array();
  538. $offline_pay = model('payment')->getPaymentOpenInfo(array(array('payment_code', '=', 'offline')));
  539. if ($offline_pay) {
  540. //下单里包括平台自营商品并且平台已开启货到付款,则显示货到付款项及对应商品数量,取出支持货到付款的店铺ID组成的数组,目前就一个,DEFAULT_PLATFORM_STORE_ID
  541. $offline_store_id_array = model('store')->getOwnShopIds();
  542. foreach ($buy_list as $value) {
  543. if (in_array($value['store_id'], $offline_store_id_array)) {
  544. $buy_goods_list['offline'][] = $value;
  545. } else {
  546. $buy_goods_list['online'][] = $value;
  547. }
  548. }
  549. }
  550. return $buy_goods_list;
  551. }
  552. /**
  553. * 计算每个店铺(所有店铺级优惠活动)总共优惠多少金额
  554. * @param array $store_goods_total 最初店铺商品总金额
  555. * @param array $store_final_goods_total 去除各种店铺级促销后,最终店铺商品总金额(不含运费)
  556. * @return array
  557. */
  558. public function getStorePromotionTotal($store_goods_total, $store_final_goods_total)
  559. {
  560. if (!is_array($store_goods_total) || !is_array($store_final_goods_total))
  561. return array();
  562. $store_promotion_total = array();
  563. foreach ($store_goods_total as $store_id => $goods_total) {
  564. $store_promotion_total[$store_id] = abs($goods_total - $store_final_goods_total[$store_id]);
  565. }
  566. return $store_promotion_total;
  567. }
  568. /**
  569. * 返回需要计算运费的店铺ID组成的数组 和 免运费店铺ID及免运费下限金额描述
  570. * @param array $store_goods_total 每个店铺的商品金额小计,以店铺ID为下标
  571. * @return array
  572. */
  573. public function getStoreFreightDescList($store_goods_total)
  574. {
  575. if (empty($store_goods_total) || !is_array($store_goods_total))
  576. return array(array(), array());
  577. //定义返回数组
  578. $need_calc_sid_array = array();
  579. $cancel_calc_sid_array = array();
  580. //如果商品金额未达到免运费设置下线,则需要计算运费
  581. $condition = array();
  582. $condition[] = array('store_id', 'in', array_keys($store_goods_total));
  583. $store_list = model('store')->getStoreOnlineList($condition, null, '', 'store_id,store_free_price');
  584. foreach ($store_list as $store_info) {
  585. $limit_price = floatval($store_info['store_free_price']);
  586. if ($limit_price == 0 || $limit_price > $store_goods_total[$store_info['store_id']]) {
  587. //需要计算运费
  588. $need_calc_sid_array[] = $store_info['store_id'];
  589. } else {
  590. //返回免运费金额下限
  591. $cancel_calc_sid_array[$store_info['store_id']]['free_price'] = $limit_price;
  592. $cancel_calc_sid_array[$store_info['store_id']]['desc'] = sprintf('满%s免运费', $limit_price);
  593. }
  594. }
  595. return array($need_calc_sid_array, $cancel_calc_sid_array);
  596. }
  597. /**
  598. * 取得店铺运费(使用运费模板的商品运费不会计算,但会返回模板信息)
  599. * 先将免运费的店铺运费置0,然后算出店铺里没使用运费模板的商品运费之和 ,存到iscalced下标中
  600. * 然后再计算使用运费模板的信息(array(店铺ID=>array(运费模板ID=>购买数量)),放到nocalced下标里
  601. * @param array $buy_list 购买商品列表
  602. * @param array $free_freight_sid_list 免运费的店铺ID数组
  603. */
  604. public function getStoreFreightList($buy_list = array(), $free_freight_sid_list)
  605. {
  606. //定义返回数组
  607. $return = array();
  608. //先将免运费的店铺运费置0(格式:店铺ID=>0)
  609. $freight_list = array();
  610. if (!empty($free_freight_sid_list) && is_array($free_freight_sid_list)) {
  611. foreach ($free_freight_sid_list as $store_id) {
  612. $freight_list[$store_id] = 0;
  613. }
  614. }
  615. //然后算出店铺里没使用运费模板(优惠套装商品除外)的商品运费之和(格式:店铺ID=>运费)
  616. //定义数组,存放店铺优惠套装商品运费总额 store_id=>运费
  617. $store_bl_goods_freight = array();
  618. foreach ($buy_list as $key => $goods_info) {
  619. //免运费店铺的商品不需要计算
  620. if (in_array($goods_info['store_id'], $free_freight_sid_list)) {
  621. unset($buy_list[$key]);
  622. continue;
  623. }
  624. //优惠套装商品运费另算
  625. if (intval($goods_info['bl_id'])) {
  626. unset($buy_list[$key]);
  627. $store_bl_goods_freight[$goods_info['store_id']] = $goods_info['bl_id'];
  628. continue;
  629. }
  630. if (!intval($goods_info['transport_id']) && !in_array($goods_info['store_id'], $free_freight_sid_list)) {
  631. if (!isset($freight_list[$goods_info['store_id']])) {
  632. $freight_list[$goods_info['store_id']] = $goods_info['goods_freight'];
  633. } else {
  634. $freight_list[$goods_info['store_id']] += $goods_info['goods_freight'];
  635. }
  636. unset($buy_list[$key]);
  637. }
  638. }
  639. //计算优惠套装商品运费
  640. if (!empty($store_bl_goods_freight)) {
  641. $pbundling_model = model('pbundling');
  642. foreach (array_unique($store_bl_goods_freight) as $store_id => $bl_id) {
  643. $bl_info = $pbundling_model->getBundlingInfo(array('bl_id' => $bl_id));
  644. if (!empty($bl_info)) {
  645. if (!isset($freight_list[$store_id])) {
  646. $freight_list[$store_id] = $bl_info['bl_freight'];
  647. } else {
  648. $freight_list[$store_id] += $bl_info['bl_freight'];
  649. }
  650. }
  651. }
  652. }
  653. $return['iscalced'] = $freight_list;
  654. //最后再计算使用运费模板的信息(店铺ID,运费模板ID,购买数量),使用使用相同运费模板的商品数量累加
  655. $freight_list = array();
  656. foreach ($buy_list as $goods_info) {
  657. if (!isset($freight_list[$goods_info['store_id']])) {
  658. $freight_list[$goods_info['store_id']] = array();
  659. }
  660. if (!isset($freight_list[$goods_info['store_id']][$goods_info['transport_id']])) {
  661. $freight_list[$goods_info['store_id']][$goods_info['transport_id']] = array('count' => 0, 'weight' => 0);
  662. }
  663. $freight_list[$goods_info['store_id']][$goods_info['transport_id']]['count'] += $goods_info['goods_num'];
  664. $freight_list[$goods_info['store_id']][$goods_info['transport_id']]['weight'] += $goods_info['goods_num'] * $goods_info['goods_weight'];
  665. }
  666. $return['nocalced'] = $freight_list;
  667. return $return;
  668. }
  669. /**
  670. * 根据地区选择计算出所有店铺最终运费
  671. * @param array $freight_list 运费信息(店铺ID,运费,运费模板ID,购买数量)
  672. * @param int $city_id 市级ID
  673. * @return array 返回店铺ID=>运费
  674. */
  675. public function calcStoreFreight($freight_list, $city_id)
  676. {
  677. if (!is_array($freight_list) || empty($freight_list) || empty($city_id))
  678. return;
  679. //免费和固定运费计算结果
  680. $return_list = $freight_list['iscalced'];
  681. //使用运费模板的信息(array(店铺ID=>array(运费模板ID=>购买数量))
  682. $nocalced_list = $freight_list['nocalced'];
  683. //然后计算使用运费运费模板的在该$city_id时的运费值
  684. if (!empty($nocalced_list) && is_array($nocalced_list)) {
  685. //如果有商品使用的运费模板,先计算这些商品的运费总金额
  686. $transport_model = model('transport');
  687. foreach ($nocalced_list as $store_id => $value) {
  688. if (is_array($value)) {
  689. foreach ($value as $transport_id => $v) {
  690. $freight_total = $transport_model->calcTransport($transport_id, $city_id, $v['count'], $v['weight']);
  691. if ($freight_total === false) {
  692. return;
  693. } else {
  694. if (empty($return_list[$store_id])) {
  695. $return_list[$store_id] = $freight_total;
  696. } else {
  697. $return_list[$store_id] += $freight_total;
  698. }
  699. }
  700. }
  701. }
  702. }
  703. }
  704. return $return_list;
  705. }
  706. /**
  707. * 追加赠品到下单列表,并更新购买数量
  708. * @param array $store_cart_list 购买列表
  709. * @param array $store_premiums_list 赠品列表
  710. * @param array $store_mansong_rule_list 满即送规则
  711. */
  712. public function appendPremiumsToCartList($store_cart_list, $store_premiums_list = array(), $store_mansong_rule_list = array(), $member_id)
  713. {
  714. if (empty($store_cart_list))
  715. return array();
  716. //处理商品级赠品
  717. foreach ($store_cart_list as $store_id => $cart_list) {
  718. foreach ($cart_list as $cart_info) {
  719. if (empty($cart_info['gift_list']))
  720. continue;
  721. if (!is_array($store_premiums_list))
  722. $store_premiums_list = array();
  723. if (!array_key_exists($store_id, $store_premiums_list))
  724. $store_premiums_list[$store_id] = array();
  725. $zenpin_info = array();
  726. foreach ($cart_info['gift_list'] as $gift_info) {
  727. $zenpin_info['goods_id'] = $gift_info['gift_goodsid'];
  728. $zenpin_info['goods_name'] = $gift_info['gift_goodsname'];
  729. $zenpin_info['goods_image'] = $gift_info['gift_goodsimage'];
  730. $zenpin_info['goods_storage'] = $gift_info['goods_storage'];
  731. $zenpin_info['goods_num'] = $cart_info['goods_num'] * $gift_info['gift_amount'];
  732. $store_premiums_list[$store_id][] = $zenpin_info;
  733. }
  734. }
  735. }
  736. //取得每种商品的库存[含赠品]
  737. $goods_storage_quantity = $this->_getEachGoodsStorageQuantity($store_cart_list, $store_premiums_list);
  738. //取得每种商品的购买量[不含赠品]
  739. $goods_buy_quantity = $this->_getEachGoodsBuyQuantity($store_cart_list);
  740. foreach ($goods_buy_quantity as $goods_id => $quantity) {
  741. $goods_storage_quantity[$goods_id] -= $quantity;
  742. if ($goods_storage_quantity[$goods_id] < 0) {
  743. //商品库存不足,请重购买
  744. return false;
  745. }
  746. }
  747. //将赠品追加到购买列表
  748. if (is_array($store_premiums_list)) {
  749. foreach ($store_premiums_list as $store_id => $goods_list) {
  750. $zp_list = array();
  751. $gift_desc = '';
  752. foreach ($goods_list as $goods_info) {
  753. //如果没有库存了,则不再送赠品
  754. if ($goods_storage_quantity[$goods_info['goods_id']] == 0) {
  755. $gift_desc = ',赠品库存不足,未能全部送出 ';
  756. continue;
  757. }
  758. $new_data = array();
  759. $new_data['chain_id'] = 0;
  760. $new_data['buyer_id'] = $member_id;
  761. $new_data['store_id'] = $store_id;
  762. $new_data['store_name'] = $store_cart_list[$store_id][0]['store_name'];
  763. $new_data['goods_id'] = $goods_info['goods_id'];
  764. $new_data['goods_name'] = $goods_info['goods_name'];
  765. $new_data['goods_price'] = 0;
  766. $new_data['goods_image'] = $goods_info['goods_image'];
  767. $new_data['bl_id'] = 0;
  768. $new_data['state'] = true;
  769. $new_data['storage_state'] = true;
  770. $new_data['gc_id'] = 0;
  771. $new_data['goods_weight'] = 0;
  772. $new_data['gc_id_1'] = 0;
  773. $new_data['gc_id_2'] = 0;
  774. $new_data['gc_id_3'] = 0;
  775. $new_data['transport_id'] = 0;
  776. $new_data['goods_freight'] = 0;
  777. $new_data['goods_vat'] = 0;
  778. $new_data['goods_total'] = 0;
  779. $new_data['ifzengpin'] = true;
  780. //计算赠送数量,有就赠,赠完为止
  781. if ($goods_storage_quantity[$goods_info['goods_id']] - $goods_info['goods_num'] >= 0) {
  782. if (!isset($goods_buy_quantity[$goods_info['goods_id']])) {
  783. $goods_buy_quantity[$goods_info['goods_id']] = $goods_info['goods_num'];
  784. } else {
  785. $goods_buy_quantity[$goods_info['goods_id']] += $goods_info['goods_num'];
  786. }
  787. $goods_storage_quantity[$goods_info['goods_id']] -= $goods_info['goods_num'];
  788. $new_data['goods_num'] = $goods_info['goods_num'];
  789. } else {
  790. $new_data['goods_num'] = $goods_storage_quantity[$goods_info['goods_id']];
  791. $goods_buy_quantity[$goods_info['goods_id']] += $goods_storage_quantity[$goods_info['goods_id']];
  792. $goods_storage_quantity[$goods_info['goods_id']] = 0;
  793. }
  794. if (array_key_exists($goods_info['goods_id'], $zp_list)) {
  795. $zp_list[$goods_info['goods_id']]['goods_num'] += $new_data['goods_num'];
  796. } else {
  797. $zp_list[$goods_info['goods_id']] = $new_data;
  798. }
  799. }
  800. sort($zp_list);
  801. $store_cart_list[$store_id] = array_merge($store_cart_list[$store_id], $zp_list);
  802. @$store_mansong_rule_list[$store_id]['desc'] .= $gift_desc;
  803. @$store_mansong_rule_list[$store_id]['desc'] = trim($store_mansong_rule_list[$store_id]['desc'], ',');
  804. }
  805. }
  806. return array($store_cart_list, $goods_buy_quantity, $store_mansong_rule_list);
  807. }
  808. /**
  809. * 充值卡支付,依次循环每个订单
  810. * 如果充值卡足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  811. */
  812. public function rcbPay($order_list, $input, $buyer_info)
  813. {
  814. $member_id = $buyer_info['member_id'];
  815. $member_name = $buyer_info['member_name'];
  816. $available_rcb_amount = floatval($buyer_info['available_rc_balance']);
  817. if ($available_rcb_amount <= 0)
  818. return;
  819. $order_model = model('order');
  820. $logic_order = model('order', 'logic');
  821. $predeposit_model = model('predeposit');
  822. $canPay = false;
  823. foreach ($order_list as $key => $order_info) {
  824. //货到付款的订单跳过
  825. if ($order_info['payment_code'] == 'offline')
  826. continue;
  827. if (!isset($order_info['rcb_amount'])) {
  828. $order_list[$key]['rcb_amount'] = $order_info['rcb_amount'] = 0;
  829. }
  830. if (!isset($order_info['pd_amount'])) {
  831. $order_list[$key]['pd_amount'] = $order_info['pd_amount'] = 0;
  832. }
  833. $order_amount = round(($order_info['order_state'] == ORDER_STATE_DEPOSIT ? $order_info['presell_deposit_amount'] : ($order_info['order_amount'] - $order_info['presell_deposit_amount'] + $order_info['presell_rcb_amount'] + $order_info['presell_pd_amount'])) - $order_info['rcb_amount'] - $order_info['pd_amount'], 2);
  834. $data_pd = array();
  835. $data_pd['member_id'] = $member_id;
  836. $data_pd['member_name'] = $member_name;
  837. $data_pd['order_sn'] = $order_info['order_sn'];
  838. //暂冻结充值卡,后面还需要 API彻底完成支付
  839. if ($available_rcb_amount > 0 && $order_amount > 0) {
  840. if ($available_rcb_amount >= $order_amount) {
  841. $available_rcb_amount -= $order_amount;
  842. $data_pd['amount'] = $order_amount;
  843. $canPay = true;
  844. } else {
  845. $data_pd['amount'] = $available_rcb_amount;
  846. $available_rcb_amount = 0;
  847. $canPay = false;
  848. }
  849. $predeposit_model->changeRcb('order_freeze', $data_pd);
  850. //支付金额保存到订单
  851. $data_order = array();
  852. $order_list[$key]['rcb_amount'] = $data_order['rcb_amount'] = round($order_info['rcb_amount'] + $data_pd['amount'], 2);
  853. $result = $order_model->editOrder($data_order, array('order_id' => $order_info['order_id']));
  854. if (!$result) {
  855. throw new \think\Exception('订单更新失败', 10006);
  856. }
  857. }
  858. }
  859. if ($canPay) { //收到货款
  860. $logic_order->changeOrderReceivePay($order_list, 'buyer', $member_name, array('payment_code' => 'predeposit'));
  861. }
  862. return $order_list;
  863. }
  864. /**
  865. * 预存款支付,依次循环每个订单
  866. * 如果预存款足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  867. */
  868. public function pdPay($order_list, $input, $buyer_info)
  869. {
  870. $member_id = $buyer_info['member_id'];
  871. $member_name = $buyer_info['member_name'];
  872. // $payment_model = model('payment');
  873. // $pd_payment_info = $payment_model->getPaymentOpenInfo(array('payment_code'=>'predeposit'));
  874. // if (empty($pd_payment_info)) return;
  875. $available_pd_amount = floatval($buyer_info['available_predeposit']);
  876. if ($available_pd_amount <= 0)
  877. return;
  878. $order_model = model('order');
  879. $logic_order = model('order', 'logic');
  880. $predeposit_model = model('predeposit');
  881. $canPay = false;
  882. foreach ($order_list as $key => $order_info) {
  883. //货到付款的订单、已经充值卡支付的订单跳过
  884. if ($order_info['payment_code'] == 'offline')
  885. continue;
  886. if ($order_info['order_state'] == ORDER_STATE_PAY)
  887. continue;
  888. if (!isset($order_info['rcb_amount'])) {
  889. $order_list[$key]['rcb_amount'] = $order_info['rcb_amount'] = 0;
  890. }
  891. if (!isset($order_info['pd_amount'])) {
  892. $order_list[$key]['pd_amount'] = $order_info['pd_amount'] = 0;
  893. }
  894. $order_amount = round(($order_info['order_state'] == ORDER_STATE_DEPOSIT ? $order_info['presell_deposit_amount'] : ($order_info['order_amount'] - $order_info['presell_deposit_amount'] + $order_info['presell_rcb_amount'] + $order_info['presell_pd_amount'])) - $order_info['rcb_amount'] - $order_info['pd_amount'], 2);
  895. $data_pd = array();
  896. $data_pd['member_id'] = $member_id;
  897. $data_pd['member_name'] = $member_name;
  898. $data_pd['order_sn'] = $order_info['order_sn'];
  899. //暂冻结预存款,后面还需要 API彻底完成支付
  900. if ($available_pd_amount > 0 && $order_amount > 0) {
  901. if ($available_pd_amount >= $order_amount) {
  902. $data_pd['amount'] = $order_amount;
  903. $available_pd_amount -= $order_amount;
  904. $canPay = true;
  905. } else {
  906. $data_pd['amount'] = $available_pd_amount;
  907. $available_pd_amount = 0;
  908. $canPay = false;
  909. }
  910. $predeposit_model->changePd('order_freeze', $data_pd);
  911. //预存款支付金额保存到订单
  912. $data_order = array();
  913. $order_list[$key]['pd_amount'] = $data_order['pd_amount'] = round($order_info['pd_amount'] + $data_pd['amount'], 2);
  914. $result = $order_model->editOrder($data_order, array('order_id' => $order_info['order_id']));
  915. if (!$result) {
  916. throw new \think\Exception('订单更新失败', 10006);
  917. }
  918. }
  919. }
  920. if ($canPay) { //收到货款
  921. $logic_order->changeOrderReceivePay($order_list, 'buyer', $member_name, array('payment_code' => 'predeposit'));
  922. }
  923. return $order_list;
  924. }
  925. /**
  926. * 订单编号生成规则,n(n>=1)个订单表对应一个支付表,
  927. * 生成订单编号(年取1位 + $pay_id取13位 + 第N个子订单取2位)
  928. * 1000个会员同一微秒提订单,重复机率为1/100
  929. * @param $pay_id 支付表自增ID
  930. * @return string
  931. */
  932. public function makeOrderSn($pay_id)
  933. {
  934. //记录生成子订单的个数,如果生成多个子订单,该值会累加
  935. static $num;
  936. if (empty($num)) {
  937. $num = 1;
  938. } else {
  939. $num++;
  940. }
  941. return (date('y', TIMESTAMP) % 9 + 1) . sprintf('%013d', $pay_id) . sprintf('%02d', $num);
  942. }
  943. /**
  944. * 更新库存与销量
  945. *
  946. * @param array $buy_items 商品ID => 购买数量
  947. */
  948. public function editGoodsNum($buy_items)
  949. {
  950. foreach ($buy_items as $goods_id => $buy_num) {
  951. $data = array(
  952. 'goods_storage' => Db::raw('goods_storage-' . $buy_num),
  953. 'goods_salenum' => Db::raw('goods_salenum+' . $buy_num)
  954. );
  955. $result = model('goods')->editGoods($data, array('goods_id' => $goods_id));
  956. if (!$result)
  957. throw new \think\Exception(lang('cart_step2_submit_fail'), 10006);
  958. }
  959. }
  960. /**
  961. * 取得店铺级活动 - 每个店铺可用的满即送活动规则列表
  962. * @param unknown $store_id_array 店铺ID数组
  963. */
  964. public function getMansongruleList($store_id_array)
  965. {
  966. if (!config('ds_config.promotion_allow') || empty($store_id_array) || !is_array($store_id_array))
  967. return array();
  968. $pmansong_model = model('pmansong');
  969. $mansong_rule_list = array();
  970. foreach ($store_id_array as $store_id) {
  971. $store_mansong_rule = $pmansong_model->getMansongInfoByStoreID($store_id);
  972. if (!empty($store_mansong_rule['rules']) && is_array($store_mansong_rule['rules'])) {
  973. foreach ($store_mansong_rule['rules'] as $rule_info) {
  974. //如果减金额 或 有赠品(在售且有库存)
  975. if (!empty($rule_info['mansongrule_discount']) || (!empty($rule_info['mansong_goods_name']) && !empty($rule_info['goods_storage']))) {
  976. $mansong_rule_list[$store_id][] = $this->_parseMansongruleDesc($rule_info);
  977. }
  978. }
  979. }
  980. }
  981. return $mansong_rule_list;
  982. }
  983. /**
  984. * 取得哪些店铺有满免运费活动
  985. * @param array $store_id_array 店铺ID数组
  986. * @return array
  987. */
  988. public function getFreeFreightActiveList($store_id_array)
  989. {
  990. if (empty($store_id_array) || !is_array($store_id_array))
  991. return array();
  992. //定义返回数组
  993. $store_free_freight_active = array();
  994. //如果商品金额未达到免运费设置下线,则需要计算运费
  995. $condition = array();
  996. $condition[] = array('store_id', 'in', $store_id_array);
  997. $store_list = model('store')->getStoreOnlineList($condition, null, '', 'store_id,store_free_price');
  998. foreach ($store_list as $store_info) {
  999. $limit_price = floatval($store_info['store_free_price']);
  1000. if ($limit_price > 0) {
  1001. $store_free_freight_active[$store_info['store_id']] = sprintf('满%s免运费', $limit_price);
  1002. }
  1003. }
  1004. return $store_free_freight_active;
  1005. }
  1006. /**
  1007. * 取得收货人地址信息
  1008. * @param array $address_info
  1009. * @return array
  1010. */
  1011. public function getReciverAddr($address_info = array())
  1012. {
  1013. if (intval($address_info['chain_id'])) {
  1014. $reciver_info['phone'] = trim($address_info['address_mob_phone'] . ($address_info['address_tel_phone'] ? ',' . $address_info['address_tel_phone'] : null), ',');
  1015. $reciver_info['tel_phone'] = $address_info['address_tel_phone'];
  1016. $reciver_info['mob_phone'] = $address_info['address_mob_phone'];
  1017. $reciver_info['address'] = $address_info['chain_area_info'] . ' ' . $address_info['chain_address'];
  1018. $reciver_info['area'] = $address_info['chain_area_info'];
  1019. $reciver_info['street'] = $address_info['chain_address'];
  1020. $reciver_info['chain'] = 1;
  1021. $reciver_info = serialize($reciver_info);
  1022. $reciver_name = $address_info['chain_addressname'];
  1023. } else {
  1024. $reciver_info['phone'] = trim($address_info['address_mob_phone'] . ($address_info['address_tel_phone'] ? ',' . $address_info['address_tel_phone'] : null), ',');
  1025. $reciver_info['mob_phone'] = $address_info['address_mob_phone'];
  1026. $reciver_info['tel_phone'] = $address_info['address_tel_phone'];
  1027. $reciver_info['address'] = $address_info['area_info'] . ' ' . $address_info['address_detail'];
  1028. $reciver_info['area'] = $address_info['area_info'];
  1029. $reciver_info['street'] = $address_info['address_detail'];
  1030. $reciver_info = serialize($reciver_info);
  1031. $reciver_name = $address_info['address_realname'];
  1032. }
  1033. return array($reciver_info, $reciver_name);
  1034. }
  1035. /**
  1036. * 整理发票信息
  1037. * @param array $invoice_info 发票信息数组
  1038. * @return string
  1039. */
  1040. public function createInvoiceData($invoice_info)
  1041. {
  1042. //发票信息
  1043. $inv = array();
  1044. if (isset($invoice_info['invoice_state']) && $invoice_info['invoice_state'] == 1) {
  1045. $inv['类型'] = '普通发票 ';
  1046. $inv['抬头'] = isset($invoice_info['invoice_title']) ? $invoice_info['invoice_title'] : '个人';
  1047. $inv['内容'] = $invoice_info['invoice_content'];
  1048. $inv['纳税人识别号'] = $invoice_info['invoice_code'];
  1049. } elseif (!empty($invoice_info)) {
  1050. $inv['单位名称'] = $invoice_info['invoice_company'];
  1051. $inv['纳税人识别号'] = $invoice_info['invoice_company_code'];
  1052. $inv['注册地址'] = $invoice_info['invoice_reg_addr'];
  1053. $inv['注册电话'] = $invoice_info['invoice_reg_phone'];
  1054. $inv['开户银行'] = $invoice_info['invoice_reg_bname'];
  1055. $inv['银行账户'] = $invoice_info['invoice_reg_baccount'];
  1056. // $inv['收票人姓名'] = $invoice_info['invoice_rec_name'];
  1057. // $inv['收票人手机号'] = $invoice_info['invoice_rec_mobphone'];
  1058. // $inv['收票人省份'] = $invoice_info['invoice_rec_province'];
  1059. // $inv['送票地址'] = $invoice_info['invoice_goto_addr'];
  1060. }
  1061. return !empty($inv) ? serialize($inv) : serialize(array());
  1062. }
  1063. /**
  1064. * 计算本次下单中每个店铺订单是货到付款还是线上支付,店铺ID=>付款方式[online在线支付offline货到付款]
  1065. * @param array $store_id_array 店铺ID数组
  1066. * @param boolean $if_offpay 是否支持货到付款 true/false
  1067. * @param string $pay_name 付款方式 online/offline
  1068. * @return array
  1069. */
  1070. public function getStorePayTypeList($store_id_array, $if_offpay, $pay_name)
  1071. {
  1072. $store_pay_type_list = array();
  1073. if ($pay_name == 'online') {
  1074. foreach ($store_id_array as $store_id) {
  1075. $store_pay_type_list[$store_id] = 'online';
  1076. }
  1077. } else {
  1078. $offline_pay = model('payment')->getPaymentOpenInfo(array(array('payment_code', '=', 'offline')));
  1079. if ($offline_pay) {
  1080. //下单里包括平台自营商品并且平台已开启货到付款
  1081. $offline_store_id_array = model('store')->getOwnShopIds();
  1082. foreach ($store_id_array as $store_id) {
  1083. //if (in_array($store_id,$offline_store_id_array)) {
  1084. $store_pay_type_list[$store_id] = 'offline';
  1085. //} else {
  1086. // $store_pay_type_list[$store_id] = 'online';
  1087. //}
  1088. }
  1089. }
  1090. }
  1091. return $store_pay_type_list;
  1092. }
  1093. /**
  1094. * 直接购买时返回最新的在售商品信息(需要在售)
  1095. *
  1096. * @param int $goods_id 所购商品ID
  1097. * @param int $quantity 购买数量
  1098. * @return array
  1099. */
  1100. private function _getGoodsOnlineInfo($goods_id, $quantity)
  1101. {
  1102. //取目前在售商品
  1103. $goods_model = model('goods');
  1104. $goods_model->lock = $this->lock;
  1105. $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($goods_id);
  1106. if (empty($goods_info)) {
  1107. return null;
  1108. }
  1109. $new_array = array();
  1110. $new_array['goods_num'] = $goods_info['is_goodsfcode'] ? 1 : $quantity;
  1111. $new_array['goods_id'] = $goods_id;
  1112. $new_array['goods_commonid'] = $goods_info['goods_commonid'];
  1113. $new_array['is_virtual'] = $goods_info['is_virtual'];
  1114. $new_array['gc_id'] = $goods_info['gc_id'];
  1115. $new_array['goods_weight'] = $goods_info['goods_weight'];
  1116. $new_array['gc_id_1'] = $goods_info['gc_id_1'];
  1117. $new_array['gc_id_2'] = $goods_info['gc_id_2'];
  1118. $new_array['gc_id_3'] = $goods_info['gc_id_3'];
  1119. $new_array['store_id'] = $goods_info['store_id'];
  1120. $new_array['goods_name'] = $goods_info['goods_name'];
  1121. $new_array['goods_price'] = $goods_info['goods_price'];
  1122. $new_array['goods_original_price'] = $goods_info['goods_original_price'];
  1123. $new_array['store_name'] = $goods_info['store_name'];
  1124. $new_array['goods_image'] = $goods_info['goods_image'];
  1125. $new_array['transport_id'] = $goods_info['transport_id'];
  1126. $new_array['goods_freight'] = $goods_info['goods_freight'];
  1127. $new_array['goods_vat'] = $goods_info['goods_vat'];
  1128. $new_array['goods_storage'] = $goods_info['goods_storage'];
  1129. $new_array['goods_storage_alarm'] = $goods_info['goods_storage_alarm'];
  1130. $new_array['is_goodsfcode'] = $goods_info['is_goodsfcode'];
  1131. $new_array['is_have_gift'] = $goods_info['is_have_gift'];
  1132. $new_array['state'] = true;
  1133. $new_array['storage_state'] = intval($goods_info['goods_storage']) < intval($quantity) ? false : true;
  1134. $new_array['groupbuy_info'] = $goods_info['groupbuy_info'];
  1135. $new_array['xianshi_info'] = $goods_info['xianshi_info'];
  1136. $new_array['presell_info'] = $goods_info['presell_info'];
  1137. $new_array['wholesale_info'] = $goods_info['wholesale_info'];
  1138. $new_array['pintuan_info'] = $goods_info['pintuan_info'];
  1139. $new_array['bargain_info'] = $goods_info['bargain_info'];
  1140. $new_array['mgdiscount_info'] = $goods_info['mgdiscount_info'];
  1141. //填充必要下标,方便后面统一使用购物车方法与模板
  1142. //cart_id=goods_id,优惠套装目前只能进购物车,不能立即购买
  1143. $new_array['cart_id'] = $goods_id;
  1144. $new_array['bl_id'] = 0;
  1145. return $new_array;
  1146. }
  1147. /**
  1148. * 直接购买时,判断商品是不是正在抢购中,如果是,按抢购价格计算,购买数量若超过抢购规定的上限,则按抢购上限计算
  1149. * @param array $goods_info
  1150. */
  1151. public function getGroupbuyInfo(&$goods_info = array(), $quantity)
  1152. {
  1153. if (!config('ds_config.groupbuy_allow') || empty($goods_info['groupbuy_info']))
  1154. return;
  1155. $groupbuy_info = $goods_info['groupbuy_info'];
  1156. $temp = array();
  1157. $temp['goods_num'] = $quantity;
  1158. $temp['goods_price'] = $groupbuy_info['groupbuy_price'];
  1159. if ($groupbuy_info['groupbuy_upper_limit'] && $quantity > $groupbuy_info['groupbuy_upper_limit']) {
  1160. $temp['goods_num'] = $groupbuy_info['groupbuy_upper_limit'];
  1161. }
  1162. $temp['upper_limit'] = $groupbuy_info['groupbuy_upper_limit'];
  1163. $temp['promotions_id'] = $temp['groupbuy_id'] = $groupbuy_info['groupbuy_id'];
  1164. $temp['ifgroupbuy'] = true;
  1165. if ($groupbuy_info['groupbuy_upper_limit'] > 0) { //如果限购
  1166. $ordergoods = Db::name('ordergoods')->where(array('buyer_id' => session('member_id'), 'goods_type' => 2, 'promotions_id' => $groupbuy_info['groupbuy_id']))->sum('goods_num');
  1167. if (!empty($ordergoods) && intval($ordergoods) > 0) {
  1168. $tnum = intval($groupbuy_info['groupbuy_upper_limit']) - intval($ordergoods); //-intval($goods_info['goods_num']);
  1169. if ($tnum <= 0)
  1170. // $goods_info = null;
  1171. return;
  1172. else {
  1173. if ($temp['goods_num'] > $tnum) {
  1174. $temp['goods_num'] = $tnum;
  1175. }
  1176. }
  1177. }
  1178. }
  1179. $goods_info = array_merge($goods_info, $temp);
  1180. //end
  1181. }
  1182. /**
  1183. * 取得某商品赠品列表信息
  1184. * @param array $goods_info
  1185. */
  1186. private function _getGoodsgiftList(&$goods_info)
  1187. {
  1188. if (!isset($goods_info['is_have_gift']))
  1189. return;
  1190. $gift_list = model('goodsgift')->getGoodsgiftListByGoodsId($goods_info['goods_id']);
  1191. //取得赠品当前信息,如果未在售踢除,如果在售取出库存
  1192. if (empty($gift_list))
  1193. return array();
  1194. $goods_model = model('goods');
  1195. foreach ($gift_list as $k => $v) {
  1196. $goods_online_info = $goods_model->getGoodsOnlineInfoByID($v['gift_goodsid']);
  1197. if (empty($goods_online_info)) {
  1198. unset($gift_list[$k]);
  1199. } else {
  1200. $gift_list[$k]['goods_storage'] = $goods_online_info['goods_storage'];
  1201. }
  1202. }
  1203. $goods_info['gift_list'] = $gift_list;
  1204. }
  1205. /**
  1206. * 取商品最新的在售信息
  1207. * @param unknown $cart_list
  1208. * @return array
  1209. */
  1210. private function _getOnlineCartList($cart_list)
  1211. {
  1212. if (empty($cart_list) || !is_array($cart_list))
  1213. return $cart_list;
  1214. //验证商品是否有效
  1215. $goods_id_array = array();
  1216. foreach ($cart_list as $key => $cart_info) {
  1217. if (!intval($cart_info['bl_id'])) {
  1218. $goods_id_array[] = $cart_info['goods_id'];
  1219. }
  1220. }
  1221. $goods_model = model('goods');
  1222. $goods_model->lock = $this->lock;
  1223. $goods_online_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  1224. $goods_online_array = array();
  1225. foreach ($goods_online_list as $goods) {
  1226. $goods_online_array[$goods['goods_id']] = $goods;
  1227. }
  1228. foreach ((array) $cart_list as $key => $cart_info) {
  1229. if (intval($cart_info['bl_id']))
  1230. continue;
  1231. $cart_list[$key]['state'] = true;
  1232. $cart_list[$key]['storage_state'] = true;
  1233. $cart_list[$key]['chain_id'] = 0;
  1234. if (in_array($cart_info['goods_id'], array_keys($goods_online_array))) {
  1235. $goods_online_info = $goods_online_array[$cart_info['goods_id']];
  1236. $cart_list[$key]['goods_commonid'] = $goods_online_info['goods_commonid'];
  1237. $cart_list[$key]['goods_name'] = $goods_online_info['goods_name'];
  1238. $cart_list[$key]['gc_id'] = $goods_online_info['gc_id'];
  1239. $cart_list[$key]['goods_weight'] = $goods_online_info['goods_weight'];
  1240. $cart_list[$key]['gc_id_1'] = $goods_online_info['gc_id_1'];
  1241. $cart_list[$key]['gc_id_2'] = $goods_online_info['gc_id_2'];
  1242. $cart_list[$key]['gc_id_3'] = $goods_online_info['gc_id_3'];
  1243. $cart_list[$key]['goods_image'] = $goods_online_info['goods_image'];
  1244. $cart_list[$key]['goods_price'] = $goods_online_info['goods_price'];
  1245. $cart_list[$key]['goods_original_price'] = $goods_online_info['goods_original_price'];
  1246. $cart_list[$key]['transport_id'] = $goods_online_info['transport_id'];
  1247. $cart_list[$key]['goods_freight'] = $goods_online_info['goods_freight'];
  1248. $cart_list[$key]['goods_vat'] = $goods_online_info['goods_vat'];
  1249. $cart_list[$key]['goods_storage'] = $goods_online_info['goods_storage'];
  1250. $cart_list[$key]['goods_storage_alarm'] = $goods_online_info['goods_storage_alarm'];
  1251. $cart_list[$key]['is_goodsfcode'] = $goods_online_info['is_goodsfcode'];
  1252. $cart_list[$key]['is_have_gift'] = $goods_online_info['is_have_gift'];
  1253. if ($cart_info['goods_num'] > $goods_online_info['goods_storage']) {
  1254. $cart_list[$key]['storage_state'] = false;
  1255. }
  1256. $cart_list[$key]['groupbuy_info'] = $goods_online_info['groupbuy_info'];
  1257. $cart_list[$key]['mgdiscount_info'] = $goods_online_info['mgdiscount_info'];
  1258. $cart_list[$key]['xianshi_info'] = $goods_online_info['xianshi_info'];
  1259. $cart_list[$key]['presell_info'] = $goods_online_info['presell_info'];
  1260. $cart_list[$key]['wholesale_info'] = $goods_online_info['wholesale_info'];
  1261. } else {
  1262. //如果商品下架
  1263. $cart_list[$key]['state'] = false;
  1264. $cart_list[$key]['storage_state'] = false;
  1265. }
  1266. }
  1267. return $cart_list;
  1268. }
  1269. /**
  1270. * 直接购买时,判断商品是不是正在抢购中,如果是,按抢购价格计算,购买数量若超过抢购规定的上限,则按抢购上限计算
  1271. * @param array $cart_list
  1272. */
  1273. public function getGroupbuyCartList(&$cart_list)
  1274. {
  1275. if (!config('ds_config.promotion_allow') || empty($cart_list))
  1276. return;
  1277. foreach ($cart_list as $key => $cart_info) {
  1278. if ((isset($cart_info['bl_id']) && $cart_info['bl_id'] === '1') || empty($cart_info['groupbuy_info']))
  1279. continue;
  1280. $this->getGroupbuyInfo($cart_info, $cart_info['goods_num']);
  1281. if ($cart_info) {
  1282. $cart_list[$key] = $cart_info;
  1283. } else {
  1284. unset($cart_list[$key]);
  1285. }
  1286. }
  1287. }
  1288. /**
  1289. * 批量判断购物车内的商品是不是秒杀中,如果购买数量若>=规定的下限,按折扣价格计算,否则按原价计算
  1290. * 并标识该商品为秒杀商品
  1291. * @param array $cart_list
  1292. */
  1293. public function getXianshiCartList(&$cart_list)
  1294. {
  1295. if (!config('ds_config.promotion_allow') || empty($cart_list))
  1296. return;
  1297. foreach ($cart_list as $key => $cart_info) {
  1298. if ((isset($cart_info['bl_id']) && $cart_info['bl_id'] === '1') || empty($cart_info['xianshi_info']))
  1299. continue;
  1300. $this->getXianshiInfo($cart_info, $cart_info['goods_num']);
  1301. $cart_list[$key] = $cart_info;
  1302. }
  1303. }
  1304. /**
  1305. * 批量判断购物车内的商品是不是预售
  1306. * 并标识该商品为秒杀商品
  1307. * @param array $cart_list
  1308. */
  1309. public function getPresellCartList(&$cart_list)
  1310. {
  1311. if (!config('ds_config.promotion_allow') || empty($cart_list))
  1312. return;
  1313. foreach ($cart_list as $key => $cart_info) {
  1314. if ((isset($cart_info['bl_id']) && $cart_info['bl_id'] === '1') || empty($cart_info['presell_info']))
  1315. continue;
  1316. $this->getPresellInfo($cart_info, $cart_info['goods_num']);
  1317. $cart_list[$key] = $cart_info;
  1318. if (count($cart_list) > 1) {
  1319. $cart_list[$key]['state'] = false; //预售商品只能直接购买
  1320. }
  1321. }
  1322. }
  1323. /**
  1324. * 批量判断购物车内的商品是不是批发中,如果购买数量若>=规定的下限,按批发价格计算,否则按原价计算
  1325. * 并标识该商品为秒杀商品
  1326. * @param array $cart_list
  1327. */
  1328. public function getWholesaleCartList(&$cart_list)
  1329. {
  1330. if (!config('ds_config.promotion_allow') || empty($cart_list))
  1331. return;
  1332. foreach ($cart_list as $key => $cart_info) {
  1333. if ((isset($cart_info['bl_id']) && $cart_info['bl_id'] === '1') || empty($cart_info['wholesale_info']))
  1334. continue;
  1335. $this->getWholesaleInfo($cart_info, $cart_info['goods_num']);
  1336. $cart_list[$key] = $cart_info;
  1337. }
  1338. }
  1339. /**
  1340. * 批量判断购物车内的商品是不是会员等级折扣中,如果平台开启会员折扣、商家开启会员折扣并设置,则按折扣价格计算,否则按原价计算
  1341. * 并标识该商品为会员等级折扣
  1342. * @param type $cart_list
  1343. * @return type
  1344. */
  1345. public function getMgdiscountCartList(&$cart_list)
  1346. {
  1347. if (!config('ds_config.mgdiscount_allow') || empty($cart_list))
  1348. return;
  1349. foreach ($cart_list as $key => $cart_info) {
  1350. if (empty($cart_info['mgdiscount_info']))
  1351. continue;
  1352. $this->getMgdiscountInfo($cart_info);
  1353. $cart_list[$key] = $cart_info;
  1354. }
  1355. }
  1356. /**
  1357. * 取得购物车商品的赠品列表[商品级赠品]
  1358. *
  1359. * @param array $cart_list
  1360. */
  1361. private function _getGiftCartList(&$cart_list)
  1362. {
  1363. foreach ($cart_list as $k => $cart_info) {
  1364. if ($cart_info['bl_id'])
  1365. continue;
  1366. $this->_getGoodsgiftList($cart_info);
  1367. $cart_list[$k] = $cart_info;
  1368. }
  1369. }
  1370. /**
  1371. * 取得购买车内组合销售信息以及包含的商品及有效状态
  1372. * @param array $cart_list
  1373. */
  1374. private function _getBundlingCartList(&$cart_list)
  1375. {
  1376. if (!config('ds_config.promotion_allow') || empty($cart_list))
  1377. return;
  1378. $pbundling_model = model('pbundling');
  1379. $goods_model = model('goods');
  1380. foreach ($cart_list as $key => $cart_info) {
  1381. if (!intval($cart_info['bl_id']))
  1382. continue;
  1383. $cart_list[$key]['state'] = true;
  1384. $cart_list[$key]['storage_state'] = true;
  1385. $bl_info = $pbundling_model->getBundlingInfo(array('bl_id' => $cart_info['bl_id']));
  1386. //标志优惠套装是否处于有效状态
  1387. if (empty($bl_info) || !intval($bl_info['bl_state'])) {
  1388. $cart_list[$key]['state'] = false;
  1389. }
  1390. //取得优惠套装商品列表
  1391. $cart_list[$key]['bl_goods_list'] = $pbundling_model->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
  1392. //取最新在售商品信息
  1393. $goods_id_array = array();
  1394. foreach ($cart_list[$key]['bl_goods_list'] as $goods_info) {
  1395. $goods_id_array[] = $goods_info['goods_id'];
  1396. }
  1397. $goods_model->lock = $this->lock;
  1398. $goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  1399. $goods_online_list = array();
  1400. foreach ($goods_list as $goods_info) {
  1401. $goods_online_list[$goods_info['goods_id']] = $goods_info;
  1402. }
  1403. unset($goods_list);
  1404. //使用最新的商品名称、图片,如果一旦有商品下架,则整个套装置置为无效状态
  1405. $total_down_price = 0;
  1406. $tmp_amount = 0;
  1407. foreach ($cart_list[$key]['bl_goods_list'] as $k => $goods_info) {
  1408. if (array_key_exists($goods_info['goods_id'], $goods_online_list)) {
  1409. $goods_online_info = $goods_online_list[$goods_info['goods_id']];
  1410. //如果库存不足,标识false
  1411. if ($cart_info['goods_num'] > $goods_online_info['goods_storage']) {
  1412. $cart_list[$key]['storage_state'] = false;
  1413. }
  1414. $cart_list[$key]['bl_goods_list'][$k]['chain_id'] = 0;
  1415. $cart_list[$key]['bl_goods_list'][$k]['goods_id'] = $goods_online_info['goods_id'];
  1416. $cart_list[$key]['bl_goods_list'][$k]['goods_commonid'] = $goods_online_info['goods_commonid'];
  1417. $cart_list[$key]['bl_goods_list'][$k]['goods_original_price'] = $goods_online_info['goods_original_price'];
  1418. $cart_list[$key]['bl_goods_list'][$k]['store_id'] = $goods_online_info['store_id'];
  1419. $cart_list[$key]['bl_goods_list'][$k]['store_name'] = $goods_online_info['store_name'];
  1420. $cart_list[$key]['bl_goods_list'][$k]['goods_name'] = $goods_online_info['goods_name'];
  1421. $cart_list[$key]['bl_goods_list'][$k]['goods_image'] = $goods_online_info['goods_image'];
  1422. $cart_list[$key]['bl_goods_list'][$k]['transport_id'] = $goods_online_info['transport_id'];
  1423. $cart_list[$key]['bl_goods_list'][$k]['goods_freight'] = $goods_online_info['goods_freight'];
  1424. $cart_list[$key]['bl_goods_list'][$k]['goods_vat'] = $goods_online_info['goods_vat'];
  1425. $cart_list[$key]['bl_goods_list'][$k]['goods_storage'] = $goods_online_info['goods_storage'];
  1426. $cart_list[$key]['bl_goods_list'][$k]['goods_storage_alarm'] = $goods_online_info['goods_storage_alarm'];
  1427. $cart_list[$key]['bl_goods_list'][$k]['gc_id'] = $goods_online_info['gc_id'];
  1428. $cart_list[$key]['bl_goods_list'][$k]['goods_weight'] = $goods_online_info['goods_weight'];
  1429. $cart_list[$key]['bl_goods_list'][$k]['gc_id_1'] = $goods_online_info['gc_id_1'];
  1430. $cart_list[$key]['bl_goods_list'][$k]['gc_id_2'] = $goods_online_info['gc_id_2'];
  1431. $cart_list[$key]['bl_goods_list'][$k]['gc_id_3'] = $goods_online_info['gc_id_3'];
  1432. //每个商品直降多少
  1433. $total_down_price += $cart_list[$key]['bl_goods_list'][$k]['down_price'] = ds_price_format($goods_online_info['goods_price'] - $goods_info['blgoods_price']);
  1434. $tmp_amount += $goods_online_info['goods_original_price'];
  1435. } else {
  1436. //商品已经下架
  1437. $cart_list[$key]['state'] = false;
  1438. $cart_list[$key]['storage_state'] = false;
  1439. }
  1440. }
  1441. $cart_list[$key]['down_price'] = ds_price_format($total_down_price);
  1442. $cart_list[$key]['goods_original_price'] = ds_price_format($tmp_amount);
  1443. }
  1444. }
  1445. /**
  1446. * 取得每种商品的库存
  1447. * @param array $store_cart_list 购买列表
  1448. * @param array $store_premiums_list 赠品列表
  1449. * @return array 商品ID=>库存
  1450. */
  1451. private function _getEachGoodsStorageQuantity($store_cart_list, $store_premiums_list = array())
  1452. {
  1453. if (empty($store_cart_list) || !is_array($store_cart_list))
  1454. return array();
  1455. $goods_storage_quangity = array();
  1456. foreach ($store_cart_list as $store_cart) {
  1457. foreach ($store_cart as $cart_info) {
  1458. if (!intval($cart_info['bl_id'])) {
  1459. //正常商品
  1460. $goods_storage_quangity[$cart_info['goods_id']] = $cart_info['goods_storage'];
  1461. } elseif (!empty($cart_info['bl_goods_list']) && is_array($cart_info['bl_goods_list'])) {
  1462. //优惠套装
  1463. foreach ($cart_info['bl_goods_list'] as $goods_info) {
  1464. $goods_storage_quangity[$goods_info['goods_id']] = $goods_info['goods_storage'];
  1465. }
  1466. }
  1467. }
  1468. }
  1469. //取得赠品商品的库存
  1470. if (is_array($store_premiums_list)) {
  1471. foreach ($store_premiums_list as $store_id => $goods_list) {
  1472. foreach ($goods_list as $goods_info) {
  1473. if (!isset($goods_storage_quangity[$goods_info['goods_id']])) {
  1474. $goods_storage_quangity[$goods_info['goods_id']] = $goods_info['goods_storage'];
  1475. }
  1476. }
  1477. }
  1478. }
  1479. return $goods_storage_quangity;
  1480. }
  1481. /**
  1482. * 取得每种商品的购买量
  1483. * @param array $store_cart_list 购买列表
  1484. * @return array 商品ID=>购买数量
  1485. */
  1486. private function _getEachGoodsBuyQuantity($store_cart_list)
  1487. {
  1488. if (empty($store_cart_list) || !is_array($store_cart_list))
  1489. return array();
  1490. $goods_buy_quangity = array();
  1491. foreach ($store_cart_list as $store_cart) {
  1492. foreach ($store_cart as $cart_info) {
  1493. if (!intval($cart_info['bl_id'])) {
  1494. //正常商品
  1495. if (!isset($goods_buy_quangity[$cart_info['goods_id']])) {
  1496. $goods_buy_quangity[$cart_info['goods_id']] = $cart_info['goods_num'];
  1497. } else {
  1498. $goods_buy_quangity[$cart_info['goods_id']] += $cart_info['goods_num'];
  1499. }
  1500. } elseif (!empty($cart_info['bl_goods_list']) && is_array($cart_info['bl_goods_list'])) {
  1501. //优惠套装
  1502. foreach ($cart_info['bl_goods_list'] as $goods_info) {
  1503. if (!isset($goods_buy_quangity[$goods_info['goods_id']])) {
  1504. $goods_buy_quangity[$goods_info['goods_id']] = $cart_info['goods_num'];
  1505. } else {
  1506. $goods_buy_quangity[$goods_info['goods_id']] += $cart_info['goods_num'];
  1507. }
  1508. }
  1509. }
  1510. }
  1511. }
  1512. return $goods_buy_quangity;
  1513. }
  1514. /**
  1515. * 得到所购买的id和数量
  1516. *
  1517. */
  1518. private function _parseItems($cart_id)
  1519. {
  1520. //存放所购商品ID和数量组成的键值对
  1521. $buy_items = array();
  1522. if (is_array($cart_id)) {
  1523. foreach ($cart_id as $value) {
  1524. if (preg_match_all('/^(\d{1,10})\|(\d{1,6})$/', $value, $match)) {
  1525. $buy_items[$match[1][0]] = $match[2][0];
  1526. }
  1527. }
  1528. }
  1529. return $buy_items;
  1530. }
  1531. /**
  1532. * 拼装单条满即送规则页面描述信息
  1533. * @param array $rule_info 满即送单条规则信息
  1534. * @return string
  1535. */
  1536. private function _parseMansongruleDesc($rule_info)
  1537. {
  1538. if (empty($rule_info) || !is_array($rule_info))
  1539. return;
  1540. $discount_desc = !empty($rule_info['mansongrule_discount']) ? '减' . $rule_info['mansongrule_discount'] : '';
  1541. $goods_desc = (!empty($rule_info['mansong_goods_name']) && !empty($rule_info['goods_storage'])) ? " 送<a href='" . (string) url('home/Goods/index', ['goods_id' => $rule_info['goods_id']]) . "' title='{$rule_info['mansong_goods_name']}' target='_blank'>[赠品]</a>" : '';
  1542. return sprintf('满%s%s%s', $rule_info['mansongrule_price'], $discount_desc, $goods_desc);
  1543. }
  1544. }