Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (23,5 ko)

1
<?php
2
/*
3
 * @version $Id: cartridge.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
 * Print out a link to add directly a new cartridge from a cartridge type.
42
 *
43
 * Print out the link witch make a new cartridge from cartridge type idetified by $ID
44
 *
45
 *@param $ID Cartridge type identifier.
46
 *
47
 *
48
 *@return Nothing (displays)
49
 **/
50
function showCartridgesAdd($ID) {
51

    
52
        global $CFG_GLPI,$LANG;
53

    
54
        if (!haveRight("cartridge","w")) return false;
55

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

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

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

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

    
88
        $query = "SELECT count(*) AS COUNT  FROM glpi_cartridges WHERE (FK_glpi_cartridges_type = '$tID')";
89

    
90
        if ($result = $DB->query($query)) {
91
                if ($DB->result($result,0,0)!=0) { 
92
                        $total=$DB->result($result, 0, "COUNT");
93
                        $unused=getUnusedCartridgesNumber($tID);
94
                        $used=getUsedCartridgesNumber($tID);
95
                        $old=getOldCartridgesNumber($tID);
96

    
97
                        echo "<br><div class='center'><table cellpadding='2' class='tab_cadre_fixe'>";
98
                        if (!$show_old){
99
                                echo "<tr><th colspan='7'>";
100
                                echo countCartridges($tID,-1);
101
                                echo "</th>";
102
                                echo "<th colspan='2'>";
103
                                echo "&nbsp;</th></tr>";
104
                        } else { // Old
105
                                echo "<tr><th colspan='8'>";
106
                                echo $LANG["cartridges"][35];
107
                                echo "</th>";
108
                                echo "<th colspan='2'>";
109
                                echo "&nbsp;</th></tr>";
110
                        }
111
                        $i=0;
112
                        echo "<tr><th>".$LANG["common"][2]."</th><th>".$LANG["consumables"][23]."</th><th>".$LANG["cartridges"][24]."</th><th>".$LANG["consumables"][26]."</th><th>".$LANG["cartridges"][27]."</th><th>".$LANG["search"][9]."</th>";
113

    
114
                        if ($show_old){
115
                                echo "<th>".$LANG["cartridges"][39]."</th>";
116
                        }
117

    
118
                        echo "<th>".$LANG["financial"][3]."</th>";
119
                        echo "<th colspan='2'>&nbsp;</th>";
120

    
121
                        echo "</tr>";
122
                } else {
123
                        echo "<br><div class='center'><table border='0' width='50%' cellpadding='2'>";
124
                        echo "<tr><th>".$LANG["cartridges"][7]."</th></tr>";
125
                        echo "</table></div>";
126
                }
127
        }
128

    
129
        if (!$show_old){ // NEW
130
                $where= " AND glpi_cartridges.date_out IS NULL";
131
        } else { //OLD
132
                $where= " AND glpi_cartridges.date_out IS NOT NULL";
133
        }
134

    
135
        $stock_time=0;
136
        $use_time=0;        
137
        $pages_printed=0;
138
        $nb_pages_printed=0;
139
        $ORDER="glpi_cartridges.date_use ASC, glpi_cartridges.date_out DESC,  glpi_cartridges.date_in";
140
        if (!$show_old){
141
                $ORDER=" glpi_cartridges.date_out ASC, glpi_cartridges.date_use ASC,  glpi_cartridges.date_in";
142
        }
143
        $query = "SELECT glpi_cartridges.*, glpi_printers.ID as printID, glpi_printers.name as printname, glpi_printers.initial_pages as initial_pages FROM glpi_cartridges LEFT JOIN glpi_printers ON (glpi_cartridges.FK_glpi_printers = glpi_printers.ID) WHERE (glpi_cartridges.FK_glpi_cartridges_type = '$tID') $where ORDER BY $ORDER";
144

    
145
        $pages=array();
146
        if ($result = $DB->query($query)) {                        
147
                $number=$DB->numrows($result);
148
                while ($data=$DB->fetch_array($result)) {
149
                        $date_in=convDate($data["date_in"]);
150
                        $date_use=convDate($data["date_use"]);
151
                        $date_out=convDate($data["date_out"]);
152
                        $printer=$data["FK_glpi_printers"];
153
                        $page=$data["pages"];
154

    
155
                        echo "<tr  class='tab_bg_1'><td class='center'>";
156
                        echo $data["ID"]; 
157
                        echo "</td><td class='center'>";
158
                        echo getCartridgeStatus($data["date_use"],$data["date_out"]);
159
                        echo "</td><td class='center'>";
160
                        echo $date_in;
161
                        echo "</td><td class='center'>";
162
                        echo $date_use;
163
                        echo "</td><td class='center'>";
164
                        if (!is_null($date_use)){
165
                                if ($data["printID"]>0){
166
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/printer.form.php?ID=".$data["printID"]."'><strong>".$data["printname"];
167
                                if ($CFG_GLPI["view_ID"]){
168
                                        echo " (".$data["printID"].")";
169
                                }
170
                                echo "</strong></a>";
171
                                } else {
172
                                        echo "N/A";
173
                                }
174
                                $tmp_dbeg=split("-",$data["date_in"]);
175
                                $tmp_dend=split("-",$data["date_use"]);
176
                                $stock_time_tmp= mktime(0,0,0,$tmp_dend[1],$tmp_dend[2],$tmp_dend[0]) 
177
                                        - mktime(0,0,0,$tmp_dbeg[1],$tmp_dbeg[2],$tmp_dbeg[0]);                
178
                                $stock_time+=$stock_time_tmp;
179
                        }
180
                        echo "</td><td class='center'>";
181
                        echo $date_out;                
182
                        if ($show_old){
183
                                $tmp_dbeg=split("-",$data["date_use"]);
184
                                $tmp_dend=split("-",$data["date_out"]);
185

    
186
                                $use_time_tmp= mktime(0,0,0,$tmp_dend[1],$tmp_dend[2],$tmp_dend[0]) 
187
                                        - mktime(0,0,0,$tmp_dbeg[1],$tmp_dbeg[2],$tmp_dbeg[0]);                
188
                                $use_time+=$use_time_tmp;
189
                        }
190

    
191
                        echo "</td>";
192
                        if ($show_old){
193
                                // Get initial counter page
194
                                if (!isset($pages[$printer])){
195
                                        $pages[$printer]=$data['initial_pages'];
196
                                }
197
                                echo "<td class='center'>";
198
                                if ($pages[$printer]<$data['pages']){
199
                                        $pages_printed+=$data['pages']-$pages[$printer];
200
                                        $nb_pages_printed++;
201
                                        echo ($data['pages']-$pages[$printer])." ".$LANG["printers"][31];
202
                                        $pages[$printer]=$data['pages'];
203
                                }
204
                                echo "</td>";
205
                        } 
206
                        echo "<td class='center'>";
207
                        showDisplayInfocomLink(CARTRIDGE_ITEM_TYPE,$data["ID"],1);
208
                        echo "</td>";
209
                        echo "<td class='center'>";
210
                        if (!is_null($date_use)&&$canedit)
211
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/cartridge.edit.php?restore=restore&amp;ID=".$data["ID"]."&amp;tID=$tID'>".$LANG["consumables"][37]."</a>";                
212
                        else echo "&nbsp;";
213

    
214
                        echo "</td>";
215
                        echo "<td class='center'>";
216
                        if ($canedit){
217
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/cartridge.edit.php?delete=delete&amp;ID=".$data["ID"]."&amp;tID=$tID'>".$LANG["buttons"][6]."</a>";
218
                        } else echo "&nbsp;";
219
                        echo "</td></tr>";
220
                }        
221
                if ($show_old&&$number>0){
222
                        if ($nb_pages_printed==0) $nb_pages_printed=1;
223
                        echo "<tr class='tab_bg_2'><td colspan='3'>&nbsp;</td>";
224
                        echo "<td class='center'>".$LANG["cartridges"][40].":<br>".round($stock_time/$number/60/60/24/30.5,1)." ".$LANG["financial"][57]."</td>";
225
                        echo "<td>&nbsp;</td>";
226
                        echo "<td class='center'>".$LANG["cartridges"][41].":<br>".round($use_time/$number/60/60/24/30.5,1)." ".$LANG["financial"][57]."</td>";
227
                        echo "<td class='center'>".$LANG["cartridges"][42].":<br>".round($pages_printed/$nb_pages_printed)."</td>";
228
                        echo "<td colspan='3'>&nbsp;</td></tr>";
229
                }
230
        }        
231
        echo "</table></div>\n\n";
232
}
233

    
234

    
235

    
236
/**
237
 * Show the printer types that are compatible with a cartridge type
238
 *
239
 * Show the printer types that are compatible with the cartridge type identified by $instID
240
 *
241
 *@param $instID : cartridge type identifier
242
 *
243
 *@return nothing (display)
244
 *
245
 **/
