Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (30,6 ko)

1
<?php
2
/*
3
 * @version $Id: contract.function.php 7875 2009-01-23 15:16:47Z 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

    
43
/**
44
 * Show central contract resume
45
 * HTML array
46
 * 
47
 *
48
 * @return Nothing (display)
49
 *
50
 **/
51
function showCentralContract(){
52

    
53
        global $DB,$CFG_GLPI, $LANG;
54

    
55
        if (!haveRight("contract_infocom","r")) return false;
56

    
57
        // No recursive contract, not in local management 
58
        
59
        // contrats echus depuis moins de 30j
60
        $query = "SELECT count(*)  FROM glpi_contracts WHERE glpi_contracts.deleted='0' ".getEntitiesRestrictRequest("AND","glpi_contracts")." AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )>-30 AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )<0";
61
        $result = $DB->query($query);
62
        $contract0=$DB->result($result,0,0);
63

    
64

    
65
        // contrats  echeance j-7
66
        $query = "SELECT count(*)  FROM glpi_contracts WHERE glpi_contracts.deleted='0' ".getEntitiesRestrictRequest("AND","glpi_contracts")." AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )>0 AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )<=7";
67
        $result = $DB->query($query);
68
        $contract7= $DB->result($result,0,0);
69

    
70

    
71
        // contrats echeance j -30
72
        $query = "SELECT count(*)  FROM glpi_contracts WHERE glpi_contracts.deleted='0' ".getEntitiesRestrictRequest("AND","glpi_contracts")." AND  DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )>7 AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )<30";
73
        $result = $DB->query($query);
74
        $contract30= $DB->result($result,0,0);
75

    
76

    
77
        // contrats avec préavis echeance j-7
78
        $query = "SELECT count(*)  FROM glpi_contracts WHERE glpi_contracts.deleted='0' ".getEntitiesRestrictRequest("AND","glpi_contracts")." AND glpi_contracts.notice<>0 AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL (glpi_contracts.duration-glpi_contracts.notice) MONTH),CURDATE() )>0 AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL(glpi_contracts.duration-glpi_contracts.notice) MONTH),CURDATE() )<=7";
79
        $result = $DB->query($query);
80
        $contractpre7= $DB->result($result,0,0);
81

    
82

    
83
        // contrats avec préavis echeance j -30
84
        $query = "SELECT count(*)  FROM glpi_contracts WHERE glpi_contracts.deleted='0'".getEntitiesRestrictRequest("AND","glpi_contracts")."  AND  glpi_contracts.notice<>0  AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL (glpi_contracts.duration-glpi_contracts.notice) MONTH),CURDATE() )>7 AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL (glpi_contracts.duration-glpi_contracts.notice) MONTH),CURDATE() )<30";
85
        $result = $DB->query($query);
86
        $contractpre30= $DB->result($result,0,0);
87

    
88

    
89

    
90
        echo "<table class='tab_cadrehov' style='text-align:center'>";
91

    
92
        echo "<tr><th colspan='2'><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.php?reset=reset_before\">".$LANG["financial"][1]."</a></th></tr>";
93

    
94
        echo "<tr class='tab_bg_2'>";
95
        echo "<td><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.php?reset_before=1&amp;glpisearchcount=2&amp;sort=12&amp;order=DESC&amp;start=0&amp;field[0]=12&amp;field[1]=12&amp;link[1]=AND&amp;contains[0]=%3C0&amp;contains[1]=%3E-30\">".$LANG["financial"][93]."</a> </td>";
96
        echo "<td>$contract0</td></tr>";
97
        echo "<tr class='tab_bg_2'>";
98
        echo "<td><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.php?reset_before=1&amp;glpisearchcount=2&amp;contains%5B0%5D=%3E0&amp;field%5B0%5D=12&amp;link%5B1%5D=AND&amp;contains%5B1%5D=%3C7&amp;field%5B1%5D=12&amp;sort=12&amp;deleted=0&amp;start=0\">".$LANG["financial"][94]."</a></td>";
99
        echo "<td>".$contract7."</td></tr>";
100
        echo "<tr class='tab_bg_2'>";
101
        echo "<td><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.php?reset_before=1&amp;glpisearchcount=2&amp;contains%5B0%5D=%3E6&amp;field%5B0%5D=12&amp;link%5B1%5D=AND&amp;contains%5B1%5D=%3C30&amp;field%5B1%5D=12&amp;sort=12&amp;deleted=0&amp;start=0\">".$LANG["financial"][95]."</a></td>";
102
        echo "<td>".$contract30."</td></tr>";
103
        echo "<tr class='tab_bg_2'>";
104
        echo "<td><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.php?reset_before=1&amp;glpisearchcount=2&amp;contains%5B0%5D=%3E0&amp;field%5B0%5D=13&amp;link%5B1%5D=AND&amp;contains%5B1%5D=%3C7&amp;field%5B1%5D=13&amp;sort=12&amp;deleted=0&amp;start=0\">".$LANG["financial"][96]."</a></td>";
105
        echo "<td>".$contractpre7."</td></tr>";
106
        echo "<tr class='tab_bg_2'>";
107
        echo "<td><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.php?reset_before=1&amp;glpisearchcount=2&amp;sort=13&amp;order=DESC&amp;start=0&amp;field[0]=13&amp;field[1]=13&amp;link[1]=AND&amp;contains[0]=%3E6&amp;contains[1]=%3C30\">".$LANG["financial"][97]."</a></td>";
108
        echo "<td>".$contractpre30."</td></tr>";
109

    
110
        echo "</table>";
111

    
112

    
113
}
114

    
115

    
116

    
117
/**
118
 * Print the HTML array for contract on devices
119
 *
120
 * Print the HTML array for contract on devices $instID
121
 *
122
 *@param $instID array : Contract identifier.
123
 *
124
 *@return Nothing (display)
125
 *
126
 **/
