pdf.lib.php 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  1. <?php
  2. /* Copyright (C) 2006-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  4. * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  5. * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2017 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  8. * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
  9. * Copyright (C) 2012-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  10. * Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
  11. * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
  12. * Copyright (C) 2015-2016 Marcos García <marcosgdf@gmail.com>
  13. * Copyright (C) 2019 Lenin Rivas <lenin.rivas@servcom-it.com>
  14. * Copyright (C) 2020 Nicolas ZABOURI <info@inovea-conseil.com>
  15. * Copyright (C) 2021-2022 Anthony Berton <bertonanthony@gmail.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  29. * or see https://www.gnu.org/
  30. */
  31. /**
  32. * \file htdocs/core/lib/pdf.lib.php
  33. * \brief Set of functions used for PDF generation
  34. * \ingroup core
  35. */
  36. include_once DOL_DOCUMENT_ROOT.'/core/lib/signature.lib.php';
  37. /**
  38. * Return array head with list of tabs to view object informations.
  39. *
  40. * @return array head array with tabs
  41. */
  42. function pdf_admin_prepare_head()
  43. {
  44. global $langs, $conf, $user;
  45. $h = 0;
  46. $head = array();
  47. $head[$h][0] = DOL_URL_ROOT.'/admin/pdf.php';
  48. $head[$h][1] = $langs->trans("Parameters");
  49. $head[$h][2] = 'general';
  50. $h++;
  51. // Show more tabs from modules
  52. // Entries must be declared in modules descriptor with line
  53. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  54. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  55. complete_head_from_modules($conf, $langs, null, $head, $h, 'pdf_admin');
  56. if (!empty($conf->propal->enabled)) {
  57. $head[$h][0] = DOL_URL_ROOT.'/admin/pdf_other.php';
  58. $head[$h][1] = $langs->trans("Other");
  59. $head[$h][2] = 'other';
  60. $h++;
  61. }
  62. complete_head_from_modules($conf, $langs, null, $head, $h, 'pdf_admin', 'remove');
  63. return $head;
  64. }
  65. /**
  66. * Return array with format properties of default PDF format
  67. *
  68. * @param Translate $outputlangs Output lang to use to autodetect output format if we need 'auto' detection
  69. * @param string $mode 'setup' = Use setup, 'auto' = Force autodetection whatever is setup
  70. * @return array Array('width'=>w,'height'=>h,'unit'=>u);
  71. */
  72. function pdf_getFormat(Translate $outputlangs = null, $mode = 'setup')
  73. {
  74. global $conf, $db, $langs;
  75. dol_syslog("pdf_getFormat Get paper format with mode=".$mode." MAIN_PDF_FORMAT=".(empty($conf->global->MAIN_PDF_FORMAT) ? 'null' : $conf->global->MAIN_PDF_FORMAT)." outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang : 'null')." and langs->defaultlang=".(is_object($langs) ? $langs->defaultlang : 'null'));
  76. // Default value if setup was not done and/or entry into c_paper_format not defined
  77. $width = 210;
  78. $height = 297;
  79. $unit = 'mm';
  80. if ($mode == 'auto' || empty($conf->global->MAIN_PDF_FORMAT) || $conf->global->MAIN_PDF_FORMAT == 'auto') {
  81. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  82. $pdfformat = dol_getDefaultFormat($outputlangs);
  83. } else {
  84. $pdfformat = $conf->global->MAIN_PDF_FORMAT;
  85. }
  86. $sql = "SELECT code, label, width, height, unit FROM ".MAIN_DB_PREFIX."c_paper_format";
  87. $sql .= " WHERE code = '".$db->escape($pdfformat)."'";
  88. $resql = $db->query($sql);
  89. if ($resql) {
  90. $obj = $db->fetch_object($resql);
  91. if ($obj) {
  92. $width = (int) $obj->width;
  93. $height = (int) $obj->height;
  94. $unit = $obj->unit;
  95. }
  96. }
  97. //print "pdfformat=".$pdfformat." width=".$width." height=".$height." unit=".$unit;
  98. return array('width'=>$width, 'height'=>$height, 'unit'=>$unit);
  99. }
  100. /**
  101. * Return a PDF instance object. We create a FPDI instance that instantiate TCPDF.
  102. *
  103. * @param string $format Array(width,height). Keep empty to use default setup.
  104. * @param string $metric Unit of format ('mm')
  105. * @param string $pagetype 'P' or 'l'
  106. * @return TCPDF PDF object
  107. */
  108. function pdf_getInstance($format = '', $metric = 'mm', $pagetype = 'P')
  109. {
  110. global $conf;
  111. // Define constant for TCPDF
  112. if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
  113. define('K_TCPDF_EXTERNAL_CONFIG', 1); // this avoid using tcpdf_config file
  114. define('K_PATH_CACHE', DOL_DATA_ROOT.'/admin/temp/');
  115. define('K_PATH_URL_CACHE', DOL_DATA_ROOT.'/admin/temp/');
  116. dol_mkdir(K_PATH_CACHE);
  117. define('K_BLANK_IMAGE', '_blank.png');
  118. define('PDF_PAGE_FORMAT', 'A4');
  119. define('PDF_PAGE_ORIENTATION', 'P');
  120. define('PDF_CREATOR', 'TCPDF');
  121. define('PDF_AUTHOR', 'TCPDF');
  122. define('PDF_HEADER_TITLE', 'TCPDF Example');
  123. define('PDF_HEADER_STRING', "by Dolibarr ERP CRM");
  124. define('PDF_UNIT', 'mm');
  125. define('PDF_MARGIN_HEADER', 5);
  126. define('PDF_MARGIN_FOOTER', 10);
  127. define('PDF_MARGIN_TOP', 27);
  128. define('PDF_MARGIN_BOTTOM', 25);
  129. define('PDF_MARGIN_LEFT', 15);
  130. define('PDF_MARGIN_RIGHT', 15);
  131. define('PDF_FONT_NAME_MAIN', 'helvetica');
  132. define('PDF_FONT_SIZE_MAIN', 10);
  133. define('PDF_FONT_NAME_DATA', 'helvetica');
  134. define('PDF_FONT_SIZE_DATA', 8);
  135. define('PDF_FONT_MONOSPACED', 'courier');
  136. define('PDF_IMAGE_SCALE_RATIO', 1.25);
  137. define('HEAD_MAGNIFICATION', 1.1);
  138. define('K_CELL_HEIGHT_RATIO', 1.25);
  139. define('K_TITLE_MAGNIFICATION', 1.3);
  140. define('K_SMALL_RATIO', 2 / 3);
  141. define('K_THAI_TOPCHARS', true);
  142. define('K_TCPDF_CALLS_IN_HTML', true);
  143. if (!empty($conf->global->TCPDF_THROW_ERRORS_INSTEAD_OF_DIE)) {
  144. define('K_TCPDF_THROW_EXCEPTION_ERROR', true);
  145. } else {
  146. define('K_TCPDF_THROW_EXCEPTION_ERROR', false);
  147. }
  148. }
  149. // Load TCPDF
  150. require_once TCPDF_PATH.'tcpdf.php';
  151. // We need to instantiate tcpdi object (instead of tcpdf) to use merging features. But we can disable it (this will break all merge features).
  152. if (empty($conf->global->MAIN_DISABLE_TCPDI)) {
  153. require_once TCPDI_PATH.'tcpdi.php';
  154. }
  155. //$arrayformat=pdf_getFormat();
  156. //$format=array($arrayformat['width'],$arrayformat['height']);
  157. //$metric=$arrayformat['unit'];
  158. $pdfa = false; // PDF-1.3
  159. if (!empty($conf->global->PDF_USE_A)) {
  160. $pdfa = $conf->global->PDF_USE_A; // PDF/A-1 ou PDF/A-3
  161. }
  162. if (class_exists('TCPDI')) {
  163. $pdf = new TCPDI($pagetype, $metric, $format, true, 'UTF-8', false, $pdfa);
  164. } else {
  165. $pdf = new TCPDF($pagetype, $metric, $format, true, 'UTF-8', false, $pdfa);
  166. }
  167. // Protection and encryption of pdf
  168. if (!empty($conf->global->PDF_SECURITY_ENCRYPTION)) {
  169. /* Permission supported by TCPDF
  170. - print : Print the document;
  171. - modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';
  172. - copy : Copy or otherwise extract text and graphics from the document;
  173. - annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);
  174. - fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;
  175. - extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);
  176. - assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;
  177. - print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.
  178. - owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.
  179. */
  180. // For TCPDF, we specify permission we want to block
  181. $pdfrights = (!empty($conf->global->PDF_SECURITY_ENCRYPTION_RIGHTS) ?json_decode($conf->global->PDF_SECURITY_ENCRYPTION_RIGHTS, true) : array('modify', 'copy')); // Json format in llx_const
  182. // Password for the end user
  183. $pdfuserpass = (!empty($conf->global->PDF_SECURITY_ENCRYPTION_USERPASS) ? $conf->global->PDF_SECURITY_ENCRYPTION_USERPASS : '');
  184. // Password of the owner, created randomly if not defined
  185. $pdfownerpass = (!empty($conf->global->PDF_SECURITY_ENCRYPTION_OWNERPASS) ? $conf->global->PDF_SECURITY_ENCRYPTION_OWNERPASS : null);
  186. // For encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit
  187. $encstrength = (!empty($conf->global->PDF_SECURITY_ENCRYPTION_STRENGTH) ? $conf->global->PDF_SECURITY_ENCRYPTION_STRENGTH : 0);
  188. // Array of recipients containing public-key certificates ('c') and permissions ('p').
  189. // For example: array(array('c' => 'file://../examples/data/cert/tcpdf.crt', 'p' => array('print')))
  190. $pubkeys = (!empty($conf->global->PDF_SECURITY_ENCRYPTION_PUBKEYS) ?json_decode($conf->global->PDF_SECURITY_ENCRYPTION_PUBKEYS, true) : null); // Json format in llx_const
  191. $pdf->SetProtection($pdfrights, $pdfuserpass, $pdfownerpass, $encstrength, $pubkeys);
  192. }
  193. return $pdf;
  194. }
  195. /**
  196. * Return if pdf file is protected/encrypted
  197. *
  198. * @param string $pathoffile Path of file
  199. * @return boolean True or false
  200. */
  201. function pdf_getEncryption($pathoffile)
  202. {
  203. require_once TCPDF_PATH.'tcpdf_parser.php';
  204. $isencrypted = false;
  205. $content = file_get_contents($pathoffile);
  206. //ob_start();
  207. @($parser = new \TCPDF_PARSER(ltrim($content)));
  208. list($xref, $data) = $parser->getParsedData();
  209. unset($parser);
  210. //ob_end_clean();
  211. if (isset($xref['trailer']['encrypt'])) {
  212. $isencrypted = true; // Secured pdf file are currently not supported
  213. }
  214. if (empty($data)) {
  215. $isencrypted = true; // Object list not found. Possible secured file
  216. }
  217. return $isencrypted;
  218. }
  219. /**
  220. * Return font name to use for PDF generation
  221. *
  222. * @param Translate $outputlangs Output langs object
  223. * @return string Name of font to use
  224. */
  225. function pdf_getPDFFont($outputlangs)
  226. {
  227. global $conf;
  228. if (!empty($conf->global->MAIN_PDF_FORCE_FONT)) {
  229. return $conf->global->MAIN_PDF_FORCE_FONT;
  230. }
  231. $font = 'Helvetica'; // By default, for FPDI, or ISO language on TCPDF
  232. if (class_exists('TCPDF')) { // If TCPDF on, we can use an UTF8 one like DejaVuSans if required (slower)
  233. if ($outputlangs->trans('FONTFORPDF') != 'FONTFORPDF') {
  234. $font = $outputlangs->trans('FONTFORPDF');
  235. }
  236. }
  237. return $font;
  238. }
  239. /**
  240. * Return font size to use for PDF generation
  241. *
  242. * @param Translate $outputlangs Output langs object
  243. * @return int Size of font to use
  244. */
  245. function pdf_getPDFFontSize($outputlangs)
  246. {
  247. global $conf;
  248. $size = 10; // By default, for FPDI or ISO language on TCPDF
  249. if (class_exists('TCPDF')) { // If TCPDF on, we can use an UTF8 font like DejaVuSans if required (slower)
  250. if ($outputlangs->trans('FONTSIZEFORPDF') != 'FONTSIZEFORPDF') {
  251. $size = (int) $outputlangs->trans('FONTSIZEFORPDF');
  252. }
  253. }
  254. if (!empty($conf->global->MAIN_PDF_FORCE_FONT_SIZE)) {
  255. $size = $conf->global->MAIN_PDF_FORCE_FONT_SIZE;
  256. }
  257. return $size;
  258. }
  259. /**
  260. * Return height to use for Logo onto PDF
  261. *
  262. * @param string $logo Full path to logo file to use
  263. * @param bool $url Image with url (true or false)
  264. * @return number
  265. */
  266. function pdf_getHeightForLogo($logo, $url = false)
  267. {
  268. global $conf;
  269. $height = (empty($conf->global->MAIN_DOCUMENTS_LOGO_HEIGHT) ? 20 : $conf->global->MAIN_DOCUMENTS_LOGO_HEIGHT);
  270. $maxwidth = 130;
  271. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  272. $tmp = dol_getImageSize($logo, $url);
  273. if ($tmp['height']) {
  274. $width = round($height * $tmp['width'] / $tmp['height']);
  275. if ($width > $maxwidth) {
  276. $height = $height * $maxwidth / $width;
  277. }
  278. }
  279. //print $tmp['width'].' '.$tmp['height'].' '.$width; exit;
  280. return $height;
  281. }
  282. /**
  283. * Function to try to calculate height of a HTML Content
  284. *
  285. * @param TCPDF $pdf PDF initialized object
  286. * @param string $htmlcontent HTML Contect
  287. * @return int Height
  288. * @see getStringHeight()
  289. */
  290. function pdfGetHeightForHtmlContent(&$pdf, $htmlcontent)
  291. {
  292. // store current object
  293. $pdf->startTransaction();
  294. // store starting values
  295. $start_y = $pdf->GetY();
  296. //var_dump($start_y);
  297. $start_page = $pdf->getPage();
  298. // call printing functions with content
  299. $pdf->writeHTMLCell(0, 0, 0, $start_y, $htmlcontent, 0, 1, false, true, 'J', true);
  300. // get the new Y
  301. $end_y = $pdf->GetY();
  302. $end_page = $pdf->getPage();
  303. // calculate height
  304. $height = 0;
  305. if ($end_page == $start_page) {
  306. $height = $end_y - $start_y;
  307. } else {
  308. for ($page = $start_page; $page <= $end_page; ++$page) {
  309. $pdf->setPage($page);
  310. $tmpm = $pdf->getMargins();
  311. $tMargin = $tmpm['top'];
  312. if ($page == $start_page) {
  313. // first page
  314. $height = $pdf->getPageHeight() - $start_y - $pdf->getBreakMargin();
  315. } elseif ($page == $end_page) {
  316. // last page
  317. $height = $end_y - $tMargin;
  318. } else {
  319. $height = $pdf->getPageHeight() - $tMargin - $pdf->getBreakMargin();
  320. }
  321. }
  322. }
  323. // restore previous object
  324. $pdf = $pdf->rollbackTransaction();
  325. return $height;
  326. }
  327. /**
  328. * Returns the name of the thirdparty
  329. *
  330. * @param Societe|Contact $thirdparty Contact or thirdparty
  331. * @param Translate $outputlangs Output language
  332. * @param int $includealias 1=Include alias name after name
  333. * @return string String with name of thirdparty (+ alias if requested)
  334. */
  335. function pdfBuildThirdpartyName($thirdparty, Translate $outputlangs, $includealias = 0)
  336. {
  337. global $conf;
  338. // Recipient name
  339. $socname = '';
  340. if ($thirdparty instanceof Societe) {
  341. $socname = $thirdparty->name;
  342. if (($includealias || getDolGlobalInt('PDF_INCLUDE_ALIAS_IN_THIRDPARTY_NAME')) && !empty($thirdparty->name_alias)) {
  343. if (getDolGlobalInt('PDF_INCLUDE_ALIAS_IN_THIRDPARTY_NAME') == 2) {
  344. $socname = $thirdparty->name_alias." - ".$thirdparty->name;
  345. } else {
  346. $socname = $thirdparty->name." - ".$thirdparty->name_alias;
  347. }
  348. }
  349. } elseif ($thirdparty instanceof Contact) {
  350. $socname = $thirdparty->socname;
  351. if (($includealias || getDolGlobalInt('PDF_INCLUDE_ALIAS_IN_THIRDPARTY_NAME')) && !empty($thirdparty->name_alias)) {
  352. // TODO PDF_INCLUDE_ALIAS_IN_THIRDPARTY_NAME not completely implemented
  353. }
  354. } else {
  355. throw new InvalidArgumentException('Parameter 1 $thirdparty is not a Societe nor Contact');
  356. }
  357. return $outputlangs->convToOutputCharset($socname);
  358. }
  359. /**
  360. * Return a string with full address formated for output on documents
  361. *
  362. * @param Translate $outputlangs Output langs object
  363. * @param Societe $sourcecompany Source company object
  364. * @param Societe|string|null $targetcompany Target company object
  365. * @param Contact|string|null $targetcontact Target contact object
  366. * @param int $usecontact Use contact instead of company
  367. * @param string $mode Address type ('source', 'target', 'targetwithdetails', 'targetwithdetails_xxx': target but include also phone/fax/email/url)
  368. * @param Object $object Object we want to build document for
  369. * @return string String with full address
  370. */
  371. function pdf_build_address($outputlangs, $sourcecompany, $targetcompany = '', $targetcontact = '', $usecontact = 0, $mode = 'source', $object = null)
  372. {
  373. global $conf, $hookmanager;
  374. if ($mode == 'source' && !is_object($sourcecompany)) {
  375. return -1;
  376. }
  377. if ($mode == 'target' && !is_object($targetcompany)) {
  378. return -1;
  379. }
  380. if (!empty($sourcecompany->state_id) && empty($sourcecompany->state)) {
  381. $sourcecompany->state = getState($sourcecompany->state_id);
  382. }
  383. if (!empty($targetcompany->state_id) && empty($targetcompany->state)) {
  384. $targetcompany->state = getState($targetcompany->state_id);
  385. }
  386. $reshook = 0;
  387. $stringaddress = '';
  388. if (is_object($hookmanager)) {
  389. $parameters = array('sourcecompany' => &$sourcecompany, 'targetcompany' => &$targetcompany, 'targetcontact' => &$targetcontact, 'outputlangs' => $outputlangs, 'mode' => $mode, 'usecontact' => $usecontact);
  390. $action = '';
  391. $reshook = $hookmanager->executeHooks('pdf_build_address', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  392. $stringaddress .= $hookmanager->resPrint;
  393. }
  394. if (empty($reshook)) {
  395. if ($mode == 'source') {
  396. $withCountry = 0;
  397. if (!empty($sourcecompany->country_code) && ($targetcompany->country_code != $sourcecompany->country_code)) {
  398. $withCountry = 1;
  399. }
  400. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($sourcecompany, $withCountry, "\n", $outputlangs))."\n";
  401. if (empty($conf->global->MAIN_PDF_DISABLESOURCEDETAILS)) {
  402. // Phone
  403. if ($sourcecompany->phone) {
  404. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("PhoneShort").": ".$outputlangs->convToOutputCharset($sourcecompany->phone);
  405. }
  406. // Fax
  407. if ($sourcecompany->fax) {
  408. $stringaddress .= ($stringaddress ? ($sourcecompany->phone ? " - " : "\n") : '').$outputlangs->transnoentities("Fax").": ".$outputlangs->convToOutputCharset($sourcecompany->fax);
  409. }
  410. // EMail
  411. if ($sourcecompany->email) {
  412. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Email").": ".$outputlangs->convToOutputCharset($sourcecompany->email);
  413. }
  414. // Web
  415. if ($sourcecompany->url) {
  416. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Web").": ".$outputlangs->convToOutputCharset($sourcecompany->url);
  417. }
  418. }
  419. // Intra VAT
  420. if (!empty($conf->global->MAIN_TVAINTRA_IN_SOURCE_ADDRESS)) {
  421. if ($sourcecompany->tva_intra) {
  422. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("VATIntraShort").': '.$outputlangs->convToOutputCharset($sourcecompany->tva_intra);
  423. }
  424. }
  425. // Professionnal Ids
  426. $reg = array();
  427. if (!empty($conf->global->MAIN_PROFID1_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof1)) {
  428. $tmp = $outputlangs->transcountrynoentities("ProfId1", $sourcecompany->country_code);
  429. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  430. $tmp = $reg[1];
  431. }
  432. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof1);
  433. }
  434. if (!empty($conf->global->MAIN_PROFID2_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof2)) {
  435. $tmp = $outputlangs->transcountrynoentities("ProfId2", $sourcecompany->country_code);
  436. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  437. $tmp = $reg[1];
  438. }
  439. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof2);
  440. }
  441. if (!empty($conf->global->MAIN_PROFID3_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof3)) {
  442. $tmp = $outputlangs->transcountrynoentities("ProfId3", $sourcecompany->country_code);
  443. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  444. $tmp = $reg[1];
  445. }
  446. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof3);
  447. }
  448. if (!empty($conf->global->MAIN_PROFID4_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof4)) {
  449. $tmp = $outputlangs->transcountrynoentities("ProfId4", $sourcecompany->country_code);
  450. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  451. $tmp = $reg[1];
  452. }
  453. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof4);
  454. }
  455. if (!empty($conf->global->MAIN_PROFID5_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof5)) {
  456. $tmp = $outputlangs->transcountrynoentities("ProfId5", $sourcecompany->country_code);
  457. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  458. $tmp = $reg[1];
  459. }
  460. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof5);
  461. }
  462. if (!empty($conf->global->MAIN_PROFID6_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof6)) {
  463. $tmp = $outputlangs->transcountrynoentities("ProfId6", $sourcecompany->country_code);
  464. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  465. $tmp = $reg[1];
  466. }
  467. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof6);
  468. }
  469. if (!empty($conf->global->PDF_ADD_MORE_AFTER_SOURCE_ADDRESS)) {
  470. $stringaddress .= ($stringaddress ? "\n" : '').$conf->global->PDF_ADD_MORE_AFTER_SOURCE_ADDRESS;
  471. }
  472. }
  473. if ($mode == 'target' || preg_match('/targetwithdetails/', $mode)) {
  474. if ($usecontact) {
  475. if (is_object($targetcontact)) {
  476. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($targetcontact->getFullName($outputlangs, 1));
  477. if (!empty($targetcontact->address)) {
  478. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($targetcontact));
  479. } else {
  480. $companytouseforaddress = $targetcompany;
  481. // Contact on a thirdparty that is a different thirdparty than the thirdparty of object
  482. if ($targetcontact->socid > 0 && $targetcontact->socid != $targetcompany->id) {
  483. $targetcontact->fetch_thirdparty();
  484. $companytouseforaddress = $targetcontact->thirdparty;
  485. }
  486. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($companytouseforaddress));
  487. }
  488. // Country
  489. if (!empty($targetcontact->country_code) && $targetcontact->country_code != $sourcecompany->country_code) {
  490. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$targetcontact->country_code));
  491. } elseif (empty($targetcontact->country_code) && !empty($targetcompany->country_code) && ($targetcompany->country_code != $sourcecompany->country_code)) {
  492. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$targetcompany->country_code));
  493. }
  494. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || preg_match('/targetwithdetails/', $mode)) {
  495. // Phone
  496. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_phone/', $mode)) {
  497. if (!empty($targetcontact->phone_pro) || !empty($targetcontact->phone_mobile)) {
  498. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Phone").": ";
  499. }
  500. if (!empty($targetcontact->phone_pro)) {
  501. $stringaddress .= $outputlangs->convToOutputCharset($targetcontact->phone_pro);
  502. }
  503. if (!empty($targetcontact->phone_pro) && !empty($targetcontact->phone_mobile)) {
  504. $stringaddress .= " / ";
  505. }
  506. if (!empty($targetcontact->phone_mobile)) {
  507. $stringaddress .= $outputlangs->convToOutputCharset($targetcontact->phone_mobile);
  508. }
  509. }
  510. // Fax
  511. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_fax/', $mode)) {
  512. if ($targetcontact->fax) {
  513. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Fax").": ".$outputlangs->convToOutputCharset($targetcontact->fax);
  514. }
  515. }
  516. // EMail
  517. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_email/', $mode)) {
  518. if ($targetcontact->email) {
  519. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Email").": ".$outputlangs->convToOutputCharset($targetcontact->email);
  520. }
  521. }
  522. // Web
  523. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_url/', $mode)) {
  524. if ($targetcontact->url) {
  525. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Web").": ".$outputlangs->convToOutputCharset($targetcontact->url);
  526. }
  527. }
  528. }
  529. }
  530. } else {
  531. if (is_object($targetcompany)) {
  532. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($targetcompany));
  533. // Country
  534. if (!empty($targetcompany->country_code) && $targetcompany->country_code != $sourcecompany->country_code) {
  535. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$targetcompany->country_code));
  536. }
  537. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || preg_match('/targetwithdetails/', $mode)) {
  538. // Phone
  539. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_phone/', $mode)) {
  540. if (!empty($targetcompany->phone)) {
  541. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Phone").": ";
  542. }
  543. if (!empty($targetcompany->phone)) {
  544. $stringaddress .= $outputlangs->convToOutputCharset($targetcompany->phone);
  545. }
  546. }
  547. // Fax
  548. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_fax/', $mode)) {
  549. if ($targetcompany->fax) {
  550. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Fax").": ".$outputlangs->convToOutputCharset($targetcompany->fax);
  551. }
  552. }
  553. // EMail
  554. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_email/', $mode)) {
  555. if ($targetcompany->email) {
  556. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Email").": ".$outputlangs->convToOutputCharset($targetcompany->email);
  557. }
  558. }
  559. // Web
  560. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_url/', $mode)) {
  561. if ($targetcompany->url) {
  562. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Web").": ".$outputlangs->convToOutputCharset($targetcompany->url);
  563. }
  564. }
  565. }
  566. }
  567. }
  568. // Intra VAT
  569. if (empty($conf->global->MAIN_TVAINTRA_NOT_IN_ADDRESS)) {
  570. if ($targetcompany->tva_intra) {
  571. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("VATIntraShort").': '.$outputlangs->convToOutputCharset($targetcompany->tva_intra);
  572. }
  573. }
  574. // Professionnal Ids
  575. if (!empty($conf->global->MAIN_PROFID1_IN_ADDRESS) && !empty($targetcompany->idprof1)) {
  576. $tmp = $outputlangs->transcountrynoentities("ProfId1", $targetcompany->country_code);
  577. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  578. $tmp = $reg[1];
  579. }
  580. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof1);
  581. }
  582. if (!empty($conf->global->MAIN_PROFID2_IN_ADDRESS) && !empty($targetcompany->idprof2)) {
  583. $tmp = $outputlangs->transcountrynoentities("ProfId2", $targetcompany->country_code);
  584. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  585. $tmp = $reg[1];
  586. }
  587. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof2);
  588. }
  589. if (!empty($conf->global->MAIN_PROFID3_IN_ADDRESS) && !empty($targetcompany->idprof3)) {
  590. $tmp = $outputlangs->transcountrynoentities("ProfId3", $targetcompany->country_code);
  591. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  592. $tmp = $reg[1];
  593. }
  594. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof3);
  595. }
  596. if (!empty($conf->global->MAIN_PROFID4_IN_ADDRESS) && !empty($targetcompany->idprof4)) {
  597. $tmp = $outputlangs->transcountrynoentities("ProfId4", $targetcompany->country_code);
  598. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  599. $tmp = $reg[1];
  600. }
  601. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof4);
  602. }
  603. if (!empty($conf->global->MAIN_PROFID5_IN_ADDRESS) && !empty($targetcompany->idprof5)) {
  604. $tmp = $outputlangs->transcountrynoentities("ProfId5", $targetcompany->country_code);
  605. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  606. $tmp = $reg[1];
  607. }
  608. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof5);
  609. }
  610. if (!empty($conf->global->MAIN_PROFID6_IN_ADDRESS) && !empty($targetcompany->idprof6)) {
  611. $tmp = $outputlangs->transcountrynoentities("ProfId6", $targetcompany->country_code);
  612. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  613. $tmp = $reg[1];
  614. }
  615. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof6);
  616. }
  617. // Public note
  618. if (!empty($conf->global->MAIN_PUBLIC_NOTE_IN_ADDRESS)) {
  619. if ($mode == 'source' && !empty($sourcecompany->note_public)) {
  620. $stringaddress .= ($stringaddress ? "\n" : '').dol_string_nohtmltag($sourcecompany->note_public);
  621. }
  622. if (($mode == 'target' || preg_match('/targetwithdetails/', $mode)) && !empty($targetcompany->note_public)) {
  623. $stringaddress .= ($stringaddress ? "\n" : '').dol_string_nohtmltag($targetcompany->note_public);
  624. }
  625. }
  626. }
  627. }
  628. return $stringaddress;
  629. }
  630. /**
  631. * Show header of page for PDF generation
  632. *
  633. * @param TCPDF $pdf Object PDF
  634. * @param Translate $outputlangs Object lang for output
  635. * @param int $page_height Height of page
  636. * @return void
  637. */
  638. function pdf_pagehead(&$pdf, $outputlangs, $page_height)
  639. {
  640. global $conf;
  641. // Add a background image on document only if good setup of const
  642. if (!empty($conf->global->MAIN_USE_BACKGROUND_ON_PDF) && ($conf->global->MAIN_USE_BACKGROUND_ON_PDF != '-1')) { // Warning, this option make TCPDF generation being crazy and some content disappeared behind the image
  643. $filepath = $conf->mycompany->dir_output.'/logos/'.$conf->global->MAIN_USE_BACKGROUND_ON_PDF;
  644. if (file_exists($filepath)) {
  645. $pdf->SetAutoPageBreak(0, 0); // Disable auto pagebreak before adding image
  646. $pdf->Image($filepath, (isset($conf->global->MAIN_USE_BACKGROUND_ON_PDF_X) ? $conf->global->MAIN_USE_BACKGROUND_ON_PDF_X : 0), (isset($conf->global->MAIN_USE_BACKGROUND_ON_PDF_Y) ? $conf->global->MAIN_USE_BACKGROUND_ON_PDF_Y : 0), 0, $page_height);
  647. $pdf->SetPageMark(); // This option avoid to have the images missing on some pages
  648. $pdf->SetAutoPageBreak(1, 0); // Restore pagebreak
  649. }
  650. }
  651. }
  652. /**
  653. * Return array of possible substitutions for PDF content (without external module substitutions).
  654. *
  655. * @param Translate $outputlangs Output language
  656. * @param array $exclude Array of family keys we want to exclude. For example array('mycompany', 'object', 'date', 'user', ...)
  657. * @param Object $object Object
  658. * @param int $onlykey 1=Do not calculate some heavy values of keys (performance enhancement when we need only the keys), 2=Values are truncated and html sanitized (to use for help tooltip)
  659. * @return array Array of substitutions
  660. */
  661. function pdf_getSubstitutionArray($outputlangs, $exclude = null, $object = null, $onlykey = 0)
  662. {
  663. $substitutionarray = getCommonSubstitutionArray($outputlangs, $onlykey, $exclude, $object);
  664. $substitutionarray['__FROM_NAME__'] = '__FROM_NAME__';
  665. $substitutionarray['__FROM_EMAIL__'] = '__FROM_EMAIL__';
  666. return $substitutionarray;
  667. }
  668. /**
  669. * Add a draft watermark on PDF files
  670. *
  671. * @param TCPDF $pdf Object PDF
  672. * @param Translate $outputlangs Object lang
  673. * @param int $h Height of PDF
  674. * @param int $w Width of PDF
  675. * @param string $unit Unit of height (mm, pt, ...)
  676. * @param string $text Text to show
  677. * @return void
  678. */
  679. function pdf_watermark(&$pdf, $outputlangs, $h, $w, $unit, $text)
  680. {
  681. global $langs, $mysoc, $user;
  682. // Print Draft Watermark
  683. if ($unit == 'pt') {
  684. $k = 1;
  685. } elseif ($unit == 'mm') {
  686. $k = 72 / 25.4;
  687. } elseif ($unit == 'cm') {
  688. $k = 72 / 2.54;
  689. } elseif ($unit == 'in') {
  690. $k = 72;
  691. }
  692. // Make substitution
  693. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, null);
  694. complete_substitutions_array($substitutionarray, $outputlangs, null);
  695. $text = make_substitutions($text, $substitutionarray, $outputlangs);
  696. $text = $outputlangs->convToOutputCharset($text);
  697. $savx = $pdf->getX();
  698. $savy = $pdf->getY();
  699. $watermark_angle = atan($h / $w) / 2;
  700. $watermark_x_pos = 0;
  701. $watermark_y_pos = $h / 3;
  702. $watermark_x = $w / 2;
  703. $watermark_y = $h / 3;
  704. $pdf->SetFont('', 'B', 40);
  705. $pdf->SetTextColor(255, 192, 203);
  706. //rotate
  707. $pdf->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', cos($watermark_angle), sin($watermark_angle), -sin($watermark_angle), cos($watermark_angle), $watermark_x * $k, ($h - $watermark_y) * $k, -$watermark_x * $k, -($h - $watermark_y) * $k));
  708. //print watermark
  709. $pdf->SetXY($watermark_x_pos, $watermark_y_pos);
  710. $pdf->Cell($w - 20, 25, $outputlangs->convToOutputCharset($text), "", 2, "C", 0);
  711. //antirotate
  712. $pdf->_out('Q');
  713. $pdf->SetXY($savx, $savy);
  714. }
  715. /**
  716. * Show bank informations for PDF generation
  717. *
  718. * @param TCPDF $pdf Object PDF
  719. * @param Translate $outputlangs Object lang
  720. * @param int $curx X
  721. * @param int $cury Y
  722. * @param Account $account Bank account object
  723. * @param int $onlynumber Output only number (bank+desk+key+number according to country, but without name of bank and domiciliation)
  724. * @param int $default_font_size Default font size
  725. * @return float The Y PDF position
  726. */
  727. function pdf_bank(&$pdf, $outputlangs, $curx, $cury, $account, $onlynumber = 0, $default_font_size = 10)
  728. {
  729. global $mysoc, $conf;
  730. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbank.class.php';
  731. $diffsizetitle = (empty($conf->global->PDF_DIFFSIZE_TITLE) ? 3 : $conf->global->PDF_DIFFSIZE_TITLE);
  732. $diffsizecontent = (empty($conf->global->PDF_DIFFSIZE_CONTENT) ? 4 : $conf->global->PDF_DIFFSIZE_CONTENT);
  733. $pdf->SetXY($curx, $cury);
  734. if (empty($onlynumber)) {
  735. $pdf->SetFont('', 'B', $default_font_size - $diffsizetitle);
  736. $pdf->MultiCell(100, 3, $outputlangs->transnoentities('PaymentByTransferOnThisBankAccount').':', 0, 'L', 0);
  737. $cury += 4;
  738. }
  739. $outputlangs->load("banks");
  740. // Use correct name of bank id according to country
  741. $bickey = "BICNumber";
  742. if ($account->getCountryCode() == 'IN') {
  743. $bickey = "SWIFT";
  744. }
  745. // Get format of bank account according to its country
  746. $usedetailedbban = $account->useDetailedBBAN();
  747. //$onlynumber=0; $usedetailedbban=1; // For tests
  748. if ($usedetailedbban) {
  749. $savcurx = $curx;
  750. if (empty($onlynumber)) {
  751. $pdf->SetFont('', '', $default_font_size - $diffsizecontent);
  752. $pdf->SetXY($curx, $cury);
  753. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("Bank").': '.$outputlangs->convToOutputCharset($account->bank), 0, 'L', 0);
  754. $cury += 3;
  755. }
  756. if (empty($conf->global->PDF_BANK_HIDE_NUMBER_SHOW_ONLY_BICIBAN)) { // Note that some countries still need bank number, BIC/IBAN not enougth for them
  757. // Note:
  758. // bank = code_banque (FR), sort code (GB, IR. Example: 12-34-56)
  759. // desk = code guichet (FR), used only when $usedetailedbban = 1
  760. // number = account number
  761. // key = check control key used only when $usedetailedbban = 1
  762. if (empty($onlynumber)) {
  763. $pdf->line($curx + 1, $cury + 1, $curx + 1, $cury + 6);
  764. }
  765. foreach ($account->getFieldsToShow() as $val) {
  766. $pdf->SetXY($curx, $cury + 4);
  767. $pdf->SetFont('', '', $default_font_size - 3);
  768. if ($val == 'BankCode') {
  769. // Bank code
  770. $tmplength = 18;
  771. $content = $account->code_banque;
  772. } elseif ($val == 'DeskCode') {
  773. // Desk
  774. $tmplength = 18;
  775. $content = $account->code_guichet;
  776. } elseif ($val == 'BankAccountNumber') {
  777. // Number
  778. $tmplength = 24;
  779. $content = $account->number;
  780. } elseif ($val == 'BankAccountNumberKey') {
  781. // Key
  782. $tmplength = 15;
  783. $content = $account->cle_rib;
  784. } elseif ($val == 'IBAN' || $val == 'BIC') {
  785. // Key
  786. $tmplength = 0;
  787. $content = '';
  788. } else {
  789. dol_print_error($account->db, 'Unexpected value for getFieldsToShow: '.$val);
  790. break;
  791. }
  792. $pdf->MultiCell($tmplength, 3, $outputlangs->convToOutputCharset($content), 0, 'C', 0);
  793. $pdf->SetXY($curx, $cury + 1);
  794. $curx += $tmplength;
  795. $pdf->SetFont('', 'B', $default_font_size - $diffsizecontent);
  796. $pdf->MultiCell($tmplength, 3, $outputlangs->transnoentities($val), 0, 'C', 0);
  797. if (empty($onlynumber)) {
  798. $pdf->line($curx, $cury + 1, $curx, $cury + 7);
  799. }
  800. }
  801. $curx = $savcurx;
  802. $cury += 8;
  803. }
  804. } else {
  805. $pdf->SetFont('', 'B', $default_font_size - $diffsizecontent);
  806. $pdf->SetXY($curx, $cury);
  807. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("Bank").': '.$outputlangs->convToOutputCharset($account->bank), 0, 'L', 0);
  808. $cury += 3;
  809. $pdf->SetFont('', 'B', $default_font_size - $diffsizecontent);
  810. $pdf->SetXY($curx, $cury);
  811. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("BankAccountNumber").': '.$outputlangs->convToOutputCharset($account->number), 0, 'L', 0);
  812. $cury += 3;
  813. if ($diffsizecontent <= 2) {
  814. $cury += 1;
  815. }
  816. }
  817. $pdf->SetFont('', '', $default_font_size - $diffsizecontent);
  818. if (empty($onlynumber) && !empty($account->domiciliation)) {
  819. $pdf->SetXY($curx, $cury);
  820. $val = $outputlangs->transnoentities("Residence").': '.$outputlangs->convToOutputCharset($account->domiciliation);
  821. $pdf->MultiCell(100, 3, $val, 0, 'L', 0);
  822. //$nboflines=dol_nboflines_bis($val,120);
  823. //$cury+=($nboflines*3)+2;
  824. $tmpy = $pdf->getStringHeight(100, $val);
  825. $cury += $tmpy;
  826. }
  827. if (!empty($account->proprio)) {
  828. $pdf->SetXY($curx, $cury);
  829. $val = $outputlangs->transnoentities("BankAccountOwner").': '.$outputlangs->convToOutputCharset($account->proprio);
  830. $pdf->MultiCell(100, 3, $val, 0, 'L', 0);
  831. $tmpy = $pdf->getStringHeight(100, $val);
  832. $cury += $tmpy;
  833. } elseif (!$usedetailedbban) {
  834. $cury += 1;
  835. }
  836. // Use correct name of bank id according to country
  837. $ibankey = FormBank::getIBANLabel($account);
  838. if (!empty($account->iban)) {
  839. //Remove whitespaces to ensure we are dealing with the format we expect
  840. $ibanDisplay_temp = str_replace(' ', '', $outputlangs->convToOutputCharset($account->iban));
  841. $ibanDisplay = "";
  842. $nbIbanDisplay_temp = dol_strlen($ibanDisplay_temp);
  843. for ($i = 0; $i < $nbIbanDisplay_temp; $i++) {
  844. $ibanDisplay .= $ibanDisplay_temp[$i];
  845. if ($i % 4 == 3 && $i > 0) {
  846. $ibanDisplay .= " ";
  847. }
  848. }
  849. $pdf->SetFont('', 'B', $default_font_size - 3);
  850. $pdf->SetXY($curx, $cury);
  851. $pdf->MultiCell(100, 3, $outputlangs->transnoentities($ibankey).': '.$ibanDisplay, 0, 'L', 0);
  852. $cury += 3;
  853. }
  854. if (!empty($account->bic)) {
  855. $pdf->SetFont('', 'B', $default_font_size - 3);
  856. $pdf->SetXY($curx, $cury);
  857. $pdf->MultiCell(100, 3, $outputlangs->transnoentities($bickey).': '.$outputlangs->convToOutputCharset($account->bic), 0, 'L', 0);
  858. }
  859. return $pdf->getY();
  860. }
  861. /**
  862. * Show footer of page for PDF generation
  863. *
  864. * @param TCPDF $pdf The PDF factory
  865. * @param Translate $outputlangs Object lang for output
  866. * @param string $paramfreetext Constant name of free text
  867. * @param Societe $fromcompany Object company
  868. * @param int $marge_basse Margin bottom we use for the autobreak
  869. * @param int $marge_gauche Margin left (no more used)
  870. * @param int $page_hauteur Page height
  871. * @param Object $object Object shown in PDF
  872. * @param int $showdetails Show company adress details into footer (0=Nothing, 1=Show address, 2=Show managers, 3=Both)
  873. * @param int $hidefreetext 1=Hide free text, 0=Show free text
  874. * @param int $page_largeur Page width
  875. * @param int $watermark Watermark text to print on page
  876. * @return int Return height of bottom margin including footer text
  877. */
  878. function pdf_pagefoot(&$pdf, $outputlangs, $paramfreetext, $fromcompany, $marge_basse, $marge_gauche, $page_hauteur, $object, $showdetails = 0, $hidefreetext = 0, $page_largeur = 0, $watermark = '')
  879. {
  880. global $conf, $user, $mysoc, $hookmanager;
  881. $outputlangs->load("dict");
  882. $line = '';
  883. $reg = array();
  884. $dims = $pdf->getPageDimensions();
  885. // Line of free text
  886. if (empty($hidefreetext) && !empty($conf->global->$paramfreetext)) {
  887. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
  888. // More substitution keys
  889. $substitutionarray['__FROM_NAME__'] = $fromcompany->name;
  890. $substitutionarray['__FROM_EMAIL__'] = $fromcompany->email;
  891. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  892. $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray, $outputlangs);
  893. // Make a change into HTML code to allow to include images from medias directory.
  894. // <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
  895. // become
  896. // <img alt="" src="'.DOL_DATA_ROOT.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
  897. $newfreetext = preg_replace('/(<img.*src=")[^\"]*viewimage\.php[^\"]*modulepart=medias[^\"]*file=([^\"]*)("[^\/]*\/>)/', '\1file:/'.DOL_DATA_ROOT.'/medias/\2\3', $newfreetext);
  898. $line .= $outputlangs->convToOutputCharset($newfreetext);
  899. }
  900. // First line of company infos
  901. $line1 = "";
  902. $line2 = "";
  903. $line3 = "";
  904. $line4 = "";
  905. if ($showdetails == 1 || $showdetails == 3) {
  906. // Company name
  907. if ($fromcompany->name) {
  908. $line1 .= ($line1 ? " - " : "").$outputlangs->transnoentities("RegisteredOffice").": ".$fromcompany->name;
  909. }
  910. // Address
  911. if ($fromcompany->address) {
  912. $line1 .= ($line1 ? " - " : "").str_replace("\n", ", ", $fromcompany->address);
  913. }
  914. // Zip code
  915. if ($fromcompany->zip) {
  916. $line1 .= ($line1 ? " - " : "").$fromcompany->zip;
  917. }
  918. // Town
  919. if ($fromcompany->town) {
  920. $line1 .= ($line1 ? " " : "").$fromcompany->town;
  921. }
  922. // Country
  923. if ($fromcompany->country) {
  924. $line1 .= ($line1 ? ", " : "").$fromcompany->country;
  925. }
  926. // Phone
  927. if ($fromcompany->phone) {
  928. $line2 .= ($line2 ? " - " : "").$outputlangs->transnoentities("Phone").": ".$fromcompany->phone;
  929. }
  930. // Fax
  931. if ($fromcompany->fax) {
  932. $line2 .= ($line2 ? " - " : "").$outputlangs->transnoentities("Fax").": ".$fromcompany->fax;
  933. }
  934. // URL
  935. if ($fromcompany->url) {
  936. $line2 .= ($line2 ? " - " : "").$fromcompany->url;
  937. }
  938. // Email
  939. if ($fromcompany->email) {
  940. $line2 .= ($line2 ? " - " : "").$fromcompany->email;
  941. }
  942. }
  943. if ($showdetails == 2 || $showdetails == 3 || (!empty($fromcompany->country_code) && $fromcompany->country_code == 'DE')) {
  944. // Managers
  945. if ($fromcompany->managers) {
  946. $line2 .= ($line2 ? " - " : "").$fromcompany->managers;
  947. }
  948. }
  949. // Line 3 of company infos
  950. // Juridical status
  951. if (!empty($fromcompany->forme_juridique_code) && $fromcompany->forme_juridique_code) {
  952. $line3 .= ($line3 ? " - " : "").$outputlangs->convToOutputCharset(getFormeJuridiqueLabel($fromcompany->forme_juridique_code));
  953. }
  954. // Capital
  955. if (!empty($fromcompany->capital)) {
  956. $tmpamounttoshow = price2num($fromcompany->capital); // This field is a free string or a float
  957. if (is_numeric($tmpamounttoshow) && $tmpamounttoshow > 0) {
  958. $line3 .= ($line3 ? " - " : "").$outputlangs->transnoentities("CapitalOf", price($tmpamounttoshow, 0, $outputlangs, 0, 0, 0, $conf->currency));
  959. } elseif (!empty($fromcompany->capital)) {
  960. $line3 .= ($line3 ? " - " : "").$outputlangs->transnoentities("CapitalOf", $fromcompany->capital, $outputlangs);
  961. }
  962. }
  963. // Prof Id 1
  964. if (!empty($fromcompany->idprof1) && $fromcompany->idprof1 && ($fromcompany->country_code != 'FR' || !$fromcompany->idprof2)) {
  965. $field = $outputlangs->transcountrynoentities("ProfId1", $fromcompany->country_code);
  966. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  967. $field = $reg[1];
  968. }
  969. $line3 .= ($line3 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof1);
  970. }
  971. // Prof Id 2
  972. if (!empty($fromcompany->idprof2) && $fromcompany->idprof2) {
  973. $field = $outputlangs->transcountrynoentities("ProfId2", $fromcompany->country_code);
  974. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  975. $field = $reg[1];
  976. }
  977. $line3 .= ($line3 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof2);
  978. }
  979. // Line 4 of company infos
  980. // Prof Id 3
  981. if (!empty($fromcompany->idprof3) && $fromcompany->idprof3) {
  982. $field = $outputlangs->transcountrynoentities("ProfId3", $fromcompany->country_code);
  983. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  984. $field = $reg[1];
  985. }
  986. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof3);
  987. }
  988. // Prof Id 4
  989. if (!empty($fromcompany->idprof4) && $fromcompany->idprof4) {
  990. $field = $outputlangs->transcountrynoentities("ProfId4", $fromcompany->country_code);
  991. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  992. $field = $reg[1];
  993. }
  994. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof4);
  995. }
  996. // Prof Id 5
  997. if (!empty($fromcompany->idprof5) && $fromcompany->idprof5) {
  998. $field = $outputlangs->transcountrynoentities("ProfId5", $fromcompany->country_code);
  999. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  1000. $field = $reg[1];
  1001. }
  1002. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof5);
  1003. }
  1004. // Prof Id 6
  1005. if (!empty($fromcompany->idprof6) && $fromcompany->idprof6) {
  1006. $field = $outputlangs->transcountrynoentities("ProfId6", $fromcompany->country_code);
  1007. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  1008. $field = $reg[1];
  1009. }
  1010. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof6);
  1011. }
  1012. // IntraCommunautary VAT
  1013. if (!empty($fromcompany->tva_intra) && $fromcompany->tva_intra != '') {
  1014. $line4 .= ($line4 ? " - " : "").$outputlangs->transnoentities("VATIntraShort").": ".$outputlangs->convToOutputCharset($fromcompany->tva_intra);
  1015. }
  1016. $pdf->SetFont('', '', 7);
  1017. $pdf->SetDrawColor(224, 224, 224);
  1018. // Option for footer text color
  1019. if (!empty($conf->global->PDF_FOOTER_TEXT_COLOR)) {
  1020. list($r, $g, $b) = sscanf($conf->global->PDF_FOOTER_TEXT_COLOR, '%d, %d, %d');
  1021. $pdf->SetTextColor($r, $g, $b);
  1022. }
  1023. // The start of the bottom of this page footer is positioned according to # of lines
  1024. $freetextheight = 0;
  1025. if ($line) { // Free text
  1026. //$line="sample text<br>\nfd<strong>sf</strong>sdf<br>\nghfghg<br>";
  1027. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) {
  1028. $width = 20000;
  1029. $align = 'L'; // By default, ask a manual break: We use a large value 20000, to not have automatic wrap. This make user understand, he need to add CR on its text.
  1030. if (!empty($conf->global->MAIN_USE_AUTOWRAP_ON_FREETEXT)) {
  1031. $width = 200;
  1032. $align = 'C';
  1033. }
  1034. $freetextheight = $pdf->getStringHeight($width, $line);
  1035. } else {
  1036. $freetextheight = pdfGetHeightForHtmlContent($pdf, dol_htmlentitiesbr($line, 1, 'UTF-8', 0)); // New method (works for HTML content)
  1037. //print '<br>'.$freetextheight;exit;
  1038. }
  1039. }
  1040. // For customize footer
  1041. if (is_object($hookmanager)) {
  1042. $parameters = array('line1' => $line1, 'line2' => $line2, 'line3' => $line3, 'line4' => $line4, 'outputlangs'=>$outputlangs);
  1043. $action = '';
  1044. $hookmanager->executeHooks('pdf_pagefoot', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1045. if (!empty($hookmanager->resPrint) && $hidefreetext == 0) {
  1046. $mycustomfooter = $hookmanager->resPrint;
  1047. $mycustomfooterheight = pdfGetHeightForHtmlContent($pdf, dol_htmlentitiesbr($mycustomfooter, 1, 'UTF-8', 0));
  1048. $marginwithfooter = $marge_basse + $freetextheight + $mycustomfooterheight;
  1049. $posy = $marginwithfooter + 0;
  1050. // Option for footer background color (without freetext zone)
  1051. if (getDolGlobalString('PDF_FOOTER_BACKGROUND_COLOR')) {
  1052. list($r, $g, $b) = sscanf($conf->global->PDF_FOOTER_BACKGROUND_COLOR, '%d, %d, %d');
  1053. $pdf->SetAutoPageBreak(0, 0); // Disable auto pagebreak
  1054. $pdf->Rect(0, $dims['hk'] - $posy + $freetextheight, $dims['wk'] + 1, $marginwithfooter + 1, 'F', '', $fill_color = array($r, $g, $b));
  1055. $pdf->SetAutoPageBreak(1, 0); // Restore pagebreak
  1056. }
  1057. if ($line) { // Free text
  1058. $pdf->SetXY($dims['lm'], -$posy);
  1059. if (!getDolGlobalString('PDF_ALLOW_HTML_FOR_FREE_TEXT')) { // by default
  1060. $pdf->MultiCell(0, 3, $line, 0, $align, 0);
  1061. } else {
  1062. $pdf->writeHTMLCell($pdf->page_largeur - $pdf->margin_left - $pdf->margin_right, $freetextheight, $dims['lm'], $dims['hk'] - $marginwithfooter, dol_htmlentitiesbr($line, 1, 'UTF-8', 0));
  1063. }
  1064. $posy -= $freetextheight;
  1065. }
  1066. $pdf->SetY(-$posy);
  1067. // Hide footer line if footer background color is set
  1068. if (!getDolGlobalString('PDF_FOOTER_BACKGROUND_COLOR')) {
  1069. $pdf->line($dims['lm'], $dims['hk'] - $posy, $dims['wk'] - $dims['rm'], $dims['hk'] - $posy);
  1070. }
  1071. // Option for set top margin height of footer after freetext
  1072. if (getDolGlobalString('PDF_FOOTER_TOP_MARGIN') || getDolGlobalString('PDF_FOOTER_TOP_MARGIN') === '0') {
  1073. // TODO Remove this case. Height should be good automatically, only $posy-- should be required.
  1074. $posy -= getDolGlobalInt('PDF_FOOTER_TOP_MARGIN');
  1075. } else {
  1076. $posy--;
  1077. }
  1078. if (getDolGlobalString('PDF_FOOTER_DISABLE_PAGEBREAK') === '1') { $pdf->SetAutoPageBreak(0, 0); } // Option for disable auto pagebreak
  1079. $pdf->writeHTMLCell($pdf->page_largeur - $pdf->margin_left - $pdf->margin_right, $mycustomfooterheight, $dims['lm'], $dims['hk'] - $posy, dol_htmlentitiesbr($mycustomfooter, 1, 'UTF-8', 0));
  1080. if (getDolGlobalString('PDF_FOOTER_DISABLE_PAGEBREAK') === '1') { $pdf->SetAutoPageBreak(1, 0); } // Restore pagebreak
  1081. $posy -= $mycustomfooterheight - 3;
  1082. } else {
  1083. // Else default footer
  1084. $marginwithfooter = $marge_basse + $freetextheight + (!empty($line1) ? 3 : 0) + (!empty($line2) ? 3 : 0) + (!empty($line3) ? 3 : 0) + (!empty($line4) ? 3 : 0);
  1085. $posy = $marginwithfooter + 0;
  1086. // Option for footer background color (without freetext zone)
  1087. if (getDolGlobalString('PDF_FOOTER_BACKGROUND_COLOR')) {
  1088. list($r, $g, $b) = sscanf($conf->global->PDF_FOOTER_BACKGROUND_COLOR, '%d, %d, %d');
  1089. $pdf->SetAutoPageBreak(0, 0); // Disable auto pagebreak
  1090. $pdf->Rect(0, $dims['hk'] - $posy + $freetextheight, $dims['wk'] + 1, $marginwithfooter + 1, 'F', '', $fill_color = array($r, $g, $b));
  1091. $pdf->SetAutoPageBreak(1, 0); // Restore pagebreak
  1092. }
  1093. if ($line) { // Free text
  1094. $pdf->SetXY($dims['lm'], -$posy);
  1095. if (!getDolGlobalString('PDF_ALLOW_HTML_FOR_FREE_TEXT')) { // by default
  1096. $pdf->MultiCell(0, 3, $line, 0, $align, 0);
  1097. } else {
  1098. $pdf->writeHTMLCell($pdf->page_largeur - $pdf->margin_left - $pdf->margin_right, $freetextheight, $dims['lm'], $dims['hk'] - $marginwithfooter, dol_htmlentitiesbr($line, 1, 'UTF-8', 0));
  1099. }
  1100. $posy -= $freetextheight;
  1101. }
  1102. $pdf->SetY(-$posy);
  1103. // Hide footer line if footer background color is set
  1104. if (!getDolGlobalString('PDF_FOOTER_BACKGROUND_COLOR')) {
  1105. $pdf->line($dims['lm'], $dims['hk'] - $posy, $dims['wk'] - $dims['rm'], $dims['hk'] - $posy);
  1106. }
  1107. // Option for set top margin height of footer after freetext
  1108. if (getDolGlobalString('PDF_FOOTER_TOP_MARGIN') || getDolGlobalString('PDF_FOOTER_TOP_MARGIN') === '0') {
  1109. // TODO Remove this case. Height should be good automatically, only $posy-- should be required.
  1110. $posy -= getDolGlobalString('PDF_FOOTER_TOP_MARGIN');
  1111. } else {
  1112. $posy--;
  1113. }
  1114. if (!empty($line1)) {
  1115. $pdf->SetFont('', 'B', 7);
  1116. $pdf->SetXY($dims['lm'], -$posy);
  1117. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line1, 0, 'C', 0);
  1118. $posy -= 3;
  1119. $pdf->SetFont('', '', 7);
  1120. }
  1121. if (!empty($line2)) {
  1122. $pdf->SetFont('', 'B', 7);
  1123. $pdf->SetXY($dims['lm'], -$posy);
  1124. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line2, 0, 'C', 0);
  1125. $posy -= 3;
  1126. $pdf->SetFont('', '', 7);
  1127. }
  1128. if (!empty($line3)) {
  1129. $pdf->SetXY($dims['lm'], -$posy);
  1130. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line3, 0, 'C', 0);
  1131. }
  1132. if (!empty($line4)) {
  1133. $posy -= 3;
  1134. $pdf->SetXY($dims['lm'], -$posy);
  1135. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line4, 0, 'C', 0);
  1136. }
  1137. }
  1138. }
  1139. // Show page nb only on iso languages (so default Helvetica font)
  1140. if (strtolower(pdf_getPDFFont($outputlangs)) == 'helvetica') {
  1141. $pdf->SetXY($dims['wk'] - $dims['rm'] - 15, -$posy);
  1142. //print 'xxx'.$pdf->PageNo().'-'.$pdf->getAliasNbPages().'-'.$pdf->getAliasNumPage();exit;
  1143. $pdf->MultiCell(15, 2, $pdf->PageNo().'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
  1144. }
  1145. // Show Draft Watermark
  1146. if (!empty($watermark)) {
  1147. pdf_watermark($pdf, $outputlangs, $page_hauteur, $page_largeur, 'mm', $watermark);
  1148. }
  1149. return $marginwithfooter;
  1150. }
  1151. /**
  1152. * Show linked objects for PDF generation
  1153. *
  1154. * @param TCPDF $pdf Object PDF
  1155. * @param object $object Object
  1156. * @param Translate $outputlangs Object lang
  1157. * @param int $posx X
  1158. * @param int $posy Y
  1159. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  1160. * @param float $h Cell minimum height. The cell extends automatically if needed.
  1161. * @param int $align Align
  1162. * @param string $default_font_size Font size
  1163. * @return float The Y PDF position
  1164. */
  1165. function pdf_writeLinkedObjects(&$pdf, $object, $outputlangs, $posx, $posy, $w, $h, $align, $default_font_size)
  1166. {
  1167. $linkedobjects = pdf_getLinkedObjects($object, $outputlangs);
  1168. if (!empty($linkedobjects)) {
  1169. foreach ($linkedobjects as $linkedobject) {
  1170. $reftoshow = $linkedobject["ref_title"].' : '.$linkedobject["ref_value"];
  1171. if (!empty($linkedobject["date_value"])) {
  1172. $reftoshow .= ' / '.$linkedobject["date_value"];
  1173. }
  1174. $posy += 3;
  1175. $pdf->SetXY($posx, $posy);
  1176. $pdf->SetFont('', '', $default_font_size - 2);
  1177. $pdf->MultiCell($w, $h, $reftoshow, '', $align);
  1178. }
  1179. }
  1180. return $pdf->getY();
  1181. }
  1182. /**
  1183. * Output line description into PDF
  1184. *
  1185. * @param TCPDF $pdf PDF object
  1186. * @param Object $object Object
  1187. * @param int $i Current line number
  1188. * @param Translate $outputlangs Object lang for output
  1189. * @param int $w Width
  1190. * @param int $h Height
  1191. * @param int $posx Pos x
  1192. * @param int $posy Pos y
  1193. * @param int $hideref Hide reference
  1194. * @param int $hidedesc Hide description
  1195. * @param int $issupplierline Is it a line for a supplier object ?
  1196. * @return string
  1197. */
  1198. function pdf_writelinedesc(&$pdf, $object, $i, $outputlangs, $w, $h, $posx, $posy, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
  1199. {
  1200. global $db, $conf, $langs, $hookmanager;
  1201. $reshook = 0;
  1202. $result = '';
  1203. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1204. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1205. $special_code = empty($object->lines[$i]->special_code) ? '' : $object->lines[$i]->special_code;
  1206. if (!empty($object->lines[$i]->fk_parent_line)) {
  1207. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1208. }
  1209. $parameters = array('pdf'=>$pdf, 'i'=>$i, 'outputlangs'=>$outputlangs, 'w'=>$w, 'h'=>$h, 'posx'=>$posx, 'posy'=>$posy, 'hideref'=>$hideref, 'hidedesc'=>$hidedesc, 'issupplierline'=>$issupplierline, 'special_code'=>$special_code);
  1210. $action = '';
  1211. $reshook = $hookmanager->executeHooks('pdf_writelinedesc', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1212. if (!empty($hookmanager->resPrint)) {
  1213. $result .= $hookmanager->resPrint;
  1214. }
  1215. }
  1216. if (empty($reshook)) {
  1217. $labelproductservice = pdf_getlinedesc($object, $i, $outputlangs, $hideref, $hidedesc, $issupplierline);
  1218. //var_dump($labelproductservice);exit;
  1219. // Fix bug of some HTML editors that replace links <img src="http://localhostgit/viewimage.php?modulepart=medias&file=image/efd.png" into <img src="http://localhostgit/viewimage.php?modulepart=medias&amp;file=image/efd.png"
  1220. // We make the reverse, so PDF generation has the real URL.
  1221. $nbrep = 0;
  1222. $labelproductservice = preg_replace('/(<img[^>]*src=")([^"]*)(&amp;)([^"]*")/', '\1\2&\4', $labelproductservice, -1, $nbrep);
  1223. //var_dump($labelproductservice);exit;
  1224. // Description
  1225. $pdf->writeHTMLCell($w, $h, $posx, $posy, $outputlangs->convToOutputCharset($labelproductservice), 0, 1, false, true, 'J', true);
  1226. $result .= $labelproductservice;
  1227. }
  1228. return $result;
  1229. }
  1230. /**
  1231. * Return line description translated in outputlangs and encoded into htmlentities and with <br>
  1232. *
  1233. * @param Object $object Object
  1234. * @param int $i Current line number (0 = first line, 1 = second line, ...)
  1235. * @param Translate $outputlangs Object langs for output
  1236. * @param int $hideref Hide reference
  1237. * @param int $hidedesc Hide description
  1238. * @param int $issupplierline Is it a line for a supplier object ?
  1239. * @return string String with line
  1240. */
  1241. function pdf_getlinedesc($object, $i, $outputlangs, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
  1242. {
  1243. global $db, $conf, $langs;
  1244. $idprod = (!empty($object->lines[$i]->fk_product) ? $object->lines[$i]->fk_product : false);
  1245. // Added by MMI Mathieu Moulin iProspective
  1246. // Trim & Strio tags to be sure
  1247. $label = (!empty(trim(strip_tags($object->lines[$i]->label))) ? $object->lines[$i]->label : (!empty($object->lines[$i]->product_label) ? $object->lines[$i]->product_label : ''));
  1248. $desc = (!empty($object->lines[$i]->desc) ? $object->lines[$i]->desc : (!empty($object->lines[$i]->description) ? $object->lines[$i]->description : ''));
  1249. $ref_supplier = (!empty($object->lines[$i]->ref_supplier) ? $object->lines[$i]->ref_supplier : (!empty($object->lines[$i]->ref_fourn) ? $object->lines[$i]->ref_fourn : '')); // TODO Not yet saved for supplier invoices, only supplier orders
  1250. $note = (!empty($object->lines[$i]->note) ? $object->lines[$i]->note : '');
  1251. $dbatch = (!empty($object->lines[$i]->detail_batch) ? $object->lines[$i]->detail_batch : false);
  1252. if ($issupplierline) {
  1253. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  1254. $prodser = new ProductFournisseur($db);
  1255. } else {
  1256. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  1257. $prodser = new Product($db);
  1258. if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
  1259. include_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
  1260. }
  1261. }
  1262. if ($idprod) {
  1263. $prodser->fetch($idprod);
  1264. // If a predefined product and multilang and on other lang, we renamed label with label translated
  1265. if (!empty($conf->global->MAIN_MULTILANGS) && ($outputlangs->defaultlang != $langs->defaultlang)) {
  1266. $translatealsoifmodified = (!empty($conf->global->MAIN_MULTILANG_TRANSLATE_EVEN_IF_MODIFIED)); // By default if value was modified manually, we keep it (no translation because we don't have it)
  1267. // TODO Instead of making a compare to see if param was modified, check that content contains reference translation. If yes, add the added part to the new translation
  1268. // ($textwasnotmodified is replaced with $textwasmodifiedorcompleted and we add completion).
  1269. // Set label
  1270. // If we want another language, and if label is same than default language (we did not force it to a specific value), we can use translation.
  1271. //var_dump($outputlangs->defaultlang.' - '.$langs->defaultlang.' - '.$label.' - '.$prodser->label);exit;
  1272. $textwasnotmodified = ($label == $prodser->label);
  1273. if (!empty($prodser->multilangs[$outputlangs->defaultlang]["label"]) && ($textwasnotmodified || $translatealsoifmodified)) {
  1274. $label = $prodser->multilangs[$outputlangs->defaultlang]["label"];
  1275. }
  1276. // Set desc
  1277. // Manage HTML entities description test because $prodser->description is store with htmlentities but $desc no
  1278. $textwasnotmodified = false;
  1279. if (!empty($desc) && dol_textishtml($desc) && !empty($prodser->description) && dol_textishtml($prodser->description)) {
  1280. $textwasnotmodified = (strpos(dol_html_entity_decode($desc, ENT_QUOTES | ENT_HTML5), dol_html_entity_decode($prodser->description, ENT_QUOTES | ENT_HTML5)) !== false);
  1281. } else {
  1282. $textwasnotmodified = ($desc == $prodser->description);
  1283. }
  1284. if (!empty($prodser->multilangs[$outputlangs->defaultlang]["description"]) && ($textwasnotmodified || $translatealsoifmodified)) {
  1285. $desc = $prodser->multilangs[$outputlangs->defaultlang]["description"];
  1286. }
  1287. // Set note
  1288. $textwasnotmodified = ($note == $prodser->note_public);
  1289. if (!empty($prodser->multilangs[$outputlangs->defaultlang]["other"]) && ($textwasnotmodified || $translatealsoifmodified)) {
  1290. $note = $prodser->multilangs[$outputlangs->defaultlang]["other"];
  1291. }
  1292. }
  1293. } elseif (($object->element == 'facture' || $object->element == 'facturefourn') && preg_match('/^\(DEPOSIT\).+/', $desc)) { // We must not replace '(DEPOSIT)' when it is alone, it will be translated and detailed later
  1294. $desc = str_replace('(DEPOSIT)', $outputlangs->trans('Deposit'), $desc);
  1295. }
  1296. if (empty($conf->global->PDF_HIDE_PRODUCT_LABEL_IN_SUPPLIER_LINES)) {
  1297. // Description short of product line
  1298. $libelleproduitservice = $label;
  1299. if (!empty($libelleproduitservice) && !empty($conf->global->PDF_BOLD_PRODUCT_LABEL)) {
  1300. // Adding <b> may convert the original string into a HTML string. Sowe have to first
  1301. // convert \n into <br> we text is not already HTML.
  1302. if (!dol_textishtml($libelleproduitservice)) {
  1303. $libelleproduitservice = str_replace("\n", '<br>', $libelleproduitservice);
  1304. }
  1305. $libelleproduitservice = '<b>'.$libelleproduitservice.'</b>';
  1306. }
  1307. }
  1308. // Add ref of subproducts
  1309. if (!empty($conf->global->SHOW_SUBPRODUCT_REF_IN_PDF)) {
  1310. $prodser->get_sousproduits_arbo();
  1311. if (!empty($prodser->sousprods) && is_array($prodser->sousprods) && count($prodser->sousprods)) {
  1312. $tmparrayofsubproducts = reset($prodser->sousprods);
  1313. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF)) {
  1314. foreach ($tmparrayofsubproducts as $subprodval) {
  1315. $libelleproduitservice = dol_concatdesc($libelleproduitservice, " * ".$subprodval[3].' ('.$subprodval[1].')');
  1316. }
  1317. } else {
  1318. foreach ($tmparrayofsubproducts as $subprodval) {
  1319. $libelleproduitservice = dol_concatdesc($libelleproduitservice, " * ".$subprodval[5].(($subprodval[5] && $subprodval[3]) ? ' - ' : '').$subprodval[3].' ('.$subprodval[1].')');
  1320. }
  1321. }
  1322. }
  1323. }
  1324. // Description long of product line
  1325. if (!empty($desc) && ($desc != $label)) {
  1326. if ($desc == '(CREDIT_NOTE)' && $object->lines[$i]->fk_remise_except) {
  1327. $discount = new DiscountAbsolute($db);
  1328. $discount->fetch($object->lines[$i]->fk_remise_except);
  1329. $sourceref = !empty($discount->discount_type) ? $discount->ref_invoice_supplier_source : $discount->ref_facture_source;
  1330. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromCreditNote", $sourceref);
  1331. } elseif ($desc == '(DEPOSIT)' && $object->lines[$i]->fk_remise_except) {
  1332. $discount = new DiscountAbsolute($db);
  1333. $discount->fetch($object->lines[$i]->fk_remise_except);
  1334. $sourceref = !empty($discount->discount_type) ? $discount->ref_invoice_supplier_source : $discount->ref_facture_source;
  1335. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromDeposit", $sourceref);
  1336. // Add date of deposit
  1337. if (!empty($conf->global->INVOICE_ADD_DEPOSIT_DATE)) {
  1338. $libelleproduitservice .= ' ('.dol_print_date($discount->datec, 'day', '', $outputlangs).')';
  1339. }
  1340. } elseif ($desc == '(EXCESS RECEIVED)' && $object->lines[$i]->fk_remise_except) {
  1341. $discount = new DiscountAbsolute($db);
  1342. $discount->fetch($object->lines[$i]->fk_remise_except);
  1343. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromExcessReceived", $discount->ref_facture_source);
  1344. } elseif ($desc == '(EXCESS PAID)' && $object->lines[$i]->fk_remise_except) {
  1345. $discount = new DiscountAbsolute($db);
  1346. $discount->fetch($object->lines[$i]->fk_remise_except);
  1347. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromExcessPaid", $discount->ref_invoice_supplier_source);
  1348. } else {
  1349. if ($idprod) {
  1350. // Check if description must be output
  1351. if (!empty($object->element)) {
  1352. $tmpkey = 'MAIN_DOCUMENTS_HIDE_DESCRIPTION_FOR_'.strtoupper($object->element);
  1353. if (!empty($conf->global->$tmpkey)) {
  1354. $hidedesc = 1;
  1355. }
  1356. }
  1357. if (empty($hidedesc)) {
  1358. if (!empty($conf->global->MAIN_DOCUMENTS_DESCRIPTION_FIRST)) {
  1359. $libelleproduitservice = dol_concatdesc($desc, $libelleproduitservice);
  1360. } else {
  1361. if (!empty($conf->global->HIDE_LABEL_VARIANT_PDF) && $prodser->isVariant()) {
  1362. $libelleproduitservice = $desc;
  1363. } else {
  1364. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desc);
  1365. }
  1366. }
  1367. }
  1368. } else {
  1369. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desc);
  1370. }
  1371. }
  1372. }
  1373. // We add ref of product (and supplier ref if defined)
  1374. $prefix_prodserv = "";
  1375. $ref_prodserv = "";
  1376. if (!empty($conf->global->PRODUCT_ADD_TYPE_IN_DOCUMENTS)) { // In standard mode, we do not show this
  1377. if ($prodser->isService()) {
  1378. $prefix_prodserv = $outputlangs->transnoentitiesnoconv("Service")." ";
  1379. } else {
  1380. $prefix_prodserv = $outputlangs->transnoentitiesnoconv("Product")." ";
  1381. }
  1382. }
  1383. if (empty($hideref)) {
  1384. if ($issupplierline) {
  1385. if (empty($conf->global->PDF_HIDE_PRODUCT_REF_IN_SUPPLIER_LINES)) { // Common case
  1386. $ref_prodserv = $prodser->ref; // Show local ref
  1387. if ($ref_supplier) {
  1388. $ref_prodserv .= ($prodser->ref ? ' (' : '').$outputlangs->transnoentitiesnoconv("SupplierRef").' '.$ref_supplier.($prodser->ref ? ')' : '');
  1389. }
  1390. } elseif ($conf->global->PDF_HIDE_PRODUCT_REF_IN_SUPPLIER_LINES == 1) {
  1391. $ref_prodserv = $ref_supplier;
  1392. } elseif ($conf->global->PDF_HIDE_PRODUCT_REF_IN_SUPPLIER_LINES == 2) {
  1393. $ref_prodserv = $ref_supplier.' ('.$outputlangs->transnoentitiesnoconv("InternalRef").' '.$prodser->ref.')';
  1394. }
  1395. } else {
  1396. $ref_prodserv = $prodser->ref; // Show local ref only
  1397. if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
  1398. $productCustomerPriceStatic = new Productcustomerprice($db);
  1399. $filter = array('fk_product' => $idprod, 'fk_soc' => $object->socid);
  1400. $nbCustomerPrices = $productCustomerPriceStatic->fetch_all('', '', 1, 0, $filter);
  1401. if ($nbCustomerPrices > 0) {
  1402. $productCustomerPrice = $productCustomerPriceStatic->lines[0];
  1403. if (! empty($productCustomerPrice->ref_customer)) {
  1404. switch ($conf->global->PRODUIT_CUSTOMER_PRICES_PDF_REF_MODE) {
  1405. case 1:
  1406. $ref_prodserv = $productCustomerPrice->ref_customer;
  1407. break;
  1408. case 2:
  1409. $ref_prodserv = $productCustomerPrice->ref_customer . ' (' . $outputlangs->transnoentitiesnoconv('InternalRef') . ' ' . $ref_prodserv . ')';
  1410. break;
  1411. default:
  1412. $ref_prodserv = $ref_prodserv . ' (' . $outputlangs->transnoentitiesnoconv('RefCustomer') . ' ' . $productCustomerPrice->ref_customer . ')';
  1413. }
  1414. }
  1415. }
  1416. }
  1417. }
  1418. if (!empty($libelleproduitservice) && !empty($ref_prodserv)) {
  1419. $ref_prodserv .= " - ";
  1420. }
  1421. }
  1422. if (!empty($ref_prodserv) && !empty($conf->global->PDF_BOLD_PRODUCT_REF_AND_PERIOD)) {
  1423. if (!dol_textishtml($libelleproduitservice)) {
  1424. $libelleproduitservice = str_replace("\n", '<br>', $libelleproduitservice);
  1425. }
  1426. $ref_prodserv = '<b>'.$ref_prodserv.'</b>';
  1427. // $prefix_prodserv and $ref_prodser are not HTML var
  1428. }
  1429. $libelleproduitservice = $prefix_prodserv.$ref_prodserv.$libelleproduitservice;
  1430. // Add an additional description for the category products
  1431. if (!empty($conf->global->CATEGORY_ADD_DESC_INTO_DOC) && $idprod && !empty($conf->categorie->enabled)) {
  1432. include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1433. $categstatic = new Categorie($db);
  1434. // recovering the list of all the categories linked to product
  1435. $tblcateg = $categstatic->containing($idprod, Categorie::TYPE_PRODUCT);
  1436. foreach ($tblcateg as $cate) {
  1437. // Adding the descriptions if they are filled
  1438. $desccateg = $cate->description;
  1439. if ($desccateg) {
  1440. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desccateg);
  1441. }
  1442. }
  1443. }
  1444. if (!empty($object->lines[$i]->date_start) || !empty($object->lines[$i]->date_end)) {
  1445. $format = 'day';
  1446. $period = '';
  1447. // Show duration if exists
  1448. if ($object->lines[$i]->date_start && $object->lines[$i]->date_end) {
  1449. $period = '('.$outputlangs->transnoentitiesnoconv('DateFromTo', dol_print_date($object->lines[$i]->date_start, $format, false, $outputlangs), dol_print_date($object->lines[$i]->date_end, $format, false, $outputlangs)).')';
  1450. }
  1451. if ($object->lines[$i]->date_start && !$object->lines[$i]->date_end) {
  1452. $period = '('.$outputlangs->transnoentitiesnoconv('DateFrom', dol_print_date($object->lines[$i]->date_start, $format, false, $outputlangs)).')';
  1453. }
  1454. if (!$object->lines[$i]->date_start && $object->lines[$i]->date_end) {
  1455. $period = '('.$outputlangs->transnoentitiesnoconv('DateUntil', dol_print_date($object->lines[$i]->date_end, $format, false, $outputlangs)).')';
  1456. }
  1457. //print '>'.$outputlangs->charset_output.','.$period;
  1458. if (!empty($conf->global->PDF_BOLD_PRODUCT_REF_AND_PERIOD)) {
  1459. if (!dol_textishtml($libelleproduitservice)) {
  1460. $libelleproduitservice = str_replace("\n", '<br>', $libelleproduitservice);
  1461. }
  1462. $libelleproduitservice .= '<br><b style="color:#333666;" ><em>'.$period.'</em></b>';
  1463. } else {
  1464. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $period);
  1465. }
  1466. //print $libelleproduitservice;
  1467. }
  1468. if ($dbatch) {
  1469. $format = 'day';
  1470. foreach ($dbatch as $detail) {
  1471. $dte = array();
  1472. if ($detail->eatby) {
  1473. // Added by MMI Mathieu Moulin iProspective
  1474. // Hack important DDM information
  1475. if (!empty($conf->global->SHIPPING_PDF_ANTIGASPI) && !empty($object->commande)) {
  1476. $eatdiff = ($detail->eatby - $object->commande->date_commande);
  1477. $eatdiff_days = round($eatdiff/86400);
  1478. $days_antigaspi = $conf->global->PRODUCT_ANTIGASPI_DAYS;
  1479. $days_daecourte = $conf->global->PRODUCT_DATECOURTE_DAYS;
  1480. //var_dump($eatdiff_days);
  1481. if ($eatdiff_days<$days_antigaspi)
  1482. $antigaspi = ' - '.$outputlangs->transnoentitiesnoconv('printAntiGaspi');
  1483. elseif ($eatdiff_days<$days_daecourte)
  1484. $antigaspi = ' - '.$outputlangs->transnoentitiesnoconv('printDateCourte');
  1485. else
  1486. $antigaspi = '';
  1487. //die();
  1488. //$antigaspi .= ' - '.$eatdiff_days;
  1489. $dte[] = ($antigaspi ?'<span style="color: red;">' :'').$outputlangs->transnoentitiesnoconv('printEatby', dol_print_date($detail->eatby, $format, false, $outputlangs)).($antigaspi ?$antigaspi.'</span>' :'');
  1490. }
  1491. else {
  1492. $dte[] = $outputlangs->transnoentitiesnoconv('printEatby', dol_print_date($detail->eatby, $format, false, $outputlangs));
  1493. }
  1494. }
  1495. if ($detail->sellby) {
  1496. $dte[] = $outputlangs->transnoentitiesnoconv('printSellby', dol_print_date($detail->sellby, $format, false, $outputlangs));
  1497. }
  1498. // Added by MMI Mathieu Moulin iProspective
  1499. // Hack Hide Batch number (not very useful)
  1500. if ($detail->batch && empty($conf->global->SHIPPING_PDF_HIDE_BATCH)) {
  1501. $dte[] = $outputlangs->transnoentitiesnoconv('printBatch', $detail->batch);
  1502. }
  1503. $dte[] = $outputlangs->transnoentitiesnoconv('printQty', $detail->qty);
  1504. $libelleproduitservice .= "__N__ ".implode(" - ", $dte);
  1505. }
  1506. }
  1507. // Now we convert \n into br
  1508. if (dol_textishtml($libelleproduitservice)) {
  1509. $libelleproduitservice = preg_replace('/__N__/', '<br>', $libelleproduitservice);
  1510. } else {
  1511. $libelleproduitservice = preg_replace('/__N__/', "\n", $libelleproduitservice);
  1512. }
  1513. $libelleproduitservice = dol_htmlentitiesbr($libelleproduitservice, 1);
  1514. return $libelleproduitservice;
  1515. }
  1516. /**
  1517. * Return line num
  1518. *
  1519. * @param Object $object Object
  1520. * @param int $i Current line number
  1521. * @param Translate $outputlangs Object langs for output
  1522. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1523. * @return string
  1524. */
  1525. function pdf_getlinenum($object, $i, $outputlangs, $hidedetails = 0)
  1526. {
  1527. global $hookmanager;
  1528. $reshook = 0;
  1529. $result = '';
  1530. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1531. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1532. $special_code = $object->lines[$i]->special_code;
  1533. if (!empty($object->lines[$i]->fk_parent_line)) {
  1534. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1535. }
  1536. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1537. $action = '';
  1538. $reshook = $hookmanager->executeHooks('pdf_getlinenum', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1539. $result .= $hookmanager->resPrint;
  1540. }
  1541. if (empty($reshook)) {
  1542. $result .= dol_htmlentitiesbr($object->lines[$i]->num);
  1543. }
  1544. return $result;
  1545. }
  1546. /**
  1547. * Return line product ref
  1548. *
  1549. * @param Object $object Object
  1550. * @param int $i Current line number
  1551. * @param Translate $outputlangs Object langs for output
  1552. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1553. * @return string
  1554. */
  1555. function pdf_getlineref($object, $i, $outputlangs, $hidedetails = 0)
  1556. {
  1557. global $hookmanager;
  1558. $reshook = 0;
  1559. $result = '';
  1560. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1561. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1562. $special_code = $object->lines[$i]->special_code;
  1563. if (!empty($object->lines[$i]->fk_parent_line)) {
  1564. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1565. }
  1566. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1567. $action = '';
  1568. $reshook = $hookmanager->executeHooks('pdf_getlineref', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1569. $result .= $hookmanager->resPrint;
  1570. }
  1571. if (empty($reshook)) {
  1572. $result .= dol_htmlentitiesbr($object->lines[$i]->product_ref);
  1573. }
  1574. return $result;
  1575. }
  1576. /**
  1577. * Return line ref_supplier
  1578. *
  1579. * @param Object $object Object
  1580. * @param int $i Current line number
  1581. * @param Translate $outputlangs Object langs for output
  1582. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1583. * @return string
  1584. */
  1585. function pdf_getlineref_supplier($object, $i, $outputlangs, $hidedetails = 0)
  1586. {
  1587. global $hookmanager;
  1588. $reshook = 0;
  1589. $result = '';
  1590. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1591. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1592. $special_code = $object->lines[$i]->special_code;
  1593. if (!empty($object->lines[$i]->fk_parent_line)) {
  1594. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1595. }
  1596. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1597. $action = '';
  1598. $reshook = $hookmanager->executeHooks('pdf_getlineref_supplier', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1599. $result .= $hookmanager->resPrint;
  1600. }
  1601. if (empty($reshook)) {
  1602. $result .= dol_htmlentitiesbr($object->lines[$i]->ref_supplier);
  1603. }
  1604. return $result;
  1605. }
  1606. /**
  1607. * Return line vat rate
  1608. *
  1609. * @param Object $object Object
  1610. * @param int $i Current line number
  1611. * @param Translate $outputlangs Object langs for output
  1612. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1613. * @return string
  1614. */
  1615. function pdf_getlinevatrate($object, $i, $outputlangs, $hidedetails = 0)
  1616. {
  1617. global $conf, $hookmanager, $mysoc;
  1618. $result = '';
  1619. $reshook = 0;
  1620. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1621. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduce this test in the pdf_xxx function if you don't want your hook to run
  1622. $special_code = $object->lines[$i]->special_code;
  1623. if (!empty($object->lines[$i]->fk_parent_line)) {
  1624. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1625. }
  1626. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1627. $action = '';
  1628. $reshook = $hookmanager->executeHooks('pdf_getlinevatrate', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1629. if (!empty($hookmanager->resPrint)) {
  1630. $result .= $hookmanager->resPrint;
  1631. }
  1632. }
  1633. if (empty($reshook)) {
  1634. if (empty($hidedetails) || $hidedetails > 1) {
  1635. $tmpresult = '';
  1636. $tmpresult .= vatrate($object->lines[$i]->tva_tx, 0, $object->lines[$i]->info_bits, -1);
  1637. if (empty($conf->global->MAIN_PDF_MAIN_HIDE_SECOND_TAX)) {
  1638. if ($object->lines[$i]->total_localtax1 != 0) {
  1639. if (preg_replace('/[\s0%]/', '', $tmpresult)) {
  1640. $tmpresult .= '/';
  1641. } else {
  1642. $tmpresult = '';
  1643. }
  1644. $tmpresult .= vatrate(abs($object->lines[$i]->localtax1_tx), 0);
  1645. }
  1646. }
  1647. if (empty($conf->global->MAIN_PDF_MAIN_HIDE_THIRD_TAX)) {
  1648. if ($object->lines[$i]->total_localtax2 != 0) {
  1649. if (preg_replace('/[\s0%]/', '', $tmpresult)) {
  1650. $tmpresult .= '/';
  1651. } else {
  1652. $tmpresult = '';
  1653. }
  1654. $tmpresult .= vatrate(abs($object->lines[$i]->localtax2_tx), 0);
  1655. }
  1656. }
  1657. $tmpresult .= '%';
  1658. $result .= $tmpresult;
  1659. }
  1660. }
  1661. return $result;
  1662. }
  1663. /**
  1664. * Return line unit price excluding tax
  1665. *
  1666. * @param Object $object Object
  1667. * @param int $i Current line number
  1668. * @param Translate $outputlangs Object langs for output
  1669. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1670. * @return string
  1671. */
  1672. function pdf_getlineupexcltax($object, $i, $outputlangs, $hidedetails = 0)
  1673. {
  1674. global $conf, $hookmanager;
  1675. $sign = 1;
  1676. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  1677. $sign = -1;
  1678. }
  1679. $result = '';
  1680. $reshook = 0;
  1681. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1682. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1683. $special_code = $object->lines[$i]->special_code;
  1684. if (!empty($object->lines[$i]->fk_parent_line)) {
  1685. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1686. }
  1687. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1688. $action = '';
  1689. $reshook = $hookmanager->executeHooks('pdf_getlineupexcltax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1690. if (!empty($hookmanager->resPrint)) {
  1691. $result .= $hookmanager->resPrint;
  1692. }
  1693. }
  1694. if (empty($reshook)) {
  1695. if (empty($hidedetails) || $hidedetails > 1) {
  1696. $subprice = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1 ? $object->lines[$i]->multicurrency_subprice : $object->lines[$i]->subprice);
  1697. $result .= price($sign * $subprice, 0, $outputlangs);
  1698. }
  1699. }
  1700. return $result;
  1701. }
  1702. /**
  1703. * Return line unit price including tax
  1704. *
  1705. * @param Object $object Object
  1706. * @param int $i Current line number
  1707. * @param Translate $outputlangs Object langs for output
  1708. * @param int $hidedetails Hide value (0 = no, 1 = yes, 2 = just special lines)
  1709. * @return string
  1710. */
  1711. function pdf_getlineupwithtax($object, $i, $outputlangs, $hidedetails = 0)
  1712. {
  1713. global $hookmanager, $conf;
  1714. $sign = 1;
  1715. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  1716. $sign = -1;
  1717. }
  1718. $result = '';
  1719. $reshook = 0;
  1720. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1721. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1722. $special_code = $object->lines[$i]->special_code;
  1723. if (!empty($object->lines[$i]->fk_parent_line)) {
  1724. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1725. }
  1726. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1727. $action = '';
  1728. $reshook = $hookmanager->executeHooks('pdf_getlineupwithtax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1729. if (!empty($hookmanager->resPrint)) {
  1730. $result .= $hookmanager->resPrint;
  1731. }
  1732. }
  1733. if (empty($reshook)) {
  1734. if (empty($hidedetails) || $hidedetails > 1) {
  1735. $result .= price($sign * (($object->lines[$i]->subprice) + ($object->lines[$i]->subprice) * ($object->lines[$i]->tva_tx) / 100), 0, $outputlangs);
  1736. }
  1737. }
  1738. return $result;
  1739. }
  1740. /**
  1741. * Return line quantity
  1742. *
  1743. * @param Object $object Object
  1744. * @param int $i Current line number
  1745. * @param Translate $outputlangs Object langs for output
  1746. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1747. * @return string
  1748. */
  1749. function pdf_getlineqty($object, $i, $outputlangs, $hidedetails = 0)
  1750. {
  1751. global $hookmanager;
  1752. $result = '';
  1753. $reshook = 0;
  1754. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1755. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1756. $special_code = $object->lines[$i]->special_code;
  1757. if (!empty($object->lines[$i]->fk_parent_line)) {
  1758. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1759. }
  1760. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1761. $action = '';
  1762. $reshook = $hookmanager->executeHooks('pdf_getlineqty', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1763. if (!empty($hookmanager->resPrint)) {
  1764. $result = $hookmanager->resPrint;
  1765. }
  1766. }
  1767. if (empty($reshook)) {
  1768. if ($object->lines[$i]->special_code == 3) {
  1769. return '';
  1770. }
  1771. if (empty($hidedetails) || $hidedetails > 1) {
  1772. $result .= $object->lines[$i]->qty;
  1773. }
  1774. }
  1775. return $result;
  1776. }
  1777. /**
  1778. * Return line quantity asked
  1779. *
  1780. * @param Object $object Object
  1781. * @param int $i Current line number
  1782. * @param Translate $outputlangs Object langs for output
  1783. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1784. * @return string
  1785. */
  1786. function pdf_getlineqty_asked($object, $i, $outputlangs, $hidedetails = 0)
  1787. {
  1788. global $hookmanager;
  1789. $reshook = 0;
  1790. $result = '';
  1791. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1792. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1793. $special_code = $object->lines[$i]->special_code;
  1794. if (!empty($object->lines[$i]->fk_parent_line)) {
  1795. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1796. }
  1797. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1798. $action = '';
  1799. $reshook = $hookmanager->executeHooks('pdf_getlineqty_asked', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1800. if (!empty($hookmanager->resPrint)) {
  1801. $result .= $hookmanager->resPrint;
  1802. }
  1803. }
  1804. if (empty($reshook)) {
  1805. if ($object->lines[$i]->special_code == 3) {
  1806. return '';
  1807. }
  1808. if (empty($hidedetails) || $hidedetails > 1) {
  1809. $result .= $object->lines[$i]->qty_asked;
  1810. }
  1811. }
  1812. return $result;
  1813. }
  1814. /**
  1815. * Return line quantity shipped
  1816. *
  1817. * @param Object $object Object
  1818. * @param int $i Current line number
  1819. * @param Translate $outputlangs Object langs for output
  1820. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1821. * @return string
  1822. */
  1823. function pdf_getlineqty_shipped($object, $i, $outputlangs, $hidedetails = 0)
  1824. {
  1825. global $hookmanager;
  1826. $reshook = 0;
  1827. $result = '';
  1828. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1829. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1830. $special_code = $object->lines[$i]->special_code;
  1831. if (!empty($object->lines[$i]->fk_parent_line)) {
  1832. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1833. }
  1834. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1835. $action = '';
  1836. $reshook = $hookmanager->executeHooks('pdf_getlineqty_shipped', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1837. if (!empty($hookmanager->resPrint)) {
  1838. $result .= $hookmanager->resPrint;
  1839. }
  1840. }
  1841. if (empty($reshook)) {
  1842. if ($object->lines[$i]->special_code == 3) {
  1843. return '';
  1844. }
  1845. if (empty($hidedetails) || $hidedetails > 1) {
  1846. $result .= $object->lines[$i]->qty_shipped;
  1847. }
  1848. }
  1849. return $result;
  1850. }
  1851. /**
  1852. * Return line keep to ship quantity
  1853. *
  1854. * @param Object $object Object
  1855. * @param int $i Current line number
  1856. * @param Translate $outputlangs Object langs for output
  1857. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1858. * @return string
  1859. */
  1860. function pdf_getlineqty_keeptoship($object, $i, $outputlangs, $hidedetails = 0)
  1861. {
  1862. global $hookmanager;
  1863. $reshook = 0;
  1864. $result = '';
  1865. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1866. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1867. $special_code = $object->lines[$i]->special_code;
  1868. if (!empty($object->lines[$i]->fk_parent_line)) {
  1869. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1870. }
  1871. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1872. $action = '';
  1873. $reshook = $hookmanager->executeHooks('pdf_getlineqty_keeptoship', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1874. if (!empty($hookmanager->resPrint)) {
  1875. $result .= $hookmanager->resPrint;
  1876. }
  1877. }
  1878. if (empty($reshook)) {
  1879. if ($object->lines[$i]->special_code == 3) {
  1880. return '';
  1881. }
  1882. if (empty($hidedetails) || $hidedetails > 1) {
  1883. $result .= ($object->lines[$i]->qty_asked - $object->lines[$i]->qty_shipped);
  1884. }
  1885. }
  1886. return $result;
  1887. }
  1888. /**
  1889. * Return line unit
  1890. *
  1891. * @param Object $object Object
  1892. * @param int $i Current line number
  1893. * @param Translate $outputlangs Object langs for output
  1894. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1895. * @param HookManager $hookmanager Hook manager instance
  1896. * @return string Value for unit cell
  1897. */
  1898. function pdf_getlineunit($object, $i, $outputlangs, $hidedetails = 0, $hookmanager = false)
  1899. {
  1900. global $langs;
  1901. $reshook = 0;
  1902. $result = '';
  1903. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1904. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1905. $special_code = $object->lines[$i]->special_code;
  1906. if (!empty($object->lines[$i]->fk_parent_line)) {
  1907. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1908. }
  1909. $parameters = array(
  1910. 'i' => $i,
  1911. 'outputlangs' => $outputlangs,
  1912. 'hidedetails' => $hidedetails,
  1913. 'special_code' => $special_code
  1914. );
  1915. $action = '';
  1916. $reshook = $hookmanager->executeHooks('pdf_getlineunit', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1917. if (!empty($hookmanager->resPrint)) {
  1918. $result .= $hookmanager->resPrint;
  1919. }
  1920. }
  1921. if (empty($reshook)) {
  1922. if (empty($hidedetails) || $hidedetails > 1) {
  1923. $result .= $langs->transnoentitiesnoconv($object->lines[$i]->getLabelOfUnit('short'));
  1924. }
  1925. }
  1926. return $result;
  1927. }
  1928. /**
  1929. * Return line remise percent
  1930. *
  1931. * @param Object $object Object
  1932. * @param int $i Current line number
  1933. * @param Translate $outputlangs Object langs for output
  1934. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1935. * @return string
  1936. */
  1937. function pdf_getlineremisepercent($object, $i, $outputlangs, $hidedetails = 0)
  1938. {
  1939. global $hookmanager;
  1940. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1941. $reshook = 0;
  1942. $result = '';
  1943. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1944. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1945. $special_code = $object->lines[$i]->special_code;
  1946. if (!empty($object->lines[$i]->fk_parent_line)) {
  1947. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1948. }
  1949. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1950. $action = '';
  1951. $reshook = $hookmanager->executeHooks('pdf_getlineremisepercent', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1952. if (!empty($hookmanager->resPrint)) {
  1953. $result .= $hookmanager->resPrint;
  1954. }
  1955. }
  1956. if (empty($reshook)) {
  1957. if ($object->lines[$i]->special_code == 3) {
  1958. return '';
  1959. }
  1960. if (empty($hidedetails) || $hidedetails > 1) {
  1961. $result .= dol_print_reduction($object->lines[$i]->remise_percent, $outputlangs);
  1962. }
  1963. }
  1964. return $result;
  1965. }
  1966. /**
  1967. * Return line percent
  1968. *
  1969. * @param Object $object Object
  1970. * @param int $i Current line number
  1971. * @param Translate $outputlangs Object langs for output
  1972. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1973. * @param HookManager $hookmanager Hook manager instance
  1974. * @return string
  1975. */
  1976. function pdf_getlineprogress($object, $i, $outputlangs, $hidedetails = 0, $hookmanager = null)
  1977. {
  1978. if (empty($hookmanager)) {
  1979. global $hookmanager;
  1980. }
  1981. global $conf;
  1982. $reshook = 0;
  1983. $result = '';
  1984. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  1985. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  1986. $special_code = $object->lines[$i]->special_code;
  1987. if (!empty($object->lines[$i]->fk_parent_line)) {
  1988. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1989. }
  1990. $parameters = array('i' => $i, 'outputlangs' => $outputlangs, 'hidedetails' => $hidedetails, 'special_code' => $special_code);
  1991. $action = '';
  1992. $reshook = $hookmanager->executeHooks('pdf_getlineprogress', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1993. if (!empty($hookmanager->resPrint)) {
  1994. return $hookmanager->resPrint;
  1995. }
  1996. }
  1997. if (empty($reshook)) {
  1998. if ($object->lines[$i]->special_code == 3) {
  1999. return '';
  2000. }
  2001. if (empty($hidedetails) || $hidedetails > 1) {
  2002. if (!empty($conf->global->SITUATION_DISPLAY_DIFF_ON_PDF)) {
  2003. $prev_progress = 0;
  2004. if (method_exists($object->lines[$i], 'get_prev_progress')) {
  2005. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  2006. }
  2007. $result = round($object->lines[$i]->situation_percent - $prev_progress, 1).'%';
  2008. } else {
  2009. $result = round($object->lines[$i]->situation_percent, 1).'%';
  2010. }
  2011. }
  2012. }
  2013. return $result;
  2014. }
  2015. /**
  2016. * Return line total excluding tax
  2017. *
  2018. * @param Object $object Object
  2019. * @param int $i Current line number
  2020. * @param Translate $outputlangs Object langs for output
  2021. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  2022. * @return string Return total of line excl tax
  2023. */
  2024. function pdf_getlinetotalexcltax($object, $i, $outputlangs, $hidedetails = 0)
  2025. {
  2026. global $conf, $hookmanager;
  2027. $sign = 1;
  2028. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  2029. $sign = -1;
  2030. }
  2031. $reshook = 0;
  2032. $result = '';
  2033. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  2034. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  2035. $special_code = $object->lines[$i]->special_code;
  2036. if (!empty($object->lines[$i]->fk_parent_line)) {
  2037. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  2038. }
  2039. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code, 'sign'=>$sign);
  2040. $action = '';
  2041. $reshook = $hookmanager->executeHooks('pdf_getlinetotalexcltax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  2042. if (!empty($hookmanager->resPrint)) {
  2043. $result .= $hookmanager->resPrint;
  2044. }
  2045. }
  2046. if (empty($reshook)) {
  2047. if ($object->lines[$i]->special_code == 3) {
  2048. $result .= $outputlangs->transnoentities("Option");
  2049. } elseif (empty($hidedetails) || $hidedetails > 1) {
  2050. $total_ht = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1 ? $object->lines[$i]->multicurrency_total_ht : $object->lines[$i]->total_ht);
  2051. if (!empty($object->lines[$i]->situation_percent) && $object->lines[$i]->situation_percent > 0) {
  2052. // TODO Remove this. The total should be saved correctly in database instead of being modified here.
  2053. $prev_progress = 0;
  2054. $progress = 1;
  2055. if (method_exists($object->lines[$i], 'get_prev_progress')) {
  2056. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  2057. $progress = ($object->lines[$i]->situation_percent - $prev_progress) / 100;
  2058. }
  2059. $result .= price($sign * ($total_ht / ($object->lines[$i]->situation_percent / 100)) * $progress, 0, $outputlangs);
  2060. } else {
  2061. $result .= price($sign * $total_ht, 0, $outputlangs);
  2062. }
  2063. }
  2064. }
  2065. return $result;
  2066. }
  2067. /**
  2068. * Return line total including tax
  2069. *
  2070. * @param Object $object Object
  2071. * @param int $i Current line number
  2072. * @param Translate $outputlangs Object langs for output
  2073. * @param int $hidedetails Hide value (0 = no, 1 = yes, 2 = just special lines)
  2074. * @return string Return total of line incl tax
  2075. */
  2076. function pdf_getlinetotalwithtax($object, $i, $outputlangs, $hidedetails = 0)
  2077. {
  2078. global $hookmanager, $conf;
  2079. $sign = 1;
  2080. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  2081. $sign = -1;
  2082. }
  2083. $reshook = 0;
  2084. $result = '';
  2085. //if (is_object($hookmanager) && ( (isset($object->lines[$i]->product_type) && $object->lines[$i]->product_type == 9 && ! empty($object->lines[$i]->special_code)) || ! empty($object->lines[$i]->fk_parent_line) ) )
  2086. if (is_object($hookmanager)) { // Old code is commented on preceding line. Reproduct this test in the pdf_xxx function if you don't want your hook to run
  2087. $special_code = $object->lines[$i]->special_code;
  2088. if (!empty($object->lines[$i]->fk_parent_line)) {
  2089. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  2090. }
  2091. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  2092. $action = '';
  2093. $reshook = $hookmanager->executeHooks('pdf_getlinetotalwithtax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  2094. if (!empty($hookmanager->resPrint)) {
  2095. $result .= $hookmanager->resPrint;
  2096. }
  2097. }
  2098. if (empty($reshook)) {
  2099. if ($object->lines[$i]->special_code == 3) {
  2100. $result .= $outputlangs->transnoentities("Option");
  2101. } elseif (empty($hidedetails) || $hidedetails > 1) {
  2102. $total_ttc = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1 ? $object->lines[$i]->multicurrency_total_ttc : $object->lines[$i]->total_ttc);
  2103. if ($object->lines[$i]->situation_percent > 0) {
  2104. // TODO Remove this. The total should be saved correctly in database instead of being modified here.
  2105. $prev_progress = 0;
  2106. $progress = 1;
  2107. if (method_exists($object->lines[$i], 'get_prev_progress')) {
  2108. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  2109. $progress = ($object->lines[$i]->situation_percent - $prev_progress) / 100;
  2110. }
  2111. $result .= price($sign * ($total_ttc / ($object->lines[$i]->situation_percent / 100)) * $progress, 0, $outputlangs);
  2112. } else {
  2113. $result .= price($sign * $total_ttc, 0, $outputlangs);
  2114. }
  2115. }
  2116. }
  2117. return $result;
  2118. }
  2119. /**
  2120. * Return linked objects to use for document generation.
  2121. * Warning: To save space, this function returns only one link per link type (all links are concated on same record string). This function is used by pdf_writeLinkedObjects
  2122. *
  2123. * @param object $object Object
  2124. * @param Translate $outputlangs Object lang for output
  2125. * @return array Linked objects
  2126. */
  2127. function pdf_getLinkedObjects(&$object, $outputlangs)
  2128. {
  2129. global $db, $hookmanager;
  2130. $linkedobjects = array();
  2131. $object->fetchObjectLinked();
  2132. foreach ($object->linkedObjects as $objecttype => $objects) {
  2133. if ($objecttype == 'facture') {
  2134. // For invoice, we don't want to have a reference line on document. Image we are using recuring invoice, we will have a line longer than document width.
  2135. } elseif ($objecttype == 'propal' || $objecttype == 'supplier_proposal') {
  2136. $outputlangs->load('propal');
  2137. foreach ($objects as $elementobject) {
  2138. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefProposal");
  2139. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref);
  2140. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("DatePropal");
  2141. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->date, 'day', '', $outputlangs);
  2142. }
  2143. } elseif ($objecttype == 'commande' || $objecttype == 'supplier_order') {
  2144. $outputlangs->load('orders');
  2145. if (count($objects) > 1 && count($objects) <= (getDolGlobalInt("MAXREFONDOC") ? getDolGlobalInt("MAXREFONDOC") : 10)) {
  2146. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities("RefOrder").' :');
  2147. foreach ($objects as $elementobject) {
  2148. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities($elementobject->ref).(empty($elementobject->ref_client) ?'' : ' ('.$elementobject->ref_client.')').(empty($elementobject->ref_supplier) ? '' : ' ('.$elementobject->ref_supplier.')').' ');
  2149. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities("OrderDate").' : '.dol_print_date($elementobject->date, 'day', '', $outputlangs));
  2150. }
  2151. } elseif (count($objects) == 1) {
  2152. $elementobject = array_shift($objects);
  2153. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefOrder");
  2154. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref).(!empty($elementobject->ref_client) ? ' ('.$elementobject->ref_client.')' : '').(!empty($elementobject->ref_supplier) ? ' ('.$elementobject->ref_supplier.')' : '');
  2155. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("OrderDate");
  2156. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->date, 'day', '', $outputlangs);
  2157. }
  2158. } elseif ($objecttype == 'contrat') {
  2159. $outputlangs->load('contracts');
  2160. foreach ($objects as $elementobject) {
  2161. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefContract");
  2162. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref);
  2163. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("DateContract");
  2164. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->date_contrat, 'day', '', $outputlangs);
  2165. }
  2166. } elseif ($objecttype == 'fichinter') {
  2167. $outputlangs->load('interventions');
  2168. foreach ($objects as $elementobject) {
  2169. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("InterRef");
  2170. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref);
  2171. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("InterDate");
  2172. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->datec, 'day', '', $outputlangs);
  2173. }
  2174. } elseif ($objecttype == 'shipping') {
  2175. $outputlangs->loadLangs(array("orders", "sendings"));
  2176. if (count($objects) > 1) {
  2177. $order = null;
  2178. if (empty($object->linkedObjects['commande']) && $object->element != 'commande') {
  2179. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities("RefOrder").' / '.$outputlangs->transnoentities("RefSending").' :');
  2180. } else {
  2181. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities("RefSending").' :');
  2182. }
  2183. // We concat this record info into fields xxx_value. title is overwrote.
  2184. foreach ($objects as $elementobject) {
  2185. if (empty($object->linkedObjects['commande']) && $object->element != 'commande') { // There is not already a link to order and object is not the order, so we show also info with order
  2186. $elementobject->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 0);
  2187. if (! empty($elementobject->linkedObjectsIds['commande'])) {
  2188. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  2189. $order = new Commande($db);
  2190. $ret = $order->fetch(reset($elementobject->linkedObjectsIds['commande']));
  2191. if ($ret < 1) {
  2192. $order = null;
  2193. }
  2194. }
  2195. }
  2196. if (! is_object($order)) {
  2197. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities($elementobject->ref));
  2198. } else {
  2199. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->convToOutputCharset($order->ref).($order->ref_client ? ' ('.$order->ref_client.')' : ''));
  2200. $object->note_public = dol_concatdesc($object->note_public, ' / '.$outputlangs->transnoentities($elementobject->ref));
  2201. }
  2202. }
  2203. } elseif (count($objects) == 1) {
  2204. $elementobject = array_shift($objects);
  2205. $order = null;
  2206. // We concat this record info into fields xxx_value. title is overwrote.
  2207. if (empty($object->linkedObjects['commande']) && $object->element != 'commande') { // There is not already a link to order and object is not the order, so we show also info with order
  2208. $elementobject->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 0);
  2209. if (! empty($elementobject->linkedObjectsIds['commande'])) {
  2210. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  2211. $order = new Commande($db);
  2212. $ret = $order->fetch(reset($elementobject->linkedObjectsIds['commande']));
  2213. if ($ret < 1) {
  2214. $order = null;
  2215. }
  2216. }
  2217. }
  2218. if (! is_object($order)) {
  2219. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefSending");
  2220. if (! empty($linkedobjects[$objecttype]['ref_value'])) $linkedobjects[$objecttype]['ref_value'] .= ' / ';
  2221. $linkedobjects[$objecttype]['ref_value'] .= $outputlangs->transnoentities($elementobject->ref);
  2222. $linkedobjects[$objecttype]['date_value'] = dol_print_date(empty($elementobject->date_shipping) ? $elementobject->date_delivery : $elementobject->date_shipping, 'day', '', $outputlangs);
  2223. } else {
  2224. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefOrder").' / '.$outputlangs->transnoentities("RefSending");
  2225. if (empty($linkedobjects[$objecttype]['ref_value'])) $linkedobjects[$objecttype]['ref_value'] = $outputlangs->convToOutputCharset($order->ref).($order->ref_client ? ' ('.$order->ref_client.')' : '');
  2226. $linkedobjects[$objecttype]['ref_value'] .= ' / '.$outputlangs->transnoentities($elementobject->ref);
  2227. $linkedobjects[$objecttype]['date_value'] = dol_print_date(empty($elementobject->date_shipping) ? $elementobject->date_delivery : $elementobject->date_shipping, 'day', '', $outputlangs);
  2228. }
  2229. }
  2230. }
  2231. }
  2232. // For add external linked objects
  2233. if (is_object($hookmanager)) {
  2234. $parameters = array('linkedobjects' => $linkedobjects, 'outputlangs'=>$outputlangs);
  2235. $action = '';
  2236. $hookmanager->executeHooks('pdf_getLinkedObjects', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  2237. if (!empty($hookmanager->resArray)) {
  2238. $linkedobjects = $hookmanager->resArray;
  2239. }
  2240. }
  2241. return $linkedobjects;
  2242. }
  2243. /**
  2244. * Return dimensions to use for images onto PDF checking that width and height are not higher than
  2245. * maximum (16x32 by default).
  2246. *
  2247. * @param string $realpath Full path to photo file to use
  2248. * @return array Height and width to use to output image (in pdf user unit, so mm)
  2249. */
  2250. function pdf_getSizeForImage($realpath)
  2251. {
  2252. global $conf;
  2253. $maxwidth = (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH) ? 20 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH);
  2254. $maxheight = (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_HEIGHT) ? 32 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_HEIGHT);
  2255. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  2256. $tmp = dol_getImageSize($realpath);
  2257. if ($tmp['height']) {
  2258. $width = (int) round($maxheight * $tmp['width'] / $tmp['height']); // I try to use maxheight
  2259. if ($width > $maxwidth) { // Pb with maxheight, so i use maxwidth
  2260. $width = $maxwidth;
  2261. $height = (int) round($maxwidth * $tmp['height'] / $tmp['width']);
  2262. } else // No pb with maxheight
  2263. {
  2264. $height = $maxheight;
  2265. }
  2266. }
  2267. return array('width'=>$width, 'height'=>$height);
  2268. }
  2269. /**
  2270. * Return line total amount discount
  2271. *
  2272. * @param Object $object Object
  2273. * @param int $i Current line number
  2274. * @param Translate $outputlangs Object langs for output
  2275. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  2276. * @return string Return total of line excl tax
  2277. */
  2278. function pdfGetLineTotalDiscountAmount($object, $i, $outputlangs, $hidedetails = 0)
  2279. {
  2280. global $conf, $hookmanager;
  2281. $sign = 1;
  2282. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  2283. $sign = -1;
  2284. }
  2285. if ($object->lines[$i]->special_code == 3) {
  2286. return $outputlangs->transnoentities("Option");
  2287. } else {
  2288. if (is_object($hookmanager)) {
  2289. $special_code = $object->lines[$i]->special_code;
  2290. if (!empty($object->lines[$i]->fk_parent_line)) {
  2291. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  2292. }
  2293. $parameters = array(
  2294. 'i'=>$i,
  2295. 'outputlangs'=>$outputlangs,
  2296. 'hidedetails'=>$hidedetails,
  2297. 'special_code'=>$special_code
  2298. );
  2299. $action = '';
  2300. if ($hookmanager->executeHooks('getlinetotalremise', $parameters, $object, $action) > 0) {
  2301. return $hookmanager->resPrint; // Note that $action and $object may have been modified by some hooks
  2302. }
  2303. }
  2304. if (empty($hidedetails) || $hidedetails > 1) {
  2305. return $sign * (($object->lines[$i]->subprice * $object->lines[$i]->qty) - $object->lines[$i]->total_ht);
  2306. }
  2307. }
  2308. return '';
  2309. }