pdf.lib.php 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  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 || !empty($conf->global->PDF_INCLUDE_ALIAS_IN_THIRDPARTY_NAME)) && !empty($thirdparty->name_alias)) {
  343. $socname .= " - ".$thirdparty->name_alias;
  344. }
  345. } elseif ($thirdparty instanceof Contact) {
  346. $socname = $thirdparty->socname;
  347. } else {
  348. throw new InvalidArgumentException('Parameter 1 $thirdparty is not a Societe nor Contact');
  349. }
  350. return $outputlangs->convToOutputCharset($socname);
  351. }
  352. /**
  353. * Return a string with full address formated for output on documents
  354. *
  355. * @param Translate $outputlangs Output langs object
  356. * @param Societe $sourcecompany Source company object
  357. * @param Societe|string|null $targetcompany Target company object
  358. * @param Contact|string|null $targetcontact Target contact object
  359. * @param int $usecontact Use contact instead of company
  360. * @param string $mode Address type ('source', 'target', 'targetwithdetails', 'targetwithdetails_xxx': target but include also phone/fax/email/url)
  361. * @param Object $object Object we want to build document for
  362. * @return string String with full address
  363. */
  364. function pdf_build_address($outputlangs, $sourcecompany, $targetcompany = '', $targetcontact = '', $usecontact = 0, $mode = 'source', $object = null)
  365. {
  366. global $conf, $hookmanager;
  367. if ($mode == 'source' && !is_object($sourcecompany)) {
  368. return -1;
  369. }
  370. if ($mode == 'target' && !is_object($targetcompany)) {
  371. return -1;
  372. }
  373. if (!empty($sourcecompany->state_id) && empty($sourcecompany->state)) {
  374. $sourcecompany->state = getState($sourcecompany->state_id);
  375. }
  376. if (!empty($targetcompany->state_id) && empty($targetcompany->state)) {
  377. $targetcompany->state = getState($targetcompany->state_id);
  378. }
  379. $reshook = 0;
  380. $stringaddress = '';
  381. if (is_object($hookmanager)) {
  382. $parameters = array('sourcecompany' => &$sourcecompany, 'targetcompany' => &$targetcompany, 'targetcontact' => &$targetcontact, 'outputlangs' => $outputlangs, 'mode' => $mode, 'usecontact' => $usecontact);
  383. $action = '';
  384. $reshook = $hookmanager->executeHooks('pdf_build_address', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  385. $stringaddress .= $hookmanager->resPrint;
  386. }
  387. if (empty($reshook)) {
  388. if ($mode == 'source') {
  389. $withCountry = 0;
  390. if (!empty($sourcecompany->country_code) && ($targetcompany->country_code != $sourcecompany->country_code)) {
  391. $withCountry = 1;
  392. }
  393. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($sourcecompany, $withCountry, "\n", $outputlangs))."\n";
  394. if (empty($conf->global->MAIN_PDF_DISABLESOURCEDETAILS)) {
  395. // Phone
  396. if ($sourcecompany->phone) {
  397. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("PhoneShort").": ".$outputlangs->convToOutputCharset($sourcecompany->phone);
  398. }
  399. // Fax
  400. if ($sourcecompany->fax) {
  401. $stringaddress .= ($stringaddress ? ($sourcecompany->phone ? " - " : "\n") : '').$outputlangs->transnoentities("Fax").": ".$outputlangs->convToOutputCharset($sourcecompany->fax);
  402. }
  403. // EMail
  404. if ($sourcecompany->email) {
  405. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Email").": ".$outputlangs->convToOutputCharset($sourcecompany->email);
  406. }
  407. // Web
  408. if ($sourcecompany->url) {
  409. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Web").": ".$outputlangs->convToOutputCharset($sourcecompany->url);
  410. }
  411. }
  412. // Intra VAT
  413. if (!empty($conf->global->MAIN_TVAINTRA_IN_SOURCE_ADDRESS)) {
  414. if ($sourcecompany->tva_intra) {
  415. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("VATIntraShort").': '.$outputlangs->convToOutputCharset($sourcecompany->tva_intra);
  416. }
  417. }
  418. // Professionnal Ids
  419. $reg = array();
  420. if (!empty($conf->global->MAIN_PROFID1_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof1)) {
  421. $tmp = $outputlangs->transcountrynoentities("ProfId1", $sourcecompany->country_code);
  422. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  423. $tmp = $reg[1];
  424. }
  425. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof1);
  426. }
  427. if (!empty($conf->global->MAIN_PROFID2_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof2)) {
  428. $tmp = $outputlangs->transcountrynoentities("ProfId2", $sourcecompany->country_code);
  429. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  430. $tmp = $reg[1];
  431. }
  432. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof2);
  433. }
  434. if (!empty($conf->global->MAIN_PROFID3_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof3)) {
  435. $tmp = $outputlangs->transcountrynoentities("ProfId3", $sourcecompany->country_code);
  436. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  437. $tmp = $reg[1];
  438. }
  439. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof3);
  440. }
  441. if (!empty($conf->global->MAIN_PROFID4_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof4)) {
  442. $tmp = $outputlangs->transcountrynoentities("ProfId4", $sourcecompany->country_code);
  443. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  444. $tmp = $reg[1];
  445. }
  446. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof4);
  447. }
  448. if (!empty($conf->global->MAIN_PROFID5_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof5)) {
  449. $tmp = $outputlangs->transcountrynoentities("ProfId5", $sourcecompany->country_code);
  450. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  451. $tmp = $reg[1];
  452. }
  453. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof5);
  454. }
  455. if (!empty($conf->global->MAIN_PROFID6_IN_SOURCE_ADDRESS) && !empty($sourcecompany->idprof6)) {
  456. $tmp = $outputlangs->transcountrynoentities("ProfId6", $sourcecompany->country_code);
  457. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  458. $tmp = $reg[1];
  459. }
  460. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($sourcecompany->idprof6);
  461. }
  462. if (!empty($conf->global->PDF_ADD_MORE_AFTER_SOURCE_ADDRESS)) {
  463. $stringaddress .= ($stringaddress ? "\n" : '').$conf->global->PDF_ADD_MORE_AFTER_SOURCE_ADDRESS;
  464. }
  465. }
  466. if ($mode == 'target' || preg_match('/targetwithdetails/', $mode)) {
  467. if ($usecontact) {
  468. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($targetcontact->getFullName($outputlangs, 1));
  469. if (!empty($targetcontact->address)) {
  470. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($targetcontact));
  471. } else {
  472. $companytouseforaddress = $targetcompany;
  473. // Contact on a thirdparty that is a different thirdparty than the thirdparty of object
  474. if ($targetcontact->socid > 0 && $targetcontact->socid != $targetcompany->id) {
  475. $targetcontact->fetch_thirdparty();
  476. $companytouseforaddress = $targetcontact->thirdparty;
  477. }
  478. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($companytouseforaddress));
  479. }
  480. // Country
  481. if (!empty($targetcontact->country_code) && $targetcontact->country_code != $sourcecompany->country_code) {
  482. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$targetcontact->country_code));
  483. } elseif (empty($targetcontact->country_code) && !empty($targetcompany->country_code) && ($targetcompany->country_code != $sourcecompany->country_code)) {
  484. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$targetcompany->country_code));
  485. }
  486. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || preg_match('/targetwithdetails/', $mode)) {
  487. // Phone
  488. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_phone/', $mode)) {
  489. if (!empty($targetcontact->phone_pro) || !empty($targetcontact->phone_mobile)) {
  490. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Phone").": ";
  491. }
  492. if (!empty($targetcontact->phone_pro)) {
  493. $stringaddress .= $outputlangs->convToOutputCharset($targetcontact->phone_pro);
  494. }
  495. if (!empty($targetcontact->phone_pro) && !empty($targetcontact->phone_mobile)) {
  496. $stringaddress .= " / ";
  497. }
  498. if (!empty($targetcontact->phone_mobile)) {
  499. $stringaddress .= $outputlangs->convToOutputCharset($targetcontact->phone_mobile);
  500. }
  501. }
  502. // Fax
  503. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_fax/', $mode)) {
  504. if ($targetcontact->fax) {
  505. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Fax").": ".$outputlangs->convToOutputCharset($targetcontact->fax);
  506. }
  507. }
  508. // EMail
  509. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_email/', $mode)) {
  510. if ($targetcontact->email) {
  511. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Email").": ".$outputlangs->convToOutputCharset($targetcontact->email);
  512. }
  513. }
  514. // Web
  515. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_url/', $mode)) {
  516. if ($targetcontact->url) {
  517. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Web").": ".$outputlangs->convToOutputCharset($targetcontact->url);
  518. }
  519. }
  520. }
  521. } else {
  522. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset(dol_format_address($targetcompany));
  523. // Country
  524. if (!empty($targetcompany->country_code) && $targetcompany->country_code != $sourcecompany->country_code) {
  525. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Country".$targetcompany->country_code));
  526. }
  527. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || preg_match('/targetwithdetails/', $mode)) {
  528. // Phone
  529. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_phone/', $mode)) {
  530. if (!empty($targetcompany->phone) || !empty($targetcompany->phone_mobile)) {
  531. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Phone").": ";
  532. }
  533. if (!empty($targetcompany->phone)) {
  534. $stringaddress .= $outputlangs->convToOutputCharset($targetcompany->phone);
  535. }
  536. if (!empty($targetcompany->phone) && !empty($targetcompany->phone_mobile)) {
  537. $stringaddress .= " / ";
  538. }
  539. if (!empty($targetcompany->phone_mobile)) {
  540. $stringaddress .= $outputlangs->convToOutputCharset($targetcompany->phone_mobile);
  541. }
  542. }
  543. // Fax
  544. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_fax/', $mode)) {
  545. if ($targetcompany->fax) {
  546. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Fax").": ".$outputlangs->convToOutputCharset($targetcompany->fax);
  547. }
  548. }
  549. // EMail
  550. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_email/', $mode)) {
  551. if ($targetcompany->email) {
  552. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Email").": ".$outputlangs->convToOutputCharset($targetcompany->email);
  553. }
  554. }
  555. // Web
  556. if (!empty($conf->global->MAIN_PDF_ADDALSOTARGETDETAILS) || $mode == 'targetwithdetails' || preg_match('/targetwithdetails_url/', $mode)) {
  557. if ($targetcompany->url) {
  558. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("Web").": ".$outputlangs->convToOutputCharset($targetcompany->url);
  559. }
  560. }
  561. }
  562. }
  563. // Intra VAT
  564. if (empty($conf->global->MAIN_TVAINTRA_NOT_IN_ADDRESS)) {
  565. if ($targetcompany->tva_intra) {
  566. $stringaddress .= ($stringaddress ? "\n" : '').$outputlangs->transnoentities("VATIntraShort").': '.$outputlangs->convToOutputCharset($targetcompany->tva_intra);
  567. }
  568. }
  569. // Professionnal Ids
  570. if (!empty($conf->global->MAIN_PROFID1_IN_ADDRESS) && !empty($targetcompany->idprof1)) {
  571. $tmp = $outputlangs->transcountrynoentities("ProfId1", $targetcompany->country_code);
  572. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  573. $tmp = $reg[1];
  574. }
  575. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof1);
  576. }
  577. if (!empty($conf->global->MAIN_PROFID2_IN_ADDRESS) && !empty($targetcompany->idprof2)) {
  578. $tmp = $outputlangs->transcountrynoentities("ProfId2", $targetcompany->country_code);
  579. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  580. $tmp = $reg[1];
  581. }
  582. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof2);
  583. }
  584. if (!empty($conf->global->MAIN_PROFID3_IN_ADDRESS) && !empty($targetcompany->idprof3)) {
  585. $tmp = $outputlangs->transcountrynoentities("ProfId3", $targetcompany->country_code);
  586. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  587. $tmp = $reg[1];
  588. }
  589. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof3);
  590. }
  591. if (!empty($conf->global->MAIN_PROFID4_IN_ADDRESS) && !empty($targetcompany->idprof4)) {
  592. $tmp = $outputlangs->transcountrynoentities("ProfId4", $targetcompany->country_code);
  593. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  594. $tmp = $reg[1];
  595. }
  596. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof4);
  597. }
  598. if (!empty($conf->global->MAIN_PROFID5_IN_ADDRESS) && !empty($targetcompany->idprof5)) {
  599. $tmp = $outputlangs->transcountrynoentities("ProfId5", $targetcompany->country_code);
  600. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  601. $tmp = $reg[1];
  602. }
  603. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof5);
  604. }
  605. if (!empty($conf->global->MAIN_PROFID6_IN_ADDRESS) && !empty($targetcompany->idprof6)) {
  606. $tmp = $outputlangs->transcountrynoentities("ProfId6", $targetcompany->country_code);
  607. if (preg_match('/\((.+)\)/', $tmp, $reg)) {
  608. $tmp = $reg[1];
  609. }
  610. $stringaddress .= ($stringaddress ? "\n" : '').$tmp.': '.$outputlangs->convToOutputCharset($targetcompany->idprof6);
  611. }
  612. // Public note
  613. if (!empty($conf->global->MAIN_PUBLIC_NOTE_IN_ADDRESS)) {
  614. if ($mode == 'source' && !empty($sourcecompany->note_public)) {
  615. $stringaddress .= ($stringaddress ? "\n" : '').dol_string_nohtmltag($sourcecompany->note_public);
  616. }
  617. if (($mode == 'target' || preg_match('/targetwithdetails/', $mode)) && !empty($targetcompany->note_public)) {
  618. $stringaddress .= ($stringaddress ? "\n" : '').dol_string_nohtmltag($targetcompany->note_public);
  619. }
  620. }
  621. }
  622. }
  623. return $stringaddress;
  624. }
  625. /**
  626. * Show header of page for PDF generation
  627. *
  628. * @param TCPDF $pdf Object PDF
  629. * @param Translate $outputlangs Object lang for output
  630. * @param int $page_height Height of page
  631. * @return void
  632. */
  633. function pdf_pagehead(&$pdf, $outputlangs, $page_height)
  634. {
  635. global $conf;
  636. // Add a background image on document only if good setup of const
  637. 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
  638. $filepath = $conf->mycompany->dir_output.'/logos/'.$conf->global->MAIN_USE_BACKGROUND_ON_PDF;
  639. if (file_exists($filepath)) {
  640. $pdf->SetAutoPageBreak(0, 0); // Disable auto pagebreak before adding image
  641. $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);
  642. $pdf->SetPageMark(); // This option avoid to have the images missing on some pages
  643. $pdf->SetAutoPageBreak(1, 0); // Restore pagebreak
  644. }
  645. }
  646. }
  647. /**
  648. * Return array of possible substitutions for PDF content (without external module substitutions).
  649. *
  650. * @param Translate $outputlangs Output language
  651. * @param array $exclude Array of family keys we want to exclude. For example array('mycompany', 'object', 'date', 'user', ...)
  652. * @param Object $object Object
  653. * @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)
  654. * @return array Array of substitutions
  655. */
  656. function pdf_getSubstitutionArray($outputlangs, $exclude = null, $object = null, $onlykey = 0)
  657. {
  658. $substitutionarray = getCommonSubstitutionArray($outputlangs, $onlykey, $exclude, $object);
  659. $substitutionarray['__FROM_NAME__'] = '__FROM_NAME__';
  660. $substitutionarray['__FROM_EMAIL__'] = '__FROM_EMAIL__';
  661. return $substitutionarray;
  662. }
  663. /**
  664. * Add a draft watermark on PDF files
  665. *
  666. * @param TCPDF $pdf Object PDF
  667. * @param Translate $outputlangs Object lang
  668. * @param int $h Height of PDF
  669. * @param int $w Width of PDF
  670. * @param string $unit Unit of height (mm, pt, ...)
  671. * @param string $text Text to show
  672. * @return void
  673. */
  674. function pdf_watermark(&$pdf, $outputlangs, $h, $w, $unit, $text)
  675. {
  676. global $langs, $mysoc, $user;
  677. // Print Draft Watermark
  678. if ($unit == 'pt') {
  679. $k = 1;
  680. } elseif ($unit == 'mm') {
  681. $k = 72 / 25.4;
  682. } elseif ($unit == 'cm') {
  683. $k = 72 / 2.54;
  684. } elseif ($unit == 'in') {
  685. $k = 72;
  686. }
  687. // Make substitution
  688. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, null);
  689. complete_substitutions_array($substitutionarray, $outputlangs, null);
  690. $text = make_substitutions($text, $substitutionarray, $outputlangs);
  691. $text = $outputlangs->convToOutputCharset($text);
  692. $savx = $pdf->getX();
  693. $savy = $pdf->getY();
  694. $watermark_angle = atan($h / $w) / 2;
  695. $watermark_x_pos = 0;
  696. $watermark_y_pos = $h / 3;
  697. $watermark_x = $w / 2;
  698. $watermark_y = $h / 3;
  699. $pdf->SetFont('', 'B', 40);
  700. $pdf->SetTextColor(255, 192, 203);
  701. //rotate
  702. $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));
  703. //print watermark
  704. $pdf->SetXY($watermark_x_pos, $watermark_y_pos);
  705. $pdf->Cell($w - 20, 25, $outputlangs->convToOutputCharset($text), "", 2, "C", 0);
  706. //antirotate
  707. $pdf->_out('Q');
  708. $pdf->SetXY($savx, $savy);
  709. }
  710. /**
  711. * Show bank informations for PDF generation
  712. *
  713. * @param TCPDF $pdf Object PDF
  714. * @param Translate $outputlangs Object lang
  715. * @param int $curx X
  716. * @param int $cury Y
  717. * @param Account $account Bank account object
  718. * @param int $onlynumber Output only number (bank+desk+key+number according to country, but without name of bank and domiciliation)
  719. * @param int $default_font_size Default font size
  720. * @return float The Y PDF position
  721. */
  722. function pdf_bank(&$pdf, $outputlangs, $curx, $cury, $account, $onlynumber = 0, $default_font_size = 10)
  723. {
  724. global $mysoc, $conf;
  725. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formbank.class.php';
  726. $diffsizetitle = (empty($conf->global->PDF_DIFFSIZE_TITLE) ? 3 : $conf->global->PDF_DIFFSIZE_TITLE);
  727. $diffsizecontent = (empty($conf->global->PDF_DIFFSIZE_CONTENT) ? 4 : $conf->global->PDF_DIFFSIZE_CONTENT);
  728. $pdf->SetXY($curx, $cury);
  729. if (empty($onlynumber)) {
  730. $pdf->SetFont('', 'B', $default_font_size - $diffsizetitle);
  731. $pdf->MultiCell(100, 3, $outputlangs->transnoentities('PaymentByTransferOnThisBankAccount').':', 0, 'L', 0);
  732. $cury += 4;
  733. }
  734. $outputlangs->load("banks");
  735. // Use correct name of bank id according to country
  736. $bickey = "BICNumber";
  737. if ($account->getCountryCode() == 'IN') {
  738. $bickey = "SWIFT";
  739. }
  740. // Get format of bank account according to its country
  741. $usedetailedbban = $account->useDetailedBBAN();
  742. //$onlynumber=0; $usedetailedbban=1; // For tests
  743. if ($usedetailedbban) {
  744. $savcurx = $curx;
  745. if (empty($onlynumber)) {
  746. $pdf->SetFont('', '', $default_font_size - $diffsizecontent);
  747. $pdf->SetXY($curx, $cury);
  748. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("Bank").': '.$outputlangs->convToOutputCharset($account->bank), 0, 'L', 0);
  749. $cury += 3;
  750. }
  751. 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
  752. // Note:
  753. // bank = code_banque (FR), sort code (GB, IR. Example: 12-34-56)
  754. // desk = code guichet (FR), used only when $usedetailedbban = 1
  755. // number = account number
  756. // key = check control key used only when $usedetailedbban = 1
  757. if (empty($onlynumber)) {
  758. $pdf->line($curx + 1, $cury + 1, $curx + 1, $cury + 6);
  759. }
  760. foreach ($account->getFieldsToShow() as $val) {
  761. $pdf->SetXY($curx, $cury + 4);
  762. $pdf->SetFont('', '', $default_font_size - 3);
  763. if ($val == 'BankCode') {
  764. // Bank code
  765. $tmplength = 18;
  766. $content = $account->code_banque;
  767. } elseif ($val == 'DeskCode') {
  768. // Desk
  769. $tmplength = 18;
  770. $content = $account->code_guichet;
  771. } elseif ($val == 'BankAccountNumber') {
  772. // Number
  773. $tmplength = 24;
  774. $content = $account->number;
  775. } elseif ($val == 'BankAccountNumberKey') {
  776. // Key
  777. $tmplength = 15;
  778. $content = $account->cle_rib;
  779. } elseif ($val == 'IBAN' || $val == 'BIC') {
  780. // Key
  781. $tmplength = 0;
  782. $content = '';
  783. } else {
  784. dol_print_error($account->db, 'Unexpected value for getFieldsToShow: '.$val);
  785. break;
  786. }
  787. $pdf->MultiCell($tmplength, 3, $outputlangs->convToOutputCharset($content), 0, 'C', 0);
  788. $pdf->SetXY($curx, $cury + 1);
  789. $curx += $tmplength;
  790. $pdf->SetFont('', 'B', $default_font_size - $diffsizecontent);
  791. $pdf->MultiCell($tmplength, 3, $outputlangs->transnoentities($val), 0, 'C', 0);
  792. if (empty($onlynumber)) {
  793. $pdf->line($curx, $cury + 1, $curx, $cury + 7);
  794. }
  795. }
  796. $curx = $savcurx;
  797. $cury += 8;
  798. }
  799. } else {
  800. $pdf->SetFont('', 'B', $default_font_size - $diffsizecontent);
  801. $pdf->SetXY($curx, $cury);
  802. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("Bank").': '.$outputlangs->convToOutputCharset($account->bank), 0, 'L', 0);
  803. $cury += 3;
  804. $pdf->SetFont('', 'B', $default_font_size - $diffsizecontent);
  805. $pdf->SetXY($curx, $cury);
  806. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("BankAccountNumber").': '.$outputlangs->convToOutputCharset($account->number), 0, 'L', 0);
  807. $cury += 3;
  808. if ($diffsizecontent <= 2) {
  809. $cury += 1;
  810. }
  811. }
  812. $pdf->SetFont('', '', $default_font_size - $diffsizecontent);
  813. if (empty($onlynumber) && !empty($account->domiciliation)) {
  814. $pdf->SetXY($curx, $cury);
  815. $val = $outputlangs->transnoentities("Residence").': '.$outputlangs->convToOutputCharset($account->domiciliation);
  816. $pdf->MultiCell(100, 3, $val, 0, 'L', 0);
  817. //$nboflines=dol_nboflines_bis($val,120);
  818. //$cury+=($nboflines*3)+2;
  819. $tmpy = $pdf->getStringHeight(100, $val);
  820. $cury += $tmpy;
  821. }
  822. if (!empty($account->proprio)) {
  823. $pdf->SetXY($curx, $cury);
  824. $val = $outputlangs->transnoentities("BankAccountOwner").': '.$outputlangs->convToOutputCharset($account->proprio);
  825. $pdf->MultiCell(100, 3, $val, 0, 'L', 0);
  826. $tmpy = $pdf->getStringHeight(100, $val);
  827. $cury += $tmpy;
  828. } elseif (!$usedetailedbban) {
  829. $cury += 1;
  830. }
  831. // Use correct name of bank id according to country
  832. $ibankey = FormBank::getIBANLabel($account);
  833. if (!empty($account->iban)) {
  834. //Remove whitespaces to ensure we are dealing with the format we expect
  835. $ibanDisplay_temp = str_replace(' ', '', $outputlangs->convToOutputCharset($account->iban));
  836. $ibanDisplay = "";
  837. $nbIbanDisplay_temp = dol_strlen($ibanDisplay_temp);
  838. for ($i = 0; $i < $nbIbanDisplay_temp; $i++) {
  839. $ibanDisplay .= $ibanDisplay_temp[$i];
  840. if ($i % 4 == 3 && $i > 0) {
  841. $ibanDisplay .= " ";
  842. }
  843. }
  844. $pdf->SetFont('', 'B', $default_font_size - 3);
  845. $pdf->SetXY($curx, $cury);
  846. $pdf->MultiCell(100, 3, $outputlangs->transnoentities($ibankey).': '.$ibanDisplay, 0, 'L', 0);
  847. $cury += 3;
  848. }
  849. if (!empty($account->bic)) {
  850. $pdf->SetFont('', 'B', $default_font_size - 3);
  851. $pdf->SetXY($curx, $cury);
  852. $pdf->MultiCell(100, 3, $outputlangs->transnoentities($bickey).': '.$outputlangs->convToOutputCharset($account->bic), 0, 'L', 0);
  853. }
  854. return $pdf->getY();
  855. }
  856. /**
  857. * Show footer of page for PDF generation
  858. *
  859. * @param TCPDF $pdf The PDF factory
  860. * @param Translate $outputlangs Object lang for output
  861. * @param string $paramfreetext Constant name of free text
  862. * @param Societe $fromcompany Object company
  863. * @param int $marge_basse Margin bottom we use for the autobreak
  864. * @param int $marge_gauche Margin left (no more used)
  865. * @param int $page_hauteur Page height
  866. * @param Object $object Object shown in PDF
  867. * @param int $showdetails Show company adress details into footer (0=Nothing, 1=Show address, 2=Show managers, 3=Both)
  868. * @param int $hidefreetext 1=Hide free text, 0=Show free text
  869. * @param int $page_largeur Page width
  870. * @param int $watermark Watermark text to print on page
  871. * @return int Return height of bottom margin including footer text
  872. */
  873. function pdf_pagefoot(&$pdf, $outputlangs, $paramfreetext, $fromcompany, $marge_basse, $marge_gauche, $page_hauteur, $object, $showdetails = 0, $hidefreetext = 0, $page_largeur = 0, $watermark = '')
  874. {
  875. global $conf, $user, $mysoc, $hookmanager;
  876. $outputlangs->load("dict");
  877. $line = '';
  878. $reg = array();
  879. $dims = $pdf->getPageDimensions();
  880. // Line of free text
  881. if (empty($hidefreetext) && !empty($conf->global->$paramfreetext)) {
  882. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
  883. // More substitution keys
  884. $substitutionarray['__FROM_NAME__'] = $fromcompany->name;
  885. $substitutionarray['__FROM_EMAIL__'] = $fromcompany->email;
  886. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  887. $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray, $outputlangs);
  888. // Make a change into HTML code to allow to include images from medias directory.
  889. // <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
  890. // become
  891. // <img alt="" src="'.DOL_DATA_ROOT.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
  892. $newfreetext = preg_replace('/(<img.*src=")[^\"]*viewimage\.php[^\"]*modulepart=medias[^\"]*file=([^\"]*)("[^\/]*\/>)/', '\1file:/'.DOL_DATA_ROOT.'/medias/\2\3', $newfreetext);
  893. $line .= $outputlangs->convToOutputCharset($newfreetext);
  894. }
  895. // First line of company infos
  896. $line1 = "";
  897. $line2 = "";
  898. $line3 = "";
  899. $line4 = "";
  900. if ($showdetails == 1 || $showdetails == 3) {
  901. // Company name
  902. if ($fromcompany->name) {
  903. $line1 .= ($line1 ? " - " : "").$outputlangs->transnoentities("RegisteredOffice").": ".$fromcompany->name;
  904. }
  905. // Address
  906. if ($fromcompany->address) {
  907. $line1 .= ($line1 ? " - " : "").str_replace("\n", ", ", $fromcompany->address);
  908. }
  909. // Zip code
  910. if ($fromcompany->zip) {
  911. $line1 .= ($line1 ? " - " : "").$fromcompany->zip;
  912. }
  913. // Town
  914. if ($fromcompany->town) {
  915. $line1 .= ($line1 ? " " : "").$fromcompany->town;
  916. }
  917. // Country
  918. if ($fromcompany->country) {
  919. $line1 .= ($line1 ? ", " : "").$fromcompany->country;
  920. }
  921. // Phone
  922. if ($fromcompany->phone) {
  923. $line2 .= ($line2 ? " - " : "").$outputlangs->transnoentities("Phone").": ".$fromcompany->phone;
  924. }
  925. // Fax
  926. if ($fromcompany->fax) {
  927. $line2 .= ($line2 ? " - " : "").$outputlangs->transnoentities("Fax").": ".$fromcompany->fax;
  928. }
  929. // URL
  930. if ($fromcompany->url) {
  931. $line2 .= ($line2 ? " - " : "").$fromcompany->url;
  932. }
  933. // Email
  934. if ($fromcompany->email) {
  935. $line2 .= ($line2 ? " - " : "").$fromcompany->email;
  936. }
  937. }
  938. if ($showdetails == 2 || $showdetails == 3 || (!empty($fromcompany->country_code) && $fromcompany->country_code == 'DE')) {
  939. // Managers
  940. if ($fromcompany->managers) {
  941. $line2 .= ($line2 ? " - " : "").$fromcompany->managers;
  942. }
  943. }
  944. // Line 3 of company infos
  945. // Juridical status
  946. if (!empty($fromcompany->forme_juridique_code) && $fromcompany->forme_juridique_code) {
  947. $line3 .= ($line3 ? " - " : "").$outputlangs->convToOutputCharset(getFormeJuridiqueLabel($fromcompany->forme_juridique_code));
  948. }
  949. // Capital
  950. if (!empty($fromcompany->capital)) {
  951. $tmpamounttoshow = price2num($fromcompany->capital); // This field is a free string or a float
  952. if (is_numeric($tmpamounttoshow) && $tmpamounttoshow > 0) {
  953. $line3 .= ($line3 ? " - " : "").$outputlangs->transnoentities("CapitalOf", price($tmpamounttoshow, 0, $outputlangs, 0, 0, 0, $conf->currency));
  954. } elseif (!empty($fromcompany->capital)) {
  955. $line3 .= ($line3 ? " - " : "").$outputlangs->transnoentities("CapitalOf", $fromcompany->capital, $outputlangs);
  956. }
  957. }
  958. // Prof Id 1
  959. if (!empty($fromcompany->idprof1) && $fromcompany->idprof1 && ($fromcompany->country_code != 'FR' || !$fromcompany->idprof2)) {
  960. $field = $outputlangs->transcountrynoentities("ProfId1", $fromcompany->country_code);
  961. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  962. $field = $reg[1];
  963. }
  964. $line3 .= ($line3 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof1);
  965. }
  966. // Prof Id 2
  967. if (!empty($fromcompany->idprof2) && $fromcompany->idprof2) {
  968. $field = $outputlangs->transcountrynoentities("ProfId2", $fromcompany->country_code);
  969. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  970. $field = $reg[1];
  971. }
  972. $line3 .= ($line3 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof2);
  973. }
  974. // Line 4 of company infos
  975. // Prof Id 3
  976. if (!empty($fromcompany->idprof3) && $fromcompany->idprof3) {
  977. $field = $outputlangs->transcountrynoentities("ProfId3", $fromcompany->country_code);
  978. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  979. $field = $reg[1];
  980. }
  981. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof3);
  982. }
  983. // Prof Id 4
  984. if (!empty($fromcompany->idprof4) && $fromcompany->idprof4) {
  985. $field = $outputlangs->transcountrynoentities("ProfId4", $fromcompany->country_code);
  986. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  987. $field = $reg[1];
  988. }
  989. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof4);
  990. }
  991. // Prof Id 5
  992. if (!empty($fromcompany->idprof5) && $fromcompany->idprof5) {
  993. $field = $outputlangs->transcountrynoentities("ProfId5", $fromcompany->country_code);
  994. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  995. $field = $reg[1];
  996. }
  997. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof5);
  998. }
  999. // Prof Id 6
  1000. if (!empty($fromcompany->idprof6) && $fromcompany->idprof6) {
  1001. $field = $outputlangs->transcountrynoentities("ProfId6", $fromcompany->country_code);
  1002. if (preg_match('/\((.*)\)/i', $field, $reg)) {
  1003. $field = $reg[1];
  1004. }
  1005. $line4 .= ($line4 ? " - " : "").$field.": ".$outputlangs->convToOutputCharset($fromcompany->idprof6);
  1006. }
  1007. // IntraCommunautary VAT
  1008. if (!empty($fromcompany->tva_intra) && $fromcompany->tva_intra != '') {
  1009. $line4 .= ($line4 ? " - " : "").$outputlangs->transnoentities("VATIntraShort").": ".$outputlangs->convToOutputCharset($fromcompany->tva_intra);
  1010. }
  1011. $pdf->SetFont('', '', 7);
  1012. $pdf->SetDrawColor(224, 224, 224);
  1013. // The start of the bottom of this page footer is positioned according to # of lines
  1014. $freetextheight = 0;
  1015. if ($line) { // Free text
  1016. //$line="sample text<br>\nfd<strong>sf</strong>sdf<br>\nghfghg<br>";
  1017. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) {
  1018. $width = 20000;
  1019. $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.
  1020. if (!empty($conf->global->MAIN_USE_AUTOWRAP_ON_FREETEXT)) {
  1021. $width = 200;
  1022. $align = 'C';
  1023. }
  1024. $freetextheight = $pdf->getStringHeight($width, $line);
  1025. } else {
  1026. $freetextheight = pdfGetHeightForHtmlContent($pdf, dol_htmlentitiesbr($line, 1, 'UTF-8', 0)); // New method (works for HTML content)
  1027. //print '<br>'.$freetextheight;exit;
  1028. }
  1029. }
  1030. // For customize footer
  1031. if (is_object($hookmanager)) {
  1032. $parameters = array('line1' => $line1, 'line2' => $line2, 'line3' => $line3, 'line4' => $line4, 'outputlangs'=>$outputlangs);
  1033. $action = '';
  1034. $hookmanager->executeHooks('pdf_pagefoot', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1035. if (!empty($hookmanager->resPrint) && $hidefreetext == 0) {
  1036. $mycustomfooter = $hookmanager->resPrint;
  1037. $mycustomfooterheight = pdfGetHeightForHtmlContent($pdf, dol_htmlentitiesbr($mycustomfooter, 1, 'UTF-8', 0));
  1038. $marginwithfooter = $marge_basse + $freetextheight + $mycustomfooterheight;
  1039. $posy = $marginwithfooter + 0;
  1040. if ($line) { // Free text
  1041. $pdf->SetXY($dims['lm'], -$posy);
  1042. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) { // by default
  1043. $pdf->MultiCell(0, 3, $line, 0, $align, 0);
  1044. } else {
  1045. $pdf->writeHTMLCell($pdf->page_largeur - $pdf->margin_left - $pdf->margin_right, $freetextheight, $dims['lm'], $dims['hk'] - $marginwithfooter, dol_htmlentitiesbr($line, 1, 'UTF-8', 0));
  1046. }
  1047. $posy -= $freetextheight;
  1048. }
  1049. $pdf->SetY(-$posy);
  1050. $pdf->line($dims['lm'], $dims['hk'] - $posy, $dims['wk'] - $dims['rm'], $dims['hk'] - $posy);
  1051. $posy--;
  1052. $pdf->writeHTMLCell($pdf->page_largeur - $pdf->margin_left - $pdf->margin_right, $freetextheight, $dims['lm'], $dims['hk'] - $posy, dol_htmlentitiesbr($mycustomfooter, 1, 'UTF-8', 0));
  1053. $posy -= $mycustomfooterheight - 3;
  1054. } else {
  1055. // Else default footer
  1056. $marginwithfooter = $marge_basse + $freetextheight + (!empty($line1) ? 3 : 0) + (!empty($line2) ? 3 : 0) + (!empty($line3) ? 3 : 0) + (!empty($line4) ? 3 : 0);
  1057. $posy = $marginwithfooter + 0;
  1058. if ($line) { // Free text
  1059. $pdf->SetXY($dims['lm'], -$posy);
  1060. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) { // by default
  1061. $pdf->MultiCell(0, 3, $line, 0, $align, 0);
  1062. } else {
  1063. $pdf->writeHTMLCell($pdf->page_largeur - $pdf->margin_left - $pdf->margin_right, $freetextheight, $dims['lm'], $dims['hk'] - $marginwithfooter, dol_htmlentitiesbr($line, 1, 'UTF-8', 0));
  1064. }
  1065. $posy -= $freetextheight;
  1066. }
  1067. $pdf->SetY(-$posy);
  1068. $pdf->line($dims['lm'], $dims['hk'] - $posy, $dims['wk'] - $dims['rm'], $dims['hk'] - $posy);
  1069. $posy--;
  1070. if (!empty($line1)) {
  1071. $pdf->SetFont('', 'B', 7);
  1072. $pdf->SetXY($dims['lm'], -$posy);
  1073. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line1, 0, 'C', 0);
  1074. $posy -= 3;
  1075. $pdf->SetFont('', '', 7);
  1076. }
  1077. if (!empty($line2)) {
  1078. $pdf->SetFont('', 'B', 7);
  1079. $pdf->SetXY($dims['lm'], -$posy);
  1080. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line2, 0, 'C', 0);
  1081. $posy -= 3;
  1082. $pdf->SetFont('', '', 7);
  1083. }
  1084. if (!empty($line3)) {
  1085. $pdf->SetXY($dims['lm'], -$posy);
  1086. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line3, 0, 'C', 0);
  1087. }
  1088. if (!empty($line4)) {
  1089. $posy -= 3;
  1090. $pdf->SetXY($dims['lm'], -$posy);
  1091. $pdf->MultiCell($dims['wk'] - $dims['rm'] - $dims['lm'], 2, $line4, 0, 'C', 0);
  1092. }
  1093. }
  1094. }
  1095. // Show page nb only on iso languages (so default Helvetica font)
  1096. if (strtolower(pdf_getPDFFont($outputlangs)) == 'helvetica') {
  1097. $pdf->SetXY($dims['wk'] - $dims['rm'] - 15, -$posy);
  1098. //print 'xxx'.$pdf->PageNo().'-'.$pdf->getAliasNbPages().'-'.$pdf->getAliasNumPage();exit;
  1099. $pdf->MultiCell(15, 2, $pdf->PageNo().'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
  1100. }
  1101. // Show Draft Watermark
  1102. if (!empty($watermark)) {
  1103. pdf_watermark($pdf, $outputlangs, $page_hauteur, $page_largeur, 'mm', $watermark);
  1104. }
  1105. return $marginwithfooter;
  1106. }
  1107. /**
  1108. * Show linked objects for PDF generation
  1109. *
  1110. * @param TCPDF $pdf Object PDF
  1111. * @param object $object Object
  1112. * @param Translate $outputlangs Object lang
  1113. * @param int $posx X
  1114. * @param int $posy Y
  1115. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  1116. * @param float $h Cell minimum height. The cell extends automatically if needed.
  1117. * @param int $align Align
  1118. * @param string $default_font_size Font size
  1119. * @return float The Y PDF position
  1120. */
  1121. function pdf_writeLinkedObjects(&$pdf, $object, $outputlangs, $posx, $posy, $w, $h, $align, $default_font_size)
  1122. {
  1123. $linkedobjects = pdf_getLinkedObjects($object, $outputlangs);
  1124. if (!empty($linkedobjects)) {
  1125. foreach ($linkedobjects as $linkedobject) {
  1126. $reftoshow = $linkedobject["ref_title"].' : '.$linkedobject["ref_value"];
  1127. if (!empty($linkedobject["date_value"])) {
  1128. $reftoshow .= ' / '.$linkedobject["date_value"];
  1129. }
  1130. $posy += 3;
  1131. $pdf->SetXY($posx, $posy);
  1132. $pdf->SetFont('', '', $default_font_size - 2);
  1133. $pdf->MultiCell($w, $h, $reftoshow, '', $align);
  1134. }
  1135. }
  1136. return $pdf->getY();
  1137. }
  1138. /**
  1139. * Output line description into PDF
  1140. *
  1141. * @param TCPDF $pdf PDF object
  1142. * @param Object $object Object
  1143. * @param int $i Current line number
  1144. * @param Translate $outputlangs Object lang for output
  1145. * @param int $w Width
  1146. * @param int $h Height
  1147. * @param int $posx Pos x
  1148. * @param int $posy Pos y
  1149. * @param int $hideref Hide reference
  1150. * @param int $hidedesc Hide description
  1151. * @param int $issupplierline Is it a line for a supplier object ?
  1152. * @return string
  1153. */
  1154. function pdf_writelinedesc(&$pdf, $object, $i, $outputlangs, $w, $h, $posx, $posy, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
  1155. {
  1156. global $db, $conf, $langs, $hookmanager;
  1157. $reshook = 0;
  1158. $result = '';
  1159. //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) ) )
  1160. 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
  1161. $special_code = empty($object->lines[$i]->special_code) ? '' : $object->lines[$i]->special_code;
  1162. if (!empty($object->lines[$i]->fk_parent_line)) {
  1163. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1164. }
  1165. $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);
  1166. $action = '';
  1167. $reshook = $hookmanager->executeHooks('pdf_writelinedesc', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1168. if (!empty($hookmanager->resPrint)) {
  1169. $result .= $hookmanager->resPrint;
  1170. }
  1171. }
  1172. if (empty($reshook)) {
  1173. $labelproductservice = pdf_getlinedesc($object, $i, $outputlangs, $hideref, $hidedesc, $issupplierline);
  1174. //var_dump($labelproductservice);exit;
  1175. // 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"
  1176. // We make the reverse, so PDF generation has the real URL.
  1177. $nbrep = 0;
  1178. $labelproductservice = preg_replace('/(<img[^>]*src=")([^"]*)(&amp;)([^"]*")/', '\1\2&\4', $labelproductservice, -1, $nbrep);
  1179. //var_dump($labelproductservice);exit;
  1180. // Description
  1181. $pdf->writeHTMLCell($w, $h, $posx, $posy, $outputlangs->convToOutputCharset($labelproductservice), 0, 1, false, true, 'J', true);
  1182. $result .= $labelproductservice;
  1183. }
  1184. return $result;
  1185. }
  1186. /**
  1187. * Return line description translated in outputlangs and encoded into htmlentities and with <br>
  1188. *
  1189. * @param Object $object Object
  1190. * @param int $i Current line number (0 = first line, 1 = second line, ...)
  1191. * @param Translate $outputlangs Object langs for output
  1192. * @param int $hideref Hide reference
  1193. * @param int $hidedesc Hide description
  1194. * @param int $issupplierline Is it a line for a supplier object ?
  1195. * @return string String with line
  1196. */
  1197. function pdf_getlinedesc($object, $i, $outputlangs, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
  1198. {
  1199. global $db, $conf, $langs;
  1200. $idprod = (!empty($object->lines[$i]->fk_product) ? $object->lines[$i]->fk_product : false);
  1201. $label = (!empty($object->lines[$i]->label) ? $object->lines[$i]->label : (!empty($object->lines[$i]->product_label) ? $object->lines[$i]->product_label : ''));
  1202. $desc = (!empty($object->lines[$i]->desc) ? $object->lines[$i]->desc : (!empty($object->lines[$i]->description) ? $object->lines[$i]->description : ''));
  1203. $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
  1204. $note = (!empty($object->lines[$i]->note) ? $object->lines[$i]->note : '');
  1205. $dbatch = (!empty($object->lines[$i]->detail_batch) ? $object->lines[$i]->detail_batch : false);
  1206. if ($issupplierline) {
  1207. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  1208. $prodser = new ProductFournisseur($db);
  1209. } else {
  1210. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  1211. $prodser = new Product($db);
  1212. if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
  1213. include_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
  1214. }
  1215. }
  1216. if ($idprod) {
  1217. $prodser->fetch($idprod);
  1218. // If a predefined product and multilang and on other lang, we renamed label with label translated
  1219. if (!empty($conf->global->MAIN_MULTILANGS) && ($outputlangs->defaultlang != $langs->defaultlang)) {
  1220. $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)
  1221. // 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
  1222. // ($textwasnotmodified is replaced with $textwasmodifiedorcompleted and we add completion).
  1223. // Set label
  1224. // 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.
  1225. //var_dump($outputlangs->defaultlang.' - '.$langs->defaultlang.' - '.$label.' - '.$prodser->label);exit;
  1226. $textwasnotmodified = ($label == $prodser->label);
  1227. if (!empty($prodser->multilangs[$outputlangs->defaultlang]["label"]) && ($textwasnotmodified || $translatealsoifmodified)) {
  1228. $label = $prodser->multilangs[$outputlangs->defaultlang]["label"];
  1229. }
  1230. // Set desc
  1231. // Manage HTML entities description test because $prodser->description is store with htmlentities but $desc no
  1232. $textwasnotmodified = false;
  1233. if (!empty($desc) && dol_textishtml($desc) && !empty($prodser->description) && dol_textishtml($prodser->description)) {
  1234. $textwasnotmodified = (strpos(dol_html_entity_decode($desc, ENT_QUOTES | ENT_HTML5), dol_html_entity_decode($prodser->description, ENT_QUOTES | ENT_HTML5)) !== false);
  1235. } else {
  1236. $textwasnotmodified = ($desc == $prodser->description);
  1237. }
  1238. if (!empty($prodser->multilangs[$outputlangs->defaultlang]["description"]) && ($textwasnotmodified || $translatealsoifmodified)) {
  1239. $desc = $prodser->multilangs[$outputlangs->defaultlang]["description"];
  1240. }
  1241. // Set note
  1242. $textwasnotmodified = ($note == $prodser->note_public);
  1243. if (!empty($prodser->multilangs[$outputlangs->defaultlang]["other"]) && ($textwasnotmodified || $translatealsoifmodified)) {
  1244. $note = $prodser->multilangs[$outputlangs->defaultlang]["other"];
  1245. }
  1246. }
  1247. } 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
  1248. $desc = str_replace('(DEPOSIT)', $outputlangs->trans('Deposit'), $desc);
  1249. }
  1250. if (empty($conf->global->PDF_HIDE_PRODUCT_LABEL_IN_SUPPLIER_LINES)) {
  1251. // Description short of product line
  1252. $libelleproduitservice = $label;
  1253. if (!empty($libelleproduitservice) && !empty($conf->global->PDF_BOLD_PRODUCT_LABEL)) {
  1254. // Adding <b> may convert the original string into a HTML string. Sowe have to first
  1255. // convert \n into <br> we text is not already HTML.
  1256. if (!dol_textishtml($libelleproduitservice)) {
  1257. $libelleproduitservice = str_replace("\n", '<br>', $libelleproduitservice);
  1258. }
  1259. $libelleproduitservice = '<b>'.$libelleproduitservice.'</b>';
  1260. }
  1261. }
  1262. // Add ref of subproducts
  1263. if (!empty($conf->global->SHOW_SUBPRODUCT_REF_IN_PDF)) {
  1264. $prodser->get_sousproduits_arbo();
  1265. if (!empty($prodser->sousprods) && is_array($prodser->sousprods) && count($prodser->sousprods)) {
  1266. $tmparrayofsubproducts = reset($prodser->sousprods);
  1267. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF)) {
  1268. foreach ($tmparrayofsubproducts as $subprodval) {
  1269. $libelleproduitservice = dol_concatdesc($libelleproduitservice, " * ".$subprodval[3].' ('.$subprodval[1].')');
  1270. }
  1271. } else {
  1272. foreach ($tmparrayofsubproducts as $subprodval) {
  1273. $libelleproduitservice = dol_concatdesc($libelleproduitservice, " * ".$subprodval[5].(($subprodval[5] && $subprodval[3]) ? ' - ' : '').$subprodval[3].' ('.$subprodval[1].')');
  1274. }
  1275. }
  1276. }
  1277. }
  1278. // Description long of product line
  1279. if (!empty($desc) && ($desc != $label)) {
  1280. if ($desc == '(CREDIT_NOTE)' && $object->lines[$i]->fk_remise_except) {
  1281. $discount = new DiscountAbsolute($db);
  1282. $discount->fetch($object->lines[$i]->fk_remise_except);
  1283. $sourceref = !empty($discount->discount_type) ? $discount->ref_invoive_supplier_source : $discount->ref_facture_source;
  1284. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromCreditNote", $sourceref);
  1285. } elseif ($desc == '(DEPOSIT)' && $object->lines[$i]->fk_remise_except) {
  1286. $discount = new DiscountAbsolute($db);
  1287. $discount->fetch($object->lines[$i]->fk_remise_except);
  1288. $sourceref = !empty($discount->discount_type) ? $discount->ref_invoive_supplier_source : $discount->ref_facture_source;
  1289. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromDeposit", $sourceref);
  1290. // Add date of deposit
  1291. if (!empty($conf->global->INVOICE_ADD_DEPOSIT_DATE)) {
  1292. $libelleproduitservice .= ' ('.dol_print_date($discount->datec, 'day', '', $outputlangs).')';
  1293. }
  1294. } elseif ($desc == '(EXCESS RECEIVED)' && $object->lines[$i]->fk_remise_except) {
  1295. $discount = new DiscountAbsolute($db);
  1296. $discount->fetch($object->lines[$i]->fk_remise_except);
  1297. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromExcessReceived", $discount->ref_facture_source);
  1298. } elseif ($desc == '(EXCESS PAID)' && $object->lines[$i]->fk_remise_except) {
  1299. $discount = new DiscountAbsolute($db);
  1300. $discount->fetch($object->lines[$i]->fk_remise_except);
  1301. $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromExcessPaid", $discount->ref_invoice_supplier_source);
  1302. } else {
  1303. if ($idprod) {
  1304. // Check if description must be output
  1305. if (!empty($object->element)) {
  1306. $tmpkey = 'MAIN_DOCUMENTS_HIDE_DESCRIPTION_FOR_'.strtoupper($object->element);
  1307. if (!empty($conf->global->$tmpkey)) {
  1308. $hidedesc = 1;
  1309. }
  1310. }
  1311. if (empty($hidedesc)) {
  1312. if (!empty($conf->global->MAIN_DOCUMENTS_DESCRIPTION_FIRST)) {
  1313. $libelleproduitservice = dol_concatdesc($desc, $libelleproduitservice);
  1314. } else {
  1315. if (!empty($conf->global->HIDE_LABEL_VARIANT_PDF) && $prodser->isVariant()) {
  1316. $libelleproduitservice = $desc;
  1317. } else {
  1318. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desc);
  1319. }
  1320. }
  1321. }
  1322. } else {
  1323. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desc);
  1324. }
  1325. }
  1326. }
  1327. // We add ref of product (and supplier ref if defined)
  1328. $prefix_prodserv = "";
  1329. $ref_prodserv = "";
  1330. if (!empty($conf->global->PRODUCT_ADD_TYPE_IN_DOCUMENTS)) { // In standard mode, we do not show this
  1331. if ($prodser->isService()) {
  1332. $prefix_prodserv = $outputlangs->transnoentitiesnoconv("Service")." ";
  1333. } else {
  1334. $prefix_prodserv = $outputlangs->transnoentitiesnoconv("Product")." ";
  1335. }
  1336. }
  1337. if (empty($hideref)) {
  1338. if ($issupplierline) {
  1339. if (empty($conf->global->PDF_HIDE_PRODUCT_REF_IN_SUPPLIER_LINES)) { // Common case
  1340. $ref_prodserv = $prodser->ref; // Show local ref
  1341. if ($ref_supplier) {
  1342. $ref_prodserv .= ($prodser->ref ? ' (' : '').$outputlangs->transnoentitiesnoconv("SupplierRef").' '.$ref_supplier.($prodser->ref ? ')' : '');
  1343. }
  1344. } elseif ($conf->global->PDF_HIDE_PRODUCT_REF_IN_SUPPLIER_LINES == 1) {
  1345. $ref_prodserv = $ref_supplier;
  1346. } elseif ($conf->global->PDF_HIDE_PRODUCT_REF_IN_SUPPLIER_LINES == 2) {
  1347. $ref_prodserv = $ref_supplier.' ('.$outputlangs->transnoentitiesnoconv("InternalRef").' '.$prodser->ref.')';
  1348. }
  1349. } else {
  1350. $ref_prodserv = $prodser->ref; // Show local ref only
  1351. if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
  1352. $productCustomerPriceStatic = new Productcustomerprice($db);
  1353. $filter = array('fk_product' => $idprod, 'fk_soc' => $object->socid);
  1354. $nbCustomerPrices = $productCustomerPriceStatic->fetch_all('', '', 1, 0, $filter);
  1355. if ($nbCustomerPrices > 0) {
  1356. $productCustomerPrice = $productCustomerPriceStatic->lines[0];
  1357. if (! empty($productCustomerPrice->ref_customer)) {
  1358. switch ($conf->global->PRODUIT_CUSTOMER_PRICES_PDF_REF_MODE) {
  1359. case 1:
  1360. $ref_prodserv = $productCustomerPrice->ref_customer;
  1361. break;
  1362. case 2:
  1363. $ref_prodserv = $productCustomerPrice->ref_customer . ' (' . $outputlangs->transnoentitiesnoconv('InternalRef') . ' ' . $ref_prodserv . ')';
  1364. break;
  1365. default:
  1366. $ref_prodserv = $ref_prodserv . ' (' . $outputlangs->transnoentitiesnoconv('RefCustomer') . ' ' . $productCustomerPrice->ref_customer . ')';
  1367. }
  1368. }
  1369. }
  1370. }
  1371. }
  1372. if (!empty($libelleproduitservice) && !empty($ref_prodserv)) {
  1373. $ref_prodserv .= " - ";
  1374. }
  1375. }
  1376. if (!empty($ref_prodserv) && !empty($conf->global->PDF_BOLD_PRODUCT_REF_AND_PERIOD)) {
  1377. if (!dol_textishtml($libelleproduitservice)) {
  1378. $libelleproduitservice = str_replace("\n", '<br>', $libelleproduitservice);
  1379. }
  1380. $ref_prodserv = '<b>'.$ref_prodserv.'</b>';
  1381. // $prefix_prodserv and $ref_prodser are not HTML var
  1382. }
  1383. $libelleproduitservice = $prefix_prodserv.$ref_prodserv.$libelleproduitservice;
  1384. // Add an additional description for the category products
  1385. if (!empty($conf->global->CATEGORY_ADD_DESC_INTO_DOC) && $idprod && !empty($conf->categorie->enabled)) {
  1386. include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1387. $categstatic = new Categorie($db);
  1388. // recovering the list of all the categories linked to product
  1389. $tblcateg = $categstatic->containing($idprod, Categorie::TYPE_PRODUCT);
  1390. foreach ($tblcateg as $cate) {
  1391. // Adding the descriptions if they are filled
  1392. $desccateg = $cate->description;
  1393. if ($desccateg) {
  1394. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desccateg);
  1395. }
  1396. }
  1397. }
  1398. if (!empty($object->lines[$i]->date_start) || !empty($object->lines[$i]->date_end)) {
  1399. $format = 'day';
  1400. $period = '';
  1401. // Show duration if exists
  1402. if ($object->lines[$i]->date_start && $object->lines[$i]->date_end) {
  1403. $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)).')';
  1404. }
  1405. if ($object->lines[$i]->date_start && !$object->lines[$i]->date_end) {
  1406. $period = '('.$outputlangs->transnoentitiesnoconv('DateFrom', dol_print_date($object->lines[$i]->date_start, $format, false, $outputlangs)).')';
  1407. }
  1408. if (!$object->lines[$i]->date_start && $object->lines[$i]->date_end) {
  1409. $period = '('.$outputlangs->transnoentitiesnoconv('DateUntil', dol_print_date($object->lines[$i]->date_end, $format, false, $outputlangs)).')';
  1410. }
  1411. //print '>'.$outputlangs->charset_output.','.$period;
  1412. if (!empty($conf->global->PDF_BOLD_PRODUCT_REF_AND_PERIOD)) {
  1413. if (!dol_textishtml($libelleproduitservice)) {
  1414. $libelleproduitservice = str_replace("\n", '<br>', $libelleproduitservice);
  1415. }
  1416. $libelleproduitservice .= '<br><b style="color:#333666;" ><em>'.$period.'</em></b>';
  1417. } else {
  1418. $libelleproduitservice = dol_concatdesc($libelleproduitservice, $period);
  1419. }
  1420. //print $libelleproduitservice;
  1421. }
  1422. if ($dbatch) {
  1423. $format = 'day';
  1424. foreach ($dbatch as $detail) {
  1425. $dte = array();
  1426. if ($detail->eatby) {
  1427. $dte[] = $outputlangs->transnoentitiesnoconv('printEatby', dol_print_date($detail->eatby, $format, false, $outputlangs));
  1428. }
  1429. if ($detail->sellby) {
  1430. $dte[] = $outputlangs->transnoentitiesnoconv('printSellby', dol_print_date($detail->sellby, $format, false, $outputlangs));
  1431. }
  1432. if ($detail->batch) {
  1433. $dte[] = $outputlangs->transnoentitiesnoconv('printBatch', $detail->batch);
  1434. }
  1435. $dte[] = $outputlangs->transnoentitiesnoconv('printQty', $detail->qty);
  1436. $libelleproduitservice .= "__N__ ".implode(" - ", $dte);
  1437. }
  1438. }
  1439. // Now we convert \n into br
  1440. if (dol_textishtml($libelleproduitservice)) {
  1441. $libelleproduitservice = preg_replace('/__N__/', '<br>', $libelleproduitservice);
  1442. } else {
  1443. $libelleproduitservice = preg_replace('/__N__/', "\n", $libelleproduitservice);
  1444. }
  1445. $libelleproduitservice = dol_htmlentitiesbr($libelleproduitservice, 1);
  1446. return $libelleproduitservice;
  1447. }
  1448. /**
  1449. * Return line num
  1450. *
  1451. * @param Object $object Object
  1452. * @param int $i Current line number
  1453. * @param Translate $outputlangs Object langs for output
  1454. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1455. * @return string
  1456. */
  1457. function pdf_getlinenum($object, $i, $outputlangs, $hidedetails = 0)
  1458. {
  1459. global $hookmanager;
  1460. $reshook = 0;
  1461. $result = '';
  1462. //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) ) )
  1463. 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
  1464. $special_code = $object->lines[$i]->special_code;
  1465. if (!empty($object->lines[$i]->fk_parent_line)) {
  1466. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1467. }
  1468. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1469. $action = '';
  1470. $reshook = $hookmanager->executeHooks('pdf_getlinenum', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1471. $result .= $hookmanager->resPrint;
  1472. }
  1473. if (empty($reshook)) {
  1474. $result .= dol_htmlentitiesbr($object->lines[$i]->num);
  1475. }
  1476. return $result;
  1477. }
  1478. /**
  1479. * Return line product ref
  1480. *
  1481. * @param Object $object Object
  1482. * @param int $i Current line number
  1483. * @param Translate $outputlangs Object langs for output
  1484. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1485. * @return string
  1486. */
  1487. function pdf_getlineref($object, $i, $outputlangs, $hidedetails = 0)
  1488. {
  1489. global $hookmanager;
  1490. $reshook = 0;
  1491. $result = '';
  1492. //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) ) )
  1493. 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
  1494. $special_code = $object->lines[$i]->special_code;
  1495. if (!empty($object->lines[$i]->fk_parent_line)) {
  1496. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1497. }
  1498. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1499. $action = '';
  1500. $reshook = $hookmanager->executeHooks('pdf_getlineref', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1501. $result .= $hookmanager->resPrint;
  1502. }
  1503. if (empty($reshook)) {
  1504. $result .= dol_htmlentitiesbr($object->lines[$i]->product_ref);
  1505. }
  1506. return $result;
  1507. }
  1508. /**
  1509. * Return line ref_supplier
  1510. *
  1511. * @param Object $object Object
  1512. * @param int $i Current line number
  1513. * @param Translate $outputlangs Object langs for output
  1514. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1515. * @return string
  1516. */
  1517. function pdf_getlineref_supplier($object, $i, $outputlangs, $hidedetails = 0)
  1518. {
  1519. global $hookmanager;
  1520. $reshook = 0;
  1521. $result = '';
  1522. //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) ) )
  1523. 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
  1524. $special_code = $object->lines[$i]->special_code;
  1525. if (!empty($object->lines[$i]->fk_parent_line)) {
  1526. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1527. }
  1528. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1529. $action = '';
  1530. $reshook = $hookmanager->executeHooks('pdf_getlineref_supplier', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1531. $result .= $hookmanager->resPrint;
  1532. }
  1533. if (empty($reshook)) {
  1534. $result .= dol_htmlentitiesbr($object->lines[$i]->ref_supplier);
  1535. }
  1536. return $result;
  1537. }
  1538. /**
  1539. * Return line vat rate
  1540. *
  1541. * @param Object $object Object
  1542. * @param int $i Current line number
  1543. * @param Translate $outputlangs Object langs for output
  1544. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1545. * @return string
  1546. */
  1547. function pdf_getlinevatrate($object, $i, $outputlangs, $hidedetails = 0)
  1548. {
  1549. global $conf, $hookmanager, $mysoc;
  1550. $result = '';
  1551. $reshook = 0;
  1552. //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) ) )
  1553. 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
  1554. $special_code = $object->lines[$i]->special_code;
  1555. if (!empty($object->lines[$i]->fk_parent_line)) {
  1556. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1557. }
  1558. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1559. $action = '';
  1560. $reshook = $hookmanager->executeHooks('pdf_getlinevatrate', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1561. if (!empty($hookmanager->resPrint)) {
  1562. $result .= $hookmanager->resPrint;
  1563. }
  1564. }
  1565. if (empty($reshook)) {
  1566. if (empty($hidedetails) || $hidedetails > 1) {
  1567. $tmpresult = '';
  1568. $tmpresult .= vatrate($object->lines[$i]->tva_tx, 0, $object->lines[$i]->info_bits, -1);
  1569. if (empty($conf->global->MAIN_PDF_MAIN_HIDE_SECOND_TAX)) {
  1570. if ($object->lines[$i]->total_localtax1 != 0) {
  1571. if (preg_replace('/[\s0%]/', '', $tmpresult)) {
  1572. $tmpresult .= '/';
  1573. } else {
  1574. $tmpresult = '';
  1575. }
  1576. $tmpresult .= vatrate(abs($object->lines[$i]->localtax1_tx), 0);
  1577. }
  1578. }
  1579. if (empty($conf->global->MAIN_PDF_MAIN_HIDE_THIRD_TAX)) {
  1580. if ($object->lines[$i]->total_localtax2 != 0) {
  1581. if (preg_replace('/[\s0%]/', '', $tmpresult)) {
  1582. $tmpresult .= '/';
  1583. } else {
  1584. $tmpresult = '';
  1585. }
  1586. $tmpresult .= vatrate(abs($object->lines[$i]->localtax2_tx), 0);
  1587. }
  1588. }
  1589. $tmpresult .= '%';
  1590. $result .= $tmpresult;
  1591. }
  1592. }
  1593. return $result;
  1594. }
  1595. /**
  1596. * Return line unit price excluding tax
  1597. *
  1598. * @param Object $object Object
  1599. * @param int $i Current line number
  1600. * @param Translate $outputlangs Object langs for output
  1601. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1602. * @return string
  1603. */
  1604. function pdf_getlineupexcltax($object, $i, $outputlangs, $hidedetails = 0)
  1605. {
  1606. global $conf, $hookmanager;
  1607. $sign = 1;
  1608. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  1609. $sign = -1;
  1610. }
  1611. $result = '';
  1612. $reshook = 0;
  1613. //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) ) )
  1614. 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
  1615. $special_code = $object->lines[$i]->special_code;
  1616. if (!empty($object->lines[$i]->fk_parent_line)) {
  1617. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1618. }
  1619. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1620. $action = '';
  1621. $reshook = $hookmanager->executeHooks('pdf_getlineupexcltax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1622. if (!empty($hookmanager->resPrint)) {
  1623. $result .= $hookmanager->resPrint;
  1624. }
  1625. }
  1626. if (empty($reshook)) {
  1627. if (empty($hidedetails) || $hidedetails > 1) {
  1628. $subprice = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1 ? $object->lines[$i]->multicurrency_subprice : $object->lines[$i]->subprice);
  1629. $result .= price($sign * $subprice, 0, $outputlangs);
  1630. }
  1631. }
  1632. return $result;
  1633. }
  1634. /**
  1635. * Return line unit price including tax
  1636. *
  1637. * @param Object $object Object
  1638. * @param int $i Current line number
  1639. * @param Translate $outputlangs Object langs for output
  1640. * @param int $hidedetails Hide value (0 = no, 1 = yes, 2 = just special lines)
  1641. * @return string
  1642. */
  1643. function pdf_getlineupwithtax($object, $i, $outputlangs, $hidedetails = 0)
  1644. {
  1645. global $hookmanager, $conf;
  1646. $sign = 1;
  1647. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  1648. $sign = -1;
  1649. }
  1650. $result = '';
  1651. $reshook = 0;
  1652. //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) ) )
  1653. 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
  1654. $special_code = $object->lines[$i]->special_code;
  1655. if (!empty($object->lines[$i]->fk_parent_line)) {
  1656. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1657. }
  1658. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1659. $action = '';
  1660. $reshook = $hookmanager->executeHooks('pdf_getlineupwithtax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1661. if (!empty($hookmanager->resPrint)) {
  1662. $result .= $hookmanager->resPrint;
  1663. }
  1664. }
  1665. if (empty($reshook)) {
  1666. if (empty($hidedetails) || $hidedetails > 1) {
  1667. $result .= price($sign * (($object->lines[$i]->subprice) + ($object->lines[$i]->subprice) * ($object->lines[$i]->tva_tx) / 100), 0, $outputlangs);
  1668. }
  1669. }
  1670. return $result;
  1671. }
  1672. /**
  1673. * Return line quantity
  1674. *
  1675. * @param Object $object Object
  1676. * @param int $i Current line number
  1677. * @param Translate $outputlangs Object langs for output
  1678. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1679. * @return string
  1680. */
  1681. function pdf_getlineqty($object, $i, $outputlangs, $hidedetails = 0)
  1682. {
  1683. global $hookmanager;
  1684. $result = '';
  1685. $reshook = 0;
  1686. //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) ) )
  1687. 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
  1688. $special_code = $object->lines[$i]->special_code;
  1689. if (!empty($object->lines[$i]->fk_parent_line)) {
  1690. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1691. }
  1692. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1693. $action = '';
  1694. $reshook = $hookmanager->executeHooks('pdf_getlineqty', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1695. if (!empty($hookmanager->resPrint)) {
  1696. $result = $hookmanager->resPrint;
  1697. }
  1698. }
  1699. if (empty($reshook)) {
  1700. if ($object->lines[$i]->special_code == 3) {
  1701. return '';
  1702. }
  1703. if (empty($hidedetails) || $hidedetails > 1) {
  1704. $result .= $object->lines[$i]->qty;
  1705. }
  1706. }
  1707. return $result;
  1708. }
  1709. /**
  1710. * Return line quantity asked
  1711. *
  1712. * @param Object $object Object
  1713. * @param int $i Current line number
  1714. * @param Translate $outputlangs Object langs for output
  1715. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1716. * @return string
  1717. */
  1718. function pdf_getlineqty_asked($object, $i, $outputlangs, $hidedetails = 0)
  1719. {
  1720. global $hookmanager;
  1721. $reshook = 0;
  1722. $result = '';
  1723. //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) ) )
  1724. 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
  1725. $special_code = $object->lines[$i]->special_code;
  1726. if (!empty($object->lines[$i]->fk_parent_line)) {
  1727. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1728. }
  1729. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1730. $action = '';
  1731. $reshook = $hookmanager->executeHooks('pdf_getlineqty_asked', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1732. if (!empty($hookmanager->resPrint)) {
  1733. $result .= $hookmanager->resPrint;
  1734. }
  1735. }
  1736. if (empty($reshook)) {
  1737. if ($object->lines[$i]->special_code == 3) {
  1738. return '';
  1739. }
  1740. if (empty($hidedetails) || $hidedetails > 1) {
  1741. $result .= $object->lines[$i]->qty_asked;
  1742. }
  1743. }
  1744. return $result;
  1745. }
  1746. /**
  1747. * Return line quantity shipped
  1748. *
  1749. * @param Object $object Object
  1750. * @param int $i Current line number
  1751. * @param Translate $outputlangs Object langs for output
  1752. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1753. * @return string
  1754. */
  1755. function pdf_getlineqty_shipped($object, $i, $outputlangs, $hidedetails = 0)
  1756. {
  1757. global $hookmanager;
  1758. $reshook = 0;
  1759. $result = '';
  1760. //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) ) )
  1761. 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
  1762. $special_code = $object->lines[$i]->special_code;
  1763. if (!empty($object->lines[$i]->fk_parent_line)) {
  1764. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1765. }
  1766. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1767. $action = '';
  1768. $reshook = $hookmanager->executeHooks('pdf_getlineqty_shipped', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1769. if (!empty($hookmanager->resPrint)) {
  1770. $result .= $hookmanager->resPrint;
  1771. }
  1772. }
  1773. if (empty($reshook)) {
  1774. if ($object->lines[$i]->special_code == 3) {
  1775. return '';
  1776. }
  1777. if (empty($hidedetails) || $hidedetails > 1) {
  1778. $result .= $object->lines[$i]->qty_shipped;
  1779. }
  1780. }
  1781. return $result;
  1782. }
  1783. /**
  1784. * Return line keep to ship quantity
  1785. *
  1786. * @param Object $object Object
  1787. * @param int $i Current line number
  1788. * @param Translate $outputlangs Object langs for output
  1789. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1790. * @return string
  1791. */
  1792. function pdf_getlineqty_keeptoship($object, $i, $outputlangs, $hidedetails = 0)
  1793. {
  1794. global $hookmanager;
  1795. $reshook = 0;
  1796. $result = '';
  1797. //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) ) )
  1798. 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
  1799. $special_code = $object->lines[$i]->special_code;
  1800. if (!empty($object->lines[$i]->fk_parent_line)) {
  1801. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1802. }
  1803. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1804. $action = '';
  1805. $reshook = $hookmanager->executeHooks('pdf_getlineqty_keeptoship', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1806. if (!empty($hookmanager->resPrint)) {
  1807. $result .= $hookmanager->resPrint;
  1808. }
  1809. }
  1810. if (empty($reshook)) {
  1811. if ($object->lines[$i]->special_code == 3) {
  1812. return '';
  1813. }
  1814. if (empty($hidedetails) || $hidedetails > 1) {
  1815. $result .= ($object->lines[$i]->qty_asked - $object->lines[$i]->qty_shipped);
  1816. }
  1817. }
  1818. return $result;
  1819. }
  1820. /**
  1821. * Return line unit
  1822. *
  1823. * @param Object $object Object
  1824. * @param int $i Current line number
  1825. * @param Translate $outputlangs Object langs for output
  1826. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1827. * @param HookManager $hookmanager Hook manager instance
  1828. * @return string Value for unit cell
  1829. */
  1830. function pdf_getlineunit($object, $i, $outputlangs, $hidedetails = 0, $hookmanager = false)
  1831. {
  1832. global $langs;
  1833. $reshook = 0;
  1834. $result = '';
  1835. //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) ) )
  1836. 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
  1837. $special_code = $object->lines[$i]->special_code;
  1838. if (!empty($object->lines[$i]->fk_parent_line)) {
  1839. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1840. }
  1841. $parameters = array(
  1842. 'i' => $i,
  1843. 'outputlangs' => $outputlangs,
  1844. 'hidedetails' => $hidedetails,
  1845. 'special_code' => $special_code
  1846. );
  1847. $action = '';
  1848. $reshook = $hookmanager->executeHooks('pdf_getlineunit', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1849. if (!empty($hookmanager->resPrint)) {
  1850. $result .= $hookmanager->resPrint;
  1851. }
  1852. }
  1853. if (empty($reshook)) {
  1854. if (empty($hidedetails) || $hidedetails > 1) {
  1855. $result .= $langs->transnoentitiesnoconv($object->lines[$i]->getLabelOfUnit('short'));
  1856. }
  1857. }
  1858. return $result;
  1859. }
  1860. /**
  1861. * Return line remise percent
  1862. *
  1863. * @param Object $object Object
  1864. * @param int $i Current line number
  1865. * @param Translate $outputlangs Object langs for output
  1866. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1867. * @return string
  1868. */
  1869. function pdf_getlineremisepercent($object, $i, $outputlangs, $hidedetails = 0)
  1870. {
  1871. global $hookmanager;
  1872. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1873. $reshook = 0;
  1874. $result = '';
  1875. //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) ) )
  1876. 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
  1877. $special_code = $object->lines[$i]->special_code;
  1878. if (!empty($object->lines[$i]->fk_parent_line)) {
  1879. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1880. }
  1881. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  1882. $action = '';
  1883. $reshook = $hookmanager->executeHooks('pdf_getlineremisepercent', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1884. if (!empty($hookmanager->resPrint)) {
  1885. $result .= $hookmanager->resPrint;
  1886. }
  1887. }
  1888. if (empty($reshook)) {
  1889. if ($object->lines[$i]->special_code == 3) {
  1890. return '';
  1891. }
  1892. if (empty($hidedetails) || $hidedetails > 1) {
  1893. $result .= dol_print_reduction($object->lines[$i]->remise_percent, $outputlangs);
  1894. }
  1895. }
  1896. return $result;
  1897. }
  1898. /**
  1899. * Return line percent
  1900. *
  1901. * @param Object $object Object
  1902. * @param int $i Current line number
  1903. * @param Translate $outputlangs Object langs for output
  1904. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1905. * @param HookManager $hookmanager Hook manager instance
  1906. * @return string
  1907. */
  1908. function pdf_getlineprogress($object, $i, $outputlangs, $hidedetails = 0, $hookmanager = null)
  1909. {
  1910. if (empty($hookmanager)) {
  1911. global $hookmanager;
  1912. }
  1913. global $conf;
  1914. $reshook = 0;
  1915. $result = '';
  1916. //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) ) )
  1917. 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
  1918. $special_code = $object->lines[$i]->special_code;
  1919. if (!empty($object->lines[$i]->fk_parent_line)) {
  1920. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1921. }
  1922. $parameters = array('i' => $i, 'outputlangs' => $outputlangs, 'hidedetails' => $hidedetails, 'special_code' => $special_code);
  1923. $action = '';
  1924. $reshook = $hookmanager->executeHooks('pdf_getlineprogress', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1925. if (!empty($hookmanager->resPrint)) {
  1926. return $hookmanager->resPrint;
  1927. }
  1928. }
  1929. if (empty($reshook)) {
  1930. if ($object->lines[$i]->special_code == 3) {
  1931. return '';
  1932. }
  1933. if (empty($hidedetails) || $hidedetails > 1) {
  1934. if (!empty($conf->global->SITUATION_DISPLAY_DIFF_ON_PDF)) {
  1935. $prev_progress = 0;
  1936. if (method_exists($object->lines[$i], 'get_prev_progress')) {
  1937. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  1938. }
  1939. $result = round($object->lines[$i]->situation_percent - $prev_progress, 1).'%';
  1940. } else {
  1941. $result = round($object->lines[$i]->situation_percent, 1).'%';
  1942. }
  1943. }
  1944. }
  1945. return $result;
  1946. }
  1947. /**
  1948. * Return line total excluding tax
  1949. *
  1950. * @param Object $object Object
  1951. * @param int $i Current line number
  1952. * @param Translate $outputlangs Object langs for output
  1953. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  1954. * @return string Return total of line excl tax
  1955. */
  1956. function pdf_getlinetotalexcltax($object, $i, $outputlangs, $hidedetails = 0)
  1957. {
  1958. global $conf, $hookmanager;
  1959. $sign = 1;
  1960. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  1961. $sign = -1;
  1962. }
  1963. $reshook = 0;
  1964. $result = '';
  1965. //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) ) )
  1966. 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
  1967. $special_code = $object->lines[$i]->special_code;
  1968. if (!empty($object->lines[$i]->fk_parent_line)) {
  1969. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  1970. }
  1971. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code, 'sign'=>$sign);
  1972. $action = '';
  1973. $reshook = $hookmanager->executeHooks('pdf_getlinetotalexcltax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1974. if (!empty($hookmanager->resPrint)) {
  1975. $result .= $hookmanager->resPrint;
  1976. }
  1977. }
  1978. if (empty($reshook)) {
  1979. if ($object->lines[$i]->special_code == 3) {
  1980. $result .= $outputlangs->transnoentities("Option");
  1981. } elseif (empty($hidedetails) || $hidedetails > 1) {
  1982. $total_ht = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1 ? $object->lines[$i]->multicurrency_total_ht : $object->lines[$i]->total_ht);
  1983. if (!empty($object->lines[$i]->situation_percent) && $object->lines[$i]->situation_percent > 0) {
  1984. // TODO Remove this. The total should be saved correctly in database instead of being modified here.
  1985. $prev_progress = 0;
  1986. $progress = 1;
  1987. if (method_exists($object->lines[$i], 'get_prev_progress')) {
  1988. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  1989. $progress = ($object->lines[$i]->situation_percent - $prev_progress) / 100;
  1990. }
  1991. $result .= price($sign * ($total_ht / ($object->lines[$i]->situation_percent / 100)) * $progress, 0, $outputlangs);
  1992. } else {
  1993. $result .= price($sign * $total_ht, 0, $outputlangs);
  1994. }
  1995. }
  1996. }
  1997. return $result;
  1998. }
  1999. /**
  2000. * Return line total including tax
  2001. *
  2002. * @param Object $object Object
  2003. * @param int $i Current line number
  2004. * @param Translate $outputlangs Object langs for output
  2005. * @param int $hidedetails Hide value (0 = no, 1 = yes, 2 = just special lines)
  2006. * @return string Return total of line incl tax
  2007. */
  2008. function pdf_getlinetotalwithtax($object, $i, $outputlangs, $hidedetails = 0)
  2009. {
  2010. global $hookmanager, $conf;
  2011. $sign = 1;
  2012. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  2013. $sign = -1;
  2014. }
  2015. $reshook = 0;
  2016. $result = '';
  2017. //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) ) )
  2018. 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
  2019. $special_code = $object->lines[$i]->special_code;
  2020. if (!empty($object->lines[$i]->fk_parent_line)) {
  2021. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  2022. }
  2023. $parameters = array('i'=>$i, 'outputlangs'=>$outputlangs, 'hidedetails'=>$hidedetails, 'special_code'=>$special_code);
  2024. $action = '';
  2025. $reshook = $hookmanager->executeHooks('pdf_getlinetotalwithtax', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  2026. if (!empty($hookmanager->resPrint)) {
  2027. $result .= $hookmanager->resPrint;
  2028. }
  2029. }
  2030. if (empty($reshook)) {
  2031. if ($object->lines[$i]->special_code == 3) {
  2032. $result .= $outputlangs->transnoentities("Option");
  2033. } elseif (empty($hidedetails) || $hidedetails > 1) {
  2034. $total_ttc = (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1 ? $object->lines[$i]->multicurrency_total_ttc : $object->lines[$i]->total_ttc);
  2035. if ($object->lines[$i]->situation_percent > 0) {
  2036. // TODO Remove this. The total should be saved correctly in database instead of being modified here.
  2037. $prev_progress = 0;
  2038. $progress = 1;
  2039. if (method_exists($object->lines[$i], 'get_prev_progress')) {
  2040. $prev_progress = $object->lines[$i]->get_prev_progress($object->id);
  2041. $progress = ($object->lines[$i]->situation_percent - $prev_progress) / 100;
  2042. }
  2043. $result .= price($sign * ($total_ttc / ($object->lines[$i]->situation_percent / 100)) * $progress, 0, $outputlangs);
  2044. } else {
  2045. $result .= price($sign * $total_ttc, 0, $outputlangs);
  2046. }
  2047. }
  2048. }
  2049. return $result;
  2050. }
  2051. /**
  2052. * Return linked objects to use for document generation.
  2053. * 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
  2054. *
  2055. * @param object $object Object
  2056. * @param Translate $outputlangs Object lang for output
  2057. * @return array Linked objects
  2058. */
  2059. function pdf_getLinkedObjects(&$object, $outputlangs)
  2060. {
  2061. global $db, $hookmanager;
  2062. $linkedobjects = array();
  2063. $object->fetchObjectLinked();
  2064. foreach ($object->linkedObjects as $objecttype => $objects) {
  2065. if ($objecttype == 'facture') {
  2066. // 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.
  2067. } elseif ($objecttype == 'propal' || $objecttype == 'supplier_proposal') {
  2068. $outputlangs->load('propal');
  2069. foreach ($objects as $elementobject) {
  2070. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefProposal");
  2071. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref);
  2072. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("DatePropal");
  2073. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->date, 'day', '', $outputlangs);
  2074. }
  2075. } elseif ($objecttype == 'commande' || $objecttype == 'supplier_order') {
  2076. $outputlangs->load('orders');
  2077. if (count($objects) > 1 && count($objects) <= (getDolGlobalInt("MAXREFONDOC") ? getDolGlobalInt("MAXREFONDOC") : 10)) {
  2078. $object->note_public = dol_concatdesc($object->note_public, '<br>'.$outputlangs->transnoentities("RefOrder").' : <br>');
  2079. foreach ($objects as $elementobject) {
  2080. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities($elementobject->ref).($elementobject->ref_client ? ' ('.$elementobject->ref_client.')' : '').($elementobject->ref_supplier ? ' ('.$elementobject->ref_supplier.')' : '').' ');
  2081. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities("OrderDate").' : '.dol_print_date($elementobject->date, 'day', '', $outputlangs).'<br>');
  2082. }
  2083. } elseif (count($objects) == 1) {
  2084. $elementobject = array_shift($objects);
  2085. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefOrder");
  2086. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref).($elementobject->ref_client ? ' ('.$elementobject->ref_client.')' : '').($elementobject->ref_supplier ? ' ('.$elementobject->ref_supplier.')' : '');
  2087. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("OrderDate");
  2088. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->date, 'day', '', $outputlangs);
  2089. }
  2090. } elseif ($objecttype == 'contrat') {
  2091. $outputlangs->load('contracts');
  2092. foreach ($objects as $elementobject) {
  2093. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefContract");
  2094. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref);
  2095. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("DateContract");
  2096. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->date_contrat, 'day', '', $outputlangs);
  2097. }
  2098. } elseif ($objecttype == 'fichinter') {
  2099. $outputlangs->load('interventions');
  2100. foreach ($objects as $elementobject) {
  2101. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("InterRef");
  2102. $linkedobjects[$objecttype]['ref_value'] = $outputlangs->transnoentities($elementobject->ref);
  2103. $linkedobjects[$objecttype]['date_title'] = $outputlangs->transnoentities("InterDate");
  2104. $linkedobjects[$objecttype]['date_value'] = dol_print_date($elementobject->datec, 'day', '', $outputlangs);
  2105. }
  2106. } elseif ($objecttype == 'shipping') {
  2107. $outputlangs->loadLangs(array("orders", "sendings"));
  2108. if (count($objects) > 1) {
  2109. $order = null;
  2110. if (empty($object->linkedObjects['commande']) && $object->element != 'commande') {
  2111. $object->note_public = dol_concatdesc($object->note_public, '<br>'.$outputlangs->transnoentities("RefOrder").' / '.$outputlangs->transnoentities("RefSending").' : <br>');
  2112. } else {
  2113. $object->note_public = dol_concatdesc($object->note_public, '<br>'.$outputlangs->transnoentities("RefSending").' : <br>');
  2114. }
  2115. // We concat this record info into fields xxx_value. title is overwrote.
  2116. foreach ($objects as $elementobject) {
  2117. 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
  2118. $elementobject->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 0);
  2119. if (! empty($elementobject->linkedObjectsIds['commande'])) {
  2120. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  2121. $order = new Commande($db);
  2122. $ret = $order->fetch(reset($elementobject->linkedObjectsIds['commande']));
  2123. if ($ret < 1) {
  2124. $order = null;
  2125. }
  2126. }
  2127. }
  2128. if (! is_object($order)) {
  2129. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->transnoentities($elementobject->ref));
  2130. $object->note_public = dol_concatdesc($object->note_public, '<br>');
  2131. } else {
  2132. $object->note_public = dol_concatdesc($object->note_public, $outputlangs->convToOutputCharset($order->ref).($order->ref_client ? ' ('.$order->ref_client.')' : ''));
  2133. $object->note_public = dol_concatdesc($object->note_public, ' / '.$outputlangs->transnoentities($elementobject->ref));
  2134. $object->note_public = dol_concatdesc($object->note_public, '<br>');
  2135. }
  2136. }
  2137. } elseif (count($objects) == 1) {
  2138. $elementobject = array_shift($objects);
  2139. $order = null;
  2140. // We concat this record info into fields xxx_value. title is overwrote.
  2141. 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
  2142. $elementobject->fetchObjectLinked(null, '', null, '', 'OR', 1, 'sourcetype', 0);
  2143. if (! empty($elementobject->linkedObjectsIds['commande'])) {
  2144. include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  2145. $order = new Commande($db);
  2146. $ret = $order->fetch(reset($elementobject->linkedObjectsIds['commande']));
  2147. if ($ret < 1) {
  2148. $order = null;
  2149. }
  2150. }
  2151. }
  2152. if (! is_object($order)) {
  2153. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefSending");
  2154. if (! empty($linkedobjects[$objecttype]['ref_value'])) $linkedobjects[$objecttype]['ref_value'] .= ' / ';
  2155. $linkedobjects[$objecttype]['ref_value'] .= $outputlangs->transnoentities($elementobject->ref);
  2156. } else {
  2157. $linkedobjects[$objecttype]['ref_title'] = $outputlangs->transnoentities("RefOrder").' / '.$outputlangs->transnoentities("RefSending");
  2158. if (empty($linkedobjects[$objecttype]['ref_value'])) $linkedobjects[$objecttype]['ref_value'] = $outputlangs->convToOutputCharset($order->ref).($order->ref_client ? ' ('.$order->ref_client.')' : '');
  2159. $linkedobjects[$objecttype]['ref_value'] .= ' / '.$outputlangs->transnoentities($elementobject->ref);
  2160. }
  2161. }
  2162. }
  2163. }
  2164. // For add external linked objects
  2165. if (is_object($hookmanager)) {
  2166. $parameters = array('linkedobjects' => $linkedobjects, 'outputlangs'=>$outputlangs);
  2167. $action = '';
  2168. $hookmanager->executeHooks('pdf_getLinkedObjects', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  2169. if (!empty($hookmanager->resArray)) {
  2170. $linkedobjects = $hookmanager->resArray;
  2171. }
  2172. }
  2173. return $linkedobjects;
  2174. }
  2175. /**
  2176. * Return dimensions to use for images onto PDF checking that width and height are not higher than
  2177. * maximum (16x32 by default).
  2178. *
  2179. * @param string $realpath Full path to photo file to use
  2180. * @return array Height and width to use to output image (in pdf user unit, so mm)
  2181. */
  2182. function pdf_getSizeForImage($realpath)
  2183. {
  2184. global $conf;
  2185. $maxwidth = (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH) ? 20 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH);
  2186. $maxheight = (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_HEIGHT) ? 32 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_HEIGHT);
  2187. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  2188. $tmp = dol_getImageSize($realpath);
  2189. if ($tmp['height']) {
  2190. $width = (int) round($maxheight * $tmp['width'] / $tmp['height']); // I try to use maxheight
  2191. if ($width > $maxwidth) { // Pb with maxheight, so i use maxwidth
  2192. $width = $maxwidth;
  2193. $height = (int) round($maxwidth * $tmp['height'] / $tmp['width']);
  2194. } else // No pb with maxheight
  2195. {
  2196. $height = $maxheight;
  2197. }
  2198. }
  2199. return array('width'=>$width, 'height'=>$height);
  2200. }
  2201. /**
  2202. * Return line total amount discount
  2203. *
  2204. * @param Object $object Object
  2205. * @param int $i Current line number
  2206. * @param Translate $outputlangs Object langs for output
  2207. * @param int $hidedetails Hide details (0=no, 1=yes, 2=just special lines)
  2208. * @return string Return total of line excl tax
  2209. */
  2210. function pdfGetLineTotalDiscountAmount($object, $i, $outputlangs, $hidedetails = 0)
  2211. {
  2212. global $conf, $hookmanager;
  2213. $sign = 1;
  2214. if (isset($object->type) && $object->type == 2 && !empty($conf->global->INVOICE_POSITIVE_CREDIT_NOTE)) {
  2215. $sign = -1;
  2216. }
  2217. if ($object->lines[$i]->special_code == 3) {
  2218. return $outputlangs->transnoentities("Option");
  2219. } else {
  2220. if (is_object($hookmanager)) {
  2221. $special_code = $object->lines[$i]->special_code;
  2222. if (!empty($object->lines[$i]->fk_parent_line)) {
  2223. $special_code = $object->getSpecialCode($object->lines[$i]->fk_parent_line);
  2224. }
  2225. $parameters = array(
  2226. 'i'=>$i,
  2227. 'outputlangs'=>$outputlangs,
  2228. 'hidedetails'=>$hidedetails,
  2229. 'special_code'=>$special_code
  2230. );
  2231. $action = '';
  2232. if ($hookmanager->executeHooks('getlinetotalremise', $parameters, $object, $action) > 0) {
  2233. return $hookmanager->resPrint; // Note that $action and $object may have been modified by some hooks
  2234. }
  2235. }
  2236. if (empty($hidedetails) || $hidedetails > 1) {
  2237. return $sign * (($object->lines[$i]->subprice * $object->lines[$i]->qty) - $object->lines[$i]->total_ht);
  2238. }
  2239. }
  2240. return '';
  2241. }