buy_first.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. public function __construct($logic_out)
  16. {
  17. $this->mLogicOut = $logic_out;
  18. }
  19. //商品金额相关信息
  20. private function payinfo()
  21. {
  22. $result = [];
  23. //商品总价,红包可抵扣,满减金额
  24. $goods_amount = 0.00;
  25. $store_goods_total = $this->mLogicOut['store_goods_total'];
  26. foreach ($store_goods_total as $store_id => $value) {
  27. $goods_amount += doubleval($value);
  28. }
  29. $result['goods_amount'] = $goods_amount;
  30. //满送
  31. $full_discount = 0.00;
  32. $full_desc = '';
  33. $store_mansong_rule_list = $this->mLogicOut['store_mansong_rule_list'];
  34. foreach ($store_mansong_rule_list as $store_id => $value) {
  35. $full_discount += doubleval($value['discount']);
  36. $full_desc = $this->full_desc($value['desc']);
  37. }
  38. $result['full_discount'] = $full_discount;
  39. $result['full_desc'] = $full_desc;
  40. $result['full_goods'] = $this->full_goods();
  41. //运费
  42. if($this->freight($freight,$offpay_hash,$offpay_hash_batch) == true) {
  43. $result['freight'] = $freight;
  44. $result['offpay_hash_batch'] = $offpay_hash_batch;
  45. $result['offpay_hash'] = $offpay_hash;
  46. } else {
  47. $result['freight'] = 0;
  48. $result['offpay_hash_batch'] = "";
  49. $result['offpay_hash'] = "";
  50. }
  51. $result['freight_hash'] = $this->mLogicOut['freight_list'];
  52. //红包信息
  53. $result['total_pred'] = doubleval($this->mLogicOut['available_predeposit']);
  54. $result['available_pred'] = doubleval($this->available_pred($goods_amount,$full_discount,$result['total_pred']));
  55. $result['usable_pred'] = true;
  56. $result['pay_cash_pred'] = $result['goods_amount'] + $result['freight'] - $result['full_discount'] - $result['available_pred'];
  57. $result['pay_cash_nopred'] = $result['goods_amount'] + $result['freight'] - $result['full_discount'];
  58. //是否可以开增值税发票
  59. $result['vat_deny'] = $this->mLogicOut['vat_deny'];
  60. //发票
  61. $result['vat_hash'] = $this->mLogicOut['vat_hash'];
  62. return $result;
  63. }
  64. private function full_desc($desc)
  65. {
  66. $ret = preg_match('#(.*?)<a.*?title=("|\')(.*?)\2.*?>.*?</a>#s',$desc,$matches);
  67. if($ret != false && count($matches) == 4) {
  68. $desc = $matches[1] . $matches[3];
  69. }
  70. return $desc;
  71. }
  72. private function full_goods()
  73. {
  74. $store_mansong_rule_list = $this->mLogicOut['store_mansong_rule_list'];
  75. foreach ($store_mansong_rule_list as $store_id => $value) {
  76. return intval($value['goods_id']);
  77. }
  78. return 0;
  79. }
  80. private function city_id()
  81. {
  82. $address_info = $this->mLogicOut['address_info'];
  83. if(empty($address_info)) {
  84. return false;
  85. } else {
  86. return intval($address_info['city_id']);
  87. }
  88. }
  89. private function area_id()
  90. {
  91. $address_info = $this->mLogicOut['address_info'];
  92. if(empty($address_info)) {
  93. return false;
  94. } else {
  95. return intval($address_info['area_id']);
  96. }
  97. }
  98. private function paytype()
  99. {
  100. return pay_helper::pay_types();
  101. }
  102. //运费相关信息
  103. private function freight(&$freight,&$offpay_hash,&$offpay_hash_batch)
  104. {
  105. $logic_buy = Logic("buy");
  106. $freight_hash = $this->mLogicOut['freight_list'];
  107. $city_id = $this->city_id();
  108. $area_id = $this->area_id();
  109. $data = $logic_buy->changeAddr($freight_hash, $city_id, $area_id, $_SESSION['member_id']);
  110. if (!empty($data) && $data['state'] == 'success')
  111. {
  112. $offpay_hash = $data['offpay_hash'];
  113. $offpay_hash_batch = $data['offpay_hash_batch'];
  114. $freight = 0.00;
  115. foreach ($data['content'] as $value) {
  116. $freight += $value['value'];
  117. }
  118. return true;
  119. }
  120. else {
  121. return false;
  122. }
  123. }
  124. private function goods_ids()
  125. {
  126. $ids = [];
  127. $sent_id = $this->full_goods();
  128. if($sent_id > 0) {
  129. $ids[] = $sent_id;
  130. }
  131. $carts = $this->goods_list();
  132. foreach ($carts as $item)
  133. {
  134. if($item['bl_id'] > 0)
  135. {
  136. $bl_goods = activity_helper::bundling_goods($item['bl_id']);
  137. if($bl_goods != false) {
  138. foreach ($bl_goods as $gid) {
  139. $ids[] = $gid;
  140. }
  141. }
  142. }
  143. else
  144. {
  145. $ids[] = $item['goods_id'];
  146. }
  147. }
  148. return $ids;
  149. }
  150. private function summary()
  151. {
  152. $goods_ids = $this->goods_ids();
  153. $helper = new goods_helper();
  154. $summaries = $helper->get_summary($goods_ids,$related_goods);
  155. $summary_list = $summaries['summary'];
  156. if(!empty($related_goods)) {
  157. $related_summary = $helper->cart_summary($related_goods,$x);
  158. $summary_list = array_merge($summary_list,$related_summary['summary']);
  159. }
  160. $ret['summary'] = $summary_list;
  161. $ret['groupbuy'] = $summaries['groupbuy'];
  162. $ret['limitime'] = $summaries['limitime'];
  163. $ret['bundling'] = $summaries['bundling'];
  164. return $ret;
  165. }
  166. private function address()
  167. {
  168. return $this->mLogicOut['address_info'];
  169. }
  170. private function goods_list()
  171. {
  172. //购物车中商品列表
  173. $goods_list = [];
  174. $store_cart_list = $this->mLogicOut['store_cart_list'];
  175. foreach ($store_cart_list as $store_id => $carts)
  176. {
  177. foreach ($carts as $value) {
  178. $item['goods_id'] = intval($value['goods_id']);
  179. $item['goods_num'] = intval($value['goods_num']);
  180. $item['bl_id'] = intval($value['bl_id']);
  181. $goods_list[] = $item;
  182. }
  183. }
  184. return $goods_list;
  185. }
  186. private function invoice()
  187. {
  188. return $this->mLogicOut['inv_info'];
  189. }
  190. private function available_pred($goods_amount,$full_discount,$pd_total)
  191. {
  192. $full = intval($full_discount * 100 + 0.5);
  193. $scale = predeposit_helper::scale();
  194. $pred_amound = $this->pred_goods_amount();
  195. if($full > 0) {
  196. $pred_amound = intval($pred_amound - ($pred_amound / $goods_amount) * $full_discount);
  197. }
  198. $pred_cend = intval($pred_amound * $scale * 100 + 0.5);
  199. $pd_total = intval($pd_total * 100 + 0.5);
  200. return ($pred_cend > $pd_total) ? ($pd_total / 100) : ($pred_cend / 100);
  201. }
  202. private function pred_goods_amount()
  203. {
  204. $pred_amount = 0.00;
  205. $cart_list = $this->mLogicOut['store_cart_list'];
  206. foreach ($cart_list as $store => $cart)
  207. {
  208. foreach ($cart as $item)
  209. {
  210. $goods_total = $item['goods_total'];
  211. if(intval($item['bl_id']) > 0) {
  212. }
  213. elseif(!empty($item['groupbuy_info'])) {
  214. }
  215. elseif(!empty($item['xianshi_info'])) {
  216. }
  217. else {
  218. $pred_amount += $goods_total;
  219. }
  220. }
  221. }
  222. Log::record("pred_goods_amount = {$pred_amount}",Log::DEBUG);
  223. return $pred_amount;
  224. }
  225. public function format()
  226. {
  227. $result = [];
  228. $result['payinfo'] = $this->payinfo();
  229. $result['paytype'] = $this->paytype();
  230. $result['address'] = $this->address();
  231. $result['invoice'] = $this->invoice();
  232. $result['goods_list'] = $this->goods_list();
  233. $summary = $this->summary();
  234. $result['summary'] = $summary['summary'];
  235. $result['groupbuy'] = $summary['groupbuy'];
  236. $result['limitime'] = $summary['limitime'];
  237. $result['bundling'] = $summary['bundling'];
  238. return $result;
  239. }
  240. }