127
function showDeviceContract($instID) {
128
        global $DB,$CFG_GLPI, $LANG,$INFOFORM_PAGES,$LINK_ID_TABLE,$SEARCH_PAGES;
129

    
130
        if (!haveRight("contract_infocom","r")) return false;
131
        $contract=new Contract();
132
        $canedit=$contract->can($instID,'w');
133
        $query = "SELECT DISTINCT device_type FROM glpi_contract_device WHERE glpi_contract_device.FK_contract = '$instID' order by device_type";
134

    
135
        $result = $DB->query($query);
136
        $number = $DB->numrows($result);
137
        $i = 0;
138

    
139
        echo "<br><br><div class='center'><table class='tab_cadrehov'>";
140
        echo "<tr><th colspan='2'>";
141
        printPagerForm($_SERVER["REQUEST_URI"]);
142
        echo "</th><th colspan='".($canedit ? 4 : 3) ."'>".$LANG["document"][19].":</th></tr>";
143
        if ($canedit) {
144
                echo "</table>";
145
                echo "</div>";
146

    
147
                echo "<form method='post' name='contract_form' id='contract_form' action=\"".$CFG_GLPI["root_doc"]."/front/contract.form.php\">";
148
                echo "<div class='center'>";
149
                echo "<table class='tab_cadrehov'>";
150
                // massive action checkbox
151
                echo "<tr><th>&nbsp;</th>";
152
        } else {
153
                echo "<tr>";
154
        }
155
        echo "<th>".$LANG["common"][16]."</th>";
156
        echo "<th>".$LANG["entity"][0]."</th>";
157
        echo "<th>".$LANG["common"][17]."</th>";
158
        echo "<th>".$LANG["common"][19]."</th>";
159
        echo "<th>".$LANG["common"][20]."</th></tr>";
160
        
161
        $ci=new CommonItem;
162
        while ($i < $number) {
163
                $type=$DB->result($result, $i, "device_type");
164

    
165
                if (haveTypeRight($type,"r")){
166
                         $query = "SELECT ".$LINK_ID_TABLE[$type].".*, glpi_contract_device.ID AS IDD, glpi_entities.ID AS entity"
167
                                ." FROM glpi_contract_device, " .$LINK_ID_TABLE[$type]
168
                                ." LEFT JOIN glpi_entities ON (".$LINK_ID_TABLE[$type].".FK_entities=glpi_entities.ID)"
169
                                ." WHERE ".$LINK_ID_TABLE[$type].".ID = glpi_contract_device.FK_device AND glpi_contract_device.device_type='$type' AND glpi_contract_device.FK_contract = '$instID'";
170
                        if (in_array($LINK_ID_TABLE[$type],$CFG_GLPI["template_tables"])){
171
                                $query.=" AND ".$LINK_ID_TABLE[$type].".is_template='0'";
172
                        }
173
                        $query .= getEntitiesRestrictRequest(" AND",$LINK_ID_TABLE[$type])
174
                                ." ORDER BY glpi_entities.completename, ".$LINK_ID_TABLE[$type].".name";
175

    
176
                        $result_linked=$DB->query($query);
177
                        $nb=$DB->numrows($result_linked);
178
                        if ($nb>$_SESSION["glpilist_limit"] && isset($SEARCH_PAGES["$type"])) {
179
                                $ci->setType($type);
180
                                
181
                                echo "<tr class='tab_bg_1'>";
182
                                if ($canedit) {
183
                                        echo "<td>&nbsp;</td>";        
184
                                }
185
                                echo "<td class='center' colspan='2'><a href='"
186
                                        . $CFG_GLPI["root_doc"]."/".$SEARCH_PAGES["$type"] . "?" . rawurlencode("contains[0]") . "=" . rawurlencode('$$$$'.$instID) . "&amp;" . rawurlencode("field[0]") . "=29&amp;sort=80&amp;order=ASC&amp;deleted=0&amp;start=0"
187
                                        . "'>" . $LANG["reports"][57]."</a></td>";
188
                                echo "<td class='center'>".$ci->getType()."<br>$nb</td>";
189
                                
190
                                echo "<td class='center'>-</td><td class='center'>-</td></tr>";                                
191
                        } else if ($nb>0){
192
                                $ci->setType($type);
193
                                for ($prem=true ; $data=$DB->fetch_assoc($result_linked) ; $prem=false){
194
                                        $ID="";
195
                                        if($CFG_GLPI["view_ID"]||empty($data["name"])) $ID= " (".$data["ID"].")";
196
                                        $name= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data["ID"]."\">".$data["name"]."$ID</a>";
197

    
198
                                        echo "<tr class='tab_bg_1'>";
199
                                        
200
                                        if ($canedit){
201
                                                $sel="";
202
                                                if (isset($_GET["select"])&&$_GET["select"]=="all") $sel="checked";
203
                                                echo "<td width='10'><input type='checkbox' name='item[".$data["IDD"]."]' value='1' $sel></td>";
204
                                        } 
205

    
206
                                        echo "<td class='center' ".(isset($data['deleted'])&&$data['deleted']?"class='tab_bg_2_2'":"").">".$name."</td>";
207
                                        echo "<td>".getDropdownName("glpi_entities",$data['entity'])."</td>";
208
                                        if ($prem) {
209
                                                echo "<td class='center' rowspan='$nb' valign='top'>".$ci->getType().
210
                                                        ($nb>1?"<br>$nb</td>":"</td>");
211
                                        }
212
                                        echo "<td class='center'>".(isset($data["serial"])? "".$data["serial"]."" :"-")."</td>";
213
                                        echo "<td class='center'>".(isset($data["otherserial"])? "".$data["otherserial"]."" :"-")."</td>";
214
                                        
215
                                        echo "</tr>";
216
                                }
217
                        }
218
                }
219
                $i++;
220
        }
221
        if ($canedit){
222
                echo "<tr class='tab_bg_1'><td colspan='4' class='right'>";
223
                echo "<div class='software-instal'><input type='hidden' name='conID' value='$instID'>";
224
                dropdownAllItems("item",0,0,($contract->fields['recursive']?-1:$contract->fields['FK_entities']));
225
                echo "</div></td><td class='center'><input type='submit' name='additem' value=\"".$LANG["buttons"][8]."\" class='submit'>";
226
                echo "<input type='hidden' name='ID' value='$instID'>";
227
                echo "</td><td>&nbsp;</td>";
228
                echo "</tr>";
229
                echo "</table></div>"    ;
230
                
231
                echo "<div class='center'>";
232
                echo "<table width='950px'>";
233
                echo "<tr><td><img src=\"".$CFG_GLPI["root_doc"]."/pics/arrow-left.png\" alt=''></td><td class='center'><a onclick= \"if ( markAllRows('contract_form') ) return false;\" href='".$_SERVER['PHP_SELF']."?ID=$instID&amp;select=all'>".$LANG["buttons"][18]."</a></td>";
234
        
235
                echo "<td>/</td><td class='center'><a onclick= \"if ( unMarkAllRows('contract_form') ) return false;\" href='".$_SERVER['PHP_SELF']."?ID=$instID&amp;select=none'>".$LANG["buttons"][19]."</a>";
236
                echo "</td><td align='left' width='80%'>";
237
                echo "<input type='submit' name='deleteitem' value=\"".$LANG["buttons"][6]."\" class='submit'>";
238
                echo "</td>";
239
                echo "</table>";
240
                echo "</div>";
241
                echo "</form>";
242
                
243
        } else {
244
                echo "</table></div>";
245
        }
246

    
247
}
248

    
249
/**
250
 * Link a contract to a device
251
 *
252
 * Link the contract $conID to the device $ID witch device type is $type. 
253
 *
254
 *@param $conID integer : contract identifier.
255
 *@param $type integer : device type identifier.
256
 *@param $ID integer : device identifier.
257
 *
258
 *@return Nothing ()
259
 *
260
 **/
