Buy.php 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. <?php
  2. namespace app\common\logic;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 逻辑层模型
  13. */
  14. class Buy
  15. {
  16. /**
  17. * 会员信息
  18. * @var array
  19. */
  20. private $_member_info = array();
  21. /**
  22. * 下单数据
  23. * @var array
  24. */
  25. private $_order_data = array();
  26. /**
  27. * 表单数据
  28. * @var array
  29. */
  30. private $_post_data = array();
  31. /**
  32. * buy_1.logic 对象
  33. * @var obj
  34. */
  35. private $_logic_buy_1;
  36. public function __construct()
  37. {
  38. $this->_logic_buy_1 = model('buy_1', 'logic');
  39. }
  40. /**
  41. * 购买第一步
  42. * @param type $cart_id
  43. * @param type $ifcart
  44. * @param type $member_id
  45. * @param type $store_id
  46. * @param type $extra 额外特殊判断处理数据,比如拼团功能
  47. * @return type
  48. */
  49. public function buyStep1($cart_id, $ifcart, $member_id, $store_id, $extra = array())
  50. {
  51. //得到购买商品信息
  52. if ($ifcart) {
  53. $result = $this->getCartList($cart_id, $member_id);
  54. } else {
  55. $result = $this->getGoodsList($cart_id, $member_id, $store_id, $extra);
  56. }
  57. if (!$result['code']) {
  58. return $result;
  59. }
  60. //得到页面所需要数据:收货地址、发票、代金券、预存款、商品列表等信息
  61. $result = $this->getBuyStep1Data($member_id, $result['data']);
  62. return $result;
  63. }
  64. /**
  65. * 第一步:处理购物车
  66. * @param type $cart_id 购物车
  67. * @param type $member_id 会员编号
  68. * @param type $extra 额外特殊判断处理数据,比如拼团功能
  69. * @return type
  70. */
  71. public function getCartList($cart_id, $member_id, $extra = array())
  72. {
  73. $cart_model = model('cart');
  74. //取得POST ID和购买数量
  75. $buy_items = $this->_parseItems($cart_id);
  76. if (empty($buy_items)) {
  77. return ds_callback(false, '所购商品无效');
  78. }
  79. if (count($buy_items) > 50) {
  80. return ds_callback(false, '一次最多只可购买50种商品');
  81. }
  82. //购物车列表
  83. $condition = array();
  84. $condition[] = array('cart_id', 'in', array_keys($buy_items));
  85. $condition[] = array('buyer_id', '=', $member_id);
  86. $cart_list = $cart_model->getCartList('db', $condition);
  87. //购物车列表 [得到最新商品属性及促销信息]
  88. $cart_list = $this->_logic_buy_1->getGoodsCartList($cart_list);
  89. //商品列表 [优惠套装子商品与普通商品同级罗列]
  90. $goods_list = $this->_getGoodsList($cart_list);
  91. //以店铺下标归类
  92. $store_cart_list = $this->_getStoreCartList($cart_list);
  93. return ds_callback(true, '', array('goods_list' => $goods_list, 'store_cart_list' => $store_cart_list));
  94. }
  95. /**
  96. * 第一步:处理立即购买
  97. * @param type $cart_id 购物车
  98. * @param type $member_id 会员编号
  99. * @param type $store_id 店铺编号
  100. * @param type $extra 额外特殊判断处理数据,比如拼团功能
  101. * @return type
  102. */
  103. public function getGoodsList($cart_id, $member_id, $store_id, $extra = array())
  104. {
  105. //取得POST ID和购买数量
  106. $buy_items = $this->_parseItems($cart_id);
  107. if (empty($buy_items)) {
  108. return ds_callback(false, '所购商品无效');
  109. }
  110. $goods_id = key($buy_items);
  111. $quantity = current($buy_items);
  112. //商品信息[得到最新商品属性及促销信息]
  113. $goods_info = $this->_logic_buy_1->getGoodsOnlineInfo($goods_id, intval($quantity), $extra, $member_id);
  114. if (empty($goods_info)) {
  115. return ds_callback(false, '商品已下架或不存在');
  116. }
  117. //不能购买自己店铺的商品
  118. if ($goods_info['store_id'] == $store_id) {
  119. return ds_callback(false, '不能购买自己店铺的商品');
  120. }
  121. //进一步处理数组
  122. $store_cart_list = array();
  123. $goods_list = array();
  124. $goods_info['chain_id'] = 0;
  125. $goods_list[0] = $store_cart_list[$goods_info['store_id']][0] = $goods_info;
  126. return ds_callback(true, '', array('goods_list' => $goods_list, 'store_cart_list' => $store_cart_list));
  127. }
  128. /**
  129. * 购买第一步:返回商品、促销、地址、发票等信息,然后交前台抛出
  130. * @param unknown $member_id
  131. * @param unknown $data 商品信息
  132. * @return
  133. */
  134. public function getBuyStep1Data($member_id, $data)
  135. {
  136. //list($goods_list, $store_cart_list) = $data;
  137. $goods_list = $data['goods_list'];
  138. $store_cart_list = $data['store_cart_list'];
  139. //定义返回数组
  140. $result = array();
  141. //商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
  142. list($store_cart_list, $store_goods_total, $store_goods_original_total, $store_goods_discount_total) = $this->_logic_buy_1->calcCartList($store_cart_list);
  143. $result['store_cart_list'] = $store_cart_list;
  144. $result['store_goods_total'] = $store_goods_total;
  145. $result['store_goods_original_total'] = $store_goods_original_total;
  146. $result['store_goods_discount_total'] = $store_goods_discount_total;
  147. //取得店铺优惠 - 满即送(赠品列表,店铺满送规则列表)
  148. list($store_premiums_list, $store_mansong_rule_list) = $this->_logic_buy_1->getMansongruleCartListByTotal($store_goods_total);
  149. $result['store_premiums_list'] = $store_premiums_list;
  150. $result['store_mansong_rule_list'] = $store_mansong_rule_list;
  151. //重新计算优惠后(满即送)的店铺实际商品总金额
  152. $store_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_goods_total, $store_mansong_rule_list, 'mansong');
  153. //返回店铺可用的代金券
  154. $store_voucher_list = $this->_logic_buy_1->getStoreAvailableVoucherList($store_goods_total, $member_id);
  155. $result['store_voucher_list'] = $store_voucher_list;
  156. //返回店铺可用的平台代金券
  157. $mall_voucher_list = $this->_logic_buy_1->getAvailableMallVoucherUserList($goods_list, $member_id);
  158. $result['mall_voucher_list'] = $mall_voucher_list;
  159. //返回需要计算运费的店铺ID数组 和 不需要计算运费(满免运费活动的)店铺ID及描述
  160. list($need_calc_sid_list, $cancel_calc_sid_list) = $this->_logic_buy_1->getStoreFreightDescList($store_goods_total);
  161. $result['need_calc_sid_list'] = $need_calc_sid_list;
  162. $result['cancel_calc_sid_list'] = $cancel_calc_sid_list;
  163. //将商品ID、数量、售卖区域、运费序列化,加密,输出到模板,选择地区AJAX计算运费时作为参数使用
  164. $freight_list = $this->_logic_buy_1->getStoreFreightList($goods_list, array_keys($cancel_calc_sid_list));
  165. $result['freight_list'] = $this->buyEncrypt($freight_list, $member_id);
  166. //处理加密店铺商品 用于获取门店用
  167. $chaingoods_list = array();
  168. $chain_goods = array();
  169. foreach ($store_cart_list as $key => $val) {
  170. $chaingoods_list[$key] = $this->buyEncrypt($val, $member_id);
  171. }
  172. $result['chaingoods_list'] = $chaingoods_list;
  173. //输出用户默认收货地址
  174. $result['address_info'] = model('address')->getDefaultAddressInfo(array('member_id' => $member_id));
  175. //输出有货到付款时,在线支付和货到付款及每种支付下商品数量和详细列表
  176. $pay_goods_list = $this->_logic_buy_1->getOfflineGoodsPay($goods_list);
  177. if (!empty($pay_goods_list['offline'])) {
  178. $result['pay_goods_list'] = $pay_goods_list;
  179. $result['ifshow_offpay'] = true;
  180. } else {
  181. //如果所购商品只支持线上支付,支付方式不允许修改
  182. $result['deny_edit_payment'] = true;
  183. $result['pay_goods_list'] = $pay_goods_list;
  184. $result['ifshow_offpay'] = FALSE;
  185. }
  186. //是否是预售订单
  187. $if_presell = 0;
  188. $presell_deposit_amount = 0;
  189. if (count($goods_list) == 1) {
  190. $current_goods = current($goods_list);
  191. if (!empty($current_goods['presell_info']) && $current_goods['presell_info']['presell_type'] == 2) {
  192. $if_presell = 1;
  193. $presell_deposit_amount = $current_goods['presell_info']['presell_deposit_amount'];
  194. $result['deny_edit_payment'] = true;
  195. $result['ifshow_offpay'] = FALSE;
  196. }
  197. }
  198. $result['if_presell'] = $if_presell;
  199. $result['presell_deposit_amount'] = $presell_deposit_amount;
  200. //发票 :只有所有商品都支持增值税发票才提供增值税发票
  201. $vat_deny = false;
  202. foreach ($goods_list as $goods) {
  203. if (!intval($goods['goods_vat'])) {
  204. $vat_deny = true;
  205. break;
  206. }
  207. }
  208. //不提供增值税发票时抛出true(模板使用)
  209. $result['vat_deny'] = $vat_deny;
  210. $result['vat_hash'] = $this->buyEncrypt($result['vat_deny'] ? 'deny_vat' : 'allow_vat', $member_id);
  211. //输出默认使用的发票信息
  212. $inv_info = model('invoice')->getDefaultInvoiceInfo(array('member_id' => $member_id));
  213. if ($inv_info['invoice_state'] == '2' && !$vat_deny) {
  214. $inv_info['content'] = '增值税发票 ' . $inv_info['invoice_company'] . ' ' . $inv_info['invoice_company_code'] . ' ' . $inv_info['invoice_reg_addr'];
  215. } elseif ($inv_info['invoice_state'] == '2' && $vat_deny) {
  216. $inv_info = array();
  217. $inv_info['content'] = '不需要发票';
  218. } elseif (!empty($inv_info)) {
  219. $inv_info['content'] = '普通发票 ' . $inv_info['invoice_title'] . ' ' . $inv_info['invoice_code'] . ' ' . $inv_info['invoice_content'];
  220. } else {
  221. $inv_info = array();
  222. $inv_info['content'] = '不需要发票';
  223. }
  224. $result['inv_info'] = $inv_info;
  225. $buyer_info = model('member')->getMemberInfoByID($member_id);
  226. if (floatval($buyer_info['available_predeposit']) > 0) {
  227. $result['available_predeposit'] = $buyer_info['available_predeposit'];
  228. }
  229. if (floatval($buyer_info['available_rc_balance']) > 0) {
  230. $result['available_rc_balance'] = $buyer_info['available_rc_balance'];
  231. }
  232. $result['member_paypwd'] = $buyer_info['member_paypwd'] ? true : false;
  233. return ds_callback(true, '', $result);
  234. }
  235. /**
  236. * 购买第二步
  237. * @param array $post
  238. * @param int $member_id
  239. * @param string $member_name
  240. * @param string $member_email
  241. * @return array
  242. */
  243. public function buyStep2($post, $member_id, $member_name, $member_email)
  244. {
  245. $this->_member_info['member_id'] = $member_id;
  246. $this->_member_info['member_name'] = $member_name;
  247. $this->_member_info['member_email'] = $member_email;
  248. $this->_post_data = $post;
  249. try {
  250. $order_model = model('order');
  251. Db::startTrans();
  252. $this->_logic_buy_1->lock = true;
  253. //第1步 表单验证
  254. $this->_createOrderStep1();
  255. //第2步 得到购买商品信息
  256. $this->_createOrderStep2();
  257. //第3步 得到购买相关金额计算等信息
  258. $this->_createOrderStep3();
  259. //第4步 生成订单
  260. $this->_createOrderStep4();
  261. //第6步 订单后续处理
  262. $this->_createOrderStep6();
  263. Db::commit();
  264. return ds_callback(true, '', $this->_order_data);
  265. } catch (\Exception $e) {
  266. Db::rollback();
  267. return ds_callback(false, $e->getMessage());
  268. }
  269. }
  270. /**
  271. * 生成推广记录
  272. * @param array $order_list
  273. */
  274. public function addOrderInviter($order_list = array())
  275. {
  276. if (!config('ds_config.inviter_open')) {
  277. return;
  278. }
  279. if (empty($order_list) || !is_array($order_list))
  280. return;
  281. $inviter_ratio_1 = config('ds_config.inviter_ratio_1');
  282. $inviter_ratio_2 = config('ds_config.inviter_ratio_2');
  283. $inviter_ratio_3 = config('ds_config.inviter_ratio_3');
  284. $orderinviter_model = model('orderinviter');
  285. foreach ($order_list as $order_id => $order) {
  286. //如果是线下支付因为不会生成结算单所以不生成推广记录
  287. if ($order['payment_code'] == 'offline') {
  288. continue;
  289. }
  290. foreach ($order['order_goods'] as $goods) {
  291. //查询商品的分销信息
  292. $goods_common_info = Db::name('goodscommon')->alias('gc')->join('goods g', 'g.goods_commonid=gc.goods_commonid')->where('g.goods_id=' . $goods['goods_id'])->field('gc.goods_commonid,gc.inviter_open,gc.inviter_ratio')->find();
  293. if (!$goods_common_info['inviter_open']) {
  294. continue;
  295. }
  296. $goods_amount = $goods['goods_pay_price'] * $goods_common_info['inviter_ratio'] / 100;
  297. $inviter_ratios = array(
  298. $inviter_ratio_1,
  299. $inviter_ratio_2,
  300. $inviter_ratio_3,
  301. );
  302. //判断买家是否是分销员
  303. if (config('ds_config.inviter_return')) {
  304. if (Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $order['buyer_id'])->value('inviter_id')) {
  305. if (isset($inviter_ratios[0]) && floatval($inviter_ratios[0]) > 0) {
  306. $ratio = round($inviter_ratios[0] * $goods_common_info['inviter_ratio'] / 100, 2);
  307. $money_1 = round($inviter_ratios[0] / 100 * $goods_amount, 2);
  308. if ($money_1 > 0) {
  309. //生成推广记录
  310. Db::name('orderinviter')->insert(array(
  311. 'orderinviter_addtime' => TIMESTAMP,
  312. 'orderinviter_store_name' => $order['store_name'],
  313. 'orderinviter_goods_amount' => $goods['goods_pay_price'],
  314. 'orderinviter_goods_quantity' => $goods['goods_num'],
  315. 'orderinviter_order_type' => 0,
  316. 'orderinviter_store_id' => $goods['store_id'],
  317. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  318. 'orderinviter_goods_id' => $goods['goods_id'],
  319. 'orderinviter_level' => 1,
  320. 'orderinviter_ratio' => $ratio,
  321. 'orderinviter_goods_name' => $goods['goods_name'],
  322. 'orderinviter_order_id' => $order_id,
  323. 'orderinviter_order_sn' => $order['order_sn'],
  324. 'orderinviter_member_id' => $order['buyer_id'],
  325. 'orderinviter_member_name' => $order['buyer_name'],
  326. 'orderinviter_money' => $money_1,
  327. 'orderinviter_remark' => '获得分销员返佣,佣金比例' . $ratio . '%,订单号' . $order['order_sn'],
  328. ));
  329. }
  330. }
  331. }
  332. }
  333. //一级推荐人
  334. $inviter_1_id = Db::name('member')->where('member_id', $order['buyer_id'])->value('inviter_id');
  335. if (!$inviter_1_id || !Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $inviter_1_id)->value('inviter_id')) {
  336. continue;
  337. }
  338. $inviter_1 = Db::name('member')->where('member_id', $inviter_1_id)->field('inviter_id,member_id,member_name')->find();
  339. if ($inviter_1 && isset($inviter_ratios[0]) && floatval($inviter_ratios[0]) > 0) {
  340. $ratio = round($inviter_ratios[0] * $goods_common_info['inviter_ratio'] / 100, 2);
  341. $money_1 = round($inviter_ratios[0] / 100 * $goods_amount, 2);
  342. if ($money_1 > 0) {
  343. //生成推广记录
  344. Db::name('orderinviter')->insert(array(
  345. 'orderinviter_addtime' => TIMESTAMP,
  346. 'orderinviter_store_name' => $order['store_name'],
  347. 'orderinviter_goods_amount' => $goods['goods_pay_price'],
  348. 'orderinviter_goods_quantity' => $goods['goods_num'],
  349. 'orderinviter_order_type' => 0,
  350. 'orderinviter_store_id' => $goods['store_id'],
  351. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  352. 'orderinviter_goods_id' => $goods['goods_id'],
  353. 'orderinviter_level' => 1,
  354. 'orderinviter_ratio' => $ratio,
  355. 'orderinviter_goods_name' => $goods['goods_name'],
  356. 'orderinviter_order_id' => $order_id,
  357. 'orderinviter_order_sn' => $order['order_sn'],
  358. 'orderinviter_member_id' => $inviter_1['member_id'],
  359. 'orderinviter_member_name' => $inviter_1['member_name'],
  360. 'orderinviter_money' => $money_1,
  361. 'orderinviter_remark' => '获得一级推荐佣金,佣金比例' . $ratio . '%,推荐关系' . $inviter_1['member_name'] . '->' . $order['buyer_name'] . ',订单号' . $order['order_sn'],
  362. ));
  363. }
  364. }
  365. if (config('ds_config.inviter_level') <= 1) {
  366. continue;
  367. }
  368. //二级推荐人
  369. $inviter_2_id = Db::name('member')->where('member_id', $inviter_1_id)->value('inviter_id');
  370. if (!$inviter_2_id || !Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $inviter_2_id)->value('inviter_id')) {
  371. continue;
  372. }
  373. $inviter_2 = Db::name('member')->where('member_id', $inviter_2_id)->field('inviter_id,member_id,member_name')->find();
  374. if ($inviter_2 && isset($inviter_ratios[1]) && floatval($inviter_ratios[1]) > 0) {
  375. $ratio = round($inviter_ratios[1] * $goods_common_info['inviter_ratio'] / 100, 2);
  376. $money_2 = round($inviter_ratios[1] / 100 * $goods_amount, 2);
  377. if ($money_2 > 0) {
  378. //生成推广记录
  379. Db::name('orderinviter')->insert(array(
  380. 'orderinviter_addtime' => TIMESTAMP,
  381. 'orderinviter_store_name' => $order['store_name'],
  382. 'orderinviter_goods_amount' => $goods['goods_pay_price'],
  383. 'orderinviter_goods_quantity' => $goods['goods_num'],
  384. 'orderinviter_order_type' => 0,
  385. 'orderinviter_store_id' => $goods['store_id'],
  386. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  387. 'orderinviter_goods_id' => $goods['goods_id'],
  388. 'orderinviter_level' => 2,
  389. 'orderinviter_ratio' => $ratio,
  390. 'orderinviter_goods_name' => $goods['goods_name'],
  391. 'orderinviter_order_id' => $order_id,
  392. 'orderinviter_order_sn' => $order['order_sn'],
  393. 'orderinviter_member_id' => $inviter_2['member_id'],
  394. 'orderinviter_member_name' => $inviter_2['member_name'],
  395. 'orderinviter_money' => $money_2,
  396. 'orderinviter_remark' => '获得二级推荐佣金,佣金比例' . $ratio . '%,推荐关系' . $inviter_2['member_name'] . '->' . $inviter_1['member_name'] . '->' . $order['buyer_name'] . ',订单号' . $order['order_sn'],
  397. ));
  398. }
  399. }
  400. if (config('ds_config.inviter_level') <= 2) {
  401. continue;
  402. }
  403. //三级推荐人
  404. $inviter_3_id = Db::name('member')->where('member_id', $inviter_2_id)->value('inviter_id');
  405. if (!$inviter_3_id || !Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $inviter_3_id)->value('inviter_id')) {
  406. continue;
  407. }
  408. $inviter_3 = Db::name('member')->where('member_id', $inviter_3_id)->field('inviter_id,member_id,member_name')->find();
  409. if ($inviter_3 && isset($inviter_ratios[2]) && floatval($inviter_ratios[2]) > 0) {
  410. $ratio = round($inviter_ratios[2] * $goods_common_info['inviter_ratio'] / 100, 2);
  411. $money_3 = round($inviter_ratios[2] / 100 * $goods_amount, 2);
  412. if ($money_3 > 0) {
  413. //生成推广记录
  414. Db::name('orderinviter')->insert(array(
  415. 'orderinviter_addtime' => TIMESTAMP,
  416. 'orderinviter_store_name' => $order['store_name'],
  417. 'orderinviter_goods_amount' => $goods['goods_pay_price'],
  418. 'orderinviter_goods_quantity' => $goods['goods_num'],
  419. 'orderinviter_order_type' => 0,
  420. 'orderinviter_store_id' => $goods['store_id'],
  421. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  422. 'orderinviter_goods_id' => $goods['goods_id'],
  423. 'orderinviter_level' => 3,
  424. 'orderinviter_ratio' => $ratio,
  425. 'orderinviter_goods_name' => $goods['goods_name'],
  426. 'orderinviter_order_id' => $order_id,
  427. 'orderinviter_order_sn' => $order['order_sn'],
  428. 'orderinviter_member_id' => $inviter_3['member_id'],
  429. 'orderinviter_member_name' => $inviter_3['member_name'],
  430. 'orderinviter_money' => $money_3,
  431. 'orderinviter_remark' => '获得三级推荐佣金,佣金比例' . $ratio . '%,推荐关系' . $inviter_3['member_name'] . '->' . $inviter_2['member_name'] . '->' . $inviter_1['member_name'] . '->' . $order['buyer_name'] . ',订单号' . $order['order_sn'],
  432. ));
  433. }
  434. }
  435. }
  436. }
  437. }
  438. /**
  439. * 删除购物车商品
  440. * @param unknown $ifcart
  441. * @param unknown $cart_ids
  442. */
  443. public function delCart($ifcart, $member_id, $cart_ids)
  444. {
  445. if (!$ifcart || !is_array($cart_ids))
  446. return;
  447. $cart_id_str = implode(',', $cart_ids);
  448. if (preg_match('/^[\d,]+$/', $cart_id_str)) {
  449. $condition = array();
  450. $condition[] = array('buyer_id', '=', $member_id);
  451. $condition[] = array('cart_id', 'in', $cart_ids);
  452. model('cart')->delCart('db', $condition, $member_id);
  453. }
  454. }
  455. /**
  456. * 选择不同地区时,异步处理并返回每个店铺总运费以及本地区是否能使用货到付款
  457. * 如果店铺统一设置了满免运费规则,则售卖区域无效
  458. * 如果店铺未设置满免规则,且使用售卖区域,按售卖区域计算,如果其中有商品使用相同的售卖区域,则两种商品数量相加后再应用该售卖区域计算(即作为一种商品算运费)
  459. * 如果未找到售卖区域,按免运费处理
  460. * 如果没有使用售卖区域,商品运费按快递价格计算,运费不随购买数量增加
  461. */
  462. public function changeAddr($freight_hash, $city_id, $area_id, $member_id, $goods = array())
  463. {
  464. //限制配送的商品
  465. $transport_model = model('transport');
  466. $goods_list = $this->buyDecrypt($goods, $member_id);
  467. $limitidarray = array();
  468. if (is_array($goods_list)) {
  469. foreach ($goods_list as $key => $val) {
  470. $transport = $transport_model->getTransportInfo(array('transport_id' => $val['transport_id']));
  471. if ($transport['transport_is_limited'] == 1) {
  472. $extend_list = $transport_model->getTransportextendList(array('transport_id' => $val['transport_id']));
  473. if (!empty($extend_list)) {
  474. foreach ($extend_list as $k => $v) {
  475. if ($v['transportext_area_id'] != '') {
  476. if (strpos($v['transportext_area_id'], "," . $city_id . ",") == false) {
  477. $limitidarray[] = $val['goods_id'];
  478. }
  479. }
  480. }
  481. }
  482. $limitidarray = array_unique($limitidarray);
  483. }
  484. }
  485. }
  486. //$city_id计算售卖区域,$area_id计算货到付款
  487. $city_id = intval($city_id);
  488. $area_id = intval($area_id);
  489. if ($city_id <= 0 || $area_id <= 0)
  490. return null;
  491. //将hash解密,得到运费信息(店铺ID,运费,售卖区域ID,购买数量),hash内容有效期为1小时
  492. $freight_list = $this->buyDecrypt($freight_hash, $member_id);
  493. //算运费
  494. $store_freight_list = $this->_logic_buy_1->calcStoreFreight($freight_list, $city_id);
  495. $data = array();
  496. $data['state'] = empty($store_freight_list) ? 'fail' : 'success';
  497. $data['content'] = $store_freight_list;
  498. //是否能使用货到付款(只有包含平台店铺的商品才会判断)
  499. //$if_include_platform_store = array_key_exists(DEFAULT_PLATFORM_STORE_ID,$freight_list['iscalced']) || array_key_exists(DEFAULT_PLATFORM_STORE_ID,$freight_list['nocalced']);
  500. //$offline_store_id_array = model('store')->getOwnShopIds();
  501. $order_platform_store_ids = array();
  502. if (!empty($freight_list['iscalced']) && is_array($freight_list['iscalced']))
  503. foreach (array_keys($freight_list['iscalced']) as $k)
  504. $order_platform_store_ids[$k] = null;
  505. if (!empty($freight_list['nocalced']) && is_array($freight_list['nocalced']))
  506. foreach (array_keys($freight_list['nocalced']) as $k)
  507. //if (in_array($k, $offline_store_id_array))
  508. $order_platform_store_ids[$k] = null;
  509. //if ($order_platform_store_ids) {
  510. $allow_offpay_batch = model('offpayarea')->checkSupportOffpayBatch($area_id, array_keys($order_platform_store_ids));
  511. /*
  512. //JS验证使用
  513. $data['allow_offpay'] = array_filter($allow_offpay_batch) ? '1' : '0';
  514. $data['allow_offpay_batch'] = $allow_offpay_batch;
  515. } else {*/
  516. //JS验证使用
  517. $data['allow_offpay'] = array_filter($allow_offpay_batch) ? '1' : '0';
  518. $data['allow_offpay_batch'] = $allow_offpay_batch;
  519. //}
  520. //PHP验证使用
  521. $data['offpay_hash'] = $this->buyEncrypt($data['allow_offpay'] ? 'allow_offpay' : 'deny_offpay', $member_id);
  522. $data['offpay_hash_batch'] = $this->buyEncrypt($data['allow_offpay_batch'], $member_id);
  523. $data['limitidarray'] = $limitidarray;
  524. return $data;
  525. }
  526. /**
  527. * 自提门店
  528. * @param int $goods_commonid
  529. * @param string $fcode
  530. * @return array
  531. */
  532. public function changechain($goods, $area_id, $member_id)
  533. {
  534. $chain_model = model('chain');
  535. //将hash解密,得到商品信息
  536. $goods_list = $this->buyDecrypt($goods, $member_id);
  537. if (!empty($goods_list)) {
  538. $store_id = $goods_list[0]['store_id'];
  539. }
  540. $allchain_list = array();
  541. $chain_idsarr = array();
  542. $onechain_id = array();
  543. foreach ($goods_list as $key => $val) {
  544. if ($val['bl_id'] == 1) {
  545. foreach ($val['bl_goods_list'] as $k => $v) {
  546. $chain_ids = Db::name('chain_goods')->where(array(array('goods_id', '=', $v['goods_id']), array('goods_storage', '>=', 1)))->column('chain_id');
  547. if ($key == 0) {
  548. $onechain_id = $chain_ids;
  549. $chain_idsarr = array_intersect($onechain_id, $chain_ids);
  550. } else {
  551. $chain_idsarr = array_intersect($chain_idsarr, $chain_ids);
  552. }
  553. }
  554. } else {
  555. $chain_ids = Db::name('chain_goods')->where(array(array('goods_id', '=', $val['goods_id']), array('goods_storage', '>=', $val['goods_num'])))->column('chain_id');
  556. if ($key == 0) {
  557. $onechain_id = $chain_ids;
  558. $chain_idsarr = array_intersect($onechain_id, $chain_ids);
  559. } else {
  560. $chain_idsarr = array_intersect($chain_idsarr, $chain_ids);
  561. }
  562. }
  563. }
  564. foreach ($goods_list as $key => $val) {
  565. $condition = array();
  566. $condition[] = array('chain_if_pickup', '=', 1);
  567. $condition[] = array('chain_id', 'in', $chain_idsarr);
  568. if ($area_id) {
  569. $condition[] = array('chain_area_2|chain_area_3', '=', $area_id);
  570. }
  571. $chain_list = $chain_model->getChainOpenList($condition);
  572. $allchain_list[] = $chain_list;
  573. }
  574. $chain_list = array_values($chain_list);
  575. return $chain_list;
  576. }
  577. /**
  578. * 验证F码
  579. * @param int $goods_commonid
  580. * @param string $fcode
  581. * @return array
  582. */
  583. public function checkFcode($goods_goodid, $fcode)
  584. {
  585. $fcode_info = model('goodsfcode')->getGoodsfcode(array(
  586. 'goods_commonid' => $goods_goodid, 'goodsfcode_code' => $fcode,
  587. 'goodsfcode_state' => 0
  588. ));
  589. if ($fcode_info) {
  590. return ds_callback(true, '', $fcode_info);
  591. } else {
  592. return ds_callback(false, 'F码错误');
  593. }
  594. }
  595. /**
  596. * 订单生成前的表单验证与处理
  597. *
  598. */
  599. private function _createOrderStep1()
  600. {
  601. $post = $this->_post_data;
  602. //取得商品ID和购买数量
  603. $input_buy_items = $this->_parseItems($post['cart_id']);
  604. if (empty($input_buy_items)) {
  605. throw new \think\Exception('所购商品无效', 10006);
  606. }
  607. //验证收货地址
  608. $input_address_id = intval($post['address_id']);
  609. if ($input_address_id <= 0) {
  610. throw new \think\Exception('请选择收货地址', 10006);
  611. } else {
  612. $input_address_info = model('address')->getAddressInfo(array('address_id' => $input_address_id));
  613. if ($input_address_info['member_id'] != $this->_member_info['member_id']) {
  614. throw new \think\Exception('请选择收货地址', 10006);
  615. }
  616. if ($input_address_info['chain_id']) {
  617. //门店是否开启了代收
  618. if (!isset($input_address_info['chain_if_collect']) || $input_address_info['chain_if_collect'] != 1) {
  619. throw new \think\Exception('代收点已关闭代收', 10006);
  620. }
  621. }
  622. }
  623. //收货地址城市编号
  624. $input_city_id = intval($input_address_info['city_id']);
  625. //是否开增值税发票
  626. $input_if_vat = $this->buyDecrypt($post['vat_hash'], $this->_member_info['member_id']);
  627. if (!in_array($input_if_vat, array('allow_vat', 'deny_vat'))) {
  628. throw new \think\Exception('订单保存出现异常[值税发票出现错误],请重试', 10006);
  629. }
  630. $input_if_vat = ($input_if_vat == 'allow_vat') ? true : false;
  631. //是否支持货到付款
  632. $input_if_offpay = $this->buyDecrypt($post['offpay_hash'], $this->_member_info['member_id']);
  633. if (!in_array($input_if_offpay, array('allow_offpay', 'deny_offpay'))) {
  634. throw new \think\Exception('订单保存出现异常[货到付款验证错误],请重试', 10006);
  635. }
  636. $input_if_offpay = ($input_if_offpay == 'allow_offpay') ? true : false;
  637. // 是否支持货到付款 具体到各个店铺
  638. $input_if_offpay_batch = $this->buyDecrypt($post['offpay_hash_batch'], $this->_member_info['member_id']);
  639. if (!is_array($input_if_offpay_batch)) {
  640. throw new \think\Exception('订单保存出现异常[部分店铺付款方式出现异常],请重试', 10006);
  641. }
  642. //付款方式:在线支付/货到付款(online/offline)
  643. if (!in_array($post['pay_name'], array('online', 'offline'))) {
  644. throw new \think\Exception('付款方式错误,请重新选择', 10006);
  645. }
  646. $input_pay_name = $post['pay_name'];
  647. //验证发票信息
  648. $input_invoice_info = array();
  649. if (!empty($post['invoice_id'])) {
  650. $input_invoice_id = intval($post['invoice_id']);
  651. if ($input_invoice_id > 0) {
  652. $input_invoice_info = model('invoice')->getInvoiceInfo(array('invoice_id' => $input_invoice_id));
  653. if ($input_invoice_info['member_id'] != $this->_member_info['member_id']) {
  654. throw new \think\Exception('请正确填写发票信息', 10006);
  655. }
  656. }
  657. }
  658. //验证店铺代金券
  659. $input_voucher_list = array();
  660. if (!empty($post['voucher']) && is_array($post['voucher'])) {
  661. foreach ($post['voucher'] as $store_id => $voucher) {
  662. if (preg_match_all('/^(\d+)\|(\d+)\|([\d.]+)$/', $voucher, $matchs)) {
  663. if (floatval($matchs[3][0]) > 0) {
  664. $input_voucher_list[$store_id]['vouchertemplate_id'] = $matchs[1][0];
  665. $input_voucher_list[$store_id]['voucher_price'] = $matchs[3][0];
  666. }
  667. }
  668. }
  669. }
  670. //验证平台代金券
  671. $input_mallvoucher_list = array();
  672. if (isset($post['mallvoucher'])) {
  673. $mallvoucher = explode('|', $post['mallvoucher']);
  674. if (!empty($mallvoucher[0])) {
  675. $input_mallvoucher_list['mallvoucher_price'] = substr($post['mallvoucher'], strripos($post['mallvoucher'], "|") + 1);
  676. $input_mallvoucher_list['mallvouchertemplate_id'] = substr($post['mallvoucher'], 0, strrpos($post['mallvoucher'], "|"));
  677. }
  678. }
  679. //处理自提门店
  680. $input_chain_list = array();
  681. if (!empty($post['chain_goods']) && is_array($post['chain_goods'])) {
  682. foreach ($post['chain_goods'] as $store_id => $chain_goods) {
  683. $input_chain_list[$store_id]['chain_id'] = $chain_goods;
  684. }
  685. }
  686. //保存数据
  687. $this->_order_data['input_buy_items'] = $input_buy_items;
  688. $this->_order_data['input_city_id'] = $input_city_id;
  689. $this->_order_data['input_pay_name'] = $input_pay_name;
  690. $this->_order_data['input_if_offpay'] = $input_if_offpay;
  691. $this->_order_data['input_if_offpay_batch'] = $input_if_offpay_batch;
  692. $this->_order_data['input_pay_message'] = $post['pay_message'];
  693. $this->_order_data['input_address_info'] = $input_address_info;
  694. $this->_order_data['input_invoice_info'] = $input_invoice_info;
  695. $this->_order_data['input_voucher_list'] = $input_voucher_list;
  696. $this->_order_data['input_chain_list'] = $input_chain_list;
  697. $this->_order_data['input_mallvoucher_list'] = $input_mallvoucher_list;
  698. $this->_order_data['order_from'] = $post['order_from'] == 2 ? 2 : 1;
  699. }
  700. /**
  701. * 得到购买商品信息
  702. *
  703. */
  704. private function _createOrderStep2()
  705. {
  706. $post = $this->_post_data;
  707. $input_buy_items = $this->_order_data['input_buy_items'];
  708. if ($post['ifcart']) {
  709. //购物车列表
  710. $cart_model = model('cart');
  711. $condition = array();
  712. $condition[] = array('cart_id', 'in', array_keys($input_buy_items));
  713. $condition[] = array('buyer_id', '=', $this->_member_info['member_id']);
  714. $cart_list = $cart_model->getCartList('db', $condition);
  715. //购物车列表 [得到最新商品属性及促销信息]
  716. $cart_list = $this->_logic_buy_1->getGoodsCartList($cart_list);
  717. //商品列表 [优惠套装子商品与普通商品同级罗列]
  718. $goods_list = $this->_getGoodsList($cart_list);
  719. //以店铺下标归类
  720. $store_cart_list = $this->_getStoreCartList($cart_list);
  721. } else {
  722. //来源于直接购买
  723. $goods_id = key($input_buy_items);
  724. $quantity = current($input_buy_items);
  725. //额外数据用来处理拼团等其他活动
  726. $pintuan_id = isset($post['pintuan_id']) ? intval($post['pintuan_id']) : 0;
  727. $extra = array();
  728. if ($pintuan_id > 0) {
  729. $extra['pintuan_id'] = $pintuan_id; #拼团ID
  730. #是否为开团订单
  731. $extra['pintuangroup_id'] = empty(input('param.pintuangroup_id')) ? 0 : intval(input('param.pintuangroup_id'));
  732. }
  733. $bargainorder_id = isset($post['bargainorder_id']) ? intval($post['bargainorder_id']) : 0;
  734. if ($bargainorder_id > 0) {
  735. $extra['bargainorder_id'] = $bargainorder_id; #砍价ID
  736. }
  737. //商品信息[得到最新商品属性及促销信息]
  738. $goods_info = $this->_logic_buy_1->getGoodsOnlineInfo($goods_id, intval($quantity), $extra, $this->_member_info['member_id']);
  739. if (empty($goods_info)) {
  740. throw new \think\Exception('商品已下架或不存在', 10006);
  741. }
  742. //进一步处理数组
  743. $store_cart_list = array();
  744. $goods_list = array();
  745. $goods_list[0] = $store_cart_list[$goods_info['store_id']][0] = $goods_info;
  746. }
  747. //F码验证
  748. $goodsfcode_id = '';
  749. if (!empty($post['fcode'])) {
  750. $goodsfcode_id = $this->_checkFcode($goods_list, $post['fcode']);
  751. if (!$goodsfcode_id) {
  752. throw new \think\Exception('F码商品验证错误', 10006);
  753. }
  754. }
  755. $this->_order_data['goodsfcode_id'] = $goodsfcode_id;
  756. //保存数据
  757. $this->_order_data['goods_list'] = $goods_list;
  758. $this->_order_data['store_cart_list'] = $store_cart_list;
  759. }
  760. /**
  761. * 得到购买相关金额计算等信息
  762. *
  763. */
  764. private function _createOrderStep3()
  765. {
  766. $goods_list = $this->_order_data['goods_list'];
  767. $store_cart_list = $this->_order_data['store_cart_list'];
  768. $input_voucher_list = $this->_order_data['input_voucher_list'];
  769. $input_mallvoucher_list = $this->_order_data['input_mallvoucher_list'];
  770. $input_city_id = $this->_order_data['input_city_id'];
  771. //商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
  772. list($store_cart_list, $store_goods_total, $store_goods_original_total, $store_goods_discount_total) = $this->_logic_buy_1->calcCartList($store_cart_list);
  773. //取得店铺优惠 - 满即送(赠品列表,店铺满送规则列表)
  774. list($store_premiums_list, $store_mansong_rule_list) = $this->_logic_buy_1->getMansongruleCartListByTotal($store_goods_total);
  775. //重新计算店铺扣除满即送后商品实际支付金额
  776. $store_final_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_goods_total, $store_mansong_rule_list, 'mansong');
  777. //得到有效的店铺代金券
  778. $input_voucher_list = $this->_logic_buy_1->reParseVoucherList($input_voucher_list, $store_goods_total, $this->_member_info['member_id']);
  779. //重新计算店铺扣除优惠券送商品实际支付金额
  780. $store_final_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total, $input_voucher_list, 'voucher');
  781. //得到有效的平台代金券
  782. $input_mallvoucher_list = $this->_logic_buy_1->reParseMallVoucherList($input_mallvoucher_list, $goods_list, $this->_member_info['member_id']);
  783. //平台优惠券前的金额
  784. $store_final_goods_total_before = $store_final_goods_total;
  785. //计算店铺平台优惠券优惠到的金额
  786. $store_mallvoucher_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total, $input_mallvoucher_list, 'mallvoucher', $goods_list);
  787. //平台优惠券后的折扣金额
  788. $store_final_goods_total_after = array();
  789. foreach ($store_final_goods_total as $key => $value) {
  790. $store_final_goods_total_after[$key] = $store_final_goods_total_before[$key] - $store_mallvoucher_goods_total[$key];
  791. }
  792. //计算每个店铺(所有店铺级优惠活动)总共优惠多少
  793. $store_promotion_total = $this->_logic_buy_1->getStorePromotionTotal($store_goods_total, $store_final_goods_total);
  794. //计算每个店铺运费
  795. list($need_calc_sid_list, $cancel_calc_sid_list) = $this->_logic_buy_1->getStoreFreightDescList($store_final_goods_total);
  796. $freight_list = $this->_logic_buy_1->getStoreFreightList($goods_list, array_keys($cancel_calc_sid_list));
  797. $store_freight_total = $this->_logic_buy_1->calcStoreFreight($freight_list, $input_city_id);
  798. if (empty($store_freight_total)) {
  799. throw new \think\Exception('抱歉,商品在所在地区无货', 10006);
  800. }
  801. //计算店铺最终订单实际支付金额(加上运费)
  802. $store_final_order_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total, $store_freight_total, 'freight');
  803. //计算店铺分类佣金[改由任务计划]
  804. $store_gc_id_commis_rate_list = model('storebindclass')->getStoreGcidCommisRateList($goods_list);
  805. //将赠品追加到购买列表(如果库存0,则不送赠品)
  806. $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']);
  807. if ($append_premiums_to_cart_list === false) {
  808. throw new \think\Exception('抱歉,您购买的商品库存不足,请重购买', 10006);
  809. } else {
  810. list($store_cart_list, $goods_buy_quantity, $store_mansong_rule_list) = $append_premiums_to_cart_list;
  811. }
  812. //保存数据
  813. $this->_order_data['store_goods_total'] = $store_goods_total;
  814. $this->_order_data['store_final_order_total'] = $store_final_order_total;
  815. $this->_order_data['store_final_goods_total_after'] = $store_final_goods_total_after;
  816. $this->_order_data['store_freight_total'] = $store_freight_total;
  817. $this->_order_data['store_promotion_total'] = $store_promotion_total;
  818. $this->_order_data['store_gc_id_commis_rate_list'] = $store_gc_id_commis_rate_list;
  819. $this->_order_data['store_mansong_rule_list'] = $store_mansong_rule_list;
  820. $this->_order_data['store_cart_list'] = $store_cart_list;
  821. $this->_order_data['goods_buy_quantity'] = $goods_buy_quantity;
  822. $this->_order_data['input_voucher_list'] = $input_voucher_list;
  823. $this->_order_data['input_mallvoucher_list'] = $input_mallvoucher_list;
  824. }
  825. /**
  826. * 生成订单
  827. * @param array $input
  828. * @throws Exception
  829. * @return array array(支付单sn,订单列表)
  830. */
  831. private function _createOrderStep4()
  832. {
  833. extract($this->_order_data);
  834. $member_id = $this->_member_info['member_id'];
  835. $member_name = $this->_member_info['member_name'];
  836. $member_email = $this->_member_info['member_email'];
  837. $order_model = model('order');
  838. //存储生成的订单数据
  839. $order_list = array();
  840. //存储通知信息
  841. $notice_list = array();
  842. //每个店铺订单是货到付款还是线上支付,店铺ID=>付款方式[在线支付/货到付款]
  843. $store_pay_type_list = $this->_logic_buy_1->getStorePayTypeList(array_keys($store_cart_list), $input_if_offpay, $input_pay_name);
  844. foreach ($store_pay_type_list as $k => &$v) {
  845. if (empty($input_if_offpay_batch[$k]))
  846. $v = 'online';
  847. }
  848. $pay_sn = makePaySn($member_id);
  849. $order_pay = array();
  850. $order_pay['pay_sn'] = $pay_sn;
  851. $order_pay['buyer_id'] = $member_id;
  852. $order_pay_id = $order_model->addOrderpay($order_pay);
  853. if (!$order_pay_id) {
  854. throw new \think\Exception('订单保存失败[未生成支付单]', 10006);
  855. }
  856. //收货人信息
  857. list($reciver_info, $reciver_name) = $this->_logic_buy_1->getReciverAddr($input_address_info);
  858. foreach ($store_cart_list as $store_id => $goods_list) {
  859. //取得本店优惠额度(后面用来计算每件商品实际支付金额,结算需要)
  860. $promotion_total = !empty($store_promotion_total[$store_id]) ? $store_promotion_total[$store_id] : 0;
  861. //本店总的优惠比例,保留3位小数
  862. $should_goods_total = $store_final_order_total[$store_id] - $store_freight_total[$store_id] + $promotion_total;
  863. $promotion_rate = abs(number_format($promotion_total / $should_goods_total, 5, '.', ''));
  864. if ($promotion_rate <= 1) {
  865. $promotion_rate = floatval(substr($promotion_rate, 0, 5));
  866. } else {
  867. $promotion_rate = 0;
  868. }
  869. //每种商品的优惠金额累加保存入 $promotion_sum
  870. $promotion_sum = 0;
  871. $order = array();
  872. $order_common = array();
  873. $order_goods = array();
  874. $order['order_sn'] = $this->_logic_buy_1->makeOrderSn($order_pay_id);
  875. $order['pay_sn'] = $pay_sn;
  876. $order['store_id'] = $store_id;
  877. $order['store_name'] = $goods_list[0]['store_name'];
  878. $order['buyer_id'] = $member_id;
  879. $order['buyer_name'] = $member_name;
  880. $order['buyer_email'] = $member_email;
  881. $order['add_time'] = TIMESTAMP;
  882. $order['payment_code'] = $store_pay_type_list[$store_id];
  883. if (!empty($goods_list[0]['presell_info']) && $goods_list[0]['presell_info']['presell_type'] == 2) {
  884. $order['presell_deposit_amount'] = round($goods_list[0]['presell_info']['presell_deposit_amount'] * $goods_list[0]['goods_num'], 2);
  885. $order['presell_end_time'] = $goods_list[0]['presell_info']['presell_end_time'];
  886. }
  887. $order['order_state'] = $store_pay_type_list[$store_id] == 'online' ? (isset($this->_post_data['presell_pay']) && $this->_post_data['presell_pay'] == 2 ? ORDER_STATE_DEPOSIT : ORDER_STATE_NEW) : ORDER_STATE_PAY;
  888. //判断是否自提
  889. if (isset($input_chain_list[$store_id]) && !empty($input_chain_list[$store_id])) {
  890. $order['chain_id'] = $input_chain_list[$store_id]['chain_id'];
  891. //选择自提 不计算运费
  892. $order['shipping_fee'] = 0;
  893. $store_final_order_total[$store_id] = $store_final_order_total[$store_id] - $store_freight_total[$store_id];
  894. } else {
  895. $order['chain_id'] = 0;
  896. $order['shipping_fee'] = $store_freight_total[$store_id];
  897. }
  898. if (isset($input_mallvoucher_list) && !empty($input_mallvoucher_list)) {
  899. $order['order_amount'] = $store_final_order_total[$store_id] - $store_final_goods_total_after[$store_id];
  900. } else {
  901. $order['order_amount'] = $store_final_order_total[$store_id];
  902. }
  903. $order['goods_amount'] = $order['order_amount'] - $order['shipping_fee'];
  904. $order['order_from'] = $order_from;
  905. //如果支持方式为空时,默认为货到付款
  906. if ($order['payment_code'] == "") {
  907. $order['payment_code'] = "offline";
  908. }
  909. if ($order['payment_code'] == "offline" && $input_address_info['chain_id']) {
  910. throw new \think\Exception('代收点不可以使用货到付款[未生成订单数据]', 10006);
  911. }
  912. $order_id = $order_model->addOrder($order);
  913. if (!$order_id) {
  914. throw new \think\Exception('订单保存失败[未生成订单数据]', 10006);
  915. }
  916. $order['order_id'] = $order_id;
  917. $order_list[$order_id] = $order;
  918. $order_common['order_id'] = $order_id;
  919. $order_common['store_id'] = $store_id;
  920. $order_common['order_message'] = $input_pay_message[$store_id];
  921. //店铺代金券
  922. if (isset($input_voucher_list[$store_id])) {
  923. $order_common['voucher_price'] = $input_voucher_list[$store_id]['voucher_price'];
  924. $order_common['voucher_code'] = $input_voucher_list[$store_id]['voucher_code'];
  925. }
  926. //平台代金券
  927. $mallvouchertotal = 0;
  928. if (isset($input_mallvoucher_list) && !empty($input_mallvoucher_list)) {
  929. $input_mallvoucher_list['mallvoucheruser_state'] = 1;
  930. $order_common['mallvoucher_price'] = $store_final_goods_total_after[$store_id];
  931. $order_common['mallvoucher_code'] = $input_mallvoucher_list['mallvoucheruser_code'];
  932. foreach ($goods_list as $goods_info) {
  933. $gc_id = ',' . $goods_info['gc_id'] . ',';
  934. if (strpos($input_mallvoucher_list['mallvouchertemplate_gcidarr'], $gc_id) !== false) {
  935. $mallvouchertotal += $goods_info['goods_price'] * $goods_info['goods_num'];
  936. }
  937. }
  938. }
  939. $order_common['reciver_info'] = $reciver_info;
  940. $order_common['reciver_name'] = $reciver_name;
  941. $order_common['reciver_city_id'] = $input_city_id;
  942. //发票信息
  943. $order_common['invoice_info'] = $this->_logic_buy_1->createInvoiceData($input_invoice_info);
  944. //保存促销信息
  945. if (isset($store_mansong_rule_list[$store_id])) {
  946. $order_common['promotion_info'] = addslashes($store_mansong_rule_list[$store_id]['desc']);
  947. }
  948. $order_id = $order_model->addOrdercommon($order_common);
  949. if (!$order_id) {
  950. throw new \think\Exception('订单保存失败[未生成订单扩展数据]', 10006);
  951. }
  952. $order_list[$order_id]['order_common'] = $order_common;
  953. //生成order_goods订单商品数据
  954. $i = 0;
  955. foreach ($goods_list as $goods_info) {
  956. if (!$goods_info['state'] || !$goods_info['storage_state']) {
  957. throw new \think\Exception('部分商品已经下架或库存不足,请重新选择', 10006);
  958. }
  959. if (!intval($goods_info['bl_id'])) {
  960. //如果不是优惠套装
  961. $order_goods[$i]['order_id'] = $order_id;
  962. $order_goods[$i]['goods_id'] = $goods_info['goods_id'];
  963. $order_goods[$i]['store_id'] = $store_id;
  964. $order_goods[$i]['goods_name'] = $goods_info['goods_name'];
  965. $order_goods[$i]['goods_price'] = $goods_info['goods_price'];
  966. $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
  967. $order_goods[$i]['goods_image'] = $goods_info['goods_image'];
  968. $order_goods[$i]['buyer_id'] = $member_id;
  969. $ifgroupbuy = false;
  970. if (isset($goods_info['ifgroupbuy'])) {
  971. $ifgroupbuy = true;
  972. $order_goods[$i]['goods_type'] = 2;
  973. } elseif (isset($goods_info['ifxianshi'])) {
  974. $order_goods[$i]['goods_type'] = 3;
  975. } elseif (isset($goods_info['ifpresell'])) {
  976. $order_goods[$i]['goods_type'] = 10;
  977. } elseif (isset($goods_info['ifwholesale'])) {
  978. $order_goods[$i]['goods_type'] = 9;
  979. } elseif (isset($goods_info['ifzengpin'])) {
  980. $order_goods[$i]['goods_type'] = 5;
  981. } elseif (isset($goods_info['ifpintuan']) && intval($this->_post_data['pintuan_id']) > 0) {
  982. //拼团订单
  983. /**
  984. * $goods_info['ifpintuan'] , $goods_info['pintuan_id'] 此数据是通过商品ID 获取到是否为拼团订单
  985. * $this->_post_data['pintuan_id'] $this->_post_data['pintuangroup_id'] 此数据是通过post 过来的数据,用来判断是否为首个拼团订单:0首个订单 其他为所属订单
  986. */
  987. $order_goods[$i]['goods_type'] = 6;
  988. $res = $this->_logic_buy_1->updatePintuan($this->_post_data, $goods_info, $order, 0, $member_id);
  989. $goods_info['promotions_id'] = $res['pintuangroup_id'];
  990. } elseif (isset($goods_info['ifbargain']) && $goods_info['ifbargain']) {
  991. //砍价订单
  992. $order_goods[$i]['goods_type'] = 8;
  993. if (!model('pbargainorder')->editPbargainorder(array('bargainorder_id' => $goods_info['promotions_id']), array('order_id' => $order_id))) {
  994. throw new \think\Exception('砍价活动更新失败', 10006);
  995. }
  996. } elseif (isset($goods_info['ifmgdiscount'])) {
  997. $order_goods[$i]['goods_type'] = 7;
  998. } else {
  999. $order_goods[$i]['goods_type'] = 1;
  1000. }
  1001. $order_goods[$i]['promotions_id'] = isset($goods_info['promotions_id']) ? $goods_info['promotions_id'] : 0;
  1002. $order_goods[$i]['commis_rate'] = floatval(@$store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
  1003. $order_goods[$i]['gc_id'] = $goods_info['gc_id'];
  1004. //计算商品金额
  1005. $goods_total = $goods_info['goods_price'] * $goods_info['goods_num'];
  1006. //计算商品平台优惠券优惠金额
  1007. $goodsmallvoucher = 0;
  1008. if (isset($input_mallvoucher_list) && !empty($input_mallvoucher_list)) {
  1009. $gc_id = ',' . $goods_info['gc_id'] . ',';
  1010. if (strpos($input_mallvoucher_list['mallvouchertemplate_gcidarr'], $gc_id) !== false) {
  1011. $proportion = sprintf("%.2f", $goods_total / $mallvouchertotal);
  1012. $goodsmallvoucher = sprintf("%.2f", $store_final_goods_total_after[$store_id] * $proportion);
  1013. }
  1014. }
  1015. //计算本件商品优惠金额
  1016. $promotion_value = floor($goods_total * ($promotion_rate));
  1017. $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value - $goodsmallvoucher;
  1018. $promotion_sum += $promotion_value;
  1019. $i++;
  1020. //存储库存报警数据
  1021. if (isset($goods_info['goods_storage_alarm']) && $goods_info['goods_storage_alarm'] >= ($goods_info['goods_storage'] - $goods_info['goods_num'])) {
  1022. $param = array();
  1023. $param['common_id'] = $goods_info['goods_commonid'];
  1024. $param['sku_id'] = $goods_info['goods_id'];
  1025. $ten_param = array($param['common_id'], $param['sku_id']);
  1026. $weixin_param = array(
  1027. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/goods/GoodsForm2?commonid=' . $goods_info['goods_commonid'] . '&class_id=' . $goods_info['gc_id'],
  1028. 'data' => array(
  1029. "keyword1" => array(
  1030. "value" => $goods_info['goods_storage'] - $goods_info['goods_num'],
  1031. "color" => "#333"
  1032. ),
  1033. "keyword2" => array(
  1034. "value" => date('Y-m-d H:i'),
  1035. "color" => "#333"
  1036. )
  1037. ),
  1038. );
  1039. $notice_list['goods_storage_alarm'][$goods_info['store_id']] = array('param' => $param, 'ali_param' => $param, 'ten_param' => $ten_param, 'weixin_param' => $weixin_param);
  1040. }
  1041. } elseif (!empty($goods_info['bl_goods_list']) && is_array($goods_info['bl_goods_list'])) {
  1042. $ifgroupbuy = false;
  1043. //优惠套装
  1044. foreach ($goods_info['bl_goods_list'] as $bl_goods_info) {
  1045. $order_goods[$i]['order_id'] = $order_id;
  1046. $order_goods[$i]['goods_id'] = $bl_goods_info['goods_id'];
  1047. $order_goods[$i]['store_id'] = $store_id;
  1048. $order_goods[$i]['goods_name'] = $bl_goods_info['goods_name'];
  1049. $order_goods[$i]['goods_price'] = $bl_goods_info['blgoods_price'];
  1050. $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
  1051. $order_goods[$i]['goods_image'] = $bl_goods_info['goods_image'];
  1052. $order_goods[$i]['buyer_id'] = $member_id;
  1053. $order_goods[$i]['goods_type'] = 4;
  1054. $order_goods[$i]['promotions_id'] = $bl_goods_info['bl_id'];
  1055. $order_goods[$i]['commis_rate'] = floatval(@$store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
  1056. $order_goods[$i]['gc_id'] = $bl_goods_info['gc_id'];
  1057. //计算商品实际支付金额(goods_price减去分摊优惠金额后的值)
  1058. $goods_total = $bl_goods_info['blgoods_price'] * $goods_info['goods_num'];
  1059. //计算商品平台优惠券优惠金额
  1060. $goodsmallvoucher = 0;
  1061. if (isset($input_mallvoucher_list) && !empty($input_mallvoucher_list)) {
  1062. $gc_id = ',' . $goods_info['gc_id'] . ',';
  1063. if (strpos($input_mallvoucher_list['mallvouchertemplate_gcidarr'], $gc_id) !== false) {
  1064. $proportion = sprintf("%.2f", $goods_total / $mallvouchertotal);
  1065. $goodsmallvoucher = sprintf("%.2f", $store_final_goods_total_after[$store_id] * $proportion);
  1066. }
  1067. }
  1068. //计算本件商品优惠金额
  1069. $promotion_value = floor($goods_total * ($promotion_rate));
  1070. $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value - $goodsmallvoucher;
  1071. $promotion_sum += $promotion_value;
  1072. $i++;
  1073. //存储库存报警数据
  1074. if ($bl_goods_info['goods_storage_alarm'] >= ($bl_goods_info['goods_storage'] - $goods_info['goods_num'])) {
  1075. $param = array();
  1076. $param['common_id'] = $bl_goods_info['goods_commonid'];
  1077. $param['sku_id'] = $bl_goods_info['goods_id'];
  1078. $ten_param = array($param['common_id'], $param['sku_id']);
  1079. $weixin_param = array(
  1080. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/goods/GoodsForm2?commonid=' . $goods_info['goods_commonid'] . '&class_id=' . $goods_info['gc_id'],
  1081. 'data' => array(
  1082. "keyword1" => array(
  1083. "value" => $goods_info['goods_storage'] - $goods_info['goods_num'],
  1084. "color" => "#333"
  1085. ),
  1086. "keyword2" => array(
  1087. "value" => date('Y-m-d H:i'),
  1088. "color" => "#333"
  1089. )
  1090. ),
  1091. );
  1092. $notice_list['goods_storage_alarm'][$bl_goods_info['store_id']] = array('param' => $param, 'ali_param' => $param, 'ten_param' => $ten_param, 'weixin_param' => $weixin_param);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. //将因舍出小数部分出现的差值补到最后一个商品的实际成交价中(商品goods_price=0时不给补,可能是赠品)
  1098. if ($promotion_total > $promotion_sum) {
  1099. $i--;
  1100. for ($i; $i >= 0; $i--) {
  1101. if (floatval($order_goods[$i]['goods_price']) > 0) {
  1102. $order_goods[$i]['goods_pay_price'] -= $promotion_total - $promotion_sum;
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. $insert = $order_model->addOrdergoods($order_goods);
  1108. if (!$insert) {
  1109. throw new \think\Exception('订单保存失败[未生成商品数据]', 10006);
  1110. }
  1111. //自提订单
  1112. $chain_order_data = Db::name('order')->where(array(array('order_id', '=', $order_id), array('chain_id', '>', 0)))->find();
  1113. $chain_list = array();
  1114. $chain_model = model('chain');
  1115. $chain_order_model = model('chain_order');
  1116. if (!empty($chain_order_data)) {
  1117. if (!isset($chain_list[$chain_order_data['chain_id']])) {
  1118. $chain_list[$chain_order_data['chain_id']] = $chain_model->getChainOpenInfo(array(array('chain_id', '=', $chain_order_data['chain_id'])));
  1119. }
  1120. if (!$chain_list[$chain_order_data['chain_id']]) {
  1121. throw new \think\Exception('自提点不存在[未生成自提数据]', 10006);
  1122. }
  1123. if ($chain_list[$chain_order_data['chain_id']]['chain_if_pickup'] != 1) {
  1124. throw new \think\Exception('自提点已关闭自提[未生成自提数据]', 10006);
  1125. }
  1126. $chain_order_model->addChainOrder(array(
  1127. 'order_id' => $chain_order_data['order_id'],
  1128. 'order_goods_id' => 0,
  1129. 'chain_order_type' => 2,
  1130. 'chain_order_pickup_code' => rand(1, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9),
  1131. 'payment_code' => $order_list[$order_id]['payment_code'],
  1132. 'chain_order_add_time' => TIMESTAMP,
  1133. 'order_sn' => $order_list[$order_id]['order_sn'],
  1134. 'chain_id' => $chain_order_data['chain_id'],
  1135. 'store_id' => $chain_order_data['store_id'],
  1136. ));
  1137. //减库存
  1138. foreach ($order_goods as $key => $val) {
  1139. Db::name('chain_goods')->where(array(array('chain_id', '=', $chain_order_data['chain_id']), array('goods_id', '=', $val['goods_id'])))->dec('goods_storage', $val['goods_num'])->update();
  1140. }
  1141. }
  1142. $order_list[$order_id]['order_goods'] = $order_goods;
  1143. //存储商家发货提醒数据
  1144. if ($order['order_state'] == ORDER_STATE_PAY) {
  1145. //更改自提点的订单状态
  1146. $chain_order_model->editChainOrderPay($order_id);
  1147. $weixin_param = array(
  1148. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/order/OrderDetail?order_id=' . $order_id,
  1149. 'data' => array(
  1150. "keyword1" => array(
  1151. "value" => $order['order_sn'],
  1152. "color" => "#333"
  1153. ),
  1154. "keyword2" => array(
  1155. "value" => $order_goods[0]['goods_name'] . (count($order_goods) > 1 ? sprintf(lang('order_goods_more_than_one'), count($order_goods)) : ''),
  1156. "color" => "#333"
  1157. ),
  1158. "keyword3" => array(
  1159. "value" => $order['order_amount'],
  1160. "color" => "#333"
  1161. ),
  1162. "keyword4" => array(
  1163. "value" => date('Y-m-d H:i', $order['add_time']),
  1164. "color" => "#333"
  1165. )
  1166. ),
  1167. );
  1168. $notice_list['new_order'][$order['store_id']] = array('param' => array('order_sn' => $order['order_sn']), 'ali_param' => array('order_sn' => $order['order_sn']), 'ten_param' => array($order['order_sn']), 'weixin_param' => $weixin_param);
  1169. }
  1170. }
  1171. //保存数据
  1172. $this->_order_data['pay_sn'] = $pay_sn;
  1173. $this->_order_data['order_list'] = $order_list;
  1174. $this->_order_data['notice_list'] = $notice_list;
  1175. $this->_order_data['ifgroupbuy'] = $ifgroupbuy;
  1176. $this->_order_data['input_mallvoucher_list'] = $input_mallvoucher_list;
  1177. }
  1178. /**
  1179. * 订单后续其它处理
  1180. */
  1181. private function _createOrderStep6()
  1182. {
  1183. $ifcart = $this->_post_data['ifcart'];
  1184. $goods_buy_quantity = $this->_order_data['goods_buy_quantity'];
  1185. $input_voucher_list = $this->_order_data['input_voucher_list'];
  1186. $input_mallvoucher_list = $this->_order_data['input_mallvoucher_list'];
  1187. $store_cart_list = $this->_order_data['store_cart_list'];
  1188. $input_buy_items = $this->_order_data['input_buy_items'];
  1189. $order_list = $this->_order_data['order_list'];
  1190. $input_address_info = $this->_order_data['input_address_info'];
  1191. $notice_list = $this->_order_data['notice_list'];
  1192. $goodsfcode_id = $this->_order_data['goodsfcode_id'];
  1193. $ifgroupbuy = $this->_order_data['ifgroupbuy'];
  1194. //变更库存和销量
  1195. $res = model('goods')->createOrderUpdateStorage($goods_buy_quantity);
  1196. if (!$res['code']) {
  1197. throw new \think\Exception($res['msg'], 10006);
  1198. }
  1199. //更新使用的店铺代金券状态
  1200. if (!empty($input_voucher_list) && is_array($input_voucher_list)) {
  1201. model('voucher')->editVoucherState($input_voucher_list);
  1202. }
  1203. //更新使用的平台代金券状态
  1204. if (!empty($input_mallvoucher_list) && is_array($input_mallvoucher_list)) {
  1205. model('Mallvouchertemplate')->editMallvoucherState($input_mallvoucher_list);
  1206. }
  1207. //更新F码使用状态
  1208. if ($goodsfcode_id) {
  1209. model('goodsfcode')->updateGoodsfcode($goodsfcode_id);
  1210. }
  1211. //更新抢购购买人数和数量
  1212. if ($ifgroupbuy) {
  1213. foreach ($store_cart_list as $goods_list) {
  1214. foreach ($goods_list as $goods_info) {
  1215. if (isset($goods_info['ifgroupbuy']) && isset($goods_info['groupbuy_id'])) {
  1216. $groupbuy_info = array();
  1217. $groupbuy_info['groupbuy_id'] = $goods_info['groupbuy_id'];
  1218. $groupbuy_info['quantity'] = $goods_info['goods_num'];
  1219. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'editGroupbuySaleCount', 'cron_value' => serialize($groupbuy_info)));
  1220. }
  1221. }
  1222. }
  1223. }
  1224. //删除购物车中的商品
  1225. $this->delCart($ifcart, $this->_member_info['member_id'], array_keys($input_buy_items));
  1226. cookie('cart_goods_num', '', -3600);
  1227. //保存订单代收信息
  1228. if (config('ds_config.chain_isuse') && intval($input_address_info['chain_id'])) {
  1229. $data = array();
  1230. $data['chain_id'] = $input_address_info['chain_id'];
  1231. foreach ($order_list as $v) {
  1232. if (!$v['chain_id']) {
  1233. $data['order_sn_list'][$v['order_id']]['store_id'] = $v['store_id'];
  1234. $data['order_sn_list'][$v['order_id']]['order_sn'] = $v['order_sn'];
  1235. $data['order_sn_list'][$v['order_id']]['add_time'] = $v['add_time'];
  1236. }
  1237. }
  1238. if (isset($data['order_sn_list'])) {
  1239. model('chain_order')->saveChainOrder($data);
  1240. }
  1241. }
  1242. //生成推广记录
  1243. $this->addOrderInviter($order_list);
  1244. //发送提醒类信息
  1245. if (!empty($notice_list)) {
  1246. foreach ($notice_list as $code => $value) {
  1247. $temp = current($value);
  1248. model('cron')->addCron(array(
  1249. 'cron_exetime' => TIMESTAMP, 'cron_type' => 'sendStoremsg', 'cron_value' => serialize(array(
  1250. 'code' => $code, 'store_id' => key($value), 'param' => $temp['param'], 'weixin_param' => $temp['weixin_param'], 'ali_param' => $temp['ali_param'], 'ten_param' => $temp['ten_param']
  1251. )),
  1252. ));
  1253. }
  1254. }
  1255. }
  1256. /**
  1257. * 加密
  1258. * @param array /string $string
  1259. * @param int $member_id
  1260. * @return mixed arrray/string
  1261. */
  1262. public function buyEncrypt($string, $member_id)
  1263. {
  1264. $buy_key = sha1(md5($member_id . '&' . md5(config('ds_config.setup_date'))));
  1265. if (is_array($string)) {
  1266. $string = serialize($string);
  1267. } else {
  1268. $string = strval($string);
  1269. }
  1270. return ds_encrypt(base64_encode($string), $buy_key);
  1271. }
  1272. /**
  1273. * 解密
  1274. * @param string $string
  1275. * @param int $member_id
  1276. * @param number $ttl
  1277. */
  1278. public function buyDecrypt($string, $member_id, $ttl = 0)
  1279. {
  1280. $buy_key = sha1(md5($member_id . '&' . md5(config('ds_config.setup_date'))));
  1281. if (empty($string))
  1282. return;
  1283. $string = base64_decode(ds_decrypt(strval($string), $buy_key, $ttl));
  1284. return ($tmp = @unserialize($string)) !== false ? $tmp : $string;
  1285. }
  1286. /**
  1287. * 得到所购买的id和数量
  1288. *
  1289. */
  1290. private function _parseItems($cart_id)
  1291. {
  1292. //存放所购商品ID和数量组成的键值对
  1293. $buy_items = array();
  1294. if (is_array($cart_id)) {
  1295. foreach ($cart_id as $value) {
  1296. if (preg_match_all('/^(\d{1,10})\|(\d{1,6})$/', $value, $match)) {
  1297. if (intval($match[2][0]) > 0) {
  1298. $buy_items[$match[1][0]] = $match[2][0];
  1299. }
  1300. }
  1301. }
  1302. }
  1303. return $buy_items;
  1304. }
  1305. /**
  1306. * 从购物车数组中得到商品列表
  1307. * @param unknown $cart_list
  1308. */
  1309. private function _getGoodsList($cart_list)
  1310. {
  1311. if (empty($cart_list) || !is_array($cart_list))
  1312. return $cart_list;
  1313. $goods_list = array();
  1314. $i = 0;
  1315. foreach ($cart_list as $key => $cart) {
  1316. if (!$cart['state'] || !$cart['storage_state'])
  1317. continue;
  1318. //购买数量
  1319. $quantity = $cart['goods_num'];
  1320. if (!intval($cart['bl_id'])) {
  1321. //如果是普通商品
  1322. $goods_list[$i]['goods_num'] = $quantity;
  1323. $goods_list[$i]['goods_id'] = $cart['goods_id'];
  1324. $goods_list[$i]['store_id'] = $cart['store_id'];
  1325. $goods_list[$i]['gc_id'] = $cart['gc_id'];
  1326. $goods_list[$i]['goods_weight'] = $cart['goods_weight'];
  1327. $goods_list[$i]['gc_id_1'] = $cart['gc_id_1'];
  1328. $goods_list[$i]['gc_id_2'] = $cart['gc_id_2'];
  1329. $goods_list[$i]['gc_id_3'] = $cart['gc_id_3'];
  1330. $goods_list[$i]['goods_name'] = $cart['goods_name'];
  1331. $goods_list[$i]['goods_price'] = $cart['goods_price'];
  1332. $goods_list[$i]['goods_original_price'] = $cart['goods_original_price'];
  1333. $goods_list[$i]['store_name'] = $cart['store_name'];
  1334. $goods_list[$i]['goods_image'] = $cart['goods_image'];
  1335. $goods_list[$i]['transport_id'] = $cart['transport_id'];
  1336. $goods_list[$i]['goods_freight'] = $cart['goods_freight'];
  1337. $goods_list[$i]['goods_vat'] = $cart['goods_vat'];
  1338. $goods_list[$i]['is_goodsfcode'] = $cart['is_goodsfcode'];
  1339. $goods_list[$i]['bl_id'] = 0;
  1340. $i++;
  1341. } else {
  1342. //如果是优惠套装商品
  1343. foreach ($cart['bl_goods_list'] as $bl_goods) {
  1344. $goods_list[$i]['goods_num'] = $quantity;
  1345. $goods_list[$i]['goods_id'] = $bl_goods['goods_id'];
  1346. $goods_list[$i]['store_id'] = $cart['store_id'];
  1347. $goods_list[$i]['gc_id'] = $bl_goods['gc_id'];
  1348. $goods_list[$i]['goods_weight'] = $bl_goods['goods_weight'];
  1349. $goods_list[$i]['gc_id_1'] = $bl_goods['gc_id_1'];
  1350. $goods_list[$i]['gc_id_2'] = $bl_goods['gc_id_2'];
  1351. $goods_list[$i]['gc_id_3'] = $bl_goods['gc_id_3'];
  1352. $goods_list[$i]['goods_name'] = $bl_goods['goods_name'];
  1353. $goods_list[$i]['goods_price'] = $bl_goods['blgoods_price'];
  1354. $goods_list[$i]['goods_original_price'] = $bl_goods['goods_original_price'];
  1355. $goods_list[$i]['store_name'] = $bl_goods['store_name'];
  1356. $goods_list[$i]['goods_image'] = $bl_goods['goods_image'];
  1357. $goods_list[$i]['transport_id'] = $bl_goods['transport_id'];
  1358. $goods_list[$i]['goods_freight'] = $bl_goods['goods_freight'];
  1359. $goods_list[$i]['goods_vat'] = $bl_goods['goods_vat'];
  1360. $goods_list[$i]['bl_id'] = $cart['bl_id'];
  1361. $i++;
  1362. }
  1363. }
  1364. }
  1365. return $goods_list;
  1366. }
  1367. /**
  1368. * 将下单商品列表转换为以店铺ID为下标的数组
  1369. *
  1370. * @param array $cart_list
  1371. * @return array
  1372. */
  1373. private function _getStoreCartList($cart_list)
  1374. {
  1375. if (empty($cart_list) || !is_array($cart_list))
  1376. return $cart_list;
  1377. $new_array = array();
  1378. foreach ($cart_list as $cart) {
  1379. $new_array[$cart['store_id']][] = $cart;
  1380. }
  1381. return $new_array;
  1382. }
  1383. /**
  1384. * 本次下单是否需要码及F码合法性
  1385. * 无需使用F码,返回 true
  1386. * 需要使用F码,返回($goodsfcode_id/false)
  1387. */
  1388. private function _checkFcode($goods_list, $fcode)
  1389. {
  1390. $is_goodsfcode = false;
  1391. foreach ($goods_list as $k => $v) {
  1392. if ($v['is_goodsfcode'] == 1) {
  1393. $is_goodsfcode = true;
  1394. break;
  1395. }
  1396. }
  1397. if (!$is_goodsfcode)
  1398. return true;
  1399. if (empty($fcode) || count($goods_list) > 1) {
  1400. return false;
  1401. }
  1402. $goods_info = $goods_list[0];
  1403. $fcode_info = $this->checkFcode($goods_info['goods_commonid'], $fcode);
  1404. if ($fcode_info['code'] && !$fcode_info['data']['goodsfcode_state']) {
  1405. return intval($fcode_info['data']['goodsfcode_id']);
  1406. } else {
  1407. return false;
  1408. }
  1409. }
  1410. }