Buy_1.php 78 KB

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