~sahana-relief-team/sahana-relief-experiments/relief-exp

« back to all changes in this revision

Viewing changes to mod/ims/lib_ims.inc

  • Committer: turtle
  • Date: 2009-11-10 23:51:44 UTC
  • Revision ID: turtle@turtle-laptop-20091110235144-lvhvgwfm053jvq5q
first relief codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**Internal Library of the Inventory Management system 
 
4
*
 
5
* PHP version 5
 
6
*
 
7
* LICENSE: This source file is subject to LGPL license
 
8
* that is available through the world-wide-web at the following URI:
 
9
* http://www.gnu.org/copyleft/lesser.html
 
10
*
 
11
* @author     Ravindra De Silva <ravindra@opensource.lk><ravidesilva@iee.org>
 
12
*             Mahesh Kaluarachchi <mahesh@opensource.lk>
 
13
* @copyright  Lanka Software Foundation - http://www.opensource.lk
 
14
* @package    sahana
 
15
* @subpackage ims
 
16
*/
 
17
 
 
18
global $global;
 
19
include_once $global['approot']."/inc/lib_form.inc";
 
20
include_once $global['approot']."/inc/lib_location.inc";
 
21
include_once $global['approot'].'/mod/or/lib_or.inc';
 
22
include_once $global['approot'].'/3rd/phplot/phplot_ims.php';
 
23
 
 
24
function shn_ims_org_list()
 
25
{
 
26
        global $global;
 
27
        $db=$global['db'];
 
28
        $q = "SELECT inv_uuid,inventory_name,parent_id FROM  ims_inventory_records ORDER BY inventory_name";
 
29
        $res_org=$db->Execute($q);
 
30
        $org_list=array();
 
31
        while(!$res_org==NULL && !$res_org->EOF){
 
32
                $org_list[$res_org->fields[0]]=$res_org->fields[1];
 
33
                $res_org->MoveNext();
 
34
        }
 
35
        return $org_list;
 
36
}
 
37
function _shn_ims_get_start_loc()
 
38
{
 
39
    global $global;
 
40
    global $conf;
 
41
    $db=$global['db'];
 
42
    $q="SELECT value FROM config WHERE module_id='or' AND confkey='loc_start'";
 
43
    $res=$db->Execute($q);
 
44
    if($res->fields[0]==NULL){
 
45
           return $conf['mod_or_ims_start_loc'];
 
46
    }else {
 
47
        return $res->fields[0];
 
48
    }
 
49
}
 
50
 
 
51
function shn_ims_get_loc_range()
 
52
{
 
53
    global $global;
 
54
    global $conf;
 
55
    $db=$global['db'];
 
56
    $loc=array();
 
57
    $q="SELECT value FROM config WHERE module_id='or' AND confkey='loc_range_start'";
 
58
    $res=$db->Execute($q);
 
59
    if($res->fields[0]==NULL){
 
60
           $loc["start"]=$conf['mod_or_ims_loc_level_start'];
 
61
    }else {
 
62
        $loc["start"]=$res->fields[0];
 
63
    }
 
64
    $q="SELECT value FROM config WHERE module_id='or' AND confkey='loc_range_end'";
 
65
    $res=$db->Execute($q);
 
66
    if($res->fields[0]==NULL){
 
67
           $loc["end"]=$conf['mod_or_ims_loc_level_end'];
 
68
    }else {
 
69
        $loc["end"]=$res->fields[0];
 
70
    }
 
71
    return $loc;
 
72
}
 
73
 
 
74
function _shn_ims_action_change_javascript($change)
 
75
{
 
76
?>
 
77
<script type="text/javascript">
 
78
    function change_action(action){
 
79
        var x=document.getElementsByName("<?php echo $change?>");
 
80
         x[0].value=action;
 
81
         document.view.submit();
 
82
         return;
 
83
    }
 
84
</script>
 
85
<?php
 
86
}
 
87
 
 
88
function _shn_ims_admin_javascript($name)
 
89
{
 
90
?>
 
91
<script type="text/javascript">
 
92
 
 
93
 // sort function - ascending (case-insensitive)
 
94
        function sortFuncAsc(record1, record2) {
 
95
            var value1 = record1.optText.toLowerCase();
 
96
            var value2 = record2.optText.toLowerCase();
 
97
            if (value1 > value2) return(1);
 
98
            if (value1 < value2) return(-1);
 
99
            return(0);
 
100
        }
 
101
 
 
102
        // sort function - descending (case-insensitive)
 
103
        function sortFuncDesc(record1, record2) {
 
104
            var value1 = record1.optText.toLowerCase();
 
105
            var value2 = record2.optText.toLowerCase();
 
106
            if (value1 > value2) return(-1);
 
107
            if (value1 < value2) return(1);
 
108
            return(0);
 
109
        }
 
110
 
 
111
        function sortSelect(selectToSort, ascendingOrder) {
 
112
            if (arguments.length == 1) ascendingOrder = true;    // default to ascending sort
 
113
 
 
114
            // copy options into an array
 
115
            var myOptions = [];
 
116
            for (var loop=0; loop<selectToSort.options.length; loop++) {
 
117
                myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
 
118
            }
 
119
 
 
120
            // sort array
 
121
            if (ascendingOrder) {
 
122
                myOptions.sort(sortFuncAsc);
 
123
            } else {
 
124
                myOptions.sort(sortFuncDesc);
 
125
            }
 
126
 
 
127
            // copy sorted options from array back to select box
 
128
            selectToSort.options.length = 0;
 
129
            for (var loop=0; loop<myOptions.length; loop++) {
 
130
                var optObj = document.createElement('option');
 
131
                optObj.text = myOptions[loop].optText;
 
132
                optObj.value = myOptions[loop].optValue;
 
133
                selectToSort.options.add(optObj);
 
134
            }
 
135
        }
 
136
 
 
137
        function add_types(){
 
138
            var y=document.getElementsByName("type");
 
139
            var z=document.getElementsByName("type_abbr");
 
140
            var add=document.getElementsByName("added");
 
141
            var remove=document.getElementsByName("removed");
 
142
            var exist=search(add[0].value,z[0].value,true,y[0].value);
 
143
            if(exist){
 
144
                alert("The Type Exists,you just added it");
 
145
                return;
 
146
            }
 
147
    
 
148
            var x=document.getElementsByName("<?php echo $name?>");
 
149
            exist=search_select_box(x[0],z[0].value,true,y[0].value);
 
150
            if(exist){
 
151
                alert("The Type Exists in the DataBase");
 
152
                return;
 
153
            }
 
154
            exist=search(remove[0].value,z[0].value,true,y[0].value);
 
155
            if(exist){
 
156
                remove[0]=del(remove[0].value,z[0].value);
 
157
                return;
 
158
            }
 
159
            opt = document.createElement("option") ;
 
160
            opt.text = y[0].value ;
 
161
            opt.value = z[0].value ;
 
162
            var k=x[0].options.length;
 
163
            x[0].options[k]=opt;
 
164
            sortSelect(x[0], true) ;
 
165
            add[0].value= add[0].value+":"+z[0].value+"|"+y[0].value;
 
166
            y[0].value=null;
 
167
            z[0].value=null
 
168
        }
 
169
 
 
170
        function remove_types(){
 
171
            var x=document.getElementsByName("<?php echo $name?>");
 
172
            removeSelectedOptions(x[0]);
 
173
            sortSelect(x[0], true) ;
 
174
        }
 
175
 
 
176
        function hasOptions(obj) {
 
177
            if (obj!=null && obj.options!=null) { return true; }
 
178
                return false;
 
179
        }
 
180
    
 
181
        function removeSelectedOptions(from) { 
 
182
            if (!hasOptions(from)) { return; }
 
183
            if (from.type=="select-one") {
 
184
                from.options[from.selectedIndex] = null;
 
185
            }
 
186
            else {
 
187
                var add=document.getElementsByName("added");
 
188
                var remove=document.getElementsByName("removed");
 
189
                for (var i=(from.options.length-1); i>=0; i--) { 
 
190
                    var o=from.options[i]; 
 
191
                    if (o.selected) { 
 
192
                        var exist=search(add[0].value,o.value,false);
 
193
                        if(exist){
 
194
                            add[0].value=del(add[0].value,o.value);
 
195
                        }else{
 
196
                             remove[0].value= remove[0].value+":"+o.value+"|"+o.text;
 
197
                        }
 
198
                        from.options[i] = null; 
 
199
                    }
 
200
                }
 
201
            }
 
202
                 from.selectedIndex = -1; 
 
203
        } 
 
204
 
 
205
        function search(arr,value,both,desc){
 
206
            if (window.RegExp) {
 
207
                var re = new RegExp(value);
 
208
                var temp = new Array(); 
 
209
                temp = arr.split(':');
 
210
                if (temp.length==1){
 
211
                    return false;
 
212
                }
 
213
                for (var i=0; i<temp.length; i++) {
 
214
                    var options = new Array(); 
 
215
                    options= temp[i].split('|');
 
216
                    var re = new RegExp(value);
 
217
                    if (re.test(options[0])) {
 
218
                        return true;
 
219
                    }
 
220
                    if(both){
 
221
                        re = new RegExp(desc);
 
222
                        if (re.test(options[1])) {
 
223
                            return true;
 
224
                        }
 
225
                    }
 
226
                }
 
227
            }
 
228
            return false;
 
229
        }
 
230
        function search_select_box(obj,value,both,desc) {
 
231
            if (window.RegExp) {
 
232
                if (!hasOptions(obj)) { return false; }
 
233
                for (var i=0; i<obj.options.length; i++) {
 
234
                    var re = new RegExp(value);
 
235
                    if (re.test(obj.options[i].value)) {
 
236
                        return true;
 
237
                    }
 
238
                    if(both){
 
239
                        re = new RegExp(desc);
 
240
                        if (re.test(obj.options[i].text)) {
 
241
                            return true;
 
242
                        }
 
243
                    }
 
244
                }
 
245
            }
 
246
            return false;
 
247
        }
 
248
        function del(from,what){
 
249
            var temp = new Array();
 
250
            temp = from.split(':');
 
251
            from=null;
 
252
            if (temp.length==1){
 
253
                return false;
 
254
            }
 
255
            for (var i=1; i<temp.length; i++) {
 
256
                var options = new Array(); 
 
257
                options= temp[i].split('|');
 
258
                if(options[0]!=what){
 
259
                    
 
260
                    from= from+":"+options[0]+"|"+options[1];
 
261
                }
 
262
            }
 
263
            
 
264
            return from;
 
265
        }
 
266
    
 
267
</script>
 
268
<?php
 
269
}
 
270
function _shn_ims_get_org_loc_parents($child)
 
