pdf_sponge.modules.php 101 KB

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