<?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_revenue extends stdModule
{

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

    }

    /**
     * getRevenueList
     *
     * get list of revenues
     *
     * @access public
     * @param  int    $oid      id of organisation
     * @param  object $oStart   weekyear-object of start of list
     * @param  object $oEnd     weekyear-object of end of list
     * @return array
     */    
    function getRevenueList($oid, $oStart, $oEnd)
    {
        $sSql = " SELECT rev_id
                       , rev_year
                       , rev_week
                       , rev_amount/100 as rev_amount
                    FROM tbl_dvb_revenue
                   WHERE rev_org_id = " . (int)$oid . "
                     AND (   (" . $oStart->year() . " = " . $oEnd->year() . "
                              AND rev_year = "  . $oEnd->year() . "
                              AND rev_week >= " . $oStart->week() . "
                              AND rev_week <= " . $oEnd->week() . "
                             )
                          OR
                             (" . $oStart->year() . " != " . $oEnd->year() . "
                              AND rev_year = "  . $oStart->year() . "
                              AND rev_week >= " . $oStart->week() . "
                             )
                          OR
                             (" . $oStart->year() . " != " . $oEnd->year() . "
                              AND rev_year = "  . $oEnd->year() . "
                              AND rev_week <= " . $oEnd->week() . "
                             )
                         )
                ";
         $rst = $this->_db->Query($sSql);
         while (!$rst->EOF()) {
            $aReturn[$rst->Row('rev_year') . str_pad($rst->Row('rev_week'), 2, '0', STR_PAD_LEFT)] = $rst->Row();
            $rst->MoveNext();
         }
         $rst->Close();
         return $aReturn;
    }

    /**
     * getIntervalRevenueList
     *
     * get list of revenues between two dates, summation over interval period
     *
     * @access public
     * @param  int   $oid       organisation-id
     * @param  array $aDates    array with date/inteval information
     * @return array
     */
    function getIntervalRevenueList($oid, $aDates) 
    {
        
        $startYear = date('Y', $aDates['dateStart']);
        $startWeek = date('W', $aDates['dateStart']);
        if ($startWeek >= 52 && date('m', $aDates['dateStart']) == 1) {
            $startYear--;
        }
        
        $endYear = date('Y', $aDates['dateEnd']);
        $endWeek = date('W', $aDates['dateEnd']);
        if ($endWeek >= 52 && date('m', $aDates['dateEnd']) == 1) {
            $endYear--;
        }

        $sSql = " SELECT rev_id
                       , rev_year
                       , rev_week
                       , rev_amount/100 as rev_amount
                    FROM tbl_dvb_revenue
                   WHERE rev_org_id = " . (int)$oid . "
                     AND (   (" . $startYear . " = " . $endYear . "
                              AND rev_year = "  . $endYear . "
                              AND rev_week >= " . $startWeek . "
                              AND rev_week <= " . $endWeek . "
                             )
                          OR
                             (" . $startYear . " != " . $endYear . "
                              AND rev_year = "  . $startYear . "
                              AND rev_week >= " . $startWeek . "
                             )
                          OR
                             (" . $startYear . " != " . $endYear . "
                              AND rev_year = "  . $endYear . "
                              AND rev_week <= " . $endWeek . "
                             )
                         )                   
                ";        
         $rst = $this->_db->Query($sSql);
         while (!$rst->EOF()) {
            switch($aDates['intval']) {
                case CDVB_INTVAL_WEEK:
                    $aReturn[$rst->Row('rev_year') . str_pad($rst->Row('rev_week'), 2, '0', STR_PAD_LEFT)] = $rst->Row();    
                    break;

                case CDVB_INTVAL_YEAR:
                    $aReturn[$rst->Row('rev_year')]['rev_year'] = $rst->Row('rev_year');    
                    //$aReturn[$rst->Row('rev_year')]['rev_amount'] += (int)$rst->Row('rev_amount');  
                    $aReturn[$rst->Row('rev_year')]['rev_amount'] = bcadd($aReturn[$rst->Row('rev_year')]['rev_amount']
                                                                         , $rst->Row('rev_amount'), 2);  
                    break;

                case CDVB_INTVAL_MONTH:
                    // add revenue to month of it's thursday (4th day = half-week)
                    $tsWeek = $this->getWeekTimestamp($rst->Row('rev_week'), $rst->Row('rev_year'));
                    $month = date('m', $tsWeek);
                    $sYW = $rst->Row('rev_year') . str_pad($month, 2, '0', STR_PAD_LEFT);
                    $aReturn[$sYW]['rev_year'] = $rst->Row('rev_year');
                    $aReturn[$sYW]['rev_month'] = $month;
                    $aReturn[$sYW]['rev_amount'] = bcadd($aReturn[$sYW]['rev_amount'], $rst->Row('rev_amount'), 2);  
                    //$aReturn[$rst->Row('rev_year') . str_pad($month, 2, '0', STR_PAD_LEFT)]['rev_amount'] += (int)$rst->Row('rev_amount');
                    break;
            }
            
            $aReturn['total']['rev_amount'] = bcadd($aReturn['total']['rev_amount'], $rst->Row('rev_amount'), 2);
            //$aReturn['total']['rev_amount'] += (int)$rst->Row('rev_amount');
            
            
            $rst->MoveNext();
         }
         $rst->Close();
         return $aReturn;   
    }

    /**
     * updateRevenue
     *
     * update revenue-item
     *
     * @access public
     * @param  int    $id       id of revenue to update
     * @param  float  $value    the revenue
     * @return void
     */
    function updateRevenue($id, $value)
    {
        
        $sSql = " UPDATE tbl_dvb_revenue
                     SET rev_amount = " . bcmul($value, '100', 0) . "
                   WHERE rev_id = " . (int)$id . "
                ";
		$this->_db->Execute($sSql);
    }

    /**
     * newRevenue
     *
     * new revenue-item
     *
     * @access public
     * @param  int    $oid      id of organisation
     * @param  int    $week     week
     * @param  int    $year     year
     * @param  float  $value    the revenue
     * @return void
     */
    function newRevenue($oid, $week, $year, $value)
    {
        $sSql = " INSERT INTO tbl_dvb_revenue
                       ( rev_id
                       , rev_org_id 
                       , rev_year 
                       , rev_week 
                       , rev_amount
                       )
                  VALUES
                       ( NULL
                       , " . (int)$oid . " 
                       , " . (int)$year . " 
                       , " . (int)$week . "                                               
                       , " . bcmul($value, '100', 0) . "
                       )
                ";
		$this->_db->Execute($sSql);                
    }
    
    /**
     * get Month of Week
     *
     * returns timestamp of halfway (thu) day of week
     *
     * @access public
     * @return integer
     */
    function getWeekTimestamp($week, $year)
    {
        $offset = date('w', mktime(0, 0, 0, 1, 1, $year));
        $offset = ($offset < 5) ? 1 - $offset : 8 - $offset;
        $thursday = mktime(0, 0, 0, 1, 4 + $offset, $year);
                
        return strtotime('+' . ($week - 1) . ' weeks', $thursday);
        
    }
    
}