pdf_sponge.modules.php 103 KB

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