246
function showCompatiblePrinters($instID) {
247
        global $DB,$CFG_GLPI, $LANG;
248

    
249
        if (!haveRight("cartridge","r")) return false;
250

    
251
        $query = "SELECT glpi_dropdown_model_printers.name as type, glpi_cartridges_assoc.ID as ID FROM glpi_cartridges_assoc, glpi_dropdown_model_printers WHERE glpi_cartridges_assoc.FK_glpi_dropdown_model_printers=glpi_dropdown_model_printers.ID AND glpi_cartridges_assoc.FK_glpi_cartridges_type = '$instID' order by glpi_dropdown_model_printers.name";
252

    
253
        $result = $DB->query($query);
254
        $number = $DB->numrows($result);
255
        $i = 0;
256

    
257
        echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/cartridge.form.php\">";
258
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
259
        echo "<tr><th colspan='3'>".$LANG["cartridges"][32].":</th></tr>";
260
        echo "<tr><th>".$LANG["common"][2]."</th><th>".$LANG["common"][22]."</th><th>&nbsp;</th></tr>";
261

    
262
        while ($i < $number) {
263
                $ID=$DB->result($result, $i, "ID");
264
                $type=$DB->result($result, $i, "type");
265
                echo "<tr class='tab_bg_1'><td class='center'>$ID</td>";
266
                echo "<td class='center'>$type</td>";
267
                echo "<td align='center' class='tab_bg_2'><a href='".$_SERVER['PHP_SELF']."?deletetype=deletetype&amp;ID=$ID&amp;tID=$instID'><strong>".$LANG["buttons"][6]."</strong></a></td></tr>";
268
                $i++;
269
        }
270
        if (haveRight("cartridge","w")){
271
                echo "<tr class='tab_bg_1'><td>&nbsp;</td><td class='center'>";
272
                echo "<div class='software-instal'><input type='hidden' name='tID' value='$instID'>";
273
                dropdown("glpi_dropdown_model_printers","model");
274
                echo "</div></td><td align='center' class='tab_bg_2'>";
275
                echo "<input type='submit' name='addtype' value=\"".$LANG["buttons"][8]."\" class='submit'>";
276
                echo "</td></tr>";
277
        }
278

    
279
        echo "</table></div></form>"    ;
280
}
281

    
282
/**
283
 * Show installed cartridges
284
 *
285
 * Show installed cartridge for the printer type $instID
286
 *
287
 *@param $instID integer: printer type identifier.
288
 *@param $old boolean : old cartridges or not ?
289
 *
290
 *@return nothing (display)
291
 *
292
 **/
