Buy.php 77 KB

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