Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / consumable.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (15,4 ko)

1
<?php
2
/*
3
 * @version $Id: consumable.function.php 7763 2009-01-06 18:44:50Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
// ----------------------------------------------------------------------
32
// Original Author of file: Julien Dombre
33
// Purpose of file:
34
// ----------------------------------------------------------------------
35

    
36
if (!defined('GLPI_ROOT')){
37
        die("Sorry. You can't access directly to this file");
38
        }
39

    
40

    
41
/**
42
 * Print out a link to add directly a new consumable from a consumable type.
43
 *
44
 * Print out the link witch make a new consumable from consumable type idetified by $ID
45
 *
46
 *@param $ID Consumable type identifier.
47
 *
48
 *
49
 *@return Nothing (displays)
50
 **/
51
function showConsumableAdd($ID) {
52

    
53
        global $CFG_GLPI,$LANG;
54

    
55
        if (!haveRight("consumable","w")) return false;
56

    
57
        echo "<form method='post'  action=\"".$CFG_GLPI["root_doc"]."/front/consumable.edit.php\">";
58
        echo "<div class='center'>&nbsp;<table class='tab_cadre_fixe'>";
59
        echo "<tr>";
60
        echo "<td align='center' class='tab_bg_2'>";
61
        echo "<input type='submit' name='add_several' value=\"".$LANG["buttons"][8]."\" class='submit'>";
62
        echo "<input type='hidden' name='tID' value=\"$ID\">\n";
63

    
64
        echo "&nbsp;&nbsp;";
65
        dropdownInteger('to_add',1,1,100);
66
        echo "&nbsp;&nbsp;";
67
        echo $LANG["consumables"][16];
68
        echo "</td></tr>";
69
        echo "</table></div>";
70
        echo "</form><br>";
71
}
72
/**
73
 * Print out the consumables of a defined type
74
 *
75
 * Print out all the consumables that are issued from the consumable type identified by $ID
76
 *
77
 *@param $tID integer : Consumable type identifier.
78
 *@param $show_old boolean : show old consumables or not. 
79
 *
80
 *@return Nothing (displays)
81
 **/
