<?php
Doo::loadCore ( 'db/DooModel' );
class accountBookStatistics extends DooModel {
	
	public $reusltPrice=0;
	
	public $aid;
	public $asid;
	public $inComePrice;
	public $payPrice;
	public $expensesType;
	public $financingPrice;
	public $date;
	
	public $accountType;
	public $expenditureType;
	
	public $_table = 'CLD_accountBookStatistics';
	public $_primarykey = 'asid';
	public $_fields = array (
			'aid',
			'expensesType',
			'asid',
			'inComePrice',
			'payPrice',
			'financingPrice',
			'date',
			'accountType',
			'expenditureType',
			'accountMsg',
			'expenditureMsg',
			
			
	);
	
	/**
	 * 添加相关开票操作日志
	 *
	 * @param array $item 要记录的相关发票操作数据
	 * @return integer|0 返回操作ID
	 */
	public function setAccountBookStatistics($item = array()) {
		$lid = 0;
		if (is_array ( $item ) && ! empty ( $item )) {
			foreach ( $item as $key => $value ) {
				$this->$key = $value;
			}
			$lid = $this->insert ();
		}
		return $lid;
	}
	
	public function updateAccountBookStatistics($item = array(),$aid=0){
		$lid = 0;
		if (is_array ( $item ) && ! empty ( $item )&& ! empty ( $aid )) {
			foreach ( $item as $key => $value ) {
				$this->$key = $value;
			}
			$lid = $this->update (array (
				'where' => "aid=" . $aid.' and ',
				'asArray' => TRUE 
		));
		}
		return $lid;
	}
	
	/**
	 * 
	 * @param unknown $year
	 * @param number $expensesType 汇总类型
	 */
	public function getStatisticsByCollect($year= '',$expensesType=1) {
		
		if (empty($year))
			$year=date ( "Y" );
		
		$list=array();
		if ($expensesType==1){
			$list  = $this->find ( array (
					'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Month(date) as month,accountType,accountMsg,Year(date)',
					'where' => "  Year(date) =" . $year . " and expensesType=" . $expensesType,
					'groupby' => 'Month(date),accountType,accountMsg',
					'asArray' => TRUE
			) );
		}elseif($expensesType==2){
			$list  = $this->find ( array (
					'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Month(date) as month,expenditureType,expenditureMsg,Year(date)',
					'where' => "  Year(date) =" . $year . " and expensesType=" . $expensesType,
					'groupby' => 'Month(date),expenditureType,expenditureMsg',
					'asArray' => TRUE
			) );
		}
		return $list;
	}
	
	/**
	 *
	 * @param unknown $year
	 * @param number $expensesType 汇总类型
	 */
	public function getStatisticsByCollectDay($year= '',$expensesType=1,$month=1) {
	
		if (empty($year))
			$year=date ( "Y" );
	
			$list=array();
			if ($expensesType==1){
				$list  = $this->find ( array (
						'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Month(date) as month,accountType,accountMsg,Year(date),Day(date) as day',
						'where' => "  Year(date) =" . $year . " and expensesType=" . $expensesType.' and Month(date)='.$month,
						'groupby' => 'Day(date),accountType,accountMsg',
						'asArray' => TRUE
				) );
			}elseif($expensesType==2){
				$list  = $this->find ( array (
						'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Month(date) as month,expenditureType,expenditureMsg,Year(date),Day(date) as day',
						'where' => "  Year(date) =" . $year . " and expensesType=" . $expensesType.' and Month(date)='.$month,
						'groupby' => 'Day(date),expenditureType,expenditureMsg',
						'asArray' => TRUE
				) );
			}
			return $list;
	}
	
	
	public function getStatisticsByMonth() {
		$day = date ( "d" );
		$month = date ( "m" );
		$year = date ( "Y" );
		
		$list ['statisticsMonth'] = $this->find ( array (
				'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Month(date) as month',
				'where' => "  Year(date) =" . $year . " and Month(date)=" . $month.' and  (accountType!=8 and expenditureType!=5)',
				'groupby' => 'Month(date)',
				'asArray' => TRUE
		) );
		
		if (empty ( $list ['statisticsMonth'] )) {
			$list ['statisticsMonth'] [0] ['inComePrice'] = 0;
			$list ['statisticsMonth'] [0] ['payPrice'] = 0;
			$list ['statisticsMonth'] [0] ['month'] = $month;
		}
		
		$list ['statisticsDay'] = $this->find ( array (
				'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Year(date) as Year',
				'where' => "  Year(date) =" . $year. " and Month(date)=" . $month.' and Day(date)='.$day.' and  (accountType!=8 and expenditureType!=5)',
				'groupby' => 'Day(date)',
				'asArray' => TRUE
		) );
		if (empty ( $list ['statisticsDay'] )) {
			$list ['statisticsDay'] [0] ['inComePrice'] = 0;
			$list ['statisticsDay'] [0] ['payPrice'] = 0;
			$list ['statisticsDay'] [0] ['month'] = $month;
		}
		
		//理财
		$list ['financingPrice'] = $this->find ( array (
				'select' => 'sum(financingPrice) as financingPrice',
				'where' => " expensesType=3",
				'groupby' => 'expensesType',
				'asArray' => TRUE
		) );
		
		//保证金
		$list ['statisticsBond'] = $this->find ( array (
				'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice',
				'where' => " accountType=8 or expenditureType=5",
				'groupby' => 'expenditureType,accountType',
				'asArray' => TRUE
		) );
		
		$bondPriceA=0;
		if (isset($list ['statisticsBond'][0])){
			$bondPriceA=$list ['statisticsBond'][0]['inComePrice'];
		}	
		$bondPriceB=0;
		if (isset($list ['statisticsBond'][1])){
			$bondPriceB=$list ['statisticsBond'][1]['payPrice'];
		}	
		$list ['statisticsBond']=$bondPriceA+$bondPriceB;
			
		
		if (empty ( $list ['financingPrice'] )) {
			$list ['financingPrice'] [0] ['financingPrice'] = $this->reusltPrice;
		}else{
			$list ['financingPrice'][0]['financingPrice']=$this->reusltPrice+$list ['financingPrice'][0]['financingPrice'];
		}
		
		return $list;
	}
	
	public function getStatisticsByBalance($year= '',$expensesType=1,$month=0) {
		
		$con='';$groupby='Month(date)';
		if ($month!=0){
			$con=' and Month(date)='.$month;
			$groupby='Day(date)';
		}
		
		$list  = $this->find ( array (
				'select' => 'sum(inComePrice) as inComePrice,sum(payPrice) as payPrice,Month(date) as month,expensesType,Year(date),Day(date) as day',
				'where' => "  Year(date) =" . $year . " and expensesType=" . $expensesType.$con,
				'groupby' => $groupby,
				'asArray' => TRUE
		) );
		
		return $list;
		
	}
	
}

?>