pdf_sponge.modules.php 104 KB

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