82
function showConsumables ($tID,$show_old=0) {
83

    
84
        global $DB,$CFG_GLPI,$LANG;
85

    
86
        if (!haveRight("consumable","r")) return false;
87
        $canedit=haveRight("consumable","w");
88

    
89
        $cartype=new ConsumableType();
90
        
91
        
92
        if ($cartype->getFromDB($tID)){
93

    
94
                $query = "SELECT count(*) AS COUNT  FROM glpi_consumables WHERE (FK_glpi_consumables_type = '$tID')";
95
        
96
                if ($result = $DB->query($query)) {
97
                        if ($DB->result($result,0,0)!=0) { 
98
                                if (!$show_old&&$canedit){
99
                                        echo "<form method='post' action='".$CFG_GLPI["root_doc"]."/front/consumable.edit.php'>";
100
                                        echo "<input type='hidden' name='tID' value=\"$tID\">\n";
101
                                }
102
                                echo "<br><div class='center'><table cellpadding='2' class='tab_cadre_fixe'>";
103
                                if (!$show_old){
104
                                        echo "<tr><th colspan='7'>";
105
                                        echo countConsumables($tID,-1);
106
                                        echo "</th></tr>";
107
                                }
108
                                else { // Old
109
                                        echo "<tr><th colspan='8'>";
110
                                        echo $LANG["consumables"][35];
111
                                        echo "</th></tr>";
112
        
113
                                }
114
                                $i=0;
115
                                echo "<tr><th>".$LANG["common"][2]."</th><th>".$LANG["consumables"][23]."</th><th>".$LANG["cartridges"][24]."</th><th>".$LANG["consumables"][26]."</th>";
116
        
117
        
118
                                if ($show_old){
119
                                        echo "<th>".$LANG["common"][34]."</th>";
120
                                }
121
        
122
                                echo "<th>".$LANG["financial"][3]."</th>";
123
        
124
                                if (!$show_old&&$canedit){
125
                                        echo "<th>";
126
                                        dropdownAllUsers("id_user",0,1,$cartype->fields["FK_entities"]);
127
                                        echo "&nbsp;<input type='submit' class='submit' name='give' value='".$LANG["consumables"][32]."'>";
128
                                        echo "</th>";
129
                                } else {echo "<th>&nbsp;</th>";}
130

    
131
                                if ($canedit){
132
                                        echo "<th>&nbsp;</th>";
133
                                }
134
                                echo "</tr>";
135
                        } else {
136
        
137
                                echo "<br>";
138
                                echo "<div class='center'><strong>".$LANG["consumables"][7]."</strong></div>";
139
                                return;
140
                        }
141
                }
142
        
143
                $where="";
144
                $leftjoin="";
145
                $addselect="";
146
                if (!$show_old){ // NEW
147
                        $where= " AND date_out IS NULL ORDER BY date_in, ID";
148
                } else { //OLD
149
                        $where= " AND date_out IS NOT NULL ORDER BY date_out DESC, date_in, ID";
150
                        $leftjoin=" LEFT JOIN glpi_users ON (glpi_users.ID = glpi_consumables.id_user) ";
151
                        $addselect= ", glpi_users.realname AS REALNAME, glpi_users.firstname AS FIRSTNAME, glpi_users.name AS USERNAME ";
152
                }
153
        
154
                $query = "SELECT glpi_consumables.* $addselect FROM glpi_consumables $leftjoin WHERE (FK_glpi_consumables_type = '$tID') $where";
155
        
156
                if ($result = $DB->query($query)) {                        
157
                        $number=$DB->numrows($result);
158
                        while ($data=$DB->fetch_array($result)) {
159
                                $date_in=convDate($data["date_in"]);
160
                                $date_out=convDate($data["date_out"]);
161
        
162
                                echo "<tr  class='tab_bg_1'><td class='center'>";
163
                                echo $data["ID"]; 
164
                                echo "</td><td class='center'>";
165
                                echo getConsumableStatus($data["ID"]);
166
                                echo "</td><td class='center'>";
167
                                echo $date_in;
168
                                echo "</td><td class='center'>";
169
                                echo $date_out;                
170
                                echo "</td>";
171
        
172
                                if ($show_old){
173
                                        echo "<td class='center'>";
174
                                        if (!empty($data["REALNAME"])) {
175
                                                echo $data["REALNAME"];
176
                                                if (!empty($data["FIRSTNAME"]))
177
                                                        echo " ".$data["FIRSTNAME"];
178
                                        }
179
                                        else echo $data["USERNAME"];
180
                                        echo "</td>";
181
                                }
182
        
183
                                echo "<td class='center'>";
184
                                showDisplayInfocomLink(CONSUMABLE_ITEM_TYPE,$data["ID"],1);
185
                                echo "</td>";
186
        
187
        
188
                                if (!$show_old&&$canedit){
189
                                        echo "<td class='center'>";
190
                                        echo "<input type='checkbox' name='out[".$data["ID"]."]'>";
191
                                        echo "</td>";
192
                                }
193
        
194
                                if ($show_old&&$canedit){
195
                                        echo "<td class='center'>";
196
                                        echo "<a href='".$CFG_GLPI["root_doc"]."/front/consumable.edit.php?restore=restore&amp;ID=".$data["ID"]."&amp;tID=$tID'>".$LANG["consumables"][37]."</a>";
197
                                        echo "</td>";
198
                                }                                                
199
        
200
                                echo "<td class='center'>";
201
        
202
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/consumable.edit.php?delete=delete&amp;ID=".$data["ID"]."&amp;tID=$tID'>".$LANG["buttons"][6]."</a>";
203
                                echo "</td></tr>";
204
        
205
                        }        
206
                }        
207
                echo "</table></div>\n\n";
208
                if (!$show_old&&$canedit){
209
                        echo "</form>";
210
                }
211
        }
212
}
213

    
214

    
215

    
216
/**
217
 * Print the consumable count HTML array for a defined consumable type
218
 *
219
 * Print the consumable count HTML array for the consumable type $tID
220
 *
221
 *@param $tID integer: consumable type identifier.
222
 *@param $alarm integer: threshold alarm value.
223
 *@param $nohtml integer: Return value without HTML tags.
224
 *
225
 *@return string to display
226
 *
227
 **/
