dolibarr  13.0.2
bom.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
24 require '../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
29 
30 // Load translation files required by the page
31 $langs->loadLangs(array('admin', 'errors', 'mrp', 'other'));
32 
33 if (!$user->admin) accessforbidden();
34 
35 $action = GETPOST('action', 'aZ09');
36 $value = GETPOST('value', 'alpha');
37 $label = GETPOST('label', 'alpha');
38 $scandir = GETPOST('scan_dir', 'alpha');
39 $type = 'bom';
40 
41 
42 /*
43  * Actions
44  */
45 
46 include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
47 
48 if ($action == 'updateMask')
49 {
50  $maskconstbom = GETPOST('maskconstBom', 'alpha');
51  $maskbom = GETPOST('maskBom', 'alpha');
52 
53  if ($maskconstbom) $res = dolibarr_set_const($db, $maskconstbom, $maskbom, 'chaine', 0, '', $conf->entity);
54 
55  if (!($res > 0)) $error++;
56 
57  if (!$error)
58  {
59  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
60  } else {
61  setEventMessages($langs->trans("Error"), null, 'errors');
62  }
63 } elseif ($action == 'specimen')
64 {
65  $modele = GETPOST('module', 'alpha');
66 
67  $bom = new BOM($db);
68  $bom->initAsSpecimen();
69 
70  // Search template files
71  $file = ''; $classname = ''; $filefound = 0;
72  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
73  foreach ($dirmodels as $reldir)
74  {
75  $file = dol_buildpath($reldir."core/modules/bom/doc/pdf_".$modele.".modules.php", 0);
76  if (file_exists($file))
77  {
78  $filefound = 1;
79  $classname = "pdf_".$modele;
80  break;
81  }
82  }
83 
84  if ($filefound)
85  {
86  require_once $file;
87 
88  $module = new $classname($db);
89 
90  if ($module->write_file($bom, $langs) > 0)
91  {
92  header("Location: ".DOL_URL_ROOT."/document.php?modulepart=bom&file=SPECIMEN.pdf");
93  return;
94  } else {
95  setEventMessages($module->error, null, 'errors');
96  dol_syslog($module->error, LOG_ERR);
97  }
98  } else {
99  setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
100  dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
101  }
102 }
103 
104 // Activate a model
105 elseif ($action == 'set')
106 {
107  $ret = addDocumentModel($value, $type, $label, $scandir);
108 } elseif ($action == 'del')
109 {
110  $ret = delDocumentModel($value, $type);
111  if ($ret > 0)
112  {
113  if ($conf->global->BOM_ADDON_PDF == "$value") dolibarr_del_const($db, 'BOM_ADDON_PDF', $conf->entity);
114  }
115 }
116 
117 // Set default model
118 elseif ($action == 'setdoc')
119 {
120  if (dolibarr_set_const($db, "BOM_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity))
121  {
122  // The constant that was read before the new set
123  // We therefore requires a variable to have a coherent view
124  $conf->global->BOM_ADDON_PDF = $value;
125  }
126 
127  // On active le modele
128  $ret = delDocumentModel($value, $type);
129  if ($ret > 0)
130  {
131  $ret = addDocumentModel($value, $type, $label, $scandir);
132  }
133 } elseif ($action == 'setmod')
134 {
135  // TODO Check if numbering module chosen can be activated
136  // by calling method canBeActivated
137 
138  dolibarr_set_const($db, "BOM_ADDON", $value, 'chaine', 0, '', $conf->entity);
139 } elseif ($action == 'set_BOM_DRAFT_WATERMARK')
140 {
141  $draft = GETPOST("BOM_DRAFT_WATERMARK");
142  $res = dolibarr_set_const($db, "BOM_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
143 
144  if (!($res > 0)) $error++;
145 
146  if (!$error)
147  {
148  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
149  } else {
150  setEventMessages($langs->trans("Error"), null, 'errors');
151  }
152 } elseif ($action == 'set_BOM_FREE_TEXT')
153 {
154  $freetext = GETPOST("BOM_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
155 
156  $res = dolibarr_set_const($db, "BOM_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
157 
158  if (!($res > 0)) $error++;
159 
160  if (!$error)
161  {
162  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
163  } else {
164  setEventMessages($langs->trans("Error"), null, 'errors');
165  }
166 }
167 
168 
169 /*
170  * View
171  */
172 
173 $form = new Form($db);
174 
175 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
176 
177 llxHeader("", $langs->trans("BOMsSetup"));
178 
179 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
180 print load_fiche_titre($langs->trans("BOMsSetup"), $linkback, 'title_setup');
181 
182 $head = bomAdminPrepareHead();
183 
184 print dol_get_fiche_head($head, 'settings', $langs->trans("BOMs"), -1, 'bom');
185 
186 /*
187  * BOMs Numbering model
188  */
189 
190 print load_fiche_titre($langs->trans("BOMsNumberingModules"), '', '');
191 
192 print '<table class="noborder centpercent">';
193 print '<tr class="liste_titre">';
194 print '<td>'.$langs->trans("Name").'</td>';
195 print '<td>'.$langs->trans("Description").'</td>';
196 print '<td class="nowrap">'.$langs->trans("Example").'</td>';
197 print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
198 print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
199 print '</tr>'."\n";
200 
201 clearstatcache();
202 
203 foreach ($dirmodels as $reldir)
204 {
205  $dir = dol_buildpath($reldir."core/modules/bom/");
206 
207  if (is_dir($dir))
208  {
209  $handle = opendir($dir);
210  if (is_resource($handle))
211  {
212  while (($file = readdir($handle)) !== false)
213  {
214  if (substr($file, 0, 8) == 'mod_bom_' && substr($file, dol_strlen($file) - 3, 3) == 'php')
215  {
216  $file = substr($file, 0, dol_strlen($file) - 4);
217 
218  require_once $dir.$file.'.php';
219 
220  $module = new $file($db);
221 
222  // Show modules according to features level
223  if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
224  if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
225 
226  if ($module->isEnabled())
227  {
228  print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
229  print $module->info();
230  print '</td>';
231 
232  // Show example of numbering model
233  print '<td class="nowrap">';
234  $tmp = $module->getExample();
235  if (preg_match('/^Error/', $tmp)) {
236  $langs->load("errors");
237  print '<div class="error">'.$langs->trans($tmp).'</div>';
238  } elseif ($tmp == 'NotConfigured') print $langs->trans($tmp);
239  else print $tmp;
240  print '</td>'."\n";
241 
242  print '<td class="center">';
243  if ($conf->global->BOM_ADDON == $file)
244  {
245  print img_picto($langs->trans("Activated"), 'switch_on');
246  } else {
247  print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&amp;token='.newToken().'&amp;value='.urlencode($file).'">';
248  print img_picto($langs->trans("Disabled"), 'switch_off');
249  print '</a>';
250  }
251  print '</td>';
252 
253  $bom = new BOM($db);
254  $bom->initAsSpecimen();
255 
256  // Info
257  $htmltooltip = '';
258  $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
259  $bom->type = 0;
260  $nextval = $module->getNextValue($mysoc, $bom);
261  if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
262  $htmltooltip .= ''.$langs->trans("NextValue").': ';
263  if ($nextval) {
264  if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured')
265  $nextval = $langs->trans($nextval);
266  $htmltooltip .= $nextval.'<br>';
267  } else {
268  $htmltooltip .= $langs->trans($module->error).'<br>';
269  }
270  }
271 
272  print '<td class="center">';
273  print $form->textwithpicto('', $htmltooltip, 1, 0);
274  print '</td>';
275 
276  print "</tr>\n";
277  }
278  }
279  }
280  closedir($handle);
281  }
282  }
283 }
284 print "</table><br>\n";
285 
286 
287 /*
288  * Document templates generators
289  */
290 
291 print load_fiche_titre($langs->trans("BOMsModelModule"), '', '');
292 
293 // Load array def with activated templates
294 $def = array();
295 $sql = "SELECT nom";
296 $sql .= " FROM ".MAIN_DB_PREFIX."document_model";
297 $sql .= " WHERE type = '".$db->escape($type)."'";
298 $sql .= " AND entity = ".$conf->entity;
299 $resql = $db->query($sql);
300 if ($resql)
301 {
302  $i = 0;
303  $num_rows = $db->num_rows($resql);
304  while ($i < $num_rows)
305  {
306  $array = $db->fetch_array($resql);
307  array_push($def, $array[0]);
308  $i++;
309  }
310 } else {
311  dol_print_error($db);
312 }
313 
314 
315 print "<table class=\"noborder\" width=\"100%\">\n";
316 print "<tr class=\"liste_titre\">\n";
317 print '<td>'.$langs->trans("Name").'</td>';
318 print '<td>'.$langs->trans("Description").'</td>';
319 print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
320 print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
321 print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
322 print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
323 print "</tr>\n";
324 
325 clearstatcache();
326 
327 foreach ($dirmodels as $reldir)
328 {
329  foreach (array('', '/doc') as $valdir)
330  {
331  $realpath = $reldir."core/modules/bom".$valdir;
332  $dir = dol_buildpath($realpath);
333 
334  if (is_dir($dir))
335  {
336  $handle = opendir($dir);
337  if (is_resource($handle))
338  {
339  while (($file = readdir($handle)) !== false)
340  {
341  $filelist[] = $file;
342  }
343  closedir($handle);
344  arsort($filelist);
345 
346  foreach ($filelist as $file)
347  {
348  if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file))
349  {
350  if (file_exists($dir.'/'.$file))
351  {
352  $name = substr($file, 4, dol_strlen($file) - 16);
353  $classname = substr($file, 0, dol_strlen($file) - 12);
354 
355  require_once $dir.'/'.$file;
356  $module = new $classname($db);
357 
358  $modulequalified = 1;
359  if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified = 0;
360  if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified = 0;
361 
362  if ($modulequalified)
363  {
364  print '<tr class="oddeven"><td width="100">';
365  print (empty($module->name) ? $name : $module->name);
366  print "</td><td>\n";
367  if (method_exists($module, 'info')) print $module->info($langs);
368  else print $module->description;
369  print '</td>';
370 
371  // Active
372  if (in_array($name, $def))
373  {
374  print '<td class="center">'."\n";
375  print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=del&amp;token='.newToken().'&amp;value='.$name.'">';
376  print img_picto($langs->trans("Enabled"), 'switch_on');
377  print '</a>';
378  print '</td>';
379  } else {
380  print '<td class="center">'."\n";
381  print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=set&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
382  print "</td>";
383  }
384 
385  // Default
386  print '<td class="center">';
387  if ($conf->global->BOM_ADDON_PDF == $name)
388  {
389  print img_picto($langs->trans("Default"), 'on');
390  } else {
391  print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setdoc&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
392  }
393  print '</td>';
394 
395  // Info
396  $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
397  $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
398  if ($module->type == 'pdf')
399  {
400  $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
401  }
402  $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
403 
404  $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
405  $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
406  $htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftBOMs").': '.yn($module->option_draft_watermark, 1, 1);
407 
408 
409  print '<td class="center">';
410  print $form->textwithpicto('', $htmltooltip, 1, 0);
411  print '</td>';
412 
413  // Preview
414  print '<td class="center">';
415  if ($module->type == 'pdf')
416  {
417  print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
418  } else {
419  print img_object($langs->trans("PreviewNotAvailable"), 'generic');
420  }
421  print '</td>';
422 
423  print "</tr>\n";
424  }
425  }
426  }
427  }
428  }
429  }
430  }
431 }
432 
433 print '</table>';
434 print "<br>";
435 
436 /*
437  * Other options
438  */
439 
440 print load_fiche_titre($langs->trans("OtherOptions"), '', '');
441 print '<table class="noborder centpercent">';
442 print '<tr class="liste_titre">';
443 print '<td>'.$langs->trans("Parameter").'</td>';
444 print '<td class="center" width="60">'.$langs->trans("Value").'</td>';
445 print "<td>&nbsp;</td>\n";
446 print "</tr>\n";
447 
448 $substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
449 $substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
450 $htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
451 foreach ($substitutionarray as $key => $val) $htmltext .= $key.'<br>';
452 $htmltext .= '</i>';
453 
454 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
455 print '<input type="hidden" name="token" value="'.newToken().'">';
456 print '<input type="hidden" name="action" value="set_BOM_FREE_TEXT">';
457 print '<tr class="oddeven"><td colspan="2">';
458 print $form->textwithpicto($langs->trans("FreeLegalTextOnBOMs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
459 $variablename = 'BOM_FREE_TEXT';
460 if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT))
461 {
462  print '<textarea name="'.$variablename.'" class="flat" cols="120">'.$conf->global->$variablename.'</textarea>';
463 } else {
464  include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
465  $doleditor = new DolEditor($variablename, $conf->global->$variablename, '', 80, 'dolibarr_notes');
466  print $doleditor->Create();
467 }
468 print '</td><td class="right">';
469 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
470 print "</td></tr>\n";
471 print '</form>';
472 
473 //Use draft Watermark
474 
475 print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
476 print '<input type="hidden" name="token" value="'.newToken().'">';
477 print "<input type=\"hidden\" name=\"action\" value=\"set_BOM_DRAFT_WATERMARK\">";
478 print '<tr class="oddeven"><td>';
479 print $form->textwithpicto($langs->trans("WatermarkOnDraftBOMs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
480 print '</td><td>';
481 print '<input class="flat minwidth200" type="text" name="BOM_DRAFT_WATERMARK" value="'.$conf->global->BOM_DRAFT_WATERMARK.'">';
482 print '</td><td class="right">';
483 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
484 print "</td></tr>\n";
485 print '</form>';
486 
487 print '</table>';
488 print '<br>';
489 
490 
491 // End of page
492 llxFooter();
493 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
delDocumentModel($name, $type)
Delete document model used by doc generator.
Definition: admin.lib.php:1748
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
Class for BOM.
Definition: bom.class.php:32
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:575
addDocumentModel($name, $type, $label= '', $description= '')
Add document model used by doc generator.
Definition: admin.lib.php:1717
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
dolibarr_del_const($db, $name, $entity=1)
Effacement d&#39;une constante dans la base de donnees.
Definition: admin.lib.php:499
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
bomAdminPrepareHead()
Prepare admin pages header.
Definition: bom.lib.php:30
if(!GETPOST('transkey', 'alphanohtml')&&!GETPOST('transphrase', 'alphanohtml')) else
View.
Definition: notice.php:44
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
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:643
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
print $_SERVER["PHP_SELF"] n
Edit parameters.
Definition: categories.php:101
Class to manage a WYSIWYG editor.
llxFooter()
Empty footer.
Definition: wrapper.php:59
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.