~ihris-india-team/indian-ihris/india-manage-demo

« back to all changes in this revision

Viewing changes to importCSV.php

  • Committer: Luke Duncan
  • Date: 2010-12-17 08:52:46 UTC
  • Revision ID: lduncan@intrahealth.org-20101217085246-lyzlwsawihulnsap
Initial import of India demonstration site.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * The best way to run this is:
 
5
 * php importCSV.php 2> convert.log
 
6
 * There's lots of notice messages you probably want to ignore for the most
 
7
 * part.
 
8
 * You'll need to change the include file to find the right config file
 
9
 * as well as the path to I2CE which may not work right using the one
 
10
 * from the config file.
 
11
 * The ID for the User object should be valid in your user table.
 
12
 * The $forms array is an associative array with the value being
 
13
 * an array of forms that are required for the given form to work e.g. 
 
14
 * region needs country first since it uses country as a map for a field.
 
15
 *
 
16
 * 
 
17
 *
 
18
 */
 
19
 
 
20
define( 'iHRIS_DEFAULT_STATE', 'Bihar' );
 
21
define( 'iHRIS_DEFAULT_COUNTRY', 'India' );
 
22
 
 
23
define( 'iHRIS_PERSON_TITLE', 0 );
 
24
define( 'iHRIS_FIRST_NAME', 1 );
 
25
define( 'iHRIS_MIDDLE_NAME', 2 );
 
26
define( 'iHRIS_SURNAME', 3 );
 
27
define( 'iHRIS_NATIONALITY', 4 );
 
28
define( 'iHRIS_RESIDENCE_VILLAGE', 5 );
 
29
define( 'iHRIS_RESIDENCE_THANA', 6 );
 
30
define( 'iHRIS_RESIDENCE_BLOCK', 7 );
 
31
define( 'iHRIS_RESIDENCE_DISTRICT', 8 );
 
32
define( 'iHRIS_RESIDENCE_STATE', 9 );
 
33
define( 'iHRIS_GPF_CPF_NUM', 10 );
 
34
define( 'iHRIS_VOTER_ID_NUM', 11 );
 
35
define( 'iHRIS_RATION_CARD_NUM', 12 );
 
36
define( 'iHRIS_DRIVING_LICENSE_NUM', 13 );
 
37
define( 'iHRIS_OTHER_ID_NUM', 14 );
 
38
define( 'iHRIS_PHOTO', 15 );
 
39
define( 'iHRIS_DATE_OF_BIRTH', 16 );
 
40
define( 'iHRIS_GENDER', 17 );
 
41
define( 'iHRIS_BIRTH_BLOCK', 18 );
 
42
define( 'iHRIS_BIRTH_DISTRICT', 19 );
 
43
define( 'iHRIS_BIRTH_STATE', 20 );
 
44
define( 'iHRIS_MARITAL_STATUS', 21 );
 
45
define( 'iHRIS_DEPENDENTS', 22 );
 
46
define( 'iHRIS_CASTE', 23 );
 
47
define( 'iHRIS_FATHER_FIRSTNAME', 24 );
 
48
define( 'iHRIS_FATHER_SURNAME', 25 );
 
49
define( 'iHRIS_MOTHER_FIRSTNAME', 26 );
 
50
define( 'iHRIS_MOTHER_SURNAME', 27 );
 
51
define( 'iHRIS_HUSBAND_FIRSTNAME', 28 );
 
52
define( 'iHRIS_HUSBAND_SURNAME', 29 );
 
53
define( 'iHRIS_ADDRESS', 30 );
 
54
define( 'iHRIS_WORK_PHONE', 31 );
 
55
define( 'iHRIS_WORK_FAX', 32 );
 
56
define( 'iHRIS_MOBILE_PHONE', 33 );
 
57
define( 'iHRIS_EMAIL_ADDRESS', 34 );
 
58
define( 'iHRIS_DESIGNATION', 35 );
 
59
define( 'iHRIS_DEPARTMENT', 36 );
 
60
define( 'iHRIS_START_DATE', 37 );
 
61
define( 'iHRIS_PAYSCALE', 38 );
 
62
define( 'iHRIS_FACILITY_TYPE', 39 );
 
63
define( 'iHRIS_FACILITY_BLOCK', 40 );
 
64
define( 'iHRIS_FACILITY_DISTRICT', 41 );
 
65
define( 'iHRIS_POSITION_TYPE', 42 );
 
66
define( 'iHRIS_CLASSIFICATION', 43 );
 
67
define( 'iHRIS_JOIN_DATE', 44 );
 
68
define( 'iHRIS_JOIN_DESIGNATION', 45 );
 
69
define( 'iHRIS_RETIREMENT_DATE', 46 );
 
70
define( 'iHRIS_ON_DEPUTATION', 47 );
 