293
function showCartridgeInstalled($instID,$old=0) {
294

    
295
        global $DB,$CFG_GLPI, $LANG;
296
        
297
        if (!haveRight("cartridge","r")) return false;
298
        $canedit=haveRight("cartridge","w");
299

    
300
        $query = "SELECT glpi_cartridges_type.ID as tID, glpi_cartridges_type.deleted as deleted, glpi_cartridges_type.ref as ref, glpi_cartridges_type.name as type, glpi_cartridges.ID as ID, glpi_cartridges.pages as pages, glpi_cartridges.date_use as date_use, glpi_cartridges.date_out as date_out, glpi_cartridges.date_in as date_in";
301
        if ($old==0){
302
                $query.= " FROM glpi_cartridges, glpi_cartridges_type WHERE glpi_cartridges.date_out IS NULL AND glpi_cartridges.FK_glpi_printers= '$instID' AND glpi_cartridges.FK_glpi_cartridges_type  = glpi_cartridges_type.ID ORDER BY glpi_cartridges.date_out ASC, glpi_cartridges.date_use DESC, glpi_cartridges.date_in";
303
        } else {
304
                $query.= " FROM glpi_cartridges, glpi_cartridges_type WHERE glpi_cartridges.date_out IS NOT NULL AND glpi_cartridges.FK_glpi_printers= '$instID' AND glpi_cartridges.FK_glpi_cartridges_type  = glpi_cartridges_type.ID ORDER BY glpi_cartridges.date_out ASC, glpi_cartridges.date_use DESC, glpi_cartridges.date_in";
305
        }
306
        
307
        $result = $DB->query($query);
308
        $number = $DB->numrows($result);
309
        $i = 0;
310
        $p=new Printer;
311
        $p->getFromDB($instID);
312
        $pages=$p->fields['initial_pages'];
313

    
314
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
315
        if ($old==0)
316
                echo "<tr><th colspan='7'>".$LANG["cartridges"][33].":</th></tr>";
317
        else echo "<tr><th colspan='8'>".$LANG["cartridges"][35].":</th></tr>";
318

    
319
        echo "<tr><th>".$LANG["common"][2]."</th><th>".$LANG["cartridges"][12]."</th><th>".$LANG["consumables"][23]."</th><th>".$LANG["cartridges"][24]."</th><th>".$LANG["consumables"][26]."</th><th>".$LANG["search"][9]."</th>";
320
        if ($old!=0)
321
                echo "<th>".$LANG["cartridges"][39]."</th>";
322

    
323
        echo "<th>&nbsp;</th></tr>";
324

    
325
        $stock_time=0;
326
        $use_time=0;        
327
        $pages_printed=0;
328
        $nb_pages_printed=0;
329
        $ci=new CommonItem();
330
        while ($data=$DB->fetch_array($result)) {
331
                $date_in=convDate($data["date_in"]);
332
                $date_use=convDate($data["date_use"]);
333
                $date_out=convDate($data["date_out"]);
334
                echo "<tr  class='tab_bg_1".($data["deleted"]?"_2":"")."'><td class='center'>";
335
                echo $data["ID"]; 
336
                echo "</td><td class='center'><strong>";
337
                echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/cartridge.form.php?ID=".$data["tID"]."\">";
338
                echo $data["type"]." - ".$data["ref"];
339
                echo "</a>";
340
                echo "</strong></td><td class='center'>";
341
                echo getCartridgeStatus($data["date_use"],$data["date_out"]);
342
                echo "</td><td class='center'>";
343
                echo $date_in;
344
                echo "</td><td class='center'>";
345
                echo $date_use;
346

    
347
                $tmp_dbeg=split("-",$date_in);
348
                $tmp_dend=split("-",$date_use);
349

    
350
                $stock_time_tmp= mktime(0,0,0,$tmp_dend[1],$tmp_dend[2],$tmp_dend[0]) 
351
                        - mktime(0,0,0,$tmp_dbeg[1],$tmp_dbeg[2],$tmp_dbeg[0]);
352
                $stock_time+=$stock_time_tmp;
353

    
354
                echo "</td><td class='center'>";
355
                echo $date_out;                
356

    
357
                if ($old!=0){
358
                        $tmp_dbeg=split("-",$date_use);
359
                        $tmp_dend=split("-",$date_out);
360

    
361
                        $use_time_tmp= mktime(0,0,0,$tmp_dend[1],$tmp_dend[2],$tmp_dend[0]) 
362
                                - mktime(0,0,0,$tmp_dbeg[1],$tmp_dbeg[2],$tmp_dbeg[0]);                
363
                        $use_time+=$use_time_tmp;
364
                }
365

    
366
                echo "</td><td class='center'>";
367
                if ($old!=0){
368
                        if ($canedit){
369
                                echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/cartridge.edit.php\">";
370
                                echo "<input type='hidden' name='cID' value='".$data['ID']."'>";
371
                        }
372
                        echo "<input type='text' name='pages' value=\"".$data['pages']."\" size='10'>";
373
                        if ($canedit){
374
                                echo "<input type='image' name='update_pages' value='update_pages' src='".$CFG_GLPI["root_doc"]."/pics/actualiser.png' class='calendrier'>";
375
                                echo "</form>";
376
                        }
377
                        if ($pages<$data['pages']){
378
                                $pages_printed+=$data['pages']-$pages;
379
                                $nb_pages_printed++;
380
                                echo ($data['pages']-$pages)." ".$LANG["printers"][31];
381
                                $pages=$data['pages'];
382
                        }
383
                        echo "</td><td class='center'>";
384
                }
385
                if ($canedit)
386
                        if (is_null($date_out))
387
                                echo "&nbsp;&nbsp;&nbsp;<a href='".$CFG_GLPI["root_doc"]."/front/cartridge.edit.php?uninstall=uninstall&amp;ID=".$data["ID"]."&amp;tID=".$data["tID"]."'>".$LANG["cartridges"][29]."</a>";
388
                        else echo "&nbsp;&nbsp;&nbsp;<a href='".$CFG_GLPI["root_doc"]."/front/cartridge.edit.php?delete=delete&amp;ID=".$data["ID"]."&amp;tID=".$data["tID"]."'>".$LANG["buttons"][6]."</a>";
389
                        echo "</td></tr>";
390

    
391
        }        
392
        if ($old==0&&$canedit){
393
                echo "<tr class='tab_bg_1'><td>&nbsp;</td><td align='center' colspan='5'>";
394
                echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/cartridge.edit.php\">";
395

    
396
                echo "<div class='software-instal'><input type='hidden' name='pID' value='$instID'>";
397
                if (dropdownCompatibleCartridges($instID)){
398
                        echo "&nbsp;<input type='submit' name='install' value=\"".$LANG["buttons"][4]."\" class='submit'>";
399
                }
400

    
401
                echo "</div></form></td><td align='center' class='tab_bg_2'>&nbsp;";
402
                echo "</td>";
403
                echo "</tr>";
404
        } else { // Print average
405
                if ($number>0){
406
                        if ($nb_pages_printed==0) $nb_pages_printed=1;
407
                        echo "<tr class='tab_bg_2'><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
408

    
409
                        echo "<td class='center'>".$LANG["cartridges"][40].":<br>".round($stock_time/$number/60/60/24/30.5,1)." ".$LANG["financial"][57]."</td>";
410
                        echo "<td class='center'>".$LANG["cartridges"][41].":<br>".round($use_time/$number/60/60/24/30.5,1)." ".$LANG["financial"][57]."</td>";
411
                        echo "<td class='center'>".$LANG["cartridges"][42].":<br>".round($pages_printed/$nb_pages_printed)."</td>";
412
                        echo "<td>&nbsp;</td></tr>";
413
                }
414
        }
415
        echo "</table></div>";
416
}
417

    
418

    
419
/**
420
 * Print the cartridge count HTML array for a defined cartridge type
421
 *
422
 * Print the cartridge count HTML array for the cartridge type $tID
423
 *
424
 *@param $tID integer: cartridge type identifier.
425
 *@param $alarm integer: threshold alarm value.
426
 *@param $nohtml integer: Return value without HTML tags.
427
 *
428
 *@return string to display
429
 *
430
 **/
