162 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
167 if (!is_object($outputlangs)) {
168 $outputlangs = $langs;
172 $outputlangs->charset_output =
'ISO-8859-1';
176 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"stocks",
"products"));
178 $isspecimen = empty(
$object->id);
181 $this->error =
'Error: no output dir defined for module stock';
188 $file = $dir.
'/SPECIMEN.pdf';
191 $dir = $basedir.
'/'.$objectref;
192 $file = $dir.
'/'.$objectref.
'.pdf';
195 if (!file_exists($dir)) {
197 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
205 for ($i = 1; $i <= 5; $i++) {
207 $line->product_ref =
'PROD'.sprintf(
'%04d', $i);
208 $line->product_label =
'Product '.$i;
209 $line->warehouse_ref =
'WH-A';
211 $line->qty_stock = 10 * $i;
212 $line->qty_view = 10 * $i - ($i % 2);
213 $line->pmp = 2.5 * $i;
218 $recorded = ((int)
$object->status >= Inventory::STATUS_RECORDED);
219 $sql =
"SELECT id.rowid, id.qty_stock, id.qty_view, id.qty_regulated, id.batch, id.pmp_real, id.pmp_expected,";
220 $sql .=
" p.ref as product_ref, p.label as product_label, e.ref as warehouse_ref";
221 $sql .=
" FROM ".MAIN_DB_PREFIX.
"inventorydet as id";
222 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = id.fk_product";
223 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot as e ON e.rowid = id.fk_warehouse";
224 $sql .=
" WHERE id.fk_inventory = ".((int)
$object->id);
225 $sql .=
" ORDER BY e.ref, p.ref";
226 $resql = $this->db->query($sql);
228 $this->error = $this->db->lasterror();
231 while ($obj = $this->db->fetch_object($resql)) {
233 $line->product_ref = (string) $obj->product_ref;
234 $line->product_label = (string) $obj->product_label;
235 $line->warehouse_ref = (string) $obj->warehouse_ref;
236 $line->batch = (string) $obj->batch;
237 $line->qty_stock = $obj->qty_stock;
238 $line->qty_view = $recorded ? ($obj->qty_view !==
null ? $obj->qty_view : ($obj->qty_stock + $obj->qty_regulated)) : $obj->qty_view;
239 $line->pmp = ($obj->pmp_real !==
null && (float) $obj->pmp_real != 0) ? $obj->pmp_real : $obj->pmp_expected;
247 $pdf->SetAutoPageBreak(
true, 0);
249 $heightforinfotot = 30;
250 $heightforsignature = ($isspecimen || !$recorded) ? 25 : 0;
252 $heightforfooter = $this->marge_basse + 8;
254 if (class_exists(
'TCPDF')) {
255 $pdf->setPrintHeader(
false);
256 $pdf->setPrintFooter(
false);
262 $pdf->SetDrawColor(128, 128, 128);
264 $pdf->SetTitle($outputlangs->convToOutputCharset($isspecimen ?
'SPECIMEN' :
$object->ref));
265 $pdf->SetSubject($outputlangs->transnoentities(
"Inventory"));
266 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
267 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
268 $pdf->SetKeyWords($outputlangs->convToOutputCharset($isspecimen ?
'SPECIMEN' :
$object->ref).
" ".$outputlangs->transnoentities(
"Inventory"));
270 $pdf->SetCompression(
false);
274 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
277 $this->posxproduct = $this->marge_gauche;
278 $this->posxlabel = $this->marge_gauche + 30;
279 $this->posxwarehouse = $this->page_largeur - $this->marge_droite - 92;
280 $this->posxbatch = $this->page_largeur - $this->marge_droite - 70;
281 $this->posxqtystock = $this->page_largeur - $this->marge_droite - 50;
282 $this->posxqtyview = $this->page_largeur - $this->marge_droite - 34;
283 $this->posxgap = $this->page_largeur - $this->marge_droite - 18;
284 $this->posxend = $this->page_largeur - $this->marge_droite;
289 $top_shift = $this->
pageHead($pdf,
$object, 1, $outputlangs, $recorded, $isspecimen);
290 $pdf->SetFont(
'',
'', $default_font_size - 1);
291 $pdf->MultiCell(0, 3,
'');
292 $pdf->SetTextColor(0, 0, 0);
294 $tab_top = 42 + $top_shift;
295 $tab_top_newpage = 10;
298 $this->
tableHeader($pdf, $tab_top, $outputlangs, $recorded);
299 $nexY = $tab_top + 7;
301 $nblines = count($lines);
306 for ($i = 0; $i < $nblines; $i++) {
309 $pdf->SetFont(
'',
'', $default_font_size - 2);
312 if ($curY > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot + $heightforsignature))) {
316 $this->
tableHeader($pdf, $tab_top_newpage, $outputlangs, $recorded);
317 $curY = $tab_top_newpage + 7;
318 $pdf->SetFont(
'',
'', $default_font_size - 2);
319 $pdf->SetTextColor(0, 0, 0);
323 $pdf->SetXY($this->posxproduct, $curY);
324 $pdf->MultiCell($this->posxlabel - $this->posxproduct - 1, 3, $outputlangs->convToOutputCharset($line->product_ref), 0,
'L');
326 $pdf->SetXY($this->posxlabel, $curY);
327 $pdf->MultiCell($this->posxwarehouse - $this->posxlabel - 1, 3, $outputlangs->convToOutputCharset(
pdf_truncate_text($pdf, $line->product_label, 45)), 0,
'L');
329 $pdf->SetXY($this->posxwarehouse, $curY);
330 $pdf->MultiCell($this->posxbatch - $this->posxwarehouse - 1, 3, $outputlangs->convToOutputCharset(
pdf_truncate_text($pdf, $line->warehouse_ref, 12)), 0,
'L');
332 $pdf->SetXY($this->posxbatch, $curY);
333 $pdf->MultiCell($this->posxqtystock - $this->posxbatch - 1, 3, $outputlangs->convToOutputCharset(
pdf_truncate_text($pdf, $line->batch, 10)), 0,
'L');
335 $pdf->SetXY($this->posxqtystock, $curY);
336 $pdf->MultiCell($this->posxqtyview - $this->posxqtystock - 1, 3, ($line->qty_stock !==
null && $line->qty_stock !==
'') ?
price2num($line->qty_stock,
'MS') :
'', 0,
'R');
339 $gap = (float) $line->qty_view - (
float) $line->qty_stock;
341 $pdf->SetXY($this->posxqtyview, $curY);
342 $pdf->MultiCell($this->posxgap - $this->posxqtyview - 1, 3,
price2num($line->qty_view,
'MS'), 0,
'R');
345 $pdf->SetTextColor(150, 0, 0);
348 $pdf->SetXY($this->posxgap, $curY);
349 $pdf->MultiCell($this->posxend - $this->posxgap, 3, ($gap > 0 ?
'+' :
'').
price2num($gap,
'MS'), 0,
'R');
350 $pdf->SetTextColor(0, 0, 0);
352 $totalgapvalue += $gap * (float) $line->pmp;
355 $pdf->SetXY($this->posxqtyview, $curY - 1);
356 $pdf->MultiCell($this->posxgap - $this->posxqtyview - 1, 5,
'', 1,
'R');
357 $pdf->SetXY($this->posxgap, $curY - 1);
358 $pdf->MultiCell($this->posxend - $this->posxgap, 5,
'', 1,
'R');
361 $nexY = $curY + ($recorded ? 5 : 6);
365 $pdf->SetFont(
'',
'B', $default_font_size - 1);
367 if ($curY > ($this->page_hauteur - ($heightforfooter + $heightforinfotot + $heightforsignature))) {
371 $curY = $tab_top_newpage;
373 $pdf->SetXY($this->posxproduct, $curY);
374 $pdf->MultiCell(60, 4, $outputlangs->transnoentities(
"NbOfLines").
' : '.$nblines, 0,
'L');
376 $pdf->SetXY($this->posxproduct, $curY + 5);
377 $pdf->MultiCell(120, 4, $outputlangs->transnoentities(
"Difference").
' : '.($totalgap > 0 ?
'+' :
'').
price2num($totalgap,
'MS').
' ('.$nbgap.
' '.$outputlangs->transnoentities(
"Lines").
')', 0,
'L');
378 $pdf->SetXY($this->posxproduct, $curY + 10);
379 $pdf->MultiCell(120, 4, $outputlangs->transnoentities(
"Valuation").
' : '.
price(
price2num($totalgapvalue,
'MT'), 0, $outputlangs, 1, -1, -1,
$conf->currency), 0,
'L');
385 $pdf->SetFont(
'',
'', $default_font_size - 1);
386 $pdf->SetXY($this->posxproduct, $posy);
387 $pdf->MultiCell(80, 4, $outputlangs->transnoentities(
"CountedBy").
' : ____________________', 0,
'L');
388 $pdf->SetXY($this->page_largeur - $this->marge_droite - 80, $posy);
389 $pdf->MultiCell(80, 4, $outputlangs->transnoentities(
"DateAndSignature").
' :', 0,
'L');
390 $pdf->Rect($this->page_largeur - $this->marge_droite - 80, $posy + 5, 80, 15);
395 if (method_exists($pdf,
'AliasNbPages')) {
396 $pdf->AliasNbPages();
401 $pdf->Output($file,
'F');
404 $hookmanager->initHooks(array(
'pdfgeneration'));
405 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
407 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
409 $this->error = $hookmanager->error;
410 $this->errors = $hookmanager->errors;
415 $this->result = array(
'fullpath' => $file);
429 protected function tableHeader(&$pdf, $tab_top, $outputlangs, $recorded)
435 $pdf->SetFont(
'',
'B', $default_font_size - 2);
436 $pdf->SetDrawColor(128, 128, 128);
437 $pdf->line($this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_droite, $tab_top);
439 $posy = $tab_top + 1;
440 $pdf->SetXY($this->posxproduct, $posy);
441 $pdf->MultiCell($this->posxlabel - $this->posxproduct - 1, 3, $outputlangs->transnoentities(
"ProductRef"), 0,
'L');
442 $pdf->SetXY($this->posxlabel, $posy);
443 $pdf->MultiCell($this->posxwarehouse - $this->posxlabel - 1, 3, $outputlangs->transnoentities(
"Label"), 0,
'L');
444 $pdf->SetXY($this->posxwarehouse, $posy);
445 $pdf->MultiCell($this->posxbatch - $this->posxwarehouse - 1, 3, $outputlangs->transnoentities(
"Warehouse"), 0,
'L');
446 $pdf->SetXY($this->posxbatch, $posy);
447 $pdf->MultiCell($this->posxqtystock - $this->posxbatch - 1, 3, $outputlangs->transnoentities(
"Batch"), 0,
'L');
448 $pdf->SetXY($this->posxqtystock, $posy);
449 $pdf->MultiCell($this->posxqtyview - $this->posxqtystock - 1, 3, $outputlangs->transnoentities(
"ExpectedQty"), 0,
'R');
450 $pdf->SetXY($this->posxqtyview, $posy);
451 $pdf->MultiCell($this->posxgap - $this->posxqtyview - 1, 3, $outputlangs->transnoentities(
"RealQty"), 0,
'R');
452 $pdf->SetXY($this->posxgap, $posy);
453 $pdf->MultiCell($this->posxend - $this->posxgap, 3, $outputlangs->transnoentities(
"Difference"), 0,
'R');
455 $pdf->line($this->marge_gauche, $tab_top + 6, $this->page_largeur - $this->marge_droite, $tab_top + 6);
469 protected function pageHead(&$pdf,
$object, $showaddress, $outputlangs, $recorded, $isspecimen)
477 $pdf->SetTextColor(0, 0, 60);
478 $pdf->SetFont(
'',
'B', $default_font_size + 3);
480 $posy = $this->marge_haute;
481 $posx = $this->page_largeur - $this->marge_droite - 100;
483 $pdf->SetXY($this->marge_gauche, $posy);
486 $logo =
$conf->mycompany->dir_output.
'/logos/'.
$mysoc->logo;
488 if (is_readable($logo)) {
490 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
492 $pdf->SetTextColor(200, 0, 0);
493 $pdf->SetFont(
'',
'B', $default_font_size - 2);
494 $pdf->MultiCell(100, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
495 $pdf->SetTextColor(0, 0, 60);
498 $pdf->MultiCell(100, 4, $outputlangs->convToOutputCharset(
$mysoc->name), 0,
'L');
501 $pdf->SetFont(
'',
'B', $default_font_size + 3);
502 $pdf->SetXY($posx, $posy);
503 $pdf->SetTextColor(0, 0, 60);
504 $title = $outputlangs->transnoentities(
"Inventory");
506 $title .=
' - '.$outputlangs->transnoentities(
"Draft");
508 $pdf->MultiCell(100, 4, $title,
'',
'R');
510 $pdf->SetFont(
'',
'B', $default_font_size);
512 $pdf->SetXY($posx, $posy);
513 $pdf->SetTextColor(0, 0, 60);
514 $pdf->MultiCell(100, 4, $outputlangs->transnoentities(
"Ref").
" : ".($isspecimen ?
'SPECIMEN' : $outputlangs->convToOutputCharset(
$object->ref)),
'',
'R');
516 $pdf->SetFont(
'',
'', $default_font_size - 1);
518 if (!$isspecimen && !empty(
$object->date_inventory)) {
520 $pdf->SetXY($posx, $posy);
521 $pdf->SetTextColor(0, 0, 60);
522 $pdf->MultiCell(100, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date($this->db->jdate(
$object->date_inventory),
"day",
false, $outputlangs,
true),
'',
'R');
524 if (!$isspecimen && !empty(
$object->title)) {
526 $pdf->SetXY($posx, $posy);
527 $pdf->SetTextColor(0, 0, 60);
528 $pdf->MultiCell(100, 3, $outputlangs->convToOutputCharset(
$object->title),
'',
'R');
531 $pdf->SetTextColor(0, 0, 0);