71
define( 'iHRIS_DEPUTATION_DEPARTMENT', 48 );
 
72
define( 'iHRIS_DEPUTATION_DESIGNATION', 49 );
 
73
define( 'iHRIS_DEPUTATION_START_DATE', 50 );
 
74
define( 'iHRIS_DEPUTATION_FACILITY_TYPE', 51 );
 
75
define( 'iHRIS_DEPUTATION_BLOCK', 52 );
 
76
define( 'iHRIS_DEPUTATION_DISTRICT', 53 );
 
77
define( 'iHRIS_LANG_ENGLISH', 54 );
 
78
define( 'iHRIS_LANG_HINDI', 55 );
 
79
define( 'iHRIS_LANG_OTHER', 56 );
 
80
define( 'iHRIS_ACADEMIC_QUALIFICATION', 57 );
 
81
define( 'iHRIS_ACADEMIC_INSTITUTION', 58 );
 
82
define( 'iHRIS_ACADEMIC_DISTRICT', 59 );
 
83
define( 'iHRIS_ACADEMIC_STATE', 60 );
 
84
define( 'iHRIS_ACADEMIC_YEAR', 61 );
 
85
define( 'iHRIS_ACADEMIC_DOMAIN1', 62 );
 
86
define( 'iHRIS_ACADEMIC_DOMAIN2', 63 );
 
87
define( 'iHRIS_ACADEMIC_DOMAIN3', 64 );
 
88
define( 'iHRIS_SPECIALTY1', 65 );
 
89
define( 'iHRIS_SPECIALTY2', 66 );
 
90
define( 'iHRIS_SPECIALTY_IN_PROGRESS', 67 );
 
91
define( 'iHRIS_NOTES', 68 );
 
92
 
 
93
$person_id_types = array( iHRIS_GPF_CPF_NUM => 'GPF/CPF No.', 
 
94
        iHRIS_VOTER_ID_NUM => 'Voter ID Card No.',
 
95
        iHRIS_RATION_CARD_NUM => 'Ration Card No.', 
 
96
        iHRIS_DRIVING_LICENSE_NUM => 'Driving License No.',
 
97
        iHRIS_OTHER_ID_NUM => 'Other ID Number' );
 
98
 
 
99
$i2ce_site_user_access_init = null;
 
100
$script = array_shift( $argv );
 
101
if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/local' . DIRECTORY_SEPARATOR . 'config.values.php')) {
 
102
        require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/local' . DIRECTORY_SEPARATOR . 'config.values.php');
 
103
} else {
 
104
        require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pages/config.values.php');
 
105
}
 
106
 
 
107
$i2ce_site_i2ce_path = "../../lib/4.0/I2CE";
 
108
 
 
109
require_once ($i2ce_site_i2ce_path . DIRECTORY_SEPARATOR . 'I2CE_config.inc.php');
 
110
 
 
111
@I2CE::initializeDSN($i2ce_site_dsn,   $i2ce_site_user_access_init,    $i2ce_site_module_config);
 
112
 
 
113
unset($i2ce_site_user_access_init);
 
114
unset($i2ce_site_dsn);
 
115
unset($i2ce_site_i2ce_path);
 
116
unset($i2ce_site_module_config);
 
117
 
 
118
 
 
119
global $user;
 
120
 
 
121
$user = new I2CE_User(1, false, false, false);
 
122
$db = MDB2::singleton();
 
123
if ( PEAR::isError( $db ) ) {
 
124
        die( $db->getMessage() );
 
125
}
 
126
$form_factory = I2CE_FormFactory::instance();
 
127
 
 
128
echo "Memory Limit: " . ini_get( "memory_limit" ) . "\n";
 
129
echo "Execution Time: " . ini_get( "max_execution_time" ) . "\n";
 
130
 
 
131
if ( $argv[0] == "erase" ) {
 
132
    echo "Erasing all records and entries...";
 
133
    $db->query( "TRUNCATE TABLE record" );
 
134
    $db->query( "TRUNCATE TABLE entry" );
 
135
    $db->query( "TRUNCATE TABLE last_entry" );
 
136
    echo "Done\n";
 
137
    $tmp = array_shift( $argv );
 
138
}
 
139
 
 
140
function dotrim(&$value){
 
141
  $value = trim($value);
 
142
}
 
143
 
 
144
$fh = fopen( $argv[0], "r" );
 
145
if ( $fh === false ) {
 
146
    die( "Couldn't update file: $argv[0].  Syntax: importCSV.php [erase] file.csv\n" );
 
147
}
 
