buy_first.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/9/19
  6. * Time: 下午6:48
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/activity_helper.php');
  9. require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/pay_helper.php');
  11. require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
  12. class buy_first
  13. {
  14. private $mLogicOut;
  15. private $mUserFcode;
  16. public function __construct($logic_out)
  17. {
  18. $this->mLogicOut = $logic_out;
  19. $this->mUserFcode = null;
  20. }
  21. //商品金额相关信息
  22. private function payinfo()
  23. {
  24. $result = [];
  25. //商品总价,红包可抵扣,满减金额
  26. $goods_amount = 0.00;
  27. $store_goods_total = $this->mLogicOut['store_goods_total'];
  28. foreach ($store_goods_total as $store_id => $value) {
  29. $goods_amount += doubleval($value);
  30. }
  31. $result['goods_amount'] = $goods_amount;
  32. //满送
  33. $full_discount = 0.00;
  34. $full_desc = '';
  35. $store_mansong_rule_list = $this->mLogicOut['store_mansong_rule_list'];
  36. foreach ($store_mansong_rule_list as $store_id => $value) {
  37. $full_discount += doubleval($value['discount']);
  38. $full_desc = $this->full_desc($value['desc']);
  39. }
  40. $result['full_discount'] = $full_discount;
  41. $result['full_desc'] = $full_desc;
  42. $result['full_goods'] = $this->full_goods();
  43. //运费
  44. if($this->freight($freight,$offpay_hash,$offpay_hash_batch) == true) {
  45. $result['freight'] = $freight;
  46. $result['offpay_hash_batch'] = $offpay_hash_batch;
  47. $result['offpay_hash'] = $offpay_hash;
  48. } else {
  49. $result['freight'] = 0;
  50. $result['offpay_hash_batch'] = "";
  51. $result['offpay_hash'] = "";
  52. }
  53. $result['freight_hash'] = $this->mLogicOut['freight_list'];
  54. //红包信息
  55. $result['total_pred'] = doubleval($this->mLogicOut['available_predeposit']);
  56. $result['opgoods_discount'] = $this->optional_goods();
  57. $result['opgoods_desc'] = 'N元任选优惠';
  58. $result['available_pred'] = $this->available_pred($goods_amount,$full_discount,$result['total_pred'],$rates,$discount_gap,$gap_show);
  59. $result['discount_gap'] = $discount_gap;
  60. $result['bonus_rates'] = $this->formate_rates($rates);
  61. $result['gap_show'] = $gap_show;
  62. global $config;
  63. if($gap_show == true)
  64. {
  65. if(intval($discount_gap * 100 + 0.5) == 0) {
  66. $result['gap_desc'] = $config['bonus_gap']['no_gap'];
  67. } else {
  68. $result['gap_desc'] = sprintf($config['bonus_gap']['have_gap'],$discount_gap);
  69. }
  70. } else {
  71. $result['gap_desc'] = "";
  72. }
  73. $result['earn_bonus'] = $config['bonus_gap']['earn_bonus'];
  74. $result['usable_pred'] = true;
  75. $result['pay_cash_pred'] = $result['goods_amount'] + $result['freight'] - $result['full_discount'] - $result['available_pred'] - $result['opgoods_discount'];
  76. $result['pay_cash_nopred'] = $result['goods_amount'] + $result['freight'] - $result['full_discount'] - $result['opgoods_discount'];
  77. //购物车显示应付现金
  78. $result['pay_car_pred'] = $result['goods_amount'] - $result['full_discount'] - $result['available_pred'] - $result['opgoods_discount'];
  79. //商品总价
  80. $result['pay_car_nopred'] = $result['goods_amount'];
  81. //是否可以开增值税发票
  82. $result['vat_deny'] = $this->mLogicOut['vat_deny'];
  83. //发票
  84. $result['vat_hash'] = $this->mLogicOut['vat_hash'];
  85. return $result;
  86. }
  87. private function optional_goods()
  88. {
  89. $discount = 0;
  90. $result = [];
  91. $store_cart_list = $this->mLogicOut['store_cart_list'];
  92. foreach ($store_cart_list as $store_id => $cart_list)
  93. {
  94. if($store_id == 6)
  95. {
  96. $matcher = new optional_match($cart_list);
  97. $op_result = $matcher->match();
  98. if($op_result != false) {
  99. $result[$store_id] = $op_result;
  100. $discount += $op_result['discount'];
  101. } else {
  102. $result[$store_id] = array('discount' => 0,'goods_nums' => []);
  103. }
  104. }
  105. else {
  106. $result[$store_id] = array('discount' => 0,'goods_nums' => []);
  107. }
  108. }
  109. $this->mLogicOut['optional_goods'] = $result;
  110. return $discount;
  111. }
  112. private function formate_rates($rates)
  113. {
  114. if(empty($rates)) return null;
  115. $result = [];
  116. foreach ($rates as $rate => $val) {
  117. $item['rate'] = $rate;
  118. $item['total'] = $val;
  119. $result[] = $item;
  120. }
  121. return $result;
  122. }
  123. private function full_desc($desc)
  124. {
  125. $ret = preg_match('#(.*?)<a.*?title=("|\')(.*?)\2.*?>.*?</a>#s',$desc,$matches);
  126. if($ret != false && count($matches) == 4) {
  127. $desc = $matches[1] . $matches[3];
  128. }
  129. return $desc;
  130. }
  131. private function full_goods()
  132. {
  133. $store_mansong_rule_list = $this->mLogicOut['store_mansong_rule_list'];
  134. foreach ($store_mansong_rule_list as $store_id => $value) {
  135. return intval($value['goods_id']);
  136. }
  137. return 0;
  138. }
  139. private function city_id()
  140. {
  141. $address_info = $this->mLogicOut['address_info'];
  142. if(empty($address_info)) {
  143. return false;
  144. } else {
  145. return intval($address_info['city_id']);
  146. }
  147. }
  148. private function area_id()
  149. {
  150. $address_info = $this->mLogicOut['address_info'];
  151. if(empty($address_info)) {
  152. return false;
  153. } else {
  154. return intval($address_info['area_id']);
  155. }
  156. }
  157. private function paytype()
  158. {
  159. return pay_helper::pay_types();
  160. }
  161. //运费相关信息
  162. private function freight(&$freight,&$offpay_hash,&$offpay_hash_batch)
  163. {
  164. $logic_buy = Logic("buy");
  165. $freight_hash = $this->mLogicOut['freight_list'];
  166. $city_id = $this->city_id();
  167. $area_id = $this->area_id();
  168. $data = $logic_buy->changeAddr($freight_hash, $city_id, $area_id, $_SESSION['member_id']);
  169. if (!empty($data) && $data['state'] == 'success')
  170. {
  171. $offpay_hash = $data['offpay_hash'];
  172. $offpay_hash_batch = $data['offpay_hash_batch'];
  173. $freight = 0.00;
  174. foreach ($data['content'] as $value) {
  175. $freight += $value['value'];
  176. }
  177. return true;
  178. }
  179. else {
  180. return false;
  181. }
  182. }
  183. private function goods_ids()
  184. {
  185. $ids = [];
  186. $sent_id = $this->full_goods();
  187. if($sent_id > 0) {
  188. $ids[] = $sent_id;
  189. }
  190. $carts = $this->goods_list();
  191. foreach ($carts as $item)
  192. {
  193. if($item['bl_id'] > 0)
  194. {
  195. $bl_goods = activity_helper::bundling_goods($item['bl_id']);
  196. if($bl_goods != false) {
  197. foreach ($bl_goods as $gid) {
  198. $ids[] = $gid;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. $ids[] = $item['goods_id'];
  205. }
  206. }
  207. return $ids;
  208. }
  209. private function summary()
  210. {
  211. $goods_ids = $this->goods_ids();
  212. $helper = new goods_helper();
  213. $summaries = $helper->online_summary($goods_ids,$related_goods);
  214. $summary_list = $summaries['summary'];
  215. if(!empty($related_goods)) {
  216. $related_summary = $helper->cart_summary($related_goods,$x);
  217. $summary_list = array_merge($summary_list,$related_summary['summary']);
  218. }
  219. $ret['summary'] = $summary_list;
  220. $ret['groupbuy'] = $summaries['groupbuy'];
  221. $ret['limitime'] = $summaries['limitime'];
  222. $ret['bundling'] = $summaries['bundling'];
  223. return $ret;
  224. }
  225. private function address()
  226. {
  227. return $this->mLogicOut['address_info'];
  228. }
  229. private function goods_list()
  230. {
  231. //购物车中商品列表
  232. $goods_list = [];
  233. $store_cart_list = $this->mLogicOut['store_cart_list'];
  234. foreach ($store_cart_list as $store_id => $carts)
  235. {
  236. foreach ($carts as $value) {
  237. $item['goods_id'] = intval($value['goods_id']);
  238. $item['goods_num'] = intval($value['goods_num']);
  239. $item['bl_id'] = intval($value['bl_id']);
  240. $goods_list[] = $item;
  241. }
  242. }
  243. return $goods_list;
  244. }
  245. private function invoice()
  246. {
  247. return $this->mLogicOut['inv_info'];
  248. }
  249. private function available_pred($goods_amount,$full_discount,$pd_total,&$rates,&$discount_gap,&$gap_show)
  250. {
  251. $full = intval($full_discount * 100 + 0.5);
  252. $pred_amound = $this->pred_goods_amount();
  253. $pred_cent = intval($pred_amound * 100 + 0.5);
  254. if($pred_cent > 0)
  255. {
  256. $gap_show = true;
  257. if($full > 0) {
  258. $pred_amound = $pred_amound - ($pred_amound / $goods_amount) * $full_discount;
  259. }
  260. $bonus_goods_amount = $pred_amound;
  261. $pred_amound = $pred_amound - predeposit_helper::order_cash($pred_amound,$rates);
  262. $pred_cend = intval($pred_amound * 100 + 0.5);
  263. $pd_total = intval($pd_total * 100 + 0.5);
  264. $ret = ($pred_cend > $pd_total) ? ($pd_total / 100) : ($pred_cend / 100);
  265. $discount_gap = predeposit_helper::discount_gap($bonus_goods_amount - $ret,$bonus_goods_amount);
  266. return $ret;
  267. }
  268. else
  269. {
  270. $rates = [];
  271. $gap_show = false;
  272. $discount_gap = 0.00;
  273. return 0.00;
  274. }
  275. }
  276. private function pred_goods_amount()
  277. {
  278. $pred_amount = 0.00;
  279. $store_cart_list = $this->mLogicOut['store_cart_list'];
  280. foreach ($store_cart_list as $store_id => $cart_list)
  281. {
  282. $opgoods = $this->mLogicOut['optional_goods'][$store_id];
  283. foreach ($cart_list as $cart)
  284. {
  285. $goods_total = $cart['goods_total'];
  286. if(intval($cart['bl_id']) > 0) {
  287. }
  288. elseif(!empty($cart['groupbuy_info'])) {
  289. }
  290. elseif(!empty($cart['xianshi_info'])) {
  291. }
  292. else
  293. {
  294. if(empty($opgoods)) {
  295. $pred_amount += $goods_total;
  296. }
  297. else
  298. {
  299. $goods_id = intval($cart['goods_id']);
  300. if(array_key_exists($goods_id,$opgoods['goods_nums']))
  301. {
  302. $goods_price = $cart['goods_price'];
  303. $goods_num = intval($cart['goods_num']);
  304. $opnum = $opgoods['goods_nums'][$goods_id];
  305. if($goods_num > $opnum) {
  306. $pred_amount += $goods_price * ($goods_num - $opnum);
  307. }
  308. }
  309. else {
  310. $pred_amount += $goods_total;
  311. }
  312. }
  313. }
  314. }
  315. }
  316. return $pred_amount;
  317. }
  318. public function check_fcode(&$err)
  319. {
  320. $cids = [];
  321. $store_cart_list = $this->mLogicOut['store_cart_list'];
  322. foreach ($store_cart_list as $store_id => $carts)
  323. {
  324. foreach ($carts as $value)
  325. {
  326. $bl_id = intval($value['bl_id']);
  327. if($bl_id > 0) continue;
  328. $fcode = intval($value['is_fcode']) == 1 ? true : false;
  329. if($fcode)
  330. {
  331. $goods_id = intval($value['goods_id']);
  332. $goods_num = intval($value['goods_num']);
  333. $commonid = commonid_helper::instance()->common_id($goods_id);
  334. if(isset($cids[$commonid])) {
  335. $cids[$commonid]['num'] += $goods_num;
  336. } else {
  337. $cids[$commonid]['num'] = $goods_num;
  338. $cids[$commonid]['name'] = $value['goods_name'];
  339. }
  340. }
  341. }
  342. }
  343. if(!empty($cids))
  344. {
  345. if($this->mUserFcode == null) {
  346. $this->mUserFcode = new user_session\fcode();
  347. $this->mUserFcode->onStatus();
  348. }
  349. foreach ($cids as $commonid => $val)
  350. {
  351. $goods_num = $val['num'];
  352. $name = $val['name'];
  353. if($goods_num <= 0) continue;
  354. $num = $this->mUserFcode->usable_num($commonid,$lock_num);
  355. if($num == false)
  356. {
  357. if(isset($lock_num) && $lock_num > 0) {
  358. $err = "您购买<{$name}>的F码处于锁定状态,激活后可以购买.";
  359. } else {
  360. $err = "您没有购买<{$name}>的F码,请重新选择商品.";
  361. }
  362. return false;
  363. }
  364. elseif($num < $goods_num) {
  365. $err = "您只有购买{$num}支<{$name}>的F码, 请修改商品数量.";
  366. return false;
  367. }
  368. }
  369. }
  370. return true;
  371. }
  372. public function format()
  373. {
  374. $result = [];
  375. $result['payinfo'] = $this->payinfo();
  376. $result['paytype'] = $this->paytype();
  377. $result['address'] = $this->address();
  378. $result['invoice'] = $this->invoice();
  379. $result['goods_list'] = $this->goods_list();
  380. $summary = $this->summary();
  381. $result['summary'] = $summary['summary'];
  382. $result['groupbuy'] = $summary['groupbuy'];
  383. $result['limitime'] = $summary['limitime'];
  384. $result['bundling'] = $summary['bundling'];
  385. return $result;
  386. }
  387. }