<?php
/**
 * 收银员中心
 * @Copyright (C) 2017 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @date 2017/7/24 上午 09:14:26
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Shop\Controller;
class CashierController extends BaseController {
	public function _initialize(){
		parent::_initialize();
		$this->shops_info=session('shops_info');
	}
    public function index(){$this->redirect('Index/index');
        $map=array();
        $map['shop_id']=$this->shops_info['shop_id'];
        $list=M('ShopsGuadan')->where($map)->field('id,username,total_price,addtime')->order('id asc')->select();
        foreach($list as $k=>$v){
            $list[$k]['addtime']=time_format($v['addtime'],'H:i:s');
        }
        $PurchaseCategory=getcache('PurchaseCategory');
        $cate=array();
        foreach($PurchaseCategory as $v){
            if($v['pid']==0 && $v['status']==1){
                $cate[$v['id']]=$v['name'];
            }
        }
        $peisong=array('0'=>'自送');
        if(C('DIS_TRIBUTE')==1){
            $map=array();
            $map['status']=1;
            $distribute=M('Distribute')->where($map)->field('code,name')->order('id')->cache(true)->select();
            foreach($distribute as $v){
                $peisong[$v['code']]=$v['name'];
            }
        }
        $call_status =0;// C('CALL_STATUS');
        $this->assign('call_status',$call_status);
        /*if($call_status==1){
            $push_info = M("PushSet")->field(true)->cache(true)->find();
            //被叫开始
            $param=array(
                'appkey'=>$push_info['appkey'],//appKey
                'timestamp'=>time().rand(100,999),//当前时间戳
                'random_str'=>randStr(20),// 20-36 长度的随机字符串
                'key'=>$push_info['master_secret'],//masterSecret
            );
            $str=http_build_query($param);
            $sign=strtoupper(MD5($str));;

            $this->assign('param',json_encode($param));
            $this->assign('sign',$sign);
            $shops = M('ShopsAccount')->where('id='.$this->shops_info["id"])->field('mobile,shops_call_pwd')->find();
            if($shops['shops_call_pwd']){
                $shops['login_pwd']=$shops['shops_call_pwd'];
            }else{
                $shops['reg_pwd']=md5(NOW_TIME);
            }
            $this->assign('shops',json_encode($shops));
            //被叫结束
            
            //通话记录
            $where=array();
            $where['shop_id'] = $this->shops_info['shop_id'];
            $where['call_type']=2;
            $where['addtime']=array('gt',strtotime(date('Y-m-d')));
            $call_total = M('CallLog')->where($where)->count();
            $this->assign('call_total',intval($call_total));
            $shops_call =$call_total>0? M('CallLog')->where($where)->field('caller,addtime')->order('id desc')->limit(20)->select():array();
            $this->assign('shops_call',$shops_call);
        }*/
        $takeout_order = C('TAKEOUT_ORDER');
        $this->assign('takeout_order',$takeout_order);
        if($takeout_order == 1){
            $shops = M('ShopsAccount')->where('id='.$this->shops_info["id"])->field('mobile,shops_call_pwd')->find();
            if($shops['shops_call_pwd']){
                $shops['login_pwd']=$shops['shops_call_pwd'];
            }else{
                $shops['reg_pwd']=md5(NOW_TIME);
            }
            $map_order['pay_status']=3;
            $date=date('Ymd',NOW_TIME);
            $timestart=strtotime("$date 00:00:00");
            $timeend=strtotime("$date 23:59:59");
            $map_order['type']=14;
            $map_order['shop_id']=$this->shops_info['shop_id'];
            $map_order['pay_time'][]=array('elt',$timeend);
            $map_order['pay_time'][]=array('egt',$timestart);
            $orderinfonum = M('OrderInfo')->where($map_order)->count();
            $this->assign('takeoutnum',$orderinfonum);
            $this->assign('shops',json_encode($shops));
        }
        //订单数量
        $map=array();
        $map['type']=array('in','6,12,14');
        $map['pay_status']=3;
        $map['shipping_status']=1;
        $map['status']=2;
        $map['shop_id']=$this->shops_info['shop_id'];
        $dingdan_count=M('OrderInfo')->where($map)->count();
        $this->assign('num_dingdan',$dingdan_count);

        //消息数量
        $map=array();
        $map['type']=2;
        $shopinfo=session('shops_info');
        $map['touid']=$shopinfo['id'];
        $map['status']=0;
        $xiaoxi_count=M("Message")->where($map)->count();
        $this->assign('num_xiaoxi',$xiaoxi_count);

        $this->assign('peisong',$peisong);
        $this->assign('cate',$cate);
        $this->assign('list',$list);
        $this->display();
    }
    public function login(){
        if($this->shops_info){
            $this->redirect('Shops/index');
        }else{
            $this->assign('shopnames',idtoname(1,'Admin','username'));
            $this->display();
        }
        
    }
    public function search_by_sn(){
        $return=array('status'=>0,'info'=>'');
        $sn=I('post.sn','','trim');
        $uid=I('post.uid','0','intval');
        if(empty($sn)){$return['info']='请填写商品编号';$this->ajaxReturn($return);}
        $is_sanhuo=0;
        $cashier_code=C('CASHIER_CODE');
        if($cashier_code && strpos('*'.$sn,$cashier_code)==1){//满足散货码情况识别为散货
            $sh_sn=$sn;$is_sanhuo=1;
            $cashier_num=C('CASHIER_CASHNUM');$cashier_num=intval($cashier_num);
            $sn=substr($sh_sn,count($cashier_code),-$cashier_num);
            $sh_weight=substr($sh_sn,-$cashier_num);$sh_weight=intval($sh_weight);
            if($sh_weight<=0){$return['info']='散货重量不正确';$this->ajaxReturn($return);}
        }
        $map=array();
        $map['g.publish_wait']=1;
        $map['g.is_delete']=0;
        $map['g.status']=1;
        $map['g.goods_type']=5;
        $map['g.goods_sn']=$sn;
        $check=M('Goods')->alias('g')->where($map)->count();
        $goods_num=0;$stock_id=0;$info=$goods_package=array();
        if(empty($check) && $is_sanhuo==0){
            unset($map['g.goods_sn']);
            $map['p.goods_sn']=$sn;
            $check=M('Goods')->alias('g')->join('__GOODS_PACKAGE__ p on g.id=p.goods_id')->where($map)->count();//查看包装表中是否存该编号
            if($check){
                $goods_package=M('Goods')->alias('g')->join('__GOODS_PACKAGE__ p on g.id=p.goods_id')
                ->field('g.id,p.ratio')->where($map)->find();
                $map['g.id']=$goods_package['id'];
            }
        }
        if(!empty($check)){unset($map['p.goods_sn']);
            $info=M('Goods')->alias('g')->where($map)
            ->field('g.id,g.goods_name,g.goods_sn,g.market_price,g.user_min_bought,g.user_max_bought,g.is_promote,g.promote_id,g.promote_start_time,g.promote_end_time,g.promote_price,g.unit_name')->find();
            
            $info['attr_price']=D('Shop/OrderInfo')->getgoodsprice($info,$uid);
            $info['goods_stock']=0;//商品库存
            $info['goods_attr']='';//属性描述
            if($goods_package){
                $info['goods_num']=$goods_package['ratio'];
            }else{
                if(empty($goods_num))$info['goods_num']=$info['user_min_bought']?$info['user_min_bought']:1;
            }
            if($is_sanhuo){//散货数量
                $info['goods_stock']=9999999;//散货无限库存
                $info['goods_num']=format_decimal($sh_weight/1000);
                $info['goods_sn']=$sh_sn;
                $info['id']=$cashier_code.$info['id'];
            }
        }
        if(empty($check)){
            $return['info']='没有找到此商品编号';$this->ajaxReturn($return);
        }
        if($is_sanhuo==0){//非散货检测
            //检测货品
            $goods_huopin=D('GoodsAttrStock')->liststock($info['id'],$this->shops_info['shop_id'],$info['goods_num']);
            if($goods_huopin['status']==0){//有货品且库存不足
                $return['info']='该商品库存不足';$this->ajaxReturn($return);   
            }
            if($goods_huopin['status']==1){//有货品 需要前台选择
                $return['status']=2;$huopinhtml='';
                foreach($goods_huopin['data'] as $vo){
                    $index=$vo['goods_id'].'-'.$vo['goods_stock'];
                    $goods_attr=attr_key2name($vo['goods_stock'],$vo['goods_id']);;
                    $huopinhtml.='<div class="foodsDesitem"><div class="foodTotle">'.
        	       '<span class="foodtitle"><em class="dian"></em>'.$goods_attr.'</span>'.
            	       '<span class="kucun" data-stock="'.$vo['goods_number'].'">'.
            	       '<em>库存 '.$vo['goods_number'].'</em>'.
            	       '<a class="stock_less" href="#">-</a>'.
            	       '<span class="ber">1</span>'.
            	       '<a class="stock_add" href="#">+</a>'.
        	       '</span>'.
        	       '<span class="fr radiobox iconfont daixuan" data-index="'.$index.'" data-name="'.$info['goods_name'].'" data-goods_sn="'.$info['goods_sn'].'" data-attr_price="'.$info['attr_price'].'" data-market_price="'.$info['market_price'].'">'.
        	       '</span></div></div>';
               }
                $html='<div class="foodTotle">'.
    				'<span class="foodtitle">'.$info['goods_name'].'</span>'.
    				'<span class="kucun"></span>'.
    				'<span class="f_price">'.$info['attr_price'].'</span>'.
    		    '</div>'.
    			'<div id="sildecont">'.$huopinhtml.'</div>';
                $return['data']=$html;
                $this->ajaxReturn($return);   
            }
            
            if($goods_huopin['status']==3){//有货品 且 只有一个有库存默认选中此商品
            
                $stock_info=$goods_huopin['data'][0];
                $stock_id=$stock_info['goods_stock'];
                $info['goods_stock']=$stock_info['goods_number'];
                $info['goods_attr']=attr_key2name($stock_id,$info['id']);;
            }
            
            //获取商品无货品库存
            if(empty($info['goods_stock']))$info['goods_stock']=D('ShopsGoodsStock')->checkstock($info['id'],$stock_id,$this->shops_info['shop_id'],$info['goods_num'],1);
            if($info['goods_stock']<$info['goods_num']){$return['info']='该商品库存不足';$this->ajaxReturn($return);}
                        
        }
                
        $info['goods_index']=$info['id'].'-';
        if($stock_id)$info['goods_index']+=$stock_id;
        $return['status']=1;
        $return['data']=$info;
        $this->ajaxReturn($return);        
    }
    //根据关键字搜索商品
    public function search_by_key(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        $catid=intval($postdata['catid']);
        $keywords=urldecode(trim($postdata['keywords']));
        $uid=intval($postdata['uid']);
        
        
        $map=array();        
        $map['status']=1;
        $map['shop_id']=$this->shops_info['shop_id']; 
        $map['goods_number']=array('gt',0);    
        $shopgoodsids=M('ShopsGoodsStock')->where($map)->cache(true)->getField('goods_id',true);
        if(empty($shopgoodsids)){
            $return['status']=2;
            $return['info']='没有查询到商品';
            $this->ajaxReturn($return);//返回查询商品为空
        }       
        
        
        $map=array();
        $map['publish_wait']=1;
        $map['is_delete']=0;
        $map['status']=1;
        $map['goods_type']=5;
        $map['id']=array('in',$shopgoodsids);
        if($catid){
            $PurchaseCategory=getcache('PurchaseCategory');
            $allson=$PurchaseCategory[$catid]['allson'];
            if(empty($allson)){
                $map['cat_id']=$catid;
            }else{
                $map['cat_id']=str_exists($allson,',')?array('in',$allson):$allson;
            }
        }
        if($keywords){
            $map['goods_sn|goods_name'] = array('like','%'.$keywords.'%');
        }
        $total=M('Goods')->where($map)->count();
        if(empty($total)){
            $return['status']=2;
            $return['info']='没有查询到商品';
            $this->ajaxReturn($return);//返回查询商品为空
        }
        $listRows=10;
        $page = new \Common\Vendor\Page($total, $listRows);
        $pageconfig='%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% %TO_PAGE%';
        $page->setConfig('theme',$pageconfig);
        $pagehtml = $page->show();$sc_length=explode('<script>',$pagehtml);$pagehtml=$sc_length[0];
        $list=M('Goods')->alias('g')->where($map)->field(true)->limit($page->firstRow.','.$listRows)->select();
       
       $shopgoodsids=format_array2select($list,'id');
       $map=array();
       $map['goods_id']=array('in',$shopgoodsids);
       $goodsstock=M('ShopsGoodsStock')->where($map)->field('goods_id,goods_stock,goods_number')->select();//根据商品获取所有库存情况
       
       $stock_ids=$stock_id_number=array();
       foreach($goodsstock as $v){
          if($v['goods_stock'])$stock_ids[]=$v['goods_id'];
          if($v['goods_number']>0)$stock_id_number[$v['goods_id']]=$v['goods_number'];
       }        
       $left = array();//左边产品
   	   $right = array();//右边产品       
       if(count($list) > 1){
            $listid=$stocklistid=array();
            foreach($list as $v){$listid[]=$v['id'];}
            $count = ceil(count($listid)/2);//产品平均数取上
			$left = array_chunk($listid, $count, true)[0];//左边产品
			$right= array_chunk($listid, $count, true)[1];//右边产品
       }else{
            $left[]=$list[0]['id'];
       }       
       $lefthtml=$righthtml='';
       foreach($list as $v){
            $v['attr_price']=D('Shop/OrderInfo')->getgoodsprice($v,$uid,true);
            if(in_array($v['id'],$stock_ids)){//有属性
                $goods_huopin=D('GoodsAttrStock')->liststock($v['id'],$this->shops_info['shop_id'],1);
                $huopinhtml='<div id="sildecont'.$v['id'].'" style="display:none;">';
                foreach($goods_huopin['data'] as $vo){
                    $index=$vo['goods_id'].'-'.$vo['goods_stock'];
                    $goods_attr=attr_key2name($vo['goods_stock'],$vo['goods_id']);;
                    $huopinhtml.='<div class="foodsDesitem"><div class="foodTotle">'.
        	       '<span class="foodtitle"><em class="dian"></em>'.$goods_attr.'</span>'.
            	       '<span class="kucun" data-stock="'.$vo['goods_number'].'">'.
            	       '<em>库存 '.$vo['goods_number'].'</em>'.
            	       '<a class="stock_less" href="#">-</a>'.
            	       '<span class="ber">1</span>'.
            	       '<a class="stock_add" href="#">+</a>'.
        	       '</span>'.
        	       '<span class="fr radiobox iconfont daixuan" data-index="'.$index.'" data-name="'.$v['goods_name'].'" data-goods_sn="'.$v['goods_sn'].'" data-attr_price="'.$v['attr_price'].'" data-market_price="'.$v['market_price'].'">'.
        	       '</span></div></div>';
                }
                $huopinhtml.='</div>';
                $thehtml='<div class="foodsDes"><div class="foodTotle">'.
    	       '<span class="foodtitle">'.$v['goods_name'].'</span>'.
               '<span class="kucun"></span>'.
               '<span class="f_price">'.$v['attr_price'].'</span>'.
    	       '<span class="fr fuxuan iconfont down" onclick="sildedown('.$v['id'].')"></span></div>'.$huopinhtml.'</div>';
              //  print_r($goods_huopin);
               // $thehtml='有属性';
            }elseif($stock_id_number[$v['id']]){//无属性
                $index=$v['id'].'-';
                $thehtml='<div class="foodsDes"><div class="foodTotle">'.
    	       '<span class="foodtitle">'.$v['goods_name'].'</span>'.
               '<span class="kucun" data-stock="'.$stock_id_number[$v['id']].'">'.
    		       '<em>库存 '.$stock_id_number[$v['id']].'</em>'.
    		       '<a class="stock_less" href="#">-</a>'.
    		       '<span class="ber">1</span>'.
    		       '<a class="stock_add" href="#">+</a>'.
    	       '</span>'.
               '<span class="f_price">'.$v['attr_price'].'</span>'.
    	       '<span class="fr radiobox iconfont daixuan" data-index="'.$index.'" data-goods_sn="'.$v['goods_sn'].'" data-market_price="'.$v['market_price'].'">'.
        	   '</span></div></div>';
            }
           if(in_array($v['id'],$left)){
                $lefthtml.= $thehtml;
           }else{
                $righthtml.= $thehtml;
           }
       }
       $html='<div class="foodsKindsLeft">'.$lefthtml.'</div><div class="foodsKindsRight">'.$righthtml.'</div>';    
       $return['status']=1;
       $return['data']=$html;
       $return['page']=$pagehtml;
       $return['totalpages']=$page->totalPages;
       $this->ajaxReturn($return);
    }
    //检查券是否有效
    public function checkquan(){
        $sn=I('sn');$type=0;
        $id=I('id','0','intval');if(!empty($id)){$sn=$id;$type=1;}
        $result= D('CouponsGet')->check($sn,1,$type);
        $result['checktype']=$type;
        $this->ajaxReturn($result);
    }
    //获取券列表
    public function quanlist(){
        $uid=I('uid');$return=array('status'=>0,'info'=>'');
        if(empty($uid))if(empty($res)){$return['info']='暂无可用优惠券';$this->ajaxReturn($return);}
        $res= D('CouponsGet')->cashier_coupons_list($uid);//print_r($res);
        if(empty($res)){$return['info']='暂无可用优惠券';$this->ajaxReturn($return);}
        $list=array();
        $coupons=array('DISCOUNT'=>'折扣券','CASH'=>'优惠券');
        foreach($res['info'] as $k=>$v){
            if(array_key_exists($v['rank_id'],$coupons)){
                $data=array();
                $data['id'] = $v['id'];
                $data['name']=$v['title'];
                $data['rank_id']=$v['rank_id'];
                $data['coupons_get']=$v['coupons_get'];
                $data['coupons_name']=$coupons[$v['rank_id']];
                $data['discount']=floatval((100-$v['discount'])/10);//折扣
                $data['reduce_cost']=$v['reduce_cost'];//满减
                $data['least_cost']=$v['least_cost'];//启用金额
                if($v['begin_timestamp'])$data['begin']=time_format($v['begin_timestamp'],'Y-m-d');
                if($v['end_timestamp'])$data['end']=time_format($v['end_timestamp'],'Y-m-d');
                $list[]=$data;
            }
        }
        if(empty($list)){$return['info']='暂无可用优惠券';$this->ajaxReturn($return);}
        $return['status']=1;
        $return['info']=$list;
        $this->ajaxReturn($return);
    }
    //挂单
    public function guadan(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        if(empty($postdata['goods_info']) || $postdata['goods_info']==',' || empty($postdata['goods_table']) || empty($postdata['total_price'])){
            $return['info']='请先添加商品';$this->ajaxReturn($return);
        }
        $data=$postdata;
        $data['goods_table']=htmlspecialchars_decode(urldecode($data['goods_table']));
        $data['shop_id']=$this->shops_info['shop_id'];
        $data['shop_loginname']=$this->shops_info['login_name'];
        $check=D('ShopsGuadan')->where($data)->count();
        if($check){
            $return['info']='请不要重复提交';$this->ajaxReturn($return);
        }
        $data['addtime']=NOW_TIME;
        $res=D('ShopsGuadan')->add($data);
        if($res){
            $info=array('id'=>$res,'username'=>$data['username'],'total_price'=>$data['total_price'],'addtime'=>time_format($data['addtime'],'H:i:s'));
            $return['status']=1;
            $return['data']=$info;
            $this->ajaxReturn($return);
        }else{
            $return['info']='挂单失败';$this->ajaxReturn($return);
        }
    }//提单
    function tidan(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        $id=intval($postdata['id']);
        if(empty($id)){
            $return['info']='参数错误';$this->ajaxReturn($return);
        }
        $map=array();
        $map['shop_id']=$this->shops_info['shop_id'];
        $map['id']=$id;
        $info=D('ShopsGuadan')->where($map)->field('uid,goods_info,goods_table')->find();
        if(empty($info)){
            $return['info']='您所提单不存在';$this->ajaxReturn($return);
        }
        $info['goods_table']=json_decode($info['goods_table'],1);
        $return['status']=1;
        $return['data']=$info;
        D('ShopsGuadan')->where($map)->delete();
        $this->ajaxReturn($return);
        
    }//删除挂单
    function del_guadan(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        $id=intval($postdata['id']);
        if(empty($id)){
            $return['info']='参数错误';$this->ajaxReturn($return);
        }
        $map=array();
        $map['shop_id']=$this->shops_info['shop_id'];
        $map['id']=$id;
        $info=D('ShopsGuadan')->where($map)->delete();
        if(empty($info)){
            $return['info']='挂单删除失败';$this->ajaxReturn($return);
        }else{
            $return['status']=1;$this->ajaxReturn($return);
        }
    }
    //查询会员信息
    function search_member(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        $uid=$postdata['uid'];
        if(empty($uid)){
            $card_no=$postdata['card_no'];
            if(empty($card_no)){
                $return['info']='参数错误';$this->ajaxReturn($return);
            }
        }
        $map=array();
        $map['status']=1;
        if($card_no)$map['username|mobile']=$card_no;
        else {$map['id']=$uid;}
        $info=D('Member')->where($map)->field('id,username,nickname,mobile,rank_id,money,usepoints,header_img')->find();
        if(empty($info) && empty($uid)){//会员卡搜索
            $map=array();
            $map['card_status']=1;
            $map['status']=1;
            $map['card_sn']=$card_no;
            $info=D('Member')->where($map)->field('id,username,nickname,mobile,rank_id,money,usepoints,header_img')->find();
        }
        if(empty($info)){
            $return['info']='没有查找到该会员';$this->ajaxReturn($return);
        }else{
            $data=array();$huodong='';$huodongnum=0;$huodongmin='';
            $data['uid']=$info['id'];
            $data['nickname']=$info['nickname']?$info['nickname']:$info['username'];if(empty($data['nickname']))$data['nickname']=$info['mobile'];
            $rank_info=idtoname($info['rank_id'],'MemberRank','name,goods_price');
            $data['rankname']=$rank_info['name'];
            if($rank_info['goods_price']<100)$data['user_discount']=floatval($rank_info['goods_price']/10).'折';
            $data['rankrate']=$rank_info['goods_price'];
            $data['money']=$info['money'];
            $data['usepoints']=$info['usepoints'];
            $data['header_img']=$info['header_img']?thumb($info['header_img'],100,100):'';
            $check_double=D('OrderInfo')->is_double_points($info['rank_id']);
            if($check_double['status']){
                $huodongnum=$check_double['info']['give_num'];
                $huodongmin=$check_double['info']['money_limit'];
                $huodong='消费';
                if($huodongmin)$huodong.='满'.floatval($huodongmin).'元 ';
                $huodong.=floatval($huodongnum).'倍积分';
            }
            $data['huodong'] =$huodong;    //活动介绍     
            $data['huodongnum'] =$huodongnum;//赠送积分倍数
            $data['huodongmin'] =$huodongmin;//最小金额
            $return['status']=1;
            $return['data']=$data;
            $this->ajaxReturn($return);
        }
    }
    //根据会员更新商品价格
    public function updategoodsprice(){        
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        if(empty($postdata['uid']) || empty($postdata['cart_list'])){$return['info']='参数错误';$this->ajaxReturn($return);}
        $cart_list=json_decode(htmlspecialchars_decode(trim($postdata['cart_list'])),true);
        if(empty($cart_list)){$return['info']='请先选择商品!';$this->ajaxReturn($return);}
        foreach($cart_list as $k=>$v){
            $karr=explode('-',$k);
            $goods_id=$karr[0];if(empty($goods_id))continue;
            $goods_attr_key=$karr[1];            
            $goodsids[]=$goods_id;
            $goods_list[$k]=array(
                'goods_id'=> $goods_id,
                'goods_number'=> $v['goods_num'],
                'goods_attr'=> $goods_attr_key,
            );
        }
        if(empty($goodsids)){$return['info']='商品无效!';$this->ajaxReturn($return);}
        $postdata['goodsids']=$goodsids;
        $postdata['goods_list']=$goods_list;
        $data=D('Shop/OrderInfo')->count_total($postdata);
        if($data['status']==0){$this->ajaxReturn($data);}
        $info=array();
        foreach($data['data'] as $k=>$v){
            $info[$k]=$v['goods_price'];
        }
        $return['status']=1;
        $return['data']=$info;
        $this->ajaxReturn($return);
    }
    //获取会员收货地址
    public function getuseraddress(){
        $return=array('status'=>0,'info'=>'');
        $uid=I('post.uid','0','intval');
        if(empty($uid)){$return['info']='参数错误!';$this->ajaxReturn($return);}
        $map=array();
        $map['uid']=$uid;
        $address=M('MemberAddress')->where($map)->field('id,consignee,mobile,province,city,district,address')->order('status desc,id asc')->select();
        if(empty($address)){$return['info']='无收货地址!';$this->ajaxReturn($return);}
        $info='';
        foreach($address as $v){
            $infoadd=$v;
            unset($infoadd['id']);            
            $area=array();
            if($v['province'])$area[]=$v['province'];if($v['city'])$area[]=$v['city'];if($v['district'])$area[]=$v['district'];
            if($area){
            $map=array();
            $map['status']=1;
            $map['id']=array('in',$area);
        $areaname=M('Area')->where($map)->field('id,pid,name')->cache(true)->select();
                $areavalue=format_array2select($areaname);
                $infoadd['pro_name']=$areavalue[$v['province']];
                $infoadd['city_name']=$areavalue[$v['city']];
                $infoadd['dis_name']=$areavalue[$v['district']];
            }$data='';
            foreach($infoadd as $k=> $val){
                $data.=' data-'.$k.'="'.$val.'"';
            }
            $content=$v['consignee'].'&nbsp;&nbsp;'.$v['mobile'].'&nbsp;&nbsp;'.$infoadd['pro_name'].$infoadd['city_name'].$infoadd['dis_name'].$v['address'];
            $info.='<option value="'.$v['id'].'"'.$data.'>'.$content.'</option>';
        }
        $return['status']=1;
        $return['info']=$info;
        $this->ajaxReturn($return);
    }
    //添加订单
    public function addorder(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        $postdata['shop_id']=$this->shops_info['shop_id'];
        $postdata['login_name']=$this->shops_info['login_name'];
        $result=D('Shop/OrderInfo')->addorder($postdata);          
        $this->ajaxReturn($result);
    }
    
    //订单退货
    public function returnorder(){
        $return=array('status'=>0,'info'=>'');
        $postdata=I('post.');
        $postdata['shop_id']=$this->shops_info['shop_id'];
        $postdata['login_name']=$this->shops_info['login_name'];
        $result=D('Shop/OrderInfo')->returnordergoods($postdata);    
        $this->ajaxReturn($result);
    }
    //根据订单搜索退货商品
    public function orderbysn(){        
        $return=array('status'=>0,'info'=>'');
        $sn=I('post.sn');
        if(empty($sn)){$return['info']='订单号错误';$this->ajaxReturn($return);}
        $map=array();
        $map['order_sn']=$sn;
        $map['type']=13;
        $map['shop_id']=$this->shops_info['shop_id'];
        $map['pay_status']=3;
        $map['is_delete']=0;
        $map['status']=3;
        $order=M('OrderInfo')->where($map)->field('id,uid,order_amount,pay_code,used_score,used_score_money,used_money,bonus_id,bonus_money,addtime')->find();
        if(empty($order)){$return['info']='订单不存在';$this->ajaxReturn($return);}
        $map=array();
        $map['order_id']=$order['id'];
        $map['shop_id']=$this->shops_info['shop_id'];
        $map['status']=array('in','2,6');
        $ordergoods=M('OrderGoods')->where($map)->field('id,goods_id,goods_name,goods_number,goods_price,goods_attr')->select();
        if(empty($ordergoods)){$return['info']='没有可退订单商品';$this->ajaxReturn($return);}
        foreach($ordergoods as $k=>$v){
            if($v['goods_attr']){
                $artr=unserialize($v['goods_attr']);unset($ordergoods[$k]['goods_attr']);
                $ordergoods[$k]['goods_attrname']=$artr['name'];
            }else $ordergoods[$k]['goods_attrname']='';
        }
        $paycodename=array('HANCHAO_XIANJIN'=>'现金支付','HANCHAO'=>'余额支付');
        $order['pay_name']=!empty($paycodename[$order['pay_code']])?$paycodename[$order['pay_code']]:idtoname($order['pay_code'],'Payment','name','code');
        $order['format_addtime']=time_format($order['addtime']);
        $return['status']=1;
        $return['order']=$order;
        $return['ordergoods']=$ordergoods;
        $this->ajaxReturn($return);        
    }
     //获取通话人信息
    public function get_info(){
        $call_status = C('CALL_STATUS');if(empty($call_status))return ;
        $mobile =I('post.mobile');
        $where=array();
        $where['mobile'] = $mobile;
        $where['status'] = 1;
        $check = M('Member')->where($where)->count();
        if($check){
            $rest = M('Member')->where($where)->field(true)->find();
            $rest['caller']=$mobile;
            $rest['level']=idtoname($rest['rank_id'],'MemberRank','name','id');
            $rest['reg_time']=time_format($rest['reg_time']);            
            $where=array();
            $where['uid'] = $rest['id'];
            $where['pay_status'] = 2;
            $last_buy = M('OrderInfo')->where($where)->order('addtime desc')->find();
            if($last_buy){
                $rest['last_buy'] =idtoname($last_buy['id'],'OrderGoods','goods_name','order_id');
                $rest['last_buy_time'] = time_format($last_buy['addtime']);
            }else {
                $rest['last_buy'] = '暂无';
                $rest['last_buy_time'] = '暂无';
            }
            $member_address = idtoname($rest['id'],'MemberAddress','address','uid');
            if($member_address) $rest['member_address'] = $member_address;
            else $rest['member_address'] = '暂无';
            $return['info'] = $rest;
        }else{
            $rest=array();
            $rest['caller']=$mobile;
            $rest['username']='游客';
            $rest['level']=$rest['usepoints']=$rest['money']=$rest['reg_time']=$rest['last_buy']=$rest['last_buy_time']=$rest['member_address']='暂无';
            $return['info'] = $rest;
        }        
        $return['status'] = 1;
        $this->ajaxReturn($return);
    }
    //修改通话密码
    public function add_shops_call_pwd(){
        $call_status = C('CALL_STATUS');if(empty($call_status))return ;
        $postdata =I('post.');
        if(empty($postdata))return;

        $map = array();
        $map['shops_call_pwd'] = '';

        $map['mobile'] = $postdata['across_user'];
        $save['shops_call_pwd'] = $postdata['password'];
        $res = M('ShopsAccount')->where($map)->save($save);
        if($res>0){
            $return['status'] = 1;
        }else{
            $return['status'] = 2;
        }
        $this->ajaxReturn($return);
    }
    //加载通话记录
    public function call_log(){
        /*$call_status = C('CALL_STATUS');if(empty($call_status))return ;
        $mobile =I('post.mobile');
        if(empty($mobile))return;
        $where=array();
        $where['caller'] = $mobile;
        $where['shop_id'] = $this->shops_info['shop_id'];
        $where['call_type']=2;
        $shops_call_addtime = M('CallLog')->where($where)->order('id desc')->getField('addtime');
        $uinfo=idtoname($mobile,'Member','username,reg_time','mobile');
        $uname=$uinfo['username'];if(empty($uname))$uname='游客';
        $regtime=$uinfo['reg_time']?time_format($uinfo['reg_time']):'暂无';
        $content = '<dd class="f2" onClick="call('.$mobile.');"><p class="clearfix"><span class="fl curr">'.$mobile.'</span><span class="fr">'.time_format($shops_call_addtime,'H:i:s').'</span></p><p><span><label>用户名：</label>'.$uname.'</span></p><p><span><label>注册时间：</label>'.$regtime.'</span></p></dd>';
        if(!empty($shops_call)){
            $where=array();
            $where['shop_id'] = $this->shops_info['shop_id'];
            $where['call_type']=2;
            $where['addtime']=array('gt',strtotime(date('Y-m-d')));
            $call_total = M('CallLog')->where($where)->count();            
            $return['call_total'] = intval($call_total);
            $return['info'] = $content;
            $return['status'] = 1;
        }else{
            $return['status'] = 2;
        }
        
        $this->ajaxReturn($return);*/
    }
    //外呼
    public function call(){/*
        $call_status = C('CALL_STATUS');if(empty($call_status))return ;
        $calles = I('post.calles');
        $where['id'] = $this->shops_info['shop_id'];
        $caller = M('Shops')->where($where)->getField('shops_call');
        $res = calls($caller,$calles);
        if(!empty($res)){
            $return['info'] = $res;
            $return['status'] = 1;
        }else{
            $return['status'] = 2;
        }
        $this->ajaxReturn($return);*/
    }
    //接听
    public function call_handle(){/*
        $call_status = C('CALL_STATUS');if(empty($call_status))return ;
        $postdata = I('post.');
        $mobile=$postdata['mobile'];
        $type=$postdata['type'];
        if($type==1){
            $save['call_status'] =1;
        }else{
            $save['call_status'] =2;
        }
        $where['caller'] = $mobile;
        $call_id = M('CallLog')->where($where)->order('addtime desc')->getField('id');

        $res = M('CallLog')->where('id='.$call_id)->save($save);
       
        if(!empty($call_id)){
            $return['info'] = $call_id;
            $return['status'] = 1;
        }else{
            $return['status'] = 2;
        }
        $this->ajaxReturn($return);*/
    }
}
?>