<?php
namespace App\Controller;
class PaymentReceivedController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    public function get_calculator(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $calculator=array(
            '0'=>array(
                '0'=>7,
                '1'=>8,
                '2'=>9,
            ),
            '1'=>array(
                '0'=>4,
                '1'=>5,
                '2'=>6,
            ),
            '2'=>array(
                '0'=>1,
                '1'=>2,
                '2'=>3,
            ),
            '3'=>array(
                '0'=>'00',
                '1'=>0,
                '2'=>'.',
            )
        );
        $return['data']=$calculator;
        $this->ajaxReturn($return);
    }
    public function add_order(){
        /*$return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $postdata=I('param.');
        $s_info = $this->get_shop_info($sid);
        if($postdata['is_root']==1){
            $postdata['login_name'] = $postdata['account_name'];
        }else {
            $postdata['sa_id'] = $sid;
            $postdata['login_name'] = $s_info['login_name'];
        }
        $postdata['shop_id'] = $s_info['shop_id'];
        $result=D('OrderInfo')->add_order($postdata);
        if($result['status']==1){
            $return['data']['order_id']=$result['order_id'];
            $return['msg']='支付成功';
            if($result['url']){
                $return['data']['url']=$result['url'];
                $return['msg']='';
            }
        }else{
            $return['status']=3;
            $return['msg']=$result['msg'];
        }
        $this->ajaxReturn($return);*/
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('post.');
        /*验证权限开始*/
        if($postdata['is_recharge']==1){
            $auth_code='recharge_order';
            $chuzhi=C('RECHARGE_OPEN');if(empty($chuzhi) && !is_numeric($chuzhi))$chuzhi=1;
            if(empty($chuzhi)){$return['status']=3;$return['msg']='暂未开放储值功能';$this->ajaxReturn($return);}
        }elseif($postdata['is_recharge']==2){
			$auth_code='main-giftcards_buy';
        }else{
            $auth_code='cashier_addorder';
        }
        $res=D('Pad/ShopsRoleAuth')->check_auth_code($auth_code,$postdata);
        if($res['status']!=1){$this->ajaxReturn($res);}
        /*验证权限结束*/
        $postdata['pay_type']=explode(',',strtr($postdata['pay_type'],array('['=>'',']'=>'')));
        if(in_array(3,$postdata['pay_type']) && in_array(4,$postdata['pay_type'])){
            $return['status']=3;
            $return['msg']='台卡支付和扫码支付不能同时使用';
            $this->ajaxReturn($return);
        }
		if($postdata['ids_list']){
			$ids_lists=$this->StrToparamArray($postdata['ids_list']);
			$ids_arr = array();
			if($ids_lists){
				foreach($ids_lists as $k => $v){
					$ids_arr[$k] = $k;
				}
			}
			$postdata['ids_list'] = $ids_arr;
		}
        if($postdata['cart_list']) $postdata['cart_list']=$this->StrToparamArray($postdata['cart_list']);
        $result=D('Pad/Cashier')->add_order($postdata);
        if($result['status']==1){
            $this->ajaxReturn($result);
        }
        $return['status']=3;
        $return['msg']=$result['msg'];
        $this->ajaxReturn($return);
    }
    private function get_shop_info($sid){
        $map=array();
        $map['id']=$sid;
        $map['status']=1;
        $shop_info=M('ShopsAccount')->where($map)->field('login_name,shop_id')->find();
        return $shop_info;
    }
    //检查优惠券是否有效
    public function check_code(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $coupon_code=I('coupon_code');
        $coupon_type=I('code_type');
        //查询优惠券的信息
        if($coupon_type==1){
            $cash_result= D('Shop/CouponsGet')->check($coupon_code,1);
        }else{
            $cash_result= D('Shop/CouponsGet')->check($coupon_code,1,1);
        }
        if($cash_result['status']==1){
            $cash_info=$cash_result['info'];
            //优惠券类型
            $return['data']['coupon_type']=$cash_info['type'];
            //优惠券id
            $return['data']['coupon_id']=$cash_info['cash_id'];
            //优惠券折扣
            $return['data']['discount']=$cash_info['discount'];
            //满足使用优惠券的最小值
            $return['data']['coupon_min']=$cash_info['min'];
            $return['data']['cash_goodsids']=$cash_info['cash_goodsids'];
        }else{
            $return['status']=3;
            $return['msg']=$cash_result['info'];
        }
        $this->ajaxReturn($return);
    }
    public function get_coupon(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $map['status']=1;
        $coupon_imgs=M('Admin')->where($map)->field('other_img,alipay_img,weixin_img')->find();
        if($coupon_imgs){
            $return['data']['coupon_imgs']=$coupon_imgs;
        }else{
            $return['status']=3;
            $return['msg']='你还没有上传收款码，请先上传';
        }
        $this->ajaxReturn($return);
    }
    //实时监测是否付款成功双乾支付
    public function check_mm_paysuccess(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $order_id=I('order_id');
        $result=D('Pad/Cashier')->check_paysuccess($order_id);
        if($result['status']==0){$return['msg']=$result['msg']; $return['status']=3; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
    //实时监测是否付款成功
    public function check_paysuccess(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        //$order_id=I('order_id');
        $order_id=30;
        $map=array();
        $map['id']=intval($order_id);
        $order_info=M('OrderInfo')->where($map)->field('pay_status,pay_code,order_sn,id')->find();
        $map=array();
        $map['order_id']=$order_info['id'];
        $order_sn=M('PayLog')->where($map)->getField('order_sn');
        $pay_code=strtoupper($order_info['pay_code']);
        if($pay_code!='MM_SCAN'){
            if($order_info['pay_status']==1){
                if($pay_code=='HANCHAO_SWEIXIN' || $pay_code=='HANCHAO_S_WEIXIN'){$note='微信';}
                if($pay_code=='HANCHAO_SALIPAY' || $pay_code=='HANCHAO_S_ALIPAY'){$note='支付宝';}
                $return['data']['info']='扫码成功，正在'.$note.'支付';
                if($pay_code=='CHINAUMSPOS'){
                    $p_map=array();
                    $p_map['status']=1;
                    $p_map['code']=ucfirst(strtolower($pay_code));
                    $payconfig=M('Payment')->where($p_map)->field(true)->find();
                    $pay = new \Common\Vendor\Pay($p_map['code'],$payconfig);
                    $pay->check();
                    $queryResult = $pay->query($order_sn);
                    // print_r($queryResult);exit;
                    if($queryResult['errCode']=='00'){
                        if($queryResult['queryResCode']=='0'){
                            $info = $pay->getInfo();
                            $result=D('App/OrderInfo')->endorder($info);
                        }
                        if($queryResult['queryResCode']=='5'){
                            $return['status']=3;
                            $return['msg']='支付失败';
                            $this->ajaxReturn($return);
                        }
                    }
                }
            }
            if($order_info['pay_status']==3){
                $return['data']['info']='支付成功';
                $return['data']['status']=1;
                $return['msg']='支付成功';
            }
        }else{
            $return['status']=3;
            $return['msg']='扫码失败';
        }
        $this->ajaxReturn($return);
    }
    public function get_orderinfo(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $order_id=I('order_id');
        $map=array();
        $map['id']=$order_id;
        $map['pay_status']=3;
        $map['shipping_status']=4;
        $order_info=M('OrderInfo')->where($map)->field('id,pay_code,type,order_amount')->find();
        if($order_info){
            $return['data']['id']=$order_info['id'];
            $return['data']['order_amount']=format_price($order_info['order_amount']);
            $pay_code=strtoupper($order_info['pay_code']);
            if($pay_code=='HANCHAO'){
                $return['data']['pay_name']='余额支付';
            }elseif($pay_code=='HANCHAO_XIANJIN'){
                $return['data']['pay_name']='现金支付';
            }elseif($pay_code=='HANCHAO_S_ALIPAY'){
                $return['data']['pay_name']='支付宝支付';
            }elseif($pay_code=='HANCHAO_S_WEIXIN'){
                $return['data']['pay_name']='微信支付';
            }elseif($pay_code=='CHINAUMS' || $pay_code=='CHINAUMSPOS'){
                $return['data']['pay_name']='银联支付';
            }else{
                $return['data']['pay_name']='其他支付';
            }
			$return['data']['type']=$order_info['type'];
        }else{
            $return['status']=2;
            $return['msg']='没有此订单信息';
        }
        $this->ajaxReturn($return);
    }
    public function print_order(){
        $sid=I('sid');
        $token=I('token');
        $order_sn=idtoname(I('get.order_id'),'OrderInfo','order_sn');
        $map=array('id'=>$sid);
        $machine_code=M('Shops')->where($map)->getField('machine_code');
        if(empty($machine_code)){$return=array('status'=>3,'msg'=>'请去“我的->设备管理”添加打印机终端号');$this->ajaxReturn($return);}
        $data=del_print_template($order_sn);
        if($data['status']==1){
            $print=doPrint($machine_code,$data['content'],$order_sn);
            $print['status']=$print['status']==0?3:$print['status'];
            $print['msg']=empty($print['msg'])?$print['info']:$print['msg'];
            $this->ajaxReturn($print);
        }else{
            $data['status']=$data['status']==0?3:$data['status'];
            $data['msg']=empty($data['msg'])?$data['info']:$data['msg'];
            $this->ajaxReturn($data);
        }
    }
}
?>