Buy.php 76 KB

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