148
 
 
149
function find_or_create( $value, $form, $fields=false ) {
 
150
    global $user, $cache;
 
151
    if ( !array_key_exists( $form, $cache ) ) {
 
152
        $cache[$form] = array();
 
153
    }
 
154
    if ( !array_key_exists( $value, $cache[$form] ) ) {
 
155
        $obj = I2CE_FormFactory::instance()->createContainer( $form );
 
156
        if ( !$fields ) {
 
157
            $fields = array( 'name' => $value );
 
158
        } 
 
159
        foreach( $fields as $key => $val ) {
 
160
            $obj->getField($key)->setFromDB($val);
 
161
        }
 
162
        $obj->save( $user );
 
163
        echo "Creating new form ($form) " . $obj->getId() . " ";
 
164
        print_r( $fields );
 
165
        $cache[$form][$value] = $obj->getId();
 
166
        $obj->cleanup();
 
167
        unset( $obj );
 
168
    }
 
169
    return $form . '|' . $cache[$form][$value];
 
170
}
 
171
 
 
172
function arrange_date( $date ) {
 
173
    list( $day, $month, $year ) = explode( '/', $date );
 
174
    return "$year-$month-$day";
 
175
}
 
176
 
 
177
$cache = array();
 
178
$cache['person_title'] = array_flip( I2CE_List::listOptions( "person_title" ) );
 
179
$cache['county'] = array_flip( I2CE_List::listOptions( "county" ) );
 
180
$cache['district'] = array_flip( I2CE_List::listOptions( "district" ) );
 
181
$cache['region'] = array_flip( I2CE_List::listOptions( "region" ) );
 
182
$cache['country'] = array_flip( I2CE_List::listOptions( "country" ) );
 
183
$cache['id_type'] = array_flip( I2CE_List::listOptions( "id_type" ) );
 
184
$cache['gender'] = array_flip( I2CE_List::listOptions( "gender" ) );
 
185
$cache['marital_status'] = array_flip( I2CE_List::listOptions( "marital_status" ) );
 
186
$cache['caste'] = array_flip( I2CE_List::listOptions( "caste" ) );
 
187
$cache['facility'] = array_flip( I2CE_List::listOptions( "facility" ) );
 
188
$cache['facility_type'] = array_flip( I2CE_List::listOptions( "facility_type" ) );
 
189
$cache['salary_grade'] = array_flip( I2CE_List::listOptions( "salary_grade" ) );
 
190
$cache['position_type'] = array_flip( I2CE_List::listOptions( "position_type" ) );
 
191
$cache['salary_grade'] = array_flip( I2CE_List::listOptions( "salary_grade" ) );
 
192
$cache['classification'] = array_flip( I2CE_List::listOptions( "classification" ) );
 
193
$cache['job'] = array_flip( I2CE_List::listOptions( "job" ) );
 
194
$cache['department'] = array_flip( I2CE_List::listOptions( "department" ) );
 
195
$cache['language'] = array_flip( I2CE_List::listOptions( "language" ) );
 
196
$cache['degree'] = array_flip( I2CE_List::listOptions( "degree" ) );
 
197
$cache['edu_type'] = array_flip( I2CE_List::listOptions( "edu_type" ) );
 
198
$cache['domain_of_study'] = array_flip( I2CE_List::listOptions( "domain_of_study" ) );
 
199
$cache['specialty'] = array_flip( I2CE_List::listOptions( "specialty" ) );
 
200
 
 
201
$p_code = 0;
 
