<?php
Doo::loadCore ( 'db/DooModel' );
class wasteBook extends DooModel {
	public $wid;
	public $expensesType;
	public $accountType;
	public $accountMsg;
	public $accountPrice;
	public $accountBank;
	public $category;
	public $cid;
	public $name;
	public $sid;
	public $dailyIncomeType;
	public $dailyExpenditureType;
	public $inputDate;
	public $dataID;
	
	public $accountPriceShow;
	
	
	public $_table = 'CLD_wasteBook';
	public $_primarykey = 'wid';
	public $_fields = array (
			'wid',
			'expensesType',
			'accountType',
			'accountMsg',
			
			'accountPrice',
			'accountBank',
			'category',
			'name',
			'cid',
			'sid',
			'dailyIncomeType',
			'dailyExpenditureType',
			'inputDate' ,
			'dataID',
			
			'accountPriceShow',
	);
	
	//
	function getWasteBookByStatisticsYear($year=''){
		if (empty($year))
			$year=date ( "Y" );
		
			$list  = $this->find ( array (
					'select' => 'sum(accountPrice) as accountPrice ',
					'where' => "  Year(inputDate) <" . $year ,
					//'groupby' => 'Month(inputDate)',
					'asArray' => TRUE
			) );
		
			return $list;
	}
	function getWasteBookByStatisticsDay($year='',$month=''){
		$day=cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
		if (empty($year)){
			$year=date ( "Y" );
			$month=date("m");
		}
	
			$list  = $this->find ( array (
					'select' => 'sum(accountPrice) as accountPrice ',
					'where' => "  inputDate <'" . $year."-".$month."-1'" ,
					//'groupby' => 'Month(inputDate)',
					'asArray' => TRUE
			) );
	
			return $list;
	}
	
	function getWasteBookByStatistics($expensesType=1,$year=''){
	
		if (empty($year))
			$year=date ( "Y" );
		
		$list  = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,sum(accountPriceShow) as accountPriceShow ,Month(inputDate) as month,expensesType,Year(inputDate)',
				'where' => "  Year(inputDate) =" . $year . " and expensesType=" . $expensesType,
				'groupby' => 'Month(inputDate),expensesType',
				'asArray' => TRUE
		) );
		
		//print_r($list);
		return $list;
	}
	
	public function getStatisticsByDay($expensesType=1,$year= '',$month='') {
	
		
	
		$list  = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,sum(accountPriceShow) as accountPriceShow,date_format(inputDate,"%Y-%m") ,Month(inputDate) as month,expensesType,Year(inputDate),Day(inputDate) as day',
				'where' => " date_format(inputDate,'%Y-%m')=date_format('".$year."-".$month."-1','%Y-%m')  and expensesType=" . $expensesType,
				'groupby' => 'Day(inputDate),expensesType',
				'asArray' => TRUE
		) );
		
