dolibarr 25.0.0-alpha
bom.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
32require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
34
44// Load translation files required by the page
45$langs->loadLangs(array('admin', 'mrp', 'other'));
46
47if (!$user->admin) {
49}
50
51$action = GETPOST('action', 'aZ09');
52$value = GETPOST('value', 'alpha');
53$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
54
55$label = GETPOST('label', 'alpha');
56$scandir = GETPOST('scan_dir', 'alpha');
57$type = 'bom';
58
59
60/*
61 * Actions
62 */
63$error = 0;
64
65include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
66
67if ($action == 'updateMask') {
68 $maskconstbom = GETPOST('maskconstBom', 'aZ09');
69 $maskbom = GETPOST('maskBom', 'alpha');
70
71 $res = 0;
72
73 if ($maskconstbom && preg_match('/_MASK$/', $maskconstbom)) {
74 $res = dolibarr_set_const($db, $maskconstbom, $maskbom, 'chaine', 0, '', $conf->entity);
75 }
76
77 if (!($res > 0)) {
78 $error++;
79 }
80
81 if (!$error) {
82 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
83 } else {
84 setEventMessages($langs->trans("Error"), null, 'errors');
85 }
86} elseif ($action == 'specimen') {
87 $modele = GETPOST('module', 'alpha');
88
89 $bom = new BOM($db);
90 $bom->initAsSpecimen();
91
92 // Search template files
93 $file = '';
94 $classname = '';
95 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
96 foreach ($dirmodels as $reldir) {
97 $file = dol_buildpath($reldir."core/modules/bom/doc/pdf_".$modele.".modules.php", 0);
98 if (file_exists($file)) {
99 $classname = "pdf_".$modele;
100 break;
101 }
102 }
103
104 if ($classname !== '') {
105 require_once $file;
106
107 $module = new $classname($db);
108 '@phan-var-force ModelePDFBom $module';
109
110 if ($module->write_file($bom, $langs) > 0) {
111 header("Location: ".DOL_URL_ROOT."/document.php?modulepart=bom&file=SPECIMEN.pdf");
112 return;
113 } else {
114 setEventMessages($module->error, $module->errors, 'errors');
115 dol_syslog($module->error, LOG_ERR);
116 }
117 } else {
118 $langs->load('errors');
119 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
120 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
121 }
122} elseif ($action == 'set') {
123 // Activate a model
124 $ret = addDocumentModel($value, $type, $label, $scandir);
125} elseif ($action == 'del') {
126 $ret = delDocumentModel($value, $type);
127 if ($ret > 0) {
128 if (getDolGlobalString('BOM_ADDON_PDF') == "$value") {
129 dolibarr_del_const($db, 'BOM_ADDON_PDF', $conf->entity);
130 }
131 }
132} elseif ($action == 'setdoc') {
133 // Set default model
134 if (dolibarr_set_const($db, "BOM_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
135 // The constant that was read before the new set
136 // We therefore requires a variable to have a coherent view
137 $conf->global->BOM_ADDON_PDF = $value;
138 }
139
140 // On active le modele
141 $ret = delDocumentModel($value, $type);
142 if ($ret > 0) {
143 $ret = addDocumentModel($value, $type, $label, $scandir);
144 }
145} elseif ($action == 'setmod') {
146 // TODO Check if numbering module chosen can be activated
147 // by calling method canBeActivated
148
149 dolibarr_set_const($db, "BOM_ADDON", $value, 'chaine', 0, '', $conf->entity);
150} elseif ($action == 'updateother') {
151 $draft = GETPOST("BOM_DRAFT_WATERMARK");
152 $res = dolibarr_set_const($db, "BOM_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
153 if (!($res > 0)) {
154 $error++;
155 }
156
157 $bomsearch = GETPOST('activate_BOM_USE_SEARCH_TO_SELECT', 'alpha');
158 $res = dolibarr_set_const($db, "BOM_USE_SEARCH_TO_SELECT", $bomsearch, 'chaine', 0, '', $conf->entity);
159 if (!($res > 0)) {
160 $error++;
161 }
162
163 $freetext = GETPOST("BOM_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
164 $res = dolibarr_set_const($db, "BOM_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
165 if (!($res > 0)) {
166 $error++;
167 }
168
169
170
171 if (!$error) {
172 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
173 } else {
174 setEventMessages($langs->trans("Error"), null, 'errors');
175 }
176}
177
178/*
179 * View
180 */
181
182$form = new Form($db);
183
184$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
185
186llxHeader("", $langs->trans("BOMsSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-bom');
187
188$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
189
190print load_fiche_titre($langs->trans("BOMsSetup"), $linkback, 'title_setup');
191
192$head = bomAdminPrepareHead();
193
194print dol_get_fiche_head($head, 'settings', $langs->trans("BOMs"), -1, 'bom');
195
196/*
197 * Numbering module
198 */
199
200print load_fiche_titre($langs->trans("BOMsNumberingModules"), '', '');
201
202print '<div class="div-table-responsive-no-min">';
203print '<table class="noborder centpercent">';
204print '<tr class="liste_titre">';
205print '<td>'.$langs->trans("Name").'</td>';
206print '<td>'.$langs->trans("Description").'</td>';
207print '<td class="nowrap">'.$langs->trans("Example").'</td>';
208print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
209print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
210print '</tr>'."\n";
211
212clearstatcache();
213
214foreach ($dirmodels as $reldir) {
215 $dir = dol_buildpath($reldir."core/modules/bom/");
216
217 if (is_dir($dir)) {
218 $handle = opendir($dir);
219 if (is_resource($handle)) {
220 while (($file = readdir($handle)) !== false) {
221 if (dol_substr($file, 0, 8) == 'mod_bom_' && dol_substr($file, dol_strlen($file) - 3, 3) == 'php') {
222 $file = dol_substr($file, 0, dol_strlen($file) - 4);
223 $classname = $file;
224
225 require_once $dir.$file.'.php';
226
227 $module = new $classname($db);
228
229 '@phan-var-force ModeleNumRefBoms $module';
232 // Show modules according to features level
233 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
234 continue;
235 }
236 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
237 continue;
238 }
239
240 if ($module->isEnabled()) {
241 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
242 print $module->info($langs);
243 print '</td>';
244
245 // Show example of numbering module
246 print '<td class="nowrap">';
247 $tmp = $module->getExample();
248 if (preg_match('/^Error/', $tmp)) {
249 $langs->load("errors");
250 print '<div class="error">'.$langs->trans($tmp).'</div>';
251 } elseif ($tmp == 'NotConfigured') {
252 print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
253 } else {
254 print $tmp;
255 }
256 print '</td>'."\n";
257
258 print '<td class="center">';
259 if (getDolGlobalString('BOM_ADDON') == $file) {
260 print img_picto($langs->trans("Activated"), 'switch_on');
261 } else {
262 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
263 print img_picto($langs->trans("Disabled"), 'switch_off');
264 print '</a>';
265 }
266 print '</td>';
267
268 $bom = new BOM($db);
269 $bom->initAsSpecimen();
270
271 $tmpprod = new Product($db);
272 $tmpprod->initAsSpecimen();
273
274 // Info
275 $htmltooltip = '';
276 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
277 $bom->type = 0;
278 $nextval = $module->getNextValue($tmpprod, $bom);
279 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
280 $htmltooltip .= ''.$langs->trans("NextValue").': ';
281 if ($nextval) {
282 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
283 $langs->load('errors');
284 $nextval = $langs->trans($nextval);
285 }
286 $htmltooltip .= $nextval.'<br>';
287 } else {
288 $langs->load('errors');
289 $htmltooltip .= $langs->trans($module->error).'<br>';
290 }
291 }
292
293 print '<td class="center">';
294 print $form->textwithpicto('', $htmltooltip, 1, 'info');
295 print '</td>';
296
297 print "</tr>\n";
298 }
299 }
300 }
301 closedir($handle);
302 }
303 }
304}
305print "</table>";
306print "</div>";
307
308
309/*
310 * Document templates generators
311 */
312
313print "<br>\n";
314printDocumentModelList($type, 'bom', 'BOM_ADDON_PDF', $langs->trans("BOMsModelModule"), array(
315 'MultiLanguage' => 'option_multilang',
316 'WatermarkOnDraftBOMs' => 'option_draft_watermark',
317));
318
319/*
320 * Other options
321 */
322
323print "<br>";
324print load_fiche_titre($langs->trans("OtherOptions"), '', '');
325
326print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" spellcheck="false">';
327print '<input type="hidden" name="token" value="'.newToken().'">';
328print '<input type="hidden" name="action" value="updateother">';
329
330print '<div class="div-table-responsive-no-min">';
331print '<table class="noborder centpercent">';
332print '<tr class="liste_titre">';
333print '<td>'.$langs->trans("Parameter").'</td>';
334print '<td class="center"></td>';
335print "</tr>\n";
336
337if (getDolGlobalString('MAIN_FEATURES_LEVEL') >= 1) {
338 print '<tr class="oddeven"><td>';
339 print $langs->trans("BOM_SUB_BOM");
340 print '</td><td>';
341 if ($conf->use_javascript_ajax) {
342 print ajax_constantonoff('BOM_SUB_BOM');
343 } else {
344 if (!getDolGlobalString('BOM_SUB_BOM')) {
345 print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_BOM_SUB_BOM&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
346 } else {
347 print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_BOM_SUB_BOM&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
348 }
349 }
350
351 print "</td></tr>\n";
352}
353
354
355print '<tr class="oddeven">';
356print '<td>'.$langs->trans("UseSearchToSelectBom").'</td>';
357if (!$conf->use_javascript_ajax) {
358 print '<td></td>';
359 print '<td class="nowrap right">';
360 print $langs->trans("NotAvailableWhenAjaxDisabled");
361 print "</td>";
362} else {
363 print '<td>';
364 $arrval = array(
365 0 => array('label' => $langs->trans("No")),
366 1 => array('label' => $langs->trans("Yes").' ('.$langs->trans("NumberOfKeyToSearch", 1).')', 'labelhtml' => $langs->trans("Yes").' <span class="opacitymedium small">('.$langs->trans("NumberOfKeyToSearch", 1).')</span>'),
367 2 => array('label' => $langs->trans("Yes").' ('.$langs->trans("NumberOfKeyToSearch", 2).')', 'labelhtml' => $langs->trans("Yes").' <span class="opacitymedium small">('.$langs->trans("NumberOfKeyToSearch", 2).')</span>'),
368 3 => array('label' => $langs->trans("Yes").' ('.$langs->trans("NumberOfKeyToSearch", 3).')', 'labelhtml' => $langs->trans("Yes").' <span class="opacitymedium small">('.$langs->trans("NumberOfKeyToSearch", 3).')</span>'),
369 );
370 print $form->selectarray("activate_BOM_USE_SEARCH_TO_SELECT", $arrval, getDolGlobalString("BOM_USE_SEARCH_TO_SELECT")).'</td>';
371}
372print '</form>';
373print '</tr>';
374
375$substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
376$substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
377$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
378foreach ($substitutionarray as $key => $val) {
379 $htmltext .= $key.'<br>';
380}
381$htmltext .= '</i>';
382
383print '<tr class="oddeven"><td colspan="2">';
384print $form->textwithpicto($langs->trans("FreeLegalTextOnBOMs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
385$variablename = 'BOM_FREE_TEXT';
386if (!getDolGlobalString('PDF_ALLOW_HTML_FOR_FREE_TEXT')) {
387 print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
388} else {
389 include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
390 $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
391 print $doleditor->Create();
392}
393print "</td></tr>\n";
394
395// Use draft Watermark
396
397print '<tr class="oddeven"><td>';
398print $form->textwithpicto($langs->trans("WatermarkOnDraftBOMs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
399print '</td><td>';
400print '<input class="flat minwidth200" type="text" name="BOM_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('BOM_DRAFT_WATERMARK')).'">';
401print "</td></tr>\n";
402
403print '</table>';
404print '</div>';
405
406print $form->buttonsSaveCancel("Save", '');
407
408print '</form>';
409
410print dol_get_fiche_end();
411
412// End of page
413llxFooter();
414$db->close();
addDocumentModel($name, $type, $label='', $description='')
Add document model used by doc generator.
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).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
delDocumentModel($name, $type)
Delete document model used by doc generator.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
bomAdminPrepareHead()
Prepare admin pages header.
Definition bom.lib.php:31
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class for BOM.
Definition bom.class.php:42
Class to manage a WYSIWYG editor.
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:542
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:740
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
pdf_getSubstitutionArray($outputlangs, $exclude=null, $object=null, $onlykey=0, $include=null)
Return array of possible substitutions for PDF content (without external module substitutions).
Definition pdf.lib.php:1180
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.