commondocgenerator.class.php 66 KB

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