~intrahealth+informatics/ihris-retention/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
 * © Copyright 2008 IntraHealth International, Inc.
 * 
 * This File is part of iHRIS 
 * 
 * iHRIS is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * The cadre pool form.
 * @package iHRIS
 * @subpackage Plan
 * @access public
 * @author Luke Duncan <lduncan@intrahealth.org>
 * @copyright Copyright &copy; 2008 IntraHealth International, Inc. 
 * @since v3.1.0
 * @version v3.1.0
 */

/**
 * The cadre pool form.
 * @package iHRIS
 * @subpackage Plan
 * @access public
 */
class iHRIS_Pool extends iHRIS_YearlyList {
    /**
     * Get the field names that are yearly inputs
     * @returns array of string
     */ 
    public function yearlyInputs() {
        return  array('projected_rural','projected_urban');
    }



    public function getProjectedRuralHWs($year) {
        $this->projectRural(); 
        $fieldObj = $this->getField('projected_rural');
        if (!$fieldObj instanceof I2CE_FormField_ASSOC_INT) {
            I2CE::raiseError("No projected_rural");
            return 0;
        }
        if (!$fieldObj->keyIsSet($year)) {
            I2CE::raiseError("No projected_rural for year $year");
            return 0;
        }
        return max(0,$fieldObj->getValueOfKey($year));        
    }

    public function getProjectedUrbanHWs($year) {
        $this->projectUrban(); 
        $fieldObj = $this->getField('projected_urban');
        if (!$fieldObj instanceof I2CE_FormField_ASSOC_INT) {
            return 0;
        }
        if (!$fieldObj->keyIsSet($year)) {
            return 0;
        }
        return max(0,$fieldObj->getValueOfKey($year));
        
    }

    protected $projection = null;

    protected function ensureProjection() {
        if ($this->projection === null) {
            $this->projection = I2CE_FormFactory::instance()->createContainer($this->getParent());
            if ($this->projection instanceof iHRIS_Projection) {
                $this->projection->populate();
            } else {
                $this->projection = false;
            }
        }
        return $this->projection instanceof iHRIS_Projection;
    }

    protected function projectPool($field) {
        if (!$this->ensureProjection()) {
            return ;
        }
        $start = $this->projection->getStartYear();
        $duration = $this->projection->getDuration();
        if ($start === false || $duration == false) {
            return ;
        }
        $end = $start + $duration -1;

        if ($start > $end) {
            return;
        }
        $fieldObj = $this->getField('projected_' . $field);
        if (!$fieldObj instanceof I2CE_FormField_ASSOC_INT) {
            return;
        }
        $percent =   (((float) $this->percent) / 100.0);
        $all = 'approved_' . $field;
        $allocated = $this->$all;
        $fall = 'filled_' . $field;
        $fill= $this->$fall;


        if ($end == $start)  {
            if (!$fieldObj->keyIsSet($start)) {
                $fieldObj->setValueOfKey($start,$fill);
            }
            return;
        }
        $slope = (( $percent * ((float) $allocated)) - ((float) $fill)) / ((float) $end - (float)$start );
        
        for ($year = $start; $year <= $end; $year++) {
            if ($fieldObj->keyIsSet($year)) {
                continue;
            }
            $projection =((int) ( $slope * ($year - $start) + $fill));
            $fieldObj->setValueOfKey( $year,(int) $projection);
        }
    }


    protected $projected = array();
    public function projectRural() {
        if (!array_key_exists('rural',$this->projected) || !$this->projected['rural']) {
            $this->projectPool('rural');
        }
        $this->projected['rural'] = true;
    }
    public function projectUrban() {
        if (!array_key_exists('urban',$this->projected) || !$this->projected['urban']) {
            $this->projectPool('urban');
        }
        $this->projected['urban'] = true;
    }


    public function hasSupplyData() {
        $this->populateChildren( array( "supply_data" ));
        return (array_key_exists( 'supply_data', $this->children )  && is_array($this->children['supply_data']) && count($this->children['supply_data']) >0);
    }

    public function getPoolSize($start_year,$year_offset) {
        $this->populateChildren( array( "supply_data" ));

        if ( !array_key_exists( 'supply_data', $this->children ) || !is_array($this->children['supply_data']) || count($this->children['supply_data']) == 0) {
            return 0;
        }
        reset($this->children['supply_data']);
        $supplyObj = current($this->children['supply_data']);
        $size = $supplyObj->getField('supply');
        if (!$size instanceof I2CE_FormField_INT || !$size->issetValue()) {
            return 0;
        }
        return max(0,$size->getValue());
    }


    public function getSalary($start_year, $year_offset, $inflation_rate) {
        $base_salary = $this->getBaseSalary();
        return   ( (float) $base_salary) *  pow(  1.0+ (((float)$inflation_rate)/100),$year_offset) ;
    }

    public function getBaseSalary() {
        $this->populateChildren( array( "supply_data" ));

        if ( !array_key_exists( 'supply_data', $this->children ) || !is_array($this->children['supply_data']) || count($this->children['supply_data']) == 0) {
            return 0;
        }
        reset($this->children['supply_data']);
        $supplyObj = current($this->children['supply_data']);
        $salary = $supplyObj->getField('avg_salary');
        if (!$salary instanceof I2CE_FormField_FLOAT || !$salary->issetValue()) {
            return 0;
        }
        return max(0,$salary->getValue());
    }

}
# Local Variables:
# mode: php
# c-default-style: "bsd"
# indent-tabs-mode: nil
# c-basic-offset: 4
# End: