pdf_sponge.modules.php 103 KB

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