Buy_1.php 78 KB

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