271
{
 
272
    global $global;
 
273
    $db=$global['db'];
 
274
    $q="SELECT search_id,name,location.location_id FROM location_details,location WHERE poc_uuid='{$child}' AND location_details.location_id=location.location_id";
 
275
    $res_temp=$db->Execute($q);
 
276
    $final=array();
 
277
    $final[0]=$res_temp->fields[0];
 
278
    $final[1]=$res_temp->fields[1];
 
279
    $final[2]=$res_temp->fields[2];
 
280
 
 
281
    $bsd_village=$res_temp->fields[0];
 
282
    $loc=split("\.", $bsd_village);
 
283
    $loc_return=array();
 
284
    for($k=0;$k<count($loc)-1;$k++){
 
285
        $cur=$cur.$loc[$k];
 
286
        $temp=array();
 
287
        $temp[0]=$cur;     
 
288
        $q="SELECT name,location_id FROM location WHERE search_id='$cur'";
 
289
        $res_loc=$db->Execute($q);
 
290
        $temp[1]=$res_loc->fields[0];
 
291
        $temp[2]=$res_loc->fields[1];
 
292
        array_push(
 
293
            $loc_return,
 
294
            $temp
 
295
            );
 
296
        if($k!=count($loc)-1){
 
297
            $cur=$cur.".";
 
298
        }
 
299
    }
 
300
    array_push(
 
301
        $loc_return,
 
302
        $final
 
303
    );
 
304
     return $loc_return;
 
305
}
 
306
 
 
307
 
 
308
 
 
309
function _shn_ims_display_gender($error=false,$value=NULL,$label=NULL)
 
310
{
 
311
    if($value!=NULL){
 
312
        $extra_opts['value']=$value;
 
313
        //$extra_opts['req']=false;
 
314
    }else{
 
315
        //$extra_opts['req']=true;
 
316
    }
 
317
    $label=($label==NULL)?_t("Gender:"):$label;
 
318
    shn_form_opt_select('opt_gender',$label,$select_opts,$extra_opts);
 
319
}
 
320
function _shn_ims_display_org_type($error=false,$multi=false,$value=NULL)
 
321
{
 
322
    if($value!=NULL){
 
323
        $extra_opts['value']=$value;
 
324
       // $extra_opts['req']=false;
 
325
    }else{
 
326
      //  $extra_opts['req']=true;
 
327
    }
 
328
    $extra_opts['req']=true;
 
329
    if($multi){
 
330
        $select_opts="multiple='true'";
 
331
    }else{
 
332
        //$select_opts="multiple='false'";;
 
333
    }  
 
334
    shn_form_opt_select('inventory_type','',$select_opts,$extra_opts);
 
335
}
 
336
 
 
337
function _shn_ims_display_sector($error=false,$value=NULL)
 
338
{
 
339
    if($value!=NULL){
 
340
        $extra_opts['value']=$value;
 
341
       // $extra_opts['req']=false;
 
342
    }else{
 
343
       // $extra_opts['req']=true;
 
344
    }
 
345
    $extra_opts['req']=true;
 
346
    shn_form_opt_multi_select('opt_sector_type','','multiple="true"',$extra_opts);
 
347
}
 
348
 
 
349
function _shn_ims_display_contact_person($error=false,$org=true,$po_uuid=null)
 
350
{
 
351
    if(!shn_is_null($po_uuid)){
 
352
        global $global;
 
353
        $db=$global['db'];
 
354
        $q = "SELECT address FROM location_details WHERE poc_uuid='{$po_uuid}'";
 
355
        $res_addr=$db->Execute($q); 
 
356
        $contact_address=$res_addr->fields[0];
 
357
        $q = "SELECT contact_value FROM contact WHERE pgoc_uuid='{$po_uuid}' AND opt_contact_type='curr'";
 
358
        $res_phone=$db->Execute($q);
 
359
        $contact_phone=$res_phone->fields[0];
 
360
        $q = "SELECT contact_value FROM contact WHERE pgoc_uuid='{$po_uuid}' AND opt_contact_type='pmob'";
 
361
        $res_mobile=$db->Execute($q);
 
362
        $contact_mobile=$res_mobile->fields[0];
 
363
        if($org){
 
364
            $q = "SELECT contact_value FROM contact WHERE pgoc_uuid='{$po_uuid}' AND opt_contact_type='name'";
 
365
            $res_name=$db->Execute($q);
 
366
            $contact_name=$res_name->fields[0];
 
367
        }
 
368
        $q = "SELECT contact_value FROM contact WHERE pgoc_uuid='{$po_uuid}' AND opt_contact_type='fax'";
 
369
        $res_fax=$db->Execute($q);
 
370
        $contact_fax=$res_fax->fields[0];
 
371
        $q = "SELECT contact_value FROM contact WHERE pgoc_uuid='{$po_uuid}' AND opt_contact_type='email'";
 
372
        $res_email=$db->Execute($q);
 
373
        $contact_email=$res_email->fields[0];
 
374
        $q = "SELECT contact_value FROM contact WHERE pgoc_uuid='{$po_uuid}' AND opt_contact_type='web'";
 
375
        $res_web=$db->Execute($q);
 
376
        $contact_web=$res_web->fields[0];
 
377
    }
 
378
    if($org){
 
379
        $extra_opts['value']=$contact_name;
 
380
        shn_form_text(_t("Name : "),'contact_name','size="50"',$extra_opts); 
 
381
    }
 
382
    $extra_opts['value']=$contact_address;
 
383
    shn_form_text(_t("Address : "),'contact_add','size="50"',$extra_opts); 
 
384
    $extra_opts['value']=$contact_phone;
 
385
    shn_form_text(_t("Phone : "),'contact_phone','size="50"',$extra_opts); 
 
386
    $extra_opts['value']=$contact_mobile;
 
387
    shn_form_text(_t("Mobile No : "),'contact_mobile','size="50"',$extra_opts);
 
388
    $extra_opts['value']=$contact_fax;
 
389
    shn_form_text(_t("Fax : "),'contact_fax','size="50"',$extra_opts); 
 
390
    $extra_opts['value']=$contact_email;
 
391
    shn_form_text(_t("Email : "),'contact_mail','size="50"',$extra_opts); 
 
392
    $extra_opts['value']=$contact_web;
 
393
    shn_form_text(_t("Website : "),'contact_web','size="50"',$extra_opts);
 
394
}
 
395
 
 
396
function _shn_ims_display_org_facilities($error=false,$org_id=false)
 
397
{
 
398
    if(!shn_is_null($org_id)){
 
399
        global $global;
 
400
       $db=$global['db'];
 
401
        $q = "SELECT man_power,equipment,resources,space FROM  ims_inventory_records WHERE inv_uuid='{$org_id}'";
 
402
        $res_org=$db->Execute($q);
 
403
        if(!$res_org==NULL && !$res_org->EOF){
 
404
            $man_power=$res_org->fields[0];
 
405
            $equipment=$res_org->fields[1]; 
 
406
            $resources=$res_org->fields[2]; 
 
407
        $space=$res_org->fields[3];
 
408
        }
 
409
    }
 
410
    $extra_opts['value']=$man_power;
 
411
    shn_form_text(_t("Man Power : "),'man_power','size="50"',$extra_opts);
 
412
    $extra_opts['value']=$equipment;
 
413
    shn_form_text(_t("Equipment : "),'equipment','size="50"',$extra_opts);
 
414
    $extra_opts['value']=$resources;
 
415
    shn_form_text(_t("Other relevant resources : "),'resources','size="50"',$extra_opts);
 
416
    $extra_opts['value']=$space;
 
417
    shn_form_text(_t("Inventory Space : "),'space','size="50"',$extra_opts);
 
418
  
 
419
}
 
420
 
 
421
function _shn_ims_display_logininfo($error=false)
 
422
{
 
423
// for get login info
 
424
    $login_info = array(
 
425
                       array('desc'=>_t("Account Name : "),'type'=>"text",'size'=>20,'name'=>'account_name','br'=>1),
 
426
                    array('desc'=>_t("* User Name for Login: "),'type'=>"text",'size'=>20,'name'=>'user_name','br'=>1),
 
427
                    array('desc'=>_t("* Password for Login: "),'type'=>"password",'size'=>20,'name'=>'password','br'=>1),
 
428
                    array('desc'=>_t("* Confirm Password: "),'type'=>"password",'size'=>20,'name'=>'re_password','br'=>1)
 
429
    ); // end of getting logging info
 
430
    return $login_info;
 
431
}
 
432
 
 
433
function _shn_ims_display_extra($error=false)
 
434
{
 
435
    shn_form_checkbox(_t("Add Operation<br> (Your Organization might be having branches or carrying out relief operations in this disaster. Then information of those operations is useful : )"),'chk_branch',null,$chkbox_opts);
 
436
}
 
437
 
 
438
//-----------------------------------------------------------------------------------------------------------------
 
439
function _shn_ims_expire_date_check()
 
440
{
 
441
    global $global;
 
442
    $db=$global["db"];
 
443
    $state="expired";
 
444
    $state1="destroyed";
 
445
        
 
446
    $sql="UPDATE ims_item_records SET state='$state' WHERE ((TO_DAYS(expire_date) - TO_DAYS(CURRENT_DATE()))<0 and state!='$state1');";
 
447
    $ims=$db->Execute($sql);    
 
448
}
 
449
 
 
450
function _shn_ims_date_validate($date)
 
451
{
 
452
    $error_flag1=false;
 
453
    
 
454
 
 
455
    list($year,$month,$day)=split('[/.-]', $date);
 
456
    if($year<0 || $year>2100 )
 
457
    {
 
458
        //add_error("Year should be a value between 1900 and 2100");
 
459
        $error_flag1=true;
 
460
    }
 
461
    if($month<0 || $month>12 )
 
462
    {
 
463
        //add_error("Invalid Month");
 
464
        $error_flag1=true;
 
465
    }
 
466
    if($day<0)
 
467
    {
 
468
        //add_error("Invald Day");
 
469
        $error_flag1=true;
 
470
    }
 
471
    if(($month==1 || $month==3 || $month==5 || $month==7 || $month==8 || $month==10 || $month==12) && $day>31)
 
472
    {
 
473
        //add_error("The month doesn't have such a date ");
 
474
        $error_flag1=true;
 
475
    }
 
476
    if($month==2 && $day>29)
 
477
    {
 
478
        //add_error(SHN_ERR_IMS_INVALID_DATE);
 
479
        $error_flag=true;
 
480
    }
 
481
    if(($month==4 || $month==6 || $month==9 || $month==11) && $day>30)
 
482
    {
 
483
        //add_error("The month doesn't have such a date ");
 
484
        $error_flag1=true;
 
485
    }
 
486
 
 
487
    if($error_flag1==true)
 
488
    {
 
489
        add_error(SHN_ERR_IMS_INVALID_DATE);
 
490
        return $error_flag1;
 
491
    }
 
492
    else
 
493
    {
 
494
        return $error_flag1;
 
495
    }
 
496
}
 
497
 
 
498
function _shn_ims_find_catalogid($catalog_id)
 
499
{
 
500
    global $global;
 
501
    $db=$global["db"];
 
502
    $catalog_found=false;
 
503
    $sql1="SELECT item_id FROM ims_item_records WHERE catalog_id='$catalog_id';";
 
504
    $ims1=$db->Execute($sql1);
 
505
    $item_id=$ims1->fields['item_id'];
 
506
        
 
507
                    
 
508
    $sql2="SELECT inv_id FROM ims_reorder_level WHERE catalog_id='$catalog_id';";
 
509
    $ims2=$db->Execute($sql2);
 
510
    $inv_id=$ims2->fields['inv_id'];
 
511
 
 
512
    if($item_id!=null || $inv_id!=null)
 
513
    {
 
514
        $catalog_found=true;
 
515
    }
 
516
    else
 
517
    {
 
518
        $catalog_found=false;
 
519
    }
 
520
 
 
521
    return $catalog_found;
 
522
}
 
