pdf_sponge.modules.php 97 KB

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