431
function countCartridges($tID,$alarm,$nohtml=0) {
432
        global $DB,$CFG_GLPI, $LANG;
433

    
434
        // Get total
435
        $total = getCartridgesNumber($tID);
436
        $out="";
437
        if ($total!=0) {
438
                $unused=getUnusedCartridgesNumber($tID);
439
                $used=getUsedCartridgesNumber($tID);
440
                $old=getOldCartridgesNumber($tID);
441

    
442
                $highlight="";
443
                if ($unused<=$alarm)
444
                        $highlight="class='tab_bg_1_2'";
445

    
446
                if (!$nohtml)
447
                        $out.= "<div $highlight>".$LANG["common"][33].":&nbsp;$total&nbsp;&nbsp;&nbsp;<strong>".$LANG["cartridges"][13].": $unused</strong>&nbsp;&nbsp;&nbsp;".$LANG["cartridges"][14].": $used&nbsp;&nbsp;&nbsp;".$LANG["cartridges"][15].": $old</div>";
448
                else         $out.= $LANG["common"][33].": $total   ".$LANG["cartridges"][13].": $unused   ".$LANG["cartridges"][14].": $used   ".$LANG["cartridges"][15].": $old";                
449

    
450
        } else {
451
                if (!$nohtml)
452
                        $out.= "<div class='tab_bg_1_2'><i>".$LANG["cartridges"][9]."</i></div>";
453
                else $out.= $LANG["cartridges"][9];
454
        }
455
        return $out;
456
}        
457

    
458
/**
459
 * count how many cartbridge for a cartbridge type
460
 *
461
 * count how many cartbridge for the cartbridge type $tID
462
 *
463
 *@param $tID integer: cartridge type identifier.
464
 *
465
 *@return integer : number of cartridge counted.
466
 *
467
 **/
