buy.logic.php 45 KB

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