pdf_crabe.modules.php 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. <?php
  2. /* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
  5. * Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  7. * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
  8. * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  9. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  10. * Copyright (C) 2017-2018 Ferran Marcet <fmarcet@2byte.es>
  11. * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
  12. * Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.fr>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. * or see https://www.gnu.org/
  27. */
  28. /**
  29. * \file htdocs/core/modules/facture/doc/pdf_crabe.modules.php
  30. * \ingroup facture
  31. * \brief File of class to generate customers invoices from crabe model
  32. */
  33. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  34. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  38. /**
  39. * Class to generate the customer invoice PDF with template Crabe
  40. */
  41. class pdf_crabe extends ModelePDFFactures
  42. {
  43. /**
  44. * @var DoliDb Database handler
  45. */
  46. public $db;
  47. /**
  48. * @var string model name
  49. */
  50. public $name;
  51. /**
  52. * @var string model description (short text)
  53. */
  54. public $description;
  55. /**
  56. * @var int Save the name of generated file as the main doc when generating a doc with this template
  57. */
  58. public $update_main_doc_field;
  59. /**
  60. * @var string document type
  61. */
  62. public $type;
  63. /**
  64. * @var array Minimum version of PHP required by module.
  65. * e.g.: PHP ≥ 7.0 = array(7, 0)
  66. */
  67. public $phpmin = array(7, 0);
  68. /**
  69. * Dolibarr version of the loaded document
  70. * @var string
  71. */
  72. public $version = 'dolibarr';
  73. /**
  74. * @var int page_largeur
  75. */
  76. public $page_largeur;
  77. /**
  78. * @var int page_hauteur
  79. */
  80. public $page_hauteur;
  81. /**
  82. * @var array format
  83. */
  84. public $format;
  85. /**
  86. * @var int marge_gauche
  87. */
  88. public $marge_gauche;
  89. /**
  90. * @var int marge_droite
  91. */
  92. public $marge_droite;
  93. /**
  94. * @var int marge_haute
  95. */
  96. public $marge_haute;
  97. /**
  98. * @var int marge_basse
  99. */
  100. public $marge_basse;
  101. /**
  102. * Issuer
  103. * @var Societe Object that emits
  104. */
  105. public $emetteur;
  106. /**
  107. * @var bool Situation invoice type
  108. */
  109. public $situationinvoice;
  110. /**
  111. * @var float X position for the situation progress column
  112. */
  113. public $posxprogress;
  114. /**
  115. * @var int Category of operation
  116. */
  117. public $categoryOfOperation = -1; // unknown by default
  118. /**
  119. * Constructor
  120. *
  121. * @param DoliDB $db Database handler
  122. */
  123. public function __construct($db)
  124. {
  125. global $conf, $langs, $mysoc;
  126. // Translations
  127. $langs->loadLangs(array("main", "bills"));
  128. $this->db = $db;
  129. $this->name = "crabe";
  130. $this->description = $langs->trans('PDFCrabeDescription');
  131. $this->update_main_doc_field = 1; // Save the name of generated file as the main doc when generating a doc with this template
  132. // Dimension page
  133. $this->type = 'pdf';
  134. $formatarray = pdf_getFormat();
  135. $this->page_largeur = $formatarray['width'];
  136. $this->page_hauteur = $formatarray['height'];
  137. $this->format = array($this->page_largeur, $this->page_hauteur);
  138. $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
  139. $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
  140. $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
  141. $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
  142. $this->option_logo = 1; // Display logo
  143. $this->option_tva = 1; // Manage the vat option FACTURE_TVAOPTION
  144. $this->option_modereg = 1; // Display payment mode
  145. $this->option_condreg = 1; // Display payment terms
  146. $this->option_multilang = 1; // Available in several languages
  147. $this->option_escompte = 1; // Displays if there has been a discount
  148. $this->option_credit_note = 1; // Support credit notes
  149. $this->option_freetext = 1; // Support add of a personalised text
  150. $this->option_draft_watermark = 1; // Support add of a watermark on drafts
  151. $this->watermark = '';
  152. // Get source company
  153. $this->emetteur = $mysoc;
  154. if (empty($this->emetteur->country_code)) {
  155. $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default, if was not defined
  156. }
  157. // Define position of columns
  158. $this->posxdesc = $this->marge_gauche + 1;
  159. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  160. $this->posxtva = 101;
  161. $this->posxup = 118;
  162. $this->posxqty = 135;
  163. $this->posxunit = 151;
  164. } else {
  165. $this->posxtva = 110;
  166. $this->posxup = 126;
  167. $this->posxqty = 145;
  168. $this->posxunit = 162;
  169. }
  170. $this->posxprogress = 151; // Only displayed for situation invoices
  171. $this->posxdiscount = 162;
  172. $this->posxprogress = 174;
  173. $this->postotalht = 174;
  174. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT) || !empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT_COLUMN)) {
  175. $this->posxtva = $this->posxup;
  176. }
  177. $this->posxpicture = $this->posxtva - (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH) ? 20 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH); // width of images
  178. if ($this->page_largeur < 210) { // To work with US executive format
  179. $this->posxpicture -= 20;
  180. $this->posxtva -= 20;
  181. $this->posxup -= 20;
  182. $this->posxqty -= 20;
  183. $this->posxunit -= 20;
  184. $this->posxdiscount -= 20;
  185. $this->posxprogress -= 20;
  186. $this->postotalht -= 20;
  187. }
  188. $this->tva = array();
  189. $this->tva_array = array();
  190. $this->localtax1 = array();
  191. $this->localtax2 = array();
  192. $this->atleastoneratenotnull = 0;
  193. $this->atleastonediscount = 0;
  194. $this->situationinvoice = false;
  195. }
  196. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  197. /**
  198. * Function to build pdf onto disk
  199. *
  200. * @param Facture $object Object to generate
  201. * @param Translate $outputlangs Lang output object
  202. * @param string $srctemplatepath Full path of source filename for generator using a template file
  203. * @param int $hidedetails Do not show line details
  204. * @param int $hidedesc Do not show desc
  205. * @param int $hideref Do not show ref
  206. * @return int 1=OK, 0=KO
  207. */
  208. public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  209. {
  210. // phpcs:enable
  211. global $user, $langs, $conf, $mysoc, $hookmanager, $nblines;
  212. dol_syslog("write_file outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang : 'null'));
  213. if (!is_object($outputlangs)) {
  214. $outputlangs = $langs;
  215. }
  216. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  217. if (!empty($conf->global->MAIN_USE_FPDF)) {
  218. $outputlangs->charset_output = 'ISO-8859-1';
  219. }
  220. // Load translation files required by the page
  221. $outputlangs->loadLangs(array("main", "bills", "products", "dict", "companies"));
  222. // Show Draft Watermark
  223. if ($object->statut == $object::STATUS_DRAFT && (!empty($conf->global->FACTURE_DRAFT_WATERMARK))) {
  224. $this->watermark = $conf->global->FACTURE_DRAFT_WATERMARK;
  225. }
  226. global $outputlangsbis;
  227. $outputlangsbis = null;
  228. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
  229. $outputlangsbis = new Translate('', $conf);
  230. $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
  231. $outputlangsbis->loadLangs(array("main", "bills", "products", "dict", "companies"));
  232. }
  233. $nblines = count($object->lines);
  234. // Loop on each lines to detect if there is at least one image to show
  235. $realpatharray = array();
  236. if (!empty($conf->global->MAIN_GENERATE_INVOICES_WITH_PICTURE)) {
  237. for ($i = 0; $i < $nblines; $i++) {
  238. if (empty($object->lines[$i]->fk_product)) {
  239. continue;
  240. }
  241. $objphoto = new Product($this->db);
  242. $objphoto->fetch($object->lines[$i]->fk_product);
  243. $pdir = get_exdir($object->lines[$i]->fk_product, 2, 0, 0, $objphoto, 'product').$object->lines[$i]->fk_product."/photos/";
  244. $dir = $conf->product->dir_output.'/'.$pdir;
  245. $realpath = '';
  246. foreach ($objphoto->liste_photos($dir, 1) as $key => $obj) {
  247. $filename = $obj['photo'];
  248. //if ($obj['photo_vignette']) $filename='thumbs/'.$obj['photo_vignette'];
  249. $realpath = $dir.$filename;
  250. break;
  251. }
  252. if ($realpath) {
  253. $realpatharray[$i] = $realpath;
  254. }
  255. }
  256. }
  257. if (count($realpatharray) == 0) {
  258. $this->posxpicture = $this->posxtva;
  259. }
  260. if ($conf->facture->dir_output) {
  261. $object->fetch_thirdparty();
  262. $deja_regle = $object->getSommePaiement((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? 1 : 0);
  263. $amount_credit_notes_included = $object->getSumCreditNotesUsed((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? 1 : 0);
  264. $amount_deposits_included = $object->getSumDepositsUsed((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? 1 : 0);
  265. // Definition of $dir and $file
  266. if ($object->specimen) {
  267. $dir = empty($conf->facture->multidir_output[$conf->entity]) ? $conf->facture->dir_output : $conf->facture->multidir_output[$conf->entity];
  268. $file = $dir."/SPECIMEN.pdf";
  269. } else {
  270. $objectref = dol_sanitizeFileName($object->ref);
  271. $dir = (empty($conf->facture->multidir_output[$conf->entity]) ? $conf->facture->dir_output : $conf->facture->multidir_output[$conf->entity])."/".$objectref;
  272. $file = $dir."/".$objectref.".pdf";
  273. }
  274. if (!file_exists($dir)) {
  275. if (dol_mkdir($dir) < 0) {
  276. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  277. return 0;
  278. }
  279. }
  280. if (file_exists($dir)) {
  281. // Add pdfgeneration hook
  282. if (!is_object($hookmanager)) {
  283. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  284. $hookmanager = new HookManager($this->db);
  285. }
  286. $hookmanager->initHooks(array('pdfgeneration'));
  287. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  288. global $action;
  289. $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  290. // Set nblines with the new facture lines content after hook
  291. $nblines = count($object->lines);
  292. $nbpayments = count($object->getListOfPayments());
  293. // Create pdf instance
  294. $pdf = pdf_getInstance($this->format);
  295. $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance
  296. $pdf->SetAutoPageBreak(1, 0);
  297. $heightforinfotot = 50 + (4 * $nbpayments); // Height reserved to output the info and total part and payment part
  298. if ($heightforinfotot > 220) {
  299. $heightforinfotot = 220;
  300. }
  301. $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5); // Height reserved to output the free text on last page
  302. $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
  303. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
  304. $heightforfooter += 6;
  305. }
  306. if (class_exists('TCPDF')) {
  307. $pdf->setPrintHeader(false);
  308. $pdf->setPrintFooter(false);
  309. }
  310. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  311. // Set path to the background PDF File
  312. if (!empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) {
  313. $logodir = $conf->mycompany->dir_output;
  314. if (!empty($conf->mycompany->multidir_output[$object->entity])) {
  315. $logodir = $conf->mycompany->multidir_output[$object->entity];
  316. }
  317. $pagecount = $pdf->setSourceFile($logodir.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
  318. $tplidx = $pdf->importPage(1);
  319. }
  320. $pdf->Open();
  321. $pagenb = 0;
  322. $pdf->SetDrawColor(128, 128, 128);
  323. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  324. $pdf->SetSubject($outputlangs->transnoentities("PdfInvoiceTitle"));
  325. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  326. $pdf->SetAuthor($mysoc->name.($user->id > 0 ? ' - '.$outputlangs->convToOutputCharset($user->getFullName($outputlangs)) : ''));
  327. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("PdfInvoiceTitle")." ".$outputlangs->convToOutputCharset($object->thirdparty->name));
  328. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  329. $pdf->SetCompression(false);
  330. }
  331. // Set certificate
  332. $cert = empty($user->conf->CERTIFICATE_CRT) ? '' : $user->conf->CERTIFICATE_CRT;
  333. $certprivate = empty($user->conf->CERTIFICATE_CRT_PRIVATE) ? '' : $user->conf->CERTIFICATE_CRT_PRIVATE;
  334. // If user has no certificate, we try to take the company one
  335. if (!$cert) {
  336. $cert = empty($conf->global->CERTIFICATE_CRT) ? '' : $conf->global->CERTIFICATE_CRT;
  337. }
  338. if (!$certprivate) {
  339. $certprivate = empty($conf->global->CERTIFICATE_CRT_PRIVATE) ? '' : $conf->global->CERTIFICATE_CRT_PRIVATE;
  340. }
  341. // If a certificate is found
  342. if ($cert) {
  343. $info = array(
  344. 'Name' => $this->emetteur->name,
  345. 'Location' => getCountry($this->emetteur->country_code, 0),
  346. 'Reason' => 'INVOICE',
  347. 'ContactInfo' => $this->emetteur->email
  348. );
  349. $pdf->setSignature($cert, $certprivate, $this->emetteur->name, '', 2, $info);
  350. }
  351. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  352. // Set $this->atleastonediscount if you have at least one discount
  353. // and determine category of operation
  354. $categoryOfOperation = 0;
  355. $nbProduct = 0;
  356. $nbService = 0;
  357. for ($i = 0; $i < $nblines; $i++) {
  358. if ($object->lines[$i]->remise_percent) {
  359. $this->atleastonediscount++;
  360. }
  361. // determine category of operation
  362. if ($categoryOfOperation < 2) {
  363. $lineProductType = $object->lines[$i]->product_type;
  364. if ($lineProductType == Product::TYPE_PRODUCT) {
  365. $nbProduct++;
  366. } elseif ($lineProductType == Product::TYPE_SERVICE) {
  367. $nbService++;
  368. }
  369. if ($nbProduct > 0 && $nbService > 0) {
  370. // mixed products and services
  371. $categoryOfOperation = 2;
  372. }
  373. }
  374. }
  375. // determine category of operation
  376. if ($categoryOfOperation <= 0) {
  377. // only services
  378. if ($nbProduct == 0 && $nbService > 0) {
  379. $categoryOfOperation = 1;
  380. }
  381. }
  382. $this->categoryOfOperation = $categoryOfOperation;
  383. if (empty($this->atleastonediscount)) { // retrieve space not used by discount
  384. $delta = ($this->posxprogress - $this->posxdiscount);
  385. $this->posxpicture += $delta;
  386. $this->posxtva += $delta;
  387. $this->posxup += $delta;
  388. $this->posxqty += $delta;
  389. $this->posxunit += $delta;
  390. $this->posxdiscount += $delta;
  391. // post of fields after are not modified, stay at same position
  392. }
  393. $progress_width = 0;
  394. // Situation invoice handling
  395. if ($object->situation_cycle_ref && empty($conf->global->MAIN_PDF_HIDE_SITUATION)) {
  396. $this->situationinvoice = true;
  397. $progress_width = 10;
  398. $this->posxpicture -= $progress_width;
  399. $this->posxtva -= $progress_width;
  400. $this->posxup -= $progress_width;
  401. $this->posxqty -= $progress_width;
  402. $this->posxunit -= $progress_width;
  403. $this->posxdiscount -= $progress_width;
  404. $this->posxprogress -= $progress_width;
  405. }
  406. // New page
  407. $pdf->AddPage();
  408. if (!empty($tplidx)) {
  409. $pdf->useTemplate($tplidx);
  410. }
  411. $pagenb++;
  412. // Output header (logo, ref and address blocks). This is first call for first page.
  413. $top_shift = $this->_pagehead($pdf, $object, 1, $outputlangs);
  414. $pdf->SetFont('', '', $default_font_size - 1);
  415. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  416. $pdf->SetTextColor(0, 0, 0);
  417. // $pdf->GetY() here can't be used. It is bottom of the second addresse box but first one may be higher
  418. // $tab_top is y where we must continue content (90 = 42 + 48: 42 is height of logo and ref, 48 is address blocks)
  419. $tab_top = 90 + $top_shift; // top_shift is an addition for linked objects or addons (0 in most cases)
  420. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  421. // You can add more thing under header here, if you increase $extra_under_address_shift too.
  422. $extra_under_address_shift = 0;
  423. $qrcodestring = '';
  424. if (!empty($conf->global->INVOICE_ADD_ZATCA_QR_CODE)) {
  425. $qrcodestring = $object->buildZATCAQRString();
  426. } elseif (!empty($conf->global->INVOICE_ADD_SWISS_QR_CODE)) {
  427. $qrcodestring = $object->buildSwitzerlandQRString();
  428. }
  429. if ($qrcodestring) {
  430. $qrcodecolor = array('25', '25', '25');
  431. // set style for QR-code
  432. $styleQr = array(
  433. 'border' => false,
  434. 'padding' => 0,
  435. 'fgcolor' => $qrcodecolor,
  436. 'bgcolor' => false, //array(255,255,255)
  437. 'module_width' => 1, // width of a single module in points
  438. 'module_height' => 1 // height of a single module in points
  439. );
  440. $pdf->write2DBarcode($qrcodestring, 'QRCODE,M', $this->marge_gauche, $tab_top - 5, 25, 25, $styleQr, 'N');
  441. $extra_under_address_shift += 25;
  442. }
  443. // Call hook printUnderHeaderPDFline
  444. $parameters = array(
  445. 'object' => $object,
  446. 'i' => $i,
  447. 'pdf' =>& $pdf,
  448. 'outputlangs' => $outputlangs,
  449. 'hidedetails' => $hidedetails
  450. );
  451. $reshook = $hookmanager->executeHooks('printUnderHeaderPDFline', $parameters, $this); // Note that $object may have been modified by hook
  452. if (!empty($hookmanager->resArray['extra_under_address_shift'])) {
  453. $extra_under_address_shift += $hookmanager->resArray['extra_under_header_shift'];
  454. }
  455. $tab_top += $extra_under_address_shift;
  456. $tab_top_newpage += 0;
  457. // Incoterm
  458. $height_incoterms = 0;
  459. if (isModEnabled('incoterm')) {
  460. $desc_incoterms = $object->getIncotermsForPDF();
  461. if ($desc_incoterms) {
  462. $tab_top -= 2;
  463. $pdf->SetFont('', '', $default_font_size - 1);
  464. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1, dol_htmlentitiesbr($desc_incoterms), 0, 1);
  465. $nexY = $pdf->GetY();
  466. $height_incoterms = $nexY - $tab_top;
  467. // Rect takes a length in 3rd parameter
  468. $pdf->SetDrawColor(192, 192, 192);
  469. $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_incoterms + 1);
  470. $tab_top = $nexY + 6;
  471. }
  472. }
  473. // Display notes
  474. $notetoshow = empty($object->note_public) ? '' : $object->note_public;
  475. if (!empty($conf->global->MAIN_ADD_SALE_REP_SIGNATURE_IN_NOTE)) {
  476. // Get first sale rep
  477. if (is_object($object->thirdparty)) {
  478. $salereparray = $object->thirdparty->getSalesRepresentatives($user);
  479. $salerepobj = new User($this->db);
  480. $salerepobj->fetch($salereparray[0]['id']);
  481. if (!empty($salerepobj->signature)) {
  482. $notetoshow = dol_concatdesc($notetoshow, $salerepobj->signature);
  483. }
  484. }
  485. }
  486. // Extrafields in note
  487. $extranote = $this->getExtrafieldsInHtml($object, $outputlangs);
  488. if (!empty($extranote)) {
  489. $notetoshow = dol_concatdesc($notetoshow, $extranote);
  490. }
  491. if ($notetoshow) {
  492. $tab_top -= 2;
  493. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
  494. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  495. $notetoshow = make_substitutions($notetoshow, $substitutionarray, $outputlangs);
  496. $notetoshow = convertBackOfficeMediasLinksToPublicLinks($notetoshow);
  497. $pdf->SetFont('', '', $default_font_size - 1);
  498. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1, dol_htmlentitiesbr($notetoshow), 0, 1);
  499. $nexY = $pdf->GetY();
  500. $height_note = $nexY - $tab_top;
  501. // Rect takes a length in 3rd parameter
  502. $pdf->SetDrawColor(192, 192, 192);
  503. $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
  504. $tab_top = $nexY + 6;
  505. }
  506. $iniY = $tab_top + 7;
  507. $curY = $tab_top + 7;
  508. $nexY = $tab_top + 7;
  509. // Loop on each lines
  510. for ($i = 0; $i < $nblines; $i++) {
  511. $curY = $nexY;
  512. $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage
  513. $pdf->SetTextColor(0, 0, 0);
  514. // Define size of image if we need it
  515. $imglinesize = array();
  516. if (!empty($realpatharray[$i])) {
  517. $imglinesize = pdf_getSizeForImage($realpatharray[$i]);
  518. }
  519. $pdf->setTopMargin($tab_top_newpage);
  520. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext + $heightforinfotot); // The only function to edit the bottom margin of current page to set it.
  521. $pageposbefore = $pdf->getPage();
  522. $showpricebeforepagebreak = 1;
  523. $posYAfterImage = 0;
  524. $posYAfterDescription = 0;
  525. // We start with Photo of product line
  526. if (isset($imglinesize['width']) && isset($imglinesize['height']) && ($curY + $imglinesize['height']) > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) { // If photo too high, we moved completely on new page
  527. $pdf->AddPage('', '', true);
  528. if (!empty($tplidx)) {
  529. $pdf->useTemplate($tplidx);
  530. }
  531. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  532. $top_shift = $this->_pagehead($pdf, $object, 0, $outputlangs);
  533. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  534. }
  535. $pdf->setPage($pageposbefore + 1);
  536. $curY = $tab_top_newpage;
  537. // Allows data in the first page if description is long enough to break in multiples pages
  538. if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
  539. $showpricebeforepagebreak = 1;
  540. } else {
  541. $showpricebeforepagebreak = 0;
  542. }
  543. }
  544. if (isset($imglinesize['width']) && isset($imglinesize['height'])) {
  545. $curX = $this->posxpicture - 1;
  546. $pdf->Image($realpatharray[$i], $curX + (($this->posxtva - $this->posxpicture - $imglinesize['width']) / 2), $curY, $imglinesize['width'], $imglinesize['height'], '', '', '', 2, 300); // Use 300 dpi
  547. // $pdf->Image does not increase value return by getY, so we save it manually
  548. $posYAfterImage = $curY + $imglinesize['height'];
  549. }
  550. // Description of product line
  551. $curX = $this->posxdesc - 1;
  552. $pdf->startTransaction();
  553. pdf_writelinedesc($pdf, $object, $i, $outputlangs, $this->posxpicture - $curX - $progress_width, 3, $curX, $curY, $hideref, $hidedesc);
  554. $pageposafter = $pdf->getPage();
  555. if ($pageposafter > $pageposbefore) { // There is a pagebreak
  556. $pdf->rollbackTransaction(true);
  557. $pageposafter = $pageposbefore;
  558. //print $pageposafter.'-'.$pageposbefore;exit;
  559. $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it.
  560. pdf_writelinedesc($pdf, $object, $i, $outputlangs, $this->posxpicture - $curX - $progress_width, 3, $curX, $curY, $hideref, $hidedesc);
  561. $pageposafter = $pdf->getPage();
  562. $posyafter = $pdf->GetY();
  563. //var_dump($posyafter); var_dump(($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))); exit;
  564. if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) { // There is no space left for total+free text
  565. if ($i == ($nblines - 1)) { // No more lines, and no space left to show total, so we create a new page
  566. $pdf->AddPage('', '', true);
  567. if (!empty($tplidx)) {
  568. $pdf->useTemplate($tplidx);
  569. }
  570. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  571. $top_shift = $this->_pagehead($pdf, $object, 0, $outputlangs);
  572. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  573. }
  574. $pdf->setPage($pageposafter + 1);
  575. }
  576. } else {
  577. // We found a page break
  578. // Allows data in the first page if description is long enough to break in multiples pages
  579. if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
  580. $showpricebeforepagebreak = 1;
  581. } else {
  582. $showpricebeforepagebreak = 0;
  583. }
  584. }
  585. } else // No pagebreak
  586. {
  587. $pdf->commitTransaction();
  588. }
  589. $posYAfterDescription = $pdf->GetY();
  590. $nexY = $pdf->GetY();
  591. $pageposafter = $pdf->getPage();
  592. $pdf->setPage($pageposbefore);
  593. $pdf->setTopMargin($this->marge_haute);
  594. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  595. // We suppose that a too long description or photo were moved completely on next page
  596. if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
  597. $pdf->setPage($pageposafter);
  598. $curY = $tab_top_newpage;
  599. }
  600. $pdf->SetFont('', '', $default_font_size - 1); // We reposition the default font
  601. // VAT Rate
  602. if (empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT) && empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT_COLUMN)) {
  603. $vat_rate = pdf_getlinevatrate($object, $i, $outputlangs, $hidedetails);
  604. $pdf->SetXY($this->posxtva - 5, $curY);
  605. $pdf->MultiCell($this->posxup - $this->posxtva + 4, 3, $vat_rate, 0, 'R');
  606. }
  607. // Unit price before discount
  608. $up_excl_tax = pdf_getlineupexcltax($object, $i, $outputlangs, $hidedetails);
  609. $pdf->SetXY($this->posxup, $curY);
  610. $pdf->MultiCell($this->posxqty - $this->posxup - 0.8, 3, $up_excl_tax, 0, 'R', 0);
  611. // Quantity
  612. $qty = pdf_getlineqty($object, $i, $outputlangs, $hidedetails);
  613. $pdf->SetXY($this->posxqty, $curY);
  614. $pdf->MultiCell($this->posxunit - $this->posxqty - 0.8, 4, $qty, 0, 'R'); // Enough for 6 chars
  615. // Unit
  616. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  617. $unit = pdf_getlineunit($object, $i, $outputlangs, $hidedetails, $hookmanager);
  618. $pdf->SetXY($this->posxunit, $curY);
  619. $pdf->MultiCell($this->posxdiscount - $this->posxunit - 0.8, 4, $unit, 0, 'L');
  620. }
  621. // Discount on line
  622. if ($object->lines[$i]->remise_percent) {
  623. $pdf->SetXY($this->posxdiscount - 2, $curY);
  624. $remise_percent = pdf_getlineremisepercent($object, $i, $outputlangs, $hidedetails);
  625. $pdf->MultiCell($this->posxprogress - $this->posxdiscount + 2, 3, $remise_percent, 0, 'R');
  626. }
  627. // Situation progress
  628. if ($this->situationinvoice) {
  629. $progress = pdf_getlineprogress($object, $i, $outputlangs, $hidedetails);
  630. $pdf->SetXY($this->posxprogress, $curY);
  631. $pdf->MultiCell($this->postotalht - $this->posxprogress + 1, 3, $progress, 0, 'R');
  632. }
  633. // Total HT line
  634. $total_excl_tax = pdf_getlinetotalexcltax($object, $i, $outputlangs, $hidedetails);
  635. $pdf->SetXY($this->postotalht, $curY);
  636. $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->postotalht, 3, $total_excl_tax, 0, 'R', 0);
  637. $sign = 1;
  638. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  639. $sign = -1;
  640. }
  641. // Collection of totals by value of VAT in $this->tva["taux"]=total_tva
  642. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  643. if ($prev_progress > 0 && !empty($object->lines[$i]->situation_percent)) { // Compute progress from previous situation
  644. if (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) {
  645. $tvaligne = $sign * $object->lines[$i]->multicurrency_total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent;
  646. } else {
  647. $tvaligne = $sign * $object->lines[$i]->total_tva * ($object->lines[$i]->situation_percent - $prev_progress) / $object->lines[$i]->situation_percent;
  648. }
  649. } else {
  650. if (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) {
  651. $tvaligne = $sign * $object->lines[$i]->multicurrency_total_tva;
  652. } else {
  653. $tvaligne = $sign * $object->lines[$i]->total_tva;
  654. }
  655. }
  656. $localtax1ligne = $object->lines[$i]->total_localtax1;
  657. $localtax2ligne = $object->lines[$i]->total_localtax2;
  658. $localtax1_rate = $object->lines[$i]->localtax1_tx;
  659. $localtax2_rate = $object->lines[$i]->localtax2_tx;
  660. $localtax1_type = $object->lines[$i]->localtax1_type;
  661. $localtax2_type = $object->lines[$i]->localtax2_type;
  662. if ($object->remise_percent) {
  663. $tvaligne -= ($tvaligne * $object->remise_percent) / 100;
  664. }
  665. if ($object->remise_percent) {
  666. $localtax1ligne -= ($localtax1ligne * $object->remise_percent) / 100;
  667. }
  668. if ($object->remise_percent) {
  669. $localtax2ligne -= ($localtax2ligne * $object->remise_percent) / 100;
  670. }
  671. $vatrate = (string) $object->lines[$i]->tva_tx;
  672. // Retrieve type from database for backward compatibility with old records
  673. if ((!isset($localtax1_type) || $localtax1_type == '' || !isset($localtax2_type) || $localtax2_type == '') // if tax type not defined
  674. && (!empty($localtax1_rate) || !empty($localtax2_rate))) { // and there is local tax
  675. $localtaxtmp_array = getLocalTaxesFromRate($vatrate, 0, $object->thirdparty, $mysoc);
  676. $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] : '';
  677. $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] : '';
  678. }
  679. // retrieve global local tax
  680. if ($localtax1_type && $localtax1ligne != 0) {
  681. if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
  682. $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
  683. } else {
  684. $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
  685. }
  686. }
  687. if ($localtax2_type && $localtax2ligne != 0) {
  688. if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
  689. $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
  690. } else {
  691. $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
  692. }
  693. }
  694. if (($object->lines[$i]->info_bits & 0x01) == 0x01) {
  695. $vatrate .= '*';
  696. }
  697. // Fill $this->tva and $this->tva_array
  698. if (!isset($this->tva[$vatrate])) {
  699. $this->tva[$vatrate] = 0;
  700. }
  701. $this->tva[$vatrate] += $tvaligne; // ->tva is abandonned, we use now ->tva_array that is more complete
  702. $vatcode = $object->lines[$i]->vat_src_code;
  703. if (empty($this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'])) {
  704. $this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'] = 0;
  705. }
  706. $this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')] = array('vatrate'=>$vatrate, 'vatcode'=>$vatcode, 'amount'=> $this->tva_array[$vatrate.($vatcode ? ' ('.$vatcode.')' : '')]['amount'] + $tvaligne);
  707. if ($posYAfterImage > $posYAfterDescription) {
  708. $nexY = $posYAfterImage;
  709. }
  710. // Add line
  711. if (!empty($conf->global->MAIN_PDF_DASH_BETWEEN_LINES) && $i < ($nblines - 1)) {
  712. $pdf->setPage($pageposafter);
  713. $pdf->SetLineStyle(array('dash'=>'1,1', 'color'=>array(80, 80, 80)));
  714. //$pdf->SetDrawColor(190,190,200);
  715. $pdf->line($this->marge_gauche, $nexY + 1, $this->page_largeur - $this->marge_droite, $nexY + 1);
  716. $pdf->SetLineStyle(array('dash'=>0));
  717. }
  718. $nexY += 2; // Add space between lines
  719. // Detect if some page were added automatically and output _tableau for past pages
  720. while ($pagenb < $pageposafter) {
  721. $pdf->setPage($pagenb);
  722. if ($pagenb == 1) {
  723. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code);
  724. } else {
  725. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code);
  726. }
  727. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  728. $pagenb++;
  729. $pdf->setPage($pagenb);
  730. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  731. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  732. $top_shift = $this->_pagehead($pdf, $object, 0, $outputlangs);
  733. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  734. }
  735. if (!empty($tplidx)) {
  736. $pdf->useTemplate($tplidx);
  737. }
  738. }
  739. if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
  740. if ($pagenb == 1) {
  741. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code);
  742. } else {
  743. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code);
  744. }
  745. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  746. // New page
  747. $pdf->AddPage();
  748. if (!empty($tplidx)) {
  749. $pdf->useTemplate($tplidx);
  750. }
  751. $pagenb++;
  752. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  753. $top_shift = $this->_pagehead($pdf, $object, 0, $outputlangs);
  754. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  755. }
  756. }
  757. }
  758. // Show square
  759. if ($pagenb == 1) {
  760. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0, $object->multicurrency_code);
  761. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  762. } else {
  763. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code);
  764. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  765. }
  766. dol_syslog("bottomlasttab=".$bottomlasttab." this->page_hauteur=".$this->page_hauteur." heightforinfotot=".$heightforinfotot." heightforfreetext=".$heightforfreetext." heightforfooter=".$heightforfooter);
  767. // Display info area
  768. $posy = $this->_tableau_info($pdf, $object, $bottomlasttab, $outputlangs, $outputlangsbis);
  769. // Display total area
  770. $posy = $this->_tableau_tot($pdf, $object, $deja_regle, $bottomlasttab, $outputlangs, $outputlangsbis);
  771. // Display Payments area
  772. if (($deja_regle || $amount_credit_notes_included || $amount_deposits_included) && empty($conf->global->INVOICE_NO_PAYMENT_DETAILS)) {
  773. $posy = $this->_tableau_versements($pdf, $object, $posy, $outputlangs, $heightforfooter);
  774. }
  775. // Pagefoot
  776. $this->_pagefoot($pdf, $object, $outputlangs);
  777. if (method_exists($pdf, 'AliasNbPages')) {
  778. $pdf->AliasNbPages();
  779. }
  780. $pdf->Close();
  781. $pdf->Output($file, 'F');
  782. // Add pdfgeneration hook
  783. $hookmanager->initHooks(array('pdfgeneration'));
  784. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  785. global $action;
  786. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  787. if ($reshook < 0) {
  788. $this->error = $hookmanager->error;
  789. $this->errors = $hookmanager->errors;
  790. }
  791. if (!empty($conf->global->MAIN_UMASK)) {
  792. @chmod($file, octdec($conf->global->MAIN_UMASK));
  793. }
  794. $this->result = array('fullpath'=>$file);
  795. return 1; // No error
  796. } else {
  797. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  798. return 0;
  799. }
  800. } else {
  801. $this->error = $langs->transnoentities("ErrorConstantNotDefined", "FAC_OUTPUTDIR");
  802. return 0;
  803. }
  804. }
  805. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  806. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  807. /**
  808. * Show payments table
  809. *
  810. * @param TCPDF $pdf Object PDF
  811. * @param Facture $object Object invoice
  812. * @param int $posy Position y in PDF
  813. * @param Translate $outputlangs Object langs for output
  814. * @param int $heightforfooter Height for footer
  815. * @return int <0 if KO, >0 if OK
  816. */
  817. protected function _tableau_versements(&$pdf, $object, $posy, $outputlangs, $heightforfooter = 0)
  818. {
  819. // phpcs:enable
  820. global $conf;
  821. $sign = 1;
  822. if ($object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  823. $sign = -1;
  824. }
  825. $current_page = $pdf->getPage();
  826. $tab3_posx = 120;
  827. $tab3_top = $posy + 8;
  828. $tab3_width = 80;
  829. $tab3_height = 4;
  830. if ($this->page_largeur < 210) { // To work with US executive format
  831. $tab3_posx -= 15;
  832. }
  833. $default_font_size = pdf_getPDFFontSize($outputlangs);
  834. $this->_tableau_versements_header($pdf, $object, $outputlangs, $default_font_size, $tab3_posx, $tab3_top, $tab3_width, $tab3_height);
  835. $y = 0;
  836. $pdf->SetFont('', '', $default_font_size - 4);
  837. // Loop on each discount available (deposits and credit notes and excess of payment included)
  838. $sql = "SELECT re.rowid, re.amount_ht, re.multicurrency_amount_ht, re.amount_tva, re.multicurrency_amount_tva, re.amount_ttc, re.multicurrency_amount_ttc,";
  839. $sql .= " re.description, re.fk_facture_source,";
  840. $sql .= " f.type, f.datef";
  841. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
  842. $sql .= " WHERE re.fk_facture_source = f.rowid AND re.fk_facture = ".((int) $object->id);
  843. $resql = $this->db->query($sql);
  844. if ($resql) {
  845. $num = $this->db->num_rows($resql);
  846. $i = 0;
  847. $invoice = new Facture($this->db);
  848. while ($i < $num) {
  849. $y += 3;
  850. if ($tab3_top + $y >= ($this->page_hauteur - $heightforfooter)) {
  851. $y = 0;
  852. $current_page++;
  853. $pdf->AddPage('', '', true);
  854. if (!empty($tplidx)) {
  855. $pdf->useTemplate($tplidx);
  856. }
  857. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  858. $top_shift = $this->_pagehead($pdf, $object, 0, $outputlangs);
  859. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  860. }
  861. $pdf->setPage($current_page);
  862. $this->_tableau_versements_header($pdf, $object, $outputlangs, $default_font_size, $tab3_posx, $tab3_top + $y - 3, $tab3_width, $tab3_height);
  863. }
  864. $obj = $this->db->fetch_object($resql);
  865. if ($obj->type == 2) {
  866. $text = $outputlangs->transnoentities("CreditNote");
  867. } elseif ($obj->type == 3) {
  868. $text = $outputlangs->transnoentities("Deposit");
  869. } elseif ($obj->type == 0) {
  870. $text = $outputlangs->transnoentities("ExcessReceived");
  871. } else {
  872. $text = $outputlangs->transnoentities("UnknownType");
  873. }
  874. $invoice->fetch($obj->fk_facture_source);
  875. $pdf->SetXY($tab3_posx, $tab3_top + $y);
  876. $pdf->MultiCell(20, 3, dol_print_date($this->db->jdate($obj->datef), 'day', false, $outputlangs, true), 0, 'L', 0);
  877. $pdf->SetXY($tab3_posx + 21, $tab3_top + $y);
  878. $pdf->MultiCell(20, 3, price((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $obj->multicurrency_amount_ttc : $obj->amount_ttc, 0, $outputlangs), 0, 'L', 0);
  879. $pdf->SetXY($tab3_posx + 40, $tab3_top + $y);
  880. $pdf->MultiCell(20, 3, $text, 0, 'L', 0);
  881. $pdf->SetXY($tab3_posx + 58, $tab3_top + $y);
  882. $pdf->MultiCell(20, 3, $invoice->ref, 0, 'L', 0);
  883. $pdf->line($tab3_posx, $tab3_top + $y + 3, $tab3_posx + $tab3_width, $tab3_top + $y + 3);
  884. $i++;
  885. }
  886. } else {
  887. $this->error = $this->db->lasterror();
  888. return -1;
  889. }
  890. // Loop on each payment
  891. // TODO Call getListOfPaymentsgetListOfPayments instead of hard coded sql
  892. $sql = "SELECT p.datep as date, p.fk_paiement, p.num_paiement as num, pf.amount as amount, pf.multicurrency_amount,";
  893. $sql .= " cp.code";
  894. $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p";
  895. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON p.fk_paiement = cp.id";
  896. $sql .= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = ".((int) $object->id);
  897. //$sql.= " WHERE pf.fk_paiement = p.rowid AND pf.fk_facture = 1";
  898. $sql .= " ORDER BY p.datep";
  899. $resql = $this->db->query($sql);
  900. if ($resql) {
  901. $num = $this->db->num_rows($resql);
  902. $i = 0;
  903. while ($i < $num) {
  904. $y += 3;
  905. if ($tab3_top + $y >= ($this->page_hauteur - $heightforfooter)) {
  906. $y = 0;
  907. $current_page++;
  908. $pdf->AddPage('', '', true);
  909. if (!empty($tplidx)) {
  910. $pdf->useTemplate($tplidx);
  911. }
  912. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  913. $top_shift = $this->_pagehead($pdf, $object, 0, $outputlangs);
  914. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
  915. }
  916. $pdf->setPage($current_page);
  917. $this->_tableau_versements_header($pdf, $object, $outputlangs, $default_font_size, $tab3_posx, $tab3_top + $y - 3, $tab3_width, $tab3_height);
  918. }
  919. $row = $this->db->fetch_object($resql);
  920. $pdf->SetXY($tab3_posx, $tab3_top + $y);
  921. $pdf->MultiCell(20, 3, dol_print_date($this->db->jdate($row->date), 'day', false, $outputlangs, true), 0, 'L', 0);
  922. $pdf->SetXY($tab3_posx + 21, $tab3_top + $y);
  923. $pdf->MultiCell(20, 3, price($sign * ((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $row->multicurrency_amount : $row->amount), 0, $outputlangs), 0, 'L', 0);
  924. $pdf->SetXY($tab3_posx + 40, $tab3_top + $y);
  925. $oper = $outputlangs->transnoentitiesnoconv("PaymentTypeShort".$row->code);
  926. $pdf->MultiCell(20, 3, $oper, 0, 'L', 0);
  927. $pdf->SetXY($tab3_posx + 58, $tab3_top + $y);
  928. $pdf->MultiCell(30, 3, $row->num, 0, 'L', 0);
  929. $pdf->line($tab3_posx, $tab3_top + $y + 3, $tab3_posx + $tab3_width, $tab3_top + $y + 3);
  930. $i++;
  931. }
  932. return $tab3_top + $y + 3;
  933. } else {
  934. $this->error = $this->db->lasterror();
  935. return -1;
  936. }
  937. }
  938. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  939. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  940. /**
  941. * Function _tableau_versements_header
  942. *
  943. * @param TCPDF $pdf Object PDF
  944. * @param Facture $object Object invoice
  945. * @param Translate $outputlangs Object langs for output
  946. * @param int $default_font_size Font size
  947. * @param int $tab3_posx pos x
  948. * @param int $tab3_top pos y
  949. * @param int $tab3_width width
  950. * @param int $tab3_height height
  951. * @return void
  952. */
  953. protected function _tableau_versements_header($pdf, $object, $outputlangs, $default_font_size, $tab3_posx, $tab3_top, $tab3_width, $tab3_height)
  954. {
  955. // phpcs:enable
  956. $title = $outputlangs->transnoentities("PaymentsAlreadyDone");
  957. if ($object->type == 2) {
  958. $title = $outputlangs->transnoentities("PaymentsBackAlreadyDone");
  959. }
  960. $pdf->SetFont('', '', $default_font_size - 3);
  961. $pdf->SetXY($tab3_posx, $tab3_top - 4);
  962. $pdf->MultiCell(60, 3, $title, 0, 'L', 0);
  963. $pdf->line($tab3_posx, $tab3_top, $tab3_posx + $tab3_width, $tab3_top);
  964. $pdf->SetFont('', '', $default_font_size - 4);
  965. $pdf->SetXY($tab3_posx, $tab3_top);
  966. $pdf->MultiCell(20, 3, $outputlangs->transnoentities("Payment"), 0, 'L', 0);
  967. $pdf->SetXY($tab3_posx + 21, $tab3_top);
  968. $pdf->MultiCell(20, 3, $outputlangs->transnoentities("Amount"), 0, 'L', 0);
  969. $pdf->SetXY($tab3_posx + 40, $tab3_top);
  970. $pdf->MultiCell(20, 3, $outputlangs->transnoentities("Type"), 0, 'L', 0);
  971. $pdf->SetXY($tab3_posx + 58, $tab3_top);
  972. $pdf->MultiCell(20, 3, $outputlangs->transnoentities("Num"), 0, 'L', 0);
  973. $pdf->line($tab3_posx, $tab3_top - 1 + $tab3_height, $tab3_posx + $tab3_width, $tab3_top - 1 + $tab3_height);
  974. }
  975. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  976. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  977. /**
  978. * Show miscellaneous information (payment mode, payment term, ...)
  979. *
  980. * @param TCPDF $pdf Object PDF
  981. * @param Facture $object Object to show
  982. * @param int $posy Y
  983. * @param Translate $outputlangs Langs object
  984. * @param Translate $outputlangsbis Object lang for output bis
  985. * @return int Pos y
  986. */
  987. protected function _tableau_info(&$pdf, $object, $posy, $outputlangs, $outputlangsbis)
  988. {
  989. // phpcs:enable
  990. global $conf, $mysoc;
  991. $default_font_size = pdf_getPDFFontSize($outputlangs);
  992. $pdf->SetFont('', '', $default_font_size - 1);
  993. // If France, show VAT mention if not applicable
  994. if ($this->emetteur->country_code == 'FR' && empty($mysoc->tva_assuj)) {
  995. $pdf->SetFont('', 'B', $default_font_size - 2);
  996. $pdf->SetXY($this->marge_gauche, $posy);
  997. if ($mysoc->forme_juridique_code == 92) {
  998. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("VATIsNotUsedForInvoiceAsso"), 0, 'L', 0);
  999. } else {
  1000. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("VATIsNotUsedForInvoice"), 0, 'L', 0);
  1001. }
  1002. $posy = $pdf->GetY() + 4;
  1003. }
  1004. $posxval = 52;
  1005. $posxend = 110; // End of x for text on left side
  1006. if ($this->page_largeur < 210) { // To work with US executive format
  1007. $posxend -= 10;
  1008. }
  1009. // Show payments conditions
  1010. if ($object->type != 2 && ($object->cond_reglement_code || $object->cond_reglement)) {
  1011. $pdf->SetFont('', 'B', $default_font_size - 2);
  1012. $pdf->SetXY($this->marge_gauche, $posy);
  1013. $titre = $outputlangs->transnoentities("PaymentConditions").':';
  1014. $pdf->MultiCell(43, 4, $titre, 0, 'L');
  1015. $pdf->SetFont('', '', $default_font_size - 2);
  1016. $pdf->SetXY($posxval, $posy);
  1017. $lib_condition_paiement = $outputlangs->transnoentities("PaymentCondition".$object->cond_reglement_code) != ('PaymentCondition'.$object->cond_reglement_code) ? $outputlangs->transnoentities("PaymentCondition".$object->cond_reglement_code) : $outputlangs->convToOutputCharset($object->cond_reglement_doc ? $object->cond_reglement_doc : $object->cond_reglement_label);
  1018. $lib_condition_paiement = str_replace('\n', "\n", $lib_condition_paiement);
  1019. $pdf->MultiCell(67, 4, $lib_condition_paiement, 0, 'L');
  1020. $posy = $pdf->GetY() + 3; // We need spaces for 2 lines payment conditions
  1021. }
  1022. // Show category of operations
  1023. if (getDolGlobalInt('INVOICE_CATEGORY_OF_OPERATION') == 2 && $this->categoryOfOperation >= 0) {
  1024. $pdf->SetFont('', 'B', $default_font_size - 2);
  1025. $pdf->SetXY($this->marge_gauche, $posy);
  1026. $categoryOfOperationTitle = $outputlangs->transnoentities("MentionCategoryOfOperations").' : ';
  1027. $pdf->MultiCell($posxval - $this->marge_gauche, 4, $categoryOfOperationTitle, 0, 'L');
  1028. $pdf->SetFont('', '', $default_font_size - 2);
  1029. $pdf->SetXY($posxval, $posy);
  1030. $categoryOfOperationLabel = $outputlangs->transnoentities("MentionCategoryOfOperations" . $this->categoryOfOperation);
  1031. $pdf->MultiCell($posxend - $posxval, 4, $categoryOfOperationLabel, 0, 'L');
  1032. $posy = $pdf->GetY() + 3; // for 2 lines
  1033. }
  1034. if ($object->type != 2) {
  1035. // Check a payment mode is defined
  1036. if (empty($object->mode_reglement_code)
  1037. && empty($conf->global->FACTURE_CHQ_NUMBER)
  1038. && !getDolGlobalInt('FACTURE_RIB_NUMBER')) {
  1039. $this->error = $outputlangs->transnoentities("ErrorNoPaiementModeConfigured");
  1040. } elseif (($object->mode_reglement_code == 'CHQ' && empty($conf->global->FACTURE_CHQ_NUMBER) && empty($object->fk_account) && empty($object->fk_bank))
  1041. || ($object->mode_reglement_code == 'VIR' && !getDolGlobalInt('FACTURE_RIB_NUMBER') && empty($object->fk_account) && empty($object->fk_bank))) {
  1042. // Avoid having any valid PDF with setup that is not complete
  1043. $outputlangs->load("errors");
  1044. $pdf->SetXY($this->marge_gauche, $posy);
  1045. $pdf->SetTextColor(200, 0, 0);
  1046. $pdf->SetFont('', 'B', $default_font_size - 2);
  1047. $this->error = $outputlangs->transnoentities("ErrorPaymentModeDefinedToWithoutSetup", $object->mode_reglement_code);
  1048. $pdf->MultiCell(80, 3, $this->error, 0, 'L', 0);
  1049. $pdf->SetTextColor(0, 0, 0);
  1050. $posy = $pdf->GetY() + 1;
  1051. }
  1052. // Show payment mode
  1053. if (!empty($object->mode_reglement_code)
  1054. && $object->mode_reglement_code != 'CHQ'
  1055. && $object->mode_reglement_code != 'VIR') {
  1056. $pdf->SetFont('', 'B', $default_font_size - 2);
  1057. $pdf->SetXY($this->marge_gauche, $posy);
  1058. $titre = $outputlangs->transnoentities("PaymentMode").':';
  1059. $pdf->MultiCell(80, 5, $titre, 0, 'L');
  1060. $pdf->SetFont('', '', $default_font_size - 2);
  1061. $pdf->SetXY($posxval, $posy);
  1062. $lib_mode_reg = $outputlangs->transnoentities("PaymentType".$object->mode_reglement_code) != ('PaymentType'.$object->mode_reglement_code) ? $outputlangs->transnoentities("PaymentType".$object->mode_reglement_code) : $outputlangs->convToOutputCharset($object->mode_reglement);
  1063. //#21654: add account number used for the debit
  1064. if ($object->mode_reglement_code == "PRE") {
  1065. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  1066. $bac = new CompanyBankAccount($this->db);
  1067. $bac->fetch(0, $object->thirdparty->id);
  1068. $iban= $bac->iban.(($bac->iban && $bac->bic) ? ' / ' : '').$bac->bic;
  1069. $lib_mode_reg .= $outputlangs->trans("PaymentTypePREdetails", dol_trunc($iban, 6, 'right', 'UTF-8', 1));
  1070. }
  1071. $pdf->MultiCell(80, 5, $lib_mode_reg, 0, 'L');
  1072. $posy = $pdf->GetY();
  1073. }
  1074. // Show if Option VAT debit option is on also if transmitter is french
  1075. // Decret n°2099-1299 2022-10-07
  1076. // French mention : "Option pour le paiement de la taxe d'après les débits"
  1077. if ($this->emetteur->country_code == 'FR') {
  1078. if (isset($conf->global->TAX_MODE) && $conf->global->TAX_MODE == 1) {
  1079. $pdf->SetXY($this->marge_gauche, $posy);
  1080. $pdf->writeHTMLCell(80, 5, '', '', $outputlangs->transnoentities("MentionVATDebitOptionIsOn"), 0, 1);
  1081. $posy = $pdf->GetY() + 1;
  1082. }
  1083. }
  1084. // Show online payment link
  1085. if (empty($object->mode_reglement_code) || $object->mode_reglement_code == 'CB' || $object->mode_reglement_code == 'VAD') {
  1086. $useonlinepayment = 0;
  1087. if (!empty($conf->global->PDF_SHOW_LINK_TO_ONLINE_PAYMENT)) {
  1088. if (isModEnabled('paypal')) {
  1089. $useonlinepayment++;
  1090. }
  1091. if (isModEnabled('stripe')) {
  1092. $useonlinepayment++;
  1093. }
  1094. if (isModEnabled('paybox')) {
  1095. $useonlinepayment++;
  1096. }
  1097. }
  1098. if ($object->statut != Facture::STATUS_DRAFT && $useonlinepayment) {
  1099. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  1100. global $langs;
  1101. $langs->loadLangs(array('payment', 'paybox', 'stripe'));
  1102. $servicename = $langs->transnoentities('Online');
  1103. $paiement_url = getOnlinePaymentUrl('', 'invoice', $object->ref, '', '', '');
  1104. $linktopay = $langs->trans("ToOfferALinkForOnlinePayment", $servicename).' <a href="'.$paiement_url.'">'.$outputlangs->transnoentities("ClickHere").'</a>';
  1105. $pdf->SetXY($this->marge_gauche, $posy);
  1106. $pdf->writeHTMLCell(80, 5, '', '', dol_htmlentitiesbr($linktopay), 0, 1);
  1107. $posy = $pdf->GetY() + 1;
  1108. }
  1109. }
  1110. // Show payment mode CHQ
  1111. if (empty($object->mode_reglement_code) || $object->mode_reglement_code == 'CHQ') {
  1112. // If payment mode unregulated or payment mode forced to CHQ
  1113. if (!empty($conf->global->FACTURE_CHQ_NUMBER)) {
  1114. $diffsizetitle = (empty($conf->global->PDF_DIFFSIZE_TITLE) ? 3 : $conf->global->PDF_DIFFSIZE_TITLE);
  1115. if ($conf->global->FACTURE_CHQ_NUMBER > 0) {
  1116. $account = new Account($this->db);
  1117. $account->fetch($conf->global->FACTURE_CHQ_NUMBER);
  1118. $pdf->SetXY($this->marge_gauche, $posy);
  1119. $pdf->SetFont('', 'B', $default_font_size - $diffsizetitle);
  1120. $pdf->MultiCell(100, 3, $outputlangs->transnoentities('PaymentByChequeOrderedTo', $account->proprio), 0, 'L', 0);
  1121. $posy = $pdf->GetY() + 1;
  1122. if (empty($conf->global->MAIN_PDF_HIDE_CHQ_ADDRESS)) {
  1123. $pdf->SetXY($this->marge_gauche, $posy);
  1124. $pdf->SetFont('', '', $default_font_size - $diffsizetitle);
  1125. $pdf->MultiCell(100, 3, $outputlangs->convToOutputCharset($account->owner_address), 0, 'L', 0);
  1126. $posy = $pdf->GetY() + 2;
  1127. }
  1128. }
  1129. if ($conf->global->FACTURE_CHQ_NUMBER == -1) {
  1130. $pdf->SetXY($this->marge_gauche, $posy);
  1131. $pdf->SetFont('', 'B', $default_font_size - $diffsizetitle);
  1132. $pdf->MultiCell(100, 3, $outputlangs->transnoentities('PaymentByChequeOrderedTo', $this->emetteur->name), 0, 'L', 0);
  1133. $posy = $pdf->GetY() + 1;
  1134. if (empty($conf->global->MAIN_PDF_HIDE_CHQ_ADDRESS)) {
  1135. $pdf->SetXY($this->marge_gauche, $posy);
  1136. $pdf->SetFont('', '', $default_font_size - $diffsizetitle);
  1137. $pdf->MultiCell(100, 3, $outputlangs->convToOutputCharset($this->emetteur->getFullAddress()), 0, 'L', 0);
  1138. $posy = $pdf->GetY() + 2;
  1139. }
  1140. }
  1141. }
  1142. }
  1143. // If payment mode not forced or forced to VIR, show payment with BAN
  1144. if (empty($object->mode_reglement_code) || $object->mode_reglement_code == 'VIR') {
  1145. if ($object->fk_account > 0 || $object->fk_bank > 0 || getDolGlobalInt('FACTURE_RIB_NUMBER')) {
  1146. $bankid = ($object->fk_account <= 0 ? $conf->global->FACTURE_RIB_NUMBER : $object->fk_account);
  1147. if ($object->fk_bank > 0) {
  1148. $bankid = $object->fk_bank; // For backward compatibility when object->fk_account is forced with object->fk_bank
  1149. }
  1150. $account = new Account($this->db);
  1151. $account->fetch($bankid);
  1152. $curx = $this->marge_gauche;
  1153. $cury = $posy;
  1154. $posy = pdf_bank($pdf, $outputlangs, $curx, $cury, $account, 0, $default_font_size);
  1155. $posy += 2;
  1156. }
  1157. }
  1158. }
  1159. return $posy;
  1160. }
  1161. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1162. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  1163. /**
  1164. * Show total to pay
  1165. *
  1166. * @param TCPDF $pdf Object PDF
  1167. * @param Facture $object Object invoice
  1168. * @param int $deja_regle Amount already paid (in the currency of invoice)
  1169. * @param int $posy Position depart
  1170. * @param Translate $outputlangs Objet langs
  1171. * @param Translate $outputlangsbis Object lang for output bis
  1172. * @return int Position pour suite
  1173. */
  1174. protected function _tableau_tot(&$pdf, $object, $deja_regle, $posy, $outputlangs, $outputlangsbis)
  1175. {
  1176. // phpcs:enable
  1177. global $conf, $mysoc, $hookmanager;
  1178. $sign = 1;
  1179. if ($object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  1180. $sign = -1;
  1181. }
  1182. $default_font_size = pdf_getPDFFontSize($outputlangs);
  1183. $outputlangsbis = null;
  1184. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
  1185. $outputlangsbis = new Translate('', $conf);
  1186. $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
  1187. $outputlangsbis->loadLangs(array("main", "dict", "companies", "bills", "products", "propal"));
  1188. $default_font_size--;
  1189. }
  1190. $tab2_top = $posy;
  1191. $tab2_hl = 4;
  1192. $pdf->SetFont('', '', $default_font_size - 1);
  1193. // Total table
  1194. $col1x = 120;
  1195. $col2x = 170;
  1196. if ($this->page_largeur < 210) { // To work with US executive format
  1197. $col1x -= 15;
  1198. $col2x -= 10;
  1199. }
  1200. $largcol2 = ($this->page_largeur - $this->marge_droite - $col2x);
  1201. $useborder = 0;
  1202. $index = 0;
  1203. // Total HT
  1204. $pdf->SetFillColor(255, 255, 255);
  1205. $pdf->SetXY($col1x, $tab2_top + 0);
  1206. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities(empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT) ? "TotalHT" : "Total").(is_object($outputlangsbis) ? ' / '.$outputlangsbis->transnoentities(empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT) ? "TotalHT" : "Total") : ''), 0, 'L', 1);
  1207. $total_ht = ((isModEnabled("multicurrency") && isset($object->multicurrency_tx) && $object->multicurrency_tx != 1) ? $object->multicurrency_total_ht : $object->total_ht);
  1208. $pdf->SetXY($col2x, $tab2_top + 0);
  1209. $pdf->MultiCell($largcol2, $tab2_hl, price($sign * ($total_ht + (!empty($object->remise) ? $object->remise : 0)), 0, $outputlangs), 0, 'R', 1);
  1210. // Show VAT by rates and total
  1211. $pdf->SetFillColor(248, 248, 248);
  1212. $total_ttc = (isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? $object->multicurrency_total_ttc : $object->total_ttc;
  1213. $this->atleastoneratenotnull = 0;
  1214. if (empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT)) {
  1215. $tvaisnull = ((!empty($this->tva) && count($this->tva) == 1 && isset($this->tva['0.000']) && is_float($this->tva['0.000'])) ? true : false);
  1216. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT_IFNULL) && $tvaisnull) {
  1217. // Nothing to do
  1218. } else {
  1219. // FIXME amount of vat not supported with multicurrency
  1220. //Local tax 1 before VAT
  1221. //if (!empty($conf->global->FACTURE_LOCAL_TAX1_OPTION) && $conf->global->FACTURE_LOCAL_TAX1_OPTION=='localtax1on')
  1222. //{
  1223. foreach ($this->localtax1 as $localtax_type => $localtax_rate) {
  1224. if (in_array((string) $localtax_type, array('1', '3', '5'))) {
  1225. continue;
  1226. }
  1227. foreach ($localtax_rate as $tvakey => $tvaval) {
  1228. if ($tvakey != 0) { // On affiche pas taux 0
  1229. //$this->atleastoneratenotnull++;
  1230. $index++;
  1231. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1232. $tvacompl = '';
  1233. if (preg_match('/\*/', $tvakey)) {
  1234. $tvakey = str_replace('*', '', $tvakey);
  1235. $tvacompl = " (".$outputlangs->transnoentities("NonPercuRecuperable").")";
  1236. }
  1237. $totalvat = $outputlangs->transcountrynoentities("TotalLT1", $mysoc->country_code).(is_object($outputlangsbis) ? ' / '.$outputlangsbis->transcountrynoentities("TotalLT1", $mysoc->country_code) : '');
  1238. $totalvat .= ' ';
  1239. $totalvat .= vatrate(abs($tvakey), 1).$tvacompl;
  1240. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $totalvat, 0, 'L', 1);
  1241. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1242. $pdf->MultiCell($largcol2, $tab2_hl, price($tvaval, 0, $outputlangs), 0, 'R', 1);
  1243. }
  1244. }
  1245. }
  1246. //}
  1247. //Local tax 2 before VAT
  1248. //if (!empty($conf->global->FACTURE_LOCAL_TAX2_OPTION) && $conf->global->FACTURE_LOCAL_TAX2_OPTION=='localtax2on')
  1249. //{
  1250. foreach ($this->localtax2 as $localtax_type => $localtax_rate) {
  1251. if (in_array((string) $localtax_type, array('1', '3', '5'))) {
  1252. continue;
  1253. }
  1254. foreach ($localtax_rate as $tvakey => $tvaval) {
  1255. if ($tvakey != 0) { // On affiche pas taux 0
  1256. //$this->atleastoneratenotnull++;
  1257. $index++;
  1258. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1259. $tvacompl = '';
  1260. if (preg_match('/\*/', $tvakey)) {
  1261. $tvakey = str_replace('*', '', $tvakey);
  1262. $tvacompl = " (".$outputlangs->transnoentities("NonPercuRecuperable").")";
  1263. }
  1264. $totalvat = $outputlangs->transcountrynoentities("TotalLT2", $mysoc->country_code).(is_object($outputlangsbis) ? ' / '.$outputlangsbis->transcountrynoentities("TotalLT2", $mysoc->country_code) : '');
  1265. $totalvat .= ' ';
  1266. $totalvat .= vatrate(abs($tvakey), 1).$tvacompl;
  1267. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $totalvat, 0, 'L', 1);
  1268. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1269. $pdf->MultiCell($largcol2, $tab2_hl, price($tvaval, 0, $outputlangs), 0, 'R', 1);
  1270. }
  1271. }
  1272. }
  1273. //}
  1274. // VAT
  1275. foreach ($this->tva_array as $tvakey => $tvaval) {
  1276. if ($tvakey != 0) { // On affiche pas taux 0
  1277. $this->atleastoneratenotnull++;
  1278. $index++;
  1279. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1280. $tvacompl = '';
  1281. if (preg_match('/\*/', $tvakey)) {
  1282. $tvakey = str_replace('*', '', $tvakey);
  1283. $tvacompl = " (".$outputlangs->transnoentities("NonPercuRecuperable").")";
  1284. }
  1285. $totalvat = $outputlangs->transcountrynoentities("TotalVAT", $mysoc->country_code).(is_object($outputlangsbis) ? ' / '.$outputlangsbis->transcountrynoentities("TotalVAT", $mysoc->country_code) : '');
  1286. $totalvat .= ' ';
  1287. if (getDolGlobalString('PDF_VAT_LABEL_IS_CODE_OR_RATE') == 'rateonly') {
  1288. $totalvat .= vatrate($tvaval['vatrate'], 1).$tvacompl;
  1289. } elseif (getDolGlobalString('PDF_VAT_LABEL_IS_CODE_OR_RATE') == 'codeonly') {
  1290. $totalvat .= $tvaval['vatcode'].$tvacompl;
  1291. } else {
  1292. $totalvat .= vatrate($tvaval['vatrate'], 1).($tvaval['vatcode'] ? ' ('.$tvaval['vatcode'].')' : '').$tvacompl;
  1293. }
  1294. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $totalvat, 0, 'L', 1);
  1295. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1296. $pdf->MultiCell($largcol2, $tab2_hl, price(price2num($tvaval['amount'], 'MT'), 0, $outputlangs), 0, 'R', 1);
  1297. }
  1298. }
  1299. //Local tax 1 after VAT
  1300. //if (!empty($conf->global->FACTURE_LOCAL_TAX1_OPTION) && $conf->global->FACTURE_LOCAL_TAX1_OPTION=='localtax1on')
  1301. //{
  1302. foreach ($this->localtax1 as $localtax_type => $localtax_rate) {
  1303. if (in_array((string) $localtax_type, array('2', '4', '6'))) {
  1304. continue;
  1305. }
  1306. foreach ($localtax_rate as $tvakey => $tvaval) {
  1307. if ($tvakey != 0) { // On affiche pas taux 0
  1308. //$this->atleastoneratenotnull++;
  1309. $index++;
  1310. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1311. $tvacompl = '';
  1312. if (preg_match('/\*/', $tvakey)) {
  1313. $tvakey = str_replace('*', '', $tvakey);
  1314. $tvacompl = " (".$outputlangs->transnoentities("NonPercuRecuperable").")";
  1315. }
  1316. $totalvat = $outputlangs->transcountrynoentities("TotalLT1", $mysoc->country_code).' ';
  1317. $totalvat .= vatrate(abs($tvakey), 1).$tvacompl;
  1318. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $totalvat, 0, 'L', 1);
  1319. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1320. $pdf->MultiCell($largcol2, $tab2_hl, price($tvaval, 0, $outputlangs), 0, 'R', 1);
  1321. }
  1322. }
  1323. }
  1324. //}
  1325. //Local tax 2 after VAT
  1326. //if (!empty($conf->global->FACTURE_LOCAL_TAX2_OPTION) && $conf->global->FACTURE_LOCAL_TAX2_OPTION=='localtax2on')
  1327. //{
  1328. foreach ($this->localtax2 as $localtax_type => $localtax_rate) {
  1329. if (in_array((string) $localtax_type, array('2', '4', '6'))) {
  1330. continue;
  1331. }
  1332. foreach ($localtax_rate as $tvakey => $tvaval) {
  1333. //$this->atleastoneratenotnull++;
  1334. $index++;
  1335. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1336. $tvacompl = '';
  1337. if (preg_match('/\*/', $tvakey)) {
  1338. $tvakey = str_replace('*', '', $tvakey);
  1339. $tvacompl = " (".$outputlangs->transnoentities("NonPercuRecuperable").")";
  1340. }
  1341. $totalvat = $outputlangs->transcountrynoentities("TotalLT2", $mysoc->country_code).' ';
  1342. $totalvat .= vatrate(abs($tvakey), 1).$tvacompl;
  1343. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $totalvat, 0, 'L', 1);
  1344. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1345. $pdf->MultiCell($largcol2, $tab2_hl, price($tvaval, 0, $outputlangs), 0, 'R', 1);
  1346. }
  1347. }
  1348. //}
  1349. // Revenue stamp
  1350. if (price2num($object->revenuestamp) != 0) {
  1351. $index++;
  1352. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1353. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities("RevenueStamp"), $useborder, 'L', 1);
  1354. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1355. $pdf->MultiCell($largcol2, $tab2_hl, price($sign * $object->revenuestamp), $useborder, 'R', 1);
  1356. }
  1357. // Total TTC
  1358. $index++;
  1359. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1360. $pdf->SetTextColor(0, 0, 60);
  1361. $pdf->SetFillColor(224, 224, 224);
  1362. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities("TotalTTC"), $useborder, 'L', 1);
  1363. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1364. $pdf->MultiCell($largcol2, $tab2_hl, price($sign * $total_ttc, 0, $outputlangs), $useborder, 'R', 1);
  1365. // Retained warranty
  1366. if ($object->displayRetainedWarranty()) {
  1367. $pdf->SetTextColor(40, 40, 40);
  1368. $pdf->SetFillColor(255, 255, 255);
  1369. $retainedWarranty = $object->getRetainedWarrantyAmount();
  1370. $billedWithRetainedWarranty = $object->total_ttc - $retainedWarranty;
  1371. // Billed - retained warranty
  1372. $index++;
  1373. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1374. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities("ToPayOn", dol_print_date($object->date_lim_reglement, 'day')), $useborder, 'L', 1);
  1375. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1376. $pdf->MultiCell($largcol2, $tab2_hl, price($billedWithRetainedWarranty), $useborder, 'R', 1);
  1377. // retained warranty
  1378. $index++;
  1379. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1380. $retainedWarrantyToPayOn = $outputlangs->transnoentities("RetainedWarranty").' ('.$object->retained_warranty.'%)';
  1381. $retainedWarrantyToPayOn .= !empty($object->retained_warranty_date_limit) ? ' '.$outputlangs->transnoentities("toPayOn", dol_print_date($object->retained_warranty_date_limit, 'day')) : '';
  1382. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $retainedWarrantyToPayOn, $useborder, 'L', 1);
  1383. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1384. $pdf->MultiCell($largcol2, $tab2_hl, price($retainedWarranty), $useborder, 'R', 1);
  1385. }
  1386. }
  1387. }
  1388. $pdf->SetTextColor(0, 0, 0);
  1389. $creditnoteamount = $object->getSumCreditNotesUsed((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? 1 : 0); // Warning, this also include excess received
  1390. $depositsamount = $object->getSumDepositsUsed((isModEnabled("multicurrency") && $object->multicurrency_tx != 1) ? 1 : 0);
  1391. //print "x".$creditnoteamount."-".$depositsamount;exit;
  1392. $resteapayer = price2num($total_ttc - $deja_regle - $creditnoteamount - $depositsamount, 'MT');
  1393. if (!empty($object->paye)) {
  1394. $resteapayer = 0;
  1395. }
  1396. if (($deja_regle > 0 || $creditnoteamount > 0 || $depositsamount > 0) && empty($conf->global->INVOICE_NO_PAYMENT_DETAILS)) {
  1397. // Already paid + Deposits
  1398. $index++;
  1399. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1400. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities("Paid"), 0, 'L', 0);
  1401. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1402. $pdf->MultiCell($largcol2, $tab2_hl, price($deja_regle + $depositsamount, 0, $outputlangs), 0, 'R', 0);
  1403. // Credit note
  1404. if ($creditnoteamount) {
  1405. $labeltouse = ($outputlangs->transnoentities("CreditNotesOrExcessReceived") != "CreditNotesOrExcessReceived") ? $outputlangs->transnoentities("CreditNotesOrExcessReceived") : $outputlangs->transnoentities("CreditNotes");
  1406. $index++;
  1407. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1408. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $labeltouse, 0, 'L', 0);
  1409. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1410. $pdf->MultiCell($largcol2, $tab2_hl, price($creditnoteamount, 0, $outputlangs), 0, 'R', 0);
  1411. }
  1412. // Escompte
  1413. if ($object->close_code == Facture::CLOSECODE_DISCOUNTVAT) {
  1414. $index++;
  1415. $pdf->SetFillColor(255, 255, 255);
  1416. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1417. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities("EscompteOfferedShort"), $useborder, 'L', 1);
  1418. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1419. $pdf->MultiCell($largcol2, $tab2_hl, price($object->total_ttc - $deja_regle - $creditnoteamount - $depositsamount, 0, $outputlangs), $useborder, 'R', 1);
  1420. $resteapayer = 0;
  1421. }
  1422. $index++;
  1423. $pdf->SetTextColor(0, 0, 60);
  1424. $pdf->SetFillColor(224, 224, 224);
  1425. $pdf->SetXY($col1x, $tab2_top + $tab2_hl * $index);
  1426. $pdf->MultiCell($col2x - $col1x, $tab2_hl, $outputlangs->transnoentities("RemainderToPay"), $useborder, 'L', 1);
  1427. $pdf->SetXY($col2x, $tab2_top + $tab2_hl * $index);
  1428. $pdf->MultiCell($largcol2, $tab2_hl, price($resteapayer, 0, $outputlangs), $useborder, 'R', 1);
  1429. $pdf->SetFont('', '', $default_font_size - 1);
  1430. $pdf->SetTextColor(0, 0, 0);
  1431. }
  1432. $index++;
  1433. return ($tab2_top + ($tab2_hl * $index));
  1434. }
  1435. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  1436. /**
  1437. * Show table for lines
  1438. *
  1439. * @param TCPDF $pdf Object PDF
  1440. * @param string $tab_top Top position of table
  1441. * @param string $tab_height Height of table (rectangle)
  1442. * @param int $nexY Y (not used)
  1443. * @param Translate $outputlangs Langs object
  1444. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  1445. * @param int $hidebottom Hide bottom bar of array
  1446. * @param string $currency Currency code
  1447. * @return void
  1448. */
  1449. protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0, $currency = '')
  1450. {
  1451. global $conf;
  1452. // Force to disable hidetop and hidebottom
  1453. $hidebottom = 0;
  1454. if ($hidetop) {
  1455. $hidetop = -1;
  1456. }
  1457. $currency = !empty($currency) ? $currency : $conf->currency;
  1458. $default_font_size = pdf_getPDFFontSize($outputlangs);
  1459. // Amount in (at tab_top - 1)
  1460. $pdf->SetTextColor(0, 0, 0);
  1461. $pdf->SetFont('', '', $default_font_size - 2);
  1462. if (empty($hidetop)) {
  1463. // Show category of operations
  1464. if (getDolGlobalInt('INVOICE_CATEGORY_OF_OPERATION') == 1 && $this->categoryOfOperation >= 0) {
  1465. $categoryOfOperations = $outputlangs->transnoentities("MentionCategoryOfOperations") . ' : ' . $outputlangs->transnoentities("MentionCategoryOfOperations" . $this->categoryOfOperation);
  1466. $pdf->SetXY($this->marge_gauche, $tab_top - 4);
  1467. $pdf->MultiCell(($pdf->GetStringWidth($categoryOfOperations)) + 4, 2, $categoryOfOperations);
  1468. }
  1469. $titre = $outputlangs->transnoentities("AmountInCurrency", $outputlangs->transnoentitiesnoconv("Currency".$currency));
  1470. $pdf->SetXY($this->page_largeur - $this->marge_droite - ($pdf->GetStringWidth($titre) + 3), $tab_top - 4);
  1471. $pdf->MultiCell(($pdf->GetStringWidth($titre) + 3), 2, $titre);
  1472. //$conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR='230,230,230';
  1473. if (!empty($conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR)) {
  1474. $pdf->Rect($this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_droite - $this->marge_gauche, 5, 'F', null, explode(',', $conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR));
  1475. }
  1476. }
  1477. $pdf->SetDrawColor(128, 128, 128);
  1478. $pdf->SetFont('', '', $default_font_size - 1);
  1479. // Output Rect
  1480. $this->printRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $tab_height, $hidetop, $hidebottom); // Rect takes a length in 3rd parameter and 4th parameter
  1481. if (empty($hidetop)) {
  1482. $pdf->line($this->marge_gauche, $tab_top + 5, $this->page_largeur - $this->marge_droite, $tab_top + 5); // line takes a position y in 2nd parameter and 4th parameter
  1483. $pdf->SetXY($this->posxdesc - 1, $tab_top + 1);
  1484. $pdf->MultiCell(108, 2, $outputlangs->transnoentities("Designation"), '', 'L');
  1485. }
  1486. if (!empty($conf->global->MAIN_GENERATE_INVOICES_WITH_PICTURE)) {
  1487. $pdf->line($this->posxpicture - 1, $tab_top, $this->posxpicture - 1, $tab_top + $tab_height);
  1488. if (empty($hidetop)) {
  1489. //$pdf->SetXY($this->posxpicture-1, $tab_top+1);
  1490. //$pdf->MultiCell($this->posxtva-$this->posxpicture-1,2, $outputlangs->transnoentities("Photo"),'','C');
  1491. }
  1492. }
  1493. if (empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT) && empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT_COLUMN)) {
  1494. $pdf->line($this->posxtva - 1, $tab_top, $this->posxtva - 1, $tab_top + $tab_height);
  1495. if (empty($hidetop)) {
  1496. $pdf->SetXY($this->posxtva - 3, $tab_top + 1);
  1497. $pdf->MultiCell($this->posxup - $this->posxtva + 3, 2, $outputlangs->transnoentities("VAT"), '', 'C');
  1498. }
  1499. }
  1500. $pdf->line($this->posxup - 1, $tab_top, $this->posxup - 1, $tab_top + $tab_height);
  1501. if (empty($hidetop)) {
  1502. $pdf->SetXY($this->posxup - 1, $tab_top + 1);
  1503. $pdf->MultiCell($this->posxqty - $this->posxup - 1, 2, $outputlangs->transnoentities("PriceUHT"), '', 'C');
  1504. }
  1505. $pdf->line($this->posxqty - 1, $tab_top, $this->posxqty - 1, $tab_top + $tab_height);
  1506. if (empty($hidetop)) {
  1507. $pdf->SetXY($this->posxqty - 1, $tab_top + 1);
  1508. $pdf->MultiCell($this->posxunit - $this->posxqty - 1, 2, $outputlangs->transnoentities("Qty"), '', 'C');
  1509. }
  1510. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  1511. $pdf->line($this->posxunit - 1, $tab_top, $this->posxunit - 1, $tab_top + $tab_height);
  1512. if (empty($hidetop)) {
  1513. $pdf->SetXY($this->posxunit - 1, $tab_top + 1);
  1514. $pdf->MultiCell($this->posxdiscount - $this->posxunit - 1, 2, $outputlangs->transnoentities("Unit"), '', 'C');
  1515. }
  1516. }
  1517. if ($this->atleastonediscount) {
  1518. $pdf->line($this->posxdiscount - 1, $tab_top, $this->posxdiscount - 1, $tab_top + $tab_height);
  1519. if (empty($hidetop)) {
  1520. $pdf->SetXY($this->posxdiscount - 1, $tab_top + 1);
  1521. $pdf->MultiCell($this->posxprogress - $this->posxdiscount + 1, 2, $outputlangs->transnoentities("ReductionShort"), '', 'C');
  1522. }
  1523. }
  1524. if ($this->situationinvoice) {
  1525. $pdf->line($this->posxprogress - 1, $tab_top, $this->posxprogress - 1, $tab_top + $tab_height);
  1526. if (empty($hidetop)) {
  1527. $pdf->SetXY($this->posxprogress, $tab_top + 1);
  1528. $pdf->MultiCell($this->postotalht - $this->posxprogress, 2, $outputlangs->transnoentities("ProgressShort"), '', 'C');
  1529. }
  1530. }
  1531. $pdf->line($this->postotalht, $tab_top, $this->postotalht, $tab_top + $tab_height);
  1532. if (empty($hidetop)) {
  1533. $pdf->SetXY($this->postotalht - 1, $tab_top + 1);
  1534. $pdf->MultiCell(30, 2, $outputlangs->transnoentities("TotalHT"), '', 'C');
  1535. }
  1536. }
  1537. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  1538. /**
  1539. * Show top header of page.
  1540. *
  1541. * @param TCPDF $pdf Object PDF
  1542. * @param Facture $object Object to show
  1543. * @param int $showaddress 0=no, 1=yes
  1544. * @param Translate $outputlangs Object lang for output
  1545. * @param Translate $outputlangsbis Object lang for output bis
  1546. * @return int top shift of linked object lines
  1547. */
  1548. protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $outputlangsbis = null)
  1549. {
  1550. global $conf, $langs;
  1551. $ltrdirection = 'L';
  1552. if ($outputlangs->trans("DIRECTION") == 'rtl') $ltrdirection = 'R';
  1553. // Load traductions files required by page
  1554. $outputlangs->loadLangs(array("main", "bills", "propal", "companies"));
  1555. $default_font_size = pdf_getPDFFontSize($outputlangs);
  1556. pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
  1557. $pdf->SetTextColor(0, 0, 60);
  1558. $pdf->SetFont('', 'B', $default_font_size + 3);
  1559. $w = 110;
  1560. $posy = $this->marge_haute;
  1561. $posx = $this->page_largeur - $this->marge_droite - $w;
  1562. $pdf->SetXY($this->marge_gauche, $posy);
  1563. // Logo
  1564. if (empty($conf->global->PDF_DISABLE_MYCOMPANY_LOGO)) {
  1565. if ($this->emetteur->logo) {
  1566. $logodir = $conf->mycompany->dir_output;
  1567. if (!empty($conf->mycompany->multidir_output[$object->entity])) {
  1568. $logodir = $conf->mycompany->multidir_output[$object->entity];
  1569. }
  1570. if (empty($conf->global->MAIN_PDF_USE_LARGE_LOGO)) {
  1571. $logo = $logodir.'/logos/thumbs/'.$this->emetteur->logo_small;
  1572. } else {
  1573. $logo = $logodir.'/logos/'.$this->emetteur->logo;
  1574. }
  1575. if (is_readable($logo)) {
  1576. $height = pdf_getHeightForLogo($logo);
  1577. $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
  1578. } else {
  1579. $pdf->SetTextColor(200, 0, 0);
  1580. $pdf->SetFont('', 'B', $default_font_size - 2);
  1581. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ErrorLogoFileNotFound", $logo), 0, 'L');
  1582. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ErrorGoToGlobalSetup"), 0, 'L');
  1583. }
  1584. } else {
  1585. $text = $this->emetteur->name;
  1586. $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
  1587. }
  1588. }
  1589. $pdf->SetFont('', 'B', $default_font_size + 3);
  1590. $pdf->SetXY($posx, $posy);
  1591. $pdf->SetTextColor(0, 0, 60);
  1592. $title = $outputlangs->transnoentities("PdfInvoiceTitle");
  1593. if ($object->type == 1) {
  1594. $title = $outputlangs->transnoentities("InvoiceReplacement");
  1595. }
  1596. if ($object->type == 2) {
  1597. $title = $outputlangs->transnoentities("InvoiceAvoir");
  1598. }
  1599. if ($object->type == 3) {
  1600. $title = $outputlangs->transnoentities("InvoiceDeposit");
  1601. }
  1602. if ($object->type == 4) {
  1603. $title = $outputlangs->transnoentities("InvoiceProForma");
  1604. }
  1605. if ($this->situationinvoice) {
  1606. $title = $outputlangs->transnoentities("PDFInvoiceSituation");
  1607. }
  1608. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && is_object($outputlangsbis)) {
  1609. $title .= ' - ';
  1610. if ($object->type == 0) {
  1611. if ($this->situationinvoice) {
  1612. $title .= $outputlangsbis->transnoentities("PDFInvoiceSituation");
  1613. }
  1614. $title .= $outputlangsbis->transnoentities("PdfInvoiceTitle");
  1615. } elseif ($object->type == 1) {
  1616. $title .= $outputlangsbis->transnoentities("InvoiceReplacement");
  1617. } elseif ($object->type == 2) {
  1618. $title .= $outputlangsbis->transnoentities("InvoiceAvoir");
  1619. } elseif ($object->type == 3) {
  1620. $title .= $outputlangsbis->transnoentities("InvoiceDeposit");
  1621. } elseif ($object->type == 4) {
  1622. $title .= $outputlangsbis->transnoentities("InvoiceProForma");
  1623. }
  1624. }
  1625. $title .= ' '.$outputlangs->convToOutputCharset($object->ref);
  1626. if ($object->statut == $object::STATUS_DRAFT) {
  1627. $pdf->SetTextColor(128, 0, 0);
  1628. $title .= ' - '.$outputlangs->transnoentities("NotValidated");
  1629. }
  1630. $pdf->MultiCell($w, 3, $title, '', 'R');
  1631. $pdf->SetFont('', 'B', $default_font_size);
  1632. /*
  1633. $posy += 5;
  1634. $pdf->SetXY($posx, $posy);
  1635. $pdf->SetTextColor(0, 0, 60);
  1636. $textref = $outputlangs->transnoentities("Ref")." : ".$outputlangs->convToOutputCharset($object->ref);
  1637. if ($object->statut == $object::STATUS_DRAFT) {
  1638. $pdf->SetTextColor(128, 0, 0);
  1639. $textref .= ' - '.$outputlangs->transnoentities("NotValidated");
  1640. }
  1641. $pdf->MultiCell($w, 4, $textref, '', 'R');*/
  1642. $posy += 3;
  1643. $pdf->SetFont('', '', $default_font_size - 2);
  1644. if ($object->ref_client) {
  1645. $posy += 4;
  1646. $pdf->SetXY($posx, $posy);
  1647. $pdf->SetTextColor(0, 0, 60);
  1648. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("RefCustomer")." : ".$outputlangs->convToOutputCharset($object->ref_client), '', 'R');
  1649. }
  1650. if (!empty($conf->global->PDF_SHOW_PROJECT_TITLE)) {
  1651. $object->fetch_projet();
  1652. if (!empty($object->project->ref)) {
  1653. $posy += 3;
  1654. $pdf->SetXY($posx, $posy);
  1655. $pdf->SetTextColor(0, 0, 60);
  1656. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("Project")." : ".(empty($object->project->title) ? '' : $object->project->title), '', 'R');
  1657. }
  1658. }
  1659. if (!empty($conf->global->PDF_SHOW_PROJECT)) {
  1660. $object->fetch_projet();
  1661. if (!empty($object->project->ref)) {
  1662. $outputlangs->load("projects");
  1663. $posy += 3;
  1664. $pdf->SetXY($posx, $posy);
  1665. $pdf->SetTextColor(0, 0, 60);
  1666. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("RefProject")." : ".(empty($object->project->ref) ? '' : $object->project->ref), '', 'R');
  1667. }
  1668. }
  1669. $objectidnext = $object->getIdReplacingInvoice('validated');
  1670. if ($object->type == 0 && $objectidnext) {
  1671. $objectreplacing = new Facture($this->db);
  1672. $objectreplacing->fetch($objectidnext);
  1673. $posy += 3;
  1674. $pdf->SetXY($posx, $posy);
  1675. $pdf->SetTextColor(0, 0, 60);
  1676. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ReplacementByInvoice").' : '.$outputlangs->convToOutputCharset($objectreplacing->ref), '', 'R');
  1677. }
  1678. if ($object->type == 1) {
  1679. $objectreplaced = new Facture($this->db);
  1680. $objectreplaced->fetch($object->fk_facture_source);
  1681. $posy += 4;
  1682. $pdf->SetXY($posx, $posy);
  1683. $pdf->SetTextColor(0, 0, 60);
  1684. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ReplacementInvoice").' : '.$outputlangs->convToOutputCharset($objectreplaced->ref), '', 'R');
  1685. }
  1686. if ($object->type == 2 && !empty($object->fk_facture_source)) {
  1687. $objectreplaced = new Facture($this->db);
  1688. $objectreplaced->fetch($object->fk_facture_source);
  1689. $posy += 3;
  1690. $pdf->SetXY($posx, $posy);
  1691. $pdf->SetTextColor(0, 0, 60);
  1692. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("CorrectionInvoice").' : '.$outputlangs->convToOutputCharset($objectreplaced->ref), '', 'R');
  1693. }
  1694. $posy += 4;
  1695. $pdf->SetXY($posx, $posy);
  1696. $pdf->SetTextColor(0, 0, 60);
  1697. $title = $outputlangs->transnoentities("DateInvoice");
  1698. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && is_object($outputlangsbis)) {
  1699. $title .= ' - '.$outputlangsbis->transnoentities("DateInvoice");
  1700. }
  1701. $pdf->MultiCell($w, 3, $title." : ".dol_print_date($object->date, "day", false, $outputlangs, true), '', 'R');
  1702. if (!empty($conf->global->INVOICE_POINTOFTAX_DATE)) {
  1703. $posy += 4;
  1704. $pdf->SetXY($posx, $posy);
  1705. $pdf->SetTextColor(0, 0, 60);
  1706. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("DatePointOfTax")." : ".dol_print_date($object->date_pointoftax, "day", false, $outputlangs), '', 'R');
  1707. }
  1708. if ($object->type != 2) {
  1709. $posy += 3;
  1710. $pdf->SetXY($posx, $posy);
  1711. $pdf->SetTextColor(0, 0, 60);
  1712. $title = $outputlangs->transnoentities("DateDue");
  1713. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && is_object($outputlangsbis)) {
  1714. $title .= ' - '.$outputlangsbis->transnoentities("DateDue");
  1715. }
  1716. $pdf->MultiCell($w, 3, $title." : ".dol_print_date($object->date_lim_reglement, "day", false, $outputlangs, true), '', 'R');
  1717. }
  1718. if (empty($conf->global->MAIN_PDF_HIDE_CUSTOMER_CODE) && $object->thirdparty->code_client) {
  1719. $posy += 3;
  1720. $pdf->SetXY($posx, $posy);
  1721. $pdf->SetTextColor(0, 0, 60);
  1722. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("CustomerCode")." : ".$outputlangs->transnoentities($object->thirdparty->code_client), '', 'R');
  1723. }
  1724. // Get contact
  1725. if (!empty($conf->global->DOC_SHOW_FIRST_SALES_REP)) {
  1726. $arrayidcontact = $object->getIdContact('internal', 'SALESREPFOLL');
  1727. if (count($arrayidcontact) > 0) {
  1728. $usertmp = new User($this->db);
  1729. $usertmp->fetch($arrayidcontact[0]);
  1730. $posy += 4;
  1731. $pdf->SetXY($posx, $posy);
  1732. $pdf->SetTextColor(0, 0, 60);
  1733. $pdf->MultiCell($w, 3, $langs->transnoentities("SalesRepresentative")." : ".$usertmp->getFullName($langs), '', 'R');
  1734. }
  1735. }
  1736. $posy += 1;
  1737. $top_shift = 0;
  1738. // Show list of linked objects
  1739. $current_y = $pdf->getY();
  1740. $posy = pdf_writeLinkedObjects($pdf, $object, $outputlangs, $posx, $posy, $w, 3, 'R', $default_font_size);
  1741. if ($current_y < $pdf->getY()) {
  1742. $top_shift = $pdf->getY() - $current_y;
  1743. }
  1744. if ($showaddress) {
  1745. // Sender properties
  1746. $carac_emetteur = pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, '', 0, 'source', $object);
  1747. // Show sender
  1748. $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
  1749. $posy += $top_shift;
  1750. $posx = $this->marge_gauche;
  1751. if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  1752. $posx = $this->page_largeur - $this->marge_droite - 80;
  1753. }
  1754. $hautcadre = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 38 : 40;
  1755. $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 82;
  1756. // Show sender frame
  1757. if (empty($conf->global->MAIN_PDF_NO_SENDER_FRAME)) {
  1758. $pdf->SetTextColor(0, 0, 0);
  1759. $pdf->SetFont('', '', $default_font_size - 2);
  1760. $pdf->SetXY($posx, $posy - 5);
  1761. $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities("BillFrom"), 0, $ltrdirection);
  1762. $pdf->SetXY($posx, $posy);
  1763. $pdf->SetFillColor(230, 230, 230);
  1764. $pdf->MultiCell($widthrecbox, $hautcadre, "", 0, 'R', 1);
  1765. $pdf->SetTextColor(0, 0, 60);
  1766. }
  1767. // Show sender name
  1768. if (empty($conf->global->MAIN_PDF_HIDE_SENDER_NAME)) {
  1769. $pdf->SetXY($posx + 2, $posy + 3);
  1770. $pdf->SetFont('', 'B', $default_font_size);
  1771. $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0, $ltrdirection);
  1772. $posy = $pdf->getY();
  1773. }
  1774. // Show sender information
  1775. $pdf->SetXY($posx + 2, $posy);
  1776. $pdf->SetFont('', '', $default_font_size - 1);
  1777. $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0, $ltrdirection);
  1778. // If BILLING contact defined on invoice, we use it
  1779. $usecontact = false;
  1780. $arrayidcontact = $object->getIdContact('external', 'BILLING');
  1781. if (count($arrayidcontact) > 0) {
  1782. $usecontact = true;
  1783. $result = $object->fetch_contact($arrayidcontact[0]);
  1784. }
  1785. // Recipient name
  1786. if ($usecontact && ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)))) {
  1787. $thirdparty = $object->contact;
  1788. } else {
  1789. $thirdparty = $object->thirdparty;
  1790. }
  1791. $carac_client_name = pdfBuildThirdpartyName($thirdparty, $outputlangs);
  1792. $mode = 'target';
  1793. $carac_client = pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, ($usecontact ? $object->contact : ''), $usecontact, $mode, $object);
  1794. // Show recipient
  1795. $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 100;
  1796. if ($this->page_largeur < 210) {
  1797. $widthrecbox = 84; // To work with US executive format
  1798. }
  1799. $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
  1800. $posy += $top_shift;
  1801. $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
  1802. if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  1803. $posx = $this->marge_gauche;
  1804. }
  1805. // Show recipient frame
  1806. if (empty($conf->global->MAIN_PDF_NO_RECIPENT_FRAME)) {
  1807. $pdf->SetTextColor(0, 0, 0);
  1808. $pdf->SetFont('', '', $default_font_size - 2);
  1809. $pdf->SetXY($posx + 2, $posy - 5);
  1810. $pdf->MultiCell($widthrecbox - 2, 5, $outputlangs->transnoentities("BillTo"), 0, $ltrdirection);
  1811. $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
  1812. }
  1813. // Show recipient name
  1814. $pdf->SetXY($posx + 2, $posy + 3);
  1815. $pdf->SetFont('', 'B', $default_font_size);
  1816. $pdf->MultiCell($widthrecbox - 2, 2, $carac_client_name, 0, $ltrdirection);
  1817. $posy = $pdf->getY();
  1818. // Show recipient information
  1819. $pdf->SetFont('', '', $default_font_size - 1);
  1820. $pdf->SetXY($posx + 2, $posy);
  1821. $pdf->MultiCell($widthrecbox - 2, 4, $carac_client, 0, $ltrdirection);
  1822. // Show shipping address
  1823. if (getDolGlobalInt('INVOICE_SHOW_SHIPPING_ADDRESS')) {
  1824. $idaddressshipping = $object->getIdContact('external', 'SHIPPING');
  1825. if (!empty($idaddressshipping)) {
  1826. $contactshipping = $object->fetch_Contact($idaddressshipping[0]);
  1827. $companystatic = new Societe($this->db);
  1828. $companystatic->fetch($object->contact->fk_soc);
  1829. $carac_client_name_shipping=pdfBuildThirdpartyName($object->contact, $outputlangs);
  1830. $carac_client_shipping = pdf_build_address($outputlangs, $this->emetteur, $companystatic, $object->contact, $usecontact, 'target', $object);
  1831. } else {
  1832. $carac_client_name_shipping=pdfBuildThirdpartyName($object->thirdparty, $outputlangs);
  1833. $carac_client_shipping=pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, '', 0, 'target', $object);;
  1834. }
  1835. if (!empty($carac_client_shipping) && (isset($object->contact->socid) && $object->contact->socid != $object->socid)) {
  1836. $posy += $hautcadre;
  1837. // Show shipping frame
  1838. $pdf->SetXY($posx + 2, $posy - 5);
  1839. $pdf->SetFont('', '', $default_font_size - 2);
  1840. $pdf->MultiCell($widthrecbox, '', $outputlangs->transnoentities('ShippingTo'), 0, 'L', 0);
  1841. $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
  1842. // Show shipping name
  1843. $pdf->SetXY($posx + 2, $posy + 3);
  1844. $pdf->SetFont('', 'B', $default_font_size);
  1845. $pdf->MultiCell($widthrecbox - 2, 2, $carac_client_name_shipping, '', 'L');
  1846. $posy = $pdf->getY();
  1847. // Show shipping information
  1848. $pdf->SetXY($posx+2, $posy);
  1849. $pdf->SetFont('', '', $default_font_size - 1);
  1850. $pdf->MultiCell($widthrecbox - 2, 2, $carac_client_shipping, '', 'L');
  1851. $top_shift += $hautcadre;
  1852. }
  1853. }
  1854. }
  1855. $pdf->SetTextColor(0, 0, 0);
  1856. return $top_shift;
  1857. }
  1858. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  1859. /**
  1860. * Show footer of page. Need this->emetteur object
  1861. *
  1862. * @param TCPDF $pdf PDF
  1863. * @param Facture $object Object to show
  1864. * @param Translate $outputlangs Object lang for output
  1865. * @param int $hidefreetext 1=Hide free text
  1866. * @return int Return height of bottom margin including footer text
  1867. */
  1868. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  1869. {
  1870. $showdetails = getDolGlobalInt('MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS', 0);
  1871. return pdf_pagefoot($pdf, $outputlangs, 'INVOICE_FREE_TEXT', $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext, $this->page_largeur, $this->watermark);
  1872. }
  1873. }