261
function addDeviceContract($conID,$type,$ID){
262
        global $DB;
263

    
264
        if ($ID>0&&$conID>0){
265

    
266
                $query="INSERT INTO glpi_contract_device (FK_contract,FK_device, device_type ) VALUES ('$conID','$ID','$type');";
267
                $result = $DB->query($query);
268
        }
269
}
270

    
271
/**
272
 * Delete a contract device
273
 *
274
 * Delete the contract device $ID
275
 *
276
 *@param $ID integer : contract device identifier.
277
 *
278
 *@return Nothing ()
279
 *
280
 **/
281
function deleteDeviceContract($ID){
282

    
283
        global $DB;
284
        $query="DELETE FROM glpi_contract_device WHERE ID= '$ID';";
285
        $result = $DB->query($query);
286
}
287

    
288
/**
289
 * Print the HTML array for contract on entreprises
290
 *
291
 * Print the HTML array for contract on entreprises for contract $instID
292
 *
293
 *@param $instID array : Contract identifier.
294
 *
295
 *@return Nothing (display)
296
 *
297
 **/
298
function showEnterpriseContract($instID) {
299
        global $DB,$CFG_GLPI, $LANG,$CFG_GLPI;
300

    
301
        if (!haveRight("contract_infocom","r")||!haveRight("contact_enterprise","r"))        return false;
302
        $contract=new Contract();
303
        $canedit=$contract->can($instID,'w');
304
        
305
        $query = "SELECT glpi_contract_enterprise.ID as ID, glpi_enterprises.ID as entID, glpi_enterprises.name as name, glpi_enterprises.website as website, "
306
                . " glpi_enterprises.phonenumber as phone, glpi_enterprises.type as type, glpi_entities.ID AS entity"
307
                . " FROM glpi_contract_enterprise, glpi_enterprises "
308
                . " LEFT JOIN glpi_entities ON (glpi_entities.ID=glpi_enterprises.FK_entities) "
309
                . " WHERE glpi_contract_enterprise.FK_contract = '$instID' AND glpi_contract_enterprise.FK_enterprise = glpi_enterprises.ID"
310
                . getEntitiesRestrictRequest(" AND","glpi_enterprises",'','',true)
311
                . " ORDER BY glpi_entities.completename,name";
312
                
313
        $result = $DB->query($query);
314
        $number = $DB->numrows($result);
315
        $i = 0;
316

    
317
        echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/contract.form.php\">";
318
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
319
        echo "<tr><th colspan='6'>".$LANG["financial"][65].":</th></tr>";
320
        echo "<tr><th>".$LANG["financial"][26]."</th>";
321
        echo "<th>".$LANG["entity"][0]."</th>";
322
        echo "<th>".$LANG["financial"][79]."</th>";
323
        echo "<th>".$LANG["help"][35]."</th>";
324
        echo "<th>".$LANG["financial"][45]."</th>";
325
        echo "<th>&nbsp;</th></tr>";
326

    
327
        $used=array();
328
        while ($i < $number) {
329
                $ID=$DB->result($result, $i, "ID");
330
                $website=$DB->result($result, $i, "glpi_enterprises.website");
331
                if (!empty($website)){
332
                        $website=$DB->result($result, $i, "website");
333
                        if (!ereg("https*://",$website)) $website="http://".$website;
334
                        $website="<a target=_blank href='$website'>".$DB->result($result, $i, "website")."</a>";
335
                }
336
                $entID=$DB->result($result, $i, "entID");
337
                $entity=$DB->result($result, $i, "entity");
338
                $used[$entID]=$entID;
339
                $entname=getDropdownName("glpi_enterprises",$entID);
340
                echo "<tr class='tab_bg_1'>";
341
                echo "<td class='center'><a href='".$CFG_GLPI["root_doc"]."/front/enterprise.form.php?ID=$entID'>".$entname;
342
                if ($CFG_GLPI["view_ID"]||empty($entname)) echo " ($entID)";
343
                echo "</a></td>";
344
                echo "<td class='center'>".getDropdownName("glpi_entities",$entity)."</td>";
345
                echo "<td class='center'>".getDropdownName("glpi_dropdown_enttype",$DB->result($result, $i, "type"))."</td>";
346
                echo "<td class='center'>".$DB->result($result, $i, "phone")."</td>";
347
                echo "<td class='center'>".$website."</td>";
348
                echo "<td align='center' class='tab_bg_2'>";
349
                if ($canedit)
350
                        echo "<a href='".$_SERVER['PHP_SELF']."?deleteenterprise=deleteenterprise&amp;ID=$ID&amp;conID=$instID'><strong>".$LANG["buttons"][6]."</strong></a>";
351
                else echo "&nbsp;";
352
                echo "</td></tr>";
353
                $i++;
354
        }
355
        if ($canedit){
356
                if ($contract->fields["recursive"]) {
357
                        $nb=countElementsInTableForEntity("glpi_enterprises",getEntitySons($contract->fields["FK_entities"]));
358
                } else {
359
                        $nb=countElementsInTableForEntity("glpi_enterprises",$contract->fields["FK_entities"]);
360
                }
361
                if ($nb>count($used)) {
362
                        echo "<tr class='tab_bg_1'><td align='right' colspan='2'>";
363
                        echo "<div class='software-instal'><input type='hidden' name='conID' value='$instID'>";
364
                        if ($contract->fields["recursive"]) {
365
                                dropdown("glpi_enterprises","entID",1,getEntitySons($contract->fields["FK_entities"]),$used);
366
                        } else {
367
                                dropdown("glpi_enterprises","entID",1,$contract->fields["FK_entities"],$used);
368
                        }
369
                        echo "</div></td><td class='center'>";
370
                        echo "<input type='submit' name='addenterprise' value=\"".$LANG["buttons"][8]."\" class='submit'>";
371
                        echo "</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
372
                        echo "</tr>";
373
                }
374
        }
375

    
376
        echo "</table></div></form>"    ;
377

    
378
}
379

    
380
/**
381
 * Link a contract to an entreprise
382
 *
383
 * Link the contract $conID to the entreprise $ID witch device type is $type. 
384
 *
385
 *@param $conID integer : contract identifier.
386
 *@param $ID integer : entreprise identifier.
387
 *
388
 *@return Nothing ()
389
 *
390
 **/
