<?php
Doo::loadCore ( 'db/DooModel' );
class accountItem extends DooModel {
	public $aid;
	public $category;
	public $name;
	public $_table = 'CLD_accountItem';
	public $_primarykey = 'aid';
	public $_fields = array (
			'aid',
			'category',
			'name' 
	);
	
	/**
	 * 
	 */
	function getAccountItem(){
		$sql = 'select category
				from ' . $this->_table . '
						 GROUP BY category ORDER BY  aid ASC  ';
		$query = Doo::db ()->query ( $sql );
		$itemCategory = $query->fetchAll ();
		
		$list = $this->find ( array (
				'asArray' => TRUE
		) );
		
		$acctionItem=array();
		foreach ($itemCategory as $key=>$value){
			$acctionItem[$value['category']]=array();
			foreach ($list as $k=>$v){
				if ($value['category']==$v['category']){
					array_push($acctionItem[$value['category']], $v);
				}
			}
		}
		
		return $acctionItem;
	}
	
	/**
	 * 根据项目获得ITEM
	 */
	function getAccountBookByCategory($category=''){
		
		$list = $this->find ( array (
				'where' => ' category like "%' . $category.'%"',
				'asArray' => TRUE
		) );
		return $list;
	}
	
	/**
	 * 获得培训班金额详情
	 * @param array $RItemList 支出项目
	 * @param array $RILecturerList 讲师项目
	 * @return number[]
	 */
	function getTrainingDetail($RItemList=array(),$RILecturerList=array(),$invoiceArriveAmount=0){
		$aiList = $this->find ( array (
				'where' => ' category like "%培训班费用%"',
				'asArray' => TRUE
		) );
		
		$total=0;$categoryActualExpenditure=0;
		foreach ($aiList as $k=>$v){
			$aiList[$k]['price']=$aiList[$k]['companyPrice']=$aiList[$k]['categoryPrice']=0;
			$aiList[$k]['insidePrice']=$aiList[$k]['outsideNoTaxPrice']=$aiList[$k]['outsideTaxPrice']=0;
			foreach ($RItemList as $key=>$value){
				if($v['name']=='讲课费'){
				}elseif($v['name']=='办公费'&&$value['item']=='办公费'){
					if($value['payType']==0){
						$aiList[$k]['categoryPrice']=$aiList[$k]['categoryPrice']+$value['price'];
						$total+=$value['price'];
					}elseif($value['payType']==1){
						$aiList[$k]['companyPrice']+=$value['price'];
						$categoryActualExpenditure+=$value['price'];
						$total+=$value['price'];
					}
					$aiList[$k]['price']+=$value['price'];
					unset($RItemList[$key]);
				}elseif($v['name']=='邮寄费'&&$value['item']=='邮寄费'){
					if($value['payType']==0){
						$aiList[$k]['categoryPrice']+=$value['price'];
						$total+=$value['price'];
					}elseif($value['payType']==1){
						$aiList[$k]['companyPrice']+=$value['price'];
						$categoryActualExpenditure+=$value['price'];
						$total+=$value['price'];
					}
					$aiList[$k]['price']+=$value['price'];
					unset($RItemList[$key]);
				}else{
					if ($v['name']==$value['item']){
						$aiList[$k]['price']=$aiList[$k]['price']+$value['price'];
						$total+=$value['price'];
						unset($RItemList[$key]);
					}
				}
				if($value['item']=='研究中心经费'&&$v['name']=='研究中心经费'){
					$categoryActualExpenditure+=$value['price'];
				}
				if($value['item']=='税款'&&$v['name']=='税款'){
					$categoryActualExpenditure+=$value['price'];
				}
			}
			//讲师项
			foreach ($RILecturerList as $lecKey=>$lecValue){
				if($v['name']=='讲课费'){
					if($lecValue['ltype']=='INSIDE'){
						if($lecValue['fees']=='0.00'){
							$bValue=$lecValue['price']*$lecValue['schoolDay'];
						}else{
							$bValue=$lecValue['fees'];
						}
						
						//echo $bValue."<br/>";
						$aiList[$k]['insidePrice']+=$bValue;
						$total+=$bValue;
						$categoryActualExpenditure+=$bValue;
						$aiList[$k]['price']+=$bValue;
						unset($RILecturerList[$lecKey]);
					}elseif($lecValue['ltype']=='OUTSIDE'){
						if($lecValue['taxation']==0){
							$bValue=$lecValue['price']*$lecValue['schoolDay'];
							$aiList[$k]['outsideNoTaxPrice']+=$bValue;
							$total+=$bValue;
							$aiList[$k]['price']+=$bValue;
							unset($RILecturerList[$lecKey]);
						}elseif($lecValue['taxation']==1){
							$bValue=$lecValue['price']*$lecValue['schoolDay'];
							$aiList[$k]['outsideTaxPrice']+=$bValue;
							$total+=$bValue;
							$aiList[$k]['price']+=$bValue;
							unset($RILecturerList[$lecKey]);
						}
					}
				}
			}
			
			$aiList[$k]['categoryProportion']=$aiList[$k]['companyProportion']=$aiList[$k]['proportion']=0;
			$aiList[$k]['insideProportion']=$aiList[$k]['outsideNoTaxProportion']=$aiList[$k]['outsideTaxProportion']=0;
			 
			if ($invoiceArriveAmount!=0){
	 			if($v['name']=='办公费'||$v['name']=='邮寄费'){
	 				$proportion=$aiList[$k]['categoryPrice']/$invoiceArriveAmount;
	 				$aiList[$k]['categoryProportion']= sprintf("%.2f",$proportion*100);     
	 				$proportion=$aiList[$k]['companyPrice']/$invoiceArriveAmount;
	 				$aiList[$k]['companyProportion']=sprintf("%.2f",$proportion*100);   
	 			}elseif($v['name']=='讲课费'){
	 				$proportion=$aiList[$k]['insidePrice']/$invoiceArriveAmount;
	 				$aiList[$k]['insideProportion']=sprintf("%.2f",$proportion*100);   
	 				
	 				$proportion=$aiList[$k]['outsideTaxPrice']/$invoiceArriveAmount;
	 				$aiList[$k]['outsideTaxProportion']= sprintf("%.2f",$proportion*100);   
	 				
	 				$proportion=$aiList[$k]['outsideNoTaxPrice']/$invoiceArriveAmount;
	 				$aiList[$k]['outsideNoTaxProportion']= sprintf("%.2f",$proportion*100);   
	 			}else{
	 				$proportion=$aiList[$k]['price']/$invoiceArriveAmount;
	 				$aiList[$k]['proportion']=sprintf("%.2f",$proportion*100);  
	 			}
			}
			
			//$aiList[$k]['companyPrice']=number_format ($aiList[$k]['companyPrice'],2);
			$aiList[$k]['outsideTaxPrice']=sprintf("%.2f",$aiList[$k]['outsideTaxPrice']);  
			$aiList[$k]['outsideNoTaxPrice']=sprintf("%.2f",$aiList[$k]['outsideNoTaxPrice']); 
			
			$aiList[$k]['insidePrice']=sprintf("%.2f",$aiList[$k]['insidePrice']);  
			
			$aiList[$k]['companyPrice']=sprintf("%.2f",$aiList[$k]['companyPrice']);  
			$aiList[$k]['categoryPrice']=sprintf("%.2f",$aiList[$k]['categoryPrice']);  
			$aiList[$k]['priceFormat']=sprintf("%.2f",$aiList[$k]['price']);  
			
		}
		
		return array('dataList'=>$aiList,'total'=>$total,'categoryActualExpenditure'=>$categoryActualExpenditure);
	}
	
}

?>