commondocgenerator.class.php 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
  8. * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
  9. * Copyright (C) 2020 Josep Lluís Amador <joseplluis@lliuretic.cat>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. * or see https://www.gnu.org/
  24. */
  25. /**
  26. * \file htdocs/core/class/commondocgenerator.class.php
  27. * \ingroup core
  28. * \brief File of parent class for documents generators
  29. */
  30. /**
  31. * Parent class for documents generators
  32. */
  33. abstract class CommonDocGenerator
  34. {
  35. /**
  36. * @var string Model name
  37. */
  38. public $name = '';
  39. /**
  40. * @var string Error code (or message)
  41. */
  42. public $error = '';
  43. /**
  44. * @var string[] Array of error strings
  45. */
  46. public $errors = array();
  47. /**
  48. * @var DoliDB Database handler.
  49. */
  50. protected $db;
  51. /**
  52. * @var Extrafields object
  53. */
  54. public $extrafieldsCache;
  55. /**
  56. * @var int If set to 1, save the fullname of generated file with path as the main doc when generating a doc with this template.
  57. */
  58. public $update_main_doc_field;
  59. /**
  60. * @var string The name of constant to use to scan ODT files (Exemple: 'COMMANDE_ADDON_PDF_ODT_PATH')
  61. */
  62. public $scandir;
  63. public $page_hauteur;
  64. public $page_largeur;
  65. public $marge_gauche;
  66. public $marge_droite;
  67. public $marge_haute;
  68. public $marge_basse;
  69. public $option_logo;
  70. public $option_tva;
  71. public $option_multilang;
  72. public $option_freetext;
  73. public $option_draft_watermark;
  74. public $option_modereg;
  75. public $option_condreg;
  76. public $option_escompte;
  77. public $option_credit_note;
  78. public $emetteur;
  79. /**
  80. * Constructor
  81. *
  82. * @param DoliDB $db Database handler
  83. */
  84. public function __construct($db)
  85. {
  86. $this->db = $db;
  87. }
  88. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  89. /**
  90. * Define array with couple substitution key => substitution value
  91. *
  92. * @param User $user User
  93. * @param Translate $outputlangs Language object for output
  94. * @return array Array of substitution key->code
  95. */
  96. public function get_substitutionarray_user($user, $outputlangs)
  97. {
  98. // phpcs:enable
  99. global $conf, $extrafields;
  100. $logotouse = $conf->user->dir_output.'/'.get_exdir($user->id, 2, 0, 1, $user, 'user').'/'.$user->photo;
  101. $array_user = array(
  102. 'myuser_lastname'=>$user->lastname,
  103. 'myuser_firstname'=>$user->firstname,
  104. 'myuser_fullname'=>$user->getFullName($outputlangs, 1),
  105. 'myuser_login'=>$user->login,
  106. 'myuser_phone'=>$user->office_phone,
  107. 'myuser_address'=>$user->address,
  108. 'myuser_zip'=>$user->zip,
  109. 'myuser_town'=>$user->town,
  110. 'myuser_country'=>$user->country,
  111. 'myuser_country_code'=>$user->country_code,
  112. 'myuser_state'=>$user->state,
  113. 'myuser_state_code'=>$user->state_code,
  114. 'myuser_fax'=>$user->office_fax,
  115. 'myuser_mobile'=>$user->user_mobile,
  116. 'myuser_email'=>$user->email,
  117. 'myuser_logo'=>$logotouse,
  118. 'myuser_job'=>$user->job,
  119. 'myuser_web'=>'' // url not exist in $user object
  120. );
  121. // Retrieve extrafields
  122. if (is_array($user->array_options) && count($user->array_options)) {
  123. $array_user = $this->fill_substitutionarray_with_extrafields($user, $array_user, $extrafields, 'myuser', $outputlangs);
  124. }
  125. return $array_user;
  126. }
  127. /**
  128. * Define array with couple substitution key => substitution value
  129. *
  130. * @param Adherent $member Member
  131. * @param Translate $outputlangs Language object for output
  132. * @return array Array of substitution key->code
  133. */
  134. public function getSubstitutionarrayMember($member, $outputlangs)
  135. {
  136. global $conf, $extrafields;
  137. if ($member->photo) {
  138. $logotouse = $conf->adherent->dir_output.'/'.get_exdir(0, 0, 0, 1, $member, 'user').'/photos/'.$member->photo;
  139. } else {
  140. $logotouse = DOL_DOCUMENT_ROOT.'/public/theme/common/nophoto.png';
  141. }
  142. $array_member = array(
  143. 'mymember_lastname' => $member->lastname,
  144. 'mymember_firstname' => $member->firstname,
  145. 'mymember_fullname' => $member->getFullName($outputlangs, 1),
  146. 'mymember_login' => $member->login,
  147. 'mymember_address' => $member->address,
  148. 'mymember_zip' => $member->zip,
  149. 'mymember_town' => $member->town,
  150. 'mymember_country_code' => $member->country_code,
  151. 'mymember_country' => $member->country,
  152. 'mymember_state_code' => $member->state_code,
  153. 'mymember_state' => $member->state,
  154. 'mymember_phone_perso' => $member->phone_perso,
  155. 'mymember_phone_pro' => $member->phone,
  156. 'mymember_phone_mobile' => $member->phone_mobile,
  157. 'mymember_email' => $member->email,
  158. 'mymember_logo' => $logotouse,
  159. 'mymember_gender' => $member->gender,
  160. 'mymember_birth_locale' => dol_print_date($member->birth, 'day', 'tzuser', $outputlangs),
  161. 'mymember_birth' => dol_print_date($member->birth, 'day', 'tzuser'),
  162. );
  163. // Retrieve extrafields
  164. if (is_array($member->array_options) && count($member->array_options)) {
  165. $array_member = $this->fill_substitutionarray_with_extrafields($member, $array_member, $extrafields, 'mymember', $outputlangs);
  166. }
  167. return $array_member;
  168. }
  169. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  170. /**
  171. * Define array with couple substitution key => substitution value
  172. *
  173. * @param Societe $mysoc Object thirdparty
  174. * @param Translate $outputlangs Language object for output
  175. * @return array Array of substitution key->code
  176. */
  177. public function get_substitutionarray_mysoc($mysoc, $outputlangs)
  178. {
  179. // phpcs:enable
  180. global $conf;
  181. if (empty($mysoc->forme_juridique) && !empty($mysoc->forme_juridique_code)) {
  182. $mysoc->forme_juridique = getFormeJuridiqueLabel($mysoc->forme_juridique_code);
  183. }
  184. if (empty($mysoc->country) && !empty($mysoc->country_code)) {
  185. $mysoc->country = $outputlangs->transnoentitiesnoconv("Country".$mysoc->country_code);
  186. }
  187. if (empty($mysoc->state) && !empty($mysoc->state_code)) {
  188. $mysoc->state = getState($mysoc->state_code, 0);
  189. }
  190. $logotouse = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  191. return array(
  192. 'mycompany_logo'=>$logotouse,
  193. 'mycompany_name'=>$mysoc->name,
  194. 'mycompany_email'=>$mysoc->email,
  195. 'mycompany_phone'=>$mysoc->phone,
  196. 'mycompany_fax'=>$mysoc->fax,
  197. 'mycompany_address'=>$mysoc->address,
  198. 'mycompany_zip'=>$mysoc->zip,
  199. 'mycompany_town'=>$mysoc->town,
  200. 'mycompany_country'=>$mysoc->country,
  201. 'mycompany_country_code'=>$mysoc->country_code,
  202. 'mycompany_state'=>$mysoc->state,
  203. 'mycompany_state_code'=>$mysoc->state_code,
  204. 'mycompany_web'=>$mysoc->url,
  205. 'mycompany_juridicalstatus'=>$mysoc->forme_juridique,
  206. 'mycompany_managers'=>$mysoc->managers,
  207. 'mycompany_capital'=>$mysoc->capital,
  208. 'mycompany_barcode'=>$mysoc->barcode,
  209. 'mycompany_idprof1'=>$mysoc->idprof1,
  210. 'mycompany_idprof2'=>$mysoc->idprof2,
  211. 'mycompany_idprof3'=>$mysoc->idprof3,
  212. 'mycompany_idprof4'=>$mysoc->idprof4,
  213. 'mycompany_idprof5'=>$mysoc->idprof5,
  214. 'mycompany_idprof6'=>$mysoc->idprof6,
  215. 'mycompany_vatnumber'=>$mysoc->tva_intra,
  216. 'mycompany_object'=>$mysoc->object,
  217. 'mycompany_note_private'=>$mysoc->note_private,
  218. //'mycompany_note_public'=>$mysoc->note_public, // Only private not exists for "mysoc" but both for thirdparties
  219. );
  220. }
  221. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  222. /**
  223. * Define array with couple substitution key => substitution value
  224. * For example {company_name}, {company_name_alias}
  225. *
  226. * @param Societe $object Object
  227. * @param Translate $outputlangs Language object for output
  228. * @param string $array_key Name of the key for return array
  229. * @return array Array of substitution key->code
  230. */
  231. public function get_substitutionarray_thirdparty($object, $outputlangs, $array_key = 'company')
  232. {
  233. // phpcs:enable
  234. global $conf, $extrafields;
  235. if (empty($object->country) && !empty($object->country_code)) {
  236. $object->country = $outputlangs->transnoentitiesnoconv("Country".$object->country_code);
  237. }
  238. if (empty($object->state) && !empty($object->state_code)) {
  239. $object->state = getState($object->state_code, 0);
  240. }
  241. $array_thirdparty = array(
  242. 'company_name'=>$object->name,
  243. 'company_name_alias' => $object->name_alias,
  244. 'company_email'=>$object->email,
  245. 'company_phone'=>$object->phone,
  246. 'company_fax'=>$object->fax,
  247. 'company_address'=>$object->address,
  248. 'company_zip'=>$object->zip,
  249. 'company_town'=>$object->town,
  250. 'company_country'=>$object->country,
  251. 'company_country_code'=>$object->country_code,
  252. 'company_state'=>$object->state,
  253. 'company_state_code'=>$object->state_code,
  254. 'company_web'=>$object->url,
  255. 'company_barcode'=>$object->barcode,
  256. 'company_vatnumber'=>$object->tva_intra,
  257. 'company_customercode'=>$object->code_client,
  258. 'company_suppliercode'=>$object->code_fournisseur,
  259. 'company_customeraccountancycode'=>$object->code_compta,
  260. 'company_supplieraccountancycode'=>$object->code_compta_fournisseur,
  261. 'company_juridicalstatus'=>$object->forme_juridique,
  262. 'company_outstanding_limit'=>$object->outstanding_limit,
  263. 'company_capital'=>$object->capital,
  264. 'company_idprof1'=>$object->idprof1,
  265. 'company_idprof2'=>$object->idprof2,
  266. 'company_idprof3'=>$object->idprof3,
  267. 'company_idprof4'=>$object->idprof4,
  268. 'company_idprof5'=>$object->idprof5,
  269. 'company_idprof6'=>$object->idprof6,
  270. 'company_note_public'=>$object->note_public,
  271. 'company_note_private'=>$object->note_private,
  272. 'company_default_bank_iban'=>(is_object($object->bank_account) ? $object->bank_account->iban : ''),
  273. 'company_default_bank_bic'=>(is_object($object->bank_account) ? $object->bank_account->bic : '')
  274. );
  275. // Retrieve extrafields
  276. if (is_array($object->array_options) && count($object->array_options)) {
  277. $object->fetch_optionals();
  278. $array_thirdparty = $this->fill_substitutionarray_with_extrafields($object, $array_thirdparty, $extrafields, $array_key, $outputlangs);
  279. }
  280. return $array_thirdparty;
  281. }
  282. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  283. /**
  284. * Define array with couple substitution key => substitution value
  285. *
  286. * @param Contact $object contact
  287. * @param Translate $outputlangs object for output
  288. * @param string $array_key Name of the key for return array
  289. * @return array Array of substitution key->code
  290. */
  291. public function get_substitutionarray_contact($object, $outputlangs, $array_key = 'object')
  292. {
  293. // phpcs:enable
  294. global $conf, $extrafields;
  295. if (empty($object->country) && !empty($object->country_code)) {
  296. $object->country = $outputlangs->transnoentitiesnoconv("Country".$object->country_code);
  297. }
  298. if (empty($object->state) && !empty($object->state_code)) {
  299. $object->state = getState($object->state_code, 0);
  300. }
  301. $array_contact = array(
  302. $array_key.'_fullname' => $object->getFullName($outputlangs, 1),
  303. $array_key.'_lastname' => $object->lastname,
  304. $array_key.'_firstname' => $object->firstname,
  305. $array_key.'_address' => $object->address,
  306. $array_key.'_zip' => $object->zip,
  307. $array_key.'_town' => $object->town,
  308. $array_key.'_state_id' => $object->state_id,
  309. $array_key.'_state_code' => $object->state_code,
  310. $array_key.'_state' => $object->state,
  311. $array_key.'_country_id' => $object->country_id,
  312. $array_key.'_country_code' => $object->country_code,
  313. $array_key.'_country' => $object->country,
  314. $array_key.'_poste' => $object->poste,
  315. $array_key.'_socid' => $object->socid,
  316. $array_key.'_statut' => $object->statut,
  317. $array_key.'_code' => $object->code,
  318. $array_key.'_email' => $object->email,
  319. $array_key.'_phone_pro' => $object->phone_pro,
  320. $array_key.'_phone_perso' => $object->phone_perso,
  321. $array_key.'_phone_mobile' => $object->phone_mobile,
  322. $array_key.'_fax' => $object->fax,
  323. $array_key.'_birthday' => $object->birthday,
  324. $array_key.'_default_lang' => $object->default_lang,
  325. $array_key.'_note_public' => $object->note_public,
  326. $array_key.'_note_private' => $object->note_private,
  327. $array_key.'_civility' => $object->civility,
  328. );
  329. // Retrieve extrafields
  330. if (is_array($object->array_options) && count($object->array_options)) {
  331. $object->fetch_optionals();
  332. $array_contact = $this->fill_substitutionarray_with_extrafields($object, $array_contact, $extrafields, $array_key, $outputlangs);
  333. }
  334. return $array_contact;
  335. }
  336. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  337. /**
  338. * Define array with couple substitution key => substitution value
  339. *
  340. * @param Translate $outputlangs Language object for output
  341. * @return array Array of substitution key->code
  342. */
  343. public function get_substitutionarray_other($outputlangs)
  344. {
  345. // phpcs:enable
  346. global $conf;
  347. $now = dol_now('gmt'); // gmt
  348. $array_other = array(
  349. // Date in default language
  350. 'current_date'=>dol_print_date($now, 'day', 'tzuser'),
  351. 'current_datehour'=>dol_print_date($now, 'dayhour', 'tzuser'),
  352. 'current_server_date'=>dol_print_date($now, 'day', 'tzserver'),
  353. 'current_server_datehour'=>dol_print_date($now, 'dayhour', 'tzserver'),
  354. // Date in requested output language
  355. 'current_date_locale'=>dol_print_date($now, 'day', 'tzuser', $outputlangs),
  356. 'current_datehour_locale'=>dol_print_date($now, 'dayhour', 'tzuser', $outputlangs),
  357. 'current_server_date_locale'=>dol_print_date($now, 'day', 'tzserver', $outputlangs),
  358. 'current_server_datehour_locale'=>dol_print_date($now, 'dayhour', 'tzserver', $outputlangs),
  359. );
  360. foreach ($conf->global as $key => $val) {
  361. if (isASecretKey($key)) {
  362. $newval = '*****forbidden*****';
  363. } else {
  364. $newval = $val;
  365. }
  366. $array_other['__['.$key.']__'] = $newval;
  367. }
  368. return $array_other;
  369. }
  370. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  371. /**
  372. * Define array with couple substitution key => substitution value
  373. *
  374. * @param Object $object Main object to use as data source
  375. * @param Translate $outputlangs Lang object to use for output
  376. * @param string $array_key Name of the key for return array
  377. * @return array Array of substitution
  378. */
  379. public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object')
  380. {
  381. // phpcs:enable
  382. global $conf, $extrafields;
  383. $sumpayed = $sumdeposit = $sumcreditnote = '';
  384. $already_payed_all = 0;
  385. if ($object->element == 'facture') {
  386. $invoice_source = new Facture($this->db);
  387. if ($object->fk_facture_source > 0) {
  388. $invoice_source->fetch($object->fk_facture_source);
  389. }
  390. $sumpayed = $object->getSommePaiement();
  391. $sumdeposit = $object->getSumDepositsUsed();
  392. $sumcreditnote = $object->getSumCreditNotesUsed();
  393. $already_payed_all = $sumpayed + $sumdeposit + $sumcreditnote;
  394. if ($object->fk_account > 0) {
  395. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  396. $bank_account = new Account($this->db);
  397. $bank_account->fetch($object->fk_account);
  398. }
  399. }
  400. $date = (isset($object->element) && $object->element == 'contrat' && isset($object->date_contrat)) ? $object->date_contrat : (isset($object->date) ? $object->date : null);
  401. $resarray = array(
  402. $array_key.'_id'=>$object->id,
  403. $array_key.'_ref' => (property_exists($object, 'ref') ? $object->ref : ''),
  404. $array_key.'_label' => (property_exists($object, 'label') ? $object->label : ''),
  405. $array_key.'_ref_ext' => (property_exists($object, 'ref_ext') ? $object->ref_ext : ''),
  406. $array_key.'_ref_customer'=>(!empty($object->ref_client) ? $object->ref_client : (empty($object->ref_customer) ? '' : $object->ref_customer)),
  407. $array_key.'_ref_supplier'=>(!empty($object->ref_fournisseur) ? $object->ref_fournisseur : (empty($object->ref_supplier) ? '' : $object->ref_supplier)),
  408. $array_key.'_source_invoice_ref'=>((empty($invoice_source) || empty($invoice_source->ref)) ? '' : $invoice_source->ref),
  409. // Dates
  410. $array_key.'_hour'=>dol_print_date($date, 'hour'),
  411. $array_key.'_date'=>dol_print_date($date, 'day'),
  412. $array_key.'_date_rfc'=>dol_print_date($date, 'dayrfc'),
  413. $array_key.'_date_limit'=>(!empty($object->date_lim_reglement) ?dol_print_date($object->date_lim_reglement, 'day') : ''),
  414. $array_key.'_date_end'=>(!empty($object->fin_validite) ?dol_print_date($object->fin_validite, 'day') : ''),
  415. $array_key.'_date_creation'=>dol_print_date($object->date_creation, 'day'),
  416. $array_key.'_date_modification'=>(!empty($object->date_modification) ?dol_print_date($object->date_modification, 'day') : ''),
  417. $array_key.'_date_validation'=>(!empty($object->date_validation) ?dol_print_date($object->date_validation, 'dayhour') : ''),
  418. $array_key.'_date_delivery_planed'=>(!empty($object->date_livraison) ?dol_print_date($object->date_livraison, 'day') : ''),
  419. $array_key.'_date_close'=>(!empty($object->date_cloture) ?dol_print_date($object->date_cloture, 'dayhour') : ''),
  420. $array_key.'_payment_mode_code'=>$object->mode_reglement_code,
  421. $array_key.'_payment_mode'=>($outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code) != 'PaymentType'.$object->mode_reglement_code ? $outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code) : $object->mode_reglement),
  422. $array_key.'_payment_term_code'=>$object->cond_reglement_code,
  423. $array_key.'_payment_term'=>($outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code) != 'PaymentCondition'.$object->cond_reglement_code ? $outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code) : ($object->cond_reglement_doc ? $object->cond_reglement_doc : $object->cond_reglement)),
  424. $array_key.'_incoterms' => (method_exists($object, 'display_incoterms') ? $object->display_incoterms() : ''),
  425. $array_key.'_bank_iban'=>$bank_account->iban,
  426. $array_key.'_bank_bic'=>$bank_account->bic,
  427. $array_key.'_bank_label'=>$bank_account->label,
  428. $array_key.'_bank_number'=>$bank_account->number,
  429. $array_key.'_bank_proprio'=>$bank_account->proprio,
  430. $array_key.'_total_ht_locale'=>price($object->total_ht, 0, $outputlangs),
  431. $array_key.'_total_vat_locale'=>(!empty($object->total_vat) ?price($object->total_vat, 0, $outputlangs) : price($object->total_tva, 0, $outputlangs)),
  432. $array_key.'_total_localtax1_locale'=>price($object->total_localtax1, 0, $outputlangs),
  433. $array_key.'_total_localtax2_locale'=>price($object->total_localtax2, 0, $outputlangs),
  434. $array_key.'_total_ttc_locale'=>price($object->total_ttc, 0, $outputlangs),
  435. $array_key.'_total_ht'=>price2num($object->total_ht),
  436. $array_key.'_total_vat'=>(!empty($object->total_vat) ?price2num($object->total_vat) : price2num($object->total_tva)),
  437. $array_key.'_total_localtax1'=>price2num($object->total_localtax1),
  438. $array_key.'_total_localtax2'=>price2num($object->total_localtax2),
  439. $array_key.'_total_ttc'=>price2num($object->total_ttc),
  440. $array_key.'_multicurrency_code' => $object->multicurrency_code,
  441. $array_key.'_multicurrency_tx' => price2num($object->multicurrency_tx),
  442. $array_key.'_multicurrency_total_ht' => price2num($object->multicurrency_total_ht),
  443. $array_key.'_multicurrency_total_tva' => price2num($object->multicurrency_total_tva),
  444. $array_key.'_multicurrency_total_ttc' => price2num($object->multicurrency_total_ttc),
  445. $array_key.'_multicurrency_total_ht_locale' => price($object->multicurrency_total_ht, 0, $outputlangs),
  446. $array_key.'_multicurrency_total_tva_locale' => price($object->multicurrency_total_tva, 0, $outputlangs),
  447. $array_key.'_multicurrency_total_ttc_locale' => price($object->multicurrency_total_ttc, 0, $outputlangs),
  448. $array_key.'_note_private'=>$object->note,
  449. $array_key.'_note_public'=>$object->note_public,
  450. $array_key.'_note'=>$object->note_public, // For backward compatibility
  451. // Payments
  452. $array_key.'_already_payed_locale'=>price($sumpayed, 0, $outputlangs),
  453. $array_key.'_already_payed'=>price2num($sumpayed),
  454. $array_key.'_already_deposit_locale'=>price($sumdeposit, 0, $outputlangs),
  455. $array_key.'_already_deposit'=>price2num($sumdeposit),
  456. $array_key.'_already_creditnote_locale'=>price($sumcreditnote, 0, $outputlangs),
  457. $array_key.'_already_creditnote'=>price2num($sumcreditnote),
  458. $array_key.'_already_payed_all_locale'=>price(price2num($already_payed_all, 'MT'), 0, $outputlangs),
  459. $array_key.'_already_payed_all'=> price2num($already_payed_all, 'MT'),
  460. // Remain to pay with all known information (except open direct debit requests)
  461. $array_key.'_remain_to_pay_locale'=>price(price2num($object->total_ttc - $already_payed_all, 'MT'), 0, $outputlangs),
  462. $array_key.'_remain_to_pay'=>price2num($object->total_ttc - $already_payed_all, 'MT')
  463. );
  464. if (method_exists($object, 'getTotalDiscount') && in_array(get_class($object), array('Propal', 'Proposal', 'Commande', 'Facture', 'SupplierProposal', 'CommandeFournisseur', 'FactureFournisseur'))) {
  465. $resarray[$array_key.'_total_discount_ht_locale'] = price($object->getTotalDiscount(), 0, $outputlangs);
  466. $resarray[$array_key.'_total_discount_ht'] = price2num($object->getTotalDiscount());
  467. } else {
  468. $resarray[$array_key.'_total_discount_ht_locale'] = '';
  469. $resarray[$array_key.'_total_discount_ht'] = '';
  470. }
  471. // Fetch project information if there is a project assigned to this object
  472. if ($object->element != "project" && !empty($object->fk_project) && $object->fk_project > 0) {
  473. if (!is_object($object->project)) {
  474. $object->fetch_projet();
  475. }
  476. $resarray[$array_key.'_project_ref'] = $object->project->ref;
  477. $resarray[$array_key.'_project_title'] = $object->project->title;
  478. $resarray[$array_key.'_project_description'] = $object->project->description;
  479. $resarray[$array_key.'_project_date_start'] = dol_print_date($object->project->date_start, 'day');
  480. $resarray[$array_key.'_project_date_end'] = dol_print_date($object->project->date_end, 'day');
  481. }
  482. // Add vat by rates
  483. if (is_array($object->lines) && count($object->lines) > 0) {
  484. $totalUp = 0;
  485. // Set substitution keys for different VAT rates
  486. foreach ($object->lines as $line) {
  487. // $line->tva_tx format depends on database field accuracy, no reliable. This is kept for backward compatibility
  488. if (empty($resarray[$array_key.'_total_vat_'.$line->tva_tx])) {
  489. $resarray[$array_key.'_total_vat_'.$line->tva_tx] = 0;
  490. }
  491. $resarray[$array_key.'_total_vat_'.$line->tva_tx] += $line->total_tva;
  492. $resarray[$array_key.'_total_vat_locale_'.$line->tva_tx] = price($resarray[$array_key.'_total_vat_'.$line->tva_tx]);
  493. // $vatformated is vat without not expected chars (so 20, or 8.5 or 5.99 for example)
  494. $vatformated = vatrate($line->tva_tx);
  495. if (empty($resarray[$array_key.'_total_vat_'.$vatformated])) {
  496. $resarray[$array_key.'_total_vat_'.$vatformated] = 0;
  497. }
  498. $resarray[$array_key.'_total_vat_'.$vatformated] += $line->total_tva;
  499. $resarray[$array_key.'_total_vat_locale_'.$vatformated] = price($resarray[$array_key.'_total_vat_'.$vatformated]);
  500. $totalUp += $line->subprice * $line->qty;
  501. }
  502. // Calculate total up and total discount percentage
  503. // Note that this added fields does not match a field into database in Dolibarr (Dolibarr manage discount on lines not as a global property of object)
  504. $resarray['object_total_up'] = $totalUp;
  505. $resarray['object_total_up_locale'] = price($resarray['object_total_up'], 0, $outputlangs);
  506. if (method_exists($object, 'getTotalDiscount') && in_array(get_class($object), array('Propal', 'Proposal', 'Commande', 'Facture', 'SupplierProposal', 'CommandeFournisseur', 'FactureFournisseur'))) {
  507. $totalDiscount = $object->getTotalDiscount();
  508. } else {
  509. $totalDiscount = 0;
  510. }
  511. if (!empty($totalUp) && !empty($totalDiscount)) {
  512. $resarray['object_total_discount'] = round(100 / $totalUp * $totalDiscount, 2);
  513. $resarray['object_total_discount_locale'] = price($resarray['object_total_discount'], 0, $outputlangs);
  514. } else {
  515. $resarray['object_total_discount'] = '';
  516. $resarray['object_total_discount_locale'] = '';
  517. }
  518. }
  519. // Retrieve extrafields
  520. if (is_array($object->array_options) && count($object->array_options)) {
  521. $object->fetch_optionals();
  522. $resarray = $this->fill_substitutionarray_with_extrafields($object, $resarray, $extrafields, $array_key, $outputlangs);
  523. }
  524. return $resarray;
  525. }
  526. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  527. /**
  528. * Define array with couple substitution key => substitution value
  529. *
  530. * @param Object $line Object line
  531. * @param Translate $outputlangs Lang object to use for output
  532. * @param int $linenumber The number of the line for the substitution of "object_line_pos"
  533. * @return array Return a substitution array
  534. */
  535. public function get_substitutionarray_lines($line, $outputlangs, $linenumber = 0)
  536. {
  537. // phpcs:enable
  538. global $conf;
  539. $resarray = array(
  540. 'line_pos' => $linenumber,
  541. 'line_fulldesc'=>doc_getlinedesc($line, $outputlangs),
  542. 'line_product_ref'=>(empty($line->product_ref) ? '' : $line->product_ref),
  543. 'line_product_ref_fourn'=>(empty($line->ref_fourn) ? '' : $line->ref_fourn), // for supplier doc lines
  544. 'line_product_label'=>(empty($line->product_label) ? '' : $line->product_label),
  545. 'line_product_type'=>(empty($line->product_type) ? '' : $line->product_type),
  546. 'line_product_barcode'=>(empty($line->product_barcode) ? '' : $line->product_barcode),
  547. 'line_product_desc'=>(empty($line->product_desc) ? '' : $line->product_desc),
  548. 'line_desc'=>$line->desc,
  549. 'line_vatrate'=>vatrate($line->tva_tx, true, $line->info_bits),
  550. 'line_localtax1_rate'=>vatrate($line->localtax1_tx),
  551. 'line_localtax2_rate'=>vatrate($line->localtax1_tx),
  552. 'line_up'=>price2num($line->subprice),
  553. 'line_up_locale'=>price($line->subprice, 0, $outputlangs),
  554. 'line_total_up'=>price2num($line->subprice * $line->qty),
  555. 'line_total_up_locale'=>price($line->subprice * $line->qty, 0, $outputlangs),
  556. 'line_qty'=>$line->qty,
  557. 'line_discount_percent'=>($line->remise_percent ? $line->remise_percent.'%' : ''),
  558. 'line_price_ht'=>price2num($line->total_ht),
  559. 'line_price_ttc'=>price2num($line->total_ttc),
  560. 'line_price_vat'=>price2num($line->total_tva),
  561. 'line_price_ht_locale'=>price($line->total_ht, 0, $outputlangs),
  562. 'line_price_ttc_locale'=>price($line->total_ttc, 0, $outputlangs),
  563. 'line_price_vat_locale'=>price($line->total_tva, 0, $outputlangs),
  564. // Dates
  565. 'line_date_start'=>dol_print_date($line->date_start, 'day'),
  566. 'line_date_start_locale'=>dol_print_date($line->date_start, 'day', 'tzserver', $outputlangs),
  567. 'line_date_start_rfc'=>dol_print_date($line->date_start, 'dayrfc'),
  568. 'line_date_end'=>dol_print_date($line->date_end, 'day'),
  569. 'line_date_end_locale'=>dol_print_date($line->date_end, 'day', 'tzserver', $outputlangs),
  570. 'line_date_end_rfc'=>dol_print_date($line->date_end, 'dayrfc'),
  571. 'line_multicurrency_code' => price2num($line->multicurrency_code),
  572. 'line_multicurrency_subprice' => price2num($line->multicurrency_subprice),
  573. 'line_multicurrency_total_ht' => price2num($line->multicurrency_total_ht),
  574. 'line_multicurrency_total_tva' => price2num($line->multicurrency_total_tva),
  575. 'line_multicurrency_total_ttc' => price2num($line->multicurrency_total_ttc),
  576. 'line_multicurrency_subprice_locale' => price($line->multicurrency_subprice, 0, $outputlangs),
  577. 'line_multicurrency_total_ht_locale' => price($line->multicurrency_total_ht, 0, $outputlangs),
  578. 'line_multicurrency_total_tva_locale' => price($line->multicurrency_total_tva, 0, $outputlangs),
  579. 'line_multicurrency_total_ttc_locale' => price($line->multicurrency_total_ttc, 0, $outputlangs),
  580. );
  581. // Units
  582. if (!empty($conf->global->PRODUCT_USE_UNITS)) {
  583. $resarray['line_unit'] = $outputlangs->trans($line->getLabelOfUnit('long'));
  584. $resarray['line_unit_short'] = $outputlangs->trans($line->getLabelOfUnit('short'));
  585. }
  586. // Retrieve extrafields
  587. $extrafieldkey = $line->table_element;
  588. $array_key = "line";
  589. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  590. $extrafields = new ExtraFields($this->db);
  591. $extrafields->fetch_name_optionals_label($extrafieldkey, true);
  592. $line->fetch_optionals();
  593. $resarray = $this->fill_substitutionarray_with_extrafields($line, $resarray, $extrafields, $array_key, $outputlangs);
  594. // Check if the current line belongs to a supplier order
  595. if (get_class($line) == 'CommandeFournisseurLigne') {
  596. // Add the product supplier extrafields to the substitutions
  597. $extrafields->fetch_name_optionals_label("product_fournisseur_price");
  598. $extralabels = $extrafields->attributes["product_fournisseur_price"]['label'];
  599. if (!empty($extralabels) && is_array($extralabels)) {
  600. $columns = "";
  601. foreach ($extralabels as $key => $label) {
  602. $columns .= "$key, ";
  603. }
  604. if ($columns != "") {
  605. $columns = substr($columns, 0, strlen($columns) - 2);
  606. $resql = $this->db->query("SELECT ".$columns." FROM ".$this->db->prefix()."product_fournisseur_price_extrafields AS ex INNER JOIN ".$this->db->prefix()."product_fournisseur_price AS f ON ex.fk_object = f.rowid WHERE f.ref_fourn = '".$this->db->escape($line->ref_supplier)."'");
  607. if ($this->db->num_rows($resql) > 0) {
  608. $resql = $this->db->fetch_object($resql);
  609. foreach ($extralabels as $key => $label) {
  610. $resarray['line_product_supplier_'.$key] = $resql->{$key};
  611. }
  612. }
  613. }
  614. }
  615. }
  616. // Load product data optional fields to the line -> enables to use "line_options_{extrafield}"
  617. if (isset($line->fk_product) && $line->fk_product > 0) {
  618. $tmpproduct = new Product($this->db);
  619. $result = $tmpproduct->fetch($line->fk_product);
  620. foreach ($tmpproduct->array_options as $key => $label) {
  621. $resarray["line_product_".$key] = $label;
  622. }
  623. }
  624. return $resarray;
  625. }
  626. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  627. /**
  628. * Define array with couple substitution key => substitution value
  629. *
  630. * @param Expedition $object Main object to use as data source
  631. * @param Translate $outputlangs Lang object to use for output
  632. * @param array $array_key Name of the key for return array
  633. * @return array Array of substitution
  634. */
  635. public function get_substitutionarray_shipment($object, $outputlangs, $array_key = 'object')
  636. {
  637. // phpcs:enable
  638. global $conf, $extrafields;
  639. dol_include_once('/core/lib/product.lib.php');
  640. $object->list_delivery_methods($object->shipping_method_id);
  641. $calculatedVolume = ($object->trueWidth * $object->trueHeight * $object->trueDepth);
  642. $array_shipment = array(
  643. $array_key.'_id'=>$object->id,
  644. $array_key.'_ref'=>$object->ref,
  645. $array_key.'_ref_ext'=>$object->ref_ext,
  646. $array_key.'_ref_customer'=>$object->ref_customer,
  647. $array_key.'_date_delivery'=>dol_print_date($object->date_delivery, 'day'),
  648. $array_key.'_hour_delivery'=>dol_print_date($object->date_delivery, 'hour'),
  649. $array_key.'_date_creation'=>dol_print_date($object->date_creation, 'day'),
  650. $array_key.'_total_ht'=>price($object->total_ht),
  651. $array_key.'_total_vat'=>price($object->total_tva),
  652. $array_key.'_total_ttc'=>price($object->total_ttc),
  653. $array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
  654. $array_key.'_note_private'=>$object->note_private,
  655. $array_key.'_note'=>$object->note_public,
  656. $array_key.'_tracking_number'=>$object->tracking_number,
  657. $array_key.'_tracking_url'=>$object->tracking_url,
  658. $array_key.'_shipping_method'=>$object->listmeths[0]['libelle'],
  659. $array_key.'_weight'=>$object->trueWeight.' '.measuringUnitString(0, 'weight', $object->weight_units),
  660. $array_key.'_width'=>$object->trueWidth.' '.measuringUnitString(0, 'size', $object->width_units),
  661. $array_key.'_height'=>$object->trueHeight.' '.measuringUnitString(0, 'size', $object->height_units),
  662. $array_key.'_depth'=>$object->trueDepth.' '.measuringUnitString(0, 'size', $object->depth_units),
  663. $array_key.'_size'=>$calculatedVolume.' '.measuringUnitString(0, 'volume'),
  664. );
  665. // Add vat by rates
  666. foreach ($object->lines as $line) {
  667. if (empty($array_shipment[$array_key.'_total_vat_'.$line->tva_tx])) {
  668. $array_shipment[$array_key.'_total_vat_'.$line->tva_tx] = 0;
  669. }
  670. $array_shipment[$array_key.'_total_vat_'.$line->tva_tx] += $line->total_tva;
  671. }
  672. // Retrieve extrafields
  673. if (is_array($object->array_options) && count($object->array_options)) {
  674. $object->fetch_optionals();
  675. $array_shipment = $this->fill_substitutionarray_with_extrafields($object, $array_shipment, $extrafields, $array_key, $outputlangs);
  676. }
  677. // Add infor from $object->xxx where xxx has been loaded by fetch_origin() of shipment
  678. if (!empty($object->commande) && is_object($object->commande)) {
  679. $array_shipment['order_ref'] = $object->commande->ref;
  680. $array_shipment['order_ref_customer'] = $object->commande->ref_customer;
  681. }
  682. return $array_shipment;
  683. }
  684. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  685. /**
  686. * Define array with couple substitution key => substitution value
  687. *
  688. * @param ExpeditionLigne $line Object line
  689. * @param Translate $outputlangs Lang object to use for output
  690. * @return array Substitution array
  691. */
  692. public function get_substitutionarray_shipment_lines($line, $outputlangs)
  693. {
  694. // phpcs:enable
  695. global $conf;
  696. dol_include_once('/core/lib/product.lib.php');
  697. $resarray = array(
  698. 'line_fulldesc'=>doc_getlinedesc($line, $outputlangs),
  699. 'line_product_ref'=>$line->product_ref,
  700. 'line_product_label'=>$line->product_label,
  701. 'line_desc'=>$line->desc,
  702. 'line_vatrate'=>vatrate($line->tva_tx, true, $line->info_bits),
  703. 'line_up'=>price($line->subprice),
  704. 'line_total_up'=>price($line->subprice * $line->qty),
  705. 'line_qty'=>$line->qty,
  706. 'line_qty_shipped'=>$line->qty_shipped,
  707. 'line_qty_asked'=>$line->qty_asked,
  708. 'line_discount_percent'=>($line->remise_percent ? $line->remise_percent.'%' : ''),
  709. 'line_price_ht'=>price($line->total_ht),
  710. 'line_price_ttc'=>price($line->total_ttc),
  711. 'line_price_vat'=>price($line->total_tva),
  712. 'line_weight'=>empty($line->weight) ? '' : $line->weight * $line->qty_shipped.' '.measuringUnitString(0, 'weight', $line->weight_units),
  713. 'line_length'=>empty($line->length) ? '' : $line->length * $line->qty_shipped.' '.measuringUnitString(0, 'size', $line->length_units),
  714. 'line_surface'=>empty($line->surface) ? '' : $line->surface * $line->qty_shipped.' '.measuringUnitString(0, 'surface', $line->surface_units),
  715. 'line_volume'=>empty($line->volume) ? '' : $line->volume * $line->qty_shipped.' '.measuringUnitString(0, 'volume', $line->volume_units),
  716. );
  717. // Retrieve extrafields
  718. $extrafieldkey = $line->element;
  719. $array_key = "line";
  720. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  721. $extrafields = new ExtraFields($this->db);
  722. $extrafields->fetch_name_optionals_label($extrafieldkey, true);
  723. $line->fetch_optionals();
  724. $resarray = $this->fill_substitutionarray_with_extrafields($line, $resarray, $extrafields, $array_key, $outputlangs);
  725. // Load product data optional fields to the line -> enables to use "line_product_options_{extrafield}"
  726. if (isset($line->fk_product) && $line->fk_product > 0) {
  727. $tmpproduct = new Product($this->db);
  728. $tmpproduct->fetch($line->fk_product);
  729. foreach ($tmpproduct->array_options as $key=>$label)
  730. $resarray["line_product_".$key] = $label;
  731. }
  732. return $resarray;
  733. }
  734. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  735. /**
  736. * Define array with couple substitution key => substitution value
  737. *
  738. * @param Object $object Dolibarr Object
  739. * @param Translate $outputlangs Language object for output
  740. * @param boolean|int $recursive Want to fetch child array or child object.
  741. * @return array Array of substitution key->code
  742. */
  743. public function get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive = 1)
  744. {
  745. // phpcs:enable
  746. $array_other = array();
  747. if (!empty($object)) {
  748. foreach ($object as $key => $value) {
  749. if (in_array($key, array('db', 'fields', 'lines', 'modelpdf', 'model_pdf'))) { // discard some properties
  750. continue;
  751. }
  752. if (!empty($value)) {
  753. if (!is_array($value) && !is_object($value)) {
  754. $array_other['object_'.$key] = $value;
  755. } elseif (is_array($value) && $recursive) {
  756. $tmparray = $this->get_substitutionarray_each_var_object($value, $outputlangs, 0);
  757. foreach ($tmparray as $key2 => $value2) {
  758. $array_other['object_'.$key.'_'.preg_replace('/^object_/', '', $key2)] = $value2;
  759. }
  760. } elseif (is_object($value) && $recursive) {
  761. $tmparray = $this->get_substitutionarray_each_var_object($value, $outputlangs, 0);
  762. foreach ($tmparray as $key2 => $value2) {
  763. $array_other['object_'.$key.'_'.preg_replace('/^object_/', '', $key2)] = $value2;
  764. }
  765. }
  766. }
  767. }
  768. }
  769. //var_dump($array_other);
  770. return $array_other;
  771. }
  772. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  773. /**
  774. * Fill array with couple extrafield key => extrafield value
  775. *
  776. * @param Object $object Object with extrafields (must have $object->array_options filled)
  777. * @param array $array_to_fill Substitution array
  778. * @param Extrafields $extrafields Extrafields object
  779. * @param string $array_key Prefix for name of the keys into returned array
  780. * @param Translate $outputlangs Lang object to use for output
  781. * @return array Substitution array
  782. */
  783. public function fill_substitutionarray_with_extrafields($object, $array_to_fill, $extrafields, $array_key, $outputlangs)
  784. {
  785. // phpcs:enable
  786. global $conf;
  787. if (is_array($extrafields->attributes[$object->table_element]['label'])) {
  788. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) {
  789. if ($extrafields->attributes[$object->table_element]['type'][$key] == 'price') {
  790. $object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key]);
  791. $object->array_options['options_'.$key.'_currency'] = price($object->array_options['options_'.$key], 0, $outputlangs, 0, 0, -1, $conf->currency);
  792. //Add value to store price with currency
  793. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
  794. } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'select') {
  795. $valueofselectkey = $object->array_options['options_'.$key];
  796. if (array_key_exists($valueofselectkey, $extrafields->attributes[$object->table_element]['param'][$key]['options'])) {
  797. $object->array_options['options_'.$key] = $extrafields->attributes[$object->table_element]['param'][$key]['options'][$valueofselectkey];
  798. } else {
  799. $object->array_options['options_'.$key] = '';
  800. }
  801. } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'checkbox') {
  802. $valArray = explode(',', $object->array_options['options_'.$key]);
  803. $output = array();
  804. foreach ($extrafields->attributes[$object->table_element]['param'][$key]['options'] as $keyopt => $valopt) {
  805. if (in_array($keyopt, $valArray)) {
  806. $output[] = $valopt;
  807. }
  808. }
  809. $object->array_options['options_'.$key] = implode(', ', $output);
  810. } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'date') {
  811. if (strlen($object->array_options['options_'.$key]) > 0) {
  812. $date = $object->array_options['options_'.$key];
  813. $object->array_options['options_'.$key] = dol_print_date($date, 'day'); // using company output language
  814. $object->array_options['options_'.$key.'_locale'] = dol_print_date($date, 'day', 'tzserver', $outputlangs); // using output language format
  815. $object->array_options['options_'.$key.'_rfc'] = dol_print_date($date, 'dayrfc'); // international format
  816. } else {
  817. $object->array_options['options_'.$key] = '';
  818. $object->array_options['options_'.$key.'_locale'] = '';
  819. $object->array_options['options_'.$key.'_rfc'] = '';
  820. }
  821. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  822. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  823. } elseif ($extrafields->attributes[$object->table_element]['label'][$key] == 'datetime') {
  824. $datetime = $object->array_options['options_'.$key];
  825. $object->array_options['options_'.$key] = ($datetime != "0000-00-00 00:00:00" ?dol_print_date($object->array_options['options_'.$key], 'dayhour') : ''); // using company output language
  826. $object->array_options['options_'.$key.'_locale'] = ($datetime != "0000-00-00 00:00:00" ?dol_print_date($object->array_options['options_'.$key], 'dayhour', 'tzserver', $outputlangs) : ''); // using output language format
  827. $object->array_options['options_'.$key.'_rfc'] = ($datetime != "0000-00-00 00:00:00" ?dol_print_date($object->array_options['options_'.$key], 'dayhourrfc') : ''); // international format
  828. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  829. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  830. } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'link') {
  831. $id = $object->array_options['options_'.$key];
  832. if ($id != "") {
  833. $param = $extrafields->attributes[$object->table_element]['param'][$key];
  834. $param_list = array_keys($param['options']); // $param_list='ObjectName:classPath'
  835. $InfoFieldList = explode(":", $param_list[0]);
  836. $classname = $InfoFieldList[0];
  837. $classpath = $InfoFieldList[1];
  838. if (!empty($classpath)) {
  839. dol_include_once($InfoFieldList[1]);
  840. if ($classname && class_exists($classname)) {
  841. $tmpobject = new $classname($this->db);
  842. $tmpobject->fetch($id);
  843. // completely replace the id with the linked object name
  844. $object->array_options['options_'.$key] = $tmpobject->name;
  845. }
  846. }
  847. }
  848. }
  849. if (array_key_exists('options_'.$key, $object->array_options)) {
  850. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
  851. } else {
  852. $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key => ''));
  853. }
  854. }
  855. }
  856. return $array_to_fill;
  857. }
  858. /**
  859. * Rect pdf
  860. *
  861. * @param TCPDF $pdf Object PDF
  862. * @param float $x Abscissa of first point
  863. * @param float $y Ordinate of first point
  864. * @param float $l ??
  865. * @param float $h ??
  866. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  867. * @param int $hidebottom Hide bottom
  868. * @return void
  869. */
  870. public function printRect($pdf, $x, $y, $l, $h, $hidetop = 0, $hidebottom = 0)
  871. {
  872. if (empty($hidetop) || $hidetop == -1) {
  873. $pdf->line($x, $y, $x + $l, $y);
  874. }
  875. $pdf->line($x + $l, $y, $x + $l, $y + $h);
  876. if (empty($hidebottom)) {
  877. $pdf->line($x + $l, $y + $h, $x, $y + $h);
  878. }
  879. $pdf->line($x, $y + $h, $x, $y);
  880. }
  881. /**
  882. * uasort callback function to Sort columns fields
  883. *
  884. * @param array $a PDF lines array fields configs
  885. * @param array $b PDF lines array fields configs
  886. * @return int Return compare result
  887. */
  888. public function columnSort($a, $b)
  889. {
  890. if (empty($a['rank'])) {
  891. $a['rank'] = 0;
  892. }
  893. if (empty($b['rank'])) {
  894. $b['rank'] = 0;
  895. }
  896. if ($a['rank'] == $b['rank']) {
  897. return 0;
  898. }
  899. return ($a['rank'] > $b['rank']) ? -1 : 1;
  900. }
  901. /**
  902. * Prepare Array Column Field
  903. *
  904. * @param object $object common object
  905. * @param Translate $outputlangs langs
  906. * @param int $hidedetails Do not show line details
  907. * @param int $hidedesc Do not show desc
  908. * @param int $hideref Do not show ref
  909. * @return null
  910. */
  911. public function prepareArrayColumnField($object, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  912. {
  913. global $conf;
  914. $this->defineColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref);
  915. // Sorting
  916. uasort($this->cols, array($this, 'columnSort'));
  917. // Positionning
  918. $curX = $this->page_largeur - $this->marge_droite; // start from right
  919. // Array width
  920. $arrayWidth = $this->page_largeur - $this->marge_droite - $this->marge_gauche;
  921. // Count flexible column
  922. $totalDefinedColWidth = 0;
  923. $countFlexCol = 0;
  924. foreach ($this->cols as $colKey => & $colDef) {
  925. if (!$this->getColumnStatus($colKey)) {
  926. continue; // continue if disabled
  927. }
  928. if (!empty($colDef['scale'])) {
  929. // In case of column width is defined by percentage
  930. $colDef['width'] = abs($arrayWidth * $colDef['scale'] / 100);
  931. }
  932. if (empty($colDef['width'])) {
  933. $countFlexCol++;
  934. } else {
  935. $totalDefinedColWidth += $colDef['width'];
  936. }
  937. }
  938. foreach ($this->cols as $colKey => & $colDef) {
  939. // setting empty conf with default
  940. if (!empty($colDef['title'])) {
  941. $colDef['title'] = array_replace($this->defaultTitlesFieldsStyle, $colDef['title']);
  942. } else {
  943. $colDef['title'] = $this->defaultTitlesFieldsStyle;
  944. }
  945. // setting empty conf with default
  946. if (!empty($colDef['content'])) {
  947. $colDef['content'] = array_replace($this->defaultContentsFieldsStyle, $colDef['content']);
  948. } else {
  949. $colDef['content'] = $this->defaultContentsFieldsStyle;
  950. }
  951. if ($this->getColumnStatus($colKey)) {
  952. // In case of flexible column
  953. if (empty($colDef['width'])) {
  954. $colDef['width'] = abs(($arrayWidth - $totalDefinedColWidth)) / $countFlexCol;
  955. }
  956. // Set positions
  957. $lastX = $curX;
  958. $curX = $lastX - $colDef['width'];
  959. $colDef['xStartPos'] = $curX;
  960. $colDef['xEndPos'] = $lastX;
  961. }
  962. }
  963. }
  964. /**
  965. * get column content width from column key
  966. *
  967. * @param string $colKey the column key
  968. * @return float width in mm
  969. */
  970. public function getColumnContentWidth($colKey)
  971. {
  972. $colDef = $this->cols[$colKey];
  973. return $colDef['width'] - $colDef['content']['padding'][3] - $colDef['content']['padding'][1];
  974. }
  975. /**
  976. * get column content X (abscissa) left position from column key
  977. *
  978. * @param string $colKey the column key
  979. * @return float X position in mm
  980. */
  981. public function getColumnContentXStart($colKey)
  982. {
  983. $colDef = $this->cols[$colKey];
  984. return isset($colDef['xStartPos']) ? $colDef['xStartPos'] + $colDef['content']['padding'][3] : null;
  985. }
  986. /**
  987. * get column position rank from column key
  988. *
  989. * @param string $colKey the column key
  990. * @return int rank on success and -1 on error
  991. */
  992. public function getColumnRank($colKey)
  993. {
  994. if (!isset($this->cols[$colKey]['rank'])) {
  995. return -1;
  996. }
  997. return $this->cols[$colKey]['rank'];
  998. }
  999. /**
  1000. * get column position rank from column key
  1001. *
  1002. * @param string $newColKey the new column key
  1003. * @param array $defArray a single column definition array
  1004. * @param string $targetCol target column used to place the new column beside
  1005. * @param bool $insertAfterTarget insert before or after target column ?
  1006. * @return int new rank on success and -1 on error
  1007. */
  1008. public function insertNewColumnDef($newColKey, $defArray, $targetCol = false, $insertAfterTarget = false)
  1009. {
  1010. // prepare wanted rank
  1011. $rank = -1;
  1012. // try to get rank from target column
  1013. if (!empty($targetCol)) {
  1014. $rank = $this->getColumnRank($targetCol);
  1015. if ($rank >= 0 && $insertAfterTarget) {
  1016. $rank++;
  1017. }
  1018. }
  1019. // get rank from new column definition
  1020. if ($rank < 0 && !empty($defArray['rank'])) {
  1021. $rank = $defArray['rank'];
  1022. }
  1023. // error: no rank
  1024. if ($rank < 0) {
  1025. return -1;
  1026. }
  1027. foreach ($this->cols as $colKey => & $colDef) {
  1028. if ($rank <= $colDef['rank']) {
  1029. $colDef['rank'] = $colDef['rank'] + 1;
  1030. }
  1031. }
  1032. $defArray['rank'] = $rank;
  1033. $this->cols[$newColKey] = $defArray; // array_replace is used to preserve keys
  1034. return $rank;
  1035. }
  1036. /**
  1037. * print standard column content
  1038. *
  1039. * @param TCPDF $pdf pdf object
  1040. * @param float $curY curent Y position
  1041. * @param string $colKey the column key
  1042. * @param string $columnText column text
  1043. * @return null
  1044. */
  1045. public function printStdColumnContent($pdf, &$curY, $colKey, $columnText = '')
  1046. {
  1047. global $hookmanager;
  1048. $parameters = array(
  1049. 'curY' => &$curY,
  1050. 'columnText' => $columnText,
  1051. 'colKey' => $colKey,
  1052. 'pdf' => &$pdf,
  1053. );
  1054. $reshook = $hookmanager->executeHooks('printStdColumnContent', $parameters, $this); // Note that $action and $object may have been modified by hook
  1055. if ($reshook < 0) {
  1056. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1057. }
  1058. if (!$reshook) {
  1059. if (empty($columnText)) {
  1060. return;
  1061. }
  1062. $pdf->SetXY($this->getColumnContentXStart($colKey), $curY); // Set curent position
  1063. $colDef = $this->cols[$colKey];
  1064. // save curent cell padding
  1065. $curentCellPaddinds = $pdf->getCellPaddings();
  1066. // set cell padding with column content definition
  1067. $pdf->setCellPaddings($colDef['content']['padding'][3], $colDef['content']['padding'][0], $colDef['content']['padding'][1], $colDef['content']['padding'][2]);
  1068. $pdf->writeHTMLCell($colDef['width'], 2, $colDef['xStartPos'], $curY, $columnText, 0, 1, 0, true, $colDef['content']['align']);
  1069. // restore cell padding
  1070. $pdf->setCellPaddings($curentCellPaddinds['L'], $curentCellPaddinds['T'], $curentCellPaddinds['R'], $curentCellPaddinds['B']);
  1071. }
  1072. }
  1073. /**
  1074. * print description column content
  1075. *
  1076. * @param TCPDF $pdf pdf object
  1077. * @param float $curY curent Y position
  1078. * @param string $colKey the column key
  1079. * @param object $object CommonObject
  1080. * @param int $i the $object->lines array key
  1081. * @param Translate $outputlangs Output language
  1082. * @param int $hideref hide ref
  1083. * @param int $hidedesc hide desc
  1084. * @param int $issupplierline if object need supplier product
  1085. * @return null
  1086. */
  1087. public function printColDescContent($pdf, &$curY, $colKey, $object, $i, $outputlangs, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
  1088. {
  1089. // load desc col params
  1090. $colDef = $this->cols[$colKey];
  1091. // save curent cell padding
  1092. $curentCellPaddinds = $pdf->getCellPaddings();
  1093. // set cell padding with column content definition
  1094. $pdf->setCellPaddings($colDef['content']['padding'][3], $colDef['content']['padding'][0], $colDef['content']['padding'][1], $colDef['content']['padding'][2]);
  1095. // line description
  1096. pdf_writelinedesc($pdf, $object, $i, $outputlangs, $colDef['width'], 3, $colDef['xStartPos'], $curY, $hideref, $hidedesc, $issupplierline);
  1097. $posYAfterDescription = $pdf->GetY() - $colDef['content']['padding'][0];
  1098. // restore cell padding
  1099. $pdf->setCellPaddings($curentCellPaddinds['L'], $curentCellPaddinds['T'], $curentCellPaddinds['R'], $curentCellPaddinds['B']);
  1100. // Display extrafield if needed
  1101. $params = array(
  1102. 'display' => 'list',
  1103. 'printableEnable' => array(3),
  1104. 'printableEnableNotEmpty' => array(4)
  1105. );
  1106. $extrafieldDesc = $this->getExtrafieldsInHtml($object->lines[$i], $outputlangs, $params);
  1107. if (!empty($extrafieldDesc)) {
  1108. $this->printStdColumnContent($pdf, $posYAfterDescription, $colKey, $extrafieldDesc);
  1109. }
  1110. }
  1111. /**
  1112. * get extrafield content for pdf writeHtmlCell compatibility
  1113. * usage for PDF line columns and object note block
  1114. *
  1115. * @param object $object Common object
  1116. * @param string $extrafieldKey The extrafield key
  1117. * @param Translate $outputlangs The output langs (if value is __(XXX)__ we use it to translate it).
  1118. * @return string
  1119. */
  1120. public function getExtrafieldContent($object, $extrafieldKey, $outputlangs = null)
  1121. {
  1122. global $hookmanager;
  1123. if (empty($object->table_element)) {
  1124. return;
  1125. }
  1126. $extrafieldsKeyPrefix = "options_";
  1127. // Cleanup extrafield key to remove prefix if present
  1128. $pos = strpos($extrafieldKey, $extrafieldsKeyPrefix);
  1129. if ($pos === 0) {
  1130. $extrafieldKey = substr($extrafieldKey, strlen($extrafieldsKeyPrefix));
  1131. }
  1132. $extrafieldOptionsKey = $extrafieldsKeyPrefix.$extrafieldKey;
  1133. // Load extrafiels if not allready does
  1134. if (empty($this->extrafieldsCache)) {
  1135. $this->extrafieldsCache = new ExtraFields($this->db);
  1136. }
  1137. if (empty($this->extrafieldsCache->attributes[$object->table_element])) {
  1138. $this->extrafieldsCache->fetch_name_optionals_label($object->table_element);
  1139. }
  1140. $extrafields = $this->extrafieldsCache;
  1141. $extrafieldOutputContent = '';
  1142. if (isset($object->array_options[$extrafieldOptionsKey])) $extrafieldOutputContent = $extrafields->showOutputField($extrafieldKey, $object->array_options[$extrafieldOptionsKey], '', $object->table_element);
  1143. // TODO : allow showOutputField to be pdf public friendly, ex: in a link to object, clean getNomUrl to remove link and images... like a getName methode ...
  1144. if ($extrafields->attributes[$object->table_element]['type'][$extrafieldKey] == 'link') {
  1145. // for lack of anything better we cleanup all html tags
  1146. $extrafieldOutputContent = dol_string_nohtmltag($extrafieldOutputContent);
  1147. }
  1148. $parameters = array(
  1149. 'object' => $object,
  1150. 'extrafields' => $extrafields,
  1151. 'extrafieldKey' => $extrafieldKey,
  1152. 'extrafieldOutputContent' =>& $extrafieldOutputContent
  1153. );
  1154. $reshook = $hookmanager->executeHooks('getPDFExtrafieldContent', $parameters, $this); // Note that $action and $object may have been modified by hook
  1155. if ($reshook < 0) {
  1156. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1157. }
  1158. if ($reshook) {
  1159. $extrafieldOutputContent = $hookmanager->resPrint;
  1160. }
  1161. return $extrafieldOutputContent;
  1162. }
  1163. /**
  1164. * display extrafields columns content
  1165. *
  1166. * @param object $object line of common object
  1167. * @param Translate $outputlangs Output language
  1168. * @param array $params array of additionals parameters
  1169. * @return double max y value
  1170. */
  1171. public function getExtrafieldsInHtml($object, $outputlangs, $params = array())
  1172. {
  1173. global $hookmanager;
  1174. if (empty($object->table_element)) {
  1175. return;
  1176. }
  1177. // Load extrafiels if not allready does
  1178. if (empty($this->extrafieldsCache)) {
  1179. $this->extrafieldsCache = new ExtraFields($this->db);
  1180. }
  1181. if (empty($this->extrafieldsCache->attributes[$object->table_element])) {
  1182. $this->extrafieldsCache->fetch_name_optionals_label($object->table_element);
  1183. }
  1184. $extrafields = $this->extrafieldsCache;
  1185. $defaultParams = array(
  1186. 'style' => '',
  1187. 'display' => 'auto', // auto, table, list
  1188. 'printableEnable' => array(1),
  1189. 'printableEnableNotEmpty' => array(2),
  1190. 'table' => array(
  1191. 'maxItemsInRow' => 2,
  1192. 'cellspacing' => 0,
  1193. 'cellpadding' => 0,
  1194. 'border' => 0,
  1195. 'labelcolwidth' => '25%',
  1196. 'arrayOfLineBreakType' => array('text', 'html')
  1197. ),
  1198. 'list' => array(
  1199. 'separator' => '<br>'
  1200. ),
  1201. 'auto' => array(
  1202. 'list' => 0, // 0 for default
  1203. 'table' => 4 // if there more than x extrafield to display
  1204. ),
  1205. );
  1206. $params = $params + $defaultParams;
  1207. /**
  1208. * @var $extrafields ExtraFields
  1209. */
  1210. $html = '';
  1211. $fields = array();
  1212. if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label'])) {
  1213. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) {
  1214. // Enable extrafield ?
  1215. $enabled = 0;
  1216. $disableOnEmpty = 0;
  1217. if (!empty($extrafields->attributes[$object->table_element]['printable'][$key])) {
  1218. $printable = intval($extrafields->attributes[$object->table_element]['printable'][$key]);
  1219. if (in_array($printable, $params['printableEnable']) || in_array($printable, $params['printableEnableNotEmpty'])) {
  1220. $enabled = 1;
  1221. }
  1222. if (in_array($printable, $params['printableEnableNotEmpty'])) {
  1223. $disableOnEmpty = 1;
  1224. }
  1225. }
  1226. if (empty($enabled)) {
  1227. continue;
  1228. }
  1229. $field = new stdClass();
  1230. $field->rank = intval($extrafields->attributes[$object->table_element]['pos'][$key]);
  1231. $field->content = $this->getExtrafieldContent($object, $key, $outputlangs);
  1232. $field->label = $outputlangs->transnoentities($label);
  1233. $field->type = $extrafields->attributes[$object->table_element]['type'][$key];
  1234. // dont display if empty
  1235. if ($disableOnEmpty && empty($field->content)) {
  1236. continue;
  1237. }
  1238. $fields[] = $field;
  1239. }
  1240. }
  1241. if (!empty($fields)) {
  1242. // Sort extrafields by rank
  1243. uasort($fields, function ($a, $b) {
  1244. return ($a->rank > $b->rank) ? 1 : -1;
  1245. });
  1246. // define some HTML content with style
  1247. $html .= !empty($params['style']) ? '<style>'.$params['style'].'</style>' : '';
  1248. // auto select display format
  1249. if ($params['display'] == 'auto') {
  1250. $lastNnumbItems = 0;
  1251. foreach ($params['auto'] as $display => $numbItems) {
  1252. if ($lastNnumbItems <= $numbItems && count($fields) > $numbItems) {
  1253. $lastNnumbItems = $numbItems;
  1254. $params['display'] = $display;
  1255. }
  1256. }
  1257. }
  1258. if ($params['display'] == 'list') {
  1259. // Display in list format
  1260. $i = 0;
  1261. foreach ($fields as $field) {
  1262. $html .= !empty($i) ? $params['list']['separator'] : '';
  1263. $html .= '<strong>'.$field->label.' : </strong>';
  1264. $html .= $field->content;
  1265. $i++;
  1266. }
  1267. } elseif ($params['display'] == 'table') {
  1268. // Display in table format
  1269. $html .= '<table class="extrafield-table" cellspacing="'.$params['table']['cellspacing'].'" cellpadding="'.$params['table']['cellpadding'].'" border="'.$params['table']['border'].'">';
  1270. $html .= "<tr>";
  1271. $itemsInRow = 0;
  1272. $maxItemsInRow = $params['table']['maxItemsInRow'];
  1273. foreach ($fields as $field) {
  1274. //$html.= !empty($html)?'<br>':'';
  1275. if ($itemsInRow >= $maxItemsInRow) {
  1276. // start a new line
  1277. $html .= "</tr><tr>";
  1278. $itemsInRow = 0;
  1279. }
  1280. // for some type we need line break
  1281. if (in_array($field->type, $params['table']['arrayOfLineBreakType'])) {
  1282. if ($itemsInRow > 0) {
  1283. // close table row and empty cols
  1284. for ($i = $itemsInRow; $i <= $maxItemsInRow; $i++) {
  1285. $html .= "<td></td><td></td>";
  1286. }
  1287. $html .= "</tr>";
  1288. // start a new line
  1289. $html .= "<tr>";
  1290. }
  1291. $itemsInRow = $maxItemsInRow;
  1292. $html .= '<td colspan="'.($maxItemsInRow * 2 - 1).'">';
  1293. $html .= '<strong>'.$field->label.' :</strong> ';
  1294. $html .= $field->content;
  1295. $html .= "</td>";
  1296. } else {
  1297. $itemsInRow++;
  1298. $html .= '<td width="'.$params['table']['labelcolwidth'].'" class="extrafield-label">';
  1299. $html .= '<strong>'.$field->label.' :</strong>';
  1300. $html .= "</td>";
  1301. $html .= '<td class="extrafield-content">';
  1302. $html .= $field->content;
  1303. $html .= "</td>";
  1304. }
  1305. }
  1306. $html .= "</tr>";
  1307. $html .= '</table>';
  1308. }
  1309. }
  1310. return $html;
  1311. }
  1312. /**
  1313. * get column status from column key
  1314. *
  1315. * @param string $colKey the column key
  1316. * @return float width in mm
  1317. */
  1318. public function getColumnStatus($colKey)
  1319. {
  1320. if (!empty($this->cols[$colKey]['status'])) {
  1321. return true;
  1322. } else {
  1323. return false;
  1324. }
  1325. }
  1326. /**
  1327. * Print standard column content
  1328. *
  1329. * @param TCPDI $pdf Pdf object
  1330. * @param float $tab_top Tab top position
  1331. * @param float $tab_height Default tab height
  1332. * @param Translate $outputlangs Output language
  1333. * @param int $hidetop Hide top
  1334. * @return float Height of col tab titles
  1335. */
  1336. public function pdfTabTitles(&$pdf, $tab_top, $tab_height, $outputlangs, $hidetop = 0)
  1337. {
  1338. global $hookmanager, $conf;
  1339. foreach ($this->cols as $colKey => $colDef) {
  1340. $parameters = array(
  1341. 'colKey' => $colKey,
  1342. 'pdf' => $pdf,
  1343. 'outputlangs' => $outputlangs,
  1344. 'tab_top' => $tab_top,
  1345. 'tab_height' => $tab_height,
  1346. 'hidetop' => $hidetop
  1347. );
  1348. $reshook = $hookmanager->executeHooks('pdfTabTitles', $parameters, $this); // Note that $object may have been modified by hook
  1349. if ($reshook < 0) {
  1350. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1351. } elseif (empty($reshook)) {
  1352. if (!$this->getColumnStatus($colKey)) {
  1353. continue;
  1354. }
  1355. // get title label
  1356. $colDef['title']['label'] = !empty($colDef['title']['label']) ? $colDef['title']['label'] : $outputlangs->transnoentities($colDef['title']['textkey']);
  1357. // Add column separator
  1358. if (!empty($colDef['border-left'])) {
  1359. $pdf->line($colDef['xStartPos'], $tab_top, $colDef['xStartPos'], $tab_top + $tab_height);
  1360. }
  1361. if (empty($hidetop)) {
  1362. // save curent cell padding
  1363. $curentCellPaddinds = $pdf->getCellPaddings();
  1364. // Add space for lines (more if we need to show a second alternative language)
  1365. global $outputlangsbis;
  1366. if (is_object($outputlangsbis)) {
  1367. // set cell padding with column title definition
  1368. $pdf->setCellPaddings($colDef['title']['padding'][3], $colDef['title']['padding'][0], $colDef['title']['padding'][1], 0.5);
  1369. } else {
  1370. // set cell padding with column title definition
  1371. $pdf->setCellPaddings($colDef['title']['padding'][3], $colDef['title']['padding'][0], $colDef['title']['padding'][1], $colDef['title']['padding'][2]);
  1372. }
  1373. if (isset($colDef['title']['align'])) {
  1374. $align = $colDef['title']['align'];
  1375. } else {
  1376. $align = '';
  1377. }
  1378. $pdf->SetXY($colDef['xStartPos'], $tab_top);
  1379. $textWidth = $colDef['width'];
  1380. $pdf->MultiCell($textWidth, 2, $colDef['title']['label'], '', $align);
  1381. // Add variant of translation if $outputlangsbis is an object
  1382. if (is_object($outputlangsbis) && trim($colDef['title']['label'])) {
  1383. $pdf->setCellPaddings($colDef['title']['padding'][3], 0, $colDef['title']['padding'][1], $colDef['title']['padding'][2]);
  1384. $pdf->SetXY($colDef['xStartPos'], $pdf->GetY());
  1385. $textbis = $outputlangsbis->transnoentities($colDef['title']['textkey']);
  1386. $pdf->MultiCell($textWidth, 2, $textbis, '', $align);
  1387. }
  1388. $this->tabTitleHeight = max($pdf->GetY() - $tab_top, $this->tabTitleHeight);
  1389. // restore cell padding
  1390. $pdf->setCellPaddings($curentCellPaddinds['L'], $curentCellPaddinds['T'], $curentCellPaddinds['R'], $curentCellPaddinds['B']);
  1391. }
  1392. }
  1393. }
  1394. return $this->tabTitleHeight;
  1395. }
  1396. /**
  1397. * Define Array Column Field for extrafields
  1398. *
  1399. * @param object $object common object det
  1400. * @param Translate $outputlangs langs
  1401. * @param int $hidedetails Do not show line details
  1402. * @return null
  1403. */
  1404. public function defineColumnExtrafield($object, $outputlangs, $hidedetails = 0)
  1405. {
  1406. global $conf;
  1407. if (!empty($hidedetails)) {
  1408. return;
  1409. }
  1410. if (empty($object->table_element)) {
  1411. return;
  1412. }
  1413. // Load extrafiels if not allready does
  1414. if (empty($this->extrafieldsCache)) {
  1415. $this->extrafieldsCache = new ExtraFields($this->db);
  1416. }
  1417. if (empty($this->extrafieldsCache->attributes[$object->table_element])) {
  1418. $this->extrafieldsCache->fetch_name_optionals_label($object->table_element);
  1419. }
  1420. $extrafields = $this->extrafieldsCache;
  1421. if (!empty($extrafields->attributes[$object->table_element]) && is_array($extrafields->attributes[$object->table_element]) && array_key_exists('label', $extrafields->attributes[$object->table_element]) && is_array($extrafields->attributes[$object->table_element]['label'])) {
  1422. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) {
  1423. // Dont display separator yet even is set to be displayed (not compatible yet)
  1424. if ($extrafields->attributes[$object->table_element]['type'][$key] == 'separate') {
  1425. continue;
  1426. }
  1427. // Enable extrafield ?
  1428. $enabled = 0;
  1429. if (!empty($extrafields->attributes[$object->table_element]['printable'][$key])) {
  1430. $printable = intval($extrafields->attributes[$object->table_element]['printable'][$key]);
  1431. if ($printable === 1 || $printable === 2) {
  1432. $enabled = 1;
  1433. }
  1434. // Note : if $printable === 3 or 4 so, it's displayed after line description not in cols
  1435. }
  1436. if (!$enabled) {
  1437. continue;
  1438. } // don't wast resourses if we don't need them...
  1439. // Load language if required
  1440. if (!empty($extrafields->attributes[$object->table_element]['langfile'][$key])) {
  1441. $outputlangs->load($extrafields->attributes[$object->table_element]['langfile'][$key]);
  1442. }
  1443. // TODO : add more extrafield customisation capacities for PDF like width, rank...
  1444. // set column definition
  1445. $def = array(
  1446. 'rank' => intval($extrafields->attributes[$object->table_element]['pos'][$key]),
  1447. 'width' => 25, // in mm
  1448. 'status' => boolval($enabled),
  1449. 'title' => array(
  1450. 'label' => $outputlangs->transnoentities($label)
  1451. ),
  1452. 'content' => array(
  1453. 'align' => 'C'
  1454. ),
  1455. 'border-left' => true, // add left line separator
  1456. );
  1457. $alignTypeRight = array('double', 'int', 'price');
  1458. if (in_array($extrafields->attributes[$object->table_element]['type'][$key], $alignTypeRight)) {
  1459. $def['content']['align'] = 'R';
  1460. }
  1461. $alignTypeLeft = array('text', 'html');
  1462. if (in_array($extrafields->attributes[$object->table_element]['type'][$key], $alignTypeLeft)) {
  1463. $def['content']['align'] = 'L';
  1464. }
  1465. // for extrafields we use rank of extrafield to place it on PDF
  1466. $this->insertNewColumnDef("options_".$key, $def);
  1467. }
  1468. }
  1469. }
  1470. }