391
function addEnterpriseContract($conID,$ID){
392
        global $DB;
393
        if ($conID>0&&$ID>0){
394

    
395
                $query="INSERT INTO glpi_contract_enterprise (FK_contract,FK_enterprise ) VALUES ('$conID','$ID');";
396
                $result = $DB->query($query);
397
        }
398
}
399

    
400
/**
401
 * Delete a contract entreprise
402
 *
403
 * Delete the contract entreprise $ID
404
 *
405
 *@param $ID integer : contract entreprise identifier.
406
 *
407
 *@return Nothing ()
408
 *
409
 **/
410
function deleteEnterpriseContract($ID){
411

    
412
        global $DB;
413
        $query="DELETE FROM glpi_contract_enterprise WHERE ID= '$ID';";
414
        $result = $DB->query($query);
415
}
416

    
417
/**
418
 * Get the entreprise identifier from a contract
419
 *
420
 * Get the entreprise identifier for the contract $ID
421
 *
422
 *@param $ID integer : Contract entreprise identifier
423
 *
424
 *@return integer enterprise identifier
425
 *
426
 **/
427
function getContractEnterprises($ID){
428
        global $DB;
429

    
430
        $query = "SELECT glpi_enterprises.* FROM glpi_contract_enterprise, glpi_enterprises WHERE glpi_contract_enterprise.FK_enterprise = glpi_enterprises.ID AND glpi_contract_enterprise.FK_contract = '$ID'";
431
        $result = $DB->query($query);
432
        $out="";
433
        while ($data=$DB->fetch_array($result)){
434
                $out.= getDropdownName("glpi_enterprises",$data['ID'])."<br>";
435
        }
436
        return $out;
437
}
438

    
439
/**
440
 * Print a select with contracts
441
 *
442
 * Print a select named $name with contracts options and selected value $value
443
 *
444
 * @param $name string : HTML select name
445
 * @param $entity_restrict Restrict to a defined entity
446
 * @param $alreadyused : already used contract, do not add to dropdown
447
 *
448
 *@return Nothing (display)
449
 *
450
 **/