523
 
 
524
function _shn_ims_find_unitid($unit_id)
 
525
{
 
526
    global $global;
 
527
    $db=$global["db"];
 
528
    $unit_found=false;
 
529
 
 
530
    $sql1="SELECT item_id FROM ims_item_records WHERE unit='$unit_id';";
 
531
    $ims1=$db->Execute($sql1);
 
532
    $item_id=$ims1->fields['item_id'];
 
533
        
 
534
                    
 
535
    $sql2="SELECT inv_id FROM ims_reorder_level WHERE unit='$unit_id';";
 
536
    $ims2=$db->Execute($sql2);
 
537
    $inv_id=$ims2->fields['inv_id'];
 
538
 
 
539
    $sql3="SELECT item_id FROM ims_transfer_item WHERE unit='$unit_id';";
 
540
    $ims3=$db->Execute($sql3);
 
541
    $item_id3=$ims3->fields['item_id'];
 
542
 
 
543
    if($item_id!=null || $inv_id!=null || $item_id3!=null)
 
544
    {
 
545
        $unit_found=true;
 
546
    }
 
547
    else
 
548
    {
 
549
        $unit_found=false;
 
550
    }
 
551
 
 
552
    return $unit_found;
 
553
    
 
554
}
 
555
 
 
556
/**
 
557
 * Returns an array ready of names and locations ready to be
 
558
 * 
 
559
 * @global <type> $global
 
560
 * @global <type> $conf
 
561
 * @param <type> $inv_id
 
562
 * @return <type>
 
563
 */
 
564
function _shn_ims_gis_inv($inv_id)
 
565
{
 
566
    global $conf;
 
567
    global $global;
 
568
    $db=$global['db'];
 
569
    require_once $global['approot'] . "/inc/lib_gis/gis_fns.inc";
 
570
    if($inv_id == NULL){
 
571
        $sql1 = "SELECT * FROM ims_inventory_records;";
 
572
    } else {
 
573
        $sql1 = "SELECT * FROM ims_inventory_records WHERE inv_uuid = '$inv_id';";
 
574
    }
 
575
        $ims1 = $db->Execute($sql1);
 
576
    $ims1=$db->Execute($sql1);
 
577
        $map_array=array();
 
578
    while(!$ims1==NULL && !$ims1->EOF){
 
579
        // get coords
 
580
        $gis_result = shn_gis_get_features_item_ref($ims1->fields['inv_uuid'], array('f_coords' => '1', 'f_url' => '1'));
 
581
        if($gis_result){
 
582
            // decode coords from str -> array of x y z
 
583
            $coords = shn_gis_coord_decode($gis_result[0]['f_coords']);
 
584
            array_push($map_array,array("lat"=>$coords[0][0],"lon"=>$coords[0][1],"name"=>$ims1->fields['inventory_name'],"id"=>$ims2->fields['poc_uuid'],"url"=>"mod=ims&act=view_inv&id={$ims1->fields['inv_uuid']}"));
 
585
        }
 
586
        $ims1->MoveNext();
 
587
    }
 
588
    return $map_array;
 
589
}
 
590
 
 
591
function _shn_ims_get_location($inv_id)
 
592
{
 
593
        global $global;
 
594
        $db=$global['db'];
 
595
 
 
596
        $sql1="SELECT * FROM location_details WHERE poc_uuid='$inv_id';";
 
597
        $ims1=$db->Execute($sql1);
 
598
        
 
599
        $location_id=$ims1->fields['location_id'];
 
600
 
 
601
        //$parent_id="test";
 
602
        $string_array=array();
 
603
        $i=0;
 
604
 
 
605
        while($location_id!=NULL)
 
606
        {
 
607
                $sql2="SELECT * FROM location WHERE loc_uuid='$location_id';";
 
608
                $ims2=$db->Execute($sql2);
 
609
                $parent_id=$ims2->fields['parent_id'];
 
610
                $name=$ims2->fields['name'];
 
611
                //array_push($string_array,$location_id=>$name);
 
612
                $string_array[$i]=$name;
 
613
                $i=$i+1;
 
614
 
 
615
                $location_id=$parent_id;
 
616
        }
 
617
 
 
618
        for($j=$i-2;$j>=0;$j--)
 
619
        {
 
620
                
 
621
                $string=$string_array[$j];
 
622
                if($j==$i-2)
 
623
                {
 
624
                        $location=$string;
 
625
                }
 
626
                else
 
627
                {
 
628
                        $location=$location."->".$string;
 
629
                }
 
630
                
 
631
        }
 
632
        
 
633
        return $location;
 
634
}
 
635
 
 
636
//*******************************************************SOC*******************************************************
 
637
function _shn_ims_date_conversion($date)
 
638
{
 
639
        list($year,$month,$day) = split("-",$date);
 
640
        $convert_date=$month."-".$day."-".$year;
 
641
        return $convert_date;
 
642
}
 
643
function _shn_ims_count_days($start, $end)
 
644
{
 
645
        $string1=explode("-",$start);
 
646
        $string2=explode("-",$end);
 
647
        $numStart=mktime(0,0,0,$string1[1],$string1[2],$string1[0]);
 
648
        $numEnd=mktime(0,0,0,$string2[1],$string2[2],$string2[0]);
 
649
 
 
650
    if( $start != '0000-00-00' and $end != '0000-00-00' )
 
651
    {
 
652
        /*$timestamp_start = strtotime($start);
 
653
        $timestamp_end = strtotime($end);
 
654
        if( $timestamp_start >= $timestamp_end ) return 0;
 
655
        $start_year = date("Y",$timestamp_start);
 
656
        $end_year = date("Y", $timestamp_end);*/
 
657
        //$num_days_end = date("z",strtotime($start));
 
658
        //$num_days_end = date("z", strtotime($end));
 
659
         if($numEnd<$numStart){
 
660
                        add_error(SHN_ERR_IMS_MANU_DATE_GRT_EX_DATE_INVALID);
 
661
                return;
 
662
        }
 
663
        else{
 
664
                        $s=mktime(0,0,0,date(m),date(d),date(Y));
 
665
                        if($numEnd<$s){
 
666
                                add_error(SHN_ERR_IMS_CUR_DATE_GRT_EX_DATE_INVALID);            
 
667
                                return;
 
668
                        }
 
669
                        if($numStart>$s){       
 
670
                                add_error(SHN_ERR_IMS_MANU_DATE_GRT_CUR_DATE_INVALID);
 
671
                                return;
 
672
                        }               
 
673
        }
 
674
        /*$num_days = 0;
 
675
        $i = 0;
 
676
        if( $end_year > $start_year )
 
677
        {
 
678
           while( $i < ( $end_year - $start_year ) )
 
679
           {
 
680
              $num_days = $num_days + date("z", strtotime(($start_year + $i)."-12-31"));
 
681
              $i++;
 
682
           }
 
683
         }
 
684
         return ( $num_days_end + $num_days ) - $num_days_start;
 
685
    }
 
686
    else
 
687
    {
 
688
         return 0;*/
 
689
     }
 
690
}
 
691
 
 
692
function _shn_ims_smoothing($forcasted_value,$actual_value,$alpha)
 
693
{
 
694
         //ft+1=(1-?)ft+?*at
 
695
                
 
696
        //$alpha=0.3;
 
697
        $forcast_value=((1-$alpha)*$forcasted_value)+($alpha*$actual_value);
 
698
        return $forcast_value;
 
699
        
 
700
}
 
701
 
 
702
function _shn_ims_datediff($dformat, $endDate, $beginDate)
 
703
{
 
704
$date_parts1=explode($dformat, $beginDate);
 
705
$date_parts2=explode($dformat, $endDate);
 
706
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
 
707
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
 
708
return $end_date - $start_date;
 
709
 
 
710
}
 
711
 
 
712
function _shn_ims_total_amount($catalog_id,$inv_id)
 
713
{
 
714
        global $global;
 
715
        $db=$global["db"];
 
716
 
 
717
        //if(!$inv_id==NULL)
 
718
        //{
 
719
                $sql1="SELECT * FROM ims_item_records WHERE inv_id='$inv_id' AND catalog_id='$catalog_id';";
 
720
                $ims1=$db->Execute($sql1);
 
721
                $sum_amount=0;
 
722
                while(!$ims1==NULL && !$ims1->EOF)
 
723
                {
 
724
                        $amount=$ims2->fields['amount'];
 
725
                        $unit=$ims2->fields['unit'];
 
726
                        
 
727
                        $base_unit=convert_to_base_unit($unit);
 
728
                        $converted_amount=$amount*unit_converter($base_unit,$unit);
 
729
            
 
730
                        $sum_amount=$sum_amount + $converted_amount;
 
731
                        $ims1->MoveNext();                      
 
732
                }
 
733
                
 
734
        //}
 
735
 
 
736
        return $sum_amount;
 
737
 
 
738
        
 
739
}
 
740
 
 
741
function _shn_ims_sum_amount($catalog_id,$inv_id)
 
742
{
 
743
        global $global;
 
744
        $db=$global["db"];
 
745
        if($inv_id=="all")
 
746
        {
 
747
                $sql1="SELECT * FROM ims_item_records WHERE catalog_id='$catalog_id';";
 
748
                $ims1=$db->Execute($sql1);
 
749
        }
 
750
        else
 
751
        {
 
752
                $sql1="SELECT * FROM ims_item_records WHERE inv_id='$inv_id' AND catalog_id='$catalog_id';";
 
753
                $ims1=$db->Execute($sql1);
 
754
        }
 
755
        $sum_amount=0;
 
756
        while(!$ims1==NULL && !$ims1->EOF)
 
757
        {
 
758
                $amount=$ims1->fields['amount'];
 
759
                $unit=$ims1->fields['unit'];
 
760
                        
 
761
                $base_unit=convert_to_base_unit($unit);
 
762
                $converted_amount=$amount*unit_converter($base_unit,$unit);
 
763
                
 
764
                $sum_amount=$sum_amount + $converted_amount;
 
765
                $ims1->MoveNext();                      
 
766
        }
 
767
        return $sum_amount;
 
768
}
 
769
 
 
770
function _shn_ims_first_data($inv_id,$catalog_id)
 
