<?php
class DemoExpense extends AppModel
{
	var $name = 'DemoExpense'; 
	
	 
	
	public function setORMForDemoGroupExpense(){ 
		$this->bindModel(
				
				array('hasMany' => array(
						'DemoParticipant' => array(
						'className' => 'DemoParticipant',
						'conditions' => array('DemoParticipant.expense_id' => 'DemoExpense.id'),
						'order' => 'DemoParticipant.create_date ASC',
						'foreignKey' => 'expense_id'
			),
			'DemoGroupExpense' => array(
				'className' =>'DemoGroupExpense',
					'conditions' => array('DemoGroupExpense.expense_id' => 'DemoExpense.id'),
					'order' => 'DemoGroupExpense.create_date ASC',
					'foreignKey' => 'expense_id'
			)  
			 
		)));	
		 
	}
	
	public function setORMForDemoFamilyExpense(){
		$this->bindModel(array('hasMany' => array(
			 
			'DemoFamilyExpense' => array(
				'className' =>'DemoFamilyExpense',
					'conditions' => array('DemoFamilyExpense.expense_id' => 'DemoExpense.id'),
					'order' => 'DemoFamilyExpense.create_date ASC',
					'foreignKey' => 'expense_id'
			)  
			 
		)));	
	}
	
	public function setORMForDemoMess(){
		
		$this->bindModel(array('hasMany' => array(
			'DemoParticipant' => array(
					'className' => 'DemoParticipant',
					'conditions' => array('DemoParticipant.expense_id' => 'DemoExpense.id'),
					'order' => 'DemoParticipant.create_date ASC',
					'foreignKey' => 'expense_id'
			),
			'DemoGroupExpense' => array(
				'className' =>'DemoGroupExpense',
					'conditions' => array('DemoGroupExpense.expense_id' => 'DemoExpense.id'),
					'order' => 'DemoGroupExpense.create_date ASC',
					'foreignKey' => 'expense_id'
			) ,
			'DemoMeal' => array(
					'className' =>'DemoMeal',
						'conditions' => array('DemoMeal.expense_id' => 'DemoExpense.id'),
						'order' => 'DemoMeal.date ASC',
						'foreignKey' => 'expense_id'
			
			)  
			 
		)));	 
	
	}
	
	public function getDemoGroupExpenseInfoByURL($unique_url){
		
		$expenseType = substr($unique_url, 0, 1);
		if($expenseType == "2" || $expenseType == "7"){
			 $this->setORMForDemoMess();
		} else {
			$this->setORMForDemoGroupExpense();
		}
		
		
		return  $this->find('first', array('conditions' => array('DemoExpense.unique_url' =>$unique_url))); 
	}
	 
	public function getDemoFamilyExpenseInfoByURL($unique_url){	
		 
		$this->setORMForDemoFamilyExpense();
		 
		return $this->find('first', array('conditions' => array('DemoExpense.unique_url' =>$unique_url))); 
	}
	 
	function findExpenseInfoByIds($expenseIdList){
		
		$data = NULL;
		if(!empty($expenseIdList) && count($expenseIdList) > 0){
			 $str = implode(",",$expenseIdList);
			 
			 $this->hasMany = array();
			 			 
			$result =  $this->find('all', array('conditions' => array(
					'DemoExpense.id in ('.$str.')' 
				),
				'order' => 'DemoExpense.create_date DESC'));  
				
			if(!empty($result)){
				$data["expenseList"] = $result;
				App::import('model','DemoParticipant');
	 			$participant = new DemoParticipant();
			 	$participantInfo = $participant->find('all', array('conditions' => array(
						'DemoParticipant.expense_id in ('.$str.') ' ,'is_creator'=>'1'
					)));  
					
				$data["participantList"]= $participantInfo;				
			}			
		}
		
		// $log = $this->getDataSource()->getLog(false, false);
	 	// debug($log);
	 	
		return $data;
	}
 
 
}
?>