451
function dropdownContracts($name,$entity_restrict=-1,$alreadyused=array()){
452

    
453
        global $DB;
454

    
455
        $entrest="";
456
        $idrest="";
457
        if ($entity_restrict>=0){
458
                $entrest=getEntitiesRestrictRequest("AND","glpi_contracts","FK_entities",$entity_restrict,true);
459
        }
460
        if (count($alreadyused)) {
461
                foreach ($alreadyused AS $ID) {
462
                        $idrest .= (empty($idrest) ? "AND ID NOT IN(" : ",") . "'".$ID."'";
463
                }
464
                $idrest .= ")";
465
        }
466
        $query = "SELECT * FROM glpi_contracts WHERE deleted = '0' $entrest $idrest ORDER BY FK_entities, begin_date DESC";
467
        $result=$DB->query($query);
468
        echo "<select name='$name'>";
469
        echo "<option value='-1'>-----</option>";
470
        $prev=-1;
471
        while ($data=$DB->fetch_array($result)){
472

    
473
                if ($data["device_countmax"]==0||$data["device_countmax"]>countElementsInTable("glpi_contract_device","FK_contract = '".$data['ID']."'" )){
474
                        if ($data["FK_entities"]!=$prev) {
475
                                if ($prev>=0) {
476
                                        echo "</optgroup>";
477
                                }
478
                                $prev=$data["FK_entities"];
479
                                echo "<optgroup label=\"". getDropdownName("glpi_entities", $prev) ."\">";
480
                        }
481
                        echo "<option value='".$data["ID"]."'>";
482
                        echo "#".$data["num"]." - ".convDateTime($data["begin_date"])." - ".$data["name"];
483
                        echo "</option>";
484
                }
485
        }
486
        if ($prev>=0) {
487
                echo "</optgroup>";
488
        }                
489

    
490
        echo "</select>";        
491

    
492

    
493

    
494
}
495

    
496
/**
497
 * Print an HTML array with contracts associated to a device
498
 *
499
 * Print an HTML array with contracts associated to the device identified by $ID from device type $device_type 
500
 *
501
 *@param $device_type string : HTML select name
502
 *@param $ID integer device ID
503
 *@param $withtemplate='' not used (to be deleted)
504
 *
505
 *@return Nothing (display)
506
 *
507
 **/
