Buy.php 77 KB

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