/**
  * 保存数据到订单
  * @paragram $data
  */
 public function save()
 {
     if (!is_login()) {
         $this->error("您还没有登陆", U("User/login"));
     }
     $order = D("order");
     //获取支付订单号
     $tag = I('post.tag');
     $tag = safe_replace($tag);
     //过滤
     $data['tag'] = $tag;
     //支付订单号
     //防止重复提交
     //$info=$order->where("tag='$tag'")->find();
     //isset($info)&& $this->error('重复提交订单');
     //获取会员uid
     $uid = is_login();
     $data['uid'] = $uid;
     //验证价格并清空购物车
     $list = M("shoplist")->where("tag='{$tag}'")->select();
     //遍历
     foreach ($list as $k => $val) {
         //获取购物清单数据表产品id,字段goodid
         $id = $val["goodid"];
         //提交的价格
         $goodprice = $val["price"];
         $Document = D('Document');
         $info = $Document->detail($id);
         //系统价格
         if ($info['groupprice']) {
             $string = $info['groupprice'] . '、' . $info['price'];
             $array = explode('、', $string);
             //验证,多个价格
             if (!in_array($goodprice, $array)) {
                 $this->error('商品价格与系统不符,商品id' . $val["goodid"] . '商品价格' . $safeprice . '系统价格' . $string);
             }
         } else {
             if ($goodprice !== $info['price']) {
                 $this->error('商品价格与系统不符,商品id' . $val["goodid"] . '商品价格' . $safeprice . '系统价格' . $safe['price']);
             } else {
                 #your code
                 addUserLog('价格正确', $uid);
                 //日志
             }
         }
         //删除购物车中产品id
         M("shopcart")->where("goodid='{$id}'and uid='{$uid}'")->delete();
     }
     //订单号
     $data['orderid'] = date('Ym', time()) . time() . $uid;
     //订单号
     //计算商品总额
     $total = $this->getPricetotal($tag);
     $data['total'] = $total;
     //计算运费
     if ($total < C('LOWWEST')) {
         $trans = C('SHIPMONEY');
     } else {
         $trans = 0;
     }
     $data['ship_price'] = $trans;
     //运费
     //计算积分
     if ($_POST["score"]) {
         $score = I('post.score', 0, 'intval');
         // 用intval过滤$_POST["score"];
         //读取配置,1000积分兑换1元
         $ratio = $score / C('RATIO');
         M("member")->where("uid='{$uid}'")->setField('score', 0);
     } else {
         $ratio = 0;
     }
     $data['score'] = $score;
     $data['score_money'] = $ratio;
     $title = '消耗积分' . $score . '抵消金额' . $ratio;
     addUserLog($title, $uid);
     //日志
     //计算优惠券
     $code = I('post.couponcode');
     $code = safe_replace($code);
     //过滤
     //判断优惠券是否可用
     $xfee = $total + $trans - $ratio;
     //计算优惠券可使用的金额,home/common/function
     $decfee = get_fcoupon_fee($code, $xfee);
     $data['coupon'] = $code;
     //优惠券代码
     $data['coupon_money'] = $decfee;
     //优惠券金额
     //计算使用地址id
     $senderid = I('post.sender');
     $senderid = safe_replace($senderid);
     //过滤
     $data['addressid'] = $senderid;
     //计算应付金额
     $all = $total + $trans - $ratio - $decfee;
     $data['total_money'] = $all;
     //应付金额
     $data['create_time'] = NOW_TIME;
     //创建时间
     //支付类型PayType,1-货到付款
     if ($_POST["PayType"] == "1") {
         //创建订单
         $data['status'] = 1;
         $data['ispay'] = -1;
         //货到付款
         $orderid = $order->add($data);
         M("shoplist")->where("tag='{$tag}'")->setField('orderid', $orderid);
         //保存货到付款支付数据
         $pay = M("pay");
         $pay->create();
         $pay->money = $all;
         $pay->ratio = $ratio;
         $pay->total = $total;
         $pay->out_trade_no = $tag;
         $pay->yunfee = $trans;
         $pay->coupon = $defee;
         $pay->uid = $uid;
         $pay->addressid = $senderid;
         $pay->create_time = NOW_TIME;
         $pay->type = 2;
         //货到付款
         $pay->status = 2;
         $pay->add();
         //发送邮件
         $mail = get_email($uid);
         //获取会员邮箱
         $title = "交易提醒";
         $content = "您在<a href=\"" . C('DAMAIN') . "\" target='_blank'>" . C('SITENAME') . '</a>提交了订单,订单号' . $tag;
         if (C('MAIL_PASSWORD')) {
             SendMail($mail, $title, $content);
         }
         //保存日志
         addUserLog('货到付款订单已提交', $uid);
         $this->meta_title = '提交成功';
         $this->display('Shopcart/success');
     }
     //支付类型PayType,2-在线支付
     if ($_POST["PayType"] == "2") {
         //创建订单
         $data['ispay'] = "1";
         $data['status'] = "-1";
         //待支付
         //根据订单id保存对应的费用数据
         $orderid = $order->add($data);
         M("shoplist")->where("tag='{$tag}'")->setField('orderid', $orderid);
         //发起支付时thinkpay会自动创建
         //      $pay=M("pay");
         //      $pay->create();
         //      $pay->money=$all;
         //      $pay->ratio=$ratio;
         //      $pay->total=$total;
         //      $pay->out_trade_no=$tag;
         //      $pay->yunfee=$trans;
         //      $pay->coupon=$deccode;
         //      $pay->uid=$uid;
         //      $pay->addressid=$senderid;
         //        $pay->create_time=NOW_TIME;
         //        $pay->type=1;//在线支付
         //      $pay->status=-1;//待支付
         //      $pay->add();
         //记录日志
         addUserLog('在线支付订单已提交', $uid);
         $this->meta_title = '订单支付';
         //输出支付订单号和支付金额
         $this->assign('codeid', $tag);
         $this->assign('goodprice', $all);
         //支付页
         $this->display('Pay/index');
     }
 }
 public function createorder()
 {
     if (!is_login()) {
         $this->error("您还没有登陆", U("User/login"));
     }
     /* 菜单调用*/
     $menu = R('index/menulist');
     $this->assign('categoryq', $menu);
     /* 购物车调用*/
     $cart = R("shopcart/usercart");
     $this->assign('usercart', $cart);
     if (!session('user_auth')) {
         $usercart = $_SESSION['cart'];
         $this->assign('usercart', $usercart);
     }
     /* 底部分类调用*/
     $menulist = R('Service/AllMenu');
     $this->assign('footermenu', $menulist);
     /* 热词调用*/
     $hotsearch = R("Index/getHotsearch");
     $this->assign('hotsearch', $hotsearch);
     $order = D("order");
     $tag = htmlspecialchars($_POST["tag"]);
     $value = $order->where("tag='{$tag}'")->getField('id');
     isset($value) && $this->error('重复提交订单');
     //获取会员uid
     $uid = D("member")->uid();
     //根据订单id获取购物清单
     $del = M("shoplist")->where("tag='{$tag}'")->select();
     //遍历购物清单,删除登录用户购物车中的货物id
     foreach ($del as $k => $val) {
         //获取购物清单数据表产品id,字段goodid
         $delbyid = $val["goodid"];
         //删除购物车中用户的产品id
         M("shopcart")->where("goodid='{$delbyid}'and uid='{$uid}'")->delete();
     }
     //计算提交的订单的商品总额
     $total = $this->getPricetotal($tag);
     //计算提交的订单的商品运费
     if ($total < C('LOWWEST')) {
         $trans = C('SHIPMONEY');
     } else {
         $trans = 0;
     }
     //计算提交的积分兑换
     if (htmlspecialchars($_POST["score"])) {
         $score = htmlspecialchars($_POST["score"]);
         //读取配置,1000积分兑换1元
         $ratio = $score / C('RATIO');
         $data['score'] = $score;
         $user = session('user_auth');
         $uid = D("member")->uid();
         M("member")->where("uid='{$uid}'")->setDec('score', $score);
     } else {
         $ratio = 0;
     }
     //计算提交的优惠券
     $code = htmlspecialchars($_POST["couponcode"]);
     //计算提交的订单的费用(含运费)
     $xfee = $total + $trans - $ratio;
     //计算优惠券可使用的金额,home/common/function
     $decfee = get_fcoupon_fee($code, $xfee);
     $data['codeid'] = $code;
     $data['codemoney'] = $decfee;
     $senderid = htmlspecialchars($_POST["sender"]);
     $data['addressid'] = $senderid;
     $data['total'] = $total;
     $data['create_time'] = NOW_TIME;
     $data['shipprice'] = $trans;
     //计算提交的订单的总费用
     $all = $total + $trans - $ratio - $decfee;
     $data['pricetotal'] = $all;
     $data['orderid'] = $tag;
     $data['tag'] = $tag;
     $data['uid'] = $uid;
     //修改订单状态为用户已提交
     if (htmlspecialchars($_POST["PayType"]) == "1") {
         $pay = M("pay");
         $pay->create();
         $pay->money = $all;
         $pay->ratio = $ratio;
         $pay->total = $total;
         $pay->out_trade_no = $tag;
         $pay->yunfee = $trans;
         $pay->coupon = $decfee;
         $pay->uid = $uid;
         $pay->ratioscore = $score;
         $pay->couponcode = $code;
         $pay->addressid = $senderid;
         $pay->create_time = NOW_TIME;
         $pay->type = 2;
         //货到付款
         $pay->status = 1;
         $pay->add();
         $data['status'] = 1;
         $data['ispay'] = -1;
         //货到付款
         $data['backinfo'] = "已提交等待发货";
         //增加取消订单
         //根据订单id保存对应的费用数据
         $orderid = $order->add($data);
         M("shoplist")->where("tag='{$tag}'")->setField('orderid', $orderid);
         $this->assign('codeid', $tag);
         $mail = get_email($uid);
         //获取会员邮箱
         $title = "交易提醒";
         $content = "您在<a href=\"" . C('DAMAIN') . "\" target='_blank'>" . C('SITENAME') . '</a>提交了订单,订单号' . $tag;
         if (C('MAIL_PASSWORD')) {
             SendMail($mail, $title, $content);
         }
         $this->meta_title = '提交成功';
         $this->display('success');
     }
     if (htmlspecialchars($_POST["PayType"]) == "2") {
         //设置订单状态为用户为未能完成,不删除数据
         $data['backinfo'] = "等待支付";
         $data['ispay'] = "1";
         $data['status'] = "-1";
         //待支付
         //根据订单id保存对应的费用数据
         $orderid = $order->add($data);
         M("shoplist")->where("tag='{$tag}'")->setField('orderid', $orderid);
         $pay = M("pay");
         $pay->create();
         $pay->money = $all;
         $pay->ratio = $ratio;
         $pay->total = $total;
         $pay->out_trade_no = $tag;
         $pay->yunfee = $trans;
         $pay->coupon = $decfee;
         $pay->uid = $uid;
         $pay->ratioscore = $score;
         $pay->couponcode = $code;
         $pay->addressid = $senderid;
         $pay->create_time = NOW_TIME;
         $pay->type = 1;
         //在线支付
         $pay->status = 1;
         //待支付
         $pay->add();
         $this->meta_title = '订单支付';
         $this->assign('codeid', $tag);
         $this->assign('goodprice', $all);
         //支付页
         $this->display('Pay/index');
     }
 }
 /**
  * 产生order  产生支付 pay
  */
 public function createorder()
 {
     $uid = $this->login();
     /* 热词调用*/
     $hotsearch = C('HOT_SEARCH');
     $this->assign('hotsearch', $hotsearch);
     $order = D("order");
     $tag = $_POST["tag"];
     $value = $order->where(array("tag" => $tag, 'uid' => $uid))->getField('id');
     isset($value) && $this->error('重复提交订单');
     /****计算提交的订单的商品总额 并获取商品的购物车id(sort)**/
     $shoplistInfo = $this->getPricetotal($tag, $uid);
     $total = $shoplistInfo['total'];
     //计算提交的订单的商品运费
     if ($total < C('LOWWEST')) {
         $trans = C('SHIPMONEY');
     } else {
         $trans = 0;
     }
     //计算提交的积分兑换  并减少用户的积分
     if ($_POST["score"]) {
         $score = $_POST["score"];
         //读取配置,1000积分兑换1元
         $ratio = ceil($score / C('RATIO'));
         $data['score'] = $score;
         M("ucenter_member")->where("id='{$uid}'")->setDec('score', $score);
     } else {
         $ratio = 0;
         //积分抵消的金额
         $score = 0;
         //积分
     }
     //计算提交的优惠券
     $code = I('post.couponcode');
     //计算提交的订单的费用(含运费)
     $xfee = $total + $trans - $ratio;
     //计算优惠券可使用的金额,home/common/function
     $decfee = get_fcoupon_fee($code, $xfee);
     $data['codeid'] = $code;
     $data['codemoney'] = $decfee;
     $senderid = I('post.sender');
     $data['addressid'] = $senderid;
     $data['total'] = $total;
     $data['create_time'] = NOW_TIME;
     $data['shipprice'] = $trans;
     //运费
     //计算提交的订单的总费用
     $all = $total + $trans - $ratio - $decfee;
     $data['pricetotal'] = $all;
     $data['orderid'] = $tag;
     $data['tag'] = $tag;
     $data['uid'] = $uid;
     //设置订单状态
     if ($_POST["PayType"] == "1") {
         //产生订单orderlist
         $data['status'] = 1;
         //待发货
         $data['ispay'] = -1;
         //货到付款未完成
         $data['backinfo'] = "已提交等待发货";
     } else {
         if ($_POST["PayType"] == "2") {
             //产生订单orderlist
             $data['backinfo'] = "等待支付";
             $data['status'] = -1;
             //待支付
             $data['ispay'] = -2;
             //在线支付未完成
         }
     }
     /**根据订单tag 保存对应的费用数据  并清空该物品购物车**/
     $this->saveShopListOrderStatus($tag, $data, $shoplistInfo['cart_sort']);
     $this->assign('codeid', $tag);
     $this->assign('goodprice', $all);
     //保存对应的费用数据
     $pay = M("pay");
     $pay->create();
     $pay->money = $all;
     $pay->ratio = $ratio;
     //积分抵消金额
     $pay->ratioscore = $score;
     //消耗积分
     $pay->total = $total;
     $pay->out_trade_no = $tag;
     $pay->yunfee = $trans;
     $pay->coupon = $decfee;
     //优惠卷抵消的金额
     $pay->couponcode = $code;
     //优惠卷代码
     $pay->uid = $uid;
     $pay->addressid = $senderid;
     $pay->create_time = NOW_TIME;
     if ($_POST['PayType'] == 1) {
         $pay->type = 1;
         //货到付款
         $pay->status = 1;
         //待支付
     } else {
         if ($_POST['PayType'] == 2) {
             $pay->type = 2;
             //在线支付
             $pay->status = 1;
             //待支付
         }
     }
     $pay->add();
     //邮件通知
     $mail = get_email($uid);
     //获取会员邮箱
     $title = "交易提醒";
     $content = "您在<a href=\"" . C('DAMAIN') . "\" target='_blank'>" . C('SITENAME') . '</a>提交了订单,订单号' . $tag;
     if (C('MAIL_PASSWORD')) {
         SendMail($mail, $title, $content);
     }
     if ($_POST['PayType'] == 1) {
         $this->meta_title = '提交成功';
         $this->display('success');
     } else {
         if ($_POST['PayType'] == 2) {
             $this->meta_title = '订单支付';
             $this->display('Pay/index');
         }
     }
 }