508
function showContractAssociated($device_type,$ID,$withtemplate=''){
509
        global $DB,$CFG_GLPI, $LANG;
510

    
511
        if (!haveRight("contract_infocom","r")||!haveTypeRight($device_type,"r"))        return false;
512
        $canedit=haveRight("contract_infocom","w");
513

    
514
        $ci=new CommonItem();
515
        $ci->getFromDB($device_type,$ID);
516

    
517
        $query = "SELECT glpi_contract_device.* "
518
                ." FROM glpi_contract_device, glpi_contracts LEFT JOIN glpi_entities ON (glpi_contracts.FK_entities=glpi_entities.ID)"
519
                ." WHERE glpi_contracts.ID=glpi_contract_device.FK_contract AND glpi_contract_device.FK_device = '$ID' AND glpi_contract_device.device_type = '$device_type' "
520
                .getEntitiesRestrictRequest(" AND","glpi_contracts",'','',true)." ORDER BY glpi_contracts.name";
521

    
522
        $result = $DB->query($query);
523
        $number = $DB->numrows($result);
524
        $i = 0;
525

    
526
        if ($withtemplate!=2) echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/contract.form.php\">";
527
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
528
        echo "<tr><th colspan='8'>".$LANG["financial"][66].":</th></tr>";
529
        echo "<tr><th>".$LANG["common"][16]."</th>";
530
        echo "<th>".$LANG["entity"][0]."</th>";
531
        echo "<th>".$LANG["financial"][4]."</th>";
532
        echo "<th>".$LANG["financial"][6]."</th>";
533
        echo "<th>".$LANG["financial"][26]."</th>";
534
        echo "<th>".$LANG["search"][8]."</th>";        
535
        echo "<th>".$LANG["financial"][8]."</th>";        
536
        if ($withtemplate!=2)echo "<th>&nbsp;</th>";
537
        echo "</tr>";
538

    
539
        $contracts=array();
540
        while ($i < $number) {
541
                $cID=$DB->result($result, $i, "FK_contract");
542
                $contracts[]=$cID;
543
                $assocID=$DB->result($result, $i, "ID");
544
                $con=new Contract;
545
                $con->getFromDB($cID);
546
                echo "<tr class='tab_bg_1".($con->fields["deleted"]?"_2":"")."'>";
547
                echo "<td class='center'><a href='".$CFG_GLPI["root_doc"]."/front/contract.form.php?ID=$cID'><strong>".$con->fields["name"];
548
                if ($CFG_GLPI["view_ID"]||empty($con->fields["name"])) echo " (".$con->fields["ID"].")";
549
                echo "</strong></a></td>";
550
                echo "<td class='center'>".getDropdownName("glpi_entities",$con->fields["FK_entities"])."</td>";
551
                echo "<td class='center'>".$con->fields["num"]."</td>";
552
                echo "<td class='center'>".getDropdownName("glpi_dropdown_contract_type",$con->fields["contract_type"])."</td>";
553
                echo "<td class='center'>".getContractEnterprises($cID)."</td>";        
554
                echo "<td class='center'>".convDate($con->fields["begin_date"])."</td>";
555
                echo "<td class='center'>".$con->fields["duration"]." ".$LANG["financial"][57];
556
                if ($con->fields["begin_date"]!=''&&$con->fields["begin_date"]!="0000-00-00") echo " -> ".getWarrantyExpir($con->fields["begin_date"],$con->fields["duration"]);
557
                echo "</td>";
558

    
559
                if ($withtemplate!=2) {
560
                        echo "<td align='center' class='tab_bg_2'>";
561
                        if ($canedit) {
562
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/contract.form.php?deleteitem=deleteitem&amp;ID=$assocID&amp;conID=$cID'><strong>".$LANG["buttons"][6]."</strong></a>";
563
                        } else {
564
                                echo "&nbsp;";
565
                        }
566
                        echo "</td>";
567
                }
568
                echo "</tr>";
569
                $i++;
570
        }
571
        $q="SELECT * FROM glpi_contracts WHERE deleted='0' "
572
                .getEntitiesRestrictRequest("AND","glpi_contracts","FK_entities",$ci->obj->fields["FK_entities"],true);;
573
        $result = $DB->query($q);
574
        $nb = $DB->numrows($result);
575

    
576
        if ($canedit){
577
                if ($withtemplate!=2 && $nb>count($contracts)){
578
                        echo "<tr class='tab_bg_1'><td align='right' colspan='3'>";
579
                        echo "<div class='software-instal'><input type='hidden' name='item' value='$ID'><input type='hidden' name='type' value='$device_type'>";
580
                        dropdownContracts("conID",$ci->obj->fields["FK_entities"],$contracts);
581
                        echo "</div></td><td class='center'>";
582
                        echo "<input type='submit' name='additem' value=\"".$LANG["buttons"][8]."\" class='submit'>";
583
                        echo "</td>";
584

    
585
                        echo "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
586
                }
587
        }
588
        echo "</table></div>";
589

    
590
        if ($withtemplate!=2) echo "</form>";
591
}
592

    
593

    
594
/**
595
 * Print an HTML array with contracts associated to a enterprise
596
 *
597
 * Print an HTML array with contracts associated to the enterprise identified by $ID 
598
 *
599
 *@param $ID integer device ID
600
 *
601
 *@return Nothing (display)
602
 *
603
 **/
