<?php
class balanceAction extends action
{
    function __construct(){
        parent::__construct();
		$this->mod = new balanceMod();
    }

    function __destruct(){
    }

	/**
	 * Method 列表数据
	 */
    function list(){
		$where = '';
		if(!empty($_REQUEST['cycle'])){
			$on = ' AND b_cycle='. $_REQUEST['cycle'];
		}
		if($_POST['filters']){
			$groups = json_decode(htmlspecialchars_decode($_POST['filters']),true);
			$where = $this->groups2sql($groups,function($field,$op,$data)use($scheCols){
				if($field=='s_parent_name' && $op=='cn'){
					$field = 's_parent_keyword';
				}else if($field=='s_name' && $op=='cn'){
					$field = 's_keyword';
				}
				return ['field'=>$field,'op'=>$op,'data'=>$data];
			});
		}
		$data = $this->mod->list($on, $where, $_POST['page'], $_POST['rows'], $_POST['sidx'], $_POST['sord']);
		foreach($data['rows'] as $k=>$row){
			if($row['b_cycle']=='') $row['b_cycle'] = $_REQUEST['cycle'];
			$row['b_cycle'] = substr($row['b_cycle'],2,4);
			$row['b_j'] = numformat($row['b_j']);
			$row['b_d'] = numformat($row['b_d']);
			$row['b_p_balance'] = numformat($row['b_p_balance']);
			$row['b_v_balance']   = numformat($row['b_v_balance']);
			$row['b_balance']   = numformat($row['b_balance']);
			$data['rows'][$k] = $row;
		}
		echo json_encode($data, JSON_UNESCAPED_UNICODE);
	}
    function listNoCheckout(){
		$where = '';
		$cycle = $_REQUEST['cycle'];
		if($_POST['filters']){
			$groups = json_decode(htmlspecialchars_decode($_POST['filters']),true);
			$where = $this->groups2sql($groups,function($field,$op,$data)use($scheCols){
				if($field=='s_parent_name2' && $op=='cn'){
					$field = 's_parent_keyword2';
				}else if($field=='s_name2' && $op=='cn'){
					$field = 's_keyword2';
				}
				return ['field'=>$field,'op'=>$op,'data'=>$data];
			});
		}
		$data = $this->mod->listNoCheckout($cycle, $where, $_POST['page'], $_POST['rows'], $_POST['sidx'], $_POST['sord']);
		foreach($data['rows'] as $k=>$row){
			if($row['b_cycle']=='') $row['b_cycle'] = $_REQUEST['cycle'];
			$row['b_cycle'] = substr($row['b_cycle'],2,4);
			$row['v_total_j'] = numformat($row['v_total_j']);
			$row['v_total_d'] = numformat($row['v_total_d']);
			$row['p_balance'] = numformat($row['p_balance']);
			$row['v_balance'] = numformat($row['v_balance']);
			$row['balance']   = numformat($row['balance']);
			$data['rows'][$k] = $row;
		}
		echo json_encode($data, JSON_UNESCAPED_UNICODE);
	}
	/**
	 * Method 未结算的期列表
	 */
	function getNoCheckoutCycels(){
		$rows = $this->mod->getNoCheckoutCycels();
		if($rows){
			$this->exitJson(0,$rows);
		}else{
			$this->exitJson(1002,'所有凭证都已经被结算');
		}
	}
	/**
	 * Method 通过关键字取一行数据
	 */
	function getRowByKey($key){
		$row = $this->mod->getRowByKey('*',$key);
		if($row){
			$this->exitJson(0,$row);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 唯一值检测
	 */
	function onlyCheck(){
		$result = false;
		$b_id = (empty($_REQUEST['b_id'])) ? 0 : $_REQUEST['b_id'];
		$b_subject = $_REQUEST['b_subject'];
		if($b_subject!=''){
			$where = sprintf("b_id<>'%s' AND b_subject='%s'",$b_id,$b_subject);
			if($this->mod->getOne('1',$where)){
				$result = true;
			}
		}
		echo ($result) ? 'false' : 'true'; //存在不通过，不存在通过
	}
	/**
	 * Method 添加操作
	 */
	function add(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$this->mod->Begin();
		foreach($params as $row){
			$this->mod->replaceRowSelectTable('balance',$row,sprintf("b_id='%s'",$row['b_id']));
		}

		$dbOk = $this->mod->End();
		if($dbOk){
			$this->exitJson(0);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 修改操作
	 */
	function edit(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
	}

	/**
	 * Method 删除操作
	 */
	function delete(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
	}

	/**
	 * Method 结算
	 */
	function checkout(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$cycle = $params['cycle'];
		if(empty($cycle)){
			$this->exitJson(2001);
		}
		//检查是否有未审核的凭证
		$voucher = new voucherMod();
		if($row = $voucher->getRow("1",sprintf("v_cycle=%s AND v_state=0",$cycle))){
			$this->exitJson(2004,$cycle . ' 期有未审核的凭证，不能结算。');
		}
		$this->mod->Begin();
		$result = $this->mod->checkout($cycle);
		if($result==0){
			$dbOk = $this->mod->End();
		}else if($result==2004){
			$this->exitJson(2004,'借贷方金额不平,结算失败'); 
		}
		if($dbOk){
			$this->exitJson(0);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 反结算
	 */
	function backCheckout(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$cycle = $params['cycle'];
		if(empty($cycle)){
			$this->exitJson(2001);
		}
		$this->mod->Begin();
		if($this->mod->backCheckout($cycle)){
			$dbOk = $this->mod->End();
		}
		if($dbOk){
			$this->exitJson(0);
		}else{
			$this->exitJson(2000);
		}
	}

}