468
function getCartridgesNumber($tID){
469
        global $DB;
470
        $query = "SELECT ID FROM glpi_cartridges WHERE ( FK_glpi_cartridges_type = '$tID')";
471
        $result = $DB->query($query);
472
        return $DB->numrows($result);
473
}
474

    
475
/**
476
 * count how many cartridge used for a cartbridge type
477
 *
478
 * count how many cartridge used for the cartbridge type $tID
479
 *
480
 *@param $tID integer: cartridge type identifier.
481
 *
482
 *@return integer : number of cartridge used counted.
483
 *
484
 **/
485
function getUsedCartridgesNumber($tID){
486
        global $DB;
487
        $query = "SELECT ID FROM glpi_cartridges WHERE ( FK_glpi_cartridges_type = '$tID' AND date_use IS NOT NULL AND date_out IS NULL)";
488
        $result = $DB->query($query);
489
        return $DB->numrows($result);
490
}
491

    
492
/**
493
 * count how many old cartbridge for a cartbridge type
494
 *
495
 * count how many old cartbridge for the cartbridge type $tID
496
 *
497
 *@param $tID integer: cartridge type identifier.
498
 *
499
 *@return integer : number of old cartridge counted.
500
 *
501
 **/
502
function getOldCartridgesNumber($tID){
503
        global $DB;
504
        $query = "SELECT ID FROM glpi_cartridges WHERE ( FK_glpi_cartridges_type = '$tID'  AND date_out IS NOT NULL)";
505
        $result = $DB->query($query);
506
        return $DB->numrows($result);
507
}
508
/**
509
 * count how many cartbridge unused for a cartbridge type
510
 *
511
 * count how many cartbridge unused for the cartbridge type $tID
512
 *
513
 *@param $tID integer: cartridge type identifier.
514
 *
515
 *@return integer : number of cartridge unused counted.
516
 *
517
 **/
