<?php
namespace App\Controller;
use Common\Controller\HanchaoController;
class CashierController extends BaseController {
	public function _initialize(){
		parent::_initialize ();
    }
    public function cashier_list(){
        $return=array('status'=>3,'msg'=>'');
        $paramdata = I('param.');
        $list=D('ShopsAccount')->cashier_list($paramdata);
        if($list){$return['status']=1; $return['data'] = $list;}
        else{$return=array('status'=>2,'msg'=>'暂无数据');}
        $this->ajaxReturn($return);
    }
	public function login_name_sn(){
		$return = array();
		$return['status'] = 3;
		$login_name = shop_account_sn();
		// $login_name = '123';
		if($login_name){
			$return['status'] = 1;
			$return['login_name_sn'] = $login_name;
		}
		$this->ajaxReturn($return);
	}
    public function index(){
        $return=array('status'=>3,'msg'=>'');
        $paramdata = I('param.');
        $id=intval($paramdata['id']);
        if(!$id){
            $return['msg']="无法获取该收银员";
            $this->ajaxReturn($return);
        }
        $map=array();
        $map['id']=$id;
        $list=M('ShopsAccount')->where($map)->field('id,name,login_name,mobile,qq,weixin,pid,shop_id')->find();
        if($list){$return['status']=1; $return['data'] = $list;}
        else{$return=array('status'=>2,'msg'=>'暂无数据');}
        $this->ajaxReturn($return);
    }
    public function cashier_add(){
        $result=array();
        $paramdata = I('param.');
        if(empty($paramdata)){
            $result['status'] = 2;
            $result['msg']='参数不能为空';
            $this->ajaxReturn($result);
        }
		
		if($paramdata['is_type'] == 0){
			if(isset($paramdata['id']) && $paramdata['id']>0){
				$result=D('ShopsAccount')->edit_cashier($paramdata);
				$this->ajaxReturn($result);
			}
			$pid=idtoname($paramdata['sid'],'ShopsAccount','pid','id',false);
			if($pid){
				$result['status'] = 3;
				$result['msg']='没有该权限';
				$this->ajaxReturn($result);
			}
			$result=D('ShopsAccount')->add_cashier($paramdata);
			$this->ajaxReturn($result);
		}else{
			$result=D('ShopsAccount')->edit_shop_login($paramdata);
			$this->ajaxReturn($result);
		}
    }
    public function cashier_del(){
        $return=array('status'=>3,'msg'=>'');
        $paramdata = I('param.');
        $pid=idtoname($paramdata['sid'],'ShopsAccount','pid','id',false);
        if($pid){
            $result['status'] = 3;
            $result['msg']='没有该权限';
            $this->ajaxReturn($result);
        }
        $return = D('ShopsAccount')->cashier_del($paramdata['id']);
        $this->ajaxReturn($return);
    }
    /**
     * 交接班统计
     */
    public function statistics(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $data=D('Pad/Cashier')->statistics($postdata);
        if($data['status']==0){$return['msg']=$data['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($data);
    }
    /**
     * 获取交接班记录列表
     */
    public function turn_over_list(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $result=D('Pad/Cashier')->turn_over_list($postdata);
        if($result['status']==2){ $return['status']=2;$return['msg']=$result['msg'];$this->ajaxReturn($return);}
        if($result['status']==3){ $return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $return['status']=1;
        $return['data']=$result['data']['list'];
        $this->ajaxReturn($return);
    }
    public function record_list(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $uid=I('uid');
        if(empty($uid)){$return['status']=3;$return['msg']='参数错误'; $this->ajaxReturn($return);}
        $type=I('type');
        $map=array();
        $map['uid']=$uid;
        if($type==1){
            $map['type'] = 4;
        }
        if($type==2) {
            $map['type'] = 1;
        }
        if($type==3){
            $map['type'] = 3;
        }
        $list=page_list('AccountLog',$map,'id desc',true);
        if(empty($list['list'])){$return['status']=2; $return['msg']='暂无数据'; $this->ajaxReturn($return);}
        foreach($list['list'] as $k => $v){
            $list['list'][$k]=$v;
            $list['list'][$k]['change_time']=time_format($v['change_time']);
        }
        $return['data']=$list['list'];
        $this->ajaxReturn($return);
    }
    /**
     * 交接班
     */
    public function add_turnover(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $postdata['list']=json_decode(htmlspecialchars_decode($postdata['list']),1);
        $result=D('Pad/Cashier')->add_turnover($postdata);
        if($result['status']==0){$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $return['status']=1; $return['msg']=$result['msg'];
        $this->ajaxReturn($return);
    }
     /**
     * 交接班详情
     */
    public function turn_over_content(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $result=D('Pad/Cashier')->turn_over_content($postdata);
        if($result['status']==0){$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $return['status']=1;
        $return['data']=$result['data'];
        $this->ajaxReturn($return);
    }
    /**
     * 收银打印
     */
    public function get_print_info(){
        $return=array('status'=>3,'msg'=>'');
        $sid = I('sid');
        $token = I('token');
        $postdata=I('post.');
        /*验证权限开始*/
        if($postdata['order_print_type']==1){
            $auth_code='orderinfo_print';
        }else if($postdata['order_print_type']==3){
            $auth_code='main-giftcards_buy';
        }else {
            $auth_code = 'weborder_print';
        }
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']!=1){$this->ajaxReturn($return);}
        /*验证权限结束*/
        $result= D('Pad/Cashier')->order_print_info($postdata);
        if($result['status']==0) {$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
    /**
     * 交接班打印
     */
    public function exchange_print_info(){
        $return=array('status'=>1);
        $param=I('param.');
        $result=D('Pad/Cashier')->exchange_print_info($param);
        if($result['status']==0){$return['status']=3; $return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
	/**
     * 充值小票打印
     */
    public function print_recharge_info(){
        $return=array('status'=>3,'msg'=>'');
        $sid = I('sid');
        $token = I('token');
        $postdata=I('post.');
        $result= D('Pad/Cashier')->print_recharge_info($postdata);
        if($result['status']==0) {$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
    /**
     * 获取收银设置
     */
    public function get_shop_config(){
        $return=array('status'=>1,'msg'=>'');
        $shop_id=I('shop_id');
        $sid=I('sid');
        $deviceId=I('deviceId');//设备id
        $map=array();
        $map['id']=$shop_id;
        $map['status']=1;
        $shop_config=M('Shops')->where($map)->field('printer_type,is_open_tone,is_open_ticket,machine_code,send_shop_id,send_price,extra_discount,is_share_money,online_type')->find();
        if($deviceId){
            $mod=M('PrintDevice');
            $map=array();
            $map['deviceid']=$deviceId;
            $check=$mod->where($map)->count("id");
            if($check) {
                $print_arr=array();
                $print_arr=$mod->where($map)->field(true)->find();
                if(!empty($print_arr)){
                    if(!empty($print_arr['printer_type']))$shop_config['printer_type']=$print_arr['printer_type'];
                    if(!empty($print_arr['machine_code']))$shop_config['machine_code']=$print_arr['machine_code'];
                    if(!empty($print_arr['printer_ip']))$shop_config['printer_ip']=$print_arr['printer_ip'];
                    if(!empty($print_arr['port']))$shop_config['port']=$print_arr['port'];
                    $shop_config['is_open_ticket']=$print_arr['is_open_ticket'];
                }
            }
        }else{
            if(str_exists($shop_config['printer_type'],'Jiabo')) $shop_config['printer_ip']=$shop_config['machine_code'];
        }
        if(empty($shop_config['printer_type'])) $shop_config['printer_type']='Shangmi';
        if(empty($shop_config['port'])) $shop_config['port']=9100;
        if($shop_config['printer_type']=='Jiabo' && !empty($shop_config['printer_ip'])){ $shop_config['printer_type']='Jiabo(wangkou)';}
        elseif($shop_config['printer_type']=='Jiabo' && empty($shop_config['printer_ip'])){$shop_config['printer_type']='Jiabo(usb)';}
        $printer_type=explode("_", $shop_config['printer_type']);
        if(!empty($printer_type[1]))$usb_printer_type=explode(")",$printer_type[1]);
        if(!empty($usb_printer_type[0])) $shop_config['usb_print_type']=$usb_printer_type[0];
        else $shop_config['usb_print_type']=1;
        $return['data']['shop_config']=$shop_config;
        $return['data']['config']=$this->get_siteconfig($shop_id,$sid);
        $this->ajaxReturn($return);
    }
    private function get_siteconfig($shop_id,$sid){
        //挂单列表
        /*$map=array();
        $map['shop_id']=$shop_id;
        $guadan_list=M('ShopsGuadan')->where($map)->field('id,username,total_price,addtime')->order('id asc')->select();
        foreach($guadan_list as $k=>$v){
            $guadan_list[$k]['addtime']=time_format($v['addtime'],'H:i:s');
        }*/
        $site_id=C('appid');//站点ID
        $weixin_open = new \Common\Vendor\Weixinopen;
        $weixin_config=$weixin_open->wxconfig($site_id);
        if(!empty($weixin_config)){
            $shop='shop'.$shop_id;
            $tg_img_url = U('Home/Qrcode/wxqrcode',array('id'=>$shop,'sp'=>1),true,true);

            // lxl 2018.7.20 start
            $cz ='cz'.$shop_id;
            $cz_img_url = U('Home/Qrcode/wxqrcode',array('id'=>$cz,'sp'=>1),true,true);
            // lxl 2018.7.20 end
        }else {
            $url = U('Mobile/Login/register', array('site_id' => $site_id), true, true);
            $tg_img_url = U('Home/Qrcode/index', array('data' => urlencode($url)), true, true);

            // lxl 2018.7.20 start
            $cz_url = U('Mobile/Withdrawals/index',array('site_id'=>$site_id),true,true);
            $cz_img_url = U('Home/Qrcode/index',array('data'=>urlencode($cz_url)),true,true);
            // lxl 2018.7.20 end
        }
        // lxl 2018.7.20 start
        // $url = U('Mobile/Withdrawals/index',array('site_id'=>$site_id),true,true);
        // $cz_img_url = U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
        // lxl 2018.7.20 end

        //导购员
        $map=array();
        $map['s.shop_id']=$shop_id;
        $map['s.status']=1;
        $map['s.is_delete']=0;
        $map['a.role_id']=2;
        $sales_list=M('ShopsRoleAccount')->alias('a')->where($map)->join('LEFT JOIN __SHOPS_ACCOUNT__ s ON a.account_id=s.id')->field('s.id,s.name')->select();
        $guid = unserialize(C('GUIDE'));
        if(!empty($guid)) $market_way=$guid['market_way'];

        //消息数量
        $map=array();
        $map['type']=2;
        $map['touid']=$sid;
        $map['status']=0;
        $xiaoxi_count=M("Message")->where($map)->count();
        //订单数量
        $map=array();
        $map['type']=array('in','6,12,14');
        $map['pay_status']=3;
        $map['shipping_status']=1;
        $map['status']=2;
        $map['shop_id']=$shop_id;
        $dingdan_count=M('OrderInfo')->where($map)->count();
        $map=array();
        $map['id']=$shop_id;
        $print_type=M('Shops')->where($map)->getField('printer_type');
        if(empty($print_type)) $print_type='Shangmi';
        $is_on_credit=C('IS_ON_CREDIT');
        $on_credit_val=C('MAX_ON_CREDIT');
        if($is_on_credit==1 && $on_credit_val==0) $max_on_credit=format_decimal(20000000000);
        else $max_on_credit=format_decimal(0);
        $chuzhi=C('RECHARGE_OPEN');if(empty($chuzhi) && !is_numeric($chuzhi))$chuzhi=1;
        $jizhong_config=D('Common/InvoicingOrder')->get_jizhong($shop_id);
        //收银设置
        $settings=array(
            'is_chuzhi'=>$chuzhi,//是否开启储值
            'is_jinshou'=>C('SHOUYIN_JINSHOU'),//库存小于0禁售
            'is_gaijia'=>C('SHOUYIN_GAIJIA'),//门店改价
            'is_youhui'=>C('SHOUYIN_YOUHUI'),//门店自定义优惠
            'max_youhui'=>C('SHOUYIN_MAXYOUHUI'),//最大优惠
            'moling'=>C('SHOUYIN_MOLING'),//四舍五入抹零
            'jizhong'=>$jizhong_config['jizhong'],//计重商品条码识别
            'jizhong_code'=>$jizhong_config['jizhong_code'],//计重商品编码前缀
            'code'=>$jizhong_config['code'],//计重商品识别码
            'sell_rate'=>C("SALES_GAIN_SELL_INTEGRAL_RATE"),//商品赠送积分比例
            'start_price'=>C('STARTING_PRICE'),//jxr 订货起订价
            'tg_img_url'=>$tg_img_url,//商品赠送积分比例
            'cz_img_url'=>$cz_img_url,//商品赠送积分比例
            'is_on_credit'=>$is_on_credit,//是否开启赊账
            'max_on_credit'=>$max_on_credit,//最大赊账金额
            'is_open_pwd'=>C('OPEN_CASHIER_PAYPSW'),//是否开启支付密码
            'login_time'=>NOW_TIME,//登录时间
            'market_way'=>$market_way,
            'sales_list'=>$sales_list,
            'num_xiaoxi'=>$xiaoxi_count,
            'num_dingdan'=>$dingdan_count,
            'print_type'=>$print_type,
        );
        //$config['guadan_list']=$guadan_list;
        $config['settings']=$settings;
        return $config;
    }    
    /**消息列表
     * @return array
     */
    public function get_message_list(){
        $return=array('status'=>1);
        $param=I('request.');
        $auth_code='message_list';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$param);
        if($ret['status']!=1) $this->ajaxReturn($ret);
        if(empty($param['shop_id'])){
            $return['status']=3;
            $return['msg']='参数错误';
            $this->ajaxReturn($return);
        }
        $page=$param['page'];
        $map=array();
        $map['touid']=$param['sid'];
		$map['type']=2;
        $list=page_list("Message",$map,"status asc,id desc",true);
        if($list && $list['list']){
            foreach($list['list'] as $key=>$val){
                $list['list'][$key]=$this->format_msg_info($val);
            }
            $return['data']=$list['list'];
        }else{
            $return['status']=2;
            $return['msg']="暂无数据";
        }
        $this->ajaxReturn($return);
    }   
     /**
     * 消息详情
     */
    public function get_message_count(){
        $return=array('status'=>1);
        $param=I('request.');
        if(empty($param['shop_id']) || empty($param['id'])){
            $return['status']=3;
            $return['msg']='参数错误';
            $this->ajaxReturn($return);
        }
        $map=array();
        $map['id']=$param['id'];
        $info=M("Message")->where($map)->field(true)->find();
        if($info){
            $return['data']=$this->format_msg_info($info);
        }else{
            $return['status']=3;
            $return['msg']="没有此信息";
        }
        $this->ajaxReturn($return);
    }
	/**
     * 消息标记为已读
     */
    public function message_read(){
        $return=array('status'=>1,'msg'=>'');
        $shop_id=I('shop_id');
        $sid=I('sid');
        $id=I('id');
        $map=array();
        $map['touid']=$sid;
        $map['status']=0;
        $map['type']=2;
        $map['id']=$id;
        $data=array();
        $data['status']=1;
        $result=M('Message')->where($map)->save($data);
        unset($map['id']);
        $count=M('Message')->where($map)->count();
        $return['count']=$count;
        if(!$result){$return['msg']='标记为已读失败';$return['status']=3;$this->ajaxReturn($return);}
        $this->ajaxReturn($return);
    }
    /**格式化消息信息
     * @param $data
     */
    private function format_msg_info($data){
        if(empty($data))return false;
        if($data['type']==1){
            $map=array();
            $map['id']=$data['touid'];
            $member_info=M("Member")->where($map)->field('username,nickname,mobile')->find();
            if($member_info['username']){
                $data['username']=$member_info['username'];
            }elseif($member_info['nickname']){
                $data['username']=$member_info['nickname'];
            }elseif($member_info['mobile']){
                $data['username']=$member_info['mobile'];
            }
        }else{
            $map=array();
            $map['id']=$data['touid'];
            $shops_info=M("ShopsAccount")->where($map)->field('name,mobile')->find();
            if($shops_info['name']){
                $data['username']=$shops_info['name'];
            }elseif($shops_info['mobile']){
                $data['username']=$shops_info['mobile'];
            }
        }
        $data['addtime']=time_format($data['addtime']);
        return $data;
    }
    public function get_active(){//获取现有所有有效的优惠活动
        $return=D('Pad/Cashier')->get_active();
        $this->ajaxReturn($return);
    }
    /**
     * 退货
     */
    public function return_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $auth_code='returngoods_manage';
        $res=D('Pad/ShopsRoleAuth')->check_auth_code($auth_code,$postdata);
        if($res['status']!=1){$this->ajaxReturn($res);}
        $postdata['list']=$this->StrToparamArray($postdata['list']);
        $result=D('Pad/Cashier')->return_goods($postdata);
        if($result['status']==0){$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
    
    public function return_goods_print(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        /*验证权限开始*/
        $auth_code = 'returngoods_print';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1){$this->ajaxReturn($ret);}
        /*验证权限结束*/
        $result= D('Pad/Cashier')->return_goods_print($postdata);
        if($result['status']==0) {$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
}
?>