771
{
 
772
        global $global;
 
773
        $db=$global["db"];
 
774
        $sql1="SELECT * FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
775
        $ims1=$db->Execute($sql1);
 
776
        $date_inv=$ims1->fields['added_date'];
 
777
        $date_inv_convert=_shn_ims_date_conversion($date_inv);
 
778
//has to be commented   
 
779
        $date_inv_convert="01-08-2006";
 
780
        
 
781
        $current_date=date("m-d-Y");
 
782
        $number_of_days=_shn_ims_datediff('-',$current_date,$date_inv_convert);
 
783
        $temp=$number_of_days-7;
 
784
        if($temp>0)
 
785
        {
 
786
                $days_after_inv_date=date("Y-m-d",time() - ($temp*24*60*60));
 
787
        }
 
788
        if($temp<0)
 
789
        {
 
790
                $days_after_inv_date=date("Y-m-d",time() -($number_of_days*24*60*60));
 
791
                
 
792
        }
 
793
 
 
794
        $sql2="SELECT * FROM ims_item_records WHERE catalog_id='$catalog_id' AND inv_id='$inv_id' AND (TO_DAYS(inserted_date) - TO_DAYS($date_inv))<7;";
 
795
        $ims2=$db->Execute($sql2);
 
796
        $sum_amount=0;
 
797
        while(!$ims2==NULL && !$ims2->EOF)
 
798
        {
 
799
                $amount=$ims1->fields['amount'];
 
800
                $unit=$ims1->fields['unit'];
 
801
                        
 
802
                $base_unit=convert_to_base_unit($unit);
 
803
                $converted_amount=$amount*unit_converter($base_unit,$unit);
 
804
            
 
805
                $sum_amount=$sum_amount + $converted_amount;
 
806
                $ims2->MoveNext();              
 
807
        }
 
808
        //return $sum_amount;
 
809
        //$week=date("d-m-Y",time() + 604800);
 
810
        //$date_amount_inv=date("Y-m-d",time() - 604800);
 
811
        //$date_inv_week=_shn_ims_datediff('-',$date_inv_week,$date);
 
812
        
 
813
}
 
814
 
 
815
function _shn_ims_smoothing_constant($catalog_id,$inv_id)
 
