buy.logic.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. <?php
  2. /**
  3. * 购买行为
  4. *
  5. *
  6. *
  7. *
  8. * by abc www.abc.com 开发ls
  9. */
  10. defined('InShopNC') or exit('Access Invalid!');
  11. class buyLogic {
  12. /**
  13. * 会员信息
  14. * @var array
  15. */
  16. private $_member_info = array();
  17. /**
  18. * 下单数据
  19. * @var array
  20. */
  21. private $_order_data = array();
  22. /**
  23. * 表单数据
  24. * @var array
  25. */
  26. private $_post_data = array();
  27. /**
  28. * buy_1.logic 对象
  29. * @var obj
  30. */
  31. private $_logic_buy_1;
  32. public function __construct() {
  33. $this->_logic_buy_1 = Logic('buy_1');
  34. }
  35. /**
  36. * 购买第一步
  37. * @param unknown $cart_id
  38. * @param unknown $ifcart
  39. * @param unknown $member_id
  40. * @param unknown $store_id
  41. * @return Ambigous <multitype:unknown, multitype:unknown >
  42. */
  43. public function buyStep1($cart_id, $ifcart, $member_id, $store_id) {
  44. //得到购买商品信息
  45. if ($ifcart) {
  46. $result = $this->getCartList($cart_id, $member_id);
  47. } else {
  48. $result = $this->getGoodsList($cart_id, $member_id, $store_id);
  49. }
  50. if(!$result['state']) {
  51. return $result;
  52. }
  53. //得到页面所需要数据:收货地址、发票、代金券、预存款、商品列表等信息
  54. $result = $this->getBuyStep1Data($member_id,$result['data']);
  55. return $result;
  56. }
  57. /**
  58. * 第一步:处理购物车
  59. *
  60. * @param array $cart_id 购物车
  61. * @param int $member_id 会员编号
  62. */
  63. public function getCartList($cart_id, $member_id) {
  64. $model_cart = Model('cart');
  65. //取得POST ID和购买数量
  66. $buy_items = $this->_parseItems($cart_id);
  67. if (empty($buy_items)) {
  68. return callback(false, '所购商品无效');
  69. }
  70. if (count($buy_items) > 50) {
  71. return callback(false, '一次最多只可购买50种商品');
  72. }
  73. //购物车列表
  74. $condition = array('cart_id'=>array('in',array_keys($buy_items)), 'buyer_id'=>$member_id);
  75. $cart_list = $model_cart->listCart('db', $condition);
  76. //购物车列表 [得到最新商品属性及促销信息]
  77. $cart_list = $this->_logic_buy_1->getGoodsCartList($cart_list);
  78. //商品列表 [优惠套装子商品与普通商品同级罗列]
  79. $goods_list = $this->_getGoodsList($cart_list);
  80. //以店铺下标归类
  81. $store_cart_list = $this->_getStoreCartList($cart_list);
  82. return callback(true, '', array('goods_list' => $goods_list, 'store_cart_list' => $store_cart_list));
  83. }
  84. /**
  85. * 第一步:处理立即购买
  86. *
  87. * @param array $cart_id 购物车
  88. * @param int $member_id 会员编号
  89. * @param int $store_id 店铺编号
  90. */
  91. public function getGoodsList($cart_id, $member_id, $store_id) {
  92. //取得POST ID和购买数量
  93. $buy_items = $this->_parseItems($cart_id);
  94. if (empty($buy_items)) {
  95. return callback(false, '所购商品无效');
  96. }
  97. $goods_id = key($buy_items);
  98. $quantity = current($buy_items);
  99. //商品信息[得到最新商品属性及促销信息]
  100. $goods_info = $this->_logic_buy_1->getGoodsOnlineInfo($goods_id,intval($quantity));
  101. if(empty($goods_info)) {
  102. return callback(false, '商品已下架或不存在');
  103. }
  104. //不能购买自己店铺的商品
  105. if ($goods_info['store_id'] == $store_id) {
  106. return callback(false, '不能购买自己店铺的商品');
  107. }
  108. //进一步处理数组
  109. $store_cart_list = array();
  110. $goods_list = array();
  111. $goods_list[0] = $store_cart_list[$goods_info['store_id']][0] = $goods_info;
  112. return callback(true, '', array('goods_list' => $goods_list, 'store_cart_list' => $store_cart_list));
  113. }
  114. /**
  115. * 购买第一步:返回商品、促销、地址、发票等信息,然后交前台抛出
  116. * @param unknown $member_id
  117. * @param unknown $data 商品信息
  118. * @return
  119. */
  120. public function getBuyStep1Data($member_id, $data) {
  121. list($goods_list,$store_cart_list) = $data;
  122. $goods_list = $data['goods_list'];
  123. $store_cart_list = $data['store_cart_list'];
  124. //定义返回数组
  125. $result = array();
  126. //商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
  127. list($store_cart_list,$store_goods_total) = $this->_logic_buy_1->calcCartList($store_cart_list);
  128. $result['store_cart_list'] = $store_cart_list;
  129. $result['store_goods_total'] = $store_goods_total;
  130. //取得店铺优惠 - 满即送(赠品列表,店铺满送规则列表)
  131. list($store_premiums_list,$store_mansong_rule_list) = $this->_logic_buy_1->getMansongRuleCartListByTotal($store_goods_total);
  132. $result['store_premiums_list'] = $store_premiums_list;
  133. $result['store_mansong_rule_list'] = $store_mansong_rule_list;
  134. //重新计算优惠后(满即送)的店铺实际商品总金额
  135. $store_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_goods_total,$store_mansong_rule_list,'mansong');
  136. //返回店铺可用的代金券
  137. $store_voucher_list = $this->_logic_buy_1->getStoreAvailableVoucherList($store_goods_total, $member_id);
  138. $result['store_voucher_list'] = $store_voucher_list;
  139. //返回需要计算运费的店铺ID数组 和 不需要计算运费(满免运费活动的)店铺ID及描述
  140. list($need_calc_sid_list,$cancel_calc_sid_list) = $this->_logic_buy_1->getStoreFreightDescList($store_goods_total);
  141. $result['need_calc_sid_list'] = $need_calc_sid_list;
  142. $result['cancel_calc_sid_list'] = $cancel_calc_sid_list;
  143. //将商品ID、数量、运费模板、运费序列化,加密,输出到模板,选择地区AJAX计算运费时作为参数使用
  144. $freight_list = $this->_logic_buy_1->getStoreFreightList($goods_list,array_keys($cancel_calc_sid_list));
  145. $result['freight_list'] = $this->buyEncrypt($freight_list, $member_id);
  146. //输出用户默认收货地址
  147. $address_info = Model('address')->getDefaultAddressInfo(array('member_id'=>$member_id));
  148. $result['address_info'] = empty($address_info)? null : $address_info;
  149. //输出有货到付款时,在线支付和货到付款及每种支付下商品数量和详细列表
  150. $pay_goods_list = $this->_logic_buy_1->getOfflineGoodsPay($goods_list);
  151. if (!empty($pay_goods_list['offline'])) {
  152. $result['pay_goods_list'] = $pay_goods_list;
  153. $result['ifshow_offpay'] = true;
  154. } else {
  155. //如果所购商品只支持线上支付,支付方式不允许修改
  156. $result['deny_edit_payment'] = true;
  157. }
  158. //发票 :只有所有商品都支持增值税发票才提供增值税发票
  159. foreach ($goods_list as $goods) {
  160. if (!intval($goods['goods_vat'])) {
  161. $vat_deny = true;break;
  162. }
  163. }
  164. //不提供增值税发票时抛出true(模板使用)
  165. $result['vat_deny'] = $vat_deny;
  166. $result['vat_hash'] = $this->buyEncrypt($result['vat_deny'] ? 'deny_vat' : 'allow_vat', $member_id);
  167. //输出默认使用的发票信息
  168. $inv_info = Model('invoice')->getDefaultInvInfo(array('member_id'=>$member_id));
  169. if ($inv_info['inv_state'] == '2' && !$vat_deny) {
  170. $inv_info['content'] = '增值税发票 '.$inv_info['inv_company'].' '.$inv_info['inv_code'].' '.$inv_info['inv_reg_addr'];
  171. } elseif ($inv_info['inv_state'] == '2' && $vat_deny) {
  172. $inv_info = array();
  173. $inv_info['content'] = '不需要发票';
  174. } elseif (!empty($inv_info)) {
  175. $inv_info['content'] = '普通发票 '.$inv_info['inv_title'].' '.$inv_info['inv_content'];
  176. } else {
  177. $inv_info = array();
  178. $inv_info['content'] = '不需要发票';
  179. }
  180. $result['inv_info'] = $inv_info;
  181. $buyer_info = Model('member')->getMemberInfoByID($member_id);
  182. if (floatval($buyer_info['available_predeposit']) > 0) {
  183. $result['available_predeposit'] = $buyer_info['available_predeposit'];
  184. }
  185. if (floatval($buyer_info['available_rc_balance']) > 0) {
  186. $result['available_rc_balance'] = $buyer_info['available_rc_balance'];
  187. }
  188. $result['member_paypwd'] = $buyer_info['member_paypwd'] ? true : false;
  189. return callback(true,'',$result);
  190. }
  191. /**
  192. * 购买第二步
  193. * @param array $post
  194. * @param int $member_id
  195. * @param string $member_name
  196. * @param string $member_email
  197. * @return array
  198. */
  199. public function buyStep2($post, $member_id, $member_name, $member_email) {
  200. $this->_member_info['member_id'] = $member_id;
  201. $this->_member_info['member_name'] = $member_name;
  202. $this->_member_info['member_email'] = $member_email;
  203. $this->_post_data = $post;
  204. try {
  205. $model = Model('order');
  206. $model->beginTransaction();
  207. //第1步 表单验证
  208. $this->_createOrderStep1();
  209. //第2步 得到购买商品信息
  210. $this->_createOrderStep2();
  211. //第3步 得到购买相关金额计算等信息
  212. $this->_createOrderStep3();
  213. //第4步 生成订单
  214. $this->_createOrderStep4();
  215. //第5步 处理预存款
  216. $this->_createOrderStep5();
  217. $model->commit();
  218. //第6步 订单后续处理
  219. $this->_createOrderStep6();
  220. return callback(true,'',$this->_order_data);
  221. }catch (Exception $e){
  222. $model->rollback();
  223. return callback(false, $e->getMessage());
  224. }
  225. }
  226. /**
  227. * 删除购物车商品
  228. * @param unknown $ifcart
  229. * @param unknown $cart_ids
  230. */
  231. public function delCart($ifcart, $member_id, $cart_ids) {
  232. if (!$ifcart || !is_array($cart_ids)) return;
  233. $cart_id_str = implode(',',$cart_ids);
  234. if (preg_match('/^[\d,]+$/',$cart_id_str)) {
  235. QueueClient::push('delCart', array('buyer_id'=>$member_id,'cart_ids'=>$cart_ids));
  236. }
  237. }
  238. /**
  239. * 选择不同地区时,异步处理并返回每个店铺总运费以及本地区是否能使用货到付款
  240. * 如果店铺统一设置了满免运费规则,则运费模板无效
  241. * 如果店铺未设置满免规则,且使用运费模板,按运费模板计算,如果其中有商品使用相同的运费模板,则两种商品数量相加后再应用该运费模板计算(即作为一种商品算运费)
  242. * 如果未找到运费模板,按免运费处理
  243. * 如果没有使用运费模板,商品运费按快递价格计算,运费不随购买数量增加
  244. */
  245. public function changeAddr($freight_hash, $city_id, $area_id, $member_id) {
  246. //$city_id计算运费模板,$area_id计算货到付款
  247. $city_id = intval($city_id);
  248. $area_id = intval($area_id);
  249. Log::record("Start Change Address $city_id, $area_id, $member_id", log::DEBUG);
  250. if ($city_id <= 0 || $area_id <= 0) return null;
  251. //将hash解密,得到运费信息(店铺ID,运费,运费模板ID,购买数量),hash内容有效期为1小时
  252. $freight_list = $this->buyDecrypt($freight_hash, $member_id);
  253. Log::record("Parse Freight ".json_encode($freight_list). " count " .count($freight_list), log::DEBUG);
  254. //算运费
  255. $store_freight_list = $this->_logic_buy_1->calcStoreFreight($freight_list, $city_id);
  256. $data = array();
  257. $data['state'] = empty($store_freight_list) ? 'fail' : 'success';
  258. $data['content'] = array();
  259. foreach($store_freight_list as $key => $value){
  260. array_push($data['content'], array('store_id' => $key, 'value' => $value));
  261. }
  262. //$data['content'] = $store_freight_list;
  263. //是否能使用货到付款(只有包含平台店铺的商品才会判断)
  264. //$if_include_platform_store = array_key_exists(DEFAULT_PLATFORM_STORE_ID,$freight_list['iscalced']) || array_key_exists(DEFAULT_PLATFORM_STORE_ID,$freight_list['nocalced']);
  265. //$offline_store_id_array = Model('store')->getOwnShopIds();
  266. $order_platform_store_ids = array();
  267. if (is_array($freight_list['iscalced']))
  268. foreach (array_keys($freight_list['iscalced']) as $k)
  269. //if (in_array($k, $offline_store_id_array))
  270. $order_platform_store_ids[$k] = null;
  271. if (is_array($freight_list['nocalced']))
  272. foreach (array_keys($freight_list['nocalced']) as $k)
  273. //if (in_array($k, $offline_store_id_array))
  274. $order_platform_store_ids[$k] = null;
  275. //if ($order_platform_store_ids) {
  276. $allow_offpay_batch = Model('offpay_area')->checkSupportOffpayBatch($area_id, array_keys($order_platform_store_ids));
  277. /*
  278. //JS验证使用
  279. $data['allow_offpay'] = array_filter($allow_offpay_batch) ? '1' : '0';
  280. $data['allow_offpay_batch'] = $allow_offpay_batch;
  281. } else {*/
  282. //JS验证使用
  283. $data['allow_offpay'] = array_filter($allow_offpay_batch) ? '1' : '0';
  284. $data['allow_offpay_batch'] = array();
  285. foreach($allow_offpay_batch as $key => $value){
  286. array_push($data['allow_offpay_batch'], array('store_id' => $key, 'value' => $value));
  287. }
  288. //}
  289. //PHP验证使用
  290. $data['offpay_hash'] = $this->buyEncrypt($data['allow_offpay'] ? 'allow_offpay' : 'deny_offpay', $member_id);
  291. $data['offpay_hash_batch'] = $this->buyEncrypt($data['allow_offpay_batch'], $member_id);
  292. return $data;
  293. }
  294. /**
  295. * 验证F码
  296. * @param int $goods_commonid
  297. * @param string $fcode
  298. * @return array
  299. */
  300. public function checkFcode($goods_commonid, $fcode) {
  301. $fcode_info = Model('goods_fcode')->getGoodsFCode(array('goods_commonid' => $goods_commonid,'fc_code' => $fcode,'fc_state' => 0));
  302. if ($fcode_info) {
  303. return callback(true,'',$fcode_info);
  304. } else {
  305. return callback(false,'F码错误');
  306. }
  307. }
  308. /**
  309. * 订单生成前的表单验证与处理
  310. *
  311. */
  312. private function _createOrderStep1() {
  313. $post = $this->_post_data;
  314. //取得商品ID和购买数量
  315. $input_buy_items = $this->_parseItems($post['cart_id']);
  316. Log::record('Input buy item '. json_encode($input_buy_items),Log::DEBUG);
  317. if (empty($input_buy_items)) {
  318. throw new Exception('所购商品无效');
  319. }
  320. //验证收货地址
  321. $input_address_id = intval($post['address_id']);
  322. if ($input_address_id <= 0) {
  323. throw new Exception('请选择收货地址');
  324. } else {
  325. $input_address_info = Model('address')->getAddressInfo(array('address_id'=>$input_address_id));
  326. if ($input_address_info['member_id'] != $this->_member_info['member_id']) {
  327. throw new Exception('请选择收货地址');
  328. }
  329. }
  330. //收货地址城市编号
  331. $input_city_id = intval($input_address_info['city_id']);
  332. //是否开增值税发票
  333. $input_if_vat = $this->buyDecrypt($post['vat_hash'], $this->_member_info['member_id']);
  334. Log::record('Input if vat '. json_encode($input_if_vat),Log::DEBUG);
  335. if (!in_array($input_if_vat,array('allow_vat','deny_vat'))) {
  336. throw new Exception('订单保存出现异常[值税发票出现错误],请重试');
  337. }
  338. $input_if_vat = ($input_if_vat == 'allow_vat') ? true : false;
  339. //是否支持货到付款
  340. $input_if_offpay = $this->buyDecrypt($post['offpay_hash'], $this->_member_info['member_id']);
  341. if (!in_array($input_if_offpay,array('allow_offpay','deny_offpay'))) {
  342. throw new Exception('订单保存出现异常[货到付款验证错误],请重试');
  343. }
  344. $input_if_offpay = ($input_if_offpay == 'allow_offpay') ? true : false;
  345. // 是否支持货到付款 具体到各个店铺
  346. $input_if_offpay_batch = $this->buyDecrypt($post['offpay_hash_batch'], $this->_member_info['member_id']);
  347. if (!is_array($input_if_offpay_batch)) {
  348. throw new Exception('订单保存出现异常[部分店铺付款方式出现异常],请重试');
  349. }
  350. //付款方式:在线支付/货到付款(online/offline)
  351. if (!in_array($post['pay_name'],array('online','offline'))) {
  352. throw new Exception('付款方式错误,请重新选择');
  353. }
  354. $input_pay_name = $post['pay_name'];
  355. //验证发票信息
  356. if (!empty($post['invoice_id'])) {
  357. $input_invoice_id = intval($post['invoice_id']);
  358. if ($input_invoice_id > 0) {
  359. $input_invoice_info = Model('invoice')->getinvInfo(array('inv_id'=>$input_invoice_id));
  360. if ($input_invoice_info['member_id'] != $this->_member_info['member_id']) {
  361. throw new Exception('请正确填写发票信息');
  362. }
  363. }
  364. }
  365. //验证代金券
  366. $input_voucher_list = array();
  367. if (!empty($post['voucher']) && is_array($post['voucher'])) {
  368. foreach ($post['voucher'] as $store_id => $voucher) {
  369. if (preg_match_all('/^(\d+)\|(\d+)\|([\d.]+)$/',$voucher,$matchs)) {
  370. if (floatval($matchs[3][0]) > 0) {
  371. $input_voucher_list[$store_id]['voucher_t_id'] = $matchs[1][0];
  372. $input_voucher_list[$store_id]['voucher_price'] = $matchs[3][0];
  373. }
  374. }
  375. }
  376. }
  377. //保存数据
  378. $this->_order_data['input_buy_items'] = $input_buy_items;
  379. $this->_order_data['input_city_id'] = $input_city_id;
  380. $this->_order_data['input_pay_name'] = $input_pay_name;
  381. $this->_order_data['input_if_offpay'] = $input_if_offpay;
  382. $this->_order_data['input_if_offpay_batch'] = $input_if_offpay_batch;
  383. $this->_order_data['input_pay_message'] = $post['pay_message'];
  384. $this->_order_data['input_address_info'] = $input_address_info;
  385. $this->_order_data['input_invoice_info'] = $input_invoice_info;
  386. $this->_order_data['input_voucher_list'] = $input_voucher_list;
  387. $this->_order_data['order_from'] = $post['order_from'] == 2 ? 2 : 1;
  388. }
  389. /**
  390. * 得到购买商品信息
  391. *
  392. */
  393. private function _createOrderStep2() {
  394. $post = $this->_post_data;
  395. $input_buy_items = $this->_order_data['input_buy_items'];
  396. if ($post['ifcart']) {
  397. //购物车列表
  398. $model_cart = Model('cart');
  399. $condition = array('cart_id'=>array('in',array_keys($input_buy_items)),'buyer_id'=>$this->_member_info['member_id']);
  400. $cart_list = $model_cart->listCart('db',$condition);
  401. //购物车列表 [得到最新商品属性及促销信息]
  402. $cart_list = $this->_logic_buy_1->getGoodsCartList($cart_list);
  403. //商品列表 [优惠套装子商品与普通商品同级罗列]
  404. $goods_list = $this->_getGoodsList($cart_list);
  405. //以店铺下标归类
  406. $store_cart_list = $this->_getStoreCartList($cart_list);
  407. } else {
  408. //来源于直接购买
  409. $goods_id = key($input_buy_items);
  410. $quantity = current($input_buy_items);
  411. //商品信息[得到最新商品属性及促销信息]
  412. $goods_info = $this->_logic_buy_1->getGoodsOnlineInfo($goods_id,intval($quantity));
  413. if(empty($goods_info)) {
  414. throw new Exception('商品已下架或不存在');
  415. }
  416. //进一步处理数组
  417. $store_cart_list = array();
  418. $goods_list = array();
  419. $goods_list[0] = $store_cart_list[$goods_info['store_id']][0] = $goods_info;
  420. }
  421. //F码验证
  422. $fc_id = $this->_checkFcode($goods_list, $post['fcode']);
  423. if(!$fc_id) {
  424. throw new Exception('F码商品验证错误');
  425. }
  426. //保存数据
  427. $this->_order_data['goods_list'] = $goods_list;
  428. $this->_order_data['store_cart_list'] = $store_cart_list;
  429. if ($fc_id > 0) {
  430. $this->_order_data['fc_id'] = $fc_id;
  431. }
  432. }
  433. /**
  434. * 得到购买相关金额计算等信息
  435. *
  436. */
  437. private function _createOrderStep3() {
  438. $goods_list = $this->_order_data['goods_list'];
  439. $store_cart_list = $this->_order_data['store_cart_list'];
  440. $input_voucher_list = $this->_order_data['input_voucher_list'];
  441. $input_city_id = $this->_order_data['input_city_id'];
  442. //商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
  443. list($store_cart_list,$store_goods_total) = $this->_logic_buy_1->calcCartList($store_cart_list);
  444. //取得店铺优惠 - 满即送(赠品列表,店铺满送规则列表)
  445. list($store_premiums_list,$store_mansong_rule_list) = $this->_logic_buy_1->getMansongRuleCartListByTotal($store_goods_total);
  446. //重新计算店铺扣除满即送后商品实际支付金额
  447. $store_final_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_goods_total,$store_mansong_rule_list,'mansong');
  448. //得到有效的代金券
  449. $input_voucher_list = $this->_logic_buy_1->reParseVoucherList($input_voucher_list,$store_goods_total,$this->_member_info['member_id']);
  450. //重新计算店铺扣除优惠券送商品实际支付金额
  451. $store_final_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total,$input_voucher_list,'voucher');
  452. //计算每个店铺(所有店铺级优惠活动)总共优惠多少
  453. $store_promotion_total = $this->_logic_buy_1->getStorePromotionTotal($store_goods_total, $store_final_goods_total);
  454. //计算每个店铺运费
  455. list($need_calc_sid_list,$cancel_calc_sid_list) = $this->_logic_buy_1->getStoreFreightDescList($store_final_goods_total);
  456. $freight_list = $this->_logic_buy_1->getStoreFreightList($goods_list,array_keys($cancel_calc_sid_list));
  457. $store_freight_total = $this->_logic_buy_1->calcStoreFreight($freight_list,$input_city_id);
  458. //计算店铺最终订单实际支付金额(加上运费)
  459. $store_final_order_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total,$store_freight_total,'freight');
  460. //计算店铺分类佣金[改由任务计划]
  461. //zmr>>>
  462. $store_gc_id_commis_rate_list = Model('store_bind_class')->getStoreGcidCommisRateList($goods_list);
  463. //zmr<<<
  464. //将赠品追加到购买列表(如果库存0,则不送赠品)
  465. $append_premiums_to_cart_list = $this->_logic_buy_1->appendPremiumsToCartList($store_cart_list,$store_premiums_list,$store_mansong_rule_list,$this->_member_info['member_id']);
  466. if($append_premiums_to_cart_list === false) {
  467. throw new Exception('抱歉,您购买的商品库存不足,请重购买');
  468. } else {
  469. list($store_cart_list,$goods_buy_quantity,$store_mansong_rule_list) = $append_premiums_to_cart_list;
  470. }
  471. //保存数据
  472. $this->_order_data['store_goods_total'] = $store_goods_total;
  473. $this->_order_data['store_final_order_total'] = $store_final_order_total;
  474. $this->_order_data['store_freight_total'] = $store_freight_total;
  475. $this->_order_data['store_promotion_total'] = $store_promotion_total;
  476. //zmr>>>
  477. $this->_order_data['store_gc_id_commis_rate_list'] = $store_gc_id_commis_rate_list;
  478. //zmr<<<
  479. $this->_order_data['store_mansong_rule_list'] = $store_mansong_rule_list;
  480. $this->_order_data['store_cart_list'] = $store_cart_list;
  481. $this->_order_data['goods_buy_quantity'] = $goods_buy_quantity;
  482. $this->_order_data['input_voucher_list'] = $input_voucher_list;
  483. }
  484. /**
  485. * 生成订单
  486. * @param array $input
  487. * @throws Exception
  488. * @return array array(支付单sn,订单列表)
  489. */
  490. private function _createOrderStep4() {
  491. // extract() 函数从数组中将变量导入到当前的符号表。
  492. extract($this->_order_data);
  493. $member_id = $this->_member_info['member_id'];
  494. $member_name = $this->_member_info['member_name'];
  495. $member_email = $this->_member_info['member_email'];
  496. $model_order = Model('order');
  497. //存储生成的订单数据
  498. $order_list = array();
  499. //存储通知信息
  500. $notice_list = array();
  501. //每个店铺订单是货到付款还是线上支付,店铺ID=>付款方式[在线支付/货到付款]
  502. $store_pay_type_list = $this->_logic_buy_1->getStorePayTypeList(array_keys($store_cart_list), $input_if_offpay, $input_pay_name);
  503. foreach ($store_pay_type_list as $k => & $v) {
  504. if (empty($input_if_offpay_batch[$k]))
  505. $v = 'online';
  506. }
  507. $pay_sn = $this->_logic_buy_1->makePaySn($member_id);
  508. $order_pay = array();
  509. $order_pay['pay_sn'] = $pay_sn;
  510. $order_pay['buyer_id'] = $member_id;
  511. $order_pay_id = $model_order->addOrderPay($order_pay);
  512. if (!$order_pay_id) {
  513. throw new Exception('订单保存失败[未生成支付单]');
  514. }
  515. //收货人信息
  516. list($reciver_info,$reciver_name) = $this->_logic_buy_1->getReciverAddr($input_address_info);
  517. foreach ($store_cart_list as $store_id => $goods_list) {
  518. //取得本店优惠额度(后面用来计算每件商品实际支付金额,结算需要)
  519. $promotion_total = !empty($store_promotion_total[$store_id]) ? $store_promotion_total[$store_id] : 0;
  520. //本店总的优惠比例,保留3位小数
  521. $should_goods_total = $store_final_order_total[$store_id]-$store_freight_total[$store_id]+$promotion_total;
  522. $promotion_rate = abs(number_format($promotion_total/$should_goods_total,5));
  523. if ($promotion_rate <= 1) {
  524. $promotion_rate = floatval(substr($promotion_rate,0,5));
  525. } else {
  526. $promotion_rate = 0;
  527. }
  528. //每种商品的优惠金额累加保存入 $promotion_sum
  529. $promotion_sum = 0;
  530. $order = array();
  531. $order_common = array();
  532. $order_goods = array();
  533. $order['order_sn'] = $this->_logic_buy_1->makeOrderSn($order_pay_id);
  534. $order['pay_sn'] = $pay_sn;
  535. $order['store_id'] = $store_id;
  536. $order['store_name'] = $goods_list[0]['store_name'];
  537. $order['buyer_id'] = $member_id;
  538. $order['buyer_name'] = $member_name;
  539. $order['buyer_email'] = $member_email == NULL ? '' : $member_email;
  540. $order['add_time'] = time();
  541. $order['payment_code'] = $store_pay_type_list[$store_id];
  542. $order['order_state'] = $store_pay_type_list[$store_id] == 'online' ? ORDER_STATE_NEW : ORDER_STATE_PAY;
  543. $order['order_amount'] = $store_final_order_total[$store_id];
  544. $order['shipping_fee'] = $store_freight_total[$store_id];
  545. $order['goods_amount'] = $order['order_amount'] - $order['shipping_fee'];
  546. $order['order_from'] = $order_from;
  547. //中山小修改货到付款bug>>>
  548. if( $order['payment_code']=="")
  549. {
  550. $order['payment_code']="offline";
  551. }
  552. $order_id = $model_order->addOrder($order);
  553. if (!$order_id) {
  554. throw new Exception('订单保存失败[未生成订单数据]');
  555. }
  556. $order['order_id'] = $order_id;
  557. $order_list[$order_id] = $order;
  558. $order_common['order_id'] = $order_id;
  559. $order_common['store_id'] = $store_id;
  560. $order_common['order_message'] = $input_pay_message[$store_id];
  561. //代金券
  562. if (isset($input_voucher_list[$store_id])){
  563. $order_common['voucher_price'] = $input_voucher_list[$store_id]['voucher_price'];
  564. $order_common['voucher_code'] = $input_voucher_list[$store_id]['voucher_code'];
  565. }
  566. $order_common['reciver_info']= $reciver_info;
  567. $order_common['reciver_name'] = $reciver_name;
  568. $order_common['reciver_city_id'] = $input_city_id;
  569. //发票信息
  570. $order_common['invoice_info'] = $this->_logic_buy_1->createInvoiceData($input_invoice_info);
  571. //保存促销信息
  572. if(is_array($store_mansong_rule_list[$store_id])) {
  573. $order_common['promotion_info'] = addslashes($store_mansong_rule_list[$store_id]['desc']);
  574. }
  575. $order_id = $model_order->addOrderCommon($order_common);
  576. if (!$order_id) {
  577. throw new Exception('订单保存失败[未生成订单扩展数据]');
  578. }
  579. //生成order_goods订单商品数据
  580. $i = 0;
  581. foreach ($goods_list as $goods_info) {
  582. if (!$goods_info['state'] || !$goods_info['storage_state']) {
  583. throw new Exception('部分商品已经下架或库存不足,请重新选择');
  584. }
  585. if (!intval($goods_info['bl_id'])) {
  586. //如果不是优惠套装
  587. $order_goods[$i]['order_id'] = $order_id;
  588. $order_goods[$i]['goods_id'] = $goods_info['goods_id'];
  589. $order_goods[$i]['store_id'] = $store_id;
  590. $order_goods[$i]['goods_name'] = $goods_info['goods_name'];
  591. $order_goods[$i]['goods_price'] = $goods_info['goods_price'];
  592. $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
  593. $order_goods[$i]['goods_image'] = $goods_info['goods_image'];
  594. $order_goods[$i]['buyer_id'] = $member_id;
  595. if ($goods_info['ifgroupbuy']) {
  596. $ifgroupbuy = true;
  597. $order_goods[$i]['goods_type'] = 2;
  598. }elseif ($goods_info['ifxianshi']) {
  599. $order_goods[$i]['goods_type'] = 3;
  600. }elseif ($goods_info['ifzengpin']) {
  601. $order_goods[$i]['goods_type'] = 5;
  602. }else {
  603. $order_goods[$i]['goods_type'] = 1;
  604. }
  605. $order_goods[$i]['promotions_id'] = $goods_info['promotions_id'] ? $goods_info['promotions_id'] : 0;
  606. //zmr>>>
  607. $order_goods[$i]['commis_rate'] =floatval($store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
  608. //zmr<<<
  609. $order_goods[$i]['gc_id'] = $goods_info['gc_id'];
  610. //计算商品金额
  611. $goods_total = $goods_info['goods_price'] * $goods_info['goods_num'];
  612. //计算本件商品优惠金额
  613. $promotion_value = floor($goods_total*($promotion_rate));
  614. $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value;
  615. $promotion_sum += $promotion_value;
  616. $order_goods[$i]['goods_spec'] = $goods_info['goods_spec'];
  617. $i++;
  618. //存储库存报警数据
  619. if ($goods_info['goods_storage_alarm'] >= ($goods_info['goods_storage'] - $goods_info['goods_num'])) {
  620. $param = array();
  621. $param['common_id'] = $goods_info['goods_commonid'];
  622. $param['sku_id'] = $goods_info['goods_id'];
  623. $notice_list['goods_storage_alarm'][$goods_info['store_id']] = $param;
  624. }
  625. } elseif (!empty($goods_info['bl_goods_list']) && is_array($goods_info['bl_goods_list'])) {
  626. //优惠套装
  627. foreach ($goods_info['bl_goods_list'] as $bl_goods_info) {
  628. $order_goods[$i]['order_id'] = $order_id;
  629. $order_goods[$i]['goods_id'] = $bl_goods_info['goods_id'];
  630. $order_goods[$i]['store_id'] = $store_id;
  631. $order_goods[$i]['goods_name'] = $bl_goods_info['goods_name'];
  632. $order_goods[$i]['goods_price'] = $bl_goods_info['bl_goods_price'];
  633. $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
  634. $order_goods[$i]['goods_image'] = $bl_goods_info['goods_image'];
  635. $order_goods[$i]['buyer_id'] = $member_id;
  636. $order_goods[$i]['goods_type'] = 4;
  637. $order_goods[$i]['promotions_id'] = $bl_goods_info['bl_id'];
  638. $order_goods[$i]['commis_rate'] = floatval($store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
  639. $order_goods[$i]['gc_id'] = $bl_goods_info['gc_id'];
  640. //计算商品实际支付金额(goods_price减去分摊优惠金额后的值)
  641. $goods_total = $bl_goods_info['bl_goods_price'] * $goods_info['goods_num'];
  642. //计算本件商品优惠金额
  643. $promotion_value = floor($goods_total*($promotion_rate));
  644. $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value;
  645. $promotion_sum += $promotion_value;
  646. $order_goods[$i]['goods_spec'] = $goods_info['goods_spec'];
  647. $i++;
  648. //存储库存报警数据
  649. if ($bl_goods_info['goods_storage_alarm'] >= ($bl_goods_info['goods_storage'] - $goods_info['goods_num'])) {
  650. $param = array();
  651. $param['common_id'] = $bl_goods_info['goods_commonid'];
  652. $param['sku_id'] = $bl_goods_info['goods_id'];
  653. $notice_list['goods_storage_alarm'][$bl_goods_info['store_id']] = $param;
  654. }
  655. }
  656. }
  657. }
  658. //将因舍出小数部分出现的差值补到最后一个商品的实际成交价中(商品goods_price=0时不给补,可能是赠品)
  659. if ($promotion_total > $promotion_sum) {
  660. $i--;
  661. for($i;$i>=0;$i--) {
  662. if (floatval($order_goods[$i]['goods_price']) > 0) {
  663. $order_goods[$i]['goods_pay_price'] -= $promotion_total - $promotion_sum;
  664. break;
  665. }
  666. }
  667. }
  668. $insert = $model_order->addOrderGoods($order_goods);
  669. if (!$insert) {
  670. throw new Exception('订单保存失败[未生成商品数据]');
  671. }
  672. //存储商家发货提醒数据
  673. if ($store_pay_type_list[$store_id] == 'offline') {
  674. $notice_list['new_order'][$order['store_id']] = array('order_sn' => $order['order_sn']);
  675. }
  676. }
  677. //保存数据
  678. $this->_order_data['pay_sn'] = $pay_sn;
  679. $this->_order_data['order_list'] = $order_list;
  680. $this->_order_data['notice_list'] = $notice_list;
  681. $this->_order_data['ifgroupbuy'] = $ifgroupbuy;
  682. }
  683. /**
  684. * 充值卡、预存款支付
  685. *
  686. */
  687. private function _createOrderStep5() {
  688. if (empty($this->_post_data['password'])) return ;
  689. $buyer_info = Model('member')->getMemberInfoByID($this->_member_info['member_id']);
  690. if ($buyer_info['member_paypwd'] == '' || $buyer_info['member_paypwd'] != md5($this->_post_data['password'])) return ;
  691. //delete by stanley
  692. //$order_list = $this->_post_data;
  693. //使用充值卡支付
  694. if (!empty($this->_post_data['rcb_pay'])) {
  695. $order_list = $this->_logic_buy_1->rcbPay($this->_order_data['order_list'], $this->_post_data, $buyer_info);
  696. }
  697. //使用预存款支付
  698. if (!empty($this->_post_data['pd_pay'])) {
  699. $this->_logic_buy_1->pdPay($order_list ? $order_list :$this->_order_data['order_list'], $this->_post_data, $buyer_info);
  700. }
  701. }
  702. /**
  703. * 订单后续其它处理
  704. *
  705. */
  706. private function _createOrderStep6() {
  707. $ifcart = $this->_post_data['ifcart'];
  708. $goods_buy_quantity = $this->_order_data['goods_buy_quantity'];
  709. $input_voucher_list = $this->_order_data['input_voucher_list'];
  710. $store_cart_list = $this->_order_data['store_cart_list'];
  711. $input_buy_items = $this->_order_data['input_buy_items'];
  712. $order_list = $this->_order_data['order_list'];
  713. $input_address_info = $this->_order_data['input_address_info'];
  714. $notice_list = $this->_order_data['notice_list'];
  715. $fc_id = $this->_order_data['fc_id'];
  716. $ifgroupbuy = $this->_order_data['ifgroupbuy'];
  717. //变更库存和销量
  718. QueueClient::push('createOrderUpdateStorage', $goods_buy_quantity);
  719. //更新使用的代金券状态
  720. if (!empty($input_voucher_list) && is_array($input_voucher_list)) {
  721. QueueClient::push('editVoucherState', $input_voucher_list);
  722. }
  723. //更新F码使用状态
  724. if ($fc_id) {
  725. QueueClient::push('updateGoodsFCode', $fc_id);
  726. }
  727. //更新抢购购买人数和数量
  728. if ($ifgroupbuy) {
  729. foreach ($store_cart_list as $goods_list) {
  730. foreach ($goods_list as $goods_info) {
  731. if ($goods_info['ifgroupbuy'] && $goods_info['groupbuy_id']) {
  732. $groupbuy_info = array();
  733. $groupbuy_info['groupbuy_id'] = $goods_info['groupbuy_id'];
  734. $groupbuy_info['quantity'] = $goods_info['goods_num'];
  735. QueueClient::push('editGroupbuySaleCount', $groupbuy_info);
  736. }
  737. }
  738. }
  739. }
  740. //删除购物车中的商品
  741. $this->delCart($ifcart,$this->_member_info['member_id'],array_keys($input_buy_items));
  742. @setNcCookie('cart_goods_num','',-3600);
  743. //保存订单自提点信息
  744. if (C('delivery_isuse') && intval($input_address_info['dlyp_id'])) {
  745. $data = array();
  746. $data['mob_phone'] = $input_address_info['mob_phone'];
  747. $data['tel_phone'] = $input_address_info['tel_phone'];
  748. $data['reciver_name'] = $input_address_info['true_name'];
  749. $data['dlyp_id'] = $input_address_info['dlyp_id'];
  750. foreach ($order_list as $v) {
  751. $data['order_sn_list'][$v['order_id']]['order_sn'] = $v['order_sn'];
  752. $data['order_sn_list'][$v['order_id']]['add_time'] = $v['add_time'];
  753. }
  754. QueueClient::push('saveDeliveryOrder', $data);
  755. }
  756. //发送提醒类信息
  757. if (!empty($notice_list)) {
  758. foreach ($notice_list as $code => $value) {
  759. QueueClient::push('sendStoreMsg', array('code' => $code, 'store_id' => key($value), 'param' => current($value)));
  760. }
  761. }
  762. }
  763. /**
  764. * 加密
  765. * @param array/string $string
  766. * @param int $member_id
  767. * @return mixed arrray/string
  768. */
  769. public function buyEncrypt($string, $member_id) {
  770. $buy_key = sha1(md5($member_id.'&'.MD5_KEY));
  771. if (is_array($string)) {
  772. $string = serialize($string);
  773. } else {
  774. $string = strval($string);
  775. }
  776. return encrypt(base64_encode($string), $buy_key);
  777. }
  778. /**
  779. * 解密
  780. * @param string $string
  781. * @param int $member_id
  782. * @param number $ttl
  783. */
  784. public function buyDecrypt($string, $member_id, $ttl = 0) {
  785. $buy_key = sha1(md5($member_id.'&'.MD5_KEY));
  786. if (empty($string)) return;
  787. $string = base64_decode(decrypt(strval($string), $buy_key, $ttl));
  788. return ($tmp = @unserialize($string)) !== false ? $tmp : $string;
  789. }
  790. /**
  791. * 得到所购买的id和数量
  792. *
  793. */
  794. private function _parseItems($cart_id) {
  795. //存放所购商品ID和数量组成的键值对
  796. $buy_items = array();
  797. if (is_array($cart_id)) {
  798. foreach ($cart_id as $value) {
  799. if (preg_match_all('/^(\d{1,10})\|(\d{1,6})$/', $value, $match)) {
  800. if (intval($match[2][0]) > 0) {
  801. $buy_items[$match[1][0]] = $match[2][0];
  802. }
  803. }
  804. }
  805. }
  806. return $buy_items;
  807. }
  808. /**
  809. * 从购物车数组中得到商品列表
  810. * @param unknown $cart_list
  811. */
  812. private function _getGoodsList($cart_list) {
  813. if (empty($cart_list) || !is_array($cart_list)) return $cart_list;
  814. $goods_list = array();
  815. $i = 0;
  816. foreach ($cart_list as $key => $cart) {
  817. if (!$cart['state'] || !$cart['storage_state']) continue;
  818. //购买数量
  819. $quantity = $cart['goods_num'];
  820. if (!intval($cart['bl_id'])) {
  821. //如果是普通商品
  822. $goods_list[$i]['goods_num'] = $quantity;
  823. $goods_list[$i]['goods_id'] = $cart['goods_id'];
  824. $goods_list[$i]['store_id'] = $cart['store_id'];
  825. $goods_list[$i]['gc_id'] = $cart['gc_id'];
  826. $goods_list[$i]['goods_name'] = $cart['goods_name'];
  827. $goods_list[$i]['goods_price'] = $cart['goods_price'];
  828. $goods_list[$i]['store_name'] = $cart['store_name'];
  829. $goods_list[$i]['goods_image'] = $cart['goods_image'];
  830. $goods_list[$i]['transport_id'] = $cart['transport_id'];
  831. $goods_list[$i]['goods_freight'] = $cart['goods_freight'];
  832. $goods_list[$i]['goods_vat'] = $cart['goods_vat'];
  833. $goods_list[$i]['is_fcode'] = $cart['is_fcode'];
  834. $goods_list[$i]['bl_id'] = 0;
  835. $i++;
  836. } else {
  837. //如果是优惠套装商品
  838. foreach ($cart['bl_goods_list'] as $bl_goods) {
  839. $goods_list[$i]['goods_num'] = $quantity;
  840. $goods_list[$i]['goods_id'] = $bl_goods['goods_id'];
  841. $goods_list[$i]['store_id'] = $cart['store_id'];
  842. $goods_list[$i]['gc_id'] = $bl_goods['gc_id'];
  843. $goods_list[$i]['goods_name'] = $bl_goods['goods_name'];
  844. $goods_list[$i]['goods_price'] = $bl_goods['goods_price'];
  845. $goods_list[$i]['store_name'] = $bl_goods['store_name'];
  846. $goods_list[$i]['goods_image'] = $bl_goods['goods_image'];
  847. $goods_list[$i]['transport_id'] = $bl_goods['transport_id'];
  848. $goods_list[$i]['goods_freight'] = $bl_goods['goods_freight'];
  849. $goods_list[$i]['goods_vat'] = $bl_goods['goods_vat'];
  850. $goods_list[$i]['bl_id'] = $cart['bl_id'];
  851. $i++;
  852. }
  853. }
  854. }
  855. return $goods_list;
  856. }
  857. /**
  858. * 将下单商品列表转换为以店铺ID为下标的数组
  859. *
  860. * @param array $cart_list
  861. * @return array
  862. */
  863. private function _getStoreCartList($cart_list) {
  864. if (empty($cart_list) || !is_array($cart_list)) return $cart_list;
  865. $new_array = array();
  866. foreach ($cart_list as $cart) {
  867. $new_array[$cart['store_id']][] = $cart;
  868. }
  869. return $new_array;
  870. }
  871. /**
  872. * 本次下单是否需要码及F码合法性
  873. * 无需使用F码,返回 true
  874. * 需要使用F码,返回($fc_id/false)
  875. */
  876. private function _checkFcode($goods_list, $fcode) {
  877. foreach ($goods_list as $k => $v) {
  878. if ($v['is_fcode'] == 1) {
  879. $is_fcode = true; break;
  880. }
  881. }
  882. if (!$is_fcode) return true;
  883. if (empty($fcode) || count($goods_list) > 1) {
  884. return false;
  885. }
  886. $goods_info = $goods_list[0];
  887. $fcode_info = $this->checkFcode($goods_info['goods_commonid'],$fcode);
  888. if ($fcode_info['state']) {
  889. return intval($fcode_info['data']['fc_id']);
  890. } else {
  891. return false;
  892. }
  893. }
  894. }