<?php
namespace Admin\Controller;
class ShopbalanceLogController extends BaseController{
	public function _initialize(){
        parent::_initialize();
    	$this->account_type=array(1=>'余额',7=>'积分');$this->assign('account_type',$this->account_type);
    	$change_types = array(0=>'增加',1=>'减少');$this->assign('change_types',$change_types);
    	$account_status=strtoarray(C('ACCOUNT_STATUS'));
		$this->assign('account_status',$account_status);
    }
	public function index(){
		$postdata=I('post.');
		$dates=I('dates');
		$shop_id=I('shop_id');
		$map['id']=$shop_id;
		$info=M('Shops')->field(true)->where($map)->find();
		$where=array();
		$where['shop_id']=$shop_id;

		if(I('type'))$where['type']=I('type');
		$keyword = $postdata['keyword'];
        if ($keyword) {
            $whereor['id'] = array('like', '%' . $keyword . '%');
            $whereor['change_desc'] = array('like', '%' . $keyword . '%');
            $whereor['_logic'] = 'or';
            $where['_complex'] = $whereor;
        }

        if($postdata['data_from'])
			$where['change_time'][] = array('egt',strtotime($postdata['data_from']));
		if($postdata['data_to']){
			$where['change_time'][] = array('elt',strtotime($postdata['data_to']));$dates=0;
		}else{
			if(in_array($dates,array(1,2,7,30))){
			    $between=quick_time($dates);
			    $where['change_time']=array('between',array($between[0],$between[1]));
			}else{
			    $dates=99;
			}
		}
		if($postdata['change_type'] === '0' || $postdata['change_type'] == 1)$where['change_type'] = array('eq',$postdata['change_type']);
		$postdata['dates']=$dates;
		$list=$this->lists('ShopbalanceLog',$where,'id desc');
		$balance = C('IS_BALANCE');
		$this->assign('balance',$balance);
		$this->assign($info);
		$this->assign('list',$list);
		$this->assign('shop_id',$shop_id);
		$this->assign('type',I('type'));
		$this->assign($postdata);
		$this->display();	
	}
	public function re_js(){
		$shop_id=I('id');
		$js_money=idtoname($shop_id,'Shops','onbalance');
		if($js_money <= 0){$this->error('暂无要结算的金额');}
        shop_balance_log($shop_id,-$js_money,3,4,'店铺结算,待结算金额减少','','');
        $res=shop_balance_log($shop_id,$js_money,1,4,'店铺结算,营业总额增加','','');
		if($res){
			action_log('do_balance', 'Admin', $res, UID);//操作日志
			$this->success('结算成功',U('ShopbalanceLog/index',array('id'=>$shop_id)));
		}else{
			$this->error('结算失败',U('ShopbalanceLog/index',array('id'=>$shop_id)));
		}
		
	
	}
	public function change_shop_money(){
		
	}
    public function add(){
    	if(IS_POST){
    		$postdata=I('post.');
			$adminid=UID;
	        if(!$postdata['shop_id'])$this->error('请先选择仓库ID');
	        if($postdata){
				if(!$postdata['change_desc'])$this->error('请填写变更理由！');
	            if($postdata['change_num']>0 || $postdata['change_num']<0){
	            	$total_revenues=$postdata['total_revenues'];
	            	$total_point=$postdata['total_point'];
	            	if((($postdata['change_type']==1) && ($postdata['change_num']<0) && ($total_revenues+$postdata['change_num']<0)))$postdata['change_num']=-$total_revenues;
	            	if((($postdata['change_type']==7) && ($postdata['change_num']<0) && ($total_point+$postdata['change_num']<0)))$postdata['change_num']=-$total_point;
	                $addid=shop_balance_log($postdata['shop_id'],$postdata['change_num'],$postdata['change_type'],1,$postdata['change_desc'],'',$adminid);
					//添加平台出入账日志
					if($postdata['change_num']>0){$change_desc='后台手动变更（平台出账）';}else{$change_desc='后台手动变更（平台入账）';}
					if($postdata['change_type']==1){
						shop_balance_log($postdata['shop_id'],-$postdata['change_num'],$postdata['change_type'],1,$change_desc,'',$adminid,false);
					}
	                if($addid){
						$this->success('变更成功',U('Shops/index?shop_id='.$postdata['shop_id']));
					}else{
						$this->error('变更失败',U('Shops/index?shop_id='.$postdata['shop_id']));
					}
	            }else{
	                $this->error('请填写变更数量',U('Shops/index?shop_id='.$postdata['shop_id']));
	            }
			}
    	}else{
	        $shop_id=I('shop_id');
	        $shop_info=idtoname($shop_id,'Shops','*','id',false);
	        $this->assign('shop_info', $shop_info);
	        $this->display();
    	}	    	
    }

}