Buy_1.php 78 KB

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