816
{
 
817
        global $global;
 
818
        $db=$global["db"];
 
819
 
 
820
        $sql1="SELECT * FROM ims_optimization WHERE catalog_id='$catalog_id' AND inv_id='$inv_id' ORDER BY week DESC;";
 
821
        $ims1=$db->Execute($sql1);
 
822
        
 
823
        $week=$ims1->fields['week'];
 
824
        
 
825
        $temp=$week+1;
 
826
        $alpha=2/$temp;
 
827
 
 
828
        return $alpha;
 
829
        
 
830
 
831
 
 
832
function _shn_ims_weekafter($fyear, $fmonth, $fday)
 
833
{
 
834
        return date ("Y-m-d", mktime (0,0,0,$fmonth,$fday+7,$fyear));
 
835
}
 
836
 
 
837
function _shn_ims_timeafter($fyear, $fmonth, $fday,$time_period)
 
838
{
 
839
        return date ("Y-m-d", mktime (0,0,0,$fmonth,$fday+$time_period,$fyear));
 
840
}
 
841
 
 
842
function _shn_ims_insert_data_to_predict($catalog_id,$inv_id,$time_period)
 
843
{
 
844
        global $global;
 
845
        $db=$global["db"];
 
846
 
 
847
        $sql1="SELECT * FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
848
        $ims1=$db->Execute($sql1);
 
849
        $date_inv=$ims1->fields['added_date'];
 
850
        $date_inv_convert=_shn_ims_date_conversion($date_inv);
 
851
        $current_date=date("Y-m-d");
 
852
 
 
853
        $week=0;
 
854
        $date_diff=_shn_ims_count_days($date_inv,$current_date);
 
855
 
 
856
        $item_state_destroyed="destroyed";
 
857
        
 
858
        for($i=$date_diff;$i>=0;$i-=$time_period)       
 
859
        //while(_shn_ims_count_days($date_inv,$current_date)<1 && _shn_ims_count_days($date_inv,$current_date)>0 )
 
860
        {
 
861
                $date_inv_temp=$date_inv;
 
862
                //$sql2="SELECT * FROM ims_item_records WHERE inv_id='$inv_id' AND catalog_id='$catalog_id' AND (TO_DAYS(inserted_date) - TO_DAYS($date_inv_temp))<7;";
 
863
                $sql2="SELECT * FROM ims_item_records WHERE inv_id='$inv_id' AND catalog_id='$catalog_id' AND state!='$item_state_destroyed';";
 
864
                $ims2=$db->Execute($sql2);
 
865
                
 
866
                $sum_amount=0;
 
867
                $sum_amount_temp=0;
 
868
                while(!$ims2==NULL && !$ims2->EOF)
 
869
                {
 
870
                        $amount=$ims2->fields['amount'];
 
871
                        
 
872
                        $unit=$ims2->fields['unit'];
 
873
                        $inserted_date=$ims2->fields['inserted_date'];
 
874
 
 
875
                        if(_shn_ims_count_days($date_inv_temp,$current_date)<_shn_ims_count_days($inserted_date,$current_date)||_shn_ims_count_days($date_inv_temp,$current_date)==_shn_ims_count_days($inserted_date,$current_date))
 
876
                        {
 
877
                        
 
878
                        $base_unit=convert_to_base_unit($unit);
 
879
                        $converted_amount=$amount*unit_converter($base_unit,$unit);
 
880
            
 
881
                        $sum_amount=$sum_amount + $converted_amount;
 
882
                        $sum_amount_temp=$sum_amount;
 
883
                        }
 
884
                        else
 
885
                        {
 
886
                                $sum_amount_temp=$sum_amount;
 
887
                        }
 
888
                        $ims2->MoveNext();
 
889
                }
 
890
                $actual_value=$sum_amount_temp;
 
891
                
 
892
                $week=$week+1;
 
893
                $temp=$week+1;
 
894
                $alpha=2/$temp;
 
895
                
 
896
                if($week==1)
 
897
                {
 
898
                        $forecasted_value=0;
 
899
                        
 
900
                        $forecasted_value_temp=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
901
                }
 
902
                else
 
903
                {
 
904
                        
 
905
                        //$alpha_smoothing=1-$alpha;
 
906
                        $forecasted_value=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
907
                        //$actual_value_smoothing=$alpha*$actual_value;
 
908
                        //$forecasted_value_smoothing=$alpha_smoothing*$forcasted_value;
 
909
                        //$forecasted_value=$forecasted_value_smoothing + $actual_value_smoothing;
 
910
                }
 
911
                
 
912
                $sql3="INSERT INTO ims_optimization (catalog_id,inv_id,week,actual_value,forecasted_value,unit) VALUES ('$catalog_id','$inv_id','$week','$sum_amount_temp','$forecasted_value','$base_unit');"; 
 
913
                $ims3=$db->Execute($sql3);
 
914
                
 
915
                list($year,$month,$day) = split("-",$date_inv);
 
916
                $date_inv=_shn_ims_timeafter($year, $month, $day,$time_period);
 
917
                
 
918
                if($week==1)
 
919
                {
 
920
                        $forecasted_value=$forecasted_value_temp;
 
921
                }
 
922
                else
 
923
                {
 
924
                        ;
 
925
                }
 
926
        }
 
927
 
 
928
}
 
929
 
 
930
function _shn_ims_predict_sending_items($catalog_id,$inv_id,$time_period)
 
931
{
 
932
        global $global;
 
933
        $db=$global["db"];
 
934
 
 
935
        $sql1="SELECT * FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
936
        $ims1=$db->Execute($sql1);
 
937
        $date_inv=$ims1->fields['added_date'];
 
938
        $date_inv_convert=_shn_ims_date_conversion($date_inv);
 
939
        $current_date=date("Y-m-d");
 
940
 
 
941
        $week=0;
 
942
        $date_diff=_shn_ims_count_days($date_inv,$current_date);
 
943
 
 
944
        $item_state_destroyed="destroyed";
 
945
        
 
946
        for($i=$date_diff;$i>=0;$i-=$time_period)       
 
947
        //while(_shn_ims_count_days($date_inv,$current_date)<1 && _shn_ims_count_days($date_inv,$current_date)>0 )
 
948
        {
 
949
                $date_inv_temp=$date_inv;
 
950
                //$sql2="SELECT * FROM ims_item_records WHERE inv_id='$inv_id' AND catalog_id='$catalog_id' AND (TO_DAYS(inserted_date) - TO_DAYS($date_inv_temp))<7;";
 
951
                $sql2="SELECT * FROM ims_transfer_item WHERE inv_id_from='$inv_id' AND catalog_id='$catalog_id';";
 
952
                $ims2=$db->Execute($sql2);
 
953
                
 
954
                $sum_amount=0;
 
955
                $sum_amount_temp=0;
 
956
                $amount=0;
 
957
                $converted_amount=0;
 
958
                while(!$ims2==NULL && !$ims2->EOF)
 
959
                {
 
960
                        $amount=$ims2->fields['amount_send'];
 
961
                        
 
962
                        $unit=$ims2->fields['unit'];
 
963
                        $inserted_date=$ims2->fields['date_send'];
 
964
 
 
965
                        if(_shn_ims_count_days($date_inv_temp,$current_date)==_shn_ims_count_days($inserted_date,$current_date))
 
966
                        {
 
967
                        
 
968
                        $base_unit=convert_to_base_unit($unit);
 
969
                        $converted_amount=$amount*unit_converter($base_unit,$unit);
 
970
            
 
971
                        $sum_amount=$sum_amount + $converted_amount;
 
972
                        $sum_amount_temp=$sum_amount;
 
973
                        }
 
974
                        else
 
975
                        {
 
976
                                $sum_amount_temp=$sum_amount;
 
977
                        }
 
978
                        $ims2->MoveNext();
 
979
                }
 
980
                $actual_value=$sum_amount_temp;
 
981
                
 
982
                $week=$week+1;
 
983
                $temp=$week+1;
 
984
                $alpha=2/$temp;
 
985
                
 
986
                
 
987
                if($week==1)
 
988
                {
 
989
                        $forecasted_value=0;
 
990
                        
 
991
                        $forecasted_value_temp=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
992
                }
 
993
                else
 
994
                {
 
995
                        
 
996
                        $forecasted_value=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
997
                        
 
998
                }
 
999
                
 
1000
                $sql3="INSERT INTO ims_optimization (catalog_id,inv_id,week,actual_value,forecasted_value,unit) VALUES ('$catalog_id','$inv_id','$week','$sum_amount_temp','$forecasted_value','$base_unit');"; 
 
1001
                $ims3=$db->Execute($sql3);
 
1002
                $sum_amount_temp=0;
 
1003
                
 
1004
                list($year,$month,$day) = split("-",$date_inv);
 
1005
                $date_inv=_shn_ims_timeafter($year, $month, $day,$time_period);
 
1006
                
 
1007
                if($week==1)
 
1008
                {
 
1009
                        $forecasted_value=$forecasted_value_temp;
 
1010
                }
 
1011
                else
 
1012
                {
 
1013
                        ;
 
1014
                }
 
1015
        }
 
1016
 
 
1017
}
 
1018
 
 
1019
function _shn_ims_predict_receiving_items($catalog_id,$inv_id,$time_period)
 
1020
{
 
1021
 
 
1022
        global $global;
 
1023
        $db=$global["db"];
 
1024
 
 
1025
        $sql1="SELECT * FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
1026
        $ims1=$db->Execute($sql1);
 
1027
        $date_inv=$ims1->fields['added_date'];
 
1028
        $date_inv_convert=_shn_ims_date_conversion($date_inv);
 
1029
        $current_date=date("Y-m-d");
 
1030
 
 
1031
        $week=0;
 
1032
        $date_diff=_shn_ims_count_days($date_inv,$current_date);
 
1033
 
 
1034
        $received_item_id=-1;
 
1035
 
 
1036
        $item_state_destroyed="destroyed";
 
1037
        
 
1038
        for($i=$date_diff;$i>=0;$i-=$time_period)       
 
1039
        //while(_shn_ims_count_days($date_inv,$current_date)<1 && _shn_ims_count_days($date_inv,$current_date)>0 )
 
1040
        {
 
1041
                $date_inv_temp=$date_inv;
 
1042
                
 
1043
                $sql2="SELECT * FROM ims_transfer_item WHERE inv_id_to='$inv_id' AND catalog_id='$catalog_id' AND received_item_id!='$received_item_id';";
 
1044
                $ims2=$db->Execute($sql2);
 
1045
                $sum_amount=0;
 
1046
                $sum_amount_temp=0;
 
1047
                while(!$ims2==NULL && !$ims2->EOF)
 
1048
                {
 
1049
                        $amount=$ims2->fields['amount_received'];
 
1050
                        
 
1051
                        $unit=$ims2->fields['unit'];
 
1052
                        $inserted_date=$ims2->fields['date_received'];
 
1053
 
 
1054
                        
 
1055
                        if(_shn_ims_count_days($date_inv_temp,$current_date)==_shn_ims_count_days($inserted_date,$current_date))
 
1056
                        {
 
1057
                        
 
1058
                        $base_unit=convert_to_base_unit($unit);
 
1059
                        $converted_amount=$amount*unit_converter($base_unit,$unit);
 
1060
            
 
1061
                        $sum_amount=$sum_amount + $converted_amount;
 
1062
                        $sum_amount_temp=$sum_amount;
 
1063
                        }
 
1064
                        else
 
1065
                        {
 
1066
                                $sum_amount_temp=$sum_amount;
 
1067
                        }
 
1068
                        $ims2->MoveNext();
 
1069
                }
 
1070
                $actual_value=$sum_amount_temp;
 
1071
                
 
1072
                $week=$week+1;
 
1073
                $temp=$week+1;
 
1074
                $alpha=2/$temp;
 
1075
                
 
1076
                
 
1077
                if($week==1)
 
1078
                {
 
1079
                        $forecasted_value=0;
 
1080
                        
 
1081
                        $forecasted_value_temp=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
1082
                }
 
1083
                else
 
1084
                {
 
1085
                        
 
1086
                        $forecasted_value=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
1087
                        
 
1088
                }
 
1089
                
 
1090
                $sql3="INSERT INTO ims_optimization (catalog_id,inv_id,week,actual_value,forecasted_value,unit) VALUES ('$catalog_id','$inv_id','$week','$sum_amount_temp','$forecasted_value','$base_unit');"; 
 
1091
                $ims3=$db->Execute($sql3);
 
1092
                
 
1093
                $sum_amount=0;
 
1094
                
 
1095
                list($year,$month,$day) = split("-",$date_inv);
 
1096
                $date_inv=_shn_ims_timeafter($year, $month, $day,$time_period);
 
1097
                if($week==1)
 
1098
                {
 
1099
                        $forecasted_value=$forecasted_value_temp;
 
1100
                }
 
1101
                else
 
1102
                {
 
1103
                        ;
 
1104
                }
 
1105
        }
 
1106
 
 
1107
}
 
1108
 
 
1109
 
 
1110
 
 
1111
function _shn_ims_predict_both_items($catalog_id,$inv_id,$time_period)
 
1112
{
 
1113
 
 
1114
        global $global;
 
1115
        $db=$global["db"];
 
1116
 
 
1117
        $sql1="SELECT * FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
1118
        $ims1=$db->Execute($sql1);
 
1119
        $date_inv=$ims1->fields['added_date'];
 
1120
        $date_inv_convert=_shn_ims_date_conversion($date_inv);
 
1121
        $current_date=date("Y-m-d");
 
1122
 
 
1123
        $week=0;
 
1124
        $date_diff=_shn_ims_count_days($date_inv,$current_date);
 
1125
 
 
1126
        $received_item_id=-1;
 
1127
 
 
1128
        $item_state_destroyed="destroyed";
 
1129
        
 
1130
        for($i=$date_diff;$i>=0;$i-=$time_period)       
 
1131
        //while(_shn_ims_count_days($date_inv,$current_date)<1 && _shn_ims_count_days($date_inv,$current_date)>0 )
 
1132
        {
 
1133
                $date_inv_temp=$date_inv;
 
1134
                
 
1135
                $sql2="SELECT * FROM ims_transfer_item WHERE inv_id_to='$inv_id' AND catalog_id='$catalog_id' AND received_item_id!='$received_item_id';";
 
1136
                $ims2=$db->Execute($sql2);
 
1137
                
 
1138
                $sum_amount=0;
 
1139
                $sum_amount_temp=0;
 
1140
                while(!$ims2==NULL && !$ims2->EOF)
 
1141
                {
 
1142
                        $amount=$ims2->fields['amount_received'];
 
1143
                        
 
1144
                        $unit=$ims2->fields['unit'];
 
1145
                        $inserted_date=$ims2->fields['date_received'];
 
1146
 
 
1147
                        if(_shn_ims_count_days($date_inv_temp,$current_date)<_shn_ims_count_days($inserted_date,$current_date)||_shn_ims_count_days($date_inv_temp,$current_date)==_shn_ims_count_days($inserted_date,$current_date))
 
1148
                        {
 
1149
                        
 
1150
                        $base_unit=convert_to_base_unit($unit);
 
1151
                        $converted_amount=$amount*unit_converter($base_unit,$unit);
 
1152
            
 
1153
                        $sum_amount=$sum_amount + $converted_amount;
 
1154
                        $sum_amount_temp=$sum_amount;
 
1155
                        }
 
1156
                        else
 
1157
                        {
 
1158
                                $sum_amount_temp=$sum_amount;
 
1159
                        }
 
1160
                        $ims2->MoveNext();
 
1161
                }
 
1162
                $actual_value=$sum_amount_temp;
 
1163
                
 
1164
                $week=$week+1;
 
1165
                $temp=$week+1;
 
1166
                $alpha=2/$temp;
 
1167
                
 
1168
                $forecasted_value=_shn_ims_smoothing($forecasted_value,$actual_value,$alpha);
 
1169
                
 
1170
                $sql3="INSERT INTO ims_optimization (catalog_id,inv_id,week,actual_value,forecasted_value,unit) VALUES ('$catalog_id','$inv_id','$week','$sum_amount_temp','$forecasted_value','$base_unit');"; 
 
1171
                $ims3=$db->Execute($sql3);
 
1172
                
 
1173
                $sum_amount=0;
 
1174
                
 
1175
                list($year,$month,$day) = split("-",$date_inv);
 
1176
                $date_inv=_shn_ims_timeafter($year, $month, $day,$time_period);
 
1177
                
 
1178
        }
 
1179
 
 
1180
}
 
1181
 
 
1182
 
 
1183
 
 
1184
function _shn_ims_get_alternate_item_amount($catalog_id,$inv_id)
 
1185
{
 
1186
        $item_state="destroyed";
 
1187
        $sql3="SELECT * FROM ims_alternate WHERE catalog_id='$catalog_id' AND inv_id='$inv_id';";
 
1188
        $ims3=$db->Execute($sql3);
 
1189
        
 
1190
        while(!$ims3==NULL && !$ims3->EOF)
 
1191
        {
 
1192
                $alternate=$ims3->fields['alternate'];
 
1193
                
 
1194
                if($inv_id==0)
 
1195
                {
 
1196
                        $sql4="SELECT * FROM ims_item_records WHERE catalog_id='$alternate' AND state!='$item_state';";
 
1197
                }
 
1198
                else
 
1199
                {
 
1200
                        $sql4="SELECT * FROM ims_item_records WHERE catalog_id='$alternate' AND inv_id='$inv_id' AND state!='$item_state';";
 
1201
                }
 
1202
                $ims4=$db->Execute($sql4);
 
1203
                
 
1204
                while(!$ims4==NULL && !$ims4->EOF)
 
1205
                {
 
1206
                        $item_id=$ims4->fields["item_id"];
 
1207
                        $suplier_id=$ims4->fields["suplier_id"];
 
1208
                        $suplier_name_array=_shn_or_get_suplier_name($suplier_id);
 
1209
                        $suplier_name=$suplier_name_array[$suplier_id];
 
1210
                        $item_name=$ims4->fields['item_name'];
 
1211
 
 
1212
                        $inventory_id=$ims4->fields['inv_id'];
 
1213
            
 
1214
            
 
1215
                        $amount=$ims4->fields["amount"];
 
1216
            
 
1217
            
 
1218
                        $unit=$ims4->fields["unit"];
 
1219
                        $base_unit=convert_to_base_unit($unit);
 
1220
            
 
1221
                        $converted_amount=$amount*unit_converter($base_unit,$unit);
 
1222
                        
 
1223
                        $sum_amount=$sum_amount + $converted_amount;
 
1224
                        $alternate_sum_amount=$alternate_sum_amount + $converted_amount;
 
1225
                        
 
1226
                        $unit_name=get_unit_name($unit);
 
1227
                        $base_unit_name=get_unit_name($base_unit);
 
1228
                        $state=$ims->fields['state'];
 
1229
         
 
1230
        
 
1231
                        $sql5="SELECT inventory_name FROM ims_inventory_records WHERE inv_uuid='$inventory_id';";
 
1232
                        $ims5=$db->Execute($sql5);
 
1233
                        $inventory_name=$ims5->fields['inventory_name'];
 
1234
 
 
1235
                        $ims4->MoveNext();
 
1236
                }
 
1237
 
 
1238
                $ims3->MoveNext();
 
1239
        }
 
1240
 
 
1241
}
 
1242
 
 
1243
 
 
1244
 
 
1245
 
 
1246
function _shn_ims_bar_chart_inventory($data,$catalog_array,$i,$y)
 
1247
{
 
1248
//include ( "../3rd/phplot1/phplot.php");
 
1249
$graph = new PHPlot(700,500);
 
1250
 
 
1251
//$graph->SetDataType( "linear-linear"); //text-data
 
1252
$graph->SetDataType("text-data");  //Must be called before SetDataValues
 
1253
 
 
1254
 
 
1255
$graph->SetDataValues($data);
 
1256
 $graph->SetFileFormat("png"); 
 
1257
 
 
1258
$graph->SetImageArea(700,500);
 
1259
/*commented types are ok with the data*/
 
1260
//$graph->SetPlotType( "lines");
 
1261
//$graph->SetPlotType( "linepoints");
 
1262
//$graph->SetPlotType( "points");
 
1263
//$graph->SetPlotType( "area");
 
1264
//$graph->SetPlotType( "thinbarline");
 
1265
//$graph->SetPlotType( "squared");
 
1266
//$graph->SetPlotType( "pie");
 
1267
$graph->SetPlotType( "bars");
 
1268
//$graph->SetPlotType( "stackedbars");
 
1269
 
 
1270
$graph->SetTitleFontSize( "5"); // the max value of font size is 5
 
1271
$graph->SetTitle( "Forecasting by Inventory");
 
1272
 
 
1273
$graph->SetLegend($catalog_array);
 
1274
//$graph->SetPlotAreaWorld(2000,0,2050,2500); // parameters = ($starting_val_of_x_axis, $s_val_of_y, $ending_val_of_x, $e_val_of_y)
 
1275
$graph->SetPlotBgColor( "red");
 
1276
$graph->SetPlotBorderType( "left");
 
1277
$graph->SetBackgroundColor( "orange"); // set the chart background
 
1278
 
 
1279
//you can set the x title by one line or as commented three lines
 
1280
$graph->SetXTitle("Item", "both"); // para = ($title, $position); $postion= plotup or plotdown or both or none
 
1281
$graph->SetYTitle('Amount',"both"); // plotright or plotleft or both or both
 
1282
 
 
1283
//Define the X axis
 
1284
/*$graph->SetXLabel( "Year");
 
1285
$graph->SetHorizTickIncrement( "5");
 
1286
$graph->SetXGridLabelType( "title");*/
 
1287
 
 
1288
//Define the Y axis
 
1289
$graph->SetVertTickIncrement( "500"); //the gap between two values in the Y axis
 
1290
$graph->SetPrecisionY( "0"); // determine how many decimal points should be there in Y axis values
 
1291
$graph->SetYGridLabelType( "data");
 
1292
$graph->SetLightGridColor( "blue");
 
1293
$graph->SetOutputFile("barChart.png");
 
1294
 
 
1295
$y=$y+2000;
 
1296
//$graph->SetNewPlotAreaPixels(70,120,375,220);
 
1297
$graph->SetPlotAreaWorld(0,0,$i,$y);
 
1298
$graph->DrawGraph();
 
1299
}
 
1300
 
 
1301
 
 
1302
 
 
1303
 
 
1304
 
 
1305
 
 
1306
function _shn_ims_bar_chart($data,$j,$max)
 
1307
{
 
1308
//include ( "../3rd/phplot1/phplot.php");
 
1309
$graph = new PHPlot(700,500);
 
1310
 
 
1311
//$graph->SetDataType( "linear-linear"); //text-data
 
1312
$graph->SetDataType("text-data");  //Must be called before SetDataValues
 
1313
 
 
1314
 
 
1315
$graph->SetDataValues($data);
 
1316
 $graph->SetFileFormat("png"); // parameters = "png" or "jpg" or "gif"
 
1317
 
 
1318
//Specify plotting area details
 
1319
$graph->SetImageArea(700,500);
 
1320
/*commented types are ok with the data*/
 
1321
//$graph->SetPlotType( "lines");
 
1322
//$graph->SetPlotType( "linepoints");
 
1323
//$graph->SetPlotType( "points");
 
1324
//$graph->SetPlotType( "area");
 
1325
//$graph->SetPlotType( "thinbarline");
 
1326
//$graph->SetPlotType( "squared");
 
1327
//$graph->SetPlotType( "pie");
 
1328
$graph->SetPlotType( "bars");
 
1329
//$graph->SetPlotType( "stackedbars");
 
1330
 
 
1331
$graph->SetTitleFontSize( "5"); // the max value of font size is 5
 
1332
$graph->SetTitle( "Forecasting by item");
 
1333
 
 
1334
$graph->SetLegend(array("actual value","forecasted value"));
 
1335
//$graph->SetPlotAreaWorld(2000,0,2050,2500); // parameters = ($starting_val_of_x_axis, $s_val_of_y, $ending_val_of_x, $e_val_of_y)
 
1336
$graph->SetPlotBgColor( "red");
 
1337
$graph->SetPlotBorderType( "left");
 
1338
$graph->SetBackgroundColor( "orange"); // set the chart background
 
1339
 
 
1340
//you can set the x title by one line or as commented three lines
 
1341
$graph->SetXTitle("Time Period", "both"); // para = ($title, $position); $postion= plotup or plotdown or both or none
 
1342
$graph->SetYTitle('Amount',"both"); // plotright or plotleft or both or both
 
1343
 
 
1344
//Define the X axis
 
1345
/*$graph->SetXLabel( "Year");
 
1346
$graph->SetHorizTickIncrement( "5");
 
1347
$graph->SetXGridLabelType( "title");*/
 
1348
 
 
1349
//Define the Y axis
 
1350
$graph->SetVertTickIncrement( "500"); //the gap between two values in the Y axis
 
1351
$graph->SetPrecisionY( "0"); // determine how many decimal points should be there in Y axis values
 
1352
$graph->SetYGridLabelType( "data");
 
1353
$graph->SetLightGridColor( "blue");
 
1354
$graph->SetOutputFile("barChart.png");
 
1355
 
 
1356
$max=$max+2000;
 
1357
 
 
1358
 
 
1359
//$graph->SetNewPlotAreaPixels(70,120,375,220);
 
1360
$graph->SetPlotAreaWorld(0,0,$j,$max);
 
1361
$graph->DrawGraph();
 
1362
}
 
1363
 
 
1364
function _shn_ims_line_chart($data,$x,$y)
 
1365
{
 
1366
        $graph = new PHPlot(700,500);
 
1367
 
 
1368
//$graph->SetDataType( "linear-linear"); //text-data
 
1369
$graph->SetDataType("text-data");  //Must be called before SetDataValues
 
1370
 
 
1371
$graph->SetDataValues($data);
 
1372
 $graph->SetFileFormat("png"); // parameters = "png" or "jpg" or "gif"
 
1373
 
 
1374
//Specify plotting area details
 
1375
$graph->SetImageArea(700,500);
 
1376
/*commented types are ok with the data*/
 
1377
$graph->SetPlotType( "lines");
 
1378
//$graph->SetPlotType( "linepoints");
 
1379
//$graph->SetPlotType( "points");
 
1380
//$graph->SetPlotType( "area");
 
1381
//$graph->SetPlotType( "thinbarline");
 
1382
//$graph->SetPlotType( "squared");
 
1383
//$graph->SetPlotType( "pie");
 
1384
//$graph->SetPlotType( "bars");
 
1385
//$graph->SetPlotType( "stackedbars");
 
1386
 
 
1387
$graph->SetTitleFontSize( "5"); // the max value of font size is 5
 
1388
$graph->SetTitle( "Forecasting by Item");
 
1389
 
 
1390
$graph->SetLegend(array("Re-Order level","forecasted value"));
 
1391
//$graph->SetPlotAreaWorld(2000,0,2050,2500); // parameters = ($starting_val_of_x_axis, $s_val_of_y, $ending_val_of_x, $e_val_of_y)
 
1392
$graph->SetPlotBgColor( "red");
 
1393
$graph->SetPlotBorderType( "left");
 
1394
$graph->SetBackgroundColor( "orange"); // set the chart background
 
1395
 
 
1396
//you can set the x title by one line or as commented three lines
 
1397
$graph->SetXTitle("time", "both"); // para = ($title, $position); $postion= plotup or plotdown or both or none
 
1398
$graph->SetYTitle('amount',"both"); // plotright or plotleft or both or both
 
1399
 
 
1400
//Define the X axis
 
1401
/*$graph->SetXLabel( "Year");
 
1402
$graph->SetHorizTickIncrement( "5");
 
1403
$graph->SetXGridLabelType( "title");*/
 
1404
 
 
1405
//Define the Y axis
 
1406
$graph->SetVertTickIncrement( "500"); //the gap between two values in the Y axis
 
1407
$graph->SetPrecisionY( "0"); // determine how many decimal points should be there in Y axis values
 
1408
$graph->SetYGridLabelType( "data");
 
1409
$graph->SetLightGridColor( "blue");
 
1410
$graph->SetOutputFile("lineChart.png");
 
1411
 
 
1412
$y=$y+2000;
 
1413
 
 
1414
//$graph->SetNewPlotAreaPixels(70,120,375,220);
 
1415
$graph->SetPlotAreaWorld(0,0,$x,$y);
 
1416
$graph->DrawGraph();
 
1417
}
 
1418
 
 
1419
function _shn_ims_pie_chart($data,$catalog_array)
 
1420
{
 
1421
        $graph = new PHPlot(700,500);
 
1422
 
 
1423
//$graph->SetDataType( "linear-linear"); //text-data
 
1424
$graph->SetDataType("text-data");  //Must be called before SetDataValues
 
1425
 
 
1426
$data2 = array($data);
 
1427
 
 
1428
$graph->SetDataValues($data);
 
1429
 $graph->SetFileFormat("png"); // parameters = "png" or "jpg" or "gif"
 
1430
 
 
1431
//Specify plotting area details
 
1432
$graph->SetImageArea(700,500);
 
1433
/*commented types are ok with the data*/
 
1434
//$graph->SetPlotType( "lines");
 
1435
//$graph->SetPlotType( "linepoints");
 
1436
//$graph->SetPlotType( "points");
 
1437
//$graph->SetPlotType( "area");
 
1438
//$graph->SetPlotType( "thinbarline");
 
1439
//$graph->SetPlotType( "squared");
 
1440
$graph->SetPlotType( "pie");
 
1441
//$graph->SetPlotType( "bars");
 
1442
//$graph->SetPlotType( "stackedbars");
 
1443
 
 
1444
$graph->SetTitleFontSize( "5"); // the max value of font size is 5
 
1445
$graph->SetTitle( "Forecasting by Inventory");
 
1446
 
 
1447
//$graph->SetLegend(array("2000","2001","2002","2003", "2004", "2005", "2006", "2007"));
 
1448
$graph->SetLegend($catalog_array);
 
1449
//$graph->SetPlotAreaWorld(2000,0,2050,2500); // parameters = ($starting_val_of_x_axis, $s_val_of_y, $ending_val_of_x, $e_val_of_y)
 
1450
$graph->SetPlotBgColor( "red");
 
1451
$graph->SetPlotBorderType( "left");
 
1452
$graph->SetBackgroundColor( "orange"); // set the chart background
 
1453
 
 
1454
//you can set the x title by one line or as commented three lines
 
1455
//$graph->SetXTitle("SD X title", "both"); // para = ($title, $position); $postion= plotup or plotdown or both or none
 
1456
//$graph->SetYTitle('SD Y title',"both"); // plotright or plotleft or both or both
 
1457
 
 
1458
//Define the X axis
 
1459
/*$graph->SetXLabel( "Year");
 
1460
$graph->SetHorizTickIncrement( "5");
 
1461
$graph->SetXGridLabelType( "title");*/
 
1462
 
 
1463
//Define the Y axis
 
1464
$graph->SetVertTickIncrement( "500"); //the gap between two values in the Y axis
 
1465
$graph->SetPrecisionY( "0"); // determine how many decimal points should be there in Y axis values
 
1466
$graph->SetYGridLabelType( "data");
 
1467
$graph->SetLightGridColor( "blue");
 
1468
$graph->SetOutputFile("pieChart.png");
 
1469
 
 
1470
 
 
1471
//$graph->SetNewPlotAreaPixels(70,120,375,220);
 
1472
$graph->SetPlotAreaWorld(0,0,7,2000);
 
1473
$graph->DrawGraph();
 
1474
}
 
1475
 
 
1476
 
 
1477
 
 
1478
function _shn_ims_chart_test()
 
1479
{
 
1480
$graph = new PHPlot(700,500);
 
1481
 
 
1482
//$graph->SetDataType( "linear-linear"); //text-data
 
1483
$graph->SetDataType("text-data");  //Must be called before SetDataValues
 
1484
 
 
1485
$data = array(
 
1486
    array(  "a", 2000,1500),
 
1487
    array(  "b", 2010,1000),
 
1488
    array(  "c", 2015,500),
 
1489
    array(  "dd",2020,2000),
 
1490
    array(  "e", 2025,100),
 
1491
    //array(  "f", 2030,50),
 
1492
   // array(  "f", 2030,50),
 
1493
    //array(  "f", 2030,50)
 
1494
);
 
1495
 
 
1496
// Specify some data
 
1497
$data123 = array(
 
1498
    array(  "a", 2000,1500, 0, 100, 600,200),
 
1499
    array(  "b", 2010,1000, 100, 150,200,300),
 
1500
    array(  "c", 2015,500, 700, 179, 400,500),
 
1501
    array(  "d",2020,200,1000,979, 1000,600),
 
1502
    array(  "e", 2025,100,1200,78,150,700),
 
1503
    array(  "f", 2030,50, 1450,343, 1300,800)
 
1504
   // array(  "g", 2030,50, 1450,343, 1300,800),
 
1505
    //array(  "h", 2030,50, 1450,343, 1300,800)
 
1506
);
 
1507
$graph->SetDataValues($data);
 
1508
 $graph->SetFileFormat("png"); // parameters = "png" or "jpg" or "gif"
 
1509
 
 
1510
//Specify plotting area details
 
1511
$graph->SetImageArea(700,500);
 
1512
/*commented types are ok with the data*/
 
1513
//$graph->SetPlotType( "lines");
 
1514
//$graph->SetPlotType( "linepoints");
 
1515
//$graph->SetPlotType( "points");
 
1516
//$graph->SetPlotType( "area");
 
1517
//$graph->SetPlotType( "thinbarline");
 
1518
//$graph->SetPlotType( "squared");
 
1519
//$graph->SetPlotType( "pie");
 
1520
$graph->SetPlotType( "bars");
 
1521
//$graph->SetPlotType( "stackedbars");
 
1522
 
 
1523
$graph->SetTitleFontSize( "5"); // the max value of font size is 5
 
1524
$graph->SetTitle( "This is SD test chart");
 
1525
 
 
1526
$graph->SetLegend(array("2000","2001","2002","2003", "2004", "2005", "2006", "2007"));
 
1527
//$graph->SetPlotAreaWorld(2000,0,2050,2500); // parameters = ($starting_val_of_x_axis, $s_val_of_y, $ending_val_of_x, $e_val_of_y)
 
1528
$graph->SetPlotBgColor( "red");
 
1529
$graph->SetPlotBorderType( "left");
 
1530
$graph->SetBackgroundColor( "orange"); // set the chart background
 
1531
 
 
1532
//you can set the x title by one line or as commented three lines
 
1533
$graph->SetXTitle("SD X title", "both"); // para = ($title, $position); $postion= plotup or plotdown or both or none
 
1534
$graph->SetYTitle('SD Y title',"both"); // plotright or plotleft or both or both
 
1535
 
 
1536
//Define the X axis
 
1537
/*$graph->SetXLabel( "Year");
 
1538
$graph->SetHorizTickIncrement( "5");
 
1539
$graph->SetXGridLabelType( "title");*/
 
1540
 
 
1541
//Define the Y axis
 
1542
$graph->SetVertTickIncrement( "500"); //the gap between two values in the Y axis
 
1543
$graph->SetPrecisionY( "0"); // determine how many decimal points should be there in Y axis values
 
1544
$graph->SetYGridLabelType( "data");
 
1545
$graph->SetLightGridColor( "blue");
 
1546
$graph->SetOutputFile("barChart.png");
 
1547
 
 
1548
 
 
1549
//$graph->SetNewPlotAreaPixels(70,120,375,220);
 
1550
$graph->SetPlotAreaWorld(0,0,6,2000);
 
1551
$graph->DrawGraph();
 
1552
}
 
1553
 
 
1554
 
 
1555
function _shn_ims_double_smoothing($catalog_id,$inv_id,$time_period)
 
1556
{
 
1557
        global $global;
 
1558
        $db=$global["db"];
 
1559
 
 
1560
        $sql1="SELECT * FROM ims_optimization WHERE catalog_id='$catalog_id' AND inv_id='$inv_id';";
 
1561
        $ims1=$db->Execute($sql1);
 
1562
        while(!$ims1==NULL && !$ims1->EOF)
 
1563
        {
 
1564
                $week=$ims1->fields['week'];
 
1565
                $forecasted_value=$ims1->fields['forecasted_value'];
 
1566
 
 
1567
                $beta=2/($week+1);
 
1568
 
 
1569
                if($week==1)
 
1570
                {
 
1571
                        $double_forecasted_value=0;
 
1572
                        
 
1573
                }
 
1574
                else
 
1575
                {
 
1576
                        $double_forecasted_value=($beta*($forecasted_value-$temp)) + ((1-$beta)*$double_forecasted_value_temp);
 
1577
                        
 
1578
                }
 
1579
 
 
1580
        $sql2="UPDATE ims_optimization SET double_forecasted_value='{$double_forecasted_value}' WHERE catalog_id='$catalog_id' AND inv_id='$inv_id' AND week='$week';";
 
1581
        $ims2=$db->Execute($sql2);
 
1582
 
 
1583
 
 
1584
 
 
1585
                $double_forecasted_value_temp=$double_forecasted_value;
 
1586
                $temp=$forecasted_value;
 
1587
                $ims1->MoveNext();
 
1588
        }
 
1589
 
 
1590
        //return $double_forecasted_value_temp;
 
1591
}
 
1592
 
 
1593
function _shn_ims_double_smoothing_forecast($catalog_id,$inv_id,$week)
 
1594
{
 
1595
        global $global;
 
1596
        $db=$global["db"];
 
1597
 
 
1598
        $week=$week-1;
 
1599
 
 
1600
        $query1="SELECT * FROM ims_optimization WHERE catalog_id='$catalog_id' AND inv_id='$inv_id' AND week>='$week';";
 
1601
        $ims_query1=$db->Execute($query1);
 
1602
        
 
1603
        while(!$ims_query1==NULL && !$ims_query1->EOF)
 
1604
        {
 
1605
                $forecasted_value=$ims_query1->fields['forecasted_value'];
 
1606
                $double_forecasted_value=$ims_query1->fields['double_forecasted_value'];
 
1607
                $time=$ims_query1->fields['week'];
 
1608
                $beta=2/($time+1);
 
1609
 
 
1610
                if($time==$week)
 
1611
                {
 
1612
                        $forecasted_temp=$forecasted_value;
 
1613
                        $double_forecasted_temp=$double_forecasted_value;
 
1614
                }
 
1615
                else
 
1616
                {
 
1617
                        ;
 
1618
                }
 
1619
 
 
1620
                $ims_query1->MoveNext();
 
1621
        }
 
1622
 
 
1623
        $double_forecast=($beta*($forecasted_value-$forecasted_temp)) + ((1-$beta)*$double_forecasted_temp);
 
1624
        
 
1625
        return $double_forecast;
 
1626
        
 
1627
 
1628
 
 
1629
/**
 
1630
********************************************************************************
 
1631
******************************************** Expired Items Report ************** ********************************************************************************
 
1632
**/
 
1633
 
 
1634
function _shn_ims_expired_item_report()
 
1635
{
 
1636
    global $global;
 
1637
    $db=$global['db'];
 
1638
    $state="expired";
 
1639
 
 
1640
    
 
1641
    $table_data = array();
 
1642
    $row_count = 0;
 
1643
 
 
1644
    $sql="SELECT item_id,inv_id,suplier_id,item_name,amount,unit,manufactured_date,expire_date FROM ims_item_records WHERE state='$state';";
 
1645
    //$ims = $db->Execute($sql1);
 
1646
    $ims = $db->Execute($sql);
 
1647
 
 
1648
     $suplier_array=array();
 
1649
 
 
1650
    while (!$ims==NULL && !$ims->EOF)
 
1651
    {
 
1652
        $table_row = "row".$row_coount++;
 
1653
        $table_row = array();
 
1654
 
 
1655
        $item_id=$ims->fields["item_id"];
 
1656
        $inv_id=$ims->fields["inv_id"];
 
1657
        $suplier_id=$ims->fields["suplier_id"];
 
1658
 
 
1659
        $suplier_array=_shn_or_get_suplier_name($suplier_id);
 
1660
        $suplier_name=$suplier_array[$suplier_id];
 
1661
        
 
1662
        //$category=$ims->fields["category"];
 
1663
 
 
1664
        $table_row ["name"]= $ims->fields["item_name"];
 
1665
        $table_row ["amount"]= $ims->fields["amount"];
 
1666
        $unit=$ims->fields["unit"];
 
1667
        $unit_name=get_unit_name($unit);
 
1668
        $table_row ["unit"]= $unit_name;
 
1669
        $table_row ["manu_date"]= $ims->fields["manufactured_date"];
 
1670
        $table_row ["exp_date"]= $ims->fields["expire_date"];
 
1671
 
 
1672
        $sql3="SELECT inventory_name FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
1673
        $ims3=$db->Execute($sql3);
 
1674
 
 
1675
        $table_row ["inventory"]=  $ims3->fields['inventory_name'];
 
1676
        //$ims3->fields['suplier']=$suplier_name;
 
1677
        //echo $suplier_name;
 
1678
        
 
1679
        $table_row ["suplier"]=$suplier_name;
 
1680
 
 
1681
        array_push($table_data,$table_row);
 
1682
 
 
1683
        $ims->MoveNext();
 
1684
 
 
1685
    }
 
1686
 
 
1687
    return $table_data;
 
1688
}
 
1689
 
 
1690
/**
 
1691
*************************************** Destryed Item Report ********************************************************************************
 
1692
**/
 
1693
 
 
1694
function _shn_ims_destroyed_item_report()
 
1695
{
 
1696
    global $global;
 
1697
    $db=$global['db'];
 
1698
    $state="destroyed";
 
1699
 
 
1700
    
 
1701
    $table_data = array();
 
1702
    $row_count = 0;
 
1703
 
 
1704
    $sql1="SELECT inv_id,suplier_id,item_name,amount,unit,manufactured_date,expire_date FROM ims_item_records WHERE state='$state';";
 
1705
    $ims1=$db->Execute($sql1);
 
1706
 
 
1707
    $suplier_array=array();
 
1708
 
 
1709
    while (!$ims1==NULL && !$ims1->EOF)
 
1710
    {
 
1711
        $table_row = "row".$row_coount++;
 
1712
        $table_row = array();
 
1713
 
 
1714
        $inv_id=$ims1->fields['inv_id'];
 
1715
        $suplier_id=$ims1->fields['suplier_id'];
 
1716
        $suplier_array=_shn_or_get_suplier_name($suplier_id);
 
1717
        $suplier_name=$suplier_array[$suplier_id];
 
1718
        $item_name=$ims1->fields['item_name'];
 
1719
        $amount=$ims1->fields['amount'];
 
1720
        $unit=$ims1->fields['unit'];
 
1721
        $unit_name=get_unit_name($unit);
 
1722
        $manufactured_date=$ims1->fields['manufactured_date'];
 
1723
        $expire_date=$ims1->fields['expire_date'];
 
1724
 
 
1725
        $sql2="SELECT inventory_name FROM ims_inventory_records WHERE inv_uuid='$inv_id';";
 
1726
        $ims2=$db->Execute($sql2);
 
1727
            
 
1728
        $inventory_name=$ims2->fields['inventory_name'];
 
1729
 
 
1730
        $table_row['name']=$item_name;
 
1731
        $table_row['amount']=$amount;
 
1732
        $table_row['unit']=$unit_name;
 
1733
        $table_row['manu_date']=$manufactured_date;
 
1734
        $table_row['exp_date']=$expire_date;
 
1735
        $table_row['inventory']=$inventory_name;
 
1736
        $table_row['suplier']=$suplier_name;
 
1737
 
 
1738
        array_push($table_data,$table_row);
 
1739
 
 
1740
        $ims1->MoveNext();
 
1741
    }
 
1742
 
 
1743
    return $table_data;
 
1744
}
 
1745
 
 
1746
/**
 
1747
************************************************ Re-Order Level Report ********************************************************************
 
1748
**/
 
1749
 
 
1750
function _shn_ims_reorder_level_report()
 
1751
{
 
1752
    global $global;
 
1753
    $db=$global["db"];
 
1754
    
 
1755
    $table_data = array();
 
1756
    $row_count = 0;
 
1757
        
 
1758
    $sql1="SELECT catalog_id,inv_id,minimum_quantity,unit FROM ims_reorder_level;";
 
1759
    $ims1=$db->Execute($sql1);
 
1760
    
 
1761
    $state = "destroyed";
 
1762
        
 
1763
    $sum_amount=0;
 
1764
        
 
1765
        
 
1766
     while (!$ims1==NULL && !$ims1->EOF)
 
1767
        {
 
1768
            $table_row = "row".$row_coount++;
 
1769
            $table_row = array();
 
1770
 
 
1771
            $catalog_id_min=$ims1->fields['catalog_id'];
 
1772
            
 
1773
            $inv_id_min=$ims1->fields['inv_id'];
 
1774
           
 
1775
            $minimum_quantity=$ims1->fields['minimum_quantity'];
 
1776
            
 
1777
            
 
1778
            $unit=$ims1->fields['unit'];
 
1779
            
 
1780
            $unit_name=get_unit_name($unit);
 
1781
 
 
1782
            $sql2 = "SELECT unit,amount FROM ims_item_records WHERE catalog_id='$catalog_id_min' AND inv_id='$inv_id_min'AND state!='$state';";
 
1783
            $ims2 =$db->Execute($sql2);
 
1784
            
 
1785
            while(!$ims2==NULL && !$ims2->EOF)
 
1786
            {
 
1787
          
 
1788
            $current_unit=$ims2->fields['unit'];
 
1789
            $current_amount=$ims2->fields['amount'];
 
1790
                        
 
1791
            
 
1792
    $multiplier=unit_converter($unit,$current_unit);
 
1793
    
 
1794
    
 
1795
    $current_amount_convert=$current_amount*$multiplier;
 
1796
    
 
1797
    $sum_amount=$sum_amount+$current_amount_convert;
 
1798
    
 
1799
    
 
1800
    
 
1801
            $ims2->MoveNext();
 
1802
            }
 
1803
    
 
1804
 
 
1805
        if($sum_amount<$minimum_quantity)
 
1806
            {
 
1807
                $total_amount=$sum_amount;
 
1808
                $sum_amount=0;
 
1809
                $sql3 = "SELECT item_id,item_name,unit FROM ims_item_records WHERE catalog_id='$catalog_id_min' AND inv_id='$inv_id_min';";
 
1810
                $ims3 = $db->Execute($sql3);
 
1811
    
 
1812
            
 
1813
             
 
1814
 
 
1815
                $sql4 = "SELECT inventory_name FROM ims_inventory_records WHERE inv_uuid='$inv_id_min';";
 
1816
                $ims4 = $db->Execute($sql4);
 
1817
                $inventory_name=$ims4->fields['inventory_name'];
 
1818
               
 
1819
 
 
1820
                $item_id=$ims3->fields["item_id"];
 
1821
 
 
1822
            if($item_id!=null)
 
1823
            {
 
1824
                   
 
1825
               
 
1826
                $item_name=$ims3->fields["item_name"];
 
1827
               
 
1828
                $unit=$ims3->fields["unit"];
 
1829
                
 
1830
 
 
1831
                $table_row['name']=$item_name;
 
1832
                $table_row['amount']=$total_amount;
 
1833
                $table_row['unit']=$unit_name;
 
1834
                $table_row['inventory']=$inventory_name;
 
1835
                
 
1836
            }
 
1837
    }
 
1838
    else if($sum_amount>=$minimum_quantity)
 
1839
    {
 
1840
        $sum_amount=0;
 
1841
    }
 
1842
        array_push($table_data,$table_row);
 
1843
        $ims1->MoveNext();
 
1844
    }
 
1845
 
 
1846
    return $table_data; 
 
1847
}
 
1848
 
 
1849
function _shn_ims_validate_sub_cat_form()
 
1850
{
 
1851
$error_flag=false;
 
1852
clean_errors();
 
1853
 
 
1854
$itemid=null;
 
1855
$max_depth = get_max_depth();
 
1856
    for($i=0;$i<$max_depth;$i++)
 
1857
    {
 
1858
    $itemid = trim($_POST[$i]);
 
1859
        if($itemid!=null)
 
1860
        {
 
1861
        break;
 
1862
        }
 
1863
    }
 
1864
 
 
1865
    if($itemid==null || $itemid=='none')
 
1866
    {
 
1867
   //add_error(_t(SHN_IMS_CATALOGUE_MISSING));
 
1868
    $error_flag=true;
 
1869
    }    
 
1870
 
 
1871
return $error_flag;
 
1872
}
 
1873
 
 
1874
function _shn_ims_location_string($loc)
 
1875
{
 
1876
        global $global;
 
1877
        $db=$global["db"];
 
1878
        $loc_arr=array();
 
1879
        shn_get_parents($loc,&$loc_arr);
 
1880
        if($loc_arr[0]=='unknown'){
 
1881
                shn_form_label(_t("Organization Location"),"Unknown");
 
1882
        }else{
 
1883
                
 
1884
                $max=count($loc_arr)-1;
 
1885
                array_pop($loc_arr);
 
1886
                for($count=0;$count<$max;$count++){
 
1887
                        $x=array_pop($loc_arr);
 
1888
                        $q="SELECT name FROM location WHERE loc_uuid='{$x}'";
 
1889
                        $res=$db->Execute($q);
 
1890
                        if($count==0 ){
 
1891
                                $location=$location.$res->Fields("name");
 
1892
                        }else{
 
1893
                                $location=$location." -->".$res->Fields("name");
 
1894
                        }
 
1895
                        
 
1896
                }
 
1897
        }
 
1898
        return $location;
 
1899
}
 
1900
 
 
1901
function _shn_ims_get_locid($range_end){
 
1902
//      if($_SESSION['ims_5']!=null)
 
1903
//              return $_SESSION['ims_5'];
 
1904
//      else if($_SESSION['ims_4']!=null)
 
1905
//              return $_SESSION['ims_4'];
 
1906
//      else if($_SESSION['ims_3']!=null)
 
1907
//              return $_SESSION['ims_3'];
 
1908
//      else if($_SESSION['ims_2']!=null)
 
1909
//              return $_SESSION['ims_2'];
 
1910
//      else 
 
1911
//              return $_SESSION['ims_1'];
 
1912
        
 
1913
        for($range_end; $range_end>0; $range_end--) {
 
1914
                $var='rms_'.$range_end;
 
1915
            if($_SESSION[$var]!=null && $_SESSION[$var]!="0")
 
1916
                    return $_SESSION[$var];
 
1917
        }
 
1918
}
 
1919
 
 
1920
 
 
1921
function _shn_ims_get_parent_locations($location_id)
 
1922
{
 
1923
    global $global;
 
1924
    $db=$global['db'];
 
1925
 
 
1926
    $flag=false;
 
1927
    $i=1;
 
1928
 
 
1929
    $locatiion_array=array();
 
1930
    $location_array[0]=$location_id;
 
1931
 
 
1932
    $sql="SELECT * FROM location;";
 
1933
    $ims=$db->Execute($sql);
 
1934
 
 
1935
    while(!$ims==NULL && !$ims->EOF)
 
1936
    {
 
1937
        $parent_id=$ims->fields['parent_id'];
 
1938
        $loc_uuid=$ims->fields['loc_uuid'];
 
1939
 
 
1940
 
 
1941
        if($parent_id==$location_id)
 
1942
        {
 
1943
            $flag=true;
 
1944
 
 
1945
        }
 
1946
 
 
1947
        if($flag==true)
 
1948
        {
 
1949
            $location_array[$i]=$loc_uuid;
 
1950
            $i=$i+1;
 
1951
 
 
1952
        }
 
1953
 
 
1954
        $ims->MoveNext();
 
1955
    }
 
1956
 
 
1957
    return $location_array;
 
1958
}
 
1959
 
 
1960
function _shn_ims_get_shelter_name()
 
1961
{
 
1962
    global $global;
 
1963
    $db=$global['db'];
 
1964
    $shelter_array=array();
 
1965
    $sql="SELECT * FROM camp_general;";
 
1966
    $ims=$db->Execute($sql);
 
1967
    $shelter_array['']='';
 
1968
    while(!$ims==NULL && !$ims->EOF)
 
1969
    {
 
1970
        $shelter_array[$ims->fields['c_uuid']]=$ims->fields['name'];
 
1971
        $ims->MoveNext();
 
1972
    }
 
1973
    return $shelter_array;
 
1974
}
 
1975
 
 
1976
function _shn_ims_get_organization_name()
 
1977
{
 
1978
    global $global;
 
1979
    $db=$global['db'];
 
1980
    $org_array=array();
 
1981
    $sql="SELECT * FROM org_main;";
 
1982
    $ims=$db->Execute($sql);
 
1983
    $org_array['']='';
 
1984
    while(!$ims==NULL && !$ims->EOF)
 
1985
    {
 
1986
        $org_array[$ims->fields['o_uuid']]=$ims->fields['name'];
 
1987
        $ims->MoveNext();
 
1988
    }
 
1989
    return $org_array;
 
1990
}
 
1991
 
 
1992
function _shn_ims_get_victim()
 
1993
{
 
1994
    global $global;
 
1995
    $db=$global['db'];
 
1996
    $victim_array=array();
 
1997
    $sql="SELECT pg.g_uuid as g_uuid,pu.p_uuid as p_uuid,pg.opt_group_type as group_name,pu.full_name as full_name,pu.family_name as family_name,pu.l10n_name as l10n_name,pu.custom_name as custom_name FROM pgroup pg,person_to_pgroup ptg,person_uuid pu WHERE pg.g_uuid=ptg.g_uuid and ptg.p_uuid=pu.p_uuid;";
 
1998
    $ims=$db->Execute($sql);
 
1999
    
 
2000
    $victim_array['']='';
 
2001
    while(!$ims==NULL && !$ims->EOF)
 
2002
    {
 
2003
        
 
2004
        $victim_array[$ims->fields['g_uuid']]=$ims->fields['full_name']." ".$ims->fields['family_name'];
 
2005
        $ims->MoveNext();
 
2006
    }
 
2007
    return $victim_array;
 
2008
}
 
2009
 
 
2010
?>