518
function getUnusedCartridgesNumber($tID){
519
        global $DB;
520
        $query = "SELECT ID FROM glpi_cartridges WHERE ( FK_glpi_cartridges_type = '$tID'  AND date_use IS NULL)";
521
        $result = $DB->query($query);
522
        return $DB->numrows($result);
523
}
524

    
525
/**
526
 * Print a select with compatible cartridge
527
 *
528
 * Print a select that contains compatibles cartridge for a printer model $pID
529
 *
530
 *@param $pID integer: printer type identifier.
531
 *
532
 *@return nothing (display)
533
 *
534
 **/
535
function dropdownCompatibleCartridges($pID) {
536

    
537
        global $DB,$LANG;
538

    
539
        $p=new Printer;
540
        $p->getFromDB($pID);
541

    
542
        $query = "SELECT COUNT(*) AS cpt, glpi_dropdown_locations.completename as location, glpi_cartridges_type.ref as ref, glpi_cartridges_type.name as name, glpi_cartridges_type.ID as tID 
543
                FROM glpi_cartridges_type 
544
                INNER JOIN glpi_cartridges_assoc ON (glpi_cartridges_type.ID = glpi_cartridges_assoc.FK_glpi_cartridges_type )
545
                INNER JOIN glpi_cartridges ON (glpi_cartridges.FK_glpi_cartridges_type = glpi_cartridges_type.ID 
546
                                                AND glpi_cartridges.date_use IS NULL)
547
                LEFT JOIN glpi_dropdown_locations ON (glpi_dropdown_locations.ID = glpi_cartridges_type.location)
548
                WHERE  glpi_cartridges_assoc.FK_glpi_dropdown_model_printers = '".$p->fields["model"]."' 
549
                AND glpi_cartridges_type.FK_entities='".$p->fields["FK_entities"]."' 
550
                GROUP BY glpi_cartridges_type.ID 
551
                ORDER BY glpi_cartridges_type.name, glpi_cartridges_type.ref";
552
        if ($result = $DB->query($query)){
553
                if ($DB->numrows($result)){
554

    
555
                        echo "<select name='tID' size=1>";
556
                        while ($data= $DB->fetch_assoc($result)) {
557
                                echo  "<option value='".$data["tID"]."'>".$data["name"]." - ".$data["ref"]." (".$data["cpt"]." ".$LANG["cartridges"][13].") - ".$data["location"]."</option>";
558
                        }
559
                        echo "</select>";
560
                        return true;
561
                }
562
                
563
        }
564
        return false;
565
        
566
}
567

    
568
/**
569
 * Get the dict value for the status of a cartridge
570
 *
571
 * 
572
 *
573
*@param $date_use date : date of use
574
*@param $date_out date : date of delete
575
 *
576
 *@return string : dict value for the cartridge status.
577
 *
578
 **/
