Buyvirtual.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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 Buyvirtual {
  16. /**
  17. * 虚拟商品购买第一步,得到购买数据(商品、店铺、会员)
  18. * @param int $goods_id 商品ID
  19. * @param int $quantity 购买数量
  20. * @param int $member_id 会员ID
  21. * @return array
  22. */
  23. public function getBuyStep1Data($goods_id, $quantity, $member_id) {
  24. return $this->getBuyStepData($goods_id, $quantity, $member_id);
  25. }
  26. /**
  27. * 虚拟商品购买第二步,得到购买数据(商品、店铺、会员)
  28. * @param int $goods_id 商品ID
  29. * @param int $quantity 购买数量
  30. * @param int $member_id 会员ID
  31. * @return array
  32. */
  33. public function getBuyStep2Data($goods_id, $quantity, $member_id, $extra = array()) {
  34. return $this->getBuyStepData($goods_id, $quantity, $member_id, $extra);
  35. }
  36. /**
  37. * 得到虚拟商品购买数据(商品、店铺、会员)
  38. * @param int $goods_id 商品ID
  39. * @param int $quantity 购买数量
  40. * @param int $member_id 会员ID
  41. * @return array
  42. */
  43. public function getBuyStepData($goods_id, $quantity, $member_id, $extra = array()) {
  44. $goods_model=model('goods');
  45. $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($goods_id);
  46. if (empty($goods_info)) {
  47. return ds_callback(false, '该商品不符合购买条件,可能的原因有:下架、不存在、过期等');
  48. }
  49. $result2 = $goods_model->getGoodsCommonInfoByID($goods_info['goods_commonid']);
  50. $goods_info=array_merge($result2,$goods_info);
  51. if ($goods_info['is_virtual'] != 1 || ($goods_info['virtual_type'] == 0 && $goods_info['virtual_indate'] < TIMESTAMP)) {
  52. return ds_callback(false, '该商品不符合购买条件,可能的原因有:下架、不存在、过期等');
  53. }
  54. if ($goods_info['virtual_limit'] > $goods_info['goods_storage']) {
  55. $goods_info['virtual_limit'] = $goods_info['goods_storage'];
  56. }
  57. if (isset($extra['pintuan_id']) && intval($extra['pintuan_id']) > 0) {
  58. //如果是特定拼团商品,则只按照拼团的规则进行处理
  59. model('buy_1', 'logic')->getPintuanInfo($goods_info, $quantity, $extra, $member_id);
  60. } else {
  61. //取得抢购信息
  62. $goods_info = $this->_getGroupbuyInfo($goods_info);
  63. }
  64. $quantity = abs(intval($quantity));
  65. $quantity = $quantity == 0 ? 1 : $quantity;
  66. $quantity = $quantity > $goods_info['virtual_limit'] ? $goods_info['virtual_limit'] : $quantity;
  67. if ($quantity > $goods_info['goods_storage']) {
  68. return ds_callback(false, '该商品库存不足');
  69. }
  70. $goods_info['quantity'] = $quantity;
  71. $goods_info['goods_total'] = ds_price_format($goods_info['goods_price'] * $goods_info['quantity']);
  72. $goods_info['goods_original_total'] = ds_price_format($goods_info['goods_original_price'] * $goods_info['quantity']);
  73. $goods_info['goods_discount_total'] = ds_price_format($goods_info['goods_original_total'] - $goods_info['goods_total']);
  74. $goods_info['goods_image_url'] = goods_cthumb($goods_info['goods_image'], 240, $goods_info['store_id']);
  75. $return = array();
  76. $return['goods_info'] = $goods_info;
  77. $return['store_info'] = model('store')->getStoreOnlineInfoByID($goods_info['store_id'], 'store_name,store_id,member_id');
  78. $return['member_info'] = model('member')->getMemberInfoByID($member_id);
  79. // $pd_payment_info = model('payment')->getPaymentOpenInfo(array('payment_code'=>'predeposit'));
  80. // if (empty($pd_payment_info)) {
  81. // $return['member_info']['available_predeposit'] = 0;
  82. // $return['member_info']['available_rc_balance'] = 0;
  83. // }
  84. //返回店铺可用的代金券
  85. $return['store_voucher_list'] = array();
  86. if (config('ds_config.voucher_allow')) {
  87. $voucher_model = model('voucher');
  88. $condition = array();
  89. $condition[] = array('voucher_store_id', '=', $goods_info['store_id']);
  90. $condition[] = array('voucher_owner_id', '=', $member_id);
  91. $return['store_voucher_list'] = $voucher_model->getCurrentAvailableVoucher($condition, $goods_info['goods_total']);
  92. }
  93. //返回平台可用的代金券
  94. $return['mall_voucher_list'] = array();
  95. if (config('ds_config.voucher_allow')) {
  96. $goods_info['goods_num'] = $goods_info['quantity'];
  97. $goods_list[0] = $goods_info;
  98. $condition = array();
  99. $condition[] = array('mallvoucheruser_ownerid','=',$member_id);
  100. $condition[] = array('mallvoucheruser_goodsid','=',$goods_info['goods_id']);
  101. $return['mall_voucher_list'] = model('buy_1','logic')->getAvailableMallVoucherUserList($goods_list, $member_id);
  102. }
  103. return ds_callback(true, '', $return);
  104. }
  105. /**
  106. * 虚拟商品购买第三步
  107. * @param array $post 接收POST数据,必须传入goods_id:商品ID,quantity:购买数量,buyer_phone:接收手机,buyer_msg:买家留言
  108. * @param int $member_id
  109. * @return array
  110. */
  111. public function buyStep3($post, $member_id) {
  112. $result = $this->getBuyStepData($post['goods_id'], $post['quantity'], $member_id, $post);
  113. if (!$result['code'])
  114. return $result;
  115. $goods_info = $result['data']['goods_info'];
  116. $member_info = $result['data']['member_info'];
  117. $goods_info['store_voucher_list'] = isset($result['data']['store_voucher_list']) ? $result['data']['store_voucher_list'] : array();
  118. $goods_info['mall_voucher_list'] = isset($result['data']['mall_voucher_list'])?$result['data']['mall_voucher_list']:array();
  119. //应付总金额计算
  120. $pay_total = $goods_info['goods_price'] * $goods_info['quantity'];
  121. $store_id = $goods_info['store_id'];
  122. $store_goods_total_list = array($store_id => $pay_total);
  123. $pay_total = $store_goods_total_list[$store_id];
  124. if($goods_info['virtual_type']==0){
  125. if($post['buyer_phone']==encrypt_show($member_info['member_mobile'],4,4)){
  126. $post['buyer_phone']=$member_info['member_mobile'];
  127. }
  128. }else{
  129. $post['buyer_phone']='';
  130. }
  131. //整理数据
  132. $input = array();
  133. $input['quantity'] = $goods_info['quantity'];
  134. $input['buyer_phone'] = $post['buyer_phone'];
  135. $input['buyer_msg'] = $post['buyer_msg'];
  136. $input['pay_total'] = $pay_total;
  137. $input['order_from'] = $post['order_from'];
  138. $input['pintuan_id'] = isset($post['pintuan_id']) ? $post['pintuan_id'] : 0;
  139. $input['pintuangroup_id'] = isset($post['pintuangroup_id']) ? $post['pintuangroup_id'] : 0;
  140. $goods_model = model('goods');
  141. $input['voucher'] = isset($post['voucher']) ? $post['voucher'] : '';
  142. $input['mallvoucher'] = isset($post['mallvoucher'])?$post['mallvoucher']:'';
  143. try {
  144. //开始事务
  145. Db::startTrans();
  146. //生成订单
  147. $order_info = $this->_createOrder($input, $goods_info, $member_info);
  148. //生成推广记录
  149. $this->addOrderInviter($order_info);
  150. //变更库存和销量
  151. $res=$goods_model->createOrderUpdateStorage(array($goods_info['goods_id'] => $goods_info['quantity']),$goods_info['virtual_type']>1?true:false);
  152. if(!$res['code']){
  153. throw new \think\Exception($res['msg'], 10006);
  154. }
  155. //提交事务
  156. Db::commit();
  157. } catch (Exception $e) {
  158. //回滚事务
  159. Db::rollback();
  160. return ds_callback(false, $e->getMessage());
  161. }
  162. //更新抢购信息
  163. $this->_updateGroupBuy($goods_info);
  164. return ds_callback(true, '', array('order_id' => $order_info['order_id'], 'order_sn' => $order_info['order_sn']));
  165. }
  166. /**
  167. * 生成推广记录
  168. * @param array $order_list
  169. */
  170. public function addOrderInviter($order = array()) {
  171. if (!config('ds_config.inviter_open')) {
  172. return;
  173. }
  174. if (empty($order) || !is_array($order))
  175. return;
  176. $inviter_ratio_1 = config('ds_config.inviter_ratio_1');
  177. $inviter_ratio_2 = config('ds_config.inviter_ratio_2');
  178. $inviter_ratio_3 = config('ds_config.inviter_ratio_3');
  179. $orderinviter_model = model('orderinviter');
  180. $order_id = $order['order_id'];
  181. $goods = $order;
  182. //查询商品的分销信息
  183. $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();
  184. if (!$goods_common_info['inviter_open']) {
  185. return;
  186. }
  187. $goods_amount = $order['order_amount']*$goods_common_info['inviter_ratio']/100;
  188. $inviter_ratios = array(
  189. $inviter_ratio_1,
  190. $inviter_ratio_2,
  191. $inviter_ratio_3,
  192. );
  193. //判断买家是否是分销员
  194. if (config('ds_config.inviter_return')) {
  195. if (Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $order['buyer_id'])->value('inviter_id')) {
  196. if (isset($inviter_ratios[0]) && floatval($inviter_ratios[0]) > 0) {
  197. $ratio=round($inviter_ratios[0]*$goods_common_info['inviter_ratio']/100,2);
  198. $money_1 = round($inviter_ratios[0] / 100 * $goods_amount, 2);
  199. if ($money_1 > 0) {
  200. //生成推广记录
  201. Db::name('orderinviter')->insert(array(
  202. 'orderinviter_addtime' => TIMESTAMP,
  203. 'orderinviter_store_name' => $order['store_name'],
  204. 'orderinviter_goods_amount' => $order['order_amount'],
  205. 'orderinviter_goods_quantity' => $goods['goods_num'],
  206. 'orderinviter_order_type' => 1,
  207. 'orderinviter_store_id' => $goods['store_id'],
  208. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  209. 'orderinviter_goods_id' => $goods['goods_id'],
  210. 'orderinviter_level' => 1,
  211. 'orderinviter_ratio' => $ratio,
  212. 'orderinviter_goods_name' => $goods['goods_name'],
  213. 'orderinviter_order_id' => $order_id,
  214. 'orderinviter_order_sn' => $order['order_sn'],
  215. 'orderinviter_member_id' => $order['buyer_id'],
  216. 'orderinviter_member_name' => $order['buyer_name'],
  217. 'orderinviter_money' => $money_1,
  218. 'orderinviter_remark' => '获得分销员返佣,佣金比例' . $ratio . '%,订单号' . $order['order_sn'],
  219. ));
  220. }
  221. }
  222. }
  223. }
  224. //一级推荐人
  225. $inviter_1_id = Db::name('member')->where('member_id', $order['buyer_id'])->value('inviter_id');
  226. if (!$inviter_1_id || !Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $inviter_1_id)->value('inviter_id')) {
  227. return;
  228. }
  229. $inviter_1 = Db::name('member')->where('member_id', $inviter_1_id)->field('inviter_id,member_id,member_name')->find();
  230. if ($inviter_1 && isset($inviter_ratios[0]) && floatval($inviter_ratios[0]) > 0) {
  231. $ratio=round($inviter_ratios[0]*$goods_common_info['inviter_ratio']/100,2);
  232. $money_1 = round($inviter_ratios[0] / 100 * $goods_amount, 2);
  233. if ($money_1 > 0) {
  234. //生成推广记录
  235. Db::name('orderinviter')->insert(array(
  236. 'orderinviter_addtime' => TIMESTAMP,
  237. 'orderinviter_store_name' => $order['store_name'],
  238. 'orderinviter_goods_amount' => $order['order_amount'],
  239. 'orderinviter_goods_quantity' => $goods['goods_num'],
  240. 'orderinviter_order_type' => 1,
  241. 'orderinviter_store_id' => $goods['store_id'],
  242. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  243. 'orderinviter_goods_id' => $goods['goods_id'],
  244. 'orderinviter_level' => 1,
  245. 'orderinviter_ratio' => $ratio,
  246. 'orderinviter_goods_name' => $goods['goods_name'],
  247. 'orderinviter_order_id' => $order_id,
  248. 'orderinviter_order_sn' => $order['order_sn'],
  249. 'orderinviter_member_id' => $inviter_1['member_id'],
  250. 'orderinviter_member_name' => $inviter_1['member_name'],
  251. 'orderinviter_money' => $money_1,
  252. 'orderinviter_remark' => '获得一级推荐佣金,佣金比例' . $ratio . '%,推荐关系' . $inviter_1['member_name'] . '->' . $order['buyer_name'] . ',订单号' . $order['order_sn'],
  253. ));
  254. }
  255. }
  256. if (config('ds_config.inviter_level') <= 1) {
  257. return;
  258. }
  259. //二级推荐人
  260. $inviter_2_id = Db::name('member')->where('member_id', $inviter_1_id)->value('inviter_id');
  261. if (!$inviter_2_id || !Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $inviter_2_id)->value('inviter_id')) {
  262. return;
  263. }
  264. $inviter_2 = Db::name('member')->where('member_id', $inviter_2_id)->field('inviter_id,member_id,member_name')->find();
  265. if ($inviter_2 && isset($inviter_ratios[1]) && floatval($inviter_ratios[1]) > 0) {
  266. $ratio=round($inviter_ratios[1]*$goods_common_info['inviter_ratio']/100,2);
  267. $money_2 = round($inviter_ratios[1] / 100 * $goods_amount, 2);
  268. if ($money_2 > 0) {
  269. //生成推广记录
  270. Db::name('orderinviter')->insert(array(
  271. 'orderinviter_addtime' => TIMESTAMP,
  272. 'orderinviter_store_name' => $order['store_name'],
  273. 'orderinviter_goods_amount' => $order['order_amount'],
  274. 'orderinviter_goods_quantity' => $goods['goods_num'],
  275. 'orderinviter_order_type' => 1,
  276. 'orderinviter_store_id' => $goods['store_id'],
  277. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  278. 'orderinviter_goods_id' => $goods['goods_id'],
  279. 'orderinviter_level' => 2,
  280. 'orderinviter_ratio' => $ratio,
  281. 'orderinviter_goods_name' => $goods['goods_name'],
  282. 'orderinviter_order_id' => $order_id,
  283. 'orderinviter_order_sn' => $order['order_sn'],
  284. 'orderinviter_member_id' => $inviter_2['member_id'],
  285. 'orderinviter_member_name' => $inviter_2['member_name'],
  286. 'orderinviter_money' => $money_2,
  287. 'orderinviter_remark' => '获得二级推荐佣金,佣金比例' . $ratio . '%,推荐关系' . $inviter_2['member_name'] . '->' . $inviter_1['member_name'] . '->' . $order['buyer_name'] . ',订单号' . $order['order_sn'],
  288. ));
  289. }
  290. }
  291. if (config('ds_config.inviter_level') <= 2) {
  292. return;
  293. }
  294. //三级推荐人
  295. $inviter_3_id = Db::name('member')->where('member_id', $inviter_2_id)->value('inviter_id');
  296. if (!$inviter_3_id || !Db::name('inviter')->where('inviter_state=1 AND inviter_id=' . $inviter_3_id)->value('inviter_id')) {
  297. return;
  298. }
  299. $inviter_3 = Db::name('member')->where('member_id', $inviter_3_id)->field('inviter_id,member_id,member_name')->find();
  300. if ($inviter_3 && isset($inviter_ratios[2]) && floatval($inviter_ratios[2]) > 0) {
  301. $ratio=round($inviter_ratios[2]*$goods_common_info['inviter_ratio']/100,2);
  302. $money_3 = round($inviter_ratios[2] / 100 * $goods_amount, 2);
  303. if ($money_3 > 0) {
  304. //生成推广记录
  305. Db::name('orderinviter')->insert(array(
  306. 'orderinviter_addtime' => TIMESTAMP,
  307. 'orderinviter_store_name' => $order['store_name'],
  308. 'orderinviter_goods_amount' => $order['order_amount'],
  309. 'orderinviter_goods_quantity' => $goods['goods_num'],
  310. 'orderinviter_order_type' => 1,
  311. 'orderinviter_store_id' => $goods['store_id'],
  312. 'orderinviter_goods_commonid' => $goods_common_info['goods_commonid'],
  313. 'orderinviter_goods_id' => $goods['goods_id'],
  314. 'orderinviter_level' => 3,
  315. 'orderinviter_ratio' => $ratio,
  316. 'orderinviter_goods_name' => $goods['goods_name'],
  317. 'orderinviter_order_id' => $order_id,
  318. 'orderinviter_order_sn' => $order['order_sn'],
  319. 'orderinviter_member_id' => $inviter_3['member_id'],
  320. 'orderinviter_member_name' => $inviter_3['member_name'],
  321. 'orderinviter_money' => $money_3,
  322. 'orderinviter_remark' => '获得三级推荐佣金,佣金比例' . $ratio . '%,推荐关系' . $inviter_3['member_name'] . '->' . $inviter_2['member_name'] . '->' . $inviter_1['member_name'] . '->' . $order['buyer_name'] . ',订单号' . $order['order_sn'],
  323. ));
  324. }
  325. }
  326. }
  327. /**
  328. * 生成订单
  329. * @param array $input 表单数据
  330. * @param unknown $goods_info 商品数据
  331. * @param unknown $member_info 会员数据
  332. * @throws Exception
  333. * @return array
  334. */
  335. private function _createOrder($input, $goods_info, $member_info) {
  336. extract($input);
  337. $vrorder_model = model('vrorder');
  338. $goods_model = model('goods');
  339. //存储生成的订单,函数会返回该数组
  340. $order_list = array();
  341. $order = array();
  342. $order_code = array();
  343. //验证店铺代金券
  344. if (!empty($input['voucher'])) {
  345. if (preg_match_all('/^(\d+)\|(\d+)\|([\d.]+)$/', $input['voucher'], $matchs)) {
  346. if (floatval($matchs[3][0]) > 0) {
  347. $input_voucher = array();
  348. $input_voucher['vouchertemplate_id'] = $matchs[1][0];
  349. $input_voucher['voucher_price'] = $matchs[3][0];
  350. $voucher_list = $goods_info['store_voucher_list'];
  351. if (is_array($voucher_list) && isset($voucher_list[$input_voucher['vouchertemplate_id']])) {
  352. $input_voucher['voucher_id'] = $voucher_list[$input_voucher['vouchertemplate_id']]['voucher_id'];
  353. $input_voucher['voucher_code'] = $voucher_list[$input_voucher['vouchertemplate_id']]['voucher_code'];
  354. $input_voucher['voucher_owner_id'] = $voucher_list[$input_voucher['vouchertemplate_id']]['voucher_owner_id'];
  355. $input_voucher['voucher_price'] = floatval($input_voucher['voucher_price']);
  356. $order['voucher_price'] = $input_voucher['voucher_price'];
  357. $order['voucher_code'] = $input_voucher['voucher_code'];
  358. $pay_total = bcsub($pay_total, $input_voucher['voucher_price'], 2);
  359. if ($pay_total < 0) {
  360. $pay_total = 0;
  361. }
  362. } else {
  363. $input_voucher = array();
  364. }
  365. }
  366. }
  367. }
  368. //验证平台代金券
  369. if (!empty($input['mallvoucher'])) {
  370. if (preg_match_all('/^(\d+)\|([\d.]+)$/', $input['mallvoucher'], $matchs)) {
  371. if (floatval($matchs[2][0]) > 0) {
  372. $input_mallvoucher=array();
  373. $input_mallvoucher['mallvouchertemplate_id'] = $matchs[1][0];
  374. $input_mallvoucher['mallvoucheruser_price'] = $matchs[2][0];
  375. $mallvoucher_list = $goods_info['mall_voucher_list'];
  376. if(is_array($mallvoucher_list) && isset($mallvoucher_list[$input_mallvoucher['mallvouchertemplate_id']])){
  377. $input_mallvoucher['mallvoucheruser_id'] = $mallvoucher_list[$input_mallvoucher['mallvouchertemplate_id']]['mallvoucheruser_id'];
  378. $input_mallvoucher['mallvoucheruser_code'] = $mallvoucher_list[$input_mallvoucher['mallvouchertemplate_id']]['mallvoucheruser_code'];
  379. $input_mallvoucher['mallvoucheruser_ownerid'] = $mallvoucher_list[$input_mallvoucher['mallvouchertemplate_id']]['mallvoucheruser_ownerid'];
  380. $input_mallvoucher['mallvoucheruser_price']=floatval($input_mallvoucher['mallvoucheruser_price']);
  381. $order['mallvoucher_price'] = $input_mallvoucher['mallvoucheruser_price'];
  382. $order['mallvoucher_code'] = $input_mallvoucher['mallvoucheruser_code'];
  383. $pay_total=bcsub($pay_total,$input_mallvoucher['mallvoucheruser_price'],2);
  384. if($pay_total<0){
  385. $pay_total=0;
  386. }
  387. }else{
  388. $input_mallvoucher=array();
  389. }
  390. }
  391. }
  392. }
  393. $order['order_sn'] = makePaySn($member_info['member_id']);
  394. $order['store_id'] = $goods_info['store_id'];
  395. $order['store_name'] = $goods_info['store_name'];
  396. $order['buyer_id'] = $member_info['member_id'];
  397. $order['buyer_name'] = $member_info['member_name'];
  398. $order['buyer_phone'] = $input['buyer_phone'];
  399. $order['buyer_msg'] = $input['buyer_msg'];
  400. $order['add_time'] = TIMESTAMP;
  401. $order['order_state'] = ORDER_STATE_NEW;
  402. $order['order_amount'] = $pay_total;
  403. $order['goods_id'] = $goods_info['goods_id'];
  404. $order['goods_name'] = $goods_info['goods_name'];
  405. $order['goods_price'] = $goods_info['goods_price'];
  406. $order['goods_num'] = $input['quantity'];
  407. $order['goods_image'] = $goods_info['goods_image'];
  408. $store_gc_id_commis_rate_list = model('storebindclass')->getStoreGcidCommisRateList(array($goods_info));
  409. $order['commis_rate'] = floatval(@$store_gc_id_commis_rate_list[$goods_info['store_id']][$goods_info['gc_id']]);
  410. $order['gc_id'] = $goods_info['gc_id'];
  411. $order['virtual_type'] = $goods_info['virtual_type'];
  412. if($order['virtual_type']>0){
  413. if($order['virtual_type']==1){
  414. $res=$goods_model->getAvailableGoodsCard($goods_info);
  415. if($res['code']){
  416. if($input['quantity']>count($res['data']['card_list'])){
  417. throw new \think\Exception('卡券数量不足', 10006);
  418. }
  419. $virtual_content=[];
  420. for($i=0;$i<$input['quantity'];$i++){
  421. $virtual_content[]=$res['data']['card_list'][$i];
  422. }
  423. $virtual_content= implode('\r\n', $virtual_content);
  424. }else{
  425. throw new \think\Exception($res['msg'], 10006);
  426. }
  427. }else{
  428. $virtual_content=$goods_info['virtual_content'];
  429. }
  430. $order['virtual_content'] = $virtual_content;
  431. }
  432. $order['vr_indate'] = $goods_info['virtual_indate'];
  433. $order['vr_invalid_refund'] = $goods_info['virtual_invalid_refund'];
  434. $order['order_from'] = $input['order_from'];
  435. $order['order_promotion_type'] = 0;
  436. if (isset($goods_info['ifgroupbuy']) && $goods_info['ifgroupbuy'] == 1) {
  437. $order['order_promotion_type'] = 1;
  438. $order['promotions_id'] = $goods_info['groupbuy_id'];
  439. } else if (isset($goods_info['ifpintuan']) && intval($input['pintuan_id']) > 0) {
  440. $order['order_promotion_type'] = 2;
  441. $order['promotions_id'] = $input['pintuangroup_id'];
  442. }
  443. $order_id = $vrorder_model->addVrorder($order);
  444. if (!$order_id) {
  445. throw new \think\Exception('订单保存失败', 10006);
  446. }
  447. $order['order_id'] = $order_id;
  448. if ($order['order_promotion_type'] == 2) {
  449. $res = model('buy_1', 'logic')->updatePintuan($input, $goods_info, $order, 1, $member_info['member_id']);
  450. if (!$order['promotions_id']) {
  451. $vrorder_model->editVrorder(array('promotions_id' => $res['pintuangroup_id']), array('order_id' => $order['order_id']));
  452. }
  453. }
  454. // 提醒[库存报警]
  455. if ($goods_info['goods_storage_alarm'] >= ($goods_info['goods_storage'] - $input['quantity'])) {
  456. $param = array();
  457. $param['common_id'] = $goods_info['goods_commonid'];
  458. $param['sku_id'] = $goods_info['goods_id'];
  459. $ten_param=array($param['common_id'],$param['sku_id']);
  460. $weixin_param = array(
  461. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/goods/GoodsForm2?commonid=' . $goods_info['goods_commonid'] . '&class_id=' . $goods_info['gc_id'],
  462. 'data' => array(
  463. "keyword1" => array(
  464. "value" => $goods_info['goods_storage'] - $input['quantity'],
  465. "color" => "#333"
  466. ),
  467. "keyword2" => array(
  468. "value" => date('Y-m-d H:i'),
  469. "color" => "#333"
  470. )
  471. ),
  472. );
  473. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendStoremsg','cron_value'=>serialize(array('code' => 'goods_storage_alarm', 'store_id' => $goods_info['store_id'], 'param' => $param, 'ali_param' => $param, 'ten_param' => $ten_param, 'weixin_param' => $weixin_param))));
  474. }
  475. //更新使用的店铺代金券状态
  476. if (!empty($input_voucher) && is_array($input_voucher)) {
  477. $input_voucher_list[0] = $input_voucher;
  478. model('voucher')->editVoucherState($input_voucher_list);
  479. }
  480. //更新使用的平台代金券状态
  481. if (!empty($input_mallvoucher) && is_array($input_mallvoucher)) {
  482. model('Mallvouchertemplate')->editMallvoucherState($input_mallvoucher);
  483. }
  484. return $order;
  485. }
  486. /**
  487. * 更新抢购购买人数和数量
  488. */
  489. private function _updateGroupBuy($goods_info) {
  490. if (isset($goods_info['ifgroupbuy']) && $goods_info['groupbuy_id']) {
  491. $groupbuy_info = array();
  492. $groupbuy_info['groupbuy_id'] = $goods_info['groupbuy_id'];
  493. $groupbuy_info['quantity'] = $goods_info['quantity'];
  494. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'editGroupbuySaleCount','cron_value'=>serialize($groupbuy_info)));
  495. }
  496. }
  497. /**
  498. * 充值卡支付
  499. * 如果充值卡足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  500. */
  501. private function _rcbPay($order_info, $input, $buyer_info) {
  502. $available_rcb_amount = floatval($buyer_info['available_rc_balance']);
  503. if ($available_rcb_amount <= 0)
  504. return;
  505. if(!isset($order_info['rcb_amount'])){
  506. $order_info['rcb_amount']=0;
  507. }
  508. if(!isset($order_info['pd_amount'])){
  509. $order_info['pd_amount']=0;
  510. }
  511. $vrorder_model = model('vrorder');
  512. $predeposit_model = model('predeposit');
  513. $order_amount = floatval($order_info['order_amount']);
  514. $data_pd = array();
  515. $data_pd['member_id'] = $buyer_info['member_id'];
  516. $data_pd['member_name'] = $buyer_info['member_name'];
  517. $data_pd['amount'] = $order_amount;
  518. $data_pd['order_sn'] = $order_info['order_sn'];
  519. if ($available_rcb_amount >= $order_amount) {
  520. // 预存款立即支付,订单支付完成
  521. $predeposit_model->changeRcb('order_pay', $data_pd);
  522. $available_rcb_amount -= $order_amount;
  523. // 订单状态 置为已支付
  524. $data_order = array();
  525. $order_info['order_state'] = $data_order['order_state'] = ORDER_STATE_PAY;
  526. $data_order['payment_time'] = TIMESTAMP;
  527. $data_order['payment_code'] = 'predeposit';
  528. $data_order['rcb_amount'] = $order_info['order_amount'];
  529. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  530. if (!$result) {
  531. throw new \think\Exception('订单更新失败', 10006);
  532. }
  533. if ($order_info['order_promotion_type'] != 2) {
  534. //发放兑换码
  535. $insert = $vrorder_model->addVrorderCode($order_info);
  536. //发送兑换码到手机
  537. $param = array('order_id' => $order_info['order_id'], 'buyer_id' => $order_info['buyer_id'], 'buyer_phone' => $order_info['buyer_phone']);
  538. $vrorder_model->sendVrCode($param);
  539. // 支付成功发送店铺消息
  540. $param = array();
  541. $param['code'] = 'new_order';
  542. $param['store_id'] = $order_info['store_id'];
  543. $param['ali_param'] = array(
  544. 'order_sn' => $order_info['order_sn']
  545. );
  546. $param['ten_param'] = array(
  547. $order_info['order_sn']
  548. );
  549. $param['param'] = $param['ali_param'];
  550. $param['weixin_param'] = array(
  551. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/vrorder/OrderDetail?order_id=' . $order_info['order_id'],
  552. 'data' => array(
  553. "keyword1" => array(
  554. "value" => $order_info['order_sn'],
  555. "color" => "#333"
  556. ),
  557. "keyword2" => array(
  558. "value" => $order_info['goods_name'],
  559. "color" => "#333"
  560. ),
  561. "keyword3" => array(
  562. "value" => $order_info['order_amount'],
  563. "color" => "#333"
  564. ),
  565. "keyword4" => array(
  566. "value" => date('Y-m-d H:i', $order_info['add_time']),
  567. "color" => "#333"
  568. )
  569. ),
  570. );
  571. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendStoremsg','cron_value'=>serialize($param)));
  572. if (!$insert) {
  573. throw new \think\Exception('兑换码发送失败', 10006);
  574. }
  575. }
  576. } else {
  577. //暂冻结预存款,后面还需要 API彻底完成支付
  578. $data_pd['amount'] = $available_rcb_amount;
  579. $predeposit_model->changeRcb('order_freeze', $data_pd);
  580. //预存款支付金额保存到订单
  581. $data_order = array();
  582. $order_info['rcb_amount'] = $data_order['rcb_amount'] = $available_rcb_amount;
  583. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  584. if (!$result) {
  585. throw new \think\Exception('订单更新失败', 10006);
  586. }
  587. }
  588. return $order_info;
  589. }
  590. /**
  591. * 预存款支付
  592. * 如果预存款足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  593. */
  594. private function _pdPay($order_info, $input, $buyer_info) {
  595. if ($order_info['order_state'] == ORDER_STATE_PAY)
  596. return;
  597. $available_pd_amount = floatval($buyer_info['available_predeposit']);
  598. if ($available_pd_amount <= 0)
  599. return;
  600. if(!isset($order_info['rcb_amount'])){
  601. $order_info['rcb_amount']=0;
  602. }
  603. if(!isset($order_info['pd_amount'])){
  604. $order_info['pd_amount']=0;
  605. }
  606. $vrorder_model = model('vrorder');
  607. $predeposit_model = model('predeposit');
  608. //充值卡支付金额
  609. $rcb_amount = isset($order_info['rcb_amount']) ? floatval($order_info['rcb_amount']) : 0;
  610. $order_amount = floatval($order_info['order_amount']) - $rcb_amount;
  611. $data_pd = array();
  612. $data_pd['member_id'] = $buyer_info['member_id'];
  613. $data_pd['member_name'] = $buyer_info['member_name'];
  614. $data_pd['amount'] = $order_amount;
  615. $data_pd['order_sn'] = $order_info['order_sn'];
  616. if ($available_pd_amount >= $order_amount) {
  617. //预存款立即支付,订单支付完成
  618. $predeposit_model->changePd('order_pay', $data_pd);
  619. $available_pd_amount -= $order_amount;
  620. //下单,支付被冻结的充值卡
  621. $pd_amount = $rcb_amount;
  622. if ($pd_amount > 0) {
  623. $data_pd = array();
  624. $data_pd['member_id'] = $buyer_info['member_id'];
  625. $data_pd['member_name'] = $buyer_info['member_name'];
  626. $data_pd['amount'] = $pd_amount;
  627. $data_pd['order_sn'] = $order_info['order_sn'];
  628. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  629. }
  630. // 订单状态 置为已支付
  631. $data_order = array();
  632. $data_order['order_state'] = ORDER_STATE_PAY;
  633. $data_order['payment_time'] = TIMESTAMP;
  634. $data_order['payment_code'] = 'predeposit';
  635. $data_order['pd_amount'] = $order_amount;
  636. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  637. if (!$result) {
  638. throw new \think\Exception('订单更新失败', 10006);
  639. }
  640. if ($order_info['order_promotion_type'] != 2) {
  641. //发放兑换码
  642. $vrorder_model->addVrorderCode($order_info);
  643. //发送兑换码到手机
  644. $param = array('order_id' => $order_info['order_id'], 'buyer_id' => $order_info['buyer_id'], 'buyer_phone' => $order_info['buyer_phone']);
  645. $vrorder_model->sendVrCode($param);
  646. // 支付成功发送店铺消息
  647. $param = array();
  648. $param['code'] = 'new_order';
  649. $param['store_id'] = $order_info['store_id'];
  650. $param['ali_param'] = array(
  651. 'order_sn' => $order_info['order_sn']
  652. );
  653. $param['ten_param'] = array(
  654. $order_info['order_sn']
  655. );
  656. $param['param'] = $param['ali_param'];
  657. $param['weixin_param'] = array(
  658. 'url' => config('ds_config.h5_store_site_url') . '/pages/seller/vrorder/OrderDetail?order_id=' . $order_info['order_id'],
  659. 'data' => array(
  660. "keyword1" => array(
  661. "value" => $order_info['order_sn'],
  662. "color" => "#333"
  663. ),
  664. "keyword2" => array(
  665. "value" => $order_info['goods_name'],
  666. "color" => "#333"
  667. ),
  668. "keyword3" => array(
  669. "value" => $order_info['order_amount'],
  670. "color" => "#333"
  671. ),
  672. "keyword4" => array(
  673. "value" => date('Y-m-d H:i', $order_info['add_time']),
  674. "color" => "#333"
  675. )
  676. ),
  677. );
  678. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendStoremsg','cron_value'=>serialize($param)));
  679. }
  680. } else {
  681. //暂冻结预存款,后面还需要 API彻底完成支付
  682. $data_pd['amount'] = $available_pd_amount;
  683. $predeposit_model->changePd('order_freeze', $data_pd);
  684. //预存款支付金额保存到订单
  685. $data_order = array();
  686. $data_order['pd_amount'] = $available_pd_amount;
  687. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  688. if (!$result) {
  689. throw new \think\Exception('订单更新失败', 10006);
  690. }
  691. }
  692. }
  693. /**
  694. * 充值卡支付
  695. * 如果充值卡足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  696. */
  697. public function rcbPay($order_info, $input, $buyer_info) {
  698. $available_rcb_amount = floatval($buyer_info['available_rc_balance']);
  699. if ($available_rcb_amount <= 0)
  700. return $order_info;
  701. $vrorder_model = model('vrorder');
  702. $predeposit_model = model('predeposit');
  703. $order_amount = round($order_info['order_amount'] - $order_info['rcb_amount'] - $order_info['pd_amount'], 2);
  704. $data_pd = array();
  705. $data_pd['member_id'] = $buyer_info['member_id'];
  706. $data_pd['member_name'] = $buyer_info['member_name'];
  707. $data_pd['amount'] = $order_amount;
  708. $data_pd['order_sn'] = $order_info['order_sn'];
  709. if ($available_rcb_amount >= $order_amount) {
  710. // 预存款立即支付,订单支付完成
  711. $predeposit_model->changeRcb('order_pay', $data_pd);
  712. $available_rcb_amount -= $order_amount;
  713. //支付被冻结的充值卡
  714. $rcb_amount = isset($order_info['rcb_amount']) ? floatval($order_info['rcb_amount']) : 0;
  715. if ($rcb_amount > 0) {
  716. $data_pd = array();
  717. $data_pd['member_id'] = $buyer_info['member_id'];
  718. $data_pd['member_name'] = $buyer_info['member_name'];
  719. $data_pd['amount'] = $rcb_amount;
  720. $data_pd['order_sn'] = $order_info['order_sn'];
  721. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  722. }
  723. // 订单状态 置为已支付
  724. $data_order = array();
  725. $order_info['order_state'] = $data_order['order_state'] = ORDER_STATE_PAY;
  726. $data_order['payment_time'] = TIMESTAMP;
  727. $data_order['payment_code'] = 'predeposit';
  728. $data_order['rcb_amount'] = round($order_info['rcb_amount'] + $order_amount, 2);
  729. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  730. if (!$result) {
  731. throw new \think\Exception('订单更新失败', 10006);
  732. }
  733. if ($order_info['order_promotion_type'] != 2) {
  734. if($order_info['virtual_type']==0){
  735. //发放兑换码
  736. $insert = $vrorder_model->addVrorderCode($order_info);
  737. if (!$insert) {
  738. throw new \think\Exception('兑换码发送失败', 10006);
  739. }
  740. }else{
  741. $logic_vrorder = model('vrorder','logic');
  742. $result = $logic_vrorder->changeOrderStateSuccess($order_info['order_id']);
  743. if (!$result['code']) {
  744. throw new \think\Exception($result['msg'], 10006);
  745. }
  746. }
  747. }
  748. } else {
  749. //暂冻结预存款,后面还需要 API彻底完成支付
  750. $data_pd['amount'] = $available_rcb_amount;
  751. $predeposit_model->changeRcb('order_freeze', $data_pd);
  752. //预存款支付金额保存到订单
  753. $data_order = array();
  754. $order_info['rcb_amount'] = $data_order['rcb_amount'] = round($order_info['rcb_amount'] + $available_rcb_amount, 2);
  755. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  756. if (!$result) {
  757. throw new \think\Exception('订单更新失败', 10006);
  758. }
  759. }
  760. return $order_info;
  761. }
  762. /**
  763. * 预存款支付 主要处理
  764. * 如果预存款足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  765. */
  766. public function pdPay($order_info, $input, $buyer_info) {
  767. if ($order_info['order_state'] == ORDER_STATE_PAY)
  768. return $order_info;
  769. $available_pd_amount = floatval($buyer_info['available_predeposit']);
  770. if ($available_pd_amount <= 0)
  771. return $order_info;
  772. $vrorder_model = model('vrorder');
  773. $predeposit_model = model('predeposit');
  774. $order_amount = round($order_info['order_amount'] - $order_info['rcb_amount'] - $order_info['pd_amount'], 2);
  775. $data_pd = array();
  776. $data_pd['member_id'] = $buyer_info['member_id'];
  777. $data_pd['member_name'] = $buyer_info['member_name'];
  778. $data_pd['amount'] = $order_amount;
  779. $data_pd['order_sn'] = $order_info['order_sn'];
  780. if ($available_pd_amount >= $order_amount) {
  781. //预存款立即支付,订单支付完成
  782. $predeposit_model->changePd('order_pay', $data_pd);
  783. $available_pd_amount -= $order_amount;
  784. //下单,支付被冻结的充值卡
  785. $pd_amount = floatval($order_info['rcb_amount']);
  786. if ($pd_amount > 0) {
  787. $data_pd = array();
  788. $data_pd['member_id'] = $buyer_info['member_id'];
  789. $data_pd['member_name'] = $buyer_info['member_name'];
  790. $data_pd['amount'] = $pd_amount;
  791. $data_pd['order_sn'] = $order_info['order_sn'];
  792. $predeposit_model->changeRcb('order_comb_pay', $data_pd);
  793. }
  794. //支付被冻结的预存款
  795. $pd_amount = isset($order_info['pd_amount']) ? floatval($order_info['pd_amount']) : 0;
  796. if ($pd_amount > 0) {
  797. $data_pd = array();
  798. $data_pd['member_id'] = $buyer_info['member_id'];
  799. $data_pd['member_name'] = $buyer_info['member_name'];
  800. $data_pd['amount'] = $pd_amount;
  801. $data_pd['order_sn'] = $order_info['order_sn'];
  802. $predeposit_model->changePd('order_comb_pay', $data_pd);
  803. }
  804. // 订单状态 置为已支付
  805. $data_order = array();
  806. $order_info['order_state'] = $data_order['order_state'] = ORDER_STATE_PAY;
  807. $order_info['payment_time'] = $data_order['payment_time'] = TIMESTAMP;
  808. $order_info['payment_code'] = $data_order['payment_code'] = 'predeposit';
  809. $order_info['pd_amount'] = $data_order['pd_amount'] = round($order_info['pd_amount'] + $order_amount, 2);
  810. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  811. if (!$result) {
  812. throw new \think\Exception('订单更新失败', 10006);
  813. }
  814. if ($order_info['order_promotion_type'] != 2) {
  815. if($order_info['virtual_type']==0){
  816. //发放兑换码
  817. $vrorder_model->addVrorderCode($order_info);
  818. }else{
  819. $logic_vrorder = model('vrorder','logic');
  820. $result = $logic_vrorder->changeOrderStateSuccess($order_info['order_id']);
  821. if (!$result['code']) {
  822. throw new \think\Exception($result['msg'], 10006);
  823. }
  824. }
  825. }
  826. } else {
  827. //暂冻结预存款,后面还需要 API彻底完成支付
  828. $data_pd['amount'] = $available_pd_amount;
  829. $predeposit_model->changePd('order_freeze', $data_pd);
  830. //预存款支付金额保存到订单
  831. $data_order = array();
  832. $order_info['pd_amount'] = $data_order['pd_amount'] = round($order_info['pd_amount'] + $available_pd_amount, 2);
  833. $result = $vrorder_model->editVrorder($data_order, array('order_id' => $order_info['order_id']));
  834. if (!$result) {
  835. throw new \think\Exception('订单更新失败', 10006);
  836. }
  837. }
  838. return $order_info;
  839. }
  840. /**
  841. * 取得抢购信息
  842. * @param array $goods_info
  843. * @return array
  844. */
  845. private function _getGroupbuyInfo($goods_info = array()) {
  846. if (!config('ds_config.groupbuy_allow') || empty($goods_info) || !is_array($goods_info))
  847. return $goods_info;
  848. $groupbuy_info = model('groupbuy')->getGroupbuyInfoByGoodsCommonID($goods_info['goods_commonid']);
  849. if (empty($groupbuy_info))
  850. return $goods_info;
  851. // 虚拟抢购数量限制
  852. if ($groupbuy_info['groupbuy_upper_limit'] > 0 && $groupbuy_info['groupbuy_upper_limit'] < $goods_info['virtual_limit']) {
  853. $goods_info['virtual_limit'] = $groupbuy_info['groupbuy_upper_limit'];
  854. }
  855. $goods_info['goods_price'] = $groupbuy_info['groupbuy_price'];
  856. $goods_info['groupbuy_id'] = $groupbuy_info['groupbuy_id'];
  857. $goods_info['ifgroupbuy'] = true;
  858. return $goods_info;
  859. }
  860. }