From f205825f047928bec34a7e09ad8818a5eb2127d4 Mon Sep 17 00:00:00 2001 From: "vincent@cubedesigners.com" Date: Tue, 17 Oct 2023 13:16:32 +0000 Subject: [PATCH] wait #5615 @0.25 --- .../Metier/class.extranet.document.php | 601 +++++++++--------- 1 file changed, 303 insertions(+), 298 deletions(-) diff --git a/inc/extranet/Metier/class.extranet.document.php b/inc/extranet/Metier/class.extranet.document.php index 4934bca74..3a7aa0c26 100644 --- a/inc/extranet/Metier/class.extranet.document.php +++ b/inc/extranet/Metier/class.extranet.document.php @@ -1,303 +1,308 @@ testPDF(); - $pages = $pdf['pages']; - $cesures = $pdf['cesures']; - if ($this->isFacture() && $pages > 1) { - $pages = 1; - $displayDescription = false; - } else { - $displayDescription = true; - } - - $template = ROOT . '/images/extranet/docs/'; - - - $this->euro = chr(128); - - $this->pdf = new FPDI(); - $this->pdf->SetAutoPageBreak(false); - $this->pdf->setSourceFile($template . 'template.pdf'); - - $this->pdf->AddFont('KlavikaLight', '', 'klavika-light.php'); - $this->pdf->AddFont('KlavikaLight', 'B', 'klavika-medium.php'); - $this->pdf->AddFont('Klavika', '', 'klavika-regular.php'); - $this->pdf->AddFont('Klavika', 'B', 'klavika-bold.php'); - - $this->getBackground(); - - $page = 1; - - if ($pages == 1) { - $this->addPage($this->fond); - } else { - $this->addPage($this->fond1); - } - - $this->pdf->SetTextColor(0, 0, 0); - - // Bloc adresse - if (trim($this->adresse) == '') { - $client = self::escape($this->client->rs); - $adresse = self::escape(trim($this->client->adresse) . "\n" . $this->client->code_postal . " " . $this->client->ville . "\n" . cubeCountry::getCountry($this->client->pays)); - } else { - $l = explode("\n", trim($this->adresse)); - $l0 = array_shift($l); - - $client = self::escape($l0); - $adresse = self::escape(implode("\n", $l)); - } - $this->pdf->SetXY(114, 45); - $this->pdf->SetFont('KlavikaLight', 'B', 12); - $this->pdf->MultiCell(80, 8, $client, 0, 2); - $this->pdf->SetX(114); - $this->pdf->SetFont('KlavikaLight', '', 12); - $this->pdf->MultiCell(80, 6, $adresse); - // Bloc TVA Intra - if ($this instanceof extranetFacture) { - $texte_tva = ''; - if ($this->client->tva_intra != '' && cubeCountry::inUE($this->client->pays)) { - $texte_tva .= __('N° de TVA Intracommunautaire') . ' : ' . $this->client->tva_intra . "\n"; - } - if ($this->texte_complementaire != '') { - $texte_tva .= $this->texte_complementaire; - } - if ($texte_tva != '') { - $this->pdf->SetFont('KlavikaLight', '', 8); - $this->pdf->setXY(114, 90); - $this->pdf->MultiCell(80, 4, self::escape(trim($texte_tva))); - } - } - // Bloc titre - $this->pdf->SetXY(6, 60); - $titre = $this->documentTitre($this->client->lang); - $this->pdf->SetFont('KlavikaLight', 'B', 15); - $this->pdf->MultiCell(73, 4, self::escape($titre), 0, 'L'); - - $this->pdf->SetXY(6, $this->pdf->GetY() + 3); - $this->pdf->SetFont('KlavikaLight', '', 11); - $titre = ''; - if ($this->client->lang == 'fr') { - $titre .= strftime('%d %B %Y', $this->date_creation); - } else { - $titre .= date('F jS Y'); - } - $titre .= "\n\n" . $this->nom; - $this->pdf->MultiCell(73, 4, self::escape($titre), 0, 'L'); - - // blocs - $this->pdf->setY(115); - foreach ($this->lignes as $i => $ligne) { - if ($pages > 1 && $i == $cesures[$page]) { - $page++; - - if ($page == $pages) { - $tpl = $this->fond3; - } else { - $tpl = $this->fond2; - } - - $this->addPage($tpl); - $this->pdf->setY(25); - } - - $y = $this->pdf->getY() + 4; - - $this->pdf->SetFont('Klavika', '', 10); - // Prix - $this->pdf->SetXY(170, $y); - $ligne['montant'] = trim($ligne['montant']); - if (is_numeric($ligne['montant'])) { - $this->pdf->Cell(33, 4, cubePrices::formatAmount($ligne['montant'], $this->client->lang) . ' ' . $this->euro, 0, 0, 'R'); - } else { - $this->pdf->Cell(33, 4, self::escape($ligne['montant']), 0, 0, 'R'); - } - // Titre - $size=10; - if(strpos($ligne['titre'],'__')===0){ - $size=13; +abstract class extranetDocument extends cubeMetier +{ + + protected $lignes; + protected $nom; + protected $total_ht; + protected $createur_id; + protected $date_creation; + protected $status; + protected $client_id; + protected $fond; + protected $fond1; + protected $fond2; + protected $fond3; + protected $adresse; + // Attributs composés + protected $createur; + protected $client; + protected $pdf; + protected $euro; + + public function saveAsPDF() + { + $pdf = $this->testPDF(); + $pages = $pdf['pages']; + $cesures = $pdf['cesures']; + if ($this->isFacture() && $pages > 1) { + $pages = 1; + $displayDescription = false; + } else { + $displayDescription = true; + } + + $template = ROOT . '/images/extranet/docs/'; + + + $this->euro = chr(128); + + $this->pdf = new FPDI(); + $this->pdf->SetAutoPageBreak(false); + $this->pdf->setSourceFile($template . 'template.pdf'); + + $this->pdf->AddFont('KlavikaLight', '', 'klavika-light.php'); + $this->pdf->AddFont('KlavikaLight', 'B', 'klavika-medium.php'); + $this->pdf->AddFont('Klavika', '', 'klavika-regular.php'); + $this->pdf->AddFont('Klavika', 'B', 'klavika-bold.php'); + + $this->getBackground(); + + $page = 1; + + if ($pages == 1) { + $this->addPage($this->fond); + } else { + $this->addPage($this->fond1); + } + + $this->pdf->SetTextColor(0, 0, 0); + + // Bloc adresse + if (trim($this->adresse) == '') { + $client = self::escape($this->client->rs); + $adresse = self::escape(trim($this->client->adresse) . "\n" . $this->client->code_postal . " " . $this->client->ville . "\n" . cubeCountry::getCountry($this->client->pays)); + } else { + $l = explode("\n", trim($this->adresse)); + $l0 = array_shift($l); + + $client = self::escape($l0); + $adresse = self::escape(implode("\n", $l)); + } + $this->pdf->SetXY(114, 45); + $this->pdf->SetFont('KlavikaLight', 'B', 12); + $this->pdf->MultiCell(80, 8, $client, 0, 2); + $this->pdf->SetX(114); + $this->pdf->SetFont('KlavikaLight', '', 12); + $this->pdf->MultiCell(80, 6, $adresse); + // Bloc TVA Intra + if ($this instanceof extranetFacture) { + $texte_tva = ''; + if ($this->client->tva_intra != '' && cubeCountry::inUE($this->client->pays)) { + $texte_tva .= __('N° de TVA Intracommunautaire') . ' : ' . $this->client->tva_intra . "\n"; } + if ($this->texte_complementaire != '') { + $texte_tva .= $this->texte_complementaire; + } + if ($texte_tva != '') { + $this->pdf->SetFont('KlavikaLight', '', 8); + $this->pdf->setXY(114, 90); + $this->pdf->MultiCell(80, 4, self::escape(trim($texte_tva))); + } + } + // Bloc titre + $this->pdf->SetXY(6, 60); + $titre = $this->documentTitre($this->client->lang); + $this->pdf->SetFont('KlavikaLight', 'B', 15); + $this->pdf->MultiCell(73, 4, self::escape($titre), 0, 'L'); + + $this->pdf->SetXY(6, $this->pdf->GetY() + 3); + $this->pdf->SetFont('KlavikaLight', '', 11); + $titre = ''; + if ($this->client->lang == 'fr') { + $titre .= date('d/m/Y', $this->date_creation); + } else { + $titre .= date('F jS Y'); + } + $titre .= "\n\n" . $this->nom; + $this->pdf->MultiCell(73, 4, self::escape($titre), 0, 'L'); + + // blocs + $this->pdf->setY(115); + foreach ($this->lignes as $i => $ligne) { + if ($pages > 1 && $i == $cesures[$page]) { + $page++; + + if ($page == $pages) { + $tpl = $this->fond3; + } else { + $tpl = $this->fond2; + } + + $this->addPage($tpl); + $this->pdf->setY(25); + } + + $y = $this->pdf->getY() + 4; + + $this->pdf->SetFont('Klavika', '', 10); + // Prix + $this->pdf->SetXY(170, $y); + $ligne['montant'] = trim($ligne['montant']); + if (is_numeric($ligne['montant'])) { + $this->pdf->Cell(33, 4, cubePrices::formatAmount($ligne['montant'], $this->client->lang) . ' ' . $this->euro, 0, 0, 'R'); + } else { + $this->pdf->Cell(33, 4, self::escape($ligne['montant']), 0, 0, 'R'); + } + // Titre + $size = 10; + if (strpos($ligne['titre'], '__') === 0) { + $size = 13; + } + + $this->pdf->SetFont('Klavika', 'B', $size); + $this->pdf->setXY(6, $y); + $this->pdf->Cell(171, 4, self::escape(trim($ligne['titre'], " \t\n\r\0\x0B_")), 0, 1); + // Contenu + if ($displayDescription) { + $this->pdf->SetXY(6, $this->pdf->getY() + 1); + $this->pdf->SetFont('Klavika', '', 10); + $this->pdf->MultiCell(171, 4, self::escape($ligne['description'])); + } + } + // Prix + $tva = cubeTaxes::tva($this->client->pays, $this->date_creation); + if (!$tva) { + $tva_text = 'EXPORT'; + } elseif ($tva == 'UE') { + $tva = 0; + $tva_text = 'EXPORT * '; + $notes = ' * ' . __("Conformément à l'article 242 du Code Général des Impôts"); + } else { + $tva_text = cubePrices::formatAmount($tva * 100, $this->client->lang, 1) . ' %'; + } + $ttc = cubePrices::HTtoTTC($this->total_ht, $tva, 2); + $ttcf = cubePrices::formatAmount($ttc, $this->client->lang); + $taxes = ($tva > 0) ? cubePrices::formatAmount($ttc - $this->total_ht, $this->client->lang, 2) : ''; + // Affichage du montant hors taxes + $this->pdf->setFont('KlavikaLight', 'B', 10); + if ($pages == 1) { + //$this->pdf->SetXY(170, 210); + $this->pdf->SetXY(170, 218.5); + $space = 9.5; + } else { + $this->pdf->SetXY(170, 218); + $space = 9.7; + } + $this->pdf->Cell(32, $space, cubePrices::formatAmount($this->total_ht, $this->client->lang) . ' ' . $this->euro, 0, 1, 'R'); + $this->pdf->SetX(170); + $this->pdf->setFont('KlavikaLight', '', 10); + // Affichage du taux de tva et du montant des taxes + $this->pdf->Cell(32, $space, $tva_text, 0, 1, 'R'); + $this->pdf->SetX(170); + if ($tva) { + $this->pdf->Cell(32, $space, $taxes . ' ' . $this->euro, 0, 1, 'R'); + } else { + $this->pdf->Cell(32, $space, '-', 0, 1, 'R'); + } + $this->pdf->SetX(170); + + $this->pdf->SetTextColor(255, 255, 255); + $this->pdf->setFont('KlavikaLight', 'B', 10); + $this->pdf->Cell(32, $space + 1, $ttcf . ' ' . $this->euro, 0, 1, 'R'); + + if (isset($notes)) { + $this->pdf->setXY(8, 268); + $this->pdf->setTextColor(0, 0, 0); + $this->pdf->setFont('Klavika', '', 8); + $this->pdf->Cell(195, 0, self::escape($notes), 0, 1, 'R'); + } + return $this->pdf; + } + + public function addPage($page) + { + $templateIndex = $this->pdf->importPage($page); + $this->pdf->AddPage(); + $this->pdf->useTemplate($templateIndex); + } + + protected function testPDF() + { + $pdf = new FPDF('P', 'mm', array(210, 2000)); + $pdf->AddFont('KlavikaLight', '', 'klavika-light.php'); + $pdf->AddFont('KlavikaLight', 'B', 'klavika-medium.php'); + $pdf->AddFont('Klavika', '', 'klavika-regular.php'); + $pdf->AddFont('Klavika', 'B', 'klavika-bold.php'); + $pdf->AddPage(); + $pdf->SetAutoPageBreak(false); + + $lines = array(); + + $lastY = -5; + foreach ($this->lignes as $i => $ligne) { + $y = $pdf->getY() + 5; + + $pdf->SetFont('Klavika', 'B', 10); + // Prix + $pdf->SetXY(170, $y); + $ligne['montant'] = trim($ligne['montant']); + if (is_numeric($ligne['montant'])) { + $pdf->Cell(33, 4, cubePrices::formatAmount($ligne['montant'], $this->client->lang) . ' ' . $this->euro, 0, 0, 'R'); + } else { + $pdf->Cell(33, 4, self::escape($ligne['montant']), 0, 0, 'R'); + } + // Titre + $pdf->setXY(13, $y); + $pdf->Cell(151, 4, self::escape(trim($ligne['titre'])), 0, 1); + // Contenu + $pdf->SetX(13); + $pdf->SetFont('Klavika', '', 10); + $pdf->MultiCell(171, 4, self::escape(trim($ligne['description']))); + + $y = $pdf->getY(); + $lines[] = $y - $lastY; + $lastY = $y; + } + + $cesures = array(); + $h = $pdf->getY(); + if ($h <= 105) { + $pages = 1; + } elseif ($h <= 355) { + $pages = 2; + } else { + $pages = 2 + ceil(($h - 355) / 245); + } + + $lasty = 0; + $nb_lines = count($lines); + if ($pages > 1) { + $i = 0; + $y = 0; + for ($page = 1; $page <= $pages; $page++) { + if ($page == 1) { + $limit = 170; + } else if ($page == $pages) { + $limit = 10000; + } else { + $limit = 245; + } + for (; $i <= $nb_lines; $i++) { + $y += $lines[$i]; + if (($y - $lasty) > $limit) { + $cesures[$page] = $i; + $y = $lines[$i]; + $lasty = $y; + continue 2; + } + if ($i >= $nb_lines) { + break; + } + } + } + } + + $pages = max(count($cesures), $pages); + + if (!isset($cesures[$pages - 1])) { + $cesures[$pages - 1] = $nb_lines - 1; + } + + + $res = array('pages' => $pages, 'cesures' => $cesures); + + return $res; + } + + protected function escape($str) + { + $str = cubeText::removeOddStuff($str); + $str = utf8_decode($str); + $str = str_replace(utf8_decode('€'), $this->euro, $str); + return $str; + } - $this->pdf->SetFont('Klavika', 'B', $size); - $this->pdf->setXY(6, $y); - $this->pdf->Cell(171, 4, self::escape(trim($ligne['titre']," \t\n\r\0\x0B_")), 0, 1); - // Contenu - if ($displayDescription) { - $this->pdf->SetXY(6, $this->pdf->getY() + 1); - $this->pdf->SetFont('Klavika', '', 10); - $this->pdf->MultiCell(171, 4, self::escape($ligne['description'])); - } - } - // Prix - $tva = cubeTaxes::tva($this->client->pays, $this->date_creation); - if (!$tva) { - $tva_text = 'EXPORT'; - } elseif ($tva == 'UE') { - $tva = 0; - $tva_text = 'EXPORT *'; - $notes = '* ' . __("Conformément à l'article 242 du Code Général des Impôts"); - } else { - $tva_text = cubePrices::formatAmount($tva * 100, $this->client->lang, 1) . ' %'; - } - $ttc = cubePrices::HTtoTTC($this->total_ht, $tva, 2); - $ttcf = cubePrices::formatAmount($ttc, $this->client->lang); - $taxes = ($tva > 0) ? cubePrices::formatAmount($ttc - $this->total_ht, $this->client->lang, 2) : ''; - // Affichage du montant hors taxes - $this->pdf->setFont('KlavikaLight', 'B', 10); - if ($pages == 1) { - //$this->pdf->SetXY(170, 210); - $this->pdf->SetXY(170, 218.5); - $space = 9.5; - } else { - $this->pdf->SetXY(170, 218); - $space = 9.7; - } - $this->pdf->Cell(32, $space, cubePrices::formatAmount($this->total_ht, $this->client->lang) . ' ' . $this->euro, 0, 1, 'R'); - $this->pdf->SetX(170); - $this->pdf->setFont('KlavikaLight', '', 10); - // Affichage du taux de tva et du montant des taxes - $this->pdf->Cell(32, $space, $tva_text, 0, 1, 'R'); - $this->pdf->SetX(170); - if ($tva) { - $this->pdf->Cell(32, $space, $taxes . ' ' . $this->euro, 0, 1, 'R'); - } else { - $this->pdf->Cell(32, $space, '-', 0, 1, 'R'); - } - $this->pdf->SetX(170); - - $this->pdf->SetTextColor(255, 255, 255); - $this->pdf->setFont('KlavikaLight', 'B', 10); - $this->pdf->Cell(32, $space + 1, $ttcf . ' ' . $this->euro, 0, 1, 'R'); - - if (isset($notes)) { - $this->pdf->setXY(8, 268); - $this->pdf->setTextColor(0, 0, 0); - $this->pdf->setFont('Klavika', '', 8); - $this->pdf->Cell(195, 0, self::escape($notes), 0, 1, 'R'); - } - return $this->pdf; - } - - public function addPage($page) { - $templateIndex = $this->pdf->importPage($page); - $this->pdf->AddPage(); - $this->pdf->useTemplate($templateIndex); - } - - protected function testPDF() { - $pdf = new FPDF('P', 'mm', array(210, 2000)); - $pdf->AddFont('KlavikaLight', '', 'klavika-light.php'); - $pdf->AddFont('KlavikaLight', 'B', 'klavika-medium.php'); - $pdf->AddFont('Klavika', '', 'klavika-regular.php'); - $pdf->AddFont('Klavika', 'B', 'klavika-bold.php'); - $pdf->AddPage(); - $pdf->SetAutoPageBreak(false); - - $lines = array(); - - $lastY = -5; - foreach ($this->lignes as $i => $ligne) { - $y = $pdf->getY() + 5; - - $pdf->SetFont('Klavika', 'B', 10); - // Prix - $pdf->SetXY(170, $y); - $ligne['montant'] = trim($ligne['montant']); - if (is_numeric($ligne['montant'])) { - $pdf->Cell(33, 4, cubePrices::formatAmount($ligne['montant'], $this->client->lang) . ' ' . $this->euro, 0, 0, 'R'); - } else { - $pdf->Cell(33, 4, self::escape($ligne['montant']), 0, 0, 'R'); - } - // Titre - $pdf->setXY(13, $y); - $pdf->Cell(151, 4, self::escape(trim($ligne['titre'])), 0, 1); - // Contenu - $pdf->SetX(13); - $pdf->SetFont('Klavika', '', 10); - $pdf->MultiCell(171, 4, self::escape(trim($ligne['description']))); - - $y = $pdf->getY(); - $lines[] = $y - $lastY; - $lastY = $y; - } - - $cesures = array(); - $h = $pdf->getY(); - if ($h <= 105) { - $pages = 1; - } elseif ($h <= 355) { - $pages = 2; - } else { - $pages = 2 + ceil(($h - 355) / 245); - } - - $lasty = 0; - $nb_lines = count($lines); - if ($pages > 1) { - $i = 0; - $y = 0; - for ($page = 1; $page <= $pages; $page++) { - if ($page == 1) { - $limit = 170; - } else if ($page == $pages) { - $limit = 10000; - } else { - $limit = 245; - } - for (; $i <= $nb_lines; $i++) { - $y += $lines[$i]; - if (($y - $lasty) > $limit) { - $cesures[$page] = $i; - $y = $lines[$i]; - $lasty = $y; - continue 2; - } - if ($i >= $nb_lines) { - break; - } - } - } - } - - $pages = max(count($cesures), $pages); - - if (!isset($cesures[$pages - 1])) { - $cesures[$pages - 1] = $nb_lines - 1; - } - - - $res = array('pages' => $pages, 'cesures' => $cesures); - - return $res; - } - - protected function escape($str) { - $str = cubeText::removeOddStuff($str); - $str = utf8_decode($str); - $str = str_replace(utf8_decode('€'), $this->euro, $str); - return $str; - } - - abstract protected function documentTitre(); + abstract protected function documentTitre(); } -- 2.39.5