<?php
/** pad
 * 收银功能
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author liushanshan
 */
namespace Pad\Controller;
use Common\Controller\HanchaoController;
class CashierController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    /**
     * 收银首页
     * 获取挂单列表、收银设置
     */
    public function index(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');$token=I('token');
        $shop_id=I('shop_id');
        if(empty($shop_id)){$return['status']=3;$return['msg']='参数错误'; $this->ajaxReturn($return);}        
        $return['data']=$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['order_status']=array('elt',2);
        $map['shop_id']=$shop_id;
        $threedingdan_count=M('ThreeOrder')->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);
        }elseif($is_on_credit==1 && $on_credit_val>0){
            $max_on_credit=format_decimal($on_credit_val);
        }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'],//计重商品条码识别   数据表的cashier_jizhong
            'jizhong_code'=>$jizhong_config['jizhong_code'],//计重商品编码前缀  数据表的cashier_jizhongcode
            'code'=>$jizhong_config['code'],//计重商品识别码   数据表的cashier_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,
            'num_threedingdan'=>$threedingdan_count,
        );
        $call_center=C('CALL_CENTER_NAME');if(!empty($call_center))$settings['call_center']=$call_center;//呼叫中心
        $config['guadan_list']=$guadan_list;
        $config['settings']=$settings;
        return $config;
    }

    /**
     * 获取登录店铺信息
     */
    public function get_siteinfo(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $root_id=I('root_id');
        $token=I('token');
        $shop_id=I('shop_id');
        $is_change=I('is_change');
        $deviceId=I('deviceId');//设备id
        if(empty($shop_id)){$return['status']=3;$return['msg']='参数错误'; $this->ajaxReturn($return);}
        //店铺信息
        $map=$siteinfo=array();
        $map['id']=$shop_id;
        $map['status']=1;
        $shop_info=M('Shops')->where($map)->field('province,shop_type,city_id,district,name,login_name,last_login,printer_type,is_open_tone,is_open_ticket,machine_code,send_shop_id,send_price,online_type,extra_discount,is_share_money')->find();
        if(empty($shop_info)){$return['status']=3;$return['msg']='信息获取错误'; $this->ajaxReturn($return);}
        $shop_info['region']=idtoname($shop_info['province'],'Area','name').idtoname($shop_info['city_id'],'Area','name').idtoname($shop_info['district'],'Area','name');
        $siteinfo['name']=$shop_info['name'];
        if(I('is_root')==1){
            $siteinfo['account_name']=idtoname($root_id,'Admin','username');
            $last_login=$shop_info['last_login'];
        }else{
            $map=array();
            $map['id']=$sid;
            $map['shop_id']=$shop_id;
            $map['status']=1;
            $map['is_delete']=0;
            $shop_account=M('ShopsAccount')->where($map)->field('login_name,last_login')->find();
            $siteinfo['account_name']=$shop_account['login_name'];
            $last_login=$shop_account['last_login'];
        }
        $upper_time=NOW_TIME-$last_login;
        if($upper_time>=60 && $is_change != 1){
            $postdata=I('post.');
            $postdata['login_name']=$siteinfo['account_name'];
            $tongji=D('Pad/Cashier')->statistics($postdata);
            $data=array();
            $data['list']=$tongji['data'];
            $data['login_name']=$siteinfo['account_name'];
            $data['sid']=$sid;
            $data['token']=$token;
            $data['shop_id']=$shop_id;
            $data['update_pre_cash']=$data['list']['all_price'];
            $data['remarks']='';
            //D('Pad/Cashier')->add_turnover($data);//交接班存数据库
            $return['msg']='登录超时';
            $return['status']=2;
            $this->ajaxReturn($return);
        }
        if($deviceId){
            $mod=M('PrintDevice');
            $map=array();
            $map['deviceid']=$deviceId;
            $print_arr=$mod->where($map)->field(true)->find();
            if(!empty($print_arr)){
                if(!empty($print_arr['printer_type']))$shop_info['printer_type']=$print_arr['printer_type'];
                if(!empty($print_arr['machine_code']))$shop_info['machine_code']=$print_arr['machine_code'];
                if(!empty($print_arr['printer_ip']))$shop_info['printer_ip']=$print_arr['printer_ip'];
                if(!empty($print_arr['port']))$shop_info['port']=$print_arr['port'];
                $shop_info['is_open_ticket']=$print_arr['is_open_ticket'];
            }
        }else{
            if(str_exists($shop_info['printer_type'],'Jiabo')) $shop_info['printer_ip']=$shop_info['machine_code'];
        }
        if(empty($shop_info['printer_type'])) $shop_info['printer_type']='Shangmi';
        if(empty($shop_info['port'])) $shop_info['port']=9100;
        if($shop_info['printer_type']=='Jiabo' && !empty($shop_info['printer_ip'])){ $shop_info['printer_type']='Jiabo(wifi)';}
        elseif($shop_info['printer_type']=='Jiabo' && empty($shop_info['printer_ip'])){$shop_info['printer_type']='Jiabo(usb)';}
        $printer_type=explode("_", $shop_info['printer_type']);
        if(!empty($printer_type[1]))$usb_printer_type=explode(")",$printer_type[1]);
        if(!empty($usb_printer_type[0])) $shop_info['usb_print_type']=$usb_printer_type[0];
        else $shop_info['usb_print_type']=1;
        $return['data']['siteinfo']=$siteinfo;
        $return['data']['config']=$this->get_siteconfig($shop_id,$sid);
        $return['data']['shop_config']=$shop_info;
        $this->ajaxReturn($return);
    }
	
	//2018.06.20开始  lxl
	public function shop_list_arr(){
		$return=array('status'=>1,'msg'=>'');
		$shop_id=I('shop_id');
        $map=array('status'=>1,'id'=>array('neq',$shop_id));
        $shop_list = M('Shops')->where($map)->field('id,name')->select();
        $shoplist=array();
		if($shop_list){
			foreach($shop_list as $k=>$v){
				$shoplist[$v['id']]=$v;
			}
			$return['shop_list'] = $shoplist;
		}else{
			$return=array('status'=>3,'msg'=>'没有要切换的门店');
		}
		$this->ajaxReturn($return);
	}
	// 切换门店
	public function shops_change(){
		$return=array();
		$postdata = I('post.');
		if(empty($postdata)){
			$return['status'] = 3;
			$return['msg']='参数不能为空';
			$this->ajaxReturn($return);
		}
		$map=array();
		$map['shop_id'] = $postdata['shop_id'];
		$map['status'] = 1;
		$map['is_delete'] = 0;
		$shop_info = M('ShopsAccount')->where($map)->field('id,password')->find();
		if($shop_info){
			$return['status'] = 1;
			$return['sid'] = $shop_info['id'];
			$return['token'] = $shop_info['password'];
		}else{
			$return['status'] = 3;
			$return['msg']='门店id错误';
		}
    	$this->ajaxReturn($return);
	}
	//2018.06.20结束
	
    /**
     * 获取会员折扣
     */
    public function member_discount(){
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('post.');
        $result=D('Pad/Cashier')->member_discount($postdata);
        if($result['status']==1){
            $return=$result;
            $this->ajaxReturn($return);
        }
        $return['status']=3;
        $return['msg']=$result['msg'];
        $this->ajaxReturn($return);
    }

    /**
     * 会员优惠券
     */
    public function member_infor(){
        $uid=I('uid');
        //user_ranks($uid);
        $type=I('type');
        $return = D('App/Member')->member_infor($uid,$type);
        $this->ajaxReturn($return);
    }
    //检查优惠券是否有效
    public function check_code(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $shop_id=I('shop_id');
        $coupon_code=I('coupon_code');
        $coupon_type=I('code_type');
        //查询优惠券的信息
        if($coupon_type==1){
            $cash_result= D('Shop/CouponsGet')->check($coupon_code,1,0,$shop_id);
        }else{
            $cash_result= D('Shop/CouponsGet')->check($coupon_code,1,1,$shop_id);
        }
        if($cash_result['status']==1){
            $cash_info=$cash_result['info'];
            $return['data']['cash_goodsids']=$cash_info['cash_goodsids'];//适用商品
            //优惠券类型
            $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'];
        }else{
            $return['status']=3;
            $return['msg']=$cash_result['info'];
        }
        $this->ajaxReturn($return);
    }

    /**
     * 提交订单
     */
    public function add_order(){
        $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);
        }
        if(empty($result['msg'])){
            $result['msg']='支付失败';
        }
        $return=$result;
        $return['status']=3;
        $return['msg']=$result['msg'];
        $this->ajaxReturn($return);
    }

    //实时监测是否付款成功
    public function check_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 guadan(){
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('post.');
        $auth_code='cashier_guadan';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            $result = D('Pad/Cashier')->guadan($postdata);
            if ($result['status'] == 1) {
                $this->ajaxReturn($result);
            }
            $return['status'] = 3;
            $return['msg'] = $result['msg'];
        }
        $this->ajaxReturn($return);
    }
    //删除挂单
    public function del_guadan(){
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('post.');
        $id=intval($postdata['id']);
        if(empty($id)){
            $return['msg']='参数错误';$this->ajaxReturn($return);
        }
        $auth_code='cashier_guadan';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            $result = D('Pad/Cashier')->del_guadan($postdata);
            if ($result['status'] == 1) {
                $this->ajaxReturn($result);
            }
            $return['status'] = 3;
            $return['msg'] = $result['msg'];
        }
        $this->ajaxReturn($return);
    }
    //提单
    public function tidan(){
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('post.');
        $id=intval($postdata['id']);
        if(empty($id)){
            $return['info']='参数错误';$this->ajaxReturn($return);
        }
        $auth_code='cashier_guadan';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            $result = D('Pad/Cashier')->tidan($postdata);
            if ($result['status'] == 1) {
                $this->ajaxReturn($result);
            }
            $return['status'] = 3;
            $return['msg'] = $result['msg'];
        }
        $this->ajaxReturn($return);
    }

    /**
     * 获取导购员列表
     */
    public function get_sales(){
        $return=array('status'=>1,'msg'=>'');
        $token=I('token');
        $sid=I('sid');
        $shop_id=I('shop_id');
        if(empty($shop_id)){$return['status']=3; $return['msg']='参数错误'; $this->ajaxReturn($return);}
        $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();
        if(empty($sales_list)) {$return['status']=2;$return['msg']='暂无导购员'; $this->ajaxReturn($return);}
        $guide_data = C('GUIDE');
        $guid = unserialize($guide_data['value']);
        if(!empty($guid)) $return['data']['market_way']=$guid['market_way'];
        $return['data']['sales_list']=$sales_list;
        $this->ajaxReturn($return);
    }

    /**
     * 获取订单列表
     */
    public function get_order_list(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        switch($postdata['auth_code']){
            //线上
            case 'weborder_list':$auth_code='weborder_list';
                break;
            //线下
            case 'orderinfo_list':$auth_code='orderinfo_list';
                break;
        }

        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
            $data=D('Pad/Cashier')->order_list($postdata);
            if($data['status']==0){
                $return['msg']=$data['msg'];
                $this->ajaxReturn($return);
            }
            if($data['status']==2)$this->ajaxReturn($data);
            foreach($data['data']['list'] as $k => $v){
                $tag='';
                if($v['type']==11)$tag='服务';
                else if($v['type']==2)$tag='储值';
                else if($v['type']==12)$tag='套餐';
                else{
                    if($v['send_type']==1)$tag='自提';
                    if($v['send_type']==2)$tag='配送';
                    if($v['send_type']==3)$tag='快递';
                }
                $list[$k]['id']=$v['id'];
                $list[$k]['order_sn']=$v['order_sn'];
                $list[$k]['addtime']=time_format($v['addtime']);
                $list[$k]['goods_allmoney']=$v['goods_allmoney'];
                $list[$k]['order_amount']=$v['order_amount'];
                $list[$k]['tag']=$tag;
            }
            $return=array();
            $return['status']=1;
            $return['data']['list']=$list;
            $return['count']=$this->getOrdernum($postdata['shop_id']);
            $this->ajaxReturn($return);
    }

    /**
     * 获取订单内容
     */
    public function get_order_info(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('param.');
        $data=D('Pad/Cashier')->order_info($postdata);
        if($data['status']==0){
            $return['msg']=$data['msg'];
            $this->ajaxReturn($return);
        }
        if($postdata['order_id']) $operable_list=D('Pad/Cashier')->operable_list($postdata['order_id']);//获取订单可操作列表
        $return['status']=1;
        $return['data']['goods_list']=$data['data']['order_goods'];
        $return['data']['infos']=$data['data']['infos'];
        $return['data']['distribute_code']=C("DIS_TRIBUTE");
        if($operable_list['status']==1) $return['data']['operable_list']=$operable_list['data'];
        $this->ajaxReturn($return);
    }

    /**
     * 订单状态列表
     */
    public function get_order_status(){
        $return['status']=1;
        $return['data']['order_status']=array('1'=>'待备货','5'=>'待发货','3'=>'待完成','4'=>'已完成');
        $this->ajaxReturn($return);
    }

    /**
     * 订单操作按钮列表
     */
    public function order_manage(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');

        switch($postdata['auth_code']){
            //线上
            case 'weborder_nopay':$auth_code='weborder_nopay';
                break;
            case 'weborder_confirm':$auth_code='weborder_confirm';
                break;
            case 'weborder_pay':$auth_code='weborder_pay';
                break;
            case 'weborder_ordertake':$auth_code='weborder_ordertake';
                break;
            case 'weborder_send':$auth_code='weborder_send';
                break;
            case 'weborder_prepare':$auth_code='weborder_prepare';
                break;
            case 'weborder_unship':$auth_code='weborder_unship';
                break;
            case 'weborder_receive':$auth_code='weborder_receive';
                break;
            case 'weborder_unship':$auth_code='weborder_unship';
                break;
            case 'weborder_receive':$auth_code='weborder_receive';
                break;
            case 'weborder_cancel':$auth_code='weborder_cancel';
                break;
            case 'weborder_invalid':$auth_code='weborder_invalid';
                break;
            //线下
            case 'orderinfo_nopay':$auth_code='orderinfo_nopay';
                break;
            case 'orderinfo_confirm':$auth_code='orderinfo_confirm';
                break;
            case 'orderinfo_pay':$auth_code='orderinfo_pay';
                break;
            case 'orderinfo_ordertake':$auth_code='orderinfo_ordertake';
                break;
            case 'orderinfo_send':$auth_code='orderinfo_send';
                break;
            case 'orderinfo_prepare':$auth_code='orderinfo_prepare';
                break;
            case 'orderinfo_unship':$auth_code='orderinfo_unship';
                break;
            case 'orderinfo_receive':$auth_code='orderinfo_receive';
                break;
            case 'orderinfo_unship':$auth_code='orderinfo_unship';
                break;
            case 'orderinfo_receive':$auth_code='orderinfo_receive';
                break;
            case 'orderinfo_cancel':$auth_code='orderinfo_cancel';
                break;
            case 'orderinfo_invalid':$auth_code='orderinfo_invalid';
                break;
        }
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            $result=D('Pad/Cashier')->order_manage($postdata);
        }
        if($result['status']==0){$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $return['status']=1;
        $return['msg']=$result['msg'];
        $return['count']=$this->getOrdernum($postdata['shop_id']);
        $this->ajaxReturn($return);
    }
    private function getOrdernum($shop_id){
        $map=array();
        $map['type']=array('in','6,12,14');
        $map['pay_status']=3;
        $map['status']=2;
        $map['shipping_status']=1;
        $map['shop_id']=$shop_id;
        $dingdan_count=M('OrderInfo')->where($map)->count();
        return intval($dingdan_count);
    }
    /**
     * 获取快递
     */
    public function order_take(){
        $return=array('status'=>3,'msg'=>'');
        if(I('is_kuaidi')){
            $auth_code='weborder_send';
        }else{
            $auth_code='weborder_prepare';
        }
        $paramdata=I('post.');
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$paramdata);
        if($return['status']==1) {

            if(C("DIS_TRIBUTE") == 0){$return['msg']=''; $this->ajaxReturn($return);}
            $map=array();
            $map['status']=1;
            if(I('is_kuaidi')){
                $shipping_list=M('Shipping')->where('status=1')->field('code,name')->select();
                foreach($shipping_list as $k=>$v){
                    $shipping_info[$v['code']]=$v['name'];
                }
                $return['status']=1;
                $return['data']=$shipping_info;
                $this->ajaxReturn($return);
            }else {
                $distribute = M('Distribute')->field("name,code")->where($map)->cache(true)->select();
                if (empty($distribute)) {
                    $return['msg'] = '';
                    $this->ajaxReturn($return);
                }
                $return['status']=1;
                $return['data']=$distribute;
                $this->ajaxReturn($return);
            }
        }
    }

    /**
     * 发货
     */
    public function deliver_goods(){
        $return=array('status'=>3,'msg'=>'配送失败');
        $postdata=I('post.');
        if(empty($postdata['order_id'])){$return['msg']='参数错误'; $this->ajaxReturn($return);}
        $data['order_id']=$postdata['order_id'];
        $data['notice_sn']=$postdata['notice_sn'];
        $data['send_type']=$postdata['send_type'];
        $data['order_type']=idtoname($data['order_id'],'OrderInfo','type','id');
        if(I('get_type')==1) {
            $location_id=I('locaion_id');
            if(empty($location_id)){
                $return['msg']='请选择附近门店'; $this->ajaxReturn($return);
            }
            $data['take_location_id'] = I('locaion_id');
        }
        $data['type']='Shop';
        $data['admin_id']=$postdata['sid'];
        $result=D("Common/OrderInfo")->ship_goods($data);
        if($result['status']){
            if(empty($result['info']))$result['info']='配送成功';
            $return['status']=$result['status'];
            $return['msg']=$result['info'];
            $this->ajaxReturn($return);
        }else{
            if(empty($result['info']))$result['info']='配送失败';
            $return['msg']=$result['info'];
            $this->ajaxReturn($return);
        }
    }

    /**消息列表
     * @return array
     */
    public function get_message_list(){
        $return=array('status'=>1);
        $param=I('post.');
        $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 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);
    }

    /**
     * 消息详情
     */
    public function get_message_count(){
        $return=array('status'=>1);
        $param=I('post.');
        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);
    }

    /**格式化消息信息
     * @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 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 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 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 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_list(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $result=D('Pad/Cashier')->turn_over_list($postdata);
        if($result['status']==2) $this->ajaxReturn($result);
        if($result['status']==3){ $return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $return['status']=1;
        $return['data']=$result['data']['list'];
        $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.');
        /*验证权限开始*/
		// 3.卡销售打印
        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';
        }
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1){$this->ajaxReturn($ret);}
        /*验证权限结束*/
        $result= D('Pad/Cashier')->order_print_info($postdata);
        if($result['status']==0) {$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
    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]['change_time']=time_format($v['change_time']);
            if($v['change_type']==1) $list['list'][$k]['change_type']='-';
            else $list['list'][$k]['change_type']='+';
        }
        $return['data']=$list['list'];
        $this->ajaxReturn($return);
    }
    /**
     * 充值小票打印
     */
    public function print_recharge_info(){
        $return=array('status'=>3,'msg'=>'');
        $sid = I('sid');
        $token = I('token');
        $postdata=I('post.');
        $auth_code='recharge_print';
        $res=D('Pad/ShopsRoleAuth')->check_auth_code($auth_code,$postdata);
        if($res['status']!=1){$res['status']=3;$res['msg']='无打印权限';$this->ajaxReturn($res);}
        $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_peisong(){
        $return=array('status'=>1,'msg'=>'');
        $sid=I('sid');
        $token=I('token');
        $post=I('post.');
        if($post['uid']){
            $map=array();
            $map['uid']=$post['uid'];
            $address_list=M('MemberAddress')->where($map)->field(true)->select();
            if(empty($address_list)){$return['status']=3;$return['msg']='暂无数据'; $this->ajaxReturn($return);}
            foreach($address_list as $k => $v){
                $province_name=idtoname($v['province'],'Area','name');
                $city_name=idtoname($v['city'],'Area','name');
                $district_name=idtoname($v['district'],'Area','name');
                $address_list[$k]['region']=$province_name.$city_name.$district_name;
            }
            $return['data']=$address_list;
            $this->ajaxReturn($return);
        }

    }

    /**
     * 计算运费
     */
    public function count_shipping_fee(){
        $return=array('status'=>1,'msg'=>'');
        $post=I('post.');
        if(empty($post['shop_id'])) { $return['msg']='参数错误'; $return['status']=3; $this->ajaxReturn($return);}
        if(empty($post['xpoint']) || empty($post['ypoint'])) { $return['msg']='请先定位'; $return['status']=3; $this->ajaxReturn($return);}
        $map=array();
        $map['id']=$post['shop_id'];
        $shop_info=M('Shops')->where($map)->field('xpoint,ypoint')->find();
        $distance=distance($shop_info['xpoint'],$shop_info['ypoint'],$post['xpoint'],$post['ypoint']);
        $delivery=D('Common/Takeout')->get_shops_deliveryfee($post['shop_id'],0,array(),$post['all_price'],array(),1,$distance);
        if($delivery['status']==1) {$return['shipping_fee']=$delivery['delivery_price']; $this->ajaxReturn($return);}
    }
    /**
     * 退货
     */
    public function tui_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $auth_code='orderinfo_tui';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            if(empty($postdata['order_id'])){$return['msg']='参数错误'; $this->ajaxReturn($return);}
            $result=D("Pad/Cashier")->tui_goods($postdata);
            if($result['status']){
                $return['status']=$result['status'];
                $return['msg']=$result['msg'];
            }else{
                $return['msg']=$result['msg'];
            }
            $this->ajaxReturn($return);
        }

    }
    public function get_active(){//获取现有所有有效的优惠活动
        $return=D('Pad/Cashier')->get_active();
        $this->ajaxReturn($return);
    }
    //提交投诉与咨询
    public function submit_service(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $postdata['content']=$postdata['remarks'];
        if(empty($postdata['order_id']) && $postdata['type']!=1){$return['msg']='参数错误'; $this->ajaxReturn($return);}
        if(empty($postdata['content'])){$return['msg']='请输入内容，以便我们及时解决您的问题'; $this->ajaxReturn($return);}
        $data = D('Common/Member')->submit_service($postdata);
        if($data['status']){
            $return['status']=$data['status'];
            $return['msg']=$data['msg'];
        }else{
            $return['msg']=$data['msg'];
        }
        $this->ajaxReturn($return);
    }
    /**
    * 线上退货
    */
    public function xstui_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $auth_code='weborder_tui';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            if (empty($postdata['order_id'])) {
                $return['msg'] = '参数错误';
                $this->ajaxReturn($return);
            }
            $result = D('Common/OrderInfo')->check_tui($postdata);
            if ($result['status']) {
                $return['status'] = $result['status'];
                $return['msg'] = $result['msg'];
            } else {
                $return['msg'] = $result['msg'];
            }
            $this->ajaxReturn($return);
        }
    }
    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);
    }
}
