<?php
/** 品牌模型
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@qq.com>*
 * @Author chang <13331202938@126.com>
 */
namespace Common\Model;
use Think\Model;
class CouponsModel extends Model
{
    /**获取现金购买信息
     * @param $id券id
     * @param $uid会员id
     * @return array
     */
    public function get_index($id,$uid)
    {
        $return=array('status'=>3);
        $map=array();
        $map['id']=$id;
        $map['is_inside'] = 0;
        $coupons = M('Coupons')->where($map)->field(true)->find();
        if(empty($coupons)){
            $return['msg']="卡券信息不存在";
            return $return;
        }
        if($coupons['buy_type']==1){
            $coupons['buy_money']=floatval($coupons['buy_money']);
        }
        $where=array();
        $where['uid'] = $uid;
        $where['coupons_id'] = $id;
        $get_coupons = M('CouponsGet')->where($where)->count();
        if($coupons['quantity']<=0){
            $return['msg'] = '库存不足';
            return $return;
        }else if($get_coupons >= $coupons['get_limit']){
            $return['msg'] = '领取次数已够';
            return $return;
        }elseif (time() > $coupons['end_timestamp'] && $coupons['end_timestamp']) {
            $return['msg'] = '优惠券已过期';
            return $return;
        }
        $return['status']=1;
        $return['data']=$coupons;
        return $return;
    }
    public function count_total($data){
        $return=array('status'=>3);
        if(empty($data)){
            $return['msg']='参数错误';
            return $return;
        }
        $map=array();
        $map['id']=$data['uid'];
        $memberinfo=M("Member")->where($map)->field(true)->find();
        if(empty($memberinfo)){$return['msg']='请先登录';return $return;}
        $id=$data['id'];
        $payment_id=$data['payment_id'];
        $use_account=$data['use_account'];

        $account_money=$data['account_money'];
        $map=array();
        $map['id']=$id;
        $coupons = M('Coupons')->where($map)->field(true)->find();
        $total_price=$coupons['buy_money'];
        $pay_price=$total_price;//订单应付金额=总金额
        $pay_score=$use_score_money=$total_refund_money=0;
        if($pay_price>0 && $use_account==1 ){   //余额支付
            if($memberinfo['money']>0 && $memberinfo['money']>$pay_price){
                $account_money=$pay_price;
            }else{
                $account_money=$memberinfo['money'];
            }
            $pay_price=$pay_price-$account_money;
        }
        //网银支付
        if($payment_id>0 && $payment_id!=1){    //获取手续费
            $payment_info=idtoname($payment_id,'Payment','*');
            $counter_fee=$payment_info['fee'];
        }
        if($counter_fee>0){
            $pay_fee=$pay_price*$counter_fee/100;//手续费
            $pay_price=$pay_price+$pay_fee;
        }
        if($account_money<0){$account_money=0;}
        $return['data']=$coupons;
        $return['status']=1;
        $return['total_info']=array(
            'total_price'=>format_decimal($total_price),//商品总价
            'pay_price'=>format_decimal($pay_price),//线上支付价格
            'pay_fee'=>format_decimal($pay_fee),//手续费
            'account_money'=>format_decimal($account_money),//使用余额
            'payment_info'=>$payment_info,//使网银支付信息
            'pay_total_price'=>	format_decimal($total_price),//订单实际支付的金额=商品总价+总运费-活动折扣-红包折扣
            'order_type'=>20,//现金买券
        );
        return $return;
    }
    public function sub_order($paramdata){
        $return=array('status'=>3);
        if(empty($paramdata)){
            $return['msg']='参数错误';
            return $return;
        }
        $map=array();
        $map['id']=$paramdata['uid'];
        $memberinfo=M("Member")->where($map)->field(true)->find();
        if(empty($memberinfo)){$return['msg']='请先登录';return $return;}
        $id=$paramdata['id'];
        $payment_id=$paramdata['payment_id'];
        $use_account=$paramdata['use_account'];
        $account_money=$paramdata['account_money'];
        $pay_pwd=$paramdata['pay_pwd'];
        $source=$paramdata['source'];
        $res=$this->get_index($id,$memberinfo['id']);
        if($res['status']==3){
            $return['msg']=$res['msg'];
            return $return;
        }
        $data=$this->count_total($paramdata);
        if($data['status']==3){$return=$data;return $return;}
        if($data['total_info']['pay_price']>0 && $payment_id<=1){$return['msg']='请选择支付方式';$return['app_paydata']='app_notpay';return $return;}
        if($data['total_info']['account_money'] >0){
            $return['status']=3;
            if($pay_pwd==''){
                $return['msg']='请输入支付密码';
                return $return;
            }
            $m['id']=$memberinfo['id'];
            $userinfo=M("Member")->where($m)->field('paypassword,salt')->find();
            if($userinfo['paypassword'] == ''){
                $return['msg']="您没设置交易密码,请设置交易密码";
                $return['wxsmall']=3;
                $return['url']=getjumpurl('JUMP_3');
                return $return;
            }
            if($pay_pwd=='' || creatpwd($pay_pwd,'HANCHAO'.$userinfo['salt']) != $userinfo['paypassword']){
                $return['msg']='支付密码错误';
                return $return;
            }
        }
        //添加订单
        $order=array();
        $order['order_sn']        = createOrderNo();
        $order['type']            = $data['total_info']['order_type'];
        $order['uid']             = $memberinfo['shop_id'];
        $order['pay_code'] = idtoname($payment_id,'Payment','code');//支付方式id
        $order['payment_id'] = $payment_id;//支付方式id
        $order['pay_status'] = 1;//支付状态
        $order['goods_amount'] = $data['data']['buy_money'];//商品实际金额(减去折扣的)
        $order['bonus_id']=$data['data']['id'];
        $order['order_money'] = $data['data']['buy_money'];//商品支付总金额
        $order['used_money'] = $data['total_info']['account_money'];//使用余额
        $order['pay_amount'] = $data['total_info']['pay_price'];//线上支付金额
        $order['status'] = 1;//订单状态
        $order['shipping_status'] = 1;//配送状态
        $order['addtime'] = NOW_TIME;
        $order['source'] = $source;//来源
        $order_id=M('OrderInfo')->add($order);
        if(empty($order_id)){
            $return['msg']='添加订单失败';return $return;
        }else {
            $order_goods = array();
            $info=$data['data'];
            $order_goods['order_id'] = $order_id;
            $order_goods['goods_id'] = $info['id'];
            $order_goods['goods_name'] = $info['title'];
            $order_goods['goods_number'] = 1;
            $order_goods['goods_price'] = $info['buy_money'];
            $order_goods['addtime'] = NOW_TIME;
            $order_goods['status'] = 0;//商品的发货状态
            $order_goods_id = M('OrderGoods')->add($order_goods);
        }
        //生成支付单
        $order_ids=",".$order_id.",";
        $payment_info=array();
        $payment_info['order_id']=$order_ids;
        $payment_info['user_id']= $memberinfo['id'];
        $payment_info['type']=$data['total_info']['order_type'];
        $payment_info['order_sn']= createOrderNo();
        $payment_info['use_money']=$data['total_info']['account_money'];//余额
        $payment_info['need_pay']=$data['total_info']['pay_price'];//还需支付的金额
        $payment_info['use_score']=$data['total_info']['score'];//积分
        $payment_info['use_score_money']=$data['total_info']['use_score_money'];//积分所抵的钱
        $payment_info['order_total_price']=$data['total_info']['pay_total_price'];//订单实际支付的金额=商品总价+总运费-活动折扣-红包折扣
        $payment_info['payment_id']=$payment_id;//支付方式
        $payment_info['payment_fee']=$data['total_info']['pay_fee'];//支付手续费
        $payment_info['pay_amount']=0;//
        $payment_info['pay_code']=idtoname($payment_id,"Payment",'code');//支付code
        $payment_info['is_paid']=0;
        $payment_info['create_time']=NOW_TIME;
        $payment_info['wxsmall_formid_cache']=empty($param['formId'])?'':$param['formId'];
        //生成支付单
        $pay_log_id=M('PayLog')->add($payment_info);
        if($pay_log_id){
            //余额支付
            if($data['total_info']['account_money']>0){
                account_change($memberinfo['id'],-$data['total_info']['account_money'],1,4,'购买卡券，订单'.$order['order_sn']);
            }

            //线上支付价格
            if($data['total_info']['pay_price']>0 && $data['total_info']['payment_info']){
                $return['pay']=$pay_log_id;
                $return['status']=1;
                return $return;
            }

            //处理订单
            $pay_info=array();
            $pay_info['out_trade_no']=$payment_info['order_sn'];
            $pay_info['type']=$payment_info['type'];
            $res=D("Common/OrderInfo")->endorder($pay_info);
            if($res['status']==1){
                $return['status']=1;
                $return['data']=$order_ids;
                return $return;
            }else{
                $return['msg']=$res['msg'];
                return $return;
            }
        }else{
            $return['msg']="支付单生成失败";return $return;
        }
        return $return;
    }

}
?>