228
function countConsumables($tID,$alarm,$nohtml=0) {
229

    
230
        global $DB,$CFG_GLPI, $LANG;
231

    
232

    
233
        $out="";
234
        // Get total
235
        $total = getConsumablesNumber($tID);
236

    
237
        if ($total!=0) {
238
                $unused=getUnusedConsumablesNumber($tID);
239
                $old=getOldConsumablesNumber($tID);
240

    
241
                $highlight="";
242
                if ($unused<=$alarm)
243
                        $highlight="class='tab_bg_1_2'";
244
                if (!$nohtml)
245
                        $out.= "<div $highlight>".$LANG["common"][33].":&nbsp;$total&nbsp;&nbsp;&nbsp;<strong>".$LANG["consumables"][13].": $unused</strong>&nbsp;&nbsp;&nbsp;".$LANG["consumables"][15].": $old</div>";                        
246
                else $out.= $LANG["common"][33].": $total   ".$LANG["consumables"][13].": $unused   ".$LANG["consumables"][15].": $old";                        
247

    
248
        } else {
249
                if (!$nohtml)
250
                        $out.= "<div class='tab_bg_1_2'><i>".$LANG["consumables"][9]."</i></div>";
251
                else $out.= $LANG["consumables"][9];
252
        }
253
        return $out;
254
}        
255

    
256
/**
257
 * count how many consumable for a consumable type
258
 *
259
 * count how many consumable for the consumable type $tID
260
 *
261
 *@param $tID integer: consumable type identifier.
262
 *
263
 *@return integer : number of consumable counted.
264
 *
265
 **/
266
function getConsumablesNumber($tID){
267
        global $DB;
268
        $query = "SELECT ID FROM glpi_consumables WHERE ( FK_glpi_consumables_type = '$tID')";
269
        $result = $DB->query($query);
270
        return $DB->numrows($result);
271
}
272

    
273
/**
274
 * count how many old consumable for a consumable type
275
 *
276
 * count how many old consumable for the consumable type $tID
277
 *
278
 *@param $tID integer: consumable type identifier.
279
 *
280
 *@return integer : number of old consumable counted.
281
 *
282
 **/
283
function getOldConsumablesNumber($tID){
284
        global $DB;
285
        $query = "SELECT ID FROM glpi_consumables WHERE ( FK_glpi_consumables_type = '$tID'  AND date_out IS NOT NULL)";
286
        $result = $DB->query($query);
287
        return $DB->numrows($result);
288
}
289
/**
290
 * count how many consumable unused for a consumable type
291
 *
292
 * count how many consumable unused for the consumable type $tID
293
 *
294
 *@param $tID integer: consumable type identifier.
295
 *
296
 *@return integer : number of consumable unused counted.
297
 *
298
 **/
299
function getUnusedConsumablesNumber($tID){
300
        global $DB;
301
        $query = "SELECT ID FROM glpi_consumables WHERE ( FK_glpi_consumables_type = '$tID'  AND date_out IS NULL)";
302
        $result = $DB->query($query);
303
        return $DB->numrows($result);
304
}
305

    
306

    
307
/**
308
 * To be commented
309
 *
310
 * 
311
 *
312
 *@param $cID integer : consumable type.
313
 *
314
 *@return 
315
 *
316
 **/
317
function isNewConsumable($cID){
318
        global $DB;
319
        $query = "SELECT ID FROM glpi_consumables WHERE ( ID= '$cID' AND date_out IS NULL)";
320
        $result = $DB->query($query);
321
        return ($DB->numrows($result)==1);
322
}
323

    
324
/**
325
 * To be commented
326
 *
327
 * 
328
 *
329
 *@param $cID integer : consumable type.
330
 *
331
 *@return 
332
 *
333
 **/
334
function isOldConsumable($cID){
335
        global $DB;
336
        $query = "SELECT ID FROM glpi_consumables WHERE ( ID= '$cID' AND date_out IS NOT NULL)";
337
        $result = $DB->query($query);
338
        return ($DB->numrows($result)==1);
339
}
340

    
341
/**
342
 * Get the dict value for the status of a consumable
343
 *
344
 * 
345
 *
346
 *@param $cID integer : consumable ID.
347
 *
348
 *@return string : dict value for the consumable status.
349
 *
350
 **/
