cartLogic = new \app\home\logic\CartLogic();
if (session('?user')) {
$nuser=array(
'cart3', 'cart3_nosub','cart6_nosub','cart6'
);
if (!in_array(ACTION_NAME, $nuser)) {
$user = session('user');
$user = M('users')->where("user_id", $user['user_id'])->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
}else{
$user = session('user');
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
}
$nologin = array(
'getNum', 'addskill', 'cart2skill', 'del_address','cart2','ajaxAddCart',
'get_area', 'edit_address', 'add_address', 'cart3', 'reg_user','cart3_nosub','cart6_nosub',
'cart5','cart6','addwx_address'
);
if ($this->user_id == 0 && ACTION_NAME!='getNum') {
$stoid=getMobileStoId();
if($stoid==1){
$this->user=['user_id'=>982127];
$this->user_id=982127;
}
else
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId(), 'oldurl' => urlencode(curHostURL() . $_SERVER['']))));
}
if (!in_array(ACTION_NAME, $nologin)) {
if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
}
} else {
$uid = Cookie::get('user_id');
if ($uid) {
$user = M('users')->where("user_id", $uid)->where("store_id", getMobileStoId())->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
} else {
$stoid = getMobileStoId();
if ($stoid == 1) {
$this->user = ['user_id' => 982127];
$this->user_id = 982127;
}
}
}
$this->assign('pstore_id', getMobileStoId());
/*----
$getylpres=getylapp_res(getMobileStoId(),'2');
if ($getylpres)
{
$this->assign('ylpres',$getylpres);
}-----*/
}
public function cart()
{
$stoid=getMobileStoId();
//判断一下有没有秒杀
$t=time();
$f_list=M("flash_sale")->where("store_id",$stoid)->where("is_end",0)
->where('show_time<='.$t." and end_time>=".$t)->select();
if($f_list)
$this->assign('f_list', $f_list);
$t=time();
//获取热卖商品
$hot_goods = M('Goods')
->where("on_time<".$t." and (down_time>".$t." or down_time=0 or down_time='' or down_time is null)")
->where('is_hot=1 and is_on_sale=1 ' . getMobileStoWhere())->limit(20)->select();
$this->assign('hot_goods', $hot_goods);
return $this->fetch('cart', getMobileStoId());
}
/**
* 将商品加入购物车
*/
function addCart()
{
$goods_id = I("goods_id/d"); // 商品id
$goods_num = I("goods_num/d");// 商品数量
$goods_spec = I("goods_spec"); // 商品规格
$goods_spec = json_decode($goods_spec, true); //app 端 json 形式传输过来
$unique_id = I("unique_id"); // 唯一id 类似于 pc 端的session id
$user_id = I('user_id/d', 0); // 用户id
if (empty($this->user['erpvipid']))
return array('status' => -101, 'msg' => '购买商品必须先绑定线下会员', 'result' => '');
$result = $this->cartLogic->addCart($goods_id, $goods_num, $goods_spec, $unique_id, $user_id); // 将商品加入购物车
upload_ylp_log('商品加入购物车');
exit(json_encode($result));
}
/**
* ajax 将商品加入购物车
*/
function ajaxAddCart()
{
$goods_id = I("goods_id/d"); // 商品id
$goods_num = I("goods_num/d");// 商品数量
$goods_spec = I("goods_spec/a", array()); // 商品规格
$pick_up = I("pickup_id/d"); //获取门店id
$to_catr = I("to_catr/d");//1:立即购买
if($to_catr==3) $to_catr=1;
$goods_list = I('goods_list/s');//搭配有值,其他的值是underfined
$isinte=I('isinte/d',0);//积分商品普通购买
$ispd=I('ispd/d',0);//拼团商品普通购买
if ($this->user_id == 0) {
return array('status' => -101, 'msg' => '非法操作', 'result' => '');
}
$result = $this->cartLogic
->addCart($goods_id, $goods_num, $goods_spec, $this->session_id, $this->user, $pick_up, $to_catr, $goods_list,$isinte,$ispd); // 将商品加入购物车
upload_ylp_log('商品加入购物车');
exit(json_encode($result));
}
/**
* 秒杀ajax将商品加入购物车
*/
public function addskill()
{
$goods_id = I("goods_id/d"); // 商品id
$goods_num = I("goods_num/d");// 商品数量
$goods_spec = I("goods_spec/a", array()); // 商品规格
$pick_up = I("pickup_id/d"); //获取门店id
$to_catr = I("to_catr/d");//1:立即购买
if ($this->user_id == 0) {
return array('status' => -101, 'msg' => '非法操作', 'result' => '');
}
$result = $this->cartLogic
->addCart($goods_id, $goods_num, $goods_spec, $this->session_id, $this->user, $pick_up, $to_catr); // 将商品加入购物车
upload_ylp_log('商品加入购物车');
exit(json_encode($result));
}
/*
* 请求获取购物车列表
*/
public function cartList()
{
$cart_form_data = input('cart_form_data'); // goods_num 购物车商品数量
$cart_form_data = json_decode($cart_form_data, true); //app 端 json 形式传输过来
$unique_id = I("unique_id"); // 唯一id 类似于 pc 端的session id
$user_id = I("user_id/d"); // 用户id
$where['session_id'] = $unique_id; // 默认按照 $unique_id 查询
if ($user_id) {
$where['user_id'] = $user_id;
}
$cartList = M('Cart')->where($where)->getField("id,goods_num,selected,goods_id");
/*--要剔除下架商品,活动有过期的商品--*/
foreach ($cartList as $k => $v) {
$good = M('goods')
->where('goods_id', $v['goods_id'])
->where('is_on_sale=1')->find();
if ($good) {
if ($v['prom_type'] != 0) {
$prom = get_goods_promotion($v["goods_id"], $user_id);
if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $user_id)->delete();
unset($cartList[$k]);
}
}
} else {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $user_id)->delete();
unset($cartList[$k]);
}
}
if ($cart_form_data) {
// 修改购物车数量 和勾选状态
foreach ($cart_form_data as $key => $val) {
$data['goods_num'] = $val['goodsNum'];
$data['selected'] = $val['selected'];
$cartID = $val['cartID'];
if (($cartList[$cartID]['goods_num'] != $data['goods_num']) || ($cartList[$cartID]['selected'] != $data['selected']))
M('Cart')->where("id", $cartID)->save($data);
}
//$this->assign('select_all', $_POST['select_all']); // 全选框
}
$result = $this->cartLogic->cartList($this->user, $unique_id, 0);
upload_ylp_log('购物车列表');
exit(json_encode($result));
}
/**
* 购物车第二步确定页面
*/
public function cart2()
{
$killarr = null;
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
/*--不能重复的一张表--*/
//$userd=M("users")->where('user_id',$this->user_id)->find();
$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));
//20180717 season(增加短信随机码校验)
$storeconfig = M('store_config')->where('store_id', getMobileStoId())
->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
$sms_send_type = json_decode($storeconfig['sms_send_type'], true);
$this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
$this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
$this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);
$user_openid = $this->user['openid'];
//$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
$this->assign("user_openid", $user_openid);
$user_openidkey = md5($user_openid . "&" . getErpKey());
$this->assign("user_openid", $user_openid);
$this->assign("user_openidkey", $user_openidkey);
$is_pt=I("is_pt");
$this->assign("is_pt", $is_pt);
//---先查询是不是阶梯团----
if($is_pt!=3) {
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
}
/*--是否是购物车--*/
$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 = 0;
$prom_goods_arr=$this->com_prom_goods($cart_goods);
foreach ($cart_goods as $kp => $vp) {
$exp2 = 0;
if ($vp['distr_type'] != $vp['bdistr_type']) {
$exp2 = $vp['distr_type'] == 0 ? $vp['bdistr_type'] : $vp['distr_type'];
} else {
$exp2 = $vp['distr_type'];
}
$cart_goods[$kp]['expty'] = $exp2;
if ($vp['is_gift'] == 0) {
//判断最后的物流方式
if ($expty != 0) {
if ($expty != $exp2) {
if ($exp2 == 2 || $expty == 2) {
$expty = 2;
} else {
$expty = 0;
}
}
} else {
$expty = $exp2;
}
}
}
$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)";
}
$plist_cart = Db::query($sql);
foreach ($plist_cart as $k => $v) {
$isallskill = 1;
$expty00 = 0;
$geterpwareidlist="";//当前各个门店购买线下商品Id
$check_quan_price=0;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
mlog("获取cart信息:".json_encode($vv),"cart2/".getMobileStoId());
$list[] = $vv;
$expty11 = $vv['expty'];
if ($expty00 == 0) {
$expty00 = $expty11;
}
$fect_price=$vv['goods_price'];
$cut_price=0;
$goods_num=$vv['goods_num'];
//---优惠活动----
if($vv['prom_type']==3){
if($prom_goods_arr[$vv['pick_id']][$vv['prom_id']]['is_xz_yh']) continue;
if($prom_goods_arr[$vv['pick_id']][$vv['prom_id']]['prom_price']<=0) continue;
}
//----------团购是否可以使用优惠券-----
else if($vv['prom_type']==2){
$t = time();
$gb = M('group_buy')->where('store_id', getMobileStoId())
->where('id', $vv['prom_id'])
->where('start_time<' . $t)
->where('end_time>' . $t)->where('is_end', 0)
->field('is_quan')
->find();
if ($gb) {
if ($gb['is_quan'] ==0 ) continue;
}
}
//----秒杀商品----
else if($vv['prom_type']==1) {
$t = time();
$fl = M('flash_sale')->where('store_id', getMobileStoId())
->where('id', $vv['prom_id'])
->where('start_time<' . $t)->where('end_time>' . $t)->where('is_end', 0)
->field("id")
->find();
if ($fl) {
$killarr[] = ['goods_id' => $vv['goods_id'], 'fid' => $fl['id'], 'num' => $vv['goods_num']];
continue;
}
}
//season 2018-10-29 start
if ($geterpwareidlist)
{
$geterpwareidlist .= "," . $vv['erpwareid'];
}
else {
$geterpwareidlist .= $vv['erpwareid'];
}
//season 2018-10-29 end
//season 2018-10-29 end
//$member_goods_price = $vv["goods_price"];
//$check_quan_price += $vv['goods_num'] * $member_goods_price;
$cut_price=$prom_goods_arr[$vv['pick_id']][$vv['prom_id']]['goods'][$vv['goods_id']]['fect_price'];
if($cut_price)
$check_quan_price += $cut_price;
else
$check_quan_price += $vv['goods_num'] * $fect_price;
}
if ($vv['prom_type'] != 1) {
if ($vv['prom_type'] == 6) {
$isallskill = 1;
}
/*-------
else if($vv['prom_type'] == 2 && $isallskill==1){
$t = time();
$gb = M('group_buy')->where('store_id', getMobileStoId())
->where('goods_id', $vv['goods_id'])
->where('start_time<' . $t)
->where('end_time>' . $t)->where('is_end', 0)
->field('is_quan')
->find();
if ($gb) {
if ($gb['is_quan'] ==1 ) $isallskill = 0;
}
}---*/
else{
$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"] = $expty00;
// 获取订单商品总价,$tocart=0为购物车 1为立即购买
//$r = $this->cartLogic->my_calculate_cart2($list, $this->user_id);
unset($list);
// 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
$fzquan = M('frozen_quan')->where('user_id', $this->user_id)->field('CashRepNo')->select();
$snow = date("Y-m-d", time());
//是否关闭使用优惠券
$is_close_quan=tpCache("basic.is_close_quan",getMobileStoId());
$is_pt = I("is_pt");
//--如果所有商品不全是秒杀且没有优惠商品--
if ($isallskill == 0 && !$is_pt && !$is_close_quan) {
//要拿出包邮券的这一部分
//如果是注册的会员
$qlistall = "";
if ($this->user['erpvipid']) {
// $tk = tpCache('shop_info.api_token', getMobileStoId());
// $where = "VIPId='" . $this->user['erpvipid'] . "'";
// $where .= " and isnull(useObjectID,'')='' and IsUse=0 and (ValidDate is null or ValidDate='' or ValidDate>='" . $snow . "')";
// $where .= " and (BuySum<=" . $r." or BuySum=0 or BuySum is null)";
// $where .= " and Sum<=" . $r;
// $where .= " and (BeginDate='' or BeginDate is null or BeginDate<='" . $snow . "')";
// $order['Sum'] = 'desc';
// $rs = getApiData("wxderp.cashreplace.list.get", $tk, null, $where, 1, 1000, $order);
$accdb = tpCache('shop_info.ERPId', getMobileStoId());
//$data['BuySum']=$r;
$data['BuySum']=$check_quan_price;
$data['VIPId']=urlencode($this->user['erpvipid']);
$data['IsUse']=0;//0=未使用,1=已使用,2=过期
$data['isBeginDate']=1;
$data['WareIds']=urlencode($geterpwareidlist);
mlog("抵用券条件:".json_encode($data),"cart2/".getMobileStoId());
//$rs=getApiData_java("api/erp/vip/cash/list",$accdb,$data,1,1000);
$rs=getApiData_java("api/erp/vip/cash/page",$accdb,$data,1,1000);
mlog("抵用券0:" . json_encode($rs), "cart2/" . getMobileStoId());
if ($rs) {
mlog("抵用券:" . $geterpwareidlist . $rs, "cart2/" . getMobileStoId());
$qlistall = json_decode($rs, true);
if ($qlistall && $qlistall['code']==0) {
if(isset($qlistall['data']['pageData'])) {
$qlistall['data'] = $qlistall['data']['pageData'];
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['BeginDate']));
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;
}
}
}
}
}else{
$qlistall['data']=null;
}
}else{
$qlistall['data']=null;
}
$plist_cart[$k]["couponList"] = $qlistall['data'];
mlog("抵用券2:" .json_encode($plist_cart[$k]["couponList"]), "cart2/" . getMobileStoId());
unset($qlistall);
}
}
//通过缓存拿tk
$ERPId = $this->pm_erpid;
//如果是erp用户
if (empty($ERPId)) {
//非erp用户
$ti = time();
$r=$check_quan_price; //需要使用到券的商品
$qlistall = M('coupon_list')->where('store_id', getMobileStoId())
->where('uid', $this->user_id)->where('is_user', 0)
->where('begintime<' . $ti . ' and validtime>' . $ti)
->where('buysum<=' . $r . ' and sum<=' . $r)
->select();
if (!empty($qlistall)) {
foreach ($qlistall as $k1 => $v1) {
$qlistall[$k1]['CashRepNo']=$qlistall[$k1]['code'];
$qlistall[$k1]['Sum'] = number_format(empty($v1['sum']) ? 0 : $v1['sum'], 2);
$qlistall[$k1]['BuySum'] = number_format(empty($v1['buysum']) ? 0 : $v1['buysum'], 2);
$qlistall[$k1]['BillDate'] = date('Y-m-d', $v1['begintime']);
if (empty($v1['validtime'])) {
$qlistall[$k1]['ValidDate'] = "不限";
} else {
$qlistall[$k1]['ValidDate'] = date('Y-m-d', $v1['validtime']);
}
//冻结的券不能使用
foreach ($fzquan as $ku => $vu) {
if ($qlistall[$k1]['code'] == $vu['CashRepNo']) {
unset($qlistall[$k1]);
break;
}
}
}
}
$plist_cart[$k]["couponList"] = $qlistall;
unset($qlistall);
}
}
}
$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 cart2skill()
{
$this->assign("skill", 1);
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
/*--不能重复的一张表--*/
//$userd=M("users")->where('user_id',$this->user_id)->find();
$userd = $this->user;
/*--清理未支付订单--*/
clear_flash_Order(getMobileStoId());
$storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
$sms_send_type = json_decode($storeconfig['sms_send_type'], true);
$this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
$this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
$this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);
$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));
$user_openid = $this->user['openid'];
//$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
$this->assign("user_openid", $user_openid);
$user_openidkey = md5($user_openid . "&" . getErpKey());
$this->assign("user_openid", $user_openid);
$this->assign("user_openidkey", $user_openidkey);
mlog($user_openid."|".$user_openidkey,"cart2skill/".getMobileStoId());
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
/*--是否是购物车--*/
$tocart = I("tocart/d", 0);
$result = null;
$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 = 0;
$killarr=null;
foreach ($cart_goods as $kp => $vp) {
$exp2=0;
if($vp['distr_type']!=$vp['bdistr_type']){
$exp2=$vp['distr_type']==0?$vp['bdistr_type']:$vp['distr_type'];
}else{
$exp2=$vp['distr_type'];
}
$cart_goods[$kp]['expty']=$exp2;
if ($vp['is_gift'] == 0) {
//判断最后的物流方式
if ($expty != 0) {
if ($expty != $exp2) {
if($exp2==2 || $expty==2 ){
$expty=2;
}else{
$expty=0;
}
}
} else {
$expty =$exp2;
}
}
$t=time();
$fl=M('flash_sale')->where('store_id',getMobileStoId())->where('goods_id',$vp['goods_id'])
->where('start_time<'.$t)->where('end_time>'.$t)->where('is_end',0)
->find();
if($fl) {
$killarr[] = ['goods_id' => $vp['goods_id'], 'fid' =>$fl['id'],'num'=>$vp['goods_num']];
}
}
$this->assign("expty", $expty);
if($killarr)
$this->assign("killarr", json_encode($killarr));
$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();//物流公司
/*----------购物车商品分组---------*/
$sql = null;
$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)";
//mlog('$tocart:'.$sql,'cart');
$plist_cart = Db::query($sql);
foreach ($plist_cart as $k => $v) {
$expty00 = 0;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$list[] = $vv;
$expty11=$vv['expty'];
if($expty00==0){
$expty00=$expty11;
}
}
}
$plist_cart[$k]["goods"] = $list;
$plist_cart[$k]["expty"]=$expty00;
unset($list);
}
$this->assign('slist', $plist_cart);
$this->assign('shippingList', $shippingList); // 物流公司
$this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购物车已选商品显示');
return $this->fetch('cart2', getMobileStoId());
}
/**
* ajax 获取订单商品价格或者提交订单
*/
public function cart3()
{
if(empty($this->redis)){
//$this->redis=new \Redis();
//$this->redis->connect(redisip, 6379);
$this->redis=get_redis_handle();
}
$popvalue=null;
$popvalue2=null;
$stoid = I("stoid");
//拼单有关
$ispt=I('is_pt');
$pdid=I('pdid');
$team_qh=I('team_qh');
$pdgoodsnum=I('pdgoodsnum/d',0);
$g_promid=I('g_promid/a');
$no="";
$notime=microtime_float();
$rank_switch = tpCache('shopping.rank_switch', $stoid);//等级价格
$mz_switch = tpCache('shopping.is_beauty', $stoid);//美妆价格
$isinte=I("isinte",0);
//记录秒杀
$sd=null;
if ($_REQUEST['act'] == 'submit_order') {
$sd=I('da');
$no='r'.microtime_float().rand(10000, 99990);
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
//判断秒杀数量是否超限
//$name='ms'.$lv['fid'].'-'.$stoid;
$name=get_redis_name($lv['fid'],1,$stoid);
$num0=$lv['num'];
if($this->redis->lLen($name)<$num0){
return json(array('status' => -4, 'msg' => "秒杀商品已经抢光", 'result' => ''));
}
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$value = $this->redis->lPop($name);
$popvalue[]=$value;
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$f_name=get_redis_set_name($stoid);
$this->redis->sAdd($f_name,$key);
}
}
//如果是拼单
if($ispt){
//$no='r'.microtime_float().rand(10000, 99990);
//$name='pind'.$pdid.'-'.$stoid;
$name=get_redis_name($pdid,6,$stoid);
if($this->redis->lLen($name)<$pdgoodsnum){
return json(array('status' => -4, 'msg' => "您晚了一步,商品已被抢光!", 'result' => ''));
}
//拼单队列控制
for($i=0;$i<$pdgoodsnum;$i++) {
$value = $this->redis->lPop($name);
$popvalue[]=$value;
}
//如果,$ispt是会员团的时候,要判断团的缓存
if($team_qh && $ispt=='2'){
//$name2='pind'.$pdid.'-'.$team_qh.$stoid;
$name2=get_redis_name($pdid,6,$stoid).":". $team_qh;
$value = $this->redis->lPop($name2);
if (empty($value)) {
foreach ($popvalue as $k=>$v)
$this->redis->lPush($name,$v);
return array('status' => -4, 'msg' => "手慢了,您参与的团已满团!", 'result' => '');
}
$popvalue2[]=$value;
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$p_name=get_redis_set_name($stoid);
$this->redis->sAdd($p_name,$key);
}
//如果是团购的话,要进行组合
if($g_promid){
$arr=null;
foreach ($g_promid as $key=>$val){
$promid=explode("-",$key)[1];
$arr[$promid]+=$val;
}
foreach ($arr as $ky=>$ve){
//判断秒杀数量是否超限
//$name='grb'.$ky.'-'.$stoid;
$name=get_redis_name($ky,2,$stoid);
$num0=$ve;
if($this->redis->lLen($name)<$num0){
return json(array('status' => -4, 'msg' => "团购商品已经抢光", 'result' => ''));
}
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$value = $this->redis->lPop($name);
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
//$key=$no.'-2-'.$ky.'-'.$num0.'-'.$notime;
$g_name=get_redis_set_name($stoid);
$this->redis->sAdd($g_name,$key);
}
}
}
//return;
/*--物流方式--*/
$exptype = I("exptype/d");
$state = I("state/d");
if ($this->user_id == 0) {
$this->backredis($no,$notime);
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/a'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
$exptype=0;
if($exparr) {
foreach ($exparr as $ko => $vo) {
if ($vo == 1) {
$exptype = 1;
break;
}
}
}
if($user_money<0) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -2, 'msg' => '你的余额不足', 'result' => null))); //返回结果状态
}
//if(!empty($user_money))
//$user_money=number_format(empty($user_money)?0:$user_money,2); //使用俩位
/*--购物车商品提取--*/
/*--
$cart_goods = M('Cart')->alias('a')
->join("pick_up b","a.pick_id=b.pickup_id",'left')
->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
/*---购物车和立即购买的区分---*/
$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'];
mlog("购物车出来的数量:".json_encode($cart_goods),"cart3/".$stoid);
if (empty($cart_goods)) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
}
/*------
$wlist="";$sumlist="";
//----计算商品组,用于优惠券----
foreach ($cart_goods as $k=>$v){
if($v['is_gift']!=1) {
$wlist .= $v['erpwareid'] .",";
$sumlist .= ($v['goods_price'] * $v['goods_num']) . ",";
}
}
$wlist = substr($wlist,0,strlen($wlist)-1);
$sumlist = substr($sumlist,0,strlen($sumlist)-1);--*/
$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;
// M("users")->alias('a')
// ->join('store_config b',' a.store_id=b.store_id','left')
// ->join('store c',' a.store_id=c.store_id','left')
// ->field('a.user_money,a.frozen_money,b.sales_rules,c.ERPId')
// ->where('a.user_id',$this->user_id)->find();
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
$this->backredis($no,$notime);
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
// $srule=M('store_config')->where('store_id',$stoid)->find();
// $uerp=M('store')->where('store_id',$stoid)->find();
$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])
->where('store_id',$stoid)
->getfield('id,name,is_bz');
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,$prom_info[$key]['is_bz']);
if (empty($prom['goods_id']) && !empty($val['key'])) {
//不是赠品的才删除,赠品要留着
if($cart_goods[$val['key']['is_gift']]!=1)
unset($cart_goods[$val['key']]);
}
$prom_goods[$kk][$key]['int'] = $prom['int'];
$prom_goods[$kk][$key]['coupon'] = $prom['coupon_id'];
$prom_goods[$kk][$key]['libao'] = $prom['libao'];
$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'];
$prom_goods[$kk][$key]['is_bz'] = $prom['is_bz'];
$prom_goods[$kk][$key]['coupon_num'] = $prom['coupon_num'];
$prom_goods[$kk][$key]['lb_num'] = $prom['lb_num'];
} else {
//return array('status' => -4, 'msg' => "优惠活动已经结束", 'result' => '');
unset($prom_goods[$kk][$key]);
}
}
}
}
$prom_goods_arr=$this->com_prom_goods($cart_goods);
/*--预出库库存--*/
$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'];
}
}
mlog("预出数量0:".json_encode($ddd),"cart3/".$stoid);
mlog("购物车出来的数量001:".json_encode($cart_goods),"cart3/".$stoid);
/*--库存判断和活动判断,商品的库存和活动不能过期--*/
foreach ($cart_goods as $k => $value) {
//活动不一样就是发生了变化
if($value['prom_type1']!=$value['prom_type'] && $value['prom_id1']!=$value['prom_id'] && $value['is_collocation'] == 0 ) {
$this->backredis($no,$notime);
return array('status' => -3, 'msg' => "{$value['goods_name']}商品价格已经变化", 'result' => '');
}
$ginfo = $value;
/*--线下库存的序列key--*/
$bkey = $value['pick_id'] . $value['goods_id'];
//商品参与活动查看
if ($value['prom_type1'] > 0 && $value['prom_type1'] != 3 && $value['prom_type1'] != 5 && $value['is_collocation'] == 0 && $value['is_integral_normal']!=1 && $value['is_pd_normal']!=1 ) {
/*--修改获取活动的函数--*/
$prom = get_goods_promotion2($ginfo, null, $this->user_id,0,'',$this->user);
if ($prom['is_end'] == 1) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => '');
}
if ($prom['is_end'] == 2) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => '');
}
if ($prom['is_end'] == 3) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
if ($prom['buy_limit']) {
if ($value['goods_num'] > $prom['buy_limit']) {
$this->backredis($no, $notime);
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 || $value['prom_type'] == 6) {
if ($value['goods_num'] > $ginfo['store_count']) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else {
if ($userd['sales_rules'] == 1) {
if ($value['goods_num'] > $ginfo['store_count']) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else if ($userd['sales_rules'] == 2) {
if (empty($count_arr)) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
}
/*--预出库判断--*/
$ynum=$ddd[$bkey];
mlog("预出数量:".$ynum,"cart3/".$stoid);
if($ynum) {
if ($value['goods_num']+$ynum > $count_arr[$bkey]) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}else{
if ($value['goods_num'] > $count_arr[$bkey]) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
}
}
}else{
//判断商品超出限购
if($ginfo['viplimited']>0) {
$limit = M('order')->alias('a')->join('order_goods b', 'a.order_id=b.order_id')
->where([ 'a.user_id' => $this->user_id, 'b.goods_id' => $ginfo['goods_id'],'a.order_status' => [['=', 4], ['<', 3], 'or']])
->sum('b.goods_num');
if ($value['goods_num'] + $limit > $ginfo['viplimited']) {
$this->backredis($no,$notime);
return array('status' => -3, 'msg' => '购买商品超出限购', 'result' => '');
}
}
if ($value['is_gift']== 0) {
if ($userd['sales_rules'] == 1) {
if ($value['goods_num'] > $ginfo['store_count']) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else if ($userd['sales_rules'] == 2) {
if (empty($count_arr)) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
}
/*--预出库判断--*/
$ynum=$ddd[$bkey];
$this_gd_count=$count_arr[$bkey];
if($ynum) {
$this_gd_count=$this_gd_count-$ynum;
}
mlog("预出数量55:".$this_gd_count,"cart3/".$stoid);
if ($value['goods_num'] > $this_gd_count) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
$mz_switch = tpCache('shopping.is_beauty',$stoid);//美妆开关
$rank_switch = tpCache('shopping.rank_switch',$stoid);//等级开关
$curprice=$ginfo['shop_price'];
//如果有美妆价和启用,大于0
if ($ginfo['mz_price'] > 0 && !empty($userd['is_mzvip']) && !empty($mz_switch)) {
$curprice = $ginfo['mz_price'];
}
//如果有等级价和启用,大于0
if ($rank_switch) {
if ($userd['card_field'] && strtotime($userd['card_expiredate']) > time() && $ginfo[$userd['card_field']] > 0) {
$curprice = $ginfo[$userd['card_field']];
}
}
/*--判断价格是否发生变化 并且不是搭配商品--*/
if ($value['member_goods_price'] != $curprice && $value['is_collocation'] == 0
//&& $value['prom_type1'] != 3
//&& $value['is_integral_normal']!=1 && $value['is_pd_normal']!=1)
){
$err_txt .= "{$ginfo['goods_name']} 商品价格已经变化
";
}
}
}
}
//$err_txt="跳出";
if ($err_txt != "") {
$this->backredis($no,$notime);
return array('status' => -1004, 'msg' => $err_txt, 'result' => '');
}
mlog("购物车出来的数量002:".json_encode($cart_goods),"cart3/".$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) {
$count = array();
$wlist="";$sumlist="";
$list=[];
foreach ($cart_goods as $kk => $vv) {
//组装分配券的商品ID 和 金钱
$t_arr= $this->com_package($vv,$v,$wlist,$sumlist,$prom_goods_arr);
if($t_arr['list']) $list[]=$t_arr['list'];
$wlist=$t_arr['wlist'];
$sumlist=$t_arr['sumlist'];
mlog("cart3计算优惠的内容0:".json_encode($t_arr),"cart3/".$stoid);
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']['libao'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['libao'];
//如果有券对应的数量
if($prom_goods[$v['pick_id']][$vv['prom_id']]['coupon_num']) {
$c_dd['num'] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon_num'];
$c_dd['c_id'] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
$plist[$k]['order']['coupon_num'][] = $c_dd;
}
//如果有券对应的数量
if($prom_goods[$v['pick_id']][$vv['prom_id']]['coupon_num']) {
$l_dd['num']=$prom_goods[$v['pick_id']][$vv['prom_id']]['lb_num'];
$l_dd['l_id']=$prom_goods[$v['pick_id']][$vv['prom_id']]['libao'];
$plist[$k]['order']['lb_num'][] =$l_dd ;
}
$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;
//组装商品ID和金额
$wlist = substr($wlist,0,strlen($wlist)-1);
$sumlist = substr($sumlist,0,strlen($sumlist)-1);
$plist[$k]["wlist"]=$wlist;
$plist[$k]["sumlist"]=$sumlist;
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();
$wlist="";$sumlist="";$list=[];
foreach ($cart_goods as $kk => $vv) {
//组装分配券的商品ID 和 金钱
$t_arr= $this->com_package($vv,$v,$wlist,$sumlist,$prom_goods_arr);
if($t_arr['list']) $list[]=$t_arr['list'];
$wlist=$t_arr['wlist'];
$sumlist=$t_arr['sumlist'];
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']['libao'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['libao'];
//如果有券对应的数量
if($prom_goods[$v['pick_id']][$vv['prom_id']]['coupon_num']) {
$c_dd['num'] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon_num'];
$c_dd['c_id'] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
$plist[$k]['order']['coupon_num'][] = $c_dd;
}
//如果有券对应的数量
if($prom_goods[$v['pick_id']][$vv['prom_id']]['lb_num']) {
$l_dd['num']=$prom_goods[$v['pick_id']][$vv['prom_id']]['lb_num'];
$l_dd['l_id']=$prom_goods[$v['pick_id']][$vv['prom_id']]['libao'];
$plist[$k]['order']['lb_num'][] =$l_dd ;
}
$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;
}
if($plist[$k]['order']) mlog(json_encode($plist[$k]['order']).$k,"cart3_yhui/".$stoid);
mlog("cart3计算优惠的内容:".json_encode($t_arr),"cart3/".$stoid);
}
$plist[$k]["goods"] = $list;
//组装商品ID和金额
$wlist = substr($wlist,0,strlen($wlist)-1);
$sumlist = substr($sumlist,0,strlen($sumlist)-1);
$plist[$k]["wlist"]=$wlist;
$plist[$k]["sumlist"]=$sumlist;
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;
$youhui_arr=null;
//---拼单只有一个商品---
if($ispt==3) {
$exptype = 1;$exparr[$plist[0]['pick_id']]=1;
}
mlog("plist".json_encode($plist),"cart3/".$stoid);
//启动事务
Db::startTrans();
try {
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$result = calculate_price2($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']], $v['discount'],$v['wlist'],$v['sumlist']);
if ($result['status'] < 0) {
$this->backredis($no,$notime);
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'];// 订单优惠活动优惠了多少钱
$youhui_arr = $result['result']['youhui'];
mlog("使用优惠券:".$coupon_id[$v['pick_id']].json_encode($youhui_arr),
"cart3/".$stoid);
// 提交订单
if ($_REQUEST['act'] == 'submit_order') {
$address_id = $_REQUEST['address_id'];
mlog("日志" . $address_id . "是否编辑地址" . $setadd, "cart");
if (!$address_id && $exptype == 0) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -3, 'msg' => '请先填写收货人信息', 'result' => null)));
} // 返回结果状态
if (!$shipping_code && $exptype == 0){
$this->backredis($no,$notime);
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'],//优惠、搭配促销优惠的金额
'good_prom'=>$result['result']['good_prom'],//优惠活动
);
mlog("addOrder".json_encode($v["goods"]),"cart3/".$stoid);
/*--调用生成订单的函数--*/
$result = $this->cartLogic->addOrder($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, $exparr[$v['pick_id']], $pno, $v['pick_id'], $v['order'], $this->user,$no,$ispt,$pdid,$team_qh,$youhui_arr); // 添加订单
if($result["status"] != 1) {
//如果失败就补回队列
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
//$name='ms'.$lv['fid'].'-'.$stoid;
$name=get_redis_name($lv['fid'],1,$stoid);
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
//如果是拼单
if($ispt){
//$name='pind'.$pdid.'-'.$stoid;
$name=get_redis_name($pdid,6,$stoid);
//拼单队列控制
for($i=0;$i<$pdgoodsnum;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
if($team_qh && $ispt=='2') {
//$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$name2 =get_redis_name($pdid,6,$stoid) . ':' . $team_qh;
$this->redis->lPush($name2,time().'_0');
}
//$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
if($g_promid){
$arr=null;
foreach ($g_promid as $key=>$val){
$promid=explode("-",$key)[1];
$arr[$promid]+=$val;
}
foreach ($arr as $ky=>$ve){
//判断秒杀数量是否超限
//$name='grb'.$ky.'-'.$stoid;
$name=get_redis_name($ky,2,$stoid);
$num0=$ve;
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time() . '_' . $i);
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-2-'.$ky.'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
//如果订单失败要还原数量
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();
//正确提交订单记录redis数量
if(!empty($sd)) {
$skillarray = json_decode($sd, true);
foreach ($skillarray as $lk => $lv) {
//判断秒杀数量是否超限
$num0 = $lv['num'];
//$name='ms'.$lv['fid'].'-'.$stoid;
$name=get_redis_name($lv['fid'],1,$stoid);
$jsda['len']=$this->redis->lLen($name);
$jsda['num']=-$num0;
$jsda['order_sn']=$pno;
$jsda['type']=0; //0是买单 1是5分钟自动补回 2是手动补回
mlog(json_encode($jsda),'flash_log/' . $stoid . '/' . $lv['fid']."_1");
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
if($g_promid){
$arr=null;
foreach ($g_promid as $key=>$val){
$promid=explode("-",$key)[1];
$arr[$promid]+=$val;
}
foreach ($arr as $ky=>$ve){
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-2-'.$ky.'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
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();
//正确提交订单记录redis数量
if(!empty($sd)) {
$skillarray = json_decode($sd, true);
foreach ($skillarray as $lk => $lv) {
//判断秒杀数量是否超限
$num0 = $lv['num'];
//$name='ms'.$lv['fid'].'-'.$stoid;
$name=get_redis_name($lv['fid'],1,$stoid);
$jsda['len']=$this->redis->lLen($name);
$jsda['num']=-$num0;
$jsda['order_sn']=$pno;
$jsda['type']=0; //0是买单 1是5分钟自动补回 2是手动补回
mlog(json_encode($jsda),'flash_log/' . $stoid . '/' . $lv['fid']."_1");
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
if($ispt){
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
//$name='pind'.$pdid.'-'.$stoid;
$name=get_redis_name($pdid,6,$stoid);
$jsdaa['len']=$this->redis->lLen($name);
$jsdaa['num']=-$pdgoodsnum;
$jsdaa['order_sn']=$pno;
$jsdaa['type']=0; //0是买单 1是5分钟自动补回 2是手动补回
mlog(json_encode($jsdaa),'pt_log/' . $stoid . '/' . $pdid."_1");
}
if($g_promid){
$arr=null;
foreach ($g_promid as $key=>$val){
$promid=explode("-",$key)[1];
$arr[$promid]+=$val;
}
foreach ($arr as $ky=>$ve){
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-2-'.$ky.'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
}
}
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
if ($_REQUEST['act'] == 'submit_order') {
//如果失败就补回队列
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
//$name='ms'.$lv['fid'].'-'.$stoid;
$name=get_redis_name($lv['fid'],1,$stoid);
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
//如果是拼单
if($ispt){
//$name='pind'.$pdid.'-'.$stoid;
$name=get_redis_name($pdid,6,$stoid);
//拼单队列控制
for($i=0;$i<$pdgoodsnum;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
if($team_qh && $ispt=='2') {
//$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$name2 = get_redis_name($pdid,6, $stoid). ':' . $team_qh;
$this->redis->lPush($name2,time().'_0');
}
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
if($g_promid){
$arr=null;
foreach ($g_promid as $key=>$val){
$promid=explode("-",$key)[1];
$arr[$promid]+=$val;
}
foreach ($arr as $ky=>$ve){
//判断秒杀数量是否超限
//$name='grb'.$ky.'-'.$stoid;
$name=get_redis_name($ky,2,$stoid);
$num0=$ve;
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time() . '_' . $i);
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-2-'.$ky.'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
}
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 cart3_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))); //返回结果状态
$prom_goods_arr=$this->com_prom_goods($cart_goods);
/*----
$wlist=""; $sumlist="";
//----计算商品组,用于优惠券----
foreach ($cart_goods as $k=>$v){
if($v['is_gift']!=1) {
$wlist .= $v['erpwareid'] .",";
$sumlist .= ($v['goods_price'] * $v['goods_num']) . ",";
}
}
$wlist = substr($wlist,0,strlen($wlist)-1);
$sumlist = substr($sumlist,0,strlen($sumlist)-1);--*/
$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;
$quan = null;
// 找出是不是冻结的券
$fzquan = M('frozen_quan')->where('user_id', $this->user_id)->field('CashRepNo')->select();
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) {
$wlist="";$sumlist="";$list=[];
foreach ($cart_goods as $kk => $vv) {
$t_arr= $this->com_package($vv,$v,$wlist,$sumlist,$prom_goods_arr);
if($t_arr['list']) $list[]=$t_arr['list'];
$wlist=$t_arr['wlist'];
$sumlist=$t_arr['sumlist'];
}
$plist[$k]["goods"] = $list;
//组装商品ID和金额
$wlist = substr($wlist,0,strlen($wlist)-1);
$sumlist = substr($sumlist,0,strlen($sumlist)-1);
$plist[$k]["wlist"]=$wlist;
$plist[$k]["sumlist"]=$sumlist;
//如果有地址,选择物流时,就要查询包邮券
if ($address_id && $exparr[$v['pick_id']]==0) {
//计算总价
$r = $this->cartLogic->my_calculate_cart2($list, $this->user_id);
//获取会员地址
$address = M('UserAddress')->where("address_id", $address_id)->find();
$now=time();
//拿出未使用未过期的优惠券
$wh['a.isuse']=0;
$wh['a.use_start_time']=['<',$now];
$wh['a.store_id']=getMobileStoId();
$wh['a.condition']=['<=',$r];
$wh['a.user_id']=$this->user['user_id'];
$wh2="a.use_end_time>".$now." or a.use_end_time=0 or a.use_end_time='' ";
$bylist=M("user_feemail")->alias('a')
->where($wh)
->field("a.no,a.condition,a.use_start_time,a.use_end_time,a.exparea as free_id")
->select();
$t_key=$v['pick_id'];
if($bylist){
foreach ($bylist as $kt=>$vt){
$isby=$this->cartLogic->isby($address,$vt['free_id']);
if($isby){
if($vt['use_start_time'])
$vt['start_v']=date('Y-m-d',$vt['use_start_time']);
else
$vt['start_v']="";
if($vt['use_end_time'])
$vt['end_v']=date('Y-m-d',$vt['use_end_time']);
else
$vt['end_v']='不限';
//是否冻结
$isdj=0;
//冻结的券不能使用
foreach ($fzquan as $ku => $vu) {
if ($vt['data']['no'] == $vu['CashRepNo']) {
$isdj=1;
break;
}
}
if($isdj==0) $quan[$t_key][]=$vt;
}
}
}
}
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) {
$wlist="";$sumlist="";$list=[];
foreach ($cart_goods as $kk => $vv) {
$t_arr= $this->com_package($vv,$v,$wlist,$sumlist,$prom_goods_arr);
mlog(json_encode($t_arr),"quan_ji_suan/".getMobileStoId());
if($t_arr['list']) $list[]=$t_arr['list'];
$wlist=$t_arr['wlist'];
$sumlist=$t_arr['sumlist'];
}
$plist[$k]["goods"] = $list;
//组装商品ID和金额
$wlist = substr($wlist,0,strlen($wlist)-1);
$sumlist = substr($sumlist,0,strlen($sumlist)-1);
$plist[$k]["wlist"]=$wlist;
$plist[$k]["sumlist"]=$sumlist;
//如果有地址,选择物流时,就要查询包邮券
if ($address_id && $exparr[$v['pick_id']]==0) {
//计算总价
$r = $this->cartLogic->my_calculate_cart2($list, $this->user_id);
//获取会员地址
$address = M('UserAddress')->where("address_id", $address_id)->find();
$now=time();
//拿出未使用未过期的优惠券
$wh['a.isuse']=0;
$wh['a.use_start_time']=['<',$now];
$wh['a.store_id']=getMobileStoId();
$wh['a.condition']=['<=',$r];
$wh['a.user_id']=$this->user['user_id'];
$wh2="a.use_end_time>".$now." or a.use_end_time=0 or a.use_end_time='' ";
$bylist=M("user_feemail")->alias('a')
->where($wh)
->where($wh2)
->field("a.no,a.condition,a.use_start_time,a.use_end_time,a.exparea as free_id")
->select();
$t_key=$v['pick_id'];
if($bylist){
foreach ($bylist as $kt=>$vt){
$isby=$this->cartLogic->isby($address,$vt['free_id']);
if($isby){
$vt['start_v']=date('Y-m-d',$vt['use_start_time']);
$vt['end_v']=date('Y-m-d',$vt['use_end_time']);
if($vt['use_start_time'])
$vt['start_v']=date('Y-m-d',$vt['use_start_time']);
else
$vt['start_v']="";
if($vt['use_end_time'])
$vt['end_v']=date('Y-m-d',$vt['use_end_time']);
else
$vt['end_v']='不限';
//是否冻结
$isdj=0;
//冻结的券不能使用
foreach ($fzquan as $ku => $vu) {
if ($vt['data']['no'] == $vu['CashRepNo']) {
$isdj=1;
break;
}
}
if($isdj==0) $quan[$t_key][]=$vt;
}
}
}
}
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;$goods_prom_amount = 0;
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$result = calculate_price2($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, $coupon_id[$v['pick_id']],
$couponCode, $stoid, $exparr[$v['pick_id']],$v['discount'],$v['wlist'],$v['sumlist']);
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'];//订单优惠
$goods_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), // 订单优惠优惠了多少钱
'goods_prom_amount' => round($goods_prom_amount, 2), // 优惠活动优惠了多少钱
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price,'quan'=>$quan); // 返回结果状态
exit(json_encode($return_arr));
}
/*
* 订单支付页面
*/
public function cart4()
{
$orderno = I('order_id');
$order_amount = M('Order')->where("parent_sn", $orderno)->sum('order_amount');
$order = M('Order')->where("parent_sn", $orderno)->select();
// 如果已经支付过的订单直接到订单详情页面. 不再进入支付页面
if ($order[0]['pay_status'] == 1) {
$order_detail_url = U("Mobile/User/order_detail", array('stoid' => getMobileStoId(), 'id' => $order["order_id"]));
header("Location: $order_detail_url");
exit;
}
$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and scene in(0,1)")->select();
//微信浏览器
if (strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
//$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','cod')")->select();
$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin')")->select();
}
$paymentList = convert_arr_key($paymentList, 'code');
foreach ($paymentList as $key => $val) {
$val['config_value'] = unserialize($val['config_value']);
if ($val['config_value']['is_bank'] == 2) {
$bankCodeList[$val['code']] = unserialize($val['bank_code']);
}
//判断当前浏览器显示支付方式
if (($key == 'weixin' && !is_weixin()) || ($key == 'alipayMobile' && is_weixin())) {
unset($paymentList[$key]);
}
}
$bank_img = include APP_PATH . 'home/bank.php'; // 银行对应图片
$payment = M('Plugin')->where("`type`='payment' and status = 1")->select();
$this->assign('paymentList', $paymentList);
$this->assign('bank_img', $bank_img);
$this->assign('order_amount', $order_amount);
$this->assign('bankCodeList', $bankCodeList);
$this->assign('pay_date', date('Y-m-d', strtotime("+1 day")));
upload_ylp_log('订单支付');
return $this->fetch('', getMobileStoId());
}
public function cart5()
{
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
$userd=$this->user;
//获取积分
$integ=get_userpoints($userd,getMobileStoId(),$this->pm_erpid);
//20180717 season(增加短信随机码校验)
$storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
$sms_send_type = json_decode($storeconfig['sms_send_type'], true);
$this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
$this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
$this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);
$user_openid = $this->user['openid'];
//$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
$this->assign("user_openid", $user_openid);
$user_openidkey = md5($user_openid . "&" . getErpKey());
$this->assign("user_openid", $user_openid);
$this->assign("user_openidkey", $user_openidkey);
//
$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));
$this->assign('u_integral', $integ);
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
/*--是否是购物车--*/
$tocart = I("tocart/d", 0);
$result = null;
$count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 2])->count();
if ($count == 0)
$this->redirect('Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品
$cartinfo = M('cart')->where(array('state' => 2, 'user_id' => $this->user_id))->select(); //购物车积分购
$proinfo = M('goods')->where(array('goods_id' => $cartinfo[0]['goods_id']))->select();
$inteinfo = M('integral_buy')->where(array('goods_id' => $proinfo[0]['goods_id'], 'is_end' => 0))->select();
mlog(json_encode($result), "cart");
$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())
->select();//物流公司
/*--购物车商品分组--*/
$cart_goods = $result['cartList'];
$expty = 0;
foreach ($cart_goods as $kp => $vp) {
$exp2=0;
if($vp['distr_type']!=$vp['bdistr_type']){
$exp2=$vp['distr_type']==0?$vp['bdistr_type']:$vp['distr_type'];
}else{
$exp2=$vp['distr_type'];
}
$cart_goods[$kp]['expty']=$exp2;
if ($vp['is_gift'] == 0) {
//判断最后的物流方式
if ($expty != 0) {
if ($expty != $exp2) {
if($exp2==2 || $expty==2 ){
$expty=2;
}else{
$expty=0;
}
}
} else {
$expty =$exp2;
}
}
}
$this->assign("expty", $expty);
/*----
foreach ($cart_goods as $kp => $vp) {
if ($vp['distr_type'] != 0) {
if ($expty != 0) {
if ($expty != $vp['distr_type']) {
$this->error('您不能同时选择自提和物流商品', 'cart/cart', array('stoid' => getMobileStoId()));
}
} else {
$expty = $vp['distr_type'];
}
}
}
$this->assign("expty", $expty);---*/
$sql = null;
//mlog('$tocart:'.$sql,'cart');
$plist_cart = Db::query("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)");
foreach ($plist_cart as $k => $v) {
$expty00 = 0;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$list[] = $vv;
$expty11=$vv['expty'];
if($expty00==0){
$expty00=$expty11;
}
}
}
$plist_cart[$k]["goods"] = $list;
$plist_cart[$k]["expty"]=$expty00;
// 获取订单商品总价,$tocart=0为购物车 1为立即购买
//$r = $this->cartLogic->my_calculate($list,$this->user_id,$tocart);
// 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
// $fzquan=M('frozen_quan')->where('user_id',$this->user_id)->field('CashRepNo')->select();
// $snow=date("Y-m-d H:i:s",time());
// $tk = M("store")->where("store_id", getMobileStoId())->field("api_token")->find();
// $user = M("users")->where("user_id",$this->user_id)->field("erpvipid")->find();
// $where = "VIPId='" . $user['erpvipid'] . "'";
// $where .= " and IsUse=0 and 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);
// $qlistall = json_decode($rs, true);
// if(!empty($qlistall)){
// if($qlistall['data']) {
// foreach ($qlistall['data'] as $k1 => $v1) {
// foreach ($fzquan as $ku=>$vu){
// $qlistall['data'][$k1]['CashRepNo']=$vu['CashRepNo'];
// unset($qlistall['data'][$k1]);
// break;
// }
// $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]['ValidDate']=date('Y-m-d H:i:s', strtotime($v1['ValidDate']));
// }
//
// }
// }
// //$couponList = DB::query($sql);
// $plist_cart[$k]["couponList"]=$qlistall['data'];
unset($list);
//unset($qlistall);
}
$this->assign('slist', $plist_cart);
$this->assign('shippingList', $shippingList); // 物流公司
$this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购买积分购商品订单页面');
return $this->fetch('', getMobileStoId());
}
/**
* ajax 获取积分购订单商品价格或者积分购提交订单
*/
public function cart6()
{
/*--物流方式--*/
$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");
/*--积分购传到前台的值--*/
$arystr = I("goodsstr");
$ary0 = explode("-", $arystr);
/*--是否是编辑地址--*/
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'); // 发票
$exparr= I('exparr/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);
$exptype=0;
foreach ($exparr as $ko=>$vo){
if($vo==1){ $exptype=1; break;}
}
/*--购物车商品提取--*/
/*--
$cart_goods = M('Cart')->alias('a')
->join("pick_up b","a.pick_id=b.pickup_id",'left')
->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
/*---购物车和立即购买的区分---*/
$result = null;
$result = $this->cartLogic->gointeList($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;
$userd = $this->user;
mlog('获取列表值:'.json_encode($cart_goods),'cart6/'.$stoid);
if ($state == 2) {
$pay_points = $cart_goods[0]['prom']['integral'] * $cart_goods[0]["goods_num"];
}
mlog('状态:'.$state,'cart6/'.$stoid);
mlog('支付积分值:'.$pay_points,'cart6/'.$stoid);
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
$err_txt = "";
/*--库存判断和活动判断,商品的库存和活动不能过期--*/
foreach ($cart_goods as $k => $value) {
//活动不一样就是发生了变化
if($value['prom_type1']!=$value['prom_type'] && $value['prom_id1']!=$value['prom_id'] ) {
return array('status' => -3, 'msg' => "{$value['goods_name']}商品价格已经变化", 'result' => '');
}
$ginfo = $value;
/*--线下库存的序列key--*/
$bkey = $value['pick_id'] . $value['goods_id'];
//商品参与活动查看
if ($value['prom_type'] > 0) {
$prom = get_goods_promotion1($value, 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 ($ary0[0] != $prom['integral'] && $ary0[0] != "" && $ary0[1] != "") {
$err_txt .= "商品购买积分已经变化";
}
if ($ary0[1] != $prom['addmoney'] && $ary0[0] != "" && $ary0[1] != "") {
$err_txt .= "商品价格已经变化
";
}
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 {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
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) {
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=" . $state . " 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();
$uerp =tpCache('shop_info',$stoid);
$pno = "";
if (count($plist) > 1) $pno = $uerp['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_id = 0;$order_prom_amount = 0;
// 启动事务
// Db::startTrans();
// try {
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$scode = $shipping_code[$v['pick_id']];
// if (!empty($shipping_code)){
// if ($scode == 'mdzz'){
// $exptype = 1;
// }else{
// $exptype = 0;
// }
// }
$result = calculate_price3($this->user, $v["goods"], $scode, 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, 0, "", $stoid, $exparr[$v['pick_id']]);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = $user_money - $result['result']['user_money'];
$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'];// 商品价格
// 提交订单
if ($_REQUEST['act'] == 'submit_order') {
$address_id = $_REQUEST['address_id'];
mlog("日志" . $address_id . "是否编辑地址" . $setadd, "cart");
if (!$address_id && $exptype == 0) exit(json_encode(array('status' => -3, 'msg' => '请先填写收货人信息', 'result' => null))); // 返回结果状态
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' => 0, // 订单优惠活动id
'order_prom_amount' => 0, // 订单优惠活动优惠了多少钱
);
/*--调用生成订单的函数--*/
$result = $this->cartLogic->addOrder($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, $exparr[$v['pick_id']], $pno, $v['pick_id'], null, $this->user); // 添加订单
if ($result["status"] != 1)
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();
$odr_arr=M('order')->where('parent_sn',$pno)->where('store_id',$stoid)
->field('order_amount,order_sn,order_id,user_id')->select();
foreach ($odr_arr as $k=>$v) {
//不是拼单的订单,才进行分成
if($data['pt_prom_id']>0){
}else{
$order=$v;
//分销开关全局
$distribut_switch = tpCache('distribut.switch', getMobileStoId());
mlog("分成开关-" . $order['order_id'] . $order['order_sn'] . ":" . $distribut_switch, 'rebate_log/' . getAdmStoId());
if ($distribut_switch == 1 && file_exists(APP_PATH . 'common/logic/DistributLogic.php')) {
$distributLogic = new \app\common\logic\DistributLogic();
$distributLogic->rebate_log($order, getMobileStoId()); // 生成分成记录
}
}
if ($v['order_amount'] == 0) {
update_pay_status2($v['order_sn'], 1);
}
}
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();
$odr_arr=M('order')->where('parent_sn',$pno)->where('store_id',$stoid)
->field('order_amount,order_sn,order_id,user_id')->select();
foreach ($odr_arr as $k=>$v) {
if ($v['order_amount'] == 0) {
update_pay_status2($v['order_sn'], 1);
}
}
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
}
}
// }
// catch (\Exception $e) {
// // 回滚事务
// Db::rollback();
// 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)// 商品价格
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/**
* ajax 获取积分购订单商品价格
*/
public function cart6_nosub()
{
/*--物流方式--*/
$exptype = I("exptype/d");
$stoid = I("stoid");
$state = I("state/d",0);
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");
/*--是否是编辑地址--*/
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'); // 发票
$exparr= I('exparr/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);
/*--购物车商品提取--*/
/*--
$cart_goods = M('Cart')->alias('a')
->join("pick_up b","a.pick_id=b.pickup_id",'left')
->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
/*---购物车和立即购买的区分---*/
$result = null;
$result = $this->cartLogic->gointeList($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;
$userd = $this->user;
$pay_points = $cart_goods[0]['prom']['integral'] * $cart_goods[0]["goods_num"];
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=" . $state . " 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();
$uerp = tpCache('shop_info',$stoid);
$pno = "";
if (count($plist) > 1) $pno = $uerp['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_id = 0;
$order_prom_amount = 0;
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$scode = $shipping_code[$v['pick_id']];
$result = calculate_price3($this->user, $v["goods"], $scode, 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, 0, "", $stoid, $exparr[$v['pick_id']]);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = $user_money - $result['result']['user_money'];
$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'];// 商品价格
}
$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)// 商品价格
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/*
* ajax 请求获取购物车列表
*/
public function ajaxCartList()
{
$post_goods_num = I("goods_num/a"); // goods_num 购物车商品数量
$post_cart_select = I("cart_select/a"); // 购物车选中状态
$where['session_id'] = $this->session_id; // 默认按照 session_id 查询
// 如果这个用户已经等了则按照用户id查询
$user_id = $this->user_id;
$stoid = getMobileStoId();
if ($user_id) {
unset($where);
$where['user_id'] = $user_id;
}
$where['state'] = 0;
$where['store_id'] = $stoid;
$where['is_gift'] = 0;
M('Cart')->where($where)->save(['selected' => 0]);
$cartList = M('Cart')->alias('a')->join('goods b', 'a.goods_id=b.goods_id')->where(['a.user_id' => $user_id, 'a.store_id' => $stoid, 'a.state' => 0])
->getfield('a.id,a.prom_type,a.selected,a.pick_id,a.goods_id,a.goods_num,a.prom_id,b.shop_price,b.store_count,b.is_on_sale,a.is_gift,b.prom_type as prom_type1,b.viplimited,b.prom_id as prom_id1,a.goods_price');
$now0 = time();
/*---如果是线下销售规则,获取所有购物车商品和门店的组合库存数组---*/
$sale_r = tpCache("basic.sales_rules", $stoid);
if ($sale_r == 2) {
$dlinearr = getcountarr($cartList, $stoid);
}
/*-----------------页面一开始加载的时候更新数量,剔除下架和活动结束的商品-------------*/
if (!empty($cartList)) {
/*--要剔除下架商品,活动有过期的商品,或者商品在参加活动之前被添加到购物车--*/
foreach ($cartList as $k => $v) {
if ($v['is_on_sale'] == 1 && $v['on_time']< $now0 && ($v['down_time']> $now0 || empty($v['down_time']))) {
//--当是优惠促销,搭配促销,普通商品的时候,要直接剔除商品--
if($v['prom_type']==3 || $v['prom_type']==5 || $v['prom_type']==0 ){
$rank_switch = tpCache('shopping.rank_switch', $stoid);//等级价格
$mz_switch = tpCache('shopping.is_beauty', $stoid);//美妆价格
//--判断等级价--
$card_field=$this->user['card_field'];
if($card_field && strtotime($this->user['card_expiredate'])>time()) $is_dengji=1;
$Special_price=0;
$goodsinfo=M('goods')->where("store_id",$stoid)->where('goods_id',$v['goods_id'])
->field("mz_price,cardprice1,cardprice2,cardprice3")
->find();
//--如果商家有开通等级价,或者美妆价的功能--
if($rank_switch && $is_dengji && $goodsinfo[$card_field]>0 ){
$Special_price=$goodsinfo[$card_field];
}else if( $mz_switch && $this->user['is_mzvip'] && $goodsinfo["mz_price"]>0 ) {
$Special_price = $goodsinfo["mz_price"];
}
//如果有特殊的价格,等级价和美妆价不为0
if($Special_price){
if($Special_price!=$v['goods_price']){
M('Cart')->where('id', $v['id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
continue;
}
}else{
//如果价格有发生变化就要
if($v['shop_price']!=$v['goods_price']){
M('Cart')->where('id', $v['id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
continue;
}
}
}
if ($v['prom_type'] != 0 && $v['is_gift'] == 0) {
$is_del_ok=1;
//--要判断优惠全场的情况---
if($v['prom_type']==3){
//---不同优惠活动的话---
if($v['prom_type1']==3 && $v['prom_id1']!=$v['prom_id'] ){
M('Cart')->where('id', $v['id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
continue;
}
$rs=M("prom_goods")->where("store_id",$stoid)
->where('id',$v['prom_id'])->field("good_object")->find();
if($rs && $rs['good_object']==0){
$is_del_ok=0;
}
}
//参加活动又删除的情况
if($v['prom_type1']==0 && $v['prom_id1']==0 && $is_del_ok==1){
M('Cart')->where('id', $v['id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
continue;
}
mlog("zhengq-0:".json_encode($v),"ajaxCartList/".$stoid);
//不是普通购买,且活动id不一样
if($v['is_integral_normal']!=1 && $v['is_pd_normal']!=1 && $v['prom_type']!=3){
if($v['prom_type1']!=$v['prom_type'] && $v['prom_id1']!=$v['prom_id'] ){
M('Cart')->where('id', $v['id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
continue;
}
}
mlog("zhengq:".json_encode($v),"ajaxCartList/".$stoid);
$prom = get_goods_promotion1($v, null, $user_id);
if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
} else {
$where = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $v['goods_id'],
'is_end' => 0
];
if ($v['prom_type1'] == 1) {//秒杀
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
}
if ($v['prom_type1'] == 2) {//团购
$jj = M('group_buy')->where($where)->find();
if ($jj) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
}
if ($v['prom_type1'] == 4) {//积分购
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
}
if ($v['is_gift'] == 1) {
M('cart')->where(['id' => $v['id'], 'user_id' => $user_id])->delete();
unset($cartList[$k]);
}
}
} else {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
unset($cartList[$k]);
}
}
}
//统计赠品
// $count=array();
// foreach ($cartList as $val){
// if ($val['is_gift']==1){
// $count[$val['prom_id']]+=1;
// $count[$val['pick_id']][$val['prom_id']]+=1;
// }
// }
/*--------------改变数量和选择商品--------------*/
if (!empty($post_goods_num)) {
// 修改购物车数量,和勾选状态
foreach ($post_goods_num as $key => $val) {
if(empty($cartList[$key])) continue;
/*--线下库存的序列key--*/
$bkey = $cartList[$key]['pick_id'] . $cartList[$key]['goods_id'];
$data['goods_num'] = $val < 1 ? 1 : $val;
$cnum=M("cart")->where('pick_id<>'.$cartList[$key]['pick_id'])
->where('user_id',$this->user_id)
->where('goods_id',$cartList[$key]['goods_id'])->sum('goods_num');
if ($cartList[$key]['prom_type'] > 0 && $cartList[$key]['prom_type'] != 3 && $cartList[$key]['prom_type'] != 5) {
$prom = get_goods_promotion1($cartList[$key], null, $user_id);
$ou=$prom['store_count']-$cnum>0?$prom['store_count']-$cnum:0;
/*---如果大于线上库存--*/
$data['goods_num'] = $data['goods_num']+$cnum > $prom['store_count'] ?$ou : $data['goods_num'];
if ($cartList[$key]['prom_type'] == 1 || $cartList[$key]['prom_type'] == 2 ) {
$ou=$prom['onlybuy']-$cnum>0?$prom['onlybuy']-$cnum:0;
$data['goods_num'] = $data['goods_num']+$cnum>$prom['onlybuy'] ? $ou: $data['goods_num'];
}
/*---如果是秒杀和积分购---*/
if ($cartList[$key]['prom_type'] == 1 || $cartList[$key]['prom_type'] == 2 ) {
$ou=$prom['buy_limit']-$cnum>0?$prom['buy_limit']-$cnum:0;
$data['goods_num'] = $data['goods_num']+$cnum > $prom['buy_limit'] ? $ou: $data['goods_num'];
}
} else {
/*--如果是线下规则--*/
if ($sale_r == 2) {
$dlcount = $dlinearr[$bkey];
if (!empty($dlcount) && $dlcount > 0) {
$gid=$cartList[$key]['goods_id'];
$pid=$cartList[$key]['pick_id'];
//---获取预出库数---
//线下库存判断预存是多少
$strq = 'select sum(out_qty) as sum0 from __PREFIX__erp_yqty where goods_id='
. $gid . ' and store_id='.$stoid.' and pickup_id='.$pid;
$dd = Db::query($strq);
$dlcount=$dlcount-$dd[0]['sum0'];
$data['goods_num'] = $data['goods_num'] > $dlcount ? $dlcount : $data['goods_num'];
}
else{
$data['goods_num']=0;
}
} else if ($sale_r == 1) {
$data['goods_num'] = $data['goods_num'] > $cartList[$key]['store_count'] ? $cartList[$key]['store_count'] : $data['goods_num'];
}
}
$data['selected'] = $post_cart_select[$key] ? 1 : 0;
if ($cartList[$key]['goods_num'] != $data['goods_num'] || $cartList[$key]['selected'] != $data['selected']) {
M('Cart')->where("id", $key)->save($data);
}
}
$this->assign('select_all', input('post.select_all')); // 全选框
}
$prom_goods = array();
if (!empty($post_cart_select)) {
foreach ($post_cart_select as $kk => $vv) {
if ($cartList[$kk]['prom_type'] == 3) {
$prom_goods[$cartList[$kk]['pick_id']][$cartList[$kk]['prom_id']]['num'] += $post_goods_num[$kk];
$prom_goods[$cartList[$kk]['pick_id']][$cartList[$kk]['prom_id']]['price'] += $cartList[$kk]['goods_price'] * $post_goods_num[$kk];
$prom_goods['pick'][] = $cartList[$kk]['pick_id'];//收集选中的门店
}
}
}
if (!empty($prom_goods)) {
M('cart')->where(['is_gift' => 1, 'user_id' => $user_id, 'pick_id' => ['not in', $prom_goods['pick']]])->delete();
unset($prom_goods['pick']);
$prominfo = M('prom_goods')->where(['start_time' => ['lt', time()], 'end_time' => ['gt', time()], 'is_end' => 0])->getfield('id,good_object,name,is_bz');
foreach ($prom_goods as $k => $v) {
foreach ($v as $kk => $vv) {
$prom_id_list[] = $kk;//收集选中门店选中优惠活动id
if (!empty($prominfo[$kk])) {
mlog('1-'.$vv['price'],'ajaxCartList/'.$stoid);
$prom = discount($vv['price'], $kk, $vv['num'], $user_id,$prominfo[$kk]['is_bz']);
if (!empty($prom['goods_id'])) {
$count = M('cart')->where(['user_id' => $user_id, 'prom_id' => $kk, 'is_gift' => 1])->count();
$pick = M('cart')->where(['user_id' => $user_id, 'prom_id' => $kk, 'is_gift' => 1, 'pick_id' => $k])->find();
//要判断倍增,所以赠品是数量也要倍增
$num=1;
if($prom['is_bz']==1){
//按件的判断
$num=$prom['bs'];
//if($num>$prom['limit_num']) $num=$prom['limit_num']; //礼品有最大输出限制
if($num>$prom['limit_num']) $num=0; //礼品有最大输出限制
}
//如果赠品的总送的数量大于礼包的库存,则不送
if($num+$count>$prom['gift_storecount']) $num=0;
if ($count < $prom['limit_num'] && empty($pick) && $num>0) {
M('cart')->add(['user_id' => $user_id, 'session_id' => $this->session_id, 'goods_id' => $prom['goods_id'], 'goods_sn' => '',
'goods_name' => $prom['goods_name'], 'market_price' => 0, 'goods_price' => 0, 'member_goods_price' => 0, 'goods_num' => $num, 'sku' => '',
'add_time' => time(), 'prom_type' => 3, 'prom_id' => $kk, 'pick_id' => $k, 'store_id' => $stoid, 'state' => 0, 'main_cart_id' => 1,
'is_gift' => 1, 'selected' => 1,'gift_id'=>$prom['gift_id']]);
}
else{
M('cart')->where(['id' =>$pick['id']])
->save(['goods_num'=>$num]);
}
} else {
M('cart')->where(['user_id' => $user_id, 'is_gift' => 1, 'prom_id' => $kk, 'pick_id' => $k])->delete();
}
if ($prom['type'] == 0) {
if ($vv['price'] < $prom['condition']) {
$prom_goods[$k][$kk]['condition'] = ($prom['condition'] - $vv['price']) . '元';
}
} else {
if ($vv['num'] < $prom['condition']) {
$prom_goods[$k][$kk]['condition'] = ($prom['condition'] - $vv['num']) . '件';
}
}
if (!empty($prom['rule'])){
$prom_goods[$k][$kk]['next_mon'] = $prom['rule']['money'];
$prom_goods[$k][$kk]['next_sal'] = $prom['rule']['sale'];
$prom_goods[$k][$kk]['next_pas'] = $prom['rule']['is_past'];
$prom_goods[$k][$kk]['next_int'] = $prom['rule']['int'];
$prom_goods[$k][$kk]['next_cou'] = $prom['rule']['coupon'];
$prom_goods[$k][$kk]['next_gif'] = $prom['rule']['gift'];
}
$prom_goods[$k][$kk]['all'] = $prominfo[$kk]['good_object'];
}
}
M('cart')->where(['is_gift' => 1, 'user_id' => $user_id, 'pick_id' => $k, 'prom_id' => ['not in', $prom_id_list]])->delete();
}
// $pick = substr($pick, 0, strlen($pick) - 1);//去除最后一个字符
} else {
M('cart')->where(['user_id' => $user_id, 'is_gift' => 1])->delete();
}
/*------购物车中商品库存要判断线下库存------*/
$result = $this->cartLogic->cartList($this->user, $this->session_id, 0, 1);
$cart_goods = $result['cartList'];
foreach ($cart_goods as $k=>$v){
if (empty($v['viplimited'])){
$cart_goods[$k]['viplimited'] = 10000;
}else{
$limit=M('order')->alias('a')->join('order_goods b','a.order_id=b.order_id')->where(['b.goods_id'=>$v['goods_id'],'a.user_id'=>$user_id,'a.order_status'=>[['=',4],['<',3],'or']])->sum('b.goods_num');
$cart_goods[$k]['viplimited']-= $limit;
}
}
$allprice = 0;
$selectnum = 0;
if ($sale_r == 2) {
/*---循环变更购物车商品的库存---*/
foreach ($cart_goods as $km => $vm) {
/*--线下库存的序列key--*/
$bkey = $vm['pick_id'] . $vm['goods_id'];
if (($vm['prom_type'] == 0 || $vm['prom_type'] == 3 || $vm['prom_type'] == 5) && $vm['is_gift']==0) {
$dlcount = $dlinearr[$bkey];
$gid=$vm['goods_id'];
//---获取预出库数---
//线下库存判断预存是多少
$strq = 'select sum(out_qty) as sum0 from __PREFIX__erp_yqty where goods_id='
. $gid . ' and store_id='.$stoid.' and pickup_id='.$vm['pick_id'];
mlog($strq,"ajaxCartList/".$stoid);
$dd = Db::query($strq);
mlog("num1:".$dd[0]['sum0'],"ajaxCartList/".$stoid);
$dlcount=$dlcount-$dd[0]['sum0'];
mlog("num1:".$dlcount,"ajaxCartList/".$stoid);
if ($dlcount == null) $dlcount = 0;
$cart_goods[$km]['store_count'] = $dlcount;
if ($cart_goods[$km]['goods_num'] > $dlcount) {
$cart_goods[$km]['selected'] = 0;
$cart_goods[$km]['goods_num'] = $dlcount;
$rss = M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $dlcount]);
} else {
if ($vm['selected']) {
$allprice += $vm['goods_num'] * $vm['member_goods_price'];
$selectnum += $vm['goods_num'];
}
}
} else {
if ($cart_goods[$km]['goods_num'] > $vm['store_count']) {
$cart_goods[$km]['selected'] = 0;
M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $vm['store_count']]);
} else {
if ($vm['selected']) {
$allprice += $vm['goods_num'] * $vm['member_goods_price'];
$selectnum += $vm['goods_num'];
}
}
}
}
} else {
/*---循环变更购物车商品的库存---*/
foreach ($cart_goods as $km => $vm) {
if ($cart_goods[$km]['goods_num'] > $vm['store_count']) {
$cart_goods[$km]['selected'] = 0;
M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $vm['store_count']]);
} else {
if ($vm['selected']) {
$allprice += $vm['goods_num'] * $vm['member_goods_price'];
$selectnum += $vm['goods_num'];
}
}
}
}
$this->assign("allprice", $allprice);
$this->assign("selectnum", $selectnum);
$countC = M('Cart')->where('user_id', $this->user_id)->where('store_id', getMobileStoId())->where('state', '0')->where('is_gift', 0)->sum('goods_num');
$this->assign("cart_num", $countC);
/*---计算价格---*/
// if (empty($result['total_price']))
// $result['total_price'] = Array('total_fee' => 0, 'cut_fee' => 0, 'num' => 0, 'atotal_fee' => 0, 'acut_fee' => 0, 'anum' => 0);
$cartsql = "select pickup_id,pickup_name from wxd_pick_up where pickup_id in(select pick_id from wxd_cart where user_id=" . $this->user_id . " and state=0 and store_id=" . getMobileStoId() . " GROUP BY pick_id)";
$plist_cart = Db::query($cartsql);
foreach ($plist_cart as $k => $v) {
$isallsele = 1;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$plist_cart[$k]['goods'][] = $vv;
if ($vv["selected"] != 1) {
$isallsele = 0;
}
// else {
// $good_jg = 0;
// if ($vv['prom_type'] != 0 && $vv['prom_type'] != 3) {
// $prom = get_goods_promotion1($vv,null, $user_id);
// $good_jg = $prom['price'];
// } else {
// $good_jg = $vv['member_goods_price'];
// }
// }
}
}
$plist_cart[$k]['selected'] = $isallsele;
if (!empty($prom_goods)) {
foreach ($prom_goods as $key => $val) {
if ($key == $v['pickup_id']) {
$plist_cart[$k]['condition'] = $val;
}
}
}
// $freight_free = tpCache('shopping.freight_free', getMobileStoId()); // 全场满多少免运费
// if ($selenum >= $freight_free || $selenum == 0) {
// $plist_cart[$k]["moremoney"] = 0;
// } else if ($selenum > 0) {
// $plist_cart[$k]["moremoney"] = $freight_free - $selenum;
// }
}
//$this->assign('cartList', $result['cartList']); // 购物车的商品
$this->assign('cartList', $plist_cart); // 购物车的商品
// $this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购物车列表');
return $this->fetch('ajax_cart_list', getMobileStoId());
}
/*
* ajax 获取用户收货地址 用于购物车确认订单页面
*/
public function ajaxAddress()
{
$regionList = M('Region')->getField('id,name');
$address_list = M('UserAddress')->where("user_id", $this->user_id)->select();
$c = M('UserAddress')->where("user_id = {$this->user_id} and is_default = 1")->count(); // 看看有没默认收货地址
if ((count($address_list) > 0) && ($c == 0)) // 如果没有设置默认收货地址, 则第一条设置为默认收货地址
$address_list[0]['is_default'] = 1;
$this->assign('regionList', $regionList);
$this->assign('address_list', $address_list);
return $this->fetch('ajax_address', getMobileStoId());
}
/**
* ajax 删除购物车的商品
*/
public function ajaxDelCart()
{
$ids = I("ids"); // 购物车ids
M("Cart")->where("id", "in", $ids)->delete(); // 删除id为5的用户数据
$return_arr = array('status' => 1, 'msg' => '删除成功', 'result' => ''); // 返回结果状态
exit(json_encode($return_arr));
}
public function getNum()
{
$user_id = $this->user_id;
$now0 = time();
if (session('?user')) {
/*--要剔除下架商品,活动有过期的商品,或者商品在参加活动之前被添加到购物车--*/
$cartList = M('Cart')->alias('a')->join('goods b', 'a.goods_id=b.goods_id')->where(['a.user_id' => $user_id, 'a.store_id' => getMobileStoId(), 'a.state' => 0])
->field('a.id,a.prom_type,a.goods_id,a.prom_id,b.shop_price,b.store_count,b.is_on_sale,a.is_gift,b.prom_type as prom_type1')->select();
if (!empty($cartList)) {
foreach ($cartList as $k => $v) {
if ($v['is_on_sale'] == 1 && $v['on_time']< $now0 && ($v['down_time']> $now0 || empty($v['down_time'])) ) {
if ($v['prom_type'] != 0 && $v['is_gift'] == 0) {
$prom = get_goods_promotion1($v, null, $user_id);
if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
} else {
$where = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $v['goods_id'],
'is_end' => 0
];
if ($v['prom_type1'] == 1) {//秒杀
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
if ($v['prom_type1'] == 2) {//团购
$jj = M('group_buy')->where($where)->find();
if ($jj) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
if ($v['prom_type1'] == 4) {//积分购
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
}
} else {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
}
$num = M("Cart")->where('user_id', $user_id)->where('store_id', getMobileStoId())->where('state', 0)->where('is_gift', 0)->sum('goods_num');
if (empty($num)) {
return 0;
} else {
return $num;
}
} else {
return 0;
}
}
/*----------------------------------地址管理------------------------------------*/
/*
* 添加地址
*/
public function add_address()
{
upload_ylp_log('添加地址');
$logic = new UsersLogic();
$id = I('address_id', 0);
$seleid = I("seleid");
$this->assign("seleid", $seleid);
$data = $logic->add_address($this->user_id, $id, I('get.'));
if ($data['status'] != 1)
return json(['code' => -1, 'fir' => $data['fir'],]);
else {
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
return json(['code' => 1, 'fir' => $data['fir'], 'id' => $data['result'],
'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
}
}
/*
* 添加地址
*/
public function addwx_address()
{
upload_ylp_log('添加微信地址');
$data= I('post.');
// $data="{\"userName\":\"\u5c0f\u82cf\",\"postalCode\":\"362000\",\"provinceName\":\"\u798f\u5efa\u7701\",\"cityName\":\"\u6cc9\u5dde\u5e02\",\"countryName\":\"\u4e30\u6cfd\u533a\",\"detailInfo\":\" \u6cc9\u5dde\u8f6f\u4ef6\u56ed4\u53f7\u697c4\u5c42\",\"telNumber\":\"13599727122\"}";
// $data=json_decode($data,true);
if (empty($data))
{
return json(array('code' =>-1,'msg' => '操作失败'));
}
$userName=$data['userName'];
$postalCode=$data['postalCode'];
$provinceName=$data['provinceName'];
$cityName=$data['cityName'];
$countryName=$data['countryName'];
$detailInfo=$data['detailInfo'];
$telNumber=$data['telNumber'];
$moreAddress=$provinceName."-".$cityName."-".$countryName."-".$detailInfo;
$provinceId=0;
$cityId=0;
$districtId=0;
$res_province=M('region')->where(array('name'=>$provinceName,'level'=>1))->find();
if ($res_province)
{
$provinceId=$res_province['id'];
}
$res_city=M('region')->where(array('name'=>$cityName,'parent_id'=>$provinceId))->find();
if ($res_city)
{
$cityId=$res_city['id'];
}
$res_district=M('region')->where(array('name'=>$countryName,'parent_id'=>$cityId))->find();
if ($res_district)
{
$districtId=$res_district['id'];
}
//判断是否已经添加过了
$resinfo=M('user_address')->where(array('user_id'=>$this->user_id,'consignee'=>$userName,'province'=>$provinceId,'district'=>$districtId,'address'=>$detailInfo,'mobile'=>$telNumber))->find();
if ($resinfo)
{
return json(array('code' => -1, 'msg' => '操作成功,已存在'));
}
$postdata['store_id']=getMobileStoId();
$postdata['user_id']=$this->user_id;
$postdata['consignee']=$userName;
$postdata['province']=$provinceId;
$postdata['city']=$cityId;
$postdata['district']=$districtId;
$postdata['address']=$detailInfo;
$postdata['more_address']=$moreAddress;
$postdata['zipcode']=$postalCode;
$postdata['mobile']=$telNumber;
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0,$postdata);
if ($data['status'] != 1)
return json(['code' => -1, 'fir' => $data['fir']]);
else {
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
return json(['code' => 1, 'fir' => $data['fir'], 'id' => $data['result'],
'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
}
}
/*
* 地址编辑
*/
public function edit_address()
{
upload_ylp_log('编辑地址');
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, I('get.'));
if ($data['status'] != 1)
return json(['code' => -1]);
else {
return json(['code' => 1]);
}
}
/*
* 获取省市区
*/
public function get_area()
{
$id = I('aid');
$address = M("user_address")->where('address_id', $id)->find();
//获取省份
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$c = M('region')->where(array('parent_id' => $address['province'], 'level' => 2))->select();
$d = M('region')->where(array('parent_id' => $address['city'], 'level' => 3))->select();
$data['province'] = $p;
$data['city'] = $c;
$data['district'] = $d;
$region_list = get_region_list();
$addrstr = $region_list[$address['province']]['name'] . "-" . $region_list[$address['city']]['name'];
if (!empty($address['district']))
$addrstr .= "-" . $region_list[$address['district']]['name'];
if (!empty($address['twon']))
$addrstr = $addrstr . "-" . $region_list[$address['twon']]['name'];
$address['addname'] = $addrstr;
if (empty($data['district'])) {
$e = M('region')->where(array('parent_id' => $address['city'], 'level' => 4))->select();
$data['twon'] = $e;
} else {
$e = M('region')->where(array('parent_id' => $address['district'], 'level' => 4))->select();
$data['twon'] = $e;
}
$jsond = ['code' => 1, 'data' => $data, 'data2' => $address];
return json($jsond);
}
/*
* 地址删除
*/
public function del_address()
{
upload_ylp_log('删除地址');
$backaddr = null;
$id = I('get.id');
$seleid = I("seleid");
$region_list = get_region_list();
$address = M('user_address')->where("address_id", $id)->find();
$row = M('user_address')->where(array('user_id' => $this->user_id, 'address_id' => $id))->delete();
// 如果删除的是默认收货地址 则要把第一个地址设置为默认收货地址
if ($address['is_default'] == 1) {
$address2 = M('user_address')->where("user_id", $this->user_id)->find();
$address2 && M('user_address')->where("address_id", $address2['address_id'])->save(array('is_default' => 1));
if ($address2) {
$provinename = M('region')->where("id", $address2['province'])->field('name')->find();
$cityname = M('region')->where("id", $address2['city'])->field('name')->find();
$distname = M('region')->where("id", $address2['district'])->field('name')->find();
$twonname = M('region')->where("id", $address2['twon'])->field('name')->find();
$addrstr = $provinename['name'] . "-" . $cityname['name'] . "-" . $distname['name'];
if ($twonname) {
$addrstr = $addrstr . "-" . $twonname['name'];
}
$address2['addrstr'] = $addrstr . " " . $address2['address'];
$backaddr = $address2;
$seleid = $address2['address_id'];
}
} else {
$provinename = $region_list[$address['province']]['name'];
$cityname = $region_list[$address['city']]['name'];
$distname = $region_list[$address['district']]['name'];
$twonname = $region_list[$address['twon']]['name'];
$addrstr = $provinename . "-" . $cityname;
if ($distname) {
$addrstr = $addrstr . "-" . $distname;
}
if ($twonname) {
$addrstr = $addrstr . "-" . $twonname;
}
$address['addrstr'] = $addrstr . " " . $address['address'];
$backaddr = $address;
}
$this->assign("seleid", $seleid);
if (!$row)
return json(['code' => -1]);
else {
/*---获取地区---*/
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
return json(['code' => 1, 'address2' => $backaddr,
'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
}
}
/*--绑定手机号--*/
public function reg_user()
{
$code = I('co');
$mobile = I('mob');
$session_id = session_id();
$logic = new UsersLogic();
$stoid = getMobileStoId();
mlog("0", 'reg_user/' . $stoid);
$user9 = M('users')
->where('mobile', $mobile)
->where('store_id', getMobileStoId())->find();
if ($user9) {
//如果openid为空
if (empty($user9['openid'])) {
//手机本来有记录,直接更新openid
$update_replace['openid'] = $this->user['openid'];
$update_replace['nickname'] = $this->user['nickname'];
if ($this->user['head_pic']) {
$update_replace['head_pic'] = $this->user['head_pic'];
}
M('users')->where('user_id', $user9['user_id'])->save($update_replace);
M('users')->where('user_id', $this->user['user_id'])->delete();
$user9['openid'] = $this->user['openid'];
session('user', $user9); //覆盖session 中的 user
$this->user = $user9;
$this->user_id = $user9['user_id'];
Cookie::set('user_id', $this->user_id);
return array('status' => 1, 'msg' => "绑定成功");
} else {
if ($user9['erpvipid']) {
return array('status' => -1, 'msg' => "该手机号已经绑定");
exit;
}
}
}
if (getMobileStoId() != "1") {
$check_code = $logic->check_validate_code($code, $mobile, $session_id);
if ($check_code['status'] != 1) {
return array('status' => -1, 'msg' => $check_code['msg']);
exit;
}
}
mlog("1", 'reg_user/' . $stoid);
$map = null;
// 成为分销商条件
$distribut_condition = tpCache('distribut.condition', $stoid);
$distribut_switch = tpCache('distribut.switch', $stoid);
if ($distribut_switch === 1 && $distribut_condition === 0 && $this->user['is_distribut'] == 0) // 本来要购买成为分销,后来商家改为直接注册就可以成为分销商进行更改
{
//判断分销可以注册的人数
$distr_num = M('store_distribut')->where('store_id', $stoid)->field('distribut_num')->find();
$count1 = M("users")->where('store_id', getMobileStoId())->where('is_distribut', 1)->count();
if ($distr_num['distribut_num'] > $count1) {
$map['is_distribut'] = 1;
$map['be_distribut_time'] = time();
$map['be_dis_condition'] = "直接购买成为分销商";
} else {
$is_out = M("store_module_endtime")
->where('store_id', getMobileStoId())
->where('type', 2)->find();
if (empty($is_out)) {
$yy = M('distri_price')->where('type', 0)->where('money', 0)->find();
$allnum = $yy['user_num'];
if ($allnum > $count1) {
$map['is_distribut'] = 1;
$map['be_distribut_time'] = time();
$map['be_dis_condition'] = "直接购买成为分销商";
}
}
}
}
$map = ['mobile' => $mobile];
$is_distri_rate = tpCache('distribut.is_distri_rate', $stoid); //分销商奖励
$is_invitation_rate = tpCache('distribut.is_invitation_rate', $stoid); //是否参与邀请送积分或代金券
//启动事务
Db::startTrans();
try {
$first_leader = Cookie::get('first_leader');//上级会员ID
if ($first_leader != "") {
$first_info = M('users')->where(array('user_id' => $first_leader, 'store_id' => $stoid))->find();
if ($first_info && $first_info['is_distribut'] == "1" ) {
$map['first_leader'] = $first_leader;
} else {
//往上找会员是否的分销商,
//如果是分销商则注册会员的 first_leader 就是此分销商
$first_info0 = M('users')->where(array('user_id' => $first_info['first_leader'], 'store_id' => $stoid))->find();
if ($first_info0 && $first_info0['is_distribut'] == "1" ) {
$map['first_leader'] = $first_info0['user_id'];
}
}
if ($map['first_leader']) {
$first_leader = M('users')->where("user_id", $map['first_leader'])->field("first_leader,second_leader")->find();
$map['second_leader'] = $first_leader['first_leader']; // 第一级推荐人
$map['third_leader'] = $first_leader['second_leader']; // 第二级推荐人
//他上线分销的下线人数要加1
M('users')->where(array('user_id' => $map['first_leader']))->setInc('underling_number');
M('users')->where(array('user_id' => $map['second_leader']))->setInc('underling_number');
M('users')->where(array('user_id' => $map['third_leader']))->setInc('underling_number');
} else {
$map['first_leader'] = 0;
}
}
mlog("6", 'reg_user/' . $stoid);
$rs = M("users")->where("user_id", $this->user_id)->save($map);
$user_info['api_token'] = tpCache('shop_info.api_token', $stoid);
$map['user_id']=$this->user_id;
$news_info = $map;
$is_distri_rate = tpCache('distribut.is_distri_rate', $stoid); //分销商奖励
$is_invitation_rate = tpCache('distribut.is_invitation_rate', $stoid); //是否参与邀请送积分或代金券
mlog("7", 'reg_user/' . $stoid);
//邀请赠送积分
if (!empty($first_info) &&
!empty($first_info['mobile']) &&
!empty($first_info['erpvipid'])
) { //如果上级会员是绑定,说明是通过链接进来,给发链接的用户赠送积分、优惠券
/*---当上级不是分销商 或者 开启分销商奖励,参与邀请送积分或代金券---*/
if ($first_info['is_distribut'] == 0 || $is_distri_rate == 0 || ($is_invitation_rate == 1 && $is_distri_rate == 1)) {
mlog("701", 'reg_user/' . $stoid);
$inv_type = tpCache('basic.inv_type', $stoid);
$inv_info = tpCache('basic.invitation_rate', $stoid);
mlog("703" . $inv_info, 'reg_user/' . $stoid);
$invitation_rate = json_decode($inv_info, true);
/*--当上级的会员是绑定了的--*/
if ($inv_type == 0) { //邀请送积分
if ($invitation_rate['inv_jf'] != "") {
/*---
$data = array(
'Id' => $first_info["erpvipid"],//会员id
'Integral' => $invitation_rate['inv_jf'],//金额
'Remark' => '邀请送积分',
);
$add_rs = getApiData("wxd.vip.addreduce", $user_info['api_token'], array($data));
mlog("711邀请送积分:" . json_encode($data) . "返回:" . $add_rs, 'smslog/' . $stoid);
$add_rs = json_decode($add_rs, true);
if ($add_rs['code'] != 1) {
// return json(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员!'));
}--*/
$accdb=tpCache("shop_info.ERPId",$stoid);
com_update_integral($accdb,$first_info,$invitation_rate['inv_jf'],"邀请送积分",$module="mobile/cart");
}
} else {
if ($invitation_rate['inv_coupon'] != "") {
mlog("72", 'reg_user/' . $stoid);
$coupon = M("coupon")->where("id", $invitation_rate['inv_coupon'])->find();
if ($coupon["send_end_time"] > time()) {
$CashRepNo = "1005" . date("ymdHis") . get_total_millisecond();
$data = array(
'CashRepNo' => $CashRepNo,//券号
'VIPId' => $first_info["erpvipid"],//会员id
'Sum' => $coupon["money"],//金额
'SendMan' => '邀请发放',//发放类型
'Operator' => '微信商城',//来源
'BuySum' => $coupon["condition"],//满多少才能使用
'Remark' => '邀请送微券【消费满' . $coupon["condition"] . '元可用】',
);
if ($coupon['useobjecttype'])
{
$data['UseObjectType']=$coupon['useobjecttype'];
$data['UseObjectID']=$coupon['useobjectid'];
$data['UseObjectNo']=$coupon['useobjectno'];
$data['UseObjectName']=$coupon['useobjectname'];
}
if ($coupon["use_start_time"] != "") {
$data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期
}
if ($coupon["use_end_time"] != "") {
$data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//截止期限
}
$erp=tpCache("shop_info.ERPId",$stoid);
$add_rs = getApiData_java_p("/api/erp/vip/cash/insert", $erp, array($data),null, null, null, "POST");
$add_rs = json_decode($add_rs, true);
if ($add_rs['code'] != 1) {
// return json(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员!'));
}
$colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#173177|#FF0000|#ca003a");
$pd['typeid'] = "1006";
$pd['backurl'] = curHostURL() . '/index.php/Mobile/User/coupon/stoid/' . $stoid;
$pd['colorlist'] = $colorlist;
$pd['title'] = "您好,恭喜您成功领取" . $coupon["money"] . "元优惠券";
$pd['key1'] = $first_info['vipname'];
$pd['key2'] = $coupon["money"] . "元优惠券";
$pd['key3'] = date("Y-m-d");
$pd['remark'] = "点击详情查看优惠券详情信息";
$Event = \think\Loader::controller('home/Api');
$Event->web_poswxcode($pd, $stoid, $first_info);
}
}
}
}
}
$wx_user = M('wx_user')->where('store_id', $stoid)->find();
$jssdk = new \app\mobile\logic\Jssdk($wx_user['appid'], $wx_user['appsecret']);
mlog("7.1", 'reg_user/' . $stoid);
/*---当是分销商 且 开启分销商奖励---*/
if ($is_distri_rate == 1) {
mlog("7.1.1", 'reg_user/' . $stoid);
$ydd_str = tpCache('distribut.invitation_ratelist', $stoid);
$ydd = json_decode($ydd_str, true);
// 一级 分销商赚 的钱
if ($ydd['first_money'] > 0 && $first_info['is_distribut'] == 1) {
// 微信推送消息
$tmp_user = $first_info;
$rrs1 = $this->add_user_money($ydd['first_money'], $first_info['user_id']);
if ($rrs1) {
$odata['user_id'] = $first_info['user_id'];
$odata['create_time'] = time();
$odata['money'] = $ydd['first_money'];
$odata['remark'] = "线下一级注册";
$odata['store_id'] = $stoid;
$odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
$odata['status'] = 1;
$odata['type'] = 4;
M('withdrawals')->save($odata);
if ($tmp_user['oauth'] == 'weixin') {
$wx_content = "你的一级下线刚刚绑定,你获得 ¥" . $ydd['first_money'] . "奖励 !";
$first_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
mlog($news_info['mobile'] . "的一级[" . $first_res['mobile'] . "]推送返回:" . json_encode($first_res), 'reg_user/' . $stoid);
}
}
}
mlog("7.1.2", 'reg_user/' . $stoid);
// 二级 分销商赚 的钱
if ($news_info['second_leader'] > 0 && $ydd['second_money'] > 0) {
$fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $news_info['second_leader'])->find();
if ($fuid2 && $fuid2['is_distribut'] == 1) {
$rrs2 = $this->add_user_money($ydd['second_money'], $fuid2['user_id']);
if ($rrs2) {
$odata['user_id'] = $fuid2['user_id'];
$odata['create_time'] = time();
$odata['money'] = $ydd['second_money'];
$odata['remark'] = "线下二级注册";
$odata['store_id'] = $stoid;
$odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
$odata['status'] = 1;
$odata['type'] = 4;
M('withdrawals')->save($odata);
// 微信推送消息
$tmp_user = $fuid2;
if ($tmp_user['oauth'] == 'weixin') {
$wx_content = "你的二级下线刚刚绑定,你获得 ¥" . $ydd['second_money'] . "奖励 !";
$second_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
mlog($news_info['mobile'] . "的二级[" . $tmp_user['mobile'] . "]推送返回:" . json_encode($second_res), 'reg_user/' . $stoid);
}
}
}
}
mlog("7.1.3", 'reg_user/' . $stoid);
// 三级 分销商赚 的钱
if ($news_info['third_leader'] > 0 && $ydd['third_money'] > 0) {
$fuid3 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $news_info['third_leader'])->find();
if ($fuid3 && $fuid3['is_distribut'] == 1) {
$rrs3 = $this->add_user_money($ydd['third_money'], $fuid3['user_id']);
if ($rrs3) {
$odata['user_id'] = $fuid3['user_id'];
$odata['create_time'] = time();
$odata['money'] = $ydd['third_money'];
$odata['remark'] = "线下三级注册";
$odata['store_id'] = $stoid;
$odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
$odata['status'] = 1;
$odata['type'] = 4;
M('withdrawals')->save($odata);
// 微信推送消息
$tmp_user = $fuid3;
if ($tmp_user['oauth'] == 'weixin') {
$wx_content = "你的三级下线刚刚绑定,你获得 ¥" . $ydd['third_money'] . "奖励 !";
$third_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
mlog($news_info['mobile'] . "的三级[" . $tmp_user['mobile'] . "]推送返回:" . json_encode($third_res), 'reg_user/' . $stoid);
}
}
}
}
mlog("7.1.4", 'reg_user/' . $stoid);
}
mlog("7.3", 'reg_user/' . $stoid);
Db::commit();
return array('status' => 1, 'msg' => "绑定成功");
}catch (\Exception $e){
Db::rollback();
mlog("err-".json_encode($e), 'reg_user/' . $stoid);
return array('status' => -1, 'msg' => "绑定失败");
}
}
/*---循环更新订单的状态---*/
public function updatecart2()
{
$tocart = I("state");
if ($tocart == 0) {
$result = $this->cartLogic->cartList($this->user, $this->session_id, 1, 1); //获取购物车商品
} else if ($tocart == 1) {
$result = $this->cartLogic->gobuyList($this->user, $this->session_id); //获取立即购买的商品
} else if ($tocart == 2) {
$result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取积分购的商品
}
return "1";
}
/*----增加会员的余额----*/
public function add_user_money($addmoney,$user_id){
$rs=M('users')->where('user_id',$user_id)->setInc('user_money',$addmoney);
return $rs;
}
/*----相应的返回redis----*/
public function backredis($no,$notime)
{
if ($_REQUEST['act'] == 'submit_order') {
$popvalue = null;
$popvalue2 = null;
$stoid = I("stoid");
//拼单有关
$ispt = I('is_pt');
$pdid = I('pdid');
$team_qh = I('team_qh');
$pdgoodsnum = I('pdgoodsnum/d', 0);
$sd = I('da');
if (!empty($sd)) {
$skillarray = json_decode($sd, true);
foreach ($skillarray as $lk => $lv) {
//$name = 'ms' . $lv['fid'] . '-' . $stoid;
$name =get_redis_name($lv['fid'],1, $stoid);
$num0 = $lv['num'];
//秒杀队列控制
for ($i = 0; $i < $num0; $i++) {
$this->redis->lPush($name, time() . '_' . $i);
}
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
//如果是拼单
if ($ispt) {
//$name = 'pind' . $pdid . '-' . $stoid;
$name = get_redis_name($pdid,6,$stoid);
//拼单队列控制
for ($i = 0; $i < $pdgoodsnum; $i++) {
$this->redis->lPush($name, time() . '_' . $i);
}
if ($team_qh && $ispt == '2') {
//$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$name2 = get_redis_name($pdid,6,$stoid). ':' . $team_qh ;
$this->redis->lPush($name2, time() . '_0' );
}
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$p_name=get_redis_set_name($stoid);
$this->redis->sRem($p_name,$key);
}
}
}
/**
* 支付尾款的确认订单页面
*/
public function cart2_wk()
{
$ord=I("ordsn");
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
$order=M('order')->where('order_sn',$ord)->find();
if(empty($order))
$this->error('未找到订单', U('Mobile/User/order_list', array('stoid' => getMobileStoId())));
/*--不能重复的一张表--*/
//$userd=M("users")->where('user_id',$this->user_id)->find();
$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));
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
/*--判断物流方式--*/
$listno=$order["pt_listno"];
$prom_id=$order['pt_prom_id'];
$pick_id=$order['pickup_id'];
$ord_goods=M('order_goods')->alias('a')->join('goods b','a.goods_id=b.goods_id')
->field('a.*,b.distr_type,b.original_img')
->where("a.order_id",$order["order_id"])
->find();
$pick=M('pick_up')->where('pickup_id',$pick_id)->field('pickup_name,distr_type')->find();
$exp2 = 0;
if($ord_goods['distr_type']==0){
$exp2=$pick['distr_type'];
}else{
$exp2= $ord_goods['distr_type'];
}
$this->assign("expty", $exp2);
$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();//物流公司
$this->assign('goods', $ord_goods);
$this->assign('listno', $listno);
$this->assign('prom_id', $prom_id);
$this->assign('pick', $pick);
$this->assign('ordsn', $ord);
$this->assign('order', $order);
$this->assign('shippingList', $shippingList); // 物流公司
//$this->assign('cartList', $result['cartList']); // 购物车的商品
upload_ylp_log('购物车已选商品显示');
return $this->fetch('', getMobileStoId());
}
//------计算尾款金额--------
public function cart3_wk(){
/*--物流方式--*/
$stoid = I("stoid");
$ord=I("ordsn");
$listno=I("listno");
$prom_id=I('prom_id');
$is_ajax=I('is_ajax');
$shipping_code = I("shipping_code"); // 物流编号
if ($this->user_id == 0)
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
$address_id = I("address_id"); //收货地址id
$setadd = I("setadd");
$exparr= I('exparr');//获取物流配送数组
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
$v=M('teamgroup')
->where('team_id',$prom_id)->where("listno",$listno)
->where('team_type',3)->where('store_id',$stoid)
->find();
$jsarr=json_decode($v['jt_json'],true);
$count=$v['jt_ct_num'];
//---要进行计算物流的钱---
$shipping_price=0;
$order=M("order")->where('order_sn',$ord)
->where('store_id',$stoid)
->field('order_amount,user_money,order_id')->find();
//要计算尾款的价钱
$price=0;
foreach ($jsarr as $ko=>$vo){ if($count>=$vo['rynum']) $price=$vo['price'];}
$num= M('order_goods')->where('order_sn',$ord)->sum('goods_num');
$tail_money=$price*$num-$order['order_amount']-$order['user_money'];
$ord_goods=M('order_goods')->alias('a')->join('goods b','a.goods_id=b.goods_id')
->field('a.goods_num,b.weight,b.is_free_shipping,b.exp_sum_type,b.uniform_exp_sum')
->where("a.order_sn",$ord)
->find();
//如果商品不是包邮的,且不是自提
$isfeelexp = $ord_goods['is_free_shipping'];$goods_weight=-1;$goods_piece=-1;
if($isfeelexp==0) {
switch ($ord_goods['exp_sum_type']) {
case 1:
//统一运费
if (!empty($shipping_code))
$shipping_price = $ord_goods['uniform_exp_sum'];
break;
case 2:
if ($goods_weight < 0) $goods_weight = 0;
//累积商品重量 每种商品的重量 * 数量
$goods_weight += $ord_goods['weight'] * $ord_goods['goods_num'];
break;
case 3:
if ($goods_piece < 0) $goods_piece = 0;
//累积商品数量
$goods_piece += $ord_goods['goods_num'];
break;
}
}
$freight_free = tpCache('shopping.freight_free',$stoid); // 全场满多少免运费
$cartLogic = new \app\home\logic\CartLogic();
$province=$city=$district=$twon=0;
if($address_id){
$address = M('UserAddress')->where("address_id", $address_id)->find();
$province=$address['province'];
$city=$address['city'];
$district=$address['district'];
$twon=$address['twon'];
}
//---自提和全场包邮---
if ($exparr == 1){
$shipping_price = 0;
}else if (!empty($shipping_code[0]) && !empty($province)) {
if ($goods_weight > -1)
$shipping_price += $cartLogic->cart_freight2($shipping_code, $province, $city, $district, $goods_weight, $stoid);
if ($goods_piece > -1)
$shipping_price += $cartLogic->cart_piece2($shipping_code, $province, $city, $district, $goods_piece, $stoid);
}
//---还有一个包邮模板---
$wuliu = $tail_money;
if($freight_free > 0 && $wuliu >= $freight_free &&$shipping_price>0 ){
$no_ex_id=tpCache('shopping.no_ex_id',$stoid);
if($no_ex_id) {
//判断是否包邮
$is_by = $cartLogic->isby2($province, $city, $district, $no_ex_id);
if($is_by==1) $shipping_price=0;
}else{
$shipping_price=0;
}
}
$all_money=$price*$num+$shipping_price-$order['order_amount']-$order['user_money'];
if($is_ajax){
if($exparr==0 && empty($address_id))
return json(['code' => -1, 'msg' => "请选择收货地址"]);
$save_data=['pt_tail_money'=>$all_money,
'province'=>$province,
'city'=>$city,
'district'=>$district,
'twon'=>$twon,
];
if($exparr==0){
$save_data['consignee']=$address["consignee"];
$save_data['address']=$address["address"];
$save_data['more_address']=$address["more_address"];
$save_data['exp_type']=0;
$save_data['shipping_price']=$shipping_price;
$save_data['shipping_code']=$shipping_code;
$rs=M('shipping')->where('shipping_code',$shipping_code)->field("shipping_name")->find();
$save_data['shipping_name']=$rs['shipping_name'];
}else{
$save_data['exp_type']=1;
$save_data['address']="";
$save_data['more_address']="";
$save_data['shipping_price']=0;
}
M('order')->where('order_id',$order['order_id'])
->save($save_data);
}
return json(['code'=>0,'tmoney'=>$tail_money,'tprice'=>$price,"cnum"=>$count,
'shipping_price'=>$shipping_price,'all_money'=>$all_money]);
}
public function com_prom_goods($cart_goods){
$prom_goods=null;
//--建立优惠促销数组--
foreach ($cart_goods as $k => $value) {
if ($value['prom_type'] == 3 && $value['is_gift'] == 0 && $value['is_collocation'] == 0) {
$pro=M("prom_goods")->where("id",$value['prom_id'])->field('is_bz,is_xz_yh')->find();
//要重新计算等级美妆价
$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'];
$prom_goods[$value['pick_id']][$value['prom_id']]['goods'][$value['goods_id']] =['goods_id'=> $value['goods_id'], 'all_price'=> $value['goods_num'] * $value['goods_price']];
$prom_goods[$value['pick_id']][$value['prom_id']]['is_bz']=$pro['is_bz'];
$prom_goods[$value['pick_id']][$value['prom_id']]['is_xz_yh']=$pro['is_xz_yh'];
}
}
if(empty($prom_goods)) return null;
//--平摊实际优惠金额--
foreach ($prom_goods as $kk => $vv) {
foreach ($vv as $key => $val) {
$prom = discount($val['price'], $key, $val['num'], $this->user_id,$val['is_bz']);
$prom_goods[$kk][$key]['prom_price']=$prom['price'];
if($val['is_xz_yh']) continue;
if($prom['price']<=0) continue;
//--平摊金额到商品---
$per=$prom['price']/$val['price'];
$goods=$val['goods'];
$prom_all=0;
foreach ($goods as $kl0=>$vl0){
$fect= floor($vl0['all_price']*$per*100)/100;
$prom_goods[$kk][$key]['goods'][$kl0]['fect_price']=$fect;
$prom_all+=$fect;
}
//有余数
$yushu=$prom['price']-$prom_all;
$prom_goods[$kk][$key]['goods'][0]['fect_price']+=$yushu;
}
}
return $prom_goods;
}
//组装list 商品ID和金额计算代金券
public function com_package($vv,$v,$wlist,$sumlist,$prom_goods){
$list_item=null;
if ($v['pick_id'] == $vv['pickup_id']) {
$list_item = $vv;
//---是否连接判断商品参与优惠券优惠---
$is_conect=1;
$fect_price=$vv['goods_price'];
$goods_num=$vv['goods_num'];
//---优惠活动----
if($vv['prom_type']==3){
if($prom_goods[$vv['pick_id']][$vv['prom_id']]['is_xz_yh']) $is_conect=0;
if($prom_goods[$vv['pick_id']][$vv['prom_id']]['prom_price']<=0) $is_conect=0;
}
//----------团购是否可以使用优惠券-----
else if($vv['prom_type']==2){
$t = time();
$gb = M('group_buy')->where('store_id', getMobileStoId())
->where('id', $vv['prom_id'])
->where('start_time<' . $t)
->where('end_time>' . $t)->where('is_end', 0)
->field('is_quan')
->find();
if ($gb) {
if ($gb['is_quan'] ==0 ) $is_conect=0;
}
}
//----秒杀商品----
else if($vv['prom_type']==1) {$is_conect=0;}
//循环组装商品ID,金额
if($vv['is_gift']!=1 && $is_conect==1) {
$wlist .= $vv['erpwareid'] .",";
//$sumlist .= ($vv['goods_price'] * $vv['goods_num']) . ",";
//去除实际价格
$cut_price=$prom_goods[$vv['pick_id']][$vv['prom_id']]['goods'][$vv['goods_id']]['fect_price'];
if($cut_price)
$sumlist .= ($cut_price) . ",";
else
$sumlist .= ($fect_price* $vv['goods_num']) . ",";
}
}
return ['list'=>$list_item,'wlist'=>$wlist,'sumlist'=>$sumlist];
}
}