579
function getCartridgeStatus($date_use,$date_out){
580
        global $LANG;
581
        if (is_null($date_use)||empty($date_use)) {
582
                return $LANG["cartridges"][20];
583
        }
584
        else if (is_null($date_out)||empty($date_out)) {
585
                return $LANG["cartridges"][21];
586
        } else {
587
                return $LANG["cartridges"][22];
588
        }
589
}
590
/**
591
 * Cron action on cartridges : alert if a stock is behind the threshold
592
 * @param $display display informations instead or log in file ?
593
 * @return 0 : nothing to do 1 : done with success
594
 *
595
 **/
596
function cron_cartridge($display=false){
597
        global $DB,$CFG_GLPI,$LANG;
598

    
599
        if (!$CFG_GLPI["mailing"]||!$CFG_GLPI["cartridges_alert"]){
600
                return false;
601
        }
602

    
603
        loadLanguage($CFG_GLPI["default_language"]);
604

    
605
        // Get cartridges type with alarm activated and last warning > X days depending on config
606
        $query="SELECT glpi_cartridges_type.ID AS cartID, glpi_cartridges_type.FK_entities as entity, glpi_cartridges_type.ref as cartref, glpi_cartridges_type.name AS cartname, glpi_cartridges_type.alarm AS threshold, glpi_alerts.ID AS alertID, glpi_alerts.date 
607
                FROM glpi_cartridges_type 
608
                LEFT JOIN glpi_alerts ON (glpi_cartridges_type.ID = glpi_alerts.FK_device AND glpi_alerts.device_type='".CARTRIDGE_TYPE."') 
609
                WHERE glpi_cartridges_type.deleted='0' AND glpi_cartridges_type.alarm>='0' 
610
                        AND (glpi_alerts.date IS NULL OR (glpi_alerts.date+".$CFG_GLPI["cartridges_alert"].") < CURRENT_TIMESTAMP()) 
611
                ORDER BY glpi_cartridges_type.name;";
612

    
613
        $result=$DB->query($query);
614

    
615
        $message=array();
616
        $items=array();
617
        $alert=new Alert();
618

    
619
        if ($DB->numrows($result)>0){
620
                while ($data=$DB->fetch_array($result)){
621
                        if (($unused=getUnusedCartridgesNumber($data["cartID"]))<=$data["threshold"]){
622
                                if (!isset($message[$data["entity"]])){
623
                                        $message[$data["entity"]]="";
624
                                }
625
                                if (!isset($items[$data["entity"]])){
626
                                        $items[$data["entity"]]=array();
627
                                }
628

    
629
                                // define message alert
630
                                $message[$data["entity"]].=$LANG["mailing"][34]." ".$data["cartname"]." - ".$LANG["consumables"][2].": ".$data["cartref"]." - ".$LANG["software"][20].": ".$unused."<br>\n";
631
                                $items[$data["entity"]][]=$data["cartID"];
632

    
633
                                // if alert exists -> delete 
634
                                if (!empty($data["alertID"])){
635
                                        $alert->delete(array("ID"=>$data["alertID"]));
636
                                }
637
                        }
638
                }
639

    
640
                if (count($message)>0){
641
                        foreach ($message as $entity => $msg){
642
                                $mail=new MailingAlert("alertcartridge",$msg,$entity);
643

    
644
                                if ($mail->send()){
645
                                        if ($display){
646
                                                addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  $msg");
647
                                        }
648
                                        logInFile("cron",getDropdownName("glpi_entities",$entity).":  $msg\n");
649

    
650
                                        $input["type"]=ALERT_THRESHOLD;
651
                                        $input["device_type"]=CARTRIDGE_TYPE;
652

    
653
                                        //// add alerts
654
                                        foreach ($items[$entity] as $ID){
655
                                                $input["FK_device"]=$ID;
656
                                                $alert->add($input);
657
                                                unset($alert->fields['ID']);
658
                                        }
659

    
660
                                } else {
661
                                        if ($display){
662
                                                addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  Send infocom alert failed");
663
                                        }
664
                                        logInFile("cron",getDropdownName("glpi_entities",$entity).":  Send cartdridge alert failed");
665
                                }
666
                        }
667
                        return 1;
668
                }
669
        }
670

    
671
        return 0;
672
}
673

    
674
?>
Redmine Appliance - Powered by TurnKey Linux