<?php
/** pad
 * 收银设置
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol
 */
namespace Pad\Controller;
use Common\Controller\HanchaoController;
class CashierSetController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    /**抹零设置开始**/
    //设置之前
    public function moling_before(){
        $return=array('status'=>1,'msg'=>'');
		
		$postdata = I('post.');
		$auth_code = 'cashierset_moling';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $map=array('name'=>'SHOUYIN_MOLING');
        $value=M('Config')->where($map)->field('extra,value')->find();
        if(empty($value)){$return['status']=0;$return['msg']='请检查后台抹零配置';$this->ajaxReturn($return);}
        $moling_set=$value['extra'];
        if(!empty($moling_set)){
            $moling_set=explode(',',$moling_set);
            $moling_config=array();
            foreach($moling_set as $val){
                $molingconfig=explode(':',$val);
                $moling_config[$molingconfig[0]]=$molingconfig[1];
            }
            $return['data']['moling_data']=$moling_config;
        }
        $return['data']['moling']=$value['value'];
        $this->ajaxReturn($return);
    }
    //设置抹零
    public function update_moling(){
        $return=array('status'=>0,'msg'=>'');
        $postdata=I('POST.');
		
		$auth_code = 'cashierset_moling';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $value=$postdata['moling'];
        $map=array('name'=>'SHOUYIN_MOLING');
        $data=array('value'=>$value);
        $result=M('Config')->where($map)->save($data);
        if($result!==false){S('DB_CONFIG_DATA',null);$return['status']=1;}else{$return['msg']='设置失败';}
        $this->ajaxReturn($return);
    }
    /**抹零设置结束**/
    /**台卡收款提示音开始**/
    //设置之前
    public function tone_before(){
        $return=array('status'=>1,'msg'=>'');
        $getdata=I('GET.');
		
		$auth_code = 'cashierset_tone';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$getdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		C('TOKEN_ON',false);
        $deviceId=$getdata['deviceId'];
        $shop_id=$getdata['current_shop_id'];if(empty($shop_id)){$return['status']=3;$this->ajaxReturn($return);}
        if($deviceId){//新版提示音
            $mod=M('PushDevice');
            $getdata['shop_id']=$shop_id;
            $getdata['push_type']=$getdata['registration_type']=='ios'?1:0;
            $map=array();
            $map['shop_id']=$shop_id;
            $map['deviceid']=$deviceId;
            $map['push_setid']=$getdata['push_setid'];
            $check=$mod->where($map)->count("id");
            $getdata['deviceid']=$deviceId;
            $getdata=$mod->create($getdata);
            if($check){
                $getdata=$mod->where($map)->field("shop_id as current_shop_id,status,is_online,is_message,is_cargo,is_threeorder,is_pay,push_setid")->find();
            }else{
                $mod->add($getdata);
            }
            $return['data']['tone_config']=$getdata;                        
        }else{//旧版编辑提示音
            $map=array('id'=>$shop_id);
            $tone=M('Shops')->where($map)->getField('note_tone');
            $return['data']['tone']=$tone;
            $cashier_tone=C('CASHIER_TONE');
            if(!empty($cashier_tone)){
                $cashier_tone=explode(',',$cashier_tone);
                $tone_config=array();
                foreach($cashier_tone as $val){
                    $toneconfig=explode(':',$val);
                    $tone_config[$toneconfig[0]]=$toneconfig[1];
                }
                $return['data']['tone_config']=$tone_config;
            }
        }
        $this->ajaxReturn($return);
    }
    //设置
    public function update_tone(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('POST.');
		
		$auth_code = 'cashierset_tone';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
		C('TOKEN_ON',false);
        $shop_id=$postdata['current_shop_id'];if(empty($shop_id)){$this->ajaxReturn($return);}
        $deviceId=$postdata['deviceId'];
        if($deviceId){//新版提示音
            $mod=M('PushDevice');
            $postdata['shop_id']=$shop_id;
            $postdata['push_type']=$postdata['registration_type']=='ios'?1:0;
            $map=array();
            $map['shop_id']=$shop_id;
            $map['deviceid']=$deviceId;
            $map['push_setid']=$postdata['push_setid'];
            $check=$mod->where($map)->count("id");
            $postdata['deviceid']=$deviceId;
            $postdata=$mod->create($postdata);
            if($check){
                unset($map['shop_id']);
                $mod->where($map)->save($postdata);
                $allsave=array();
                $allsave['is_online']=$postdata['is_online'];
                $allsave['is_message']=$postdata['is_message'];
                $allsave['is_cargo']=$postdata['is_cargo'];
                $allsave['is_threeorder']=$postdata['is_threeorder'];
                $allsave['is_pay']=$postdata['is_pay'];
                $allsave['status']=$postdata['status'];
                $map=array();
                $map['deviceid']=$deviceId;
                if($allsave)$mod->where($map)->save($allsave);
            }else{
                $mod->add($postdata);
            }
            $return['status']=1;
            //$return['msg']=$mod->_sql();
        }else{//旧版编辑提示音
            $value=$postdata['tone'];
            $map=array('id'=>$shop_id);
            $data=array('note_tone'=>$value);
            $result=M('Shops')->where($map)->save($data);
            if($result!==false){$return['status']=1;}else{$return['msg']='设置失败';}
        }
        $this->ajaxReturn($return);
    }
    /**台卡收款提示音结束**/
    /**自动打印小票开始**/
    //设置之前
    public function cash_before(){
        $return=array('status'=>1,'msg'=>'');
        $getdata=I('GET.');
        $shop_id=$getdata['current_shop_id'];
        $map=array('id'=>$shop_id);
        $cash=M('Shops')->where($map)->getField('is_open_ticket');
        $return['data']['cash']=$cash;
        $this->ajaxReturn($return);
    }
    //设置
    public function update_cash(){
        $return=array('status'=>0,'msg'=>'');
        $postdata=I('POST.');
		
		$auth_code = 'cashierset_tpl';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $shop_id=$postdata['current_shop_id'];
        $deviceId=$postdata['deviceId'];
        if($deviceId){
            $mod=M('PrintDevice');
            $map=array();
            $map['deviceid']=$deviceId;
            $check=$mod->where($map)->count("id");
            if($check){
                $data=array();
                $data['is_open_ticket'] = $postdata['cash'];
                $map=array();
                $map['deviceid']=$deviceId;
                if($data)$result=$mod->where($map)->save($data);
            }else{
                $data=array();
                $data['is_open_ticket'] = $postdata['cash'];
                $data['deviceid']=$deviceId;
                $result=$mod->add($data);
            }
        }else{
            $value=$postdata['cash'];
            $map=array('id'=>$shop_id);
            $data=array('is_open_ticket'=>$value);
            $result=M('Shops')->where($map)->save($data);
        }
        if($result!==false){$return['status']=1;}else{$return['msg']='设置失败';}
        $this->ajaxReturn($return);
    }
    /**自动打印小票结束**/
    /**打印机设置开始**/
    public function tpl_before(){
        $return=array('status'=>1);
		$postdata=I('get.');
		$auth_code = 'cashierset_tpl';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
        $shop_id=I("current_shop_id");
        $deviceId=$postdata['deviceId'];//设备id
        $map = array();
        $map['id'] = $shop_id;
        $shop_info = M("Shops")->where($map)->field("print_template,print_tpl_id")->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('print_template,print_tpl_id')->find();
                if(!empty($print_arr['print_template'])){
                    $shop_info['print_template']=$print_arr['print_template'];
                    $shop_info['print_tpl_id']=$print_arr['print_tpl_id'];
                }
            }
        }
        $map = array();
        $map['status'] = 1;
        $list = M('TplReceipt')->where($map)->field(true)->select();
        $d_list=array();
        foreach($list as $k=>$v){
            if($shop_info['print_template']){
                $set = json_decode($shop_info['print_template'],true);
            }else{
                $set = json_decode($v['set_info'],true);
            }
            $set['is_show']=$set['is_show']==1?true:false;
            $set['id']=$v['id'];
            $set['tpl_name']=$v['tpl_name'];
            $set['tpl_info']=$v['tpl_info'];
            if($v['id']==$shop_info['print_tpl_id']) $set['is_select']=1;
            else $set['is_select']=0;
            $set['status']=$v['status'];
            if(empty($set['temp'])){$set['temp'] = '欢迎下次光临！';}
            if(empty($set['description'])){$set['description'] = '扫码关注微信';}
            if(C('WEIXIN_PUBLIC_NUMBER_URL')){
                $url=C('WEIXIN_PUBLIC_NUMBER_URL');
                $set['qrcode_url']=U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
            }else{
                $wx = new \Common\Vendor\Weixinopen;
                $result=$wx->get_authorizer_info();
                if(!empty($result['authorizer_info']['user_name'])){
                    $qrcode=$wx->get_wxqrcode($result['authorizer_info']['user_name']);
                    if(!empty($qrcode) && str_exists($qrcode,'weixin.qq.com'))$url=$qrcode;
                    $set['qrcode_url']=U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
                    if(C('WEIXIN_PUBLIC_NUMBER_URL') != $url && strlen($url)<=80){
                        $map = $save = array();
                        $map['name'] = 'WEIXIN_PUBLIC_NUMBER_URL';
                        $save['value'] = $url;
                        M('Config')->where($map)->save($save);
                    }
                }
            }
            $d_list[$v['id']]=$set;
        }
        $return['list']=$d_list;
        $this->ajaxReturn($return);
    }
    public function update_tpl(){
        $return=array('status'=>1);
        $postdata=I('POST.');
		$auth_code = 'cashierset_tpl';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
        $id=$postdata['id'];
        if($id){
            $shop_id=$postdata['current_shop_id'];
            $deviceId=$postdata['deviceId'];//设备id
            $set=array();
            if(isset($postdata['is_show']) && $postdata['is_show']==1){
                $set['is_show'] = 1;
            }else{
                $set['is_show'] = 0;
            }
            if(empty($postdata['shopname'])){
                $set['shopname'] = '汉潮小店';
            }else{
                $set['shopname'] = $postdata['shopname'];
            }
            if(empty($postdata['type_id'])){
                $set['type_id'] = 1;
            }else{
                $set['type_id'] = $postdata['type_id'];
            }
            if(empty($postdata['temp'])){
                $set['temp'] = '欢迎下次光临！';
            }else{
                $set['temp'] = $postdata['temp'];
            }
            if(empty($postdata['description'])) {
                $set['description'] = '扫码关注微信';
            }else{
                $set['description'] = $postdata['description'];
            }
            if(isset($postdata['is_yue']) && $postdata['is_yue']==1){
                $set['is_yue'] = 1;
            }else{
                $set['is_yue'] = 0;
            }
            $set_info = json_encode($set);
            if($deviceId){
                $mod=M('PrintDevice');
                $map=array();
                $map['deviceid']=$deviceId;
                $check=$mod->where($map)->count("id");
                if($check){
                    $data=array();
                    $data['print_template'] = $set_info;
                    $data['print_tpl_id'] = $id;
                    $map=array();
                    $map['deviceid']=$deviceId;
                    if($data)$res=$mod->where($map)->save($data);
                }else{
                    $data=array();
                    $data['print_template'] = $set_info;
                    $data['print_tpl_id'] = $id;
                    $data['deviceid']=$deviceId;
                    $res=$mod->add($data);
                }
            }else{
                $map = array();
                $map['id'] = $shop_id;
                $map['status'] = 1;
                $data=array();
                $data['print_template'] = $set_info;
                $data['print_tpl_id'] = $id;
                $res = M('Shops')->where($map)->save($data);
            }
            if($res!==false){
                $return['status'] = 1;
                $return['msg'] = '操作成功';
            }else{
                $return['status'] = 3;
                $return['msg'] = '操作失败';
            }
        }else{
            $return['status'] = 3;
            $return['msg'] = '参数获取错误！';
        }
        $this->ajaxReturn($return);
    }
    /**打印机设置结束**/
    /**设置打印机
     * @return array
     */
    public function set_shop_prints(){
        $param=I("post.");
        $retrun=array('status'=>1);
		
		$auth_code = 'cashierset_shop_prints';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$param);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}

        if(empty($param)){
            $retrun['status']=3;
            $retrun['msg']="参数错误";
            $this->ajaxReturn($retrun);
        }
        if(empty($param['current_shop_id'])){
            $retrun['status']=3;
            $retrun['msg']="参数错误";
            $this->ajaxReturn($retrun);
        }
        $shop_id=$param['current_shop_id'];
        $deviceId=$param['deviceId'];//设备id
        if($deviceId){//新版打印设备绑定
            $mod=M('PrintDevice');
            $param['shop_id']=$shop_id;
            $map=array();
            $map['deviceid']=$deviceId;
            $check=$mod->where($map)->count("id");
            if($check){
                $allsave=array();
                $allsave['printer_type']=$param['print_type'];
                $allsave['printer_ip']=str_replace(' ','',$param['printer_ip']);
                $allsave['machine_code']=$param['value'];
                $allsave['port']=$param['port'];
                $map=array();
                $map['deviceid']=$deviceId;
                if($allsave)$mod->where($map)->save($allsave);
            }else{
                $allsave=array();
                $allsave['printer_type']=$param['print_type'];
                $allsave['printer_ip']=str_replace(' ','',$param['printer_ip']);
                $allsave['machine_code']=$param['value'];
                $allsave['port']=$param['port'];
                $allsave['deviceid']=$deviceId;
                $mod->add($allsave);
            }
            $retrun['status']=1;
            $retrun['msg']='绑定成功';
        }else {//旧版打印设备绑定
            $map = $data = array();
            $map['id'] = $param['current_shop_id'];
            $data['printer_type'] = $param['print_type'];
            $data['machine_code'] = $param['value'];
            $data['updatetime'] = NOW_TIME;
            $res = M("Shops")->where($map)->save($data);
            if ($res) {
                $retrun['msg'] = "绑定成功";
            } else {
                $retrun['status'] = 3;
                $retrun['msg'] = "绑定失败";
            }
        }
        $this->ajaxReturn($retrun);
    }
    
    //设置条码秤条码规则
    public function update_scale_sn(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('POST.');
		
		$auth_code = 'cashierset_scale';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
		
        $value=$postdata['scale_sn'];
        $map=array('id'=>$postdata['shop_id']);
        $data=array('cashier_jizhong'=>$value);
        $result=M('Shops')->where($map)->save($data);
        if($result!==false){S('ShopJizhong'.$postdata['shop_id'],null);$return['status']=1;}else{$return['msg']='设置失败';}
        $this->ajaxReturn($return);
    }
    public function set_jizhong_config(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('POST.');

        $cashier_code=$postdata['cashier_code'];
        if(empty($cashier_code)){$return['msg']='请填写计重商品识别码'; $this->ajaxReturn($return);}
        //if(!empty($cashier_code) && !preg_match("/^[a-zA-Z0-9]{2}$/",$cashier_code)){$return['msg']='商品识别码格式错误'; $this->ajaxReturn($return);}
        $map=array();
        $map['id']=$postdata['shop_id'];
        $data=array('cashier_code'=>$cashier_code);
        $result=M('Shops')->where($map)->save($data);
        if($result!==false){S('ShopJizhong'.$postdata['shop_id'],null);$return['status']=1;$return['msg']='设置成功';}else{$return['msg']='设置失败';}
        $this->ajaxReturn($return);
    }
}