<?php
/**
 * revenue
 *
 * dentalair voorraadbeheer omzet
 *
 * @version    1.0
 * @author     Erik Timmers, amgate <erik.timmers@amgate.com>
 * @copyright  Amgate, 2010
 * @since      14-01-2010 - initial creation
 * @package    CDVB0100
 */

require_once 'class.stdModule.php';


class cdvb100_revenueMonth extends stdModule
{

	/**
	* revenue
	*
	* PHP4 constructor
	*
	* @access public
	* @return void
	*/
	function cdvb100_revenueMonth()
	{
	$this->stdModule('cdvb0100');
	$this->setVariable('modcode', 'cdvb0100');

	}

    	function getRevenueMonthList($oid, $date) {
		
		$query  = 	"SELECT rev_id, rev_year, rev_month, rev_amount/100 as rev_amount
				 FROM tbl_dvb_revenue_month
                    		 WHERE rev_year = ". $date ."
				 AND rev_org_id = ". $oid ."
				 AND rev_month != ''; 						
				";

		$result = 	$this->_db->Query($query);

	

		//Aanmaken array voor 12 maanden.			
		$return = array();
		for ($m = 1; $m <= 12; $m++) {
			$return[$m] = array(							
				'rev_id' => 0,					
				'month' => $m,
				'rev_amount' => 0					
			);	
		}

		while (!$result->EOF()) {
			$return[$result->Row('rev_month')] = array(
				'rev_id' => $result->Row('rev_id'),							
				'month' => $result->Row('rev_month'),
				'rev_amount' => $result->Row('rev_amount')					
			); 
			$result->MoveNext();	
		}	  
		
		$result->Close();

		return $return;	

	}  

	function updateRevenue($id, $value)
	{
		$query = " UPDATE tbl_dvb_revenue_month
		           SET rev_amount = " . bcmul($value, '100', 0) . "
		           WHERE rev_id = " . (int)$id . "
		         ";
		$this->_db->Execute($query);
	}  

	function newRevenue($oid, $month, $value, $year)
    	{
        	$query = " INSERT INTO tbl_dvb_revenue_month
                	       (  rev_id
               		        , rev_org_id 
                	        , rev_year 
                       		, rev_month 
                       		, rev_amount
                       	       )
                  	  VALUES
                       	       (  NULL
                       		, " . (int)$oid . " 
                       		, " . (int)$year . " 
                       		, " . (int)$month . "                                               
                       		, " . bcmul($value, '100', 0) . "
                       	       )
                	";
		
		$this->_db->Execute($query);                
    	}

	function getNewIntervalRevenueList($oid, $aDates, $aRev)
	{		
		switch ($aDates['intval']) {
			case CDVB_INTVAL_MONTH:
				foreach ($aDates['enum'] as $key => $value) {					
					$query = ' SELECT rev_id, rev_year, rev_month, rev_amount/100 as rev_amount
						   FROM tbl_dvb_revenue_month
				    		   WHERE rev_year = '. substr($key, 0, 4) .'
						   AND rev_org_id = '. $oid .'
						   AND rev_month  = '. substr($key, 4, 2) .'; 
						 ';	
							
					$result = $this->_db->Query($query);
					
					while (!$result->EOF()) {
						if ($result->Row('rev_amount') != '0.00') {						
							unset($aRev[$key]);						
							$aRev[$key]['rev_year'] = substr($key, 0, 4);
							$aRev[$key]['rev_month'] = substr($key, 4, 2);							
							$aRev[$key]['rev_amount'] = bcadd($aRev[$key]['rev_amount'], $result->Row('rev_amount'), 2); 
							
						}
						$result->MoveNext();
					}

					
				}

				if (count($aRev) != 0) {
					foreach ($aRev as $key => $value) {
						if ($key != 'total') {			
							$total += $value["rev_amount"];
						}
					}
					unset($aRev["total"]["rev_amount"]);					
					$aRev["total"]["rev_amount"] = bcadd($aRev["total"]["rev_amount"], $total, 2);
				}

				
			break;
			case CDVB_INTVAL_YEAR:
				 
				$newRev = array();				
				$year 	= $aDates["first"];
				$query	= "SELECT rev_id, rev_year, rev_month, rev_amount/100 as rev_amount
					   FROM tbl_dvb_revenue_month
			    		   WHERE rev_year = ".$year."
					   AND rev_org_id = ".$oid.";
					  ";
				
				$result = $this->_db->Query($query);
				while (!$result->EOF()) {
					$key 	= $result->Row('rev_year'); 					
					if (strlen($result->Row('rev_month')) == 1) {
						$key .= '0'.$result->Row('rev_month');
					} else {
						$key .= $result->Row('rev_month');	
					}
					if ($result->Row('rev_amount') != '0.00') {
						unset($aRev[$key]);					
						$aRev[$key]['rev_year'] = $result->Row('rev_year');
						$aRev[$key]['rev_month'] = $result->Row('rev_month');							
						$aRev[$key]['rev_amount'] = bcadd($aRev[$key]['rev_amount'], $result->Row('rev_amount'), 2); 
					}
					$result->MoveNext();
				}
		
				if (count($aRev) != 0) {
					foreach ($aRev as $key => $value) {
						if ($key != 'total') {			
							$total += $value["rev_amount"];
						}
					}
					$newRev[$year]["rev_year"] = $year;
					$newRev[$year]["rev_amount"] = bcadd($newRev["total"]["rev_amount"], $total, 2);					
					$newRev["total"]["rev_amount"] = bcadd($newRev["total"]["rev_amount"], $total, 2);
					$aRev = $newRev;				
				}

			break;
		
		}

		return $aRev;
		
	}
    
}