604
function showContractAssociatedEnterprise($ID){
605

    
606
        global $DB,$CFG_GLPI, $LANG,$CFG_GLPI;
607
        if (!haveRight("contract_infocom","r")||!haveRight("contact_enterprise","r")) return false;
608
        $ent=new Enterprise();
609
        $canedit=$ent->can($ID,'w');
610

    
611
        $query = "SELECT glpi_contracts.*, glpi_contract_enterprise.ID AS assocID, glpi_entities.ID AS entity"
612
                . " FROM glpi_contract_enterprise, glpi_contracts "
613
                . " LEFT JOIN glpi_entities ON (glpi_entities.ID=glpi_contracts.FK_entities) "        
614
                . " WHERE glpi_contract_enterprise.FK_enterprise = '$ID' AND glpi_contract_enterprise.FK_contract=glpi_contracts.ID"
615
                . getEntitiesRestrictRequest(" AND","glpi_contracts",'','',true) 
616
                . " ORDER BY glpi_entities.completename, glpi_contracts.name";
617

    
618
        $result = $DB->query($query);
619
        $number = $DB->numrows($result);
620
        $i = 0;
621

    
622
        echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/enterprise.form.php\">";
623
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
624
        echo "<tr><th colspan='7'>".$LANG["financial"][66].":</th></tr>";
625
        echo "<tr><th>".$LANG["common"][16]."</th>";
626
        echo "<th>".$LANG["entity"][0]."</th>";
627
        echo "<th>".$LANG["financial"][4]."</th>";
628
        echo "<th>".$LANG["financial"][6]."</th>";
629
        //echo "<th>".$LANG["financial"][26]."</th>";
630
        echo "<th>".$LANG["search"][8]."</th>";        
631
        echo "<th>".$LANG["financial"][8]."</th>";        
632
        echo "<th>&nbsp;</th>";
633
        echo "</tr>";
634

    
635
        $used=array();
636
        while ($data=$DB->fetch_array($result)) {
637
                $cID=$data["ID"];
638
                $used[$cID]=$cID;
639
                $assocID=$data["assocID"];;
640
                echo "<tr class='tab_bg_1".($data["deleted"]?"_2":"")."'>";
641
                echo "<td class='center'><a href='".$CFG_GLPI["root_doc"]."/front/contract.form.php?ID=$cID'><strong>".$data["name"];
642
                if ($CFG_GLPI["view_ID"]||empty($data["name"])) echo " (".$data["ID"].")";
643
                echo "</strong></a></td>";
644
                echo "<td class='center'>".getDropdownName("glpi_entities",$data["entity"])."</td>";
645
                echo "<td class='center'>".$data["num"]."</td>";
646
                echo "<td class='center'>".getDropdownName("glpi_dropdown_contract_type",$data["contract_type"])."</td>";
647
                //echo "<td class='center'>".getContractEnterprises($cID)."</td>";        
648
                echo "<td class='center'>".convDate($data["begin_date"])."</td>";
649
                echo "<td class='center'>".$data["duration"]." ".$LANG["financial"][57];
650
                if ($data["begin_date"]!=''&&$data["begin_date"]!="0000-00-00") echo " -> ".getWarrantyExpir($data["begin_date"],$data["duration"]);
651
                echo "</td>";
652

    
653
                echo "<td align='center' class='tab_bg_2'>";
654
                if ($canedit) 
655
                        echo "<a href='".$CFG_GLPI["root_doc"]."/front/enterprise.form.php?deletecontract=deletecontract&amp;ID=$assocID&amp;entID=$ID'><strong>".$LANG["buttons"][6]."</strong></a>";
656
                else echo "&nbsp;";
657
                echo "</td></tr>";
658
                $i++;
659
        }
660
        if ($canedit){
661
                if ($ent->fields["recursive"]) {
662
                        $nb=countElementsInTableForEntity("glpi_contracts",getEntitySons($ent->fields["FK_entities"]));
663
                } else {
664
                        $nb=countElementsInTableForEntity("glpi_contracts",$ent->fields["FK_entities"]);
665
                }
666

    
667
                if ($nb>count($used)){
668
                        echo "<tr class='tab_bg_1'><td class='center' colspan='5'>";
669
                        echo "<div class='software-instal'><input type='hidden' name='entID' value='$ID'>";
670
                        if ($ent->fields["recursive"]) {
671
                                dropdownContracts("conID",getEntitySons($ent->fields["FK_entities"]),$used);
672
                        } else {
673
                                dropdownContracts("conID",$ent->fields['FK_entities'],$used);
674
                        }
675
                        echo "</div></td><td class='center'>";
676
                        echo "<input type='submit' name='addcontract' value=\"".$LANG["buttons"][8]."\" class='submit'>";
677
                        echo "</td>";
678

    
679
                        echo "<td>&nbsp;</td></tr>";
680
                }
681
        }
682
        echo "</table></div>"    ;
683
        echo "</form>";
684

    
685
}
686
/**
687
 * Cron action on contracts : alert depending of the config : on notice and expire
688
 *
689
 **/
