cartLogic = new \app\home\logic\CartLogic();
$sid = I('stoid');
if (session('?user')) {
$user = session('user');
$user = M('users')->where("user_id", $user['user_id'])->where("store_id", $sid)->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
} else {
$usrid = Cookie::get('user_id');
if ($usrid) {
$user = M('users')->where("user_id", $usrid)->where("store_id", $sid)->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
}
}
if (!$this->user_id) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
}
//选择赠送单个或多人
public function senduser()
{
//
return $this->fetch('',getMobileStoId());
}
//确认数量
public function selobject()
{
$typeinfo=M('giftuser_type')->where('isshow',1)->order('lb_ordid asc')->select();
$this->assign('typeinfo',$typeinfo);
return $this->fetch('',getMobileStoId());
}
//分享礼物
public function sharethegift()
{
$return = M('wx_ewmlist')->where(array('store_id' => getMobileStoId(), 'scene_id' => ''))->field('ticket')->find();
if ($return){
$ticket=$return['ticket'];
}
$this->assign('ticket',$ticket);
return $this->fetch('',getMobileStoId());
}
//确认支付
public function sumbitpay()
{
$killarr=null;
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
/*--不能重复的一张表--*/
$userd = $this->user;
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));
/*--是否是购物车--*/
$tocart = I("tocart/d", 0);
$result = null;
if ($tocart == 0) {
if ($this->cartLogic->cart_count($this->user_id, 1) == 0)
$this->error('你的购物车没有选中商品', 'Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->cartList_cart2($this->user, $this->session_id, 1, 1); //获取购物车商品
} else {
$count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->count();
if ($count == 0)
$this->redirect('Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
}
/*--购物车商品分组--*/
$cart_goods = $result['cartList'];
$expty = 2;
foreach ($cart_goods as $kp => $vp) {
$exp2=0;
$cart_goods[$kp]['expty']=2;
}
$this->assign("expty", $expty);
$shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
->where("a.status=1 and a.store_id=" . getMobileStoId())->cache("shippingList_" . getMobileStoId(), TPSHOP_CACHE_TIME)
->select();//物流公司
/*--
if(I('cid') != ''){
$cid = I('cid');
$checkconpon = M('coupon')->field('id,name,money')->where("id = $cid")->find(); //要使用的优惠券
$checkconpon['lid'] = I('lid');
}--*/
/*----------购物车商品分组---------*/
$sql = null;
if ($tocart == 0) {
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id)";
} else if ($tocart == 1) {
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id)";
} else {
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=2 GROUP BY pick_id)";
}
//mlog('$tocart:'.$sql,'cart');
$plist_cart = Db::query($sql);
foreach ($plist_cart as $k => $v) {
$isallskill = 1;
foreach ($cart_goods as $kk => $vv) {if ($vv['pick_id'] == $v['pickup_id']) {$list[] = $vv; }
if ($vv['prom_type'] != 1){
$isallskill = 0;
}else{
$t=time();
$fl=M('flash_sale')->where('store_id',getMobileStoId())->where('goods_id',$vv['goods_id'])
->where('start_time<'.$t)->where('end_time>'.$t)->where('is_end',0)
->find();
if($fl) {
$killarr[] = ['goods_id' => $vv['goods_id'], 'fid' =>$fl['id'],'num'=>$vv['goods_num']];
}
}
}
if($killarr){
$this->assign("killarr", json_encode($killarr));
}
$plist_cart[$k]["goods"] = $list;
$plist_cart[$k]["expty"]=2;
// 获取订单商品总价,$tocart=0为购物车 1为立即购买
//$r = $this->cartLogic->my_calculate($list,$this->user_id,$tocart);
$r = $this->cartLogic->my_calculate_cart2($list, $this->user_id);
// 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
$fzquan = M('frozen_quan')->where('user_id', $this->user_id)->field('CashRepNo')->select();
$snow = date("Y-m-d", time());
//--如果所有商品不全是秒杀且没有优惠商品--
if($isallskill == 0) {
//$tk = M("store")->where("store_id", getMobileStoId())->field("api_token")->find();
//通过缓存拿tk
$tk = tpCache('shop_info.api_token', getMobileStoId());
//$user = M("users")->where("user_id",$this->user_id)->field("erpvipid")->find();
$where = "VIPId='" . $this->user['erpvipid'] . "'";
$where .= " and IsUse=0 and (ValidDate is null or ValidDate='' or ValidDate>='" . $snow . "')";
$where .= " and BuySum<=" . $r;
$where .= " and Sum<=" . $r;
$order['Sum'] = 'desc';
//$rs=getApiData("wxderp.cashreplace.list.get",$tk["api_token"],null,$where,1,1000,$order);
$rs = getApiData("wxderp.cashreplace.list.get", $tk, null, $where, 1, 1000, $order);
$qlistall = json_decode($rs, true);
if (!empty($qlistall)) {
if ($qlistall['data']) {
foreach ($qlistall['data'] as $k1 => $v1) {
$qlistall['data'][$k1]['Sum'] = number_format(empty($v1['Sum']) ? 0 : $v1['Sum'], 2);
$qlistall['data'][$k1]['BuySum'] = number_format(empty($v1['BuySum']) ? 0 : $v1['BuySum'], 2);
$qlistall['data'][$k1]['BillDate'] = date('Y-m-d', strtotime($v1['BillDate']));
if (empty($v1['ValidDate'])) {
$qlistall['data'][$k1]['ValidDate'] = "不限";
} else {
$qlistall['data'][$k1]['ValidDate'] = date('Y-m-d', strtotime($v1['ValidDate']));
}
foreach ($fzquan as $ku => $vu) {
if ($qlistall['data'][$k1]['CashRepNo'] == $vu['CashRepNo']) {
unset($qlistall['data'][$k1]);
break;
}
}
}
}
}
//$couponList = DB::query($sql);
$plist_cart[$k]["couponList"] = $qlistall['data'];
unset($qlistall);
}
unset($list);
}
$this->assign('slist', $plist_cart);
$this->assign('shippingList', $shippingList); // 物流公司
//$this->assign('cartList', $result['cartList']); // 购物车的商品
$this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购物车已选商品显示');
return $this->fetch('', getMobileStoId());
}
//领取提示
public function togroup()
{
//
$return = M('wx_ewmlist')->where(array('store_id' => getMobileStoId(), 'scene_id' => ''))->field('ticket')->find();
if ($return)
{
$ticket=$return['ticket'];
}
$this->assign('ticket',$ticket);
return $this->fetch('',getMobileStoId());
}
public function togiftcardfromgroup()
{
$ordno=I('ordno');
return $this->fetch('',getMobileStoId());
}
/**
* ajax 获取订单商品价格或者提交订单
*/
public function cart_gift2()
{
if(empty($this->redis)){
//$this->redis=new \Redis();
//$this->redis->connect(redisip, 6379);
$this->redis=get_redis_handle();
}
$popvalue=null;
$stoid = I("stoid");
if ($_REQUEST['act'] == 'submit_order') {
$sd=I('da');
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
//判断秒杀数量是否超限
$name='ms'.$lv['fid'].'-'.$stoid;
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$value = $this->redis->lPop($name);
if (empty($value)) {
if($popvalue){
foreach ($popvalue as $k=>$v)
$this->redis->lPush($name,$v);
}
return array('status' => -4, 'msg' => "秒杀商品已经抢光", 'result' => '');
}
$popvalue[]=$value;
}
}
}
}
/*--物流方式--*/
$exptype = I("exptype/d");
$state = I("state/d");
if ($this->user_id == 0)
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
$address_id = I("address_id/d"); // 收货地址id
$setadd = I("setadd/d");
$is_skill = I("skill"); //是否是秒杀
/*--是否是编辑地址--*/
if ($setadd == 1 && $exptype == 2 && empty($address_id)) {
$consignee = I("consignee");
$mobile = I("mobile");
$province = I("province");
$city = I("city");
$district = I("district");
$twon = I("twon");
$address = I("address");
$data = ['consignee' => $consignee,
'mobile' => $mobile,
'province' => $province,
'city' => $city,
'district' => $district,
'twon' => $twon,
'address' => $address,
];
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, $data);
$address_id = $data['result'];
}
$shipping_code = I("shipping_code/a"); // 物流编号
$invoice_title = I('invoice_title/a'); // 发票
$exparr= I('exparr/a');//获取物流配送数组
$couponTypeSelect = I("couponTypeSelect"); // 优惠券类型 1 下拉框选择优惠券 2 输入框输入优惠券代码
$coupon_id = I("coupon_id/a"); // 优惠券id
$couponCode = I("couponCode"); // 优惠券代码
$user_note = I('user_note'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
//送礼神器关键字段
$kl_man_num=I('kl_man_num');
$kl_num=I('kl_num');
$lb_type=I('lb_type');
$exptype=0;
foreach ($exparr as $ko=>$vo){
if($vo==1){ $exptype=1; break;}
}
if($user_money<0)
exit(json_encode(array('status' => -2, 'msg' => '你的余额不足', 'result' => null))); //返回结果状态
$result = null;
if ($state == 0) {
$result = $this->cartLogic->cartList($this->user, $this->session_id, 1, 1); //获取购物车商品
} else {
$result = $this->cartLogic->gobuyList($this->user, $this->session_id); //获取购物车商品
}
$cart_goods = $result['cartList'];
if (empty($cart_goods))
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
$address = M('UserAddress')->where("address_id", $address_id)->find();
$allusrnum = $user_money;
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$userd = $this->user;
$salerule = tpCache('basic.sales_rules', $stoid);
$erpid = tpCache('shop_info.ERPId', $stoid);
$userd['sales_rules'] = $salerule;
$userd['ERPId'] = $erpid;
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
$count_arr = null;
/*----如果是线下库存,获取线下库存----*/
if ($userd['sales_rules'] == 2) {
if ($is_skill != 1) {
$count_arr = getcountarr($cart_goods, $stoid);
}
}
$err_txt = "";
$prom_goods = array();
//建立优惠促销数组
foreach ($cart_goods as $k => $value) {
if ($value['prom_type'] == 3 && $value['is_gift'] == 0 && $value['is_collocation'] == 0) {
$prom_goods[$value['pick_id']][$value['prom_id']]['num'] += $value['goods_num'];
$prom_goods[$value['pick_id']][$value['prom_id']]['price'] += $value['goods_num'] * $value['goods_price'];
}
if ($value['is_gift'] == 1) {
$prom_goods[$value['pick_id']][$value['prom_id']]['key'] = $k;
}
}
if (!empty($prom_goods)) {
$prom_info = M('prom_goods')->where(['start_time' => ['lt', time()], 'end_time' => ['gt', time()], 'is_end' => 0])->getfield('id,name');
foreach ($prom_goods as $kk => $vv) {
foreach ($vv as $key => $val) {
if (!empty($prom_info[$key])) {
$prom = discount($val['price'], $key, $val['num'], $this->user_id);
if (empty($prom['goods_id']) && !empty($val['key'])) {
unset($cart_goods[$val['key']]);
}
$prom_goods[$kk][$key]['int'] = $prom['int'];
$prom_goods[$kk][$key]['coupon'] = $prom['coupon_id'];
$prom_goods[$kk][$key]['is_past'] = $prom['is_past'];
$prom_goods[$kk][$key]['gift_id'] = $prom['gift_id'];
$prom_goods[$kk][$key]['price'] = $prom['price'];
$prom_goods[$kk][$key]['all_price'] = $val['price'];
} else {
return array('status' => -4, 'msg' => "优惠活动已经结束", 'result' => '');
}
}
}
}
/*--预出库库存--*/
$gidarr=get_arr_column($cart_goods,'goods_id');
$fgid0=implode(',',$gidarr);
$strq = 'select pickup_id,goods_id,sum(out_qty) as sum0 from wxd_erp_yqty where goods_id in(' . $fgid0 . ') and store_id='.$stoid.' GROUP BY pickup_id,goods_id';
$dd = Db::query($strq);
$ddd = [];
//---组装数组---
if ($dd){
foreach ($dd as $ku => $vu) {
$ypkid=$vu['pickup_id'];
$ypgid=$vu['goods_id'];
$ddd[$ypkid.".".$ypgid]=$vu['sum0'];
}
}
/*--库存判断和活动判断,商品的库存和活动不能过期--*/
foreach ($cart_goods as $k => $value) {
$ginfo = $value;
/*--线下库存的序列key--*/
$bkey = $value['pick_id'] . $value['goods_id'];
//商品参与活动查看
if ($value['prom_type'] > 0 && $value['prom_type'] != 3 && $value['is_collocation'] == 0) {
/*--修改获取活动的函数--*/
$prom = get_goods_promotion2($ginfo, null, $this->user_id);
if ($prom['is_end'] == 1) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => '');
}
if ($prom['is_end'] == 2) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => '');
}
if ($prom['is_end'] == 3) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
if ($value['goods_num'] > $prom['buy_limit']) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
}
}
if ($value['member_goods_price'] != $prom['price']) {
$err_txt .= "{$ginfo['goods_name']}商品价格已经变化,是否继续购买!
";
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
if ($value['goods_num'] > $ginfo['store_count']) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else {
if ($userd['sales_rules'] == 1) {
if ($value['goods_num'] > $ginfo['store_count']) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else if ($userd['sales_rules'] == 2) {
if (empty($count_arr)) {
return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
}
/*--预出库判断--*/
$ynum=$ddd[$bkey];
if($ynum) {
if ($value['goods_num']+$ynum > $count_arr[$bkey]) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}else{
if ($value['goods_num'] > $count_arr[$bkey]) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
}
}
} else {
if ($value['is_gift'] == 0) {
if ($userd['sales_rules'] == 1) {
if ($value['goods_num'] > $ginfo['store_count']) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else if ($userd['sales_rules'] == 2) {
if (empty($count_arr)) {
return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
}
if ($value['goods_num'] > $count_arr[$bkey]) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
/*--判断价格是否发生变化 并且不是搭配商品--*/
if ($value['member_goods_price'] != $ginfo['shop_price'] && $value['is_collocation'] == 0) {
$err_txt .= "{$ginfo['goods_name']} 商品价格已经变化
";
}
}
}
}
if ($err_txt != "") {
return array('status' => -1004, 'msg' => $err_txt, 'result' => '');
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
if ($state == 0) {
$sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
$plist = Db::query($sql);
foreach ($plist as $k => $v) {
$count = array();
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
if (!empty($prom_goods[$v['pick_id']]) && $vv['prom_type'] == 3 && $count[$vv['prom_id']] == 0) {
$plist[$k]['order']['int'] += $prom_goods[$v['pick_id']][$vv['prom_id']]['int'];
$plist[$k]['order']['coupon'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
$plist[$k]['order'][$vv['prom_id']]['gift_id'] = $prom_goods[$vv['pick_id']][$vv['prom_id']]['gift_id'];
$plist[$k]['discount'] = $prom_goods[$v['pick_id']];
$count[$vv['prom_id']] += 1;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
} else {
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id";
$plist = Db::query($sql1);
foreach ($plist as $k => $v) {
$count = array();
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
if (!empty($prom_goods[$v['pick_id']]) && $vv['prom_type'] == 3 && $count[$vv['prom_id']] == 0) {
$plist[$k]['order']['int'] += $prom_goods[$v['pick_id']][$vv['prom_id']]['int'];
$plist[$k]['order']['coupon'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
$plist[$k]['order'][$vv['prom_id']]['gift_id'] = $prom_goods[$vv['pick_id']][$vv['prom_id']]['gift_id'];
$plist[$k]['discount'] = $prom_goods[$v['pick_id']];
$count[$vv['prom_id']] += 1;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
}
/*--开始生成多单--*/
$pno = "";
if (count($plist) > 1) $pno = 'g_'.$userd['ERPId'] . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
$postFee = 0;$couponFee = 0;$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_amount = 0;
//启动事务
Db::startTrans();
try {
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$result = calculate_price4($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']], $v['discount']);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = round($user_money - $result['result']['user_money'], 2);
/*----
$order_prom = get_order_promotion($result['result']['order_amount'],$stoid);
$result['result']['order_amount'] = $order_prom['order_amount'];
$result['result']['order_prom_id'] = $order_prom['order_prom_id'];
$result['result']['order_prom_amount'] = $order_prom['order_prom_amount'];
---*/
$postFee += $result['result']['shipping_price']; // 物流费
$couponFee += $result['result']['coupon_price'];// 优惠券
$balance += $result['result']['user_money']; // 使用用户余额
$pointsFee += $result['result']['integral_money']; // 积分支付
$payables += $result['result']['order_amount'];// 应付金额
$payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
$goodsFee += $result['result']['goods_price'];// 商品价格
$order_prom_id .= $result['result']['order_prom_id'] . ",";// 订单优惠活动id
$order_prom_amount += $result['result']['order_prom_amount'] + $result['result']['discount'];// 订单优惠活动优惠了多少钱
// 提交订单
if ($_REQUEST['act'] == 'submit_order') {
if (!$shipping_code && $exptype == 0) exit(json_encode(array('status' => -4, 'msg' => '请选择物流信息', 'result' => null))); // 返回结果状态
if (empty($coupon_id) && !empty($couponCode)) {
$coupon_id = M('CouponList')->where("code", $couponCode)->getField('id');
}
//$iii=$result['result']['user_money'];
$car_price0 = array(
'postFee' => $result['result']['shipping_price'], // 物流费
'couponFee' => $result['result']['coupon_price'], // 优惠券
'balance' => $result['result']['user_money'], // 使用用户余额
'pointsFee' => $result['result']['integral_money'], // 积分支付
'payables' => $result['result']['order_amount'], // 应付金额
'payables1' => $result['result']['order_amount1'], // 应付金额
'goodsFee' => $result['result']['goods_price'],// 商品价格
'order_prom_id' => $result['result']['order_prom_id'], // 订单优惠活动id
'order_prom_amount' => $result['result']['order_prom_amount'], // 订单优惠活动优惠了多少钱
'discount_amount' => $result['result']['discount'],//优惠、搭配促销优惠的金额
);
/*--调用生成订单的函数--*/
$result = $this->cartLogic->addOrder2($this->user_id, $address_id,
$shipping_code[$v['pick_id']], $invoice_title[$v['pick_id']], $car_price0,
$v["goods"], $coupon_id[$v['pick_id']], $user_note[$v['pick_id']], $stoid, 0, $pno, $v['pick_id'], $v['order'], $this->user,$kl_man_num,$kl_num,$lb_type,$state ); // 添加订单
if($result["status"] != 1) {
//如果失败就补回队列
$sd=I('da');
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
$name='ms'.$lv['fid'].'-'.$stoid;
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
}
}
//如果订单失败要还原数量
exit(json_encode($result));
}
if (empty($pno) && count($plist) == 1) {
$pno = $result["result"];
}
}
}
//提交订单
if ($_REQUEST['act'] == 'submit_order') {
upload_ylp_log('提交订单');
if ($state == 0) {
// 删除购物车中已提交订单商品
M('Cart')->where(['user_id' => $this->user_id, 'selected' => 1])->delete();
//删除购物车的cookie
Cookie::delete("cn");
Db::commit();
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
} else {
// 删除cart表中立即购买的商品
M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->delete();
Db::commit();
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
}
}
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
if ($_REQUEST['act'] == 'submit_order') {
//如果失败就补回队列
$sd=I('da');
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
$name='ms'.$lv['fid'].'-'.$stoid;
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
}
}
}
exit(json_encode(array('status' => -1, 'msg' => '提交订单失败')));
}
$car_price = array(
'postFee' => round($postFee, 2), // 物流费
'couponFee' => round($couponFee, 2), // 优惠券
'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
'pointsFee' => round($pointsFee, 2), // 积分支付
'payables' => round($payables, 2), // 应付金额
'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
'goodsFee' => round($goodsFee, 2),// 商品价格
'order_prom_id' => $order_prom_id, // 订单优惠活动id
'order_prom_amount' => round($order_prom_amount, 2), // 订单优惠活动优惠了多少钱
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/**
* ajax 获取订单商品价格
*/
public function cart_gift2_nosub()
{
/*--物流方式--*/
$exptype = I("exptype/d");
$stoid = I("stoid");
$state = I("state/d");
if ($this->user_id == 0)
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
$address_id = I("address_id/d"); //收货地址id
$setadd = I("setadd/d");
$is_skill = I("skill"); //是否是秒杀
$exparr= I('exparr/a');//获取物流配送数组
/*--是否是编辑地址--*/
if ($setadd == 1 && $exptype == 0 && empty($address_id)) {
$consignee = I("consignee");
$mobile = I("mobile");
$province = I("province");
$city = I("city");
$district = I("district");
$twon = I("twon");
$address = I("address");
$data = ['consignee' => $consignee,
'mobile' => $mobile,
'province' => $province,
'city' => $city,
'district' => $district,
'twon' => $twon,
'address' => $address,
];
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, $data);
$address_id = $data['result'];
}
$shipping_code = I("shipping_code/a"); // 物流编号
$invoice_title = I('invoice_title/a'); // 发票
$couponTypeSelect = I("couponTypeSelect"); // 优惠券类型 1 下拉框选择优惠券 2 输入框输入优惠券代码
$coupon_id = I("coupon_id/a"); // 优惠券id
$couponCode = I("couponCode"); // 优惠券代码
$user_note = I('user_note/a'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
//if(!empty($user_money))
//$user_money=number_format($user_money,2); //使用俩位
$result = null;
if ($state == 0) {
$result = $this->cartLogic->cartList_cart2($this->user, $this->session_id, 1, 1); //获取购物车商品
} else {
$result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
}
$cart_goods = $result['cartList'];
if (empty($cart_goods))
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
$address = M('UserAddress')->where("address_id", $address_id)->find();
$allusrnum = $user_money;
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$userd = $this->user;
$salerule = tpCache('basic.sales_rules', $stoid);
$erpid = tpCache('shop_info.ERPId', $stoid);
$userd['sales_rules'] = $salerule;
$userd['ERPId'] = $erpid;
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
/*----如果是线下库存,获取线下库存----*/
$count_arr = null;
if ($userd['sales_rules'] == 2) {
if ($is_skill != 1)
$count_arr = getcountarr($cart_goods, $stoid);
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
if ($state == 0) {
$sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
$plist = Db::query($sql);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
} else {
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id";
$plist = Db::query($sql1);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
}
/*--开始生成多单--*/
//$uerp=M('store')->where('store_id',$stoid)->find();
$pno = "";
if (count($plist) > 1) $pno = $userd['ERPId'] . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
$postFee = 0;$couponFee = 0;$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_amount = 0;
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$result = calculate_price4($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']],$v['discount']);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = round($user_money - $result['result']['user_money'], 2);
$postFee += $result['result']['shipping_price']; // 物流费
$couponFee += $result['result']['coupon_price'];// 优惠券
$balance += $result['result']['user_money']; // 使用用户余额
$pointsFee += $result['result']['integral_money']; // 积分支付
$payables += $result['result']['order_amount'];// 应付金额
$payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
$goodsFee += $result['result']['goods_price'];// 商品价格
$order_prom_id .= $result['result']['order_prom_id'] . ",";// 订单优惠活动id
$order_prom_amount += $result['result']['order_prom_amount'] + $result['result']['discount'];;// 订单优惠加优惠促销活动,优惠了多少钱
}
$car_price = array(
'postFee' => round($postFee, 2), // 物流费
'couponFee' => round($couponFee, 2), // 优惠券
'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
'pointsFee' => round($pointsFee, 2), // 积分支付
'payables' => round($payables, 2), // 应付金额
'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
'goodsFee' => round($goodsFee, 2),// 商品价格
'order_prom_id' => $order_prom_id, // 订单优惠活动id
'order_prom_amount' => round($order_prom_amount, 2), // 订单优惠活动优惠了多少钱
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
//去赠送
public function to_send(){
//判断赠送单人,赠送多人
$ordno=I('ordno');
$rs=M("giftuser_order")->where('order_sn',$ordno)->find();
$btn_text="";
$share_text="";
$fenx_url="/index.php/Mobile/Giftuser/togiftcardfromgroup/stoid/1/ordno/"+$ordno;
if($rs){
$user=M("users")->where('user_id',$rs['user_id'])->find();
$good=M("giftuser_order_goods")->where('order_sn',$ordno)->select();
$goodname="";
foreach ($good as $k=>$v){
$goodname.='【'.$v['goods_name'].'】'.$v['goods_num'].'份 ';
}
if($rs['kl_man_num']>1){
$btn_text="多人群抢";
$share_text=$user['nickname']."送大家".$goodname;
$fenx_url.="/state/more";
}else{
$fenx_url.="/state/one";
$btn_text="赠送单人";
$share_text=$user['nickname']."送您".$goodname;;
}
}
$this->assign("btn_text",$btn_text);
$this->assign("fenx_url",$fenx_url);
$this->assign("share_text",$share_text);
return $this->fetch('',getMobileStoId());
}
//送礼成功
public function send_success(){
return $this->fetch('',getMobileStoId());
}
}