<?php
/**
 * Class voucher model
 */
class reportMod extends mod{
	function __construct(){
		parent::__construct();
    }
	/**
	 * 利润表类型
	 */
	public function profitReport($cycle,$cfg){
		$data = [];
		//取库中对应金额
		foreach($cfg as $k=>$v){
			$ids = '';
			if($v['code']!=''){
				$ids = $this->getSubjectIdsByCode($v['code']);
				$monthBalance = $this->getBalanceByIds($cycle,$ids);
				$yearBalance = $this->getYearBalanceByIds($cycle,$ids);
			}
			$cfg[$k]['ids'] = $ids;
			$cfg[$k]['monthBalance'] = $monthBalance;
			$cfg[$k]['yearBalance'] = $yearBalance;
		}
		//计算rule
		foreach($cfg as $k=>$v){
			if($v['rule']!=''){
				//月
				$rule = preg_replace('/(\d+)/',"\$cfg[$1]['monthBalance']",$v['rule']);
				eval('$val='.$rule.';');
				$cfg[$k]['monthBalance'] = $val;
				//年
				$rule = preg_replace('/(\d+)/',"\$cfg[$1]['yearBalance']",$v['rule']);
				eval('$val='.$rule.';');
				$cfg[$k]['yearBalance'] = $val;
			}
		}
		//
		$data = [];
		foreach($cfg as $k=>$v){
			$row = [
				'seq'=> $k,
				'title'=> $this->addSpace($v['indent']) . $v['title'],
				'indent'=> $v['indent'],
				'monthBalance'=> numformat($v['monthBalance']),
				'yearBalance'=> numformat($v['yearBalance']),
			];
			$data[$k-1] = $row;
		}
		$footerData=[];
		$data = [
			'page'=> $pages['page'],
			'total'=> $pages['total'],
			'records' => $pages['records'],
			"userdata"=> $footerData,
			'rows' => $data
		];
		return $data;
	}
	/**
	 * 资产负债表类型
	 */
	public function balanceSheet($cycle,$cfg){
		$data = [];
		//取库中对应金额
		foreach($cfg as $k=>$v){
			$ids = '';
			if($v['dbRule']!=null){
				$sum = 0;
				foreach($v['dbRule'] as $rule){
					$ids = $this->getSubjectIdsByCode($rule['code']);
					$calc = $rule['calc'];
					preg_match_all('/\[(.*?)\]/', $calc, $m);
					foreach($m[0] as $tag){
						if($tag=='[上期余额]'){
							$value = $this->getPrevBalanceByIds($cycle,$ids);
						}else if($tag=='[本期余额]'){
							$value = $this->getBalanceByIds($cycle,$ids);
						}else if($tag=='[本年累计]'){
							$value = $this->getYearBalanceByIds($cycle,$ids);
						}
						$calc = str_replace($tag,$value,$calc);
					}
					eval('$val='.$calc.';');
					$sum+=$val;
				}
				$cfg[$k]['monthBalance'] = $val;
			}
		}
		//计算seqRule
		foreach($cfg as $k=>$v){
			if($v['seqRule']!=null){
				//月
				$rule = preg_replace('/(\d+)/',"\$cfg[$1]['monthBalance']",$v['seqRule']);
				eval('$val='.$rule.';');
				$cfg[$k]['monthBalance'] = $val;
			}
		}
		//
		$data = [];
		foreach($cfg as $k=>$v){
			$row = [
				'seq'=> $k,
				'title'=> $this->addSpace($v['indent']) . $v['title'],
				'indent'=> $v['indent'],
				'monthBalance'=> numformat($v['monthBalance']),
			];
			$data[$k-1] = $row;
		}
		$footerData=[];
		$data = [
			'page'=> $pages['page'],
			'total'=> $pages['total'],
			'records' => $pages['records'],
			"userdata"=> $footerData,
			'rows' => $data
		];
		return $data;
	}