690
function cron_contract($display=false){
691
        global $DB,$CFG_GLPI,$LANG;
692

    
693
        if (!$CFG_GLPI["mailing"]){
694
                return false;
695
        }
696

    
697
        loadLanguage($CFG_GLPI["default_language"]);
698

    
699
        $message=array();
700
        $items_notice=array();
701
        $items_end=array();
702

    
703
        // Check notice
704
        $query="SELECT glpi_contracts.* 
705
                FROM glpi_contracts 
706
                LEFT JOIN glpi_alerts ON (glpi_contracts.ID = glpi_alerts.FK_device 
707
                                        AND glpi_alerts.device_type='".CONTRACT_TYPE."' 
708
                                        AND glpi_alerts.type='".ALERT_NOTICE."') 
709
                WHERE (glpi_contracts.alert & ".pow(2,ALERT_NOTICE).") >0 
710
                        AND glpi_contracts.deleted='0' 
711
                        AND glpi_contracts.begin_date IS NOT NULL 
712
                        AND glpi_contracts.duration <> '0' 
713
                        AND glpi_contracts.notice<>'0' 
714
                        AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH),CURDATE() )>0 
715
                        AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL (glpi_contracts.duration-glpi_contracts.notice) MONTH),CURDATE() )<0 
716
                        AND glpi_alerts.date IS NULL;";
717
        
718
        $result=$DB->query($query);
719
        if ($DB->numrows($result)>0){
720
                while ($data=$DB->fetch_array($result)){
721
                        if (!isset($message[$data["FK_entities"]])){
722
                                $message[$data["FK_entities"]]="";
723
                        }
724
                        if (!isset($items_notice[$data["FK_entities"]])){
725
                                $items_notice[$data["FK_entities"]]=array();
726
                        }
727
                        // define message alert
728
                        $message[$data["FK_entities"]].=$LANG["mailing"][37]." ".$data["name"].": ".getWarrantyExpir($data["begin_date"],$data["duration"],$data["notice"])."<br>\n";
729
                        $items_notice[$data["FK_entities"]][]=$data["ID"];
730
                }
731
        }
732

    
733
        // Check end
734
        $query="SELECT glpi_contracts.* FROM glpi_contracts LEFT JOIN glpi_alerts ON (glpi_contracts.ID = glpi_alerts.FK_device AND glpi_alerts.device_type='".CONTRACT_TYPE."' AND glpi_alerts.type='".ALERT_END."') WHERE (glpi_contracts.alert & ".pow(2,ALERT_END).") >0 AND glpi_contracts.deleted='0' AND glpi_contracts.begin_date IS NOT NULL AND glpi_contracts.duration <> '0' AND DATEDIFF( ADDDATE(glpi_contracts.begin_date, INTERVAL (glpi_contracts.duration) MONTH),CURDATE() )<0 AND glpi_alerts.date IS NULL;";
735

    
736
        $result=$DB->query($query);
737
        if ($DB->numrows($result)>0){
738
                while ($data=$DB->fetch_array($result)){
739
                        if (!isset($message[$data["FK_entities"]])){
740
                                $message[$data["FK_entities"]]="";
741
                        }
742
                        if (!isset($items_end[$data["FK_entities"]])){
743
                                $items_end[$data["FK_entities"]]=array();
744
                        }
745

    
746
                        // define message alert
747
                        $message[$data["FK_entities"]].=$LANG["mailing"][38]." ".$data["name"].": ".getWarrantyExpir($data["begin_date"],$data["duration"])."<br>\n";
748
                        $items_end[$data["FK_entities"]][]=$data["ID"];
749
                }
750

    
751

    
752
        }
753

    
754
        if (count($message)>0){
755
                foreach ($message as $entity => $msg){
756
                        $mail=new MailingAlert("alertcontract",$msg,$entity);
757
                        if ($mail->send()){
758
                                if ($display){
759
                                        addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  $msg");
760
                                }
761
                                logInFile("cron",getDropdownName("glpi_entities",$entity).":  $msg\n");
762
                
763
                                // Mark alert as done
764
                                $alert=new Alert();
765
                                $input["device_type"]=CONTRACT_TYPE;
766

    
767
                                $input["type"]=ALERT_NOTICE;
768
                                if (isset($items_notice[$entity])){
769
                                        foreach ($items_notice[$entity] as $ID){
770
                                                $input["FK_device"]=$ID;
771
                                                $alert->add($input);
772
                                                unset($alert->fields['ID']);
773
                                        }
774
                                }
775
                                $input["type"]=ALERT_END;
776
                                if (isset($items_end[$entity])){
777
                                        foreach ($items_end[$entity] as $ID){
778
                                                $input["FK_device"]=$ID;
779
                                                $alert->add($input);
780
                                                unset($alert->fields['ID']);
781
                                        }
782
                                }
783
                        } else {
784
                                if ($display){
785
                                        addMessageAfterRedirect(getDropdownName("glpi_entities",$entity).":  Send contract alert failed");
786
                                }
787
                                logInFile("cron",getDropdownName("glpi_entities",$entity).":  Send contract alert failed\n");
788
                        }
789
                }
790
                return 1;
791
        }
792
        return 0;
793
}
794
?>
Redmine Appliance - Powered by TurnKey Linux