351
function getConsumableStatus($cID){
352
        global $LANG;
353
        if (isNewConsumable($cID)) return $LANG["consumables"][20];
354
        else if (isOldConsumable($cID)) return $LANG["consumables"][22];
355
}
356

    
357
/**
358
 * Show the usage summary of consumables by user
359
 *
360
 **/
361
function showConsumableSummary(){
362
        global $DB,$LANG;
363

    
364
        if (!haveRight("consumable","r")) return false;
365

    
366
        $query = "SELECT COUNT(*) AS COUNT, FK_glpi_consumables_type, id_user FROM glpi_consumables WHERE date_out IS NOT NULL AND FK_glpi_consumables_type IN (SELECT ID FROM glpi_consumables_type ".getEntitiesRestrictRequest("WHERE","glpi_consumables_type").") GROUP BY id_user,FK_glpi_consumables_type";
367
        $used=array();
368

    
369
        if ($result=$DB->query($query)){
370
                if ($DB->numrows($result))
371
                        while ($data=$DB->fetch_array($result))
372
                                $used[$data["id_user"]][$data["FK_glpi_consumables_type"]]=$data["COUNT"];
373
        }
374

    
375
        $query = "SELECT COUNT(*) AS COUNT, FK_glpi_consumables_type FROM glpi_consumables WHERE date_out IS NULL AND FK_glpi_consumables_type IN (SELECT ID FROM glpi_consumables_type ".getEntitiesRestrictRequest("WHERE","glpi_consumables_type").") GROUP BY FK_glpi_consumables_type";
376
        $new=array();
377

    
378
        if ($result=$DB->query($query)){
379
                if ($DB->numrows($result))
380
                        while ($data=$DB->fetch_array($result))
381
                                $new[$data["FK_glpi_consumables_type"]]=$data["COUNT"];
382
        }
383

    
384
        $types=array();
385
        $query="SELECT * FROM glpi_consumables_type ".getEntitiesRestrictRequest("WHERE","glpi_consumables_type");
386
        if ($result=$DB->query($query)){
387
                if ($DB->numrows($result)){
388
                        while ($data=$DB->fetch_array($result)){
389
                                $types[$data["ID"]]=$data["name"];
390
                        }
391
                }
392
        }
393
        asort($types);
394
        $total=array();
395
        if (count($types)>0){
396

    
397
                // Produce headline
398
                echo "<div class='center'><table  class='tab_cadrehov'><tr>";
399

    
400
                // Type                        
401
                echo "<th>";;
402
                echo $LANG["common"][34]."</th>";
403

    
404
                foreach ($types as $key => $type){
405
                        echo "<th>$type</th>";
406
                        $total[$key]=0;
407
                }
408
                echo "<th>".$LANG["common"][33]."</th>";
409
                echo "</tr>";
410

    
411
                // new
412
                echo "<tr class='tab_bg_2'><td><strong>".$LANG["consumables"][1]."</strong></td>";
413
                $tot=0;
414
                foreach ($types as $id_type => $type){
415
                        if (!isset($new[$id_type])) $new[$id_type]=0;
416
                        echo "<td class='center'>".$new[$id_type]."</td>";
417
                        $total[$id_type]+=$new[$id_type];
418
                        $tot+=$new[$id_type];
419
                }
420
                echo "<td class='center'>".$tot."</td>";
421
                echo "</tr>";
422

    
423
                foreach ($used as $id_user => $val){
424
                        echo "<tr class='tab_bg_2'><td>".getUserName($id_user)."</td>";
425
                        $tot=0;
426
                        foreach ($types as $id_type => $type){
427
                                if (!isset($val[$id_type])) $val[$id_type]=0;
428
                                echo "<td class='center'>".$val[$id_type]."</td>";
429
                                $total[$id_type]+=$val[$id_type];
430
                                $tot+=$val[$id_type];
431
                        }
432
                        echo "<td class='center'>".$tot."</td>";
433
                        echo "</tr>";
434
                }
435
                echo "<tr class='tab_bg_1'><td><strong>".$LANG["common"][33]."</strong></td>";
436
                $tot=0;
437
                foreach ($types as $id_type => $type){
438
                        $tot+=$total[$id_type];
439
                        echo "<td class='center'>".$total[$id_type]."</td>";
440
                }
441
                echo "<td class='center'>".$tot."</td>";
442
                echo "</tr>";
443
                echo "</table></div>";
444

    
445
        } else {
446
                echo "<div class='center'><strong>".$LANG["consumables"][7]."</strong></div>";
447
        }
448

    
449
}
450

    
451
/**
452
 * Cron action on consumables : alert if a stock is behind the threshold
453
 * @param $display display informations instead or log in file ?
454
 * @return 0 : nothing to do 1 : done with success
455
 **/
