-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathconsumable.class.php
742 lines (632 loc) · 23.6 KB
/
consumable.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<?php
/*
* @version $Id$
-------------------------------------------------------------------------
GLPI - Gestionnaire Libre de Parc Informatique
Copyright (C) 2003-2014 by the INDEPNET Development Team.
http://indepnet.net/ http://glpi-project.org
-------------------------------------------------------------------------
LICENSE
This file is part of GLPI.
GLPI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
/** @file
* @brief
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
}
//! Consumable Class
/**
This class is used to manage the consumables.
@see ConsumableItem
@author Julien Dombre
**/
class Consumable extends CommonDBChild {
// From CommonDBTM
static protected $forward_entity_to = array('Infocom');
var $no_form_page = true;
static $rightname = 'consumable';
// From CommonDBChild
static public $itemtype = 'ConsumableItem';
static public $items_id = 'consumableitems_id';
/**
* @since version 0.84
**/
function getForbiddenStandardMassiveAction() {
$forbidden = parent::getForbiddenStandardMassiveAction();
$forbidden[] = 'update';
return $forbidden;
}
/**
* since version 0.84
*
* @see CommonDBTM::getNameField()
**/
static function getNameField() {
return 'id';
}
static function getTypeName($nb=0) {
return _n('Consumable', 'Consumables', $nb);
}
function cleanDBonPurge() {
global $DB;
$query = "DELETE
FROM `glpi_infocoms`
WHERE (`items_id` = '".$this->fields['id']."'
AND `itemtype` = '".$this->getType()."')";
$result = $DB->query($query);
}
function prepareInputForAdd($input) {
$item = new ConsumableItem();
if ($item->getFromDB($input["consumableitems_id"])) {
return array("consumableitems_id" => $item->fields["id"],
"entities_id" => $item->getEntityID(),
"date_in" => date("Y-m-d"));
}
return array();
}
function post_addItem() {
Infocom::cloneItem('ConsumableItem', $this->fields["consumableitems_id"], $this->fields['id'],
$this->getType());
}
/**
* send back to stock
**/
function backToStock(array $input, $history=1) {
global $DB;
$query = "UPDATE `".$this->getTable()."`
SET `date_out` = NULL
WHERE `id` = '".$input["id"]."'";
if ($result = $DB->query($query)) {
return true;
}
return false;
}
/**
* @since version 0.84
*
* @see CommonDBTM::getPreAdditionalInfosForName
**/
function getPreAdditionalInfosForName() {
$ci = new ConsumableItem();
if ($ci->getFromDB($this->fields['consumableitems_id'])) {
return $ci->getName();
}
return '';
}
/**
* UnLink a consumable linked to a printer
*
* UnLink the consumable identified by $ID
*
* @param $ID consumable identifier
* @param $itemtype itemtype of who we give the consumable (default '')
* @param $items_id ID of the item giving the consumable (default 0)
*
* @return boolean
**/
function out($ID, $itemtype='', $items_id=0) {
global $DB;
if (!empty($itemtype)
&& ($items_id > 0)) {
$query = "UPDATE `".$this->getTable()."`
SET `date_out` = '".date("Y-m-d")."',
`itemtype` = '$itemtype',
`items_id` = '$items_id'
WHERE `id` = '$ID'";
if ($result = $DB->query($query)) {
return true;
}
}
return false;
}
/**
* @since version 0.85
*
* @see CommonDBTM::showMassiveActionsSubForm()
**/
static function showMassiveActionsSubForm(MassiveAction $ma) {
global $CFG_GLPI;
$input = $ma->getInput();
switch ($ma->getAction()) {
case 'give' :
if (isset($input["entities_id"])) {
Dropdown::showSelectItemFromItemtypes(array('itemtype_name'
=> 'give_itemtype',
'items_id_name'
=> 'give_items_id',
'entity_restrict'
=> $input["entities_id"],
'itemtypes'
=> $CFG_GLPI["consumables_types"]));
echo "<br><br>".Html::submit(_x('button', 'Give'),
array('name' => 'massiveaction'));
return true;
}
}
return parent::showMassiveActionsSubForm($ma);
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item,
array $ids) {
switch ($ma->getAction()) {
case 'backtostock' :
foreach ($ids as $id) {
if ($item->can($id, UPDATE)) {
if ($item->backToStock(array("id" => $id))) {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
$ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
$ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
}
}
return;
case 'give' :
$input = $ma->getInput();
if (($input["give_items_id"] > 0)
&& !empty($input['give_itemtype'])) {
foreach ($ids as $key) {
if ($item->can($key, UPDATE)) {
if ($item->out($key, $input['give_itemtype'],$input["give_items_id"])) {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
$ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
$ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
}
}
Event::log($item->fields['consumableitems_id'], "consumables", 5, "inventory",
//TRANS: %s is the user login
sprintf(__('%s gives a consumable'), $_SESSION["glpiname"]));
} else {
$ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
}
return;
}
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
}
/**
* count how many consumable for the consumable item $tID
*
* @param $tID integer consumable item identifier.
*
* @return integer : number of consumable counted.
**/
static function getTotalNumber($tID) {
global $DB;
$query = "SELECT `id`
FROM `glpi_consumables`
WHERE `consumableitems_id` = '$tID'";
$result = $DB->query($query);
return $DB->numrows($result);
}
/**
* count how many old consumable for the consumable item $tID
*
* @param $tID integer consumable item identifier.
*
* @return integer : number of old consumable counted.
**/
static function getOldNumber($tID) {
global $DB;
$query = "SELECT `id`
FROM `glpi_consumables`
WHERE (`consumableitems_id` = '$tID'
AND `date_out` IS NOT NULL)";
$result = $DB->query($query);
return $DB->numrows($result);
}
/**
* count how many consumable unused for the consumable item $tID
*
* @param $tID integer consumable item identifier.
*
* @return integer : number of consumable unused counted.
**/
static function getUnusedNumber($tID) {
global $DB;
$query = "SELECT `id`
FROM `glpi_consumables`
WHERE (`consumableitems_id` = '$tID'
AND `date_out` IS NULL)";
$result = $DB->query($query);
return $DB->numrows($result);
}
/**
* Get the consumable count HTML array for a defined consumable type
*
* @param $tID integer consumable item identifier.
* @param $alarm_threshold integer threshold alarm value.
* @param $nohtml integer Return value without HTML tags. (default 0)
*
* @return string to display
**/
static function getCount($tID, $alarm_threshold, $nohtml=0) {
// Get total
$total = self::getTotalNumber($tID);
if ($total != 0) {
$unused = self::getUnusedNumber($tID);
$old = self::getOldNumber($tID);
$highlight = "";
if ($unused <= $alarm_threshold) {
$highlight = "class='tab_bg_1_2'";
}
//TRANS: For consumable. %1$d is total number, %2$d is unused number, %3$d is old number
$tmptxt = sprintf(__('Total: %1$d, New: %2$d, Used: %3$d'), $total, $unused, $old);
if ($nohtml) {
$out = $tmptxt;
} else {
$out = "<div $highlight>".$tmptxt."</div>";
}
} else {
if ($nohtml) {
$out = __('No consumable');
} else {
$out = "<div class='tab_bg_1_2'><i>".__('No consumable')."</i></div>";
}
}
return $out;
}
/**
* Check if a Consumable is New (not used, in stock)
*
* @param $cID integer consumable ID.
**/
static function isNew($cID) {
global $DB;
$query = "SELECT `id`
FROM `glpi_consumables`
WHERE (`id` = '$cID'
AND `date_out` IS NULL)";
$result = $DB->query($query);
return ($DB->numrows($result) == 1);
}
/**
* Check if a consumable is Old (used, not in stock)
*
* @param $cID integer consumable ID.
**/
static function isOld($cID) {
global $DB;
$query = "SELECT `id`
FROM `glpi_consumables`
WHERE (`id` = '$cID'
AND `date_out` IS NOT NULL)";
$result = $DB->query($query);
return ($DB->numrows($result) == 1);
}
/**
* Get the localized string for the status of a consumable
*
* @param $cID integer consumable ID.
*
* @return string : dict value for the consumable status.
**/
static function getStatus($cID) {
if (self::isNew($cID)) {
return _nx('consumable', 'New', 'New',1);
} else if (self::isOld($cID)) {
return _nx('consumable', 'Used', 'Used',1);
}
}
/**
* Print out a link to add directly a new consumable from a consumable item.
*
* @param $consitem ConsumableItem object
*
* @return Nothing (displays)
**/
static function showAddForm(ConsumableItem $consitem) {
global $CFG_GLPI;
$ID = $consitem->getField('id');
if (!$consitem->can($ID, UPDATE)) {
return false;
}
if ($ID > 0) {
echo "<div class='firstbloc'>";
echo "<form method='post' action=\"".static::getFormURL()."\">";
echo "<table class='tab_cadre_fixe'>";
echo "<tr><td class='tab_bg_2 center'>";
echo "<input type='hidden' name='consumableitems_id' value='$ID'>\n";
Dropdown::showNumber('to_add', array('value' => 1,
'min' => 1,
'max' => 100));
echo " <input type='submit' name='add_several' value=\""._sx('button','Add consumables')."\"
class='submit'>";
echo "</td></tr>";
echo "</table>";
Html::closeForm();
echo "</div>";
}
}
/**
* Print out the consumables of a defined type
*
* @param $consitem ConsumableItem object
* @param $show_old boolean show old consumables or not. (default 0)
*
* @return Nothing (displays)
**/
static function showForConsumableItem(ConsumableItem $consitem, $show_old=0) {
global $DB, $CFG_GLPI;
$tID = $consitem->getField('id');
if (!$consitem->can($tID, READ)) {
return false;
}
$canedit = $consitem->can($tID, UPDATE);
$rand = mt_rand();
$where = "";
if (!$show_old) { // NEW
$where = " AND `date_out` IS NULL
ORDER BY `date_in`, `id`";
} else { //OLD
$where = " AND `date_out` IS NOT NULL
ORDER BY `date_out` DESC,
`date_in`,
`id`";
}
$query = "SELECT `glpi_consumables`.*
FROM `glpi_consumables`
WHERE `consumableitems_id` = '$tID'
$where";
$result = $DB->query($query);
$number = $DB->numrows($result);
echo "<div class='spaced'>";
if ($canedit && $number) {
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$actions = array('delete' => _x('button', 'Delete permanently'),
'Infocom'.MassiveAction::CLASS_ACTION_SEPARATOR.'activate'
=> __('Enable the financial and administrative information'));
if ($show_old) {
$actions['Consumable'.MassiveAction::CLASS_ACTION_SEPARATOR.'backtostock']
= __('Back to stock');
} else {
$actions[__CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'give'] = _x('button', 'Give');
}
$entparam = array('entities_id' => $consitem->getEntityID());
if ($consitem->isRecursive()) {
$entparam = array('entities_id' => getSonsOf('glpi_entities', $consitem->getEntityID()));
}
$massiveactionparams = array('num_displayed' => $number,
'specific_actions' => $actions,
'container' => 'mass'.__CLASS__.$rand,
'extraparams' => $entparam);
Html::showMassiveActions($massiveactionparams);
echo "<input type='hidden' name='consumableitems_id' value='$tID'>\n";
}
echo "<table class='tab_cadre_fixehov'>";
if (!$show_old) {
echo "<tr><th colspan=".($canedit?'5':'4').">";
echo self::getCount($tID, -1);
echo "</th></tr>";
} else { // Old
echo "<tr><th colspan='".($canedit?'7':'6')."'>".__('Used consumables')."</th></tr>";
}
if ($number) {
$i = 0;
$header_begin = "<tr>";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit) {
$header_begin .= "<th width='10'>";
$header_top .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_bottom .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
$header_end .= "</th>";
}
$header_end .= "<th>".__('ID')."</th>";
$header_end .= "<th>"._x('item', 'State')."</th>";
$header_end .= "<th>".__('Add date')."</th>";
if ($show_old) {
$header_end .= "<th>".__('Use date')."</th>";
$header_end .= "<th>".__('Given to')."</th>";
}
$header_end .= "<th width='200px'>".__('Financial and administrative information')."</th>";
$header_end .= "</tr>";
echo $header_begin.$header_top.$header_end;
while ($data = $DB->fetch_assoc($result)) {
$date_in = Html::convDate($data["date_in"]);
$date_out = Html::convDate($data["date_out"]);
echo "<tr class='tab_bg_1'>";
if ($canedit) {
echo "<td width='10'>";
Html::showMassiveActionCheckBox(__CLASS__, $data["id"]);
echo "</td>";
}
echo "<td class='center'>".$data["id"]."</td>";
echo "<td class='center'>".self::getStatus($data["id"])."</td>";
echo "<td class='center'>".$date_in."</td>";
if ($show_old) {
echo "<td class='center'>".$date_out."</td>";
echo "<td class='center'>";
if ($item = getItemForItemtype($data['itemtype'])) {
if ($item->getFromDB($data['items_id'])) {
echo $item->getLink();
}
}
echo "</td>";
}
echo "<td class='center'>";
Infocom::showDisplayLink('Consumable', $data["id"]);
echo "</td>";
echo "</tr>";
}
echo $header_begin.$header_bottom.$header_end;
}
echo "</table>";
if ($canedit && $number) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "</div>";
}
/**
* Show the usage summary of consumables by user
**/
static function showSummary() {
global $DB;
if (!Consumable::canView()) {
return false;
}
$query = "SELECT COUNT(*) AS count, `consumableitems_id`, `itemtype`, `items_id`
FROM `glpi_consumables`
WHERE `date_out` IS NOT NULL
AND `consumableitems_id` IN (SELECT `id`
FROM `glpi_consumableitems` ".
getEntitiesRestrictRequest("WHERE",
"glpi_consumableitems").")
GROUP BY `itemtype`, `items_id`, `consumableitems_id`";
$used = array();
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$used[$data['itemtype'].'####'.$data['items_id']][$data["consumableitems_id"]]
= $data["count"];
}
}
}
$query = "SELECT COUNT(*) AS count, `consumableitems_id`
FROM `glpi_consumables`
WHERE `date_out` IS NULL
AND `consumableitems_id` IN (SELECT `id`
FROM `glpi_consumableitems` ".
getEntitiesRestrictRequest("WHERE",
"glpi_consumableitems").")
GROUP BY `consumableitems_id`";
$new = array();
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$new[$data["consumableitems_id"]] = $data["count"];
}
}
}
$types = array();
$query = "SELECT *
FROM `glpi_consumableitems` ".
getEntitiesRestrictRequest("WHERE","glpi_consumableitems");
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$types[$data["id"]] = $data["name"];
}
}
}
asort($types);
$total = array();
if (count($types) > 0) {
// Produce headline
echo "<div class='center'><table class='tab_cadrehov'><tr>";
// Type
echo "<th>".__('Give to')."</th>";
foreach ($types as $key => $type) {
echo "<th>$type</th>";
$total[$key] = 0;
}
echo "<th>".__('Total')."</th>";
echo "</tr>";
// new
echo "<tr class='tab_bg_2'><td class='b'>".__('In stock')."</td>";
$tot = 0;
foreach ($types as $id_type => $type) {
if (!isset($new[$id_type])) {
$new[$id_type] = 0;
}
echo "<td class='center'>".$new[$id_type]."</td>";
$total[$id_type] += $new[$id_type];
$tot += $new[$id_type];
}
echo "<td class='numeric'>".$tot."</td>";
echo "</tr>";
foreach ($used as $itemtype_items_id => $val) {
echo "<tr class='tab_bg_2'><td>";
list($itemtype,$items_id) = explode('####',$itemtype_items_id);
$item = new $itemtype();
if ($item->getFromDB($items_id)) {
//TRANS: %1$s is a type name - %2$s is a name
printf(__('%1$s - %2$s'), $item->getTypeName(1), $item->getNameID());
}
echo "</td>";
$tot = 0;
foreach ($types as $id_type => $type) {
if (!isset($val[$id_type])) {
$val[$id_type] = 0;
}
echo "<td class='center'>".$val[$id_type]."</td>";
$total[$id_type] += $val[$id_type];
$tot += $val[$id_type];
}
echo "<td class='numeric'>".$tot."</td>";
echo "</tr>";
}
echo "<tr class='tab_bg_1'><td class='b'>".__('Total')."</td>";
$tot = 0;
foreach ($types as $id_type => $type) {
$tot += $total[$id_type];
echo "<td class='numeric'>".$total[$id_type]."</td>";
}
echo "<td class='numeric'>".$tot."</td>";
echo "</tr>";
echo "</table></div>";
} else {
echo "<div class='center b'>".__('No consumable found')."</div>";
}
}
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
if (!$withtemplate && Consumable::canView()) {
switch ($item->getType()) {
case 'ConsumableItem' :
if ($_SESSION['glpishow_count_on_tabs']) {
return self::createTabEntry(self::getTypeName(Session::getPluralNumber()),
self::countForConsumableItem($item));
}
return self::getTypeName(Session::getPluralNumber());
}
}
return '';
}
/**
* @param $item string ConsumableItem object
**/
static function countForConsumableItem(ConsumableItem $item) {
$restrict = "`glpi_consumables`.`consumableitems_id` = '".$item->getField('id') ."'";
return countElementsInTable(array('glpi_consumables'), $restrict);
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
switch ($item->getType()) {
case 'ConsumableItem' :
self::showAddForm($item);
self::showForConsumableItem($item);
self::showForConsumableItem($item, 1);
return true;
}
}
function getRights($interface='central') {
$ci = new ConsumableItem();
return $ci->getRights($interface);
}
}
?>