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', 'errors', '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 setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
119 dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
120 }
121} elseif ($action == 'set') {
122 // Activate a model
123 $ret = addDocumentModel($value, $type, $label, $scandir);
124} elseif ($action == 'del') {
125 $ret = delDocumentModel($value, $type);
126 if ($ret > 0) {
127 if (getDolGlobalString('BOM_ADDON_PDF') == "$value") {
128 dolibarr_del_const($db, 'BOM_ADDON_PDF', $conf->entity);
129 }
130 }
131} elseif ($action == 'setdoc') {
132 // Set default model
133 if (dolibarr_set_const($db, "BOM_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
134 // The constant that was read before the new set
135 // We therefore requires a variable to have a coherent view
136 $conf->global->BOM_ADDON_PDF = $value;
137 }
138
139 // On active le modele
140 $ret = delDocumentModel($value, $type);
141 if ($ret > 0) {
142 $ret = addDocumentModel($value, $type, $label, $scandir);
143 }
144} elseif ($action == 'setmod') {
145 // TODO Check if numbering module chosen can be activated
146 // by calling method canBeActivated
147
148 dolibarr_set_const($db, "BOM_ADDON", $value, 'chaine', 0, '', $conf->entity);
149} elseif ($action == 'updateother') {
150 $draft = GETPOST("BOM_DRAFT_WATERMARK");
151 $res = dolibarr_set_const($db, "BOM_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
152 if (!($res > 0)) {
153 $error++;
154 }
155
156 $bomsearch = GETPOST('activate_BOM_USE_SEARCH_TO_SELECT', 'alpha');
157 $res = dolibarr_set_const($db, "BOM_USE_SEARCH_TO_SELECT", $bomsearch, 'chaine', 0, '', $conf->entity);
158 if (!($res > 0)) {
159 $error++;
160 }
161
162 $freetext = GETPOST("BOM_FREE_TEXT", 'restricthtml'); // No alpha here, we want exact string
163 $res = dolibarr_set_const($db, "BOM_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
164 if (!($res > 0)) {
165 $error++;
166 }
167
168
169
170 if (!$error) {
171 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
172 } else {
173 setEventMessages($langs->trans("Error"), null, 'errors');
174 }
175}
176
177/*
178 * View
179 */
180
181$form = new Form($db);
182
183$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
184
185llxHeader("", $langs->trans("BOMsSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-bom');
186
187$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>';
188
189print load_fiche_titre($langs->trans("BOMsSetup"), $linkback, 'title_setup');
190
191$head = bomAdminPrepareHead();
192
193print dol_get_fiche_head($head, 'settings', $langs->trans("BOMs"), -1, 'bom');
194
195/*
196 * Numbering module
197 */
198
199print load_fiche_titre($langs->trans("BOMsNumberingModules"), '', '');
200
201print '<div class="div-table-responsive-no-min">';
202print '<table class="noborder centpercent">';
203print '<tr class="liste_titre">';
204print '<td>'.$langs->trans("Name").'</td>';
205print '<td>'.$langs->trans("Description").'</td>';
206print '<td class="nowrap">'.$langs->trans("Example").'</td>';
207print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
208print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
209print '</tr>'."\n";
210
211clearstatcache();
212
213foreach ($dirmodels as $reldir) {
214 $dir = dol_buildpath($reldir."core/modules/bom/");
215
216 if (is_dir($dir)) {
217 $handle = opendir($dir);
218 if (is_resource($handle)) {
219 while (($file = readdir($handle)) !== false) {
220 if (dol_substr($file, 0, 8) == 'mod_bom_' && dol_substr($file, dol_strlen($file) - 3, 3) == 'php') {
221 $file = dol_substr($file, 0, dol_strlen($file) - 4);
222 $classname = $file;
223
224 require_once $dir.$file.'.php';
225
226 $module = new $classname($db);
227
228 '@phan-var-force ModeleNumRefBoms $module';
231 // Show modules according to features level
232 if ($module->version == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
233 continue;
234 }
235 if ($module->version == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
236 continue;
237 }
238
239 if ($module->isEnabled()) {
240 print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
241 print $module->info($langs);
242 print '</td>';
243
244 // Show example of numbering module
245 print '<td class="nowrap">';
246 $tmp = $module->getExample();
247 if (preg_match('/^Error/', $tmp)) {
248 $langs->load("errors");
249 print '<div class="error">'.$langs->trans($tmp).'</div>';
250 } elseif ($tmp == 'NotConfigured') {
251 print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
252 } else {
253 print $tmp;
254 }
255 print '</td>'."\n";
256
257 print '<td class="center">';
258 if (getDolGlobalString('BOM_ADDON') == $file) {
259 print img_picto($langs->trans("Activated"), 'switch_on');
260 } else {
261 print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
262 print img_picto($langs->trans("Disabled"), 'switch_off');
263 print '</a>';
264 }
265 print '</td>';
266
267 $bom = new BOM($db);
268 $bom->initAsSpecimen();
269
270 $tmpprod = new Product($db);
271 $tmpprod->initAsSpecimen();
272
273 // Info
274 $htmltooltip = '';
275 $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
276 $bom->type = 0;
277 $nextval = $module->getNextValue($tmpprod, $bom);
278 if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
279 $htmltooltip .= ''.$langs->trans("NextValue").': ';
280 if ($nextval) {
281 if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
282 $nextval = $langs->trans($nextval);
283 }
284 $htmltooltip .= $nextval.'<br>';
285 } else {
286 $htmltooltip .= $langs->trans($module->error).'<br>';
287 }
288 }
289
290 print '<td class="center">';
291 print $form->textwithpicto('', $htmltooltip, 1, 'info');
292 print '</td>';
293
294 print "</tr>\n";
295 }
296 }
297 }
298 closedir($handle);
299 }
300 }
301}
302print "</table>";
303print "</div>";
304
305
306/*
307 * Document templates generators
308 */
309
310print "<br>\n";
311printDocumentModelList($type, 'bom', 'BOM_ADDON_PDF', $langs->trans("BOMsModelModule"), array(
312 'MultiLanguage' => 'option_multilang',
313 'WatermarkOnDraftBOMs' => 'option_draft_watermark',
314));
315
316/*
317 * Other options
318 */
319
320print "<br>";
321print load_fiche_titre($langs->trans("OtherOptions"), '', '');
322
323print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" spellcheck="false">';
324print '<input type="hidden" name="token" value="'.newToken().'">';
325print '<input type="hidden" name="action" value="updateother">';
326
327print '<div class="div-table-responsive-no-min">';
328print '<table class="noborder centpercent">';
329print '<tr class="liste_titre">';
330print '<td>'.$langs->trans("Parameter").'</td>';
331print '<td class="center"></td>';
332print "</tr>\n";
333
334if (getDolGlobalString('MAIN_FEATURES_LEVEL') >= 1) {
335 print '<tr class="oddeven"><td>';
336 print $langs->trans("BOM_SUB_BOM");
337 print '</td><td>';
338 if ($conf->use_javascript_ajax) {
339 print ajax_constantonoff('BOM_SUB_BOM');
340 } else {
341 if (!getDolGlobalString('BOM_SUB_BOM')) {
342 print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_BOM_SUB_BOM&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
343 } else {
344 print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_BOM_SUB_BOM&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
345 }
346 }
347
348 print "</td></tr>\n";
349}
350
351
352print '<tr class="oddeven">';
353print '<td>'.$langs->trans("UseSearchToSelectBom").'</td>';
354if (!$conf->use_javascript_ajax) {
355 print '<td></td>';
356 print '<td class="nowrap right">';
357 print $langs->trans("NotAvailableWhenAjaxDisabled");
358 print "</td>";
359} else {
360 print '<td>';
361 $arrval = array(
362 0 => array('label' => $langs->trans("No")),
363 1 => array('label' => $langs->trans("Yes").' ('.$langs->trans("NumberOfKeyToSearch", 1).')', 'labelhtml' => $langs->trans("Yes").' <span class="opacitymedium small">('.$langs->trans("NumberOfKeyToSearch", 1).')</span>'),
364 2 => array('label' => $langs->trans("Yes").' ('.$langs->trans("NumberOfKeyToSearch", 2).')', 'labelhtml' => $langs->trans("Yes").' <span class="opacitymedium small">('.$langs->trans("NumberOfKeyToSearch", 2).')</span>'),
365 3 => array('label' => $langs->trans("Yes").' ('.$langs->trans("NumberOfKeyToSearch", 3).')', 'labelhtml' => $langs->trans("Yes").' <span class="opacitymedium small">('.$langs->trans("NumberOfKeyToSearch", 3).')</span>'),
366 );
367 print $form->selectarray("activate_BOM_USE_SEARCH_TO_SELECT", $arrval, getDolGlobalString("BOM_USE_SEARCH_TO_SELECT")).'</td>';
368}
369print '</form>';
370print '</tr>';
371
372$substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
373$substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
374$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
375foreach ($substitutionarray as $key => $val) {
376 $htmltext .= $key.'<br>';
377}
378$htmltext .= '</i>';
379
380print '<tr class="oddeven"><td colspan="2">';
381print $form->textwithpicto($langs->trans("FreeLegalTextOnBOMs"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
382$variablename = 'BOM_FREE_TEXT';
383if (!getDolGlobalString('PDF_ALLOW_HTML_FOR_FREE_TEXT')) {
384 print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
385} else {
386 include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
387 $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
388 print $doleditor->Create();
389}
390print "</td></tr>\n";
391
392// Use draft Watermark
393
394print '<tr class="oddeven"><td>';
395print $form->textwithpicto($langs->trans("WatermarkOnDraftBOMs"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
396print '</td><td>';
397print '<input class="flat minwidth200" type="text" name="BOM_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('BOM_DRAFT_WATERMARK')).'">';
398print "</td></tr>\n";
399
400print '</table>';
401print '</div>';
402
403print $form->buttonsSaveCancel("Save", '');
404
405print '</form>';
406
407print dol_get_fiche_end();
408
409// End of page
410llxFooter();
411$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
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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_strlen($string, $stringencoding='UTF-8')
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:540
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:738
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.