	//缩进处理
	public function addSpace($count){
		$result = '';
		for($i=0;$i<$count;$i++){
			$result .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		return $result;
	}
	//通过编码取编码下所有科目ID
	public function getSubjectIdsByCode($code){
		$result = 0;
		if($table = $this->getTableSelectTable('view_subject','s_id',sprintf("MID(s_code,1,%s)='%s'",strlen($code),$code))){
			$arr = array_column($table,'s_id');
			$result = implode(',', $arr);
		}
		return $result;
	}
	//最大结算周期
	public function getMaxCycle(){
		$result = 0;
		if($max = $this->getOneSelectTable('voucher_checkout','max(vc_cycle)','1')){
			$result = $max;
		}
		return $result;
	}
	//期加减计算 ,$diff 正数加，负数减
	public function calcCycle($cycle,$diff){
		$year = substr($cycle, 0, 4);
		$month = ltrim(substr($cycle, 5, 2),'0');
		$month = $month * 1 + $diff;
		if($month>12){
			$month = 1;
			$year = $year * 1 + 1;
		}
		if($month<1){
			$month = 12;
			$year = $year * 1 - 1;
		}
		if($month<10) $month = '0'.$month;
		return $year.$month;
	}
	//取ids所有的本期余额(月)
	public function getBalanceByIds($cycle,$ids){
		$result = 0;
		$maxCycle = $this->getMaxCycle();
		//取余额表
		if($cycle<=$maxCycle){ 
			$result = $this->getCheckOutBalanceByIds($cycle,$ids);
		//取凭证表
		}else{ 
			$result = $this->getVoucherBalanceByIds($cycle,$ids);
		}
		return $result;
	}
	//取ids所有的上期余额(月)
	public function getPrevBalanceByIds($cycle,$ids){
		$result = 0;
		$prevCycle = $this->calcCycle($cycle,-1);
		$maxCycle = $this->getMaxCycle();
		//取余额表
		if($prevCycle<=$maxCycle){ 
			$result = $this->getCheckOutBalanceByIds($prevCycle,$ids);
		//取凭证表
		}else{ 
			$result = $this->getCheckOutBalanceByIds($maxCycle,$ids); //最后一期结算金额
			if($row = $this->getRowSelectTable(
					'voucher',
					'SUM(v_j) j,SUM(v_d) d,v_direction direction',
					sprintf("v_cycle>%s AND v_cycle<=%s AND v_subject in(%s) AND v_state=1",$maxCycle ,$prevCycle, $ids))
			){
				//未结算金额
				$result = $result + ($row['direction']==1) ? $row['j']-$row['d'] : $row['d']-$row['j'];
			}
		}
		return $result;
	}
	//取ids所有的本年累计余额(年)
	public function getYearBalanceByIds($cycle,$ids){
		$result = 0;
		$year = substr($cycle,0,4);
		$maxCycle = $this->getMaxCycle();
		$maxYear = $year;
		if($year<$maxYear) $maxCycle = $year.'12';
		$result = $this->getCheckOutBalanceByIds($maxCycle,$ids); //最后一期结算金额
		if($row = $this->getRowSelectTable(
				'voucher',
				'SUM(v_j) j,SUM(v_d) d,v_direction direction',
				sprintf("v_year=%s AND v_cycle>%s AND v_subject in(%s) AND v_state=1",$year, $maxCycle, $ids))
		){
			//未结算金额
			$result = $result + (($row['direction']==1) ? $row['j']-$row['d'] : $row['d']-$row['j']);
		}
		return $result;
	}
	//取ids所有的已结算余额(期)
	public function getCheckOutBalanceByIds($cycle,$ids){
		$result = 0;
		if($row = $this->getRowSelectTable(
				'balance',
				'SUM(b_balance_happen) balance',
				sprintf("b_cycle=%s AND b_subject in(%s)",$cycle,$ids))
		){
			$result = $row['balance'];
		}
		return $result;
	}
	//取ids所有的凭证表余额(期)
	public function getVoucherBalanceByIds($cycle,$ids){
		$result = 0;
		if($row = $this->getRowSelectTable(
				'voucher',
				'SUM(v_j) j,SUM(v_d) d,v_direction direction',
				sprintf("v_cycle=%s AND v_subject in(%s) AND v_state=1",$cycle,$ids))
		){
			$result = ($row['direction']==1) ? $row['j']-$row['d'] : $row['d']-$row['j'];
		}
		return $result;
	}
}

