dolibarr  7.0.0-beta
printsheet.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4  * Copyright (C) 2006-2017 Laurent Destailleur <eldy@users.sourceforge.net>
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 <http://www.gnu.org/licenses/>.
18  */
19 
25 require '../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/printsheet/modules_labels.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
30 
31 $langs->load("admin");
32 $langs->load("members");
33 $langs->load("errors");
34 
35 // Choice of print year or current year.
36 $now = dol_now();
37 $year=dol_print_date($now,'%Y');
38 $month=dol_print_date($now,'%m');
39 $day=dol_print_date($now,'%d');
40 $forbarcode=GETPOST('forbarcode');
41 $fk_barcode_type=GETPOST('fk_barcode_type');
42 $mode=GETPOST('mode');
43 $modellabel=GETPOST("modellabel"); // Doc template to use
44 $numberofsticker=GETPOST('numberofsticker','int');
45 
46 $mesg='';
47 
48 $action=GETPOST('action','aZ09');
49 
50 $producttmp=new Product($db);
51 $thirdpartytmp=new Societe($db);
52 
53 
54 /*
55  * Actions
56  */
57 
58 if (GETPOST('submitproduct') && GETPOST('submitproduct'))
59 {
60  $action=''; // We reset because we don't want to build doc
61  if (GETPOST('productid') > 0)
62  {
63  $producttmp->fetch(GETPOST('productid'));
64  $forbarcode=$producttmp->barcode;
65  $fk_barcode_type=$producttmp->barcode_type;
66 
67  if (empty($fk_barcode_type) && ! empty($conf->global->PRODUIT_DEFAULT_BARCODE_TYPE)) $fk_barcode_type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE;
68 
69  if (empty($forbarcode) || empty($fk_barcode_type))
70  {
71  setEventMessages($langs->trans("DefinitionOfBarCodeForProductNotComplete",$producttmp->getNomUrl()), null, 'warnings');
72  }
73  }
74 }
75 if (GETPOST('submitthirdparty') && GETPOST('submitthirdparty'))
76 {
77  $action=''; // We reset because we don't want to build doc
78  if (GETPOST('socid') > 0)
79  {
80  $thirdpartytmp->fetch(GETPOST('socid'));
81  $forbarcode=$thirdpartytmp->barcode;
82  $fk_barcode_type=$thirdpartytmp->barcode_type_code;
83 
84  if (empty($fk_barcode_type) && ! empty($conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY)) $fk_barcode_type = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY;
85 
86  if (empty($forbarcode) || empty($fk_barcode_type))
87  {
88  setEventMessages($langs->trans("DefinitionOfBarCodeForThirdpartyNotComplete",$thirdpartytmp->getNomUrl()), null, 'warnings');
89  }
90  }
91 }
92 
93 if ($action == 'builddoc')
94 {
95  $result=0; $error=0;
96 
97  if (empty($forbarcode)) // barcode value
98  {
99  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BarcodeValue")), null, 'errors');
100  $error++;
101  }
102  if (empty($fk_barcode_type)) // barcode type = barcode encoding
103  {
104  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BarcodeType")), null, 'errors');
105  $error++;
106  }
107 
108  if (! $error)
109  {
110  // Get encoder (barcode_type_coder) from barcode type id (barcode_type)
111  $stdobject=new GenericObject($db);
112  $stdobject->barcode_type=$fk_barcode_type;
113  $result=$stdobject->fetch_barcode();
114  if ($result <= 0)
115  {
116  $error++;
117  setEventMessages('Failed to get bar code type information '.$stdobject->error, $stdobject->errors, 'errors');
118  }
119  }
120 
121  if (! $error)
122  {
123  $code=$forbarcode;
124  $generator=$stdobject->barcode_type_coder; // coder (loaded by fetch_barcode). Engine.
125  $encoding=strtoupper($stdobject->barcode_type_code); // code (loaded by fetch_barcode). Example 'ean', 'isbn', ...
126 
127  $diroutput=$conf->barcode->dir_temp;
128  dol_mkdir($diroutput);
129 
130  // Generate barcode
131  $dirbarcode=array_merge(array("/core/modules/barcode/doc/"),$conf->modules_parts['barcode']);
132 
133  foreach($dirbarcode as $reldir)
134  {
135  $dir=dol_buildpath($reldir,0);
136  $newdir=dol_osencode($dir);
137 
138  // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
139  if (! is_dir($newdir)) continue;
140 
141  $result=@include_once $newdir.$generator.'.modules.php';
142  if ($result) break;
143  }
144 
145  // Load barcode class for generating barcode image
146  $classname = "mod".ucfirst($generator);
147  $module = new $classname($db);
148  if ($generator != 'tcpdfbarcode')
149  {
150  // May be phpbarcode
151  $template = 'standardlabel';
152  $is2d = false;
153  if ($module->encodingIsSupported($encoding))
154  {
155  $barcodeimage=$conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
156  dol_delete_file($barcodeimage);
157  // File is created with full name $barcodeimage = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
158  $result=$module->writeBarCode($code,$encoding,'Y',4,1);
159  if ($result <= 0 || ! dol_is_file($barcodeimage))
160  {
161  $error++;
162  setEventMessages('Failed to generate image file of barcode for code='.$code.' encoding='.$encoding.' file='.basename($barcodeimage), null, 'errors');
163  setEventMessages($module->error, null, 'errors');
164  }
165  }
166  else
167  {
168  $error++;
169  setEventMessages("Error, encoding ".$encoding." is not supported by encoder ".$generator.'. You must choose another barcode type or install a barcode generation engine that support '.$encoding, null, 'errors');
170  }
171  } else {
172  $template = 'tcpdflabel';
173  $encoding = $module->getTcpdfEncodingType($encoding); //convert to TCPDF compatible encoding types
174  $is2d = $module->is2d;
175  }
176  }
177 
178  if (! $error)
179  {
180  // List of values to scan for a replacement
181  $substitutionarray = array (
182  '%LOGIN%'=>$user->login,
183  '%COMPANY%'=>$mysoc->name,
184  '%ADDRESS%'=>$mysoc->address,
185  '%ZIP%'=>$mysoc->zip,
186  '%TOWN%'=>$mysoc->town,
187  '%COUNTRY%'=>$mysoc->country,
188  '%COUNTRY_CODE%'=>$mysoc->country_code,
189  '%EMAIL%'=>$mysoc->email,
190  '%YEAR%'=>$year,
191  '%MONTH%'=>$month,
192  '%DAY%'=>$day,
193  '%DOL_MAIN_URL_ROOT%'=>DOL_MAIN_URL_ROOT,
194  '%SERVER%'=>"http://".$_SERVER["SERVER_NAME"]."/"
195  );
196  complete_substitutions_array($substitutionarray, $langs);
197 
198  // For labels
199  if ($mode == 'label')
200  {
201 
202  $txtforsticker="%PHOTO%"; // Photo will be barcode image, %BARCODE% posible when using TCPDF generator
203  $textleft=make_substitutions((empty($conf->global->BARCODE_LABEL_LEFT_TEXT)?$txtforsticker:$conf->global->BARCODE_LABEL_LEFT_TEXT), $substitutionarray);
204  $textheader=make_substitutions((empty($conf->global->BARCODE_LABEL_HEADER_TEXT)?'':$conf->global->BARCODE_LABEL_HEADER_TEXT), $substitutionarray);
205  $textfooter=make_substitutions((empty($conf->global->BARCODE_LABEL_FOOTER_TEXT)?'':$conf->global->BARCODE_LABEL_FOOTER_TEXT), $substitutionarray);
206  $textright=make_substitutions((empty($conf->global->BARCODE_LABEL_RIGHT_TEXT)?'':$conf->global->BARCODE_LABEL_RIGHT_TEXT), $substitutionarray);
207  $forceimgscalewidth=(empty($conf->global->BARCODE_FORCEIMGSCALEWIDTH)?1:$conf->global->BARCODE_FORCEIMGSCALEWIDTH);
208  $forceimgscaleheight=(empty($conf->global->BARCODE_FORCEIMGSCALEHEIGHT)?1:$conf->global->BARCODE_FORCEIMGSCALEHEIGHT);
209 
210  for ($i=0; $i < $numberofsticker; $i++)
211  {
212  $arrayofrecords[]=array(
213  'textleft'=>$textleft,
214  'textheader'=>$textheader,
215  'textfooter'=>$textfooter,
216  'textright'=>$textright,
217  'code'=>$code,
218  'encoding'=>$encoding,
219  'is2d'=>$is2d,
220  'photo'=>$barcodeimage // Photo must be a file that exists with format supported by TCPDF
221  );
222  }
223  }
224 
225  $i++;
226  $mesg='';
227 
228  // Build and output PDF
229  if ($mode == 'label')
230  {
231  if (! count($arrayofrecords))
232  {
233  $mesg=$langs->trans("ErrorRecordNotFound");
234  }
235  if (empty($modellabel) || $modellabel == '-1')
236  {
237  $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
238  }
239 
240  $outfile = $langs->trans("BarCode").'_sheets_'.dol_print_date(dol_now(),'dayhourlog').'.pdf';
241 
242  if (! $mesg) $result=doc_label_pdf_create($db, $arrayofrecords, $modellabel, $outputlangs, $diroutput, $template, dol_sanitizeFileName($outfile));
243  }
244 
245  if ($result <= 0)
246  {
247  dol_print_error('',$result);
248  }
249 
250  if (! $mesg)
251  {
252  $db->close();
253  exit;
254  }
255  }
256 }
257 
258 
259 /*
260  * View
261  */
262 
263 if (empty($conf->barcode->enabled)) accessforbidden();
264 
265 $form=new Form($db);
266 
267 llxHeader('',$langs->trans("BarCodePrintsheet"));
268 
269 print load_fiche_titre($langs->trans("BarCodePrintsheet"));
270 print '<br>';
271 
272 print $langs->trans("PageToGenerateBarCodeSheets",$langs->transnoentitiesnoconv("BuildPageToPrint")).'<br>';
273 print '<br>';
274 
275 dol_htmloutput_errors($mesg);
276 
277 //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
278 //print '<br>';
279 
280 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
281 print '<input type="hidden" name="mode" value="label">';
282 print '<input type="hidden" name="action" value="builddoc">';
283 
284 print '<div class="tagtable">';
285 
286 // Sheet format
287 print ' <div class="tagtr">';
288 print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
289 print $langs->trans("DescADHERENT_ETIQUETTE_TYPE").' &nbsp; ';
290 print '</div><div class="tagtd maxwidthonsmartphone" style="overflow: hidden; white-space: nowrap;">';
291 // List of possible labels (defined into $_Avery_Labels variable set into core/lib/format_cards.lib.php)
292 $arrayoflabels=array();
293 foreach(array_keys($_Avery_Labels) as $codecards)
294 {
295  $labeltoshow=$_Avery_Labels[$codecards]['name'];
296  //$labeltoshow.=' ('.$_Avery_Labels[$row['code']]['paper-size'].')';
297  $arrayoflabels[$codecards]=$labeltoshow;
298 }
299 asort($arrayoflabels);
300 print $form->selectarray('modellabel', $arrayoflabels, (GETPOST('modellabel')?GETPOST('modellabel'):$conf->global->ADHERENT_ETIQUETTE_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
301 print '</div></div>';
302 
303 // Number of stickers to print
304 print ' <div class="tagtr">';
305 print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
306 print $langs->trans("NumberOfStickers").' &nbsp; ';
307 print '</div><div class="tagtd maxwidthonsmartphone" style="overflow: hidden; white-space: nowrap;">';
308 print '<input size="4" type="text" name="numberofsticker" value="'.(GETPOST('numberofsticker')?GETPOST('numberofsticker','int'):10).'">';
309 print '</div></div>';
310 
311 print '</div>';
312 
313 
314 print '<br>';
315 
316 
317 // Add javascript to make choice dynamic
318 print '<script type="text/javascript" language="javascript">
319 jQuery(document).ready(function() {
320  function init_selectors()
321  {
322  if (jQuery("#fillmanually:checked").val() == "fillmanually")
323  {
324  jQuery("#submitproduct").prop("disabled", true);
325  jQuery("#submitthirdparty").prop("disabled", true);
326  jQuery("#search_productid").prop("disabled", true);
327  jQuery("#socid").prop("disabled", true);
328  jQuery(".showforproductselector").hide();
329  jQuery(".showforthirdpartyselector").hide();
330  }
331  if (jQuery("#fillfromproduct:checked").val() == "fillfromproduct")
332  {
333  jQuery("#submitproduct").removeAttr("disabled");
334  jQuery("#submitthirdparty").prop("disabled", true);
335  jQuery("#search_productid").removeAttr("disabled");
336  jQuery("#socid").prop("disabled", true);
337  jQuery(".showforproductselector").show();
338  jQuery(".showforthirdpartyselector").hide();
339  }
340  if (jQuery("#fillfromthirdparty:checked").val() == "fillfromthirdparty")
341  {
342  jQuery("#submitproduct").prop("disabled", true);
343  jQuery("#submitthirdparty").removeAttr("disabled");
344  jQuery("#search_productid").prop("disabled", true);
345  jQuery("#socid").removeAttr("disabled");
346  jQuery(".showforproductselector").hide();
347  jQuery(".showforthirdpartyselector").show();
348  }
349  }
350  init_selectors();
351  jQuery(".radiobarcodeselect").click(function() {
352  init_selectors();
353  });
354 
355  function init_gendoc_button()
356  {
357  if (jQuery("#select_fk_barcode_type").val() > 0 && jQuery("#forbarcode").val())
358  {
359  jQuery("#submitformbarcodegen").removeAttr("disabled");
360  }
361  else
362  {
363  jQuery("#submitformbarcodegen").prop("disabled", true);
364  }
365  }
366  init_gendoc_button();
367  jQuery("#select_fk_barcode_type").change(function() {
368  init_gendoc_button();
369  });
370  jQuery("#forbarcode").keyup(function() {
371  init_gendoc_button()
372  });
373 });
374 </script>';
375 
376 // Checkbox to select from free text
377 print '<input id="fillmanually" type="radio" '.((! GETPOST("selectorforbarcode") || GETPOST("selectorforbarcode")=='fillmanually')?'checked ':'').'name="selectorforbarcode" value="fillmanually" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueManually").' &nbsp; ';
378 print '<br>';
379 
380 if (! empty($user->rights->produit->lire) || ! empty($user->rights->service->lire))
381 {
382  print '<input id="fillfromproduct" type="radio" '.((GETPOST("selectorforbarcode")=='fillfromproduct')?'checked ':'').'name="selectorforbarcode" value="fillfromproduct" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueFromProduct").' &nbsp; ';
383  print '<br>';
384  print '<div class="showforproductselector">';
385  $form->select_produits(GETPOST('productid'), 'productid', '');
386  print ' &nbsp; <input type="submit" id="submitproduct" name="submitproduct" class="button" value="'.(dol_escape_htmltag($langs->trans("GetBarCode"))).'">';
387  print '</div>';
388 }
389 
390 if (! empty($user->rights->societe->lire))
391 {
392  print '<input id="fillfromthirdparty" type="radio" '.((GETPOST("selectorforbarcode")=='fillfromthirdparty')?'checked ':'').'name="selectorforbarcode" value="fillfromthirdparty" class="radiobarcodeselect"> '.$langs->trans("FillBarCodeTypeAndValueFromThirdParty").' &nbsp; ';
393  print '<br>';
394  print '<div class="showforthirdpartyselector">';
395  print $form->select_company(GETPOST('socid'), 'socid', '', 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
396  print ' &nbsp; <input type="submit" id="submitthirdparty" name="submitthirdparty" class="button showforthirdpartyselector" value="'.(dol_escape_htmltag($langs->trans("GetBarCode"))).'">';
397  print '</div>';
398 }
399 
400 print '<br>';
401 
402 if ($producttmp->id > 0)
403 {
404  print $langs->trans("BarCodeDataForProduct",'').' '.$producttmp->getNomUrl(1).'<br>';
405 }
406 if ($thirdpartytmp->id > 0)
407 {
408  print $langs->trans("BarCodeDataForThirdparty",'').' '.$thirdpartytmp->getNomUrl(1).'<br>';
409 }
410 
411 print '<div class="tagtable">';
412 
413 // Barcode type
414 print ' <div class="tagtr">';
415 print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
416 print $langs->trans("BarcodeType").' &nbsp; ';
417 print '</div><div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
418 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbarcode.class.php';
419 $formbarcode = new FormBarCode($db);
420 $formbarcode->select_barcode_type($fk_barcode_type, 'fk_barcode_type', 1);
421 print '</div></div>';
422 
423 // Barcode value
424 print ' <div class="tagtr">';
425 print ' <div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
426 print $langs->trans("BarcodeValue").' &nbsp; ';
427 print '</div><div class="tagtd" style="overflow: hidden; white-space: nowrap; max-width: 300px;">';
428 print '<input size="16" type="text" name="forbarcode" id="forbarcode" value="'.$forbarcode.'">';
429 print '</div></div>';
430 
431 /*
432 $barcodestickersmask=GETPOST('barcodestickersmask');
433 print '<br>'.$langs->trans("BarcodeStickersMask").':<br>';
434 print '<textarea cols="40" type="text" name="barcodestickersmask" value="'.GETPOST('barcodestickersmask').'">'.$barcodestickersmask.'</textarea>';
435 print '<br>';
436 */
437 
438 print '</div>';
439 
440 print '<br><input class="button" type="submit" id="submitformbarcodegen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode"))?'':'disabled ').'value="'.$langs->trans("BuildPageToPrint").'">';
441 
442 print '</form>';
443 print '<br>';
444 
445 llxFooter();
446 
447 $db->close();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
doc_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir='', $template='standardlabel', $filename='tmp_address_sheet.pdf')
Create a document onto disk according to template module.
Class to manage products or services.
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.
dol_htmloutput_errors($mesgstring='', $mesgarray='', $keepembedded=0)
Print formated error messages to output (Used to show messages on html output).
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.
Class to manage third parties objects (customers, suppliers, prospects...)
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
Class to manage barcode HTML.
dol_now($mode='gmt')
Return date for now.
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:427
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
Class of a generic business object.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null)
Remove a file or several files with a mask.
Definition: files.lib.php:1103
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substition into a text string, replacing keys with vals from $substitutionarray (oldval=>newval)...
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...