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