456
function cron_consumable($display=false){
457
        global $DB,$CFG_GLPI,$LANG;
458

    
459
        if (!$CFG_GLPI["mailing"]||!$CFG_GLPI["consumables_alert"]){
460
                return false;
461
        }
462

    
463
        loadLanguage($CFG_GLPI["default_language"]);
464

    
465
        // Get cartridges type with alarm activated and last warning > config
466
        $query="SELECT glpi_consumables_type.ID AS consID, glpi_consumables_type.FK_entities as entity, glpi_consumables_type.ref as consref, glpi_consumables_type.name AS consname, glpi_consumables_type.alarm AS threshold, glpi_alerts.ID AS alertID, glpi_alerts.date FROM glpi_consumables_type LEFT JOIN glpi_alerts ON (glpi_consumables_type.ID = glpi_alerts.FK_device AND glpi_alerts.device_type='".CONSUMABLE_TYPE."') WHERE glpi_consumables_type.deleted='0' AND glpi_consumables_type.alarm>='0' AND (glpi_alerts.date IS NULL OR (glpi_alerts.date+".$CFG_GLPI["consumables_alert"].") < CURRENT_TIMESTAMP());";
467

    
468
        $result=$DB->query($query);
469
        $message=array();
470
        $items=array();
471
        $alert=new Alert();
472

    
473
        if ($DB->numrows($result)>0){
474
                while ($data=$DB->fetch_array($result)){
475
                        if (($unused=getUnusedConsumablesNumber($data["consID"]))<=$data["threshold"]){
476
                                if (!isset($message[$data["entity"]])){
477
                                        $message[$data["entity"]]="";
478
                                }
479
                                if (!isset($items[$data["entity"]])){
480
                                        $items[$data["entity"]]=array();
481
                                }
482

    
483
                                // define message alert
484
                                $message[$data["entity"]].=$LANG["mailing"][35]." ".$data["consname"]." - ".$LANG["consumables"][2].": ".$data["consref"]." - ".$LANG["software"][20].": ".$unused."<br>\n";
485
                                $items[$data["entity"]][]=$data["consID"];
486

    
487
                                //// if alert exists -> delete 
488
                                if (!empty($data["alertID"])){
489
                                        $alert->delete(array("ID"=>$data["alertID"]));
490
                                }
491

    
492
                        }
493
                }
494

    
495
                if (count($message)>0){
496
                        foreach ($message as $entity => $msg){
497
                                $mail=new MailingAlert("alertconsumable",$msg,$entity);
498

    
499
                                if ($mail->send()){
500
                                        if ($display){
501
                                                addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  $msg");
502
                                        } 
503
                                        logInFile("cron",getDropdownName("glpi_entities",$entity).":  $msg\n");
504

    
505
                                        $input["type"]=ALERT_THRESHOLD;
506
                                        $input["device_type"]=CONSUMABLE_TYPE;
507

    
508
                                        //// add alerts
509
                                        foreach ($items[$entity] as $ID){
510
                                                $input["FK_device"]=$ID;
511
                                                $alert->add($input);
512
                                                unset($alert->fields['ID']);
513
                                        }
514

    
515
                                } else {
516
                                        if ($display){
517
                                                addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  Send consumable alert failed");
518
                                        }
519
                                        logInFile("cron",getDropdownName("glpi_entities",$entity).":  Send consumable alert failed\n");
520
                                }
521

    
522
                        }
523
                        return 1;
524
                }
525

    
526
        }
527
        return 0;
528
}
529
?>
Redmine Appliance - Powered by TurnKey Linux