dolibarr  7.0.0-beta
contract.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
3  * Copyright (C) 2011-2015 Philippe Grand <philippe.grand@atoo-net.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
25 require '../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/contract.lib.php';
30 
31 $langs->load("admin");
32 $langs->load("errors");
33 $langs->load("contracts");
34 
35 if (!$user->admin) accessforbidden();
36 
37 $action = GETPOST('action','alpha');
38 $value = GETPOST('value','alpha');
39 $label = GETPOST('label','alpha');
40 $scandir = GETPOST('scan_dir','alpha');
41 $type='contract';
42 
43 if (empty($conf->global->CONTRACT_ADDON))
44 {
45  $conf->global->CONTRACT_ADDON='mod_contract_serpis';
46 }
47 
48 
49 /*
50  * Actions
51  */
52 
53 include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
54 
55 if ($action == 'updateMask')
56 {
57  $maskconst = GETPOST('maskconstcontract','alpha');
58  $maskvalue = GETPOST('maskcontract','alpha');
59  if ($maskconst) $res = dolibarr_set_const($db,$maskconst,$maskvalue,'chaine',0,'',$conf->entity);
60 
61  if (! $res > 0) $error++;
62 
63  if (! $error)
64  {
65  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
66  }
67  else
68  {
69  setEventMessages($langs->trans("Error"), null, 'errors');
70  }
71 }
72 
73 else if ($action == 'specimen') // For contract
74 {
75  $modele= GETPOST('module','alpha');
76 
77  $contract = new Contrat($db);
78  $contract->initAsSpecimen();
79 
80  // Search template files
81  $file=''; $classname=''; $filefound=0;
82  $dirmodels=array_merge(array('/'),(array) $conf->modules_parts['models']);
83  foreach($dirmodels as $reldir)
84  {
85  $file=dol_buildpath($reldir."core/modules/contract/doc/pdf_".$modele.".modules.php",0);
86  if (file_exists($file))
87  {
88  $filefound=1;
89  $classname = "pdf_".$modele;
90  break;
91  }
92  }
93 
94  if ($filefound)
95  {
96  require_once $file;
97 
98  $module = new $classname($db);
99 
100  if ($module->write_file($contract,$langs) > 0)
101  {
102  header("Location: ".DOL_URL_ROOT."/document.php?modulepart=contract&file=SPECIMEN.pdf");
103  return;
104  }
105  else
106  {
107  setEventMessages($obj->error, $obj->errors, 'errors');
108  dol_syslog($obj->error, LOG_ERR);
109  }
110  }
111  else
112  {
113  setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
114  dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
115  }
116 }
117 
118 // Activate a model
119 else if ($action == 'set')
120 {
121  $ret = addDocumentModel($value, $type, $label, $scandir);
122 }
123 
124 else if ($action == 'del')
125 {
126  $ret = delDocumentModel($value, $type);
127  if ($ret > 0)
128  {
129  if ($conf->global->CONTRACT_ADDON_PDF == "$value") dolibarr_del_const($db, 'CONTRACT_ADDON_PDF',$conf->entity);
130  }
131 }
132 
133 // Set default model
134 else if ($action == 'setdoc')
135 {
136  if (dolibarr_set_const($db, "CONTRACT_ADDON_PDF",$value,'chaine',0,'',$conf->entity))
137  {
138  // La constante qui a ete lue en avant du nouveau set
139  // on passe donc par une variable pour avoir un affichage coherent
140  $conf->global->CONTRACT_ADDON_PDF = $value;
141  }
142 
143  // On active le modele
144  $ret = delDocumentModel($value, $type);
145  if ($ret > 0)
146  {
147  $ret = addDocumentModel($value, $type, $label, $scandir);
148  }
149 }
150 
151 else if ($action == 'setmod')
152 {
153  // TODO Verifier si module numerotation choisi peut etre active
154  // par appel methode canBeActivated
155 
156  dolibarr_set_const($db, "CONTRACT_ADDON",$value,'chaine',0,'',$conf->entity);
157 }
158 
159 else if ($action == 'set_other')
160 {
161  $freetext= GETPOST('CONTRACT_FREE_TEXT','none'); // No alpha here, we want exact string
162  $res1 = dolibarr_set_const($db, "CONTRACT_FREE_TEXT",$freetext,'chaine',0,'',$conf->entity);
163 
164  $draft= GETPOST('CONTRACT_DRAFT_WATERMARK','alpha');
165  $res2 = dolibarr_set_const($db, "CONTRACT_DRAFT_WATERMARK",trim($draft),'chaine',0,'',$conf->entity);
166 
167  if (! $res1 > 0 || ! $res2 > 0) $error++;
168 
169  if (! $error)
170  {
171  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
172  }
173  else
174  {
175  setEventMessages($langs->trans("Error"), null, 'errors');
176  }
177 }
178 
179 
180 /*
181  * View
182  */
183 
184 $dirmodels=array_merge(array('/'),(array) $conf->modules_parts['models']);
185 
186 llxHeader();
187 
188 $form=new Form($db);
189 
190 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
191 print load_fiche_titre($langs->trans("ContractsSetup"),$linkback,'title_setup');
192 
194 
195 dol_fiche_head($head, 'contract', $langs->trans("Contracts"), -1, 'contract');
196 
197 /*
198  * Contracts Numbering model
199  */
200 
201 print load_fiche_titre($langs->trans("ContractsNumberingModules"),'','');
202 
203 print '<table class="noborder" width="100%">';
204 print '<tr class="liste_titre">';
205 print '<td width="100">'.$langs->trans("Name").'</td>';
206 print '<td>'.$langs->trans("Description").'</td>';
207 print '<td>'.$langs->trans("Example").'</td>';
208 print '<td align="center" width="60">'.$langs->trans("Status").'</td>';
209 print '<td align="center" width="16">'.$langs->trans("ShortInfo").'</td>';
210 print "</tr>\n";
211 
212 clearstatcache();
213 
214 foreach ($dirmodels as $reldir)
215 {
216  $dir = dol_buildpath($reldir."core/modules/contract/");
217 
218  if (is_dir($dir))
219  {
220  $handle = opendir($dir);
221  if (is_resource($handle))
222  {
223  $var=true;
224 
225  while (($file = readdir($handle))!==false)
226  {
227  if (substr($file, 0, 13) == 'mod_contract_' && substr($file, dol_strlen($file)-3, 3) == 'php')
228  {
229  $file = substr($file, 0, dol_strlen($file)-4);
230 
231  require_once $dir.$file.'.php';
232 
233  $module = new $file($db);
234 
235  // Show modules according to features level
236  if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
237  if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
238 
239  if ($module->isEnabled())
240  {
241 
242  print '<tr class="oddeven"><td>'.$module->nom."</td><td>\n";
243  print $module->info();
244  print '</td>';
245 
246  // Show example of numbering model
247  print '<td class="nowrap">';
248  $tmp=$module->getExample();
249  if (preg_match('/^Error/',$tmp)) { $langs->load("errors"); print '<div class="error">'.$langs->trans($tmp).'</div>'; }
250  elseif ($tmp=='NotConfigured') print $langs->trans($tmp);
251  else print $tmp;
252  print '</td>'."\n";
253 
254  print '<td align="center">';
255  if ($conf->global->CONTRACT_ADDON == "$file")
256  {
257  print img_picto($langs->trans("Activated"),'switch_on');
258  }
259  else
260  {
261  print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&amp;value='.$file.'">';
262  print img_picto($langs->trans("Disabled"),'switch_off');
263  print '</a>';
264  }
265  print '</td>';
266 
267  $contract=new Contrat($db);
268  $contract->initAsSpecimen();
269 
270  // Info
271  $htmltooltip='';
272  $htmltooltip.=''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
273  $nextval=$module->getNextValue($mysoc,$contract);
274  if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
275  $htmltooltip.=''.$langs->trans("NextValue").': ';
276  if ($nextval) {
277  if (preg_match('/^Error/',$nextval) || $nextval=='NotConfigured')
278  $nextval = $langs->trans($nextval);
279  $htmltooltip.=$nextval.'<br>';
280  } else {
281  $htmltooltip.=$langs->trans($module->error).'<br>';
282  }
283  }
284 
285  print '<td align="center">';
286  print $form->textwithpicto('',$htmltooltip,1,0);
287  print '</td>';
288 
289  print '</tr>';
290  }
291  }
292  }
293  closedir($handle);
294  }
295  }
296 }
297 
298 print '</table><br>';
299 
300 /*
301  * Documents models for Contracts
302  */
303 
304 print load_fiche_titre($langs->trans("TemplatePDFContracts"),'','');
305 
306 // Defini tableau def des modeles
307 $def = array();
308 $sql = "SELECT nom";
309 $sql.= " FROM ".MAIN_DB_PREFIX."document_model";
310 $sql.= " WHERE type = '".$type."'";
311 $sql.= " AND entity = ".$conf->entity;
312 $resql=$db->query($sql);
313 if ($resql)
314 {
315  $i = 0;
316  $num_rows=$db->num_rows($resql);
317  while ($i < $num_rows)
318  {
319  $array = $db->fetch_array($resql);
320  array_push($def, $array[0]);
321  $i++;
322  }
323 }
324 else
325 {
326  dol_print_error($db);
327 }
328 
329 
330 print '<table class="noborder" width="100%">';
331 print '<tr class="liste_titre">';
332 print '<td>'.$langs->trans("Name").'</td>';
333 print '<td>'.$langs->trans("Description").'</td>';
334 print '<td align="center" width="60">'.$langs->trans("Status")."</td>\n";
335 print '<td align="center" width="60">'.$langs->trans("Default")."</td>\n";
336 print '<td align="center" width="80">'.$langs->trans("ShortInfo").'</td>';
337 print '<td align="center" width="80">'.$langs->trans("Preview").'</td>';
338 print "</tr>\n";
339 
340 clearstatcache();
341 
342 $var=true;
343 foreach ($dirmodels as $reldir)
344 {
345  foreach (array('','/doc') as $valdir)
346  {
347  $dir = dol_buildpath($reldir."core/modules/contract".$valdir);
348 
349  if (is_dir($dir))
350  {
351  $handle=opendir($dir);
352  if (is_resource($handle))
353  {
354  while (($file = readdir($handle))!==false)
355  {
356  $filelist[]=$file;
357  }
358  closedir($handle);
359  arsort($filelist);
360 
361  foreach($filelist as $file)
362  {
363  if (preg_match('/\.modules\.php$/i',$file) && preg_match('/^(pdf_|doc_)/',$file))
364  {
365 
366  if (file_exists($dir.'/'.$file))
367  {
368  $name = substr($file, 4, dol_strlen($file) -16);
369  $classname = substr($file, 0, dol_strlen($file) -12);
370 
371  require_once $dir.'/'.$file;
372  $module = new $classname($db);
373 
374  $modulequalified=1;
375  if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified=0;
376  if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified=0;
377 
378  if ($modulequalified)
379  {
380  $var = !$var;
381  print '<tr class="oddeven"><td width="100">';
382  print (empty($module->name)?$name:$module->name);
383  print "</td><td>\n";
384  if (method_exists($module,'info')) print $module->info($langs);
385  else print $module->description;
386  print '</td>';
387 
388  // Active
389  if (in_array($name, $def))
390  {
391  print '<td align="center">'."\n";
392  print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&value='.$name.'">';
393  print img_picto($langs->trans("Enabled"),'switch_on');
394  print '</a>';
395  print '</td>';
396  }
397  else
398  {
399  print '<td align="center">'."\n";
400  print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"),'switch_off').'</a>';
401  print "</td>";
402  }
403 
404  // Defaut
405  print '<td align="center">';
406  if ($conf->global->CONTRACT_ADDON_PDF == $name)
407  {
408  print img_picto($langs->trans("Default"),'on');
409  }
410  else
411  {
412  print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"),'off').'</a>';
413  }
414  print '</td>';
415 
416  // Info
417  $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
418  $htmltooltip.='<br>'.$langs->trans("Type").': '.($module->type?$module->type:$langs->trans("Unknown"));
419  if ($module->type == 'pdf')
420  {
421  $htmltooltip.='<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
422  }
423  $htmltooltip.='<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
424  $htmltooltip.='<br>'.$langs->trans("Logo").': '.yn($module->option_logo,1,1);
425  $htmltooltip.='<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg,1,1);
426  $htmltooltip.='<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg,1,1);
427  $htmltooltip.='<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang,1,1);
428  $htmltooltip.='<br>'.$langs->trans("WatermarkOnDraftOrders").': '.yn($module->option_draft_watermark,1,1);
429 
430 
431  print '<td align="center">';
432  print $form->textwithpicto('',$htmltooltip,1,0);
433  print '</td>';
434 
435  // Preview
436  print '<td align="center">';
437  if ($module->type == 'pdf')
438  {
439  print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"),'contract').'</a>';
440  }
441  else
442  {
443  print img_object($langs->trans("PreviewNotAvailable"),'generic');
444  }
445  print '</td>';
446 
447  print "</tr>\n";
448  }
449  }
450  }
451  }
452  }
453  }
454  }
455 }
456 
457 print '</table>';
458 print "<br>";
459 
460 /*
461  * Other options
462  *
463  */
464 
465 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
466 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
467 print '<input type="hidden" name="action" value="set_other">';
468 
469 print load_fiche_titre($langs->trans("OtherOptions"),'','');
470 print '<table class="noborder" width="100%">';
471 print '<tr class="liste_titre">';
472 print '<td>'.$langs->trans("Parameter").'</td>';
473 print '<td align="center" width="60">'.$langs->trans("Value").'</td>';
474 print "</tr>\n";
475 $var=true;
476 
477 $substitutionarray=pdf_getSubstitutionArray($langs, array('objectamount'), null, 2);
478 $substitutionarray['__(AnyTranslationKey)__']=$langs->trans("Translation");
479 $htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
480 foreach($substitutionarray as $key => $val) $htmltext.=$key.'<br>';
481 $htmltext.='</i>';
482 
483 $var=! $var;
484 print '<tr class="oddeven"><td colspan="2">';
485 print $form->textwithpicto($langs->trans("FreeLegalTextOnContracts"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'tooltiphelp');
486 print '<br>';
487 $variablename='CONTRACT_FREE_TEXT';
488 if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT))
489 {
490  print '<textarea name="'.$variablename.'" class="flat" cols="120">'.$conf->global->$variablename.'</textarea>';
491 }
492 else
493 {
494  include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
495  $doleditor=new DolEditor($variablename, $conf->global->$variablename,'',80,'dolibarr_notes');
496  print $doleditor->Create();
497 }
498 print '</td></tr>'."\n";
499 
500 //Use draft Watermark
501 
502 print '<tr class="oddeven"><td>';
503 print $form->textwithpicto($langs->trans("WatermarkOnDraftContractCards"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
504 print '</td><td>';
505 print '<input size="50" class="flat" type="text" name="CONTRACT_DRAFT_WATERMARK" value="'.$conf->global->CONTRACT_DRAFT_WATERMARK.'">';
506 print '</td></tr>'."\n";
507 
508 print '</table>';
509 
510 print '<div class="center">';
511 print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
512 print '</div>';
513 
514 print '</form>';
515 
516 dol_fiche_end();
517 
518 
519 llxFooter();
520 
521 $db->close();
delDocumentModel($name, $type)
Delete document model used by doc generator.
Definition: admin.lib.php:1540
llxFooter()
Empty footer.
Definition: wrapper.php:58
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(!GETPOST('transkey')&&!GETPOST('transphrase')) else
View.
Definition: notice.php:43
contract_admin_prepare_head()
Return array head with list of tabs to view object informations.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
Class to manage contracts.
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Definition: admin.lib.php:485
addDocumentModel($name, $type, $label='', $description='')
Add document model used by doc generator.
Definition: admin.lib.php:1506
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dolibarr_del_const($db, $name, $entity=1)
Effacement d'une constante dans la base de donnees.
Definition: admin.lib.php:410
dol_fiche_end($notab=0)
Show tab footer of a card.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
pdf_getSubstitutionArray($outputlangs, $exclude=null, $object=null, $onlykey=0)
Return array of possible substitutions for PDF content (without external module substitutions).
Definition: pdf.lib.php:603
print
Draft customers invoices.
Definition: index.php:91
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
Class to manage a WYSIWYG editor.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.