202
 
 
203
while ( ( $data = fgetcsv( $fh ) ) !== false ) {
 
204
 
 
205
    array_walk( $data, "dotrim" );
 
206
 
 
207
 
 
208
    /* 
 
209
     * Create the person form and save it.
 
210
     */
 
211
    $person = $form_factory->createContainer( "person" );
 
212
 
 
213
    $title_id = find_or_create( $data[iHRIS_PERSON_TITLE], "person_title" );
 
214
    $person->getField('person_title')->setFromDB( $title_id );
 
215
    $person->firstname = $data[iHRIS_FIRST_NAME];
 
216
    $person->othername = $data[iHRIS_MIDDLE_NAME];
 
217
    $person->surname = $data[iHRIS_SURNAME];
 
218
    if ( !$data[iHRIS_NATIONALITY] ) {
 
219
        $data[iHRIS_NATIONALITY] = iHRIS_DEFAULT_COUNTRY;
 
220
    }
 
221
    $nationality_id = find_or_create( $data[iHRIS_NATIONALITY], "country" );
 
222
    $person->getField('nationality')->setFromDB( $nationality_id );
 
223
    $person->residence_thana = $data[iHRIS_RESIDENCE_THANA];
 
224
    $person->residence_village = $data[iHRIS_RESIDENCE_VILLAGE];
 
225
 
 
226
    if ( !$data[iHRIS_RESIDENCE_STATE] ) {
 
227
        $data[iHRIS_RESIDENCE_STATE] = iHRIS_DEFAULT_STATE;
 
228
    }
 
229
    $country_id = find_or_create( iHRIS_DEFAULT_COUNTRY, "country" );
 
230
    $region_id = find_or_create( 
 
231
            $data[iHRIS_RESIDENCE_STATE] . ", " . iHRIS_DEFAULT_COUNTRY, 
 
232
            "region",
 
233
            array( 'name' => $data[iHRIS_RESIDENCE_STATE],
 
234
                'country' => $country_id ) );
 
235
    if ( $data[iHRIS_RESIDENCE_DISTRICT] ) {
 
236
        $district_id = find_or_create(
 
237
                $data[iHRIS_RESIDENCE_DISTRICT] . ", " 
 
238
                . $data[iHRIS_RESIDENCE_STATE] . ", " . iHRIS_DEFAULT_COUNTRY, 
 
239
                "district",
 
240
                array( 'name' => $data[iHRIS_RESIDENCE_DISTRICT],
 
241
                    'region' => $region_id ) );
 
242
        if ( $data[iHRIS_RESIDENCE_BLOCK] ) {
 
243
            $county_id = find_or_create(
 
244
                    $data[iHRIS_RESIDENCE_BLOCK] . ", " 
 
245
                    . $data[iHRIS_RESIDENCE_DISTRICT] . ", " 
 
246
                    . $data[iHRIS_RESIDENCE_STATE] . ", " 
 
247
                    . iHRIS_DEFAULT_COUNTRY, 
 
248
                    "county",
 
249
                    array( 'name' => $data[iHRIS_RESIDENCE_BLOCK],
 
250
                        'district' => $district_id ) );
 
251
            $person->getField('residence')->setFromDB( $county_id );
 
252
        } else {
 
253
            $person->getField('residence')->setFromDB( $district_id );
 
254
        }
 
255
    }
 
256
 
 
257
    $person->save( $user );
 
258
    echo "created " . $person->getId() . "\n";
 
259
 
 
260
 
 
261
    /*
 
262
     * Create the person id forms and save them.
 
263
     */
 
264
    foreach ( $person_id_types as $csv_idx => $id_type ) {
 
265
        if( $data[$csv_idx] == '' ) {
 
266
            continue;
 
267
        }
 
268
        $id_type_id = find_or_create( $id_type, "id_type" );
 
269
        $person_id = $form_factory->createContainer( "person_id" );
 
270
        $person_id->setParent( $person->getNameId() );
 
271
 
 
272
        $person_id->getField( 'id_type' )->setFromDB( $id_type_id );
 
273
        $person_id->id_num = $data[ $csv_idx ];
 
274
        $person_id->save( $user );
 
275
 
 
276
        $person_id->cleanup();
 
277
        unset( $person_id );
 
278
    }
 
279
 
 
280
 
 
281
    /*
 
282
     * Create the demographic form and save it.
 
283
     */
 
284
    $demographic = $form_factory->createContainer( "demographic" );
 
285
    $demographic->setParent( $person->getNameId() );
 
286
 
 
287
    if ( $data[iHRIS_DATE_OF_BIRTH] ) {
 
288
        $demographic->getField('birth_date')->setFromDB( arrange_date( $data[iHRIS_DATE_OF_BIRTH] ) );
 
289
    }
 
290
    if ( $data[iHRIS_GENDER] ) {
 
291
        $gender_id = find_or_create( $data[iHRIS_GENDER], "gender" );
 
292
        $demographic->getField('gender')->setFromDB( $gender_id );
 
293
    }
 
294
    if ( !$data[iHRIS_BIRTH_STATE] ) {
 
295
        $data[iHRIS_BIRTH_STATE] = iHRIS_DEFAULT_STATE;
 
296
    }
 
297
    $region_id = find_or_create(
 
298
            $data[iHRIS_BIRTH_STATE] . ", "
 
299
            . iHRIS_DEFAULT_COUNTRY,
 
300
            "region",
 
301
            array( 'name' => $data[iHRIS_BIRTH_STATE],
 
302
                'country' => $country_id ) );
 
303
    if ( $data[iHRIS_BIRTH_DISTRICT] ) {
 
304
        $district_id = find_or_create(
 
305
                $data[iHRIS_BIRTH_DISTRICT] . ", " 
 
306
                . $data[iHRIS_BIRTH_STATE] . ", " . iHRIS_DEFAULT_COUNTRY,
 
307
                "district",
 
308
                array( 'name' => $data[iHRIS_BIRTH_DISTRICT],
 
309
                    'region' => $region_id ) );
 
310
        if ( $data[iHRIS_BIRTH_BLOCK] ) {
 
311
            $county_id = find_or_create(
 
312
                    $data[iHRIS_BIRTH_BLOCK] . ", " 
 
313
                    . $data[iHRIS_BIRTH_DISTRICT] . ", " 
 
314
                    . $data[iHRIS_BIRTH_STATE] . ", " 
 
315
                    . iHRIS_DEFAULT_COUNTRY, 
 
316
                    "county",
 
317
                    array( 'name' => $data[iHRIS_BIRTH_BLOCK],
 
318
                        'district' => $district_id ) );
 
319
            $demographic->getField('birth_location')->setFromDB( $county_id );
 
320
        } else {
 
321
            $demographic->getField('birth_location')->setFromDB( $district_id );
 
322
        }
 
323
    }
 
324
    if ( $data[iHRIS_MARITAL_STATUS] ) {
 
325
        $marital_status_id = find_or_create( $data[iHRIS_MARITAL_STATUS], "marital_status" );
 
326
        $demographic->getField('marital_status')->setFromDB( $marital_status_id );
 
327
    }
 
328
    if ( $data[iHRIS_DEPENDENTS] != '' ) {
 
329
        $demographic->dependents = $data[iHRIS_DEPENDENTS];
 
330
    }
 
331
    if ( $data[iHRIS_CASTE] ) {
 
332
        $caste_id = find_or_create( $data[iHRIS_CASTE], "caste" );
 
333
        $demographic->getField('caste')->setFromDB( $caste_id );
 
334
    }
 
335
    
 
336
    $demographic->save( $user );
 
337
    $demographic->cleanup();
 
338
    unset( $demographic );
 
339
 
 
340
 
 
341
    /*
 
342
     * Create the parent information form and save it.
 
343
     */
 
344
    $parent_information = $form_factory->createContainer( "parent_information" );
 
345
    $parent_information->setParent( $person->getNameId() );
 
346
 
 
347
    $parent_information->father_surname = $data[iHRIS_FATHER_SURNAME];
 
348
    $parent_information->father_firstname = $data[iHRIS_FATHER_FIRSTNAME];
 
349
    $parent_information->mother_surname = $data[iHRIS_MOTHER_SURNAME];
 
350
    $parent_information->mother_firstname = $data[iHRIS_MOTHER_FIRSTNAME];
 
351
    $parent_information->husband_surname = $data[iHRIS_HUSBAND_SURNAME];
 
352
    $parent_information->husband_firstname = $data[iHRIS_HUSBAND_FIRSTNAME];
 
353
 
 
354
    $parent_information->save( $user );
 
355
    $parent_information->cleanup();
 
356
    unset( $parent_information );
 
357
    
 
358
 
 
359
    /*
 
360
     * Create the work contact form and save it.
 
361
     */
 
362
    $contact = $form_factory->createContainer( "person_contact_work" );
 
363
    $contact->setParent( $person->getNameId() );
 
364
 
 
365
    $contact->address = $data[iHRIS_ADDRESS];
 
366
    $contact->telephone = $data[iHRIS_WORK_PHONE];
 
367
    $contact->fax = $data[iHRIS_WORK_FAX];
 
368
    $contact->alt_telephone = $data[iHRIS_MOBILE_PHONE];
 
369
    $contact->email = $data[iHRIS_EMAIL_ADDRESS];
 
370
    $contact->save( $user );
 
371
    $contact->cleanup();
 
372
    unset( $contact );
 
373
 
 
374
    /*
 
375
     * Create the position form and save it.
 
376
     */
 
377
    // First get the facility details and create or find it.
 
378
    $region_id = find_or_create( iHRIS_DEFAULT_STATE . ", " .
 
379
            iHRIS_DEFAULT_COUNTRY,
 
380
            "region",
 
381
            array( 'name' => iHRIS_DEFAULT_STATE,
 
382
                'country' => iHRIS_DEFAULT_COUNTRY ) );
 
383
    $facility_fields = array( 'name' => $data[iHRIS_FACILITY_BLOCK] . " " . $data[iHRIS_FACILITY_TYPE] );
 
384
    $district_id = find_or_create(
 
385
            $data[iHRIS_FACILITY_DISTRICT] . ", " 
 
386
            . iHRIS_DEFAULT_STATE . ", " . iHRIS_DEFAULT_COUNTRY,
 
387
            "district",
 
388
            array( 'name' => $data[iHRIS_FACILITY_DISTRICT],
 
389
                'region' => $region_id ) );
 
390
    $county_id = find_or_create(
 
391
            $data[iHRIS_FACILITY_BLOCK] . ", " 
 
392
            . $data[iHRIS_FACILITY_DISTRICT] . ", " 
 
393
            . iHRIS_DEFAULT_STATE . ", " 
 
394
            . iHRIS_DEFAULT_COUNTRY, 
 
395
            "county",
 
396
            array( 'name' => $data[iHRIS_FACILITY_BLOCK],
 
397
                'district' => $district_id ) );
 
398
    $facility_fields['location'] = $county_id;
 
399
    $facility_fields['facility_type'] = find_or_create( $data[iHRIS_FACILITY_TYPE], "facility_type" );
 
400
 
 
401
    $facility_id = find_or_create( $facility_fields['name'],
 
402
            "facility", $facility_fields );
 
403
 
 
404
    // Now we create the job (designation).
 
405
 
 
406
    $salary_grade_id = find_or_create( $data[iHRIS_PAYSCALE], "salary_grade" );
 
407
    $classification_id = find_or_create( $data[iHRIS_CLASSIFICATION], "classification" );
 
408
    $job_code = preg_replace( "/[^A-Z]/", "", $data[iHRIS_DESIGNATION] );
 
409
    $job_id = find_or_create( $data[iHRIS_DESIGNATION], "job",
 
410
            array( 'title' => $data[iHRIS_DESIGNATION],
 
411
                'salary_grade' => $salary_grade_id,
 
412
                'code' => $job_code,
 
413
                'classification' => $classification_id ) );
 
414
 
 
415
    // Now we create the position.
 
416
    $p_code++;
 
417
    $post_code = sprintf( "%s-%05d", $job_code, $p_code );
 
418
    $position = $form_factory->createContainer( "position" );
 
419
    
 
420
    $position->code = $post_code;
 
421
    $position->getField('job')->setFromDB( $job_id );
 
422
    $department_id = find_or_create( $data[iHRIS_DEPARTMENT], "department" );
 
423
    $position->title = $data[iHRIS_DESIGNATION];
 
424
    $position->getField('department')->setFromDB( $department_id );
 
425
    $position->getField('facility')->setFromDB( $facility_id );
 
426
    $position_type_id = find_or_create( $data[iHRIS_POSITION_TYPE], "position_type" );
 
427
    $position->getField('pos_type')->setFromDB( $position_type_id );
 
428
    $position->getField('status')->setFromDB( 'position_status|closed' );
 
429
 
 
430
    $position->save( $user );
 
431
 
 
432
    // Now we assign the position to the person.
 
433
    $person_position = $form_factory->createContainer( "person_position" );
 
434
    $person_position->setParent( $person->getNameId() );
 
435
 
 
436
    $person_position->getField("position")->setFromDB( $position->getNameId() );
 
437
    $person_position->getField("start_date")->setFromDB( arrange_date( $data[iHRIS_START_DATE] ) );
 
438
 
 
439
    $person_position->save( $user );
 
440
 
 
441
    $salary = $form_factory->createContainer( "salary" );
 
442
    $salary->setParent( $person_position->getNameId() );
 
443
    $salary->getField('start_date')->setFromDB( arrange_date( $data[iHRIS_START_DATE] ) );
 
444
 
 
445
    $salary->save( $user );
 
446
    $salary->cleanup();
 
447
    unset( $salary );
 
448
 
 
449
 
 
450
    if ( strtolower( $data[iHRIS_ON_DEPUTATION] ) == "yes" ) {
 
451
 
 
452
        $region_id = find_or_create( iHRIS_DEFAULT_STATE . ", " .
 
453
                iHRIS_DEFAULT_COUNTRY,
 
454
                "region",
 
455
                array( 'name' => iHRIS_DEFAULT_STATE,
 
456
                    'country' => iHRIS_DEFAULT_COUNTRY ) );
 
457
        $facility_fields = array( 'name' => $data[iHRIS_DEPUTATION_BLOCK] . " " . $data[iHRIS_DEPUTATION_FACILITY_TYPE] );
 
458
        $district_id = find_or_create(
 
459
                $data[iHRIS_DEPUTATION_DISTRICT] . ", " 
 
460
                . iHRIS_DEFAULT_STATE . ", " . iHRIS_DEFAULT_COUNTRY,
 
461
                "district",
 
462
                array( 'name' => $data[iHRIS_DEPUTATION_DISTRICT],
 
463
                    'region' => $region_id ) );
 
464
        $county_id = find_or_create(
 
465
                $data[iHRIS_DEPUTATION_BLOCK] . ", " 
 
466
                . $data[iHRIS_DEPUTATION_DISTRICT] . ", " 
 
467
                . iHRIS_DEFAULT_STATE . ", " 
 
468
                . iHRIS_DEFAULT_COUNTRY, 
 
469
                "county",
 
470
                array( 'name' => $data[iHRIS_DEPUTATION_BLOCK],
 
471
                    'district' => $district_id ) );
 
472
        $facility_fields['location'] = $county_id;
 
473
        $facility_fields['facility_type'] = find_or_create( $data[iHRIS_DEPUTATION_FACILITY_TYPE], "facility_type" );
 
474
    
 
475
        $facility_id = find_or_create( $facility_fields['name'],
 
476
                "facility", $facility_fields );
 
477
 
 
478
        $dep_job_code = preg_replace("/[^A-Z]/", "", 
 
479
                $data[iHRIS_DEPUTATION_DESIGNATION] );
 
480
        $dep_job_id = find_or_create( $data[iHRIS_DEPUTATION_DESIGNATION], "job",
 
481
                array( 'title' => $data[iHRIS_DEPUTATION_DESIGNATION] 
 
482
                    ) );
 
483
 
 
484
        $p_code++;
 
485
        $post_code = sprintf( "%s-%05d", $dep_job_code, $p_code );
 
486
        $dep_position = $form_factory->createContainer( "position" );
 
487
        $dep_position->code = $post_code;
 
488
        $dep_position->getField('job')->setFromDB( $dep_job_id );
 
489
        $dep_dept_id = find_or_create( $data[iHRIS_DEPUTATION_DEPARTMENT], "department" );
 
490
        $dep_position->getField('department')->setFromDB( $dep_dept_id );
 
491
        $dep_position->getField('facility')->setFromDB( $facility_id );
 
492
        $dep_position->getField('status')->setFromDB( 'position_status|deputed_to' );
 
493
 
 
494
        $dep_position->save( $user );
 
495
 
 
496
        $position->getField('status')->setFromDB( 'position_status|deputed_from' );
 
497
        $position->save( $user );
 
498
 
 
499
        $deputation = $form_factory->createContainer( "deputation" );
 
500
        $deputation->setParent( $person_position->getNameId() );
 
501
        $deputation->getField('position')->setFromDB( $dep_position->getNameId() );
 
502
        $deputation->getField('start_date')->setFromDB( arrange_date( $data[iHRIS_DEPUTATION_START_DATE] ) );
 
503
        
 
504
        $deputation->save( $user );
 
505
 
 
506
        $deputation->cleanup();
 
507
        unset( $deputation );
 
508
 
 
509
        $dep_position->cleanup();
 
510
        unset( $dep_position );
 
511
        
 
512
    }
 
513
 
 
514
 
 
515
    $person_position->cleanup();
 
516
    unset( $person_position );
 
517
 
 
518
    $position->cleanup();
 
519
    unset( $position );
 
520
 
 
521
    /*
 
522
     * Create the joining_job form and save it.
 
523
     */
 
524
    $joining_job = $form_factory->createContainer( "joining_job" );
 
525
    $joining_job->setParent( $person->getNameId() );
 
526
 
 
527
    $joining_job->getField("date_of_joining")->setFromDB( arrange_date( $data[iHRIS_JOIN_DATE] ) );
 
528
    $jj_job_id = find_or_create( $data[iHRIS_JOIN_DESIGNATION], "job",
 
529
            array( 'title' => $data[iHRIS_JOIN_DESIGNATION],
 
530
                'code' => preg_replace( "/[^A-Z]/", "", $data[iHRIS_JOIN_DESIGNATION] ),
 
531
                ) );
 
532
    $joining_job->getField("job")->setFromDB( $jj_job_id );
 
533
    $joining_job->getField("retirement_date")->setFromDB( arrange_date( $data[iHRIS_RETIREMENT_DATE] ) );
 
534
 
 
535
    $joining_job->save( $user );
 
536
    $joining_job->cleanup();
 
537
    unset( $joining_job );
 
538
 
 
539
    if ($data[iHRIS_LANG_ENGLISH] == 'yes') {
 
540
        $language = $form_factory->createContainer( "person_language" );
 
541
        $language->setParent( $person->getNameId() );
 
542
    
 
543
        $language_id = find_or_create( 'English', "language" );                       
 
544
        $language->getField("language")->setFromDB( $language_id );
 
545
    
 
546
        $language->save( $user );
 
547
        $language->cleanup();
 
548
        unset( $language );
 
549
    }
 
550
    if ($data[iHRIS_LANG_HINDI] == 'yes') {
 
551
        $language = $form_factory->createContainer( "person_language" );
 
552
        $language->setParent( $person->getNameId() );
 
553
    
 
554
        $language_id = find_or_create( 'Hindi', "language" );                       
 
555
        $language->getField("language")->setFromDB( $language_id );
 
556
    
 
557
        $language->save( $user );
 
558
        $language->cleanup();
 
559
        unset( $language );
 
560
    }
 
561
    if ($data[iHRIS_LANG_OTHER] != '') {
 
562
        $language = $form_factory->createContainer( "person_language" );
 
563
        $language->setParent( $person->getNameId() );
 
564
    
 
565
        $language_id = find_or_create( $data[iHRIS_LANG_OTHER], "language" );                       
 
566
        $language->getField("language")->setFromDB( $language_id );
 
567
    
 
568
        $language->save( $user );
 
569
        $language->cleanup();
 
570
        unset( $language );
 
571
    }
 
572
 
 
573
 
 
574
    $education = $form_factory->createContainer( "education" );
 
575
    $education->setParent( $person->getNameId() );
 
576
    
 
577
    $degree_id = find_or_create( $data[iHRIS_ACADEMIC_QUALIFICATION], "degree",
 
578
         array( 'name' => $data[iHRIS_ACADEMIC_QUALIFICATION],
 
579
                'edu_type' => find_or_create( 'Others', 'edu_type' ),
 
580
         ) );                       
 
581
        
 
582
    $education->getField("degree")->setFromDB( $degree_id );
 
583
    $education->institution = $data[iHRIS_ACADEMIC_INSTITUTION];
 
584
    $education->location = $data[iHRIS_ACADEMIC_DISTRICT] . ", " . $data[iHRIS_ACADEMIC_STATE];
 
585
    $education->getField("year")->setFromDB( $data[iHRIS_ACADEMIC_YEAR]);
 
586
    $domain_id = find_or_create( $data[iHRIS_ACADEMIC_DOMAIN1], "domain_of_study" );                       
 
587
        
 
588
    $education->getField("domain_of_study")->setFromDB( $domain_id );       
 
589
 
 
590
    $education->save( $user );
 
591
    $education->cleanup();
 
592
    unset( $education );
 
593
 
 
594
    if ( $data[iHRIS_ACADEMIC_DOMAIN2] != "" ) {
 
595
    $education = $form_factory->createContainer( "education" );
 
596
    $education->setParent( $person->getNameId() );
 
597
    
 
598
    $domain_id = find_or_create( $data[iHRIS_ACADEMIC_DOMAIN2], "domain_of_study" );                           
 
599
    $education->getField("domain_of_study")->setFromDB( $domain_id );       
 
600
 
 
601
    $education->save( $user );
 
602
    $education->cleanup();
 
603
    unset( $education );
 
604
    }
 
605
 
 
606
    if ( $data[iHRIS_ACADEMIC_DOMAIN3] != "" ) {
 
607
    $education = $form_factory->createContainer( "education" );
 
608
    $education->setParent( $person->getNameId() );
 
609
    
 
610
    $domain_id = find_or_create( $data[iHRIS_ACADEMIC_DOMAIN3], "domain_of_study" );                           
 
611
    $education->getField("domain_of_study")->setFromDB( $domain_id );       
 
612
 
 
613
    $education->save( $user );
 
614
    $education->cleanup();
 
615
    unset( $education );
 
616
    }
 
617
 
 
618
    if ( $data[iHRIS_SPECIALTY1] != "" ) {
 
619
    $education = $form_factory->createContainer( "education" );
 
620
    $education->setParent( $person->getNameId() );
 
621
    
 
622
    $specialty_id = find_or_create( $data[iHRIS_SPECIALTY1], "specialty" );                           
 
623
    $education->getField("specialty")->setFromDB( $specialty_id );       
 
624
    $education->getField("year")->setFromDB( $data[iHRIS_ACADEMIC_YEAR]);
 
625
 
 
626
    $education->save( $user );
 
627
    $education->cleanup();
 
628
    unset( $education );
 
629
    }
 
630
 
 
631
    if ( $data[iHRIS_SPECIALTY2] != "" ) {
 
632
    $education = $form_factory->createContainer( "education" );
 
633
    $education->setParent( $person->getNameId() );
 
634
    
 
635
    $specialty_id = find_or_create( $data[iHRIS_SPECIALTY2], "specialty" );                           
 
636
    $education->getField("specialty")->setFromDB( $specialty_id );       
 
637
    $education->getField("year")->setFromDB( $data[iHRIS_ACADEMIC_YEAR]);
 
638
 
 
639
    $education->save( $user );
 
640
    $education->cleanup();
 
641
    unset( $education );
 
642
    }
 
643
 
 
644
    if ( $data[iHRIS_SPECIALTY_IN_PROGRESS] != "" ) {
 
645
    $education = $form_factory->createContainer( "education" );
 
646
    $education->setParent( $person->getNameId() );
 
647
    
 
648
    $specialty_id = find_or_create( $data[iHRIS_SPECIALTY_IN_PROGRESS], "specialty" );                           
 
649
    $education->getField("specialty")->setFromDB( $specialty_id );       
 
650
 
 
651
    $education->save( $user );
 
652
    $education->cleanup();
 
653
    unset( $education );
 
654
    }
 
655
 
 
656
 
 
657
    if ( $data[iHRIS_NOTES] != "" ) {
 
658
    $notes = $form_factory->createContainer( "notes" );
 
659
    $notes->setParent( $person->getNameId() );
 
660
    $notes->note=$data[iHRIS_NOTES];
 
661
    $notes->date_added=I2CE_Date::now(); 
 
662
   
 
663
    $notes->save( $user );
 
664
    $notes->cleanup();
 
665
    unset( $notes );
 
666
    }
 
667
 
 
668
 
 
669
    $person->cleanup();
 
670
    unset( $person );
 
671
 
 
672
}
 
673
 
 
674
?>