// 		$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;
	
	}
	
	function getAccountBookByExpensesType($limit=4,$expensesType=1,$con=''){
		
		$where= array (
				'where' => "expensesType=".$expensesType.$con,
				'desc' => 'wid',
				'asArray' => TRUE
		);
		if($limit!=0){
			$where += array (
					'limit' => $limit
			);
		}
		
		$list = $this->find ( $where );
		
		Doo::loadModel ( "accountBook" );
		$accountBook = new accountBook ();
		Doo::loadModel ( 'receipt' );
		$receipt = new receipt ();
		Doo::loadModel ( 'invoiceReceivables' );
		$invoiceReceivables = new invoiceReceivables ();
		Doo::loadModel ( 'invoice' );
		$invoice = new invoice ();
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		foreach ($list as $key=>$value){
			$list[$key]['widKey']=$XDeode->encode ( $value ['wid'] );
			$list[$key]['accountTypeKey']=$XDeode->encode ( $value ['accountType'] );
			
			$list[$key]['accountBankMsg']='';
			if($value['accountBank']=='CGBDeposit'){
				$list[$key]['accountBankMsg']='纵横广发';
			}elseif($value['accountBank']=='ICBCDeposit'){
				$list[$key]['accountBankMsg']='纵横工行';
			}elseif($value['accountBank']=='HUADeposit'){
				$list[$key]['accountBankMsg']='华润银行';
			}elseif($value['accountBank']=='PersonalDeposit'){
				$list[$key]['accountBankMsg']='个人广发';
			}
			
			
			$list[$key]['data']=array();
			if ($value['accountType']==5||$value['accountType']==6||$value['accountType']==9||$value['accountType']==10){
				
				$list[$key]['data']=$accountBook->getAccountBookByAid($value['dataID']);
			}elseif($value['accountType']==7||$value['accountType']==11){
				$list[$key]['data']=$invoiceReceivables->getInvoiceReceivablesByIrid($value['dataID']);
			}elseif($value['accountType']==8){
				$list[$key]['data']= $invoice->getInvoiceByIid ( $value['dataID'] );
 			
			}elseif($value['accountType']==1||$value['accountType']==2||$value['accountType']==3||$value['accountType']==4||$value['accountType']==12){
				$list[$key]['data']=$receipt->getReceiptByRid($value['dataID']);
			}
			
		}
		//print_r($list);
		return $list;
	}
	
	/**
	 */
	function getAccountBookByLimit($limit = 3) {
		$sql = 'select *
				from ' . $this->_table . '
						 ORDER BY  aid desc limit ' . $limit;
		$query = Doo::db ()->query ( $sql );
		$list = $query->fetchAll ();
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		foreach ( $list as $key => $value ) {
			$list [$key] ['aidKey'] = $XDeode->encode ( $value ['aid'] );
		}
		
		return $list;
	}
	/**
	 *
	 * @param number $limit
	 * @param string $con
	 * @param string $desc
	 * @return boolean
	 */
	function getAccountBookBySql($limit = 0, $con = "", $desc = 'desc') {
		
		// $listCount = $this->count ( array (
		// 'where' => $con,
		// 'asArray' => TRUE
		// ) );
		$list = $this->find ( array (
				'where' => '1 ' . $con,
				'limit' => $limit,
				$desc => 'aid',
				'asArray' => TRUE 
		) ); // echo $con;
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		foreach ( $list as $key => $value ) {
			// $list [$key] ['count'] = $listCount;
			$list [$key] ['aidKey'] = $XDeode->encode ( $value ['aid'] );
		}
		
		return $list;
	}
	
	/**
	 * 
	 * @param number $expensesType
	 * @param number $accountType
	 * @param number $dataID
	 * @param string $select
	 * @return unknown|string[]
	 */
	function getAccountBookByAid($expensesType=1,$accountType=1,$dataID = 0, $select = "") {
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		
		if (! is_numeric ( $dataID ))
			$rid=$XDeode->decode( $dataID );
			
		
		$condition = array (
				'where' => "dataID=" . $dataID.' and expensesType='.$expensesType.' and accountType='.$accountType,
				'asArray' => TRUE 
		);
		if (! empty ( $select ))
			$condition += array (
					'select' => $select 
			);
		
		$Detail = array ();
		if (is_numeric ( $dataID ) && ! empty ( $dataID ))
			$Detail = $this->getOne ( $condition );
		
		if (empty ( $Detail ))
			return $Detail;
		
		$Detail ['widKey'] = $XDeode->encode ( $Detail ['wid'] );
		
		
		return $Detail;
	}
	
	/**
	 * 
	 */
	function getAccountBookByWid(){
		
	}
	
	/**
	 * 统计存款部分
	 */
	function getWasteBookStatisticsByIncome(){
		//and dailyIncomeType!=7
		$list['incomeStatistics'] = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,expensesType,accountType,accountBank,dailyIncomeType',
				'where' => " expensesType=1  and (accountType =6 or accountType =7 or accountType=8  or accountType =1 or accountType =2 or accountType=3 or accountType=4 or accountType=12 ) ",
				'groupby' => 'accountBank',
				'asArray' => TRUE
		) );
		
		$list['expenditureStatistics'] = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,expensesType,accountType,accountBank,dailyExpenditureType',
				'where' => " expensesType=2   and (accountType =1 or accountType =2 or accountType=3 or accountType=4 or accountType=5 or accountType=11  or accountType=12) ",
				'groupby' => 'accountBank',
				'asArray' => TRUE
		) );
		//活期
		$list['currentStatistics'] = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,expensesType,accountType,accountBank,dailyExpenditureType',
				'where' => " accountType =10 ",
				'groupby' => 'accountBank',
				'asArray' => TRUE
		) );
		//固期
		$list['fixedStatistics'] = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,expensesType,accountType,accountBank,dailyExpenditureType',
				'where' => " accountType =9 ",
				'groupby' => 'accountBank',
				'asArray' => TRUE
		) );
		//月份总计
		$list['incomeReport'] = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,sum(accountPriceShow) as accountPriceShow,date_format(inputDate,"%Y-%m") as inputDate,expensesType',
				'where' => " YEAR(inputDate)= YEAR( NOW( ) ) and expensesType=1",
				'groupby' => 'date_format(inputDate,"%Y-%m"),expensesType',
				'asArray' => TRUE
		) );
		$list['expenditureReport'] = $this->find ( array (
				'select' => 'sum(accountPrice) as accountPrice,sum(accountPriceShow) as accountPriceShow,date_format(inputDate,"%Y-%m") as inputDate,expensesType',
				'where' => " YEAR(inputDate)= YEAR( NOW( ) ) and expensesType=2",
				'groupby' => 'date_format(inputDate,"%Y-%m"),expensesType',
				'asArray' => TRUE
		) );
// 		//合计
// 		$list['total'] = $this->find ( array (
// 				'select' => 'sum(accountPrice) as accountPrice',
// 				//'where' => " accountType =9 ",
// 				//'groupby' => 'accountBank',
// 				'asArray' => TRUE
// 		) );
 		return $list;
	}
	
	/**
	 * 插入数据
	 * @param array $item
	 * @return number
	 */
	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;
	}
	
	function getWateBookByAD($accountType='',$dataID=''){
		$detail=$this->getOne(array (
				'where' => "accountType=".$accountType.' and dataID='.$dataID,
				'asArray' => TRUE
		));
		
		return $detail;
	}
}

?>