commondocgenerator.class.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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@capnetworks.com>
  6. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. * or see http://www.gnu.org/
  22. */
  23. /**
  24. * \file htdocs/core/class/commondocgenerator.class.php
  25. * \ingroup core
  26. * \brief File of parent class for documents generators
  27. */
  28. /**
  29. * Parent class for documents generators
  30. */
  31. abstract class CommonDocGenerator
  32. {
  33. var $error='';
  34. protected $db;
  35. /**
  36. * Constructor
  37. *
  38. * @param DoliDB $db Database handler
  39. */
  40. public function __construct($db) {
  41. $this->db = $db;
  42. }
  43. /**
  44. * Define array with couple subtitution key => subtitution value
  45. *
  46. * @param User $user User
  47. * @param Translate $outputlangs Language object for output
  48. * @return array Array of substitution key->code
  49. */
  50. function get_substitutionarray_user($user,$outputlangs)
  51. {
  52. global $conf;
  53. $logotouse=$conf->user->dir_output.'/'.get_exdir($user->id, 2, 0, 1, $user, 'user').'/'.$user->photo;
  54. return array(
  55. 'myuser_lastname'=>$user->lastname,
  56. 'myuser_firstname'=>$user->firstname,
  57. 'myuser_fullname'=>$user->getFullName($outputlangs,1),
  58. 'myuser_login'=>$user->login,
  59. 'myuser_phone'=>$user->office_phone,
  60. 'myuser_address'=>$user->address,
  61. 'myuser_zip'=>$user->zip,
  62. 'myuser_town'=>$user->town,
  63. 'myuser_country'=>$user->country,
  64. 'myuser_country_code'=>$user->country_code,
  65. 'myuser_state'=>$user->state,
  66. 'myuser_state_code'=>$user->state_code,
  67. 'myuser_fax'=>$user->office_fax,
  68. 'myuser_mobile'=>$user->user_mobile,
  69. 'myuser_email'=>$user->email,
  70. 'myuser_logo'=>$logotouse,
  71. 'myuser_job'=>$user->job,
  72. 'myuser_web'=>'' // url not exist in $user object
  73. );
  74. }
  75. /**
  76. * Define array with couple subtitution key => subtitution value
  77. *
  78. * @param Societe $mysoc Object thirdparty
  79. * @param Translate $outputlangs Language object for output
  80. * @return array Array of substitution key->code
  81. */
  82. function get_substitutionarray_mysoc($mysoc,$outputlangs)
  83. {
  84. global $conf;
  85. if (empty($mysoc->forme_juridique) && ! empty($mysoc->forme_juridique_code))
  86. {
  87. $mysoc->forme_juridique=getFormeJuridiqueLabel($mysoc->forme_juridique_code);
  88. }
  89. if (empty($mysoc->country) && ! empty($mysoc->country_code))
  90. {
  91. $mysoc->country=$outputlangs->transnoentitiesnoconv("Country".$mysoc->country_code);
  92. }
  93. if (empty($mysoc->state) && ! empty($mysoc->state_code))
  94. {
  95. $mysoc->state=getState($mysoc->state_code,0);
  96. }
  97. $logotouse=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  98. return array(
  99. 'mycompany_logo'=>$logotouse,
  100. 'mycompany_name'=>$mysoc->name,
  101. 'mycompany_email'=>$mysoc->email,
  102. 'mycompany_phone'=>$mysoc->phone,
  103. 'mycompany_fax'=>$mysoc->fax,
  104. 'mycompany_address'=>$mysoc->address,
  105. 'mycompany_zip'=>$mysoc->zip,
  106. 'mycompany_town'=>$mysoc->town,
  107. 'mycompany_country'=>$mysoc->country,
  108. 'mycompany_country_code'=>$mysoc->country_code,
  109. 'mycompany_state'=>$mysoc->state,
  110. 'mycompany_state_code'=>$mysoc->state_code,
  111. 'mycompany_web'=>$mysoc->url,
  112. 'mycompany_juridicalstatus'=>$mysoc->forme_juridique,
  113. 'mycompany_managers'=>$mysoc->managers,
  114. 'mycompany_capital'=>$mysoc->capital,
  115. 'mycompany_barcode'=>$mysoc->barcode,
  116. 'mycompany_idprof1'=>$mysoc->idprof1,
  117. 'mycompany_idprof2'=>$mysoc->idprof2,
  118. 'mycompany_idprof3'=>$mysoc->idprof3,
  119. 'mycompany_idprof4'=>$mysoc->idprof4,
  120. 'mycompany_idprof5'=>$mysoc->idprof5,
  121. 'mycompany_idprof6'=>$mysoc->idprof6,
  122. 'mycompany_vatnumber'=>$mysoc->tva_intra,
  123. 'mycompany_object'=>$mysoc->object,
  124. 'mycompany_note_private'=>$mysoc->note_private,
  125. //'mycompany_note_public'=>$mysoc->note_public, // Only private not exists for "mysoc" but both for thirdparties
  126. );
  127. }
  128. /**
  129. * Define array with couple subtitution key => subtitution value
  130. *
  131. * @param Object $object Object
  132. * @param Translate $outputlangs Language object for output
  133. * @return array Array of substitution key->code
  134. */
  135. function get_substitutionarray_thirdparty($object,$outputlangs)
  136. {
  137. global $conf;
  138. if (empty($object->country) && ! empty($object->country_code))
  139. {
  140. $object->country=$outputlangs->transnoentitiesnoconv("Country".$object->country_code);
  141. }
  142. if (empty($object->state) && ! empty($object->state_code))
  143. {
  144. $object->state=getState($object->state_code,0);
  145. }
  146. $array_thirdparty = array(
  147. 'company_name'=>$object->name,
  148. 'company_name_alias' => $object->name_alias,
  149. 'company_email'=>$object->email,
  150. 'company_phone'=>$object->phone,
  151. 'company_fax'=>$object->fax,
  152. 'company_address'=>$object->address,
  153. 'company_zip'=>$object->zip,
  154. 'company_town'=>$object->town,
  155. 'company_country'=>$object->country,
  156. 'company_country_code'=>$object->country_code,
  157. 'company_state'=>$object->state,
  158. 'company_state_code'=>$object->state_code,
  159. 'company_web'=>$object->url,
  160. 'company_barcode'=>$object->barcode,
  161. 'company_vatnumber'=>$object->tva_intra,
  162. 'company_customercode'=>$object->code_client,
  163. 'company_suppliercode'=>$object->code_fournisseur,
  164. 'company_customeraccountancycode'=>$object->code_compta,
  165. 'company_supplieraccountancycode'=>$object->code_compta_fournisseur,
  166. 'company_juridicalstatus'=>$object->forme_juridique,
  167. 'company_outstanding_limit'=>$object->outstanding_limit,
  168. 'company_capital'=>$object->capital,
  169. 'company_idprof1'=>$object->idprof1,
  170. 'company_idprof2'=>$object->idprof2,
  171. 'company_idprof3'=>$object->idprof3,
  172. 'company_idprof4'=>$object->idprof4,
  173. 'company_idprof5'=>$object->idprof5,
  174. 'company_idprof6'=>$object->idprof6,
  175. 'company_note_public'=>$object->note_public,
  176. 'company_note_private'=>$object->note_private,
  177. 'company_default_bank_iban'=>$object->bank_account->iban,
  178. 'company_default_bank_bic'=>$object->bank_account->bic
  179. );
  180. // Retrieve extrafields
  181. if(is_array($object->array_options) && count($object->array_options))
  182. {
  183. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  184. $extrafields = new ExtraFields($this->db);
  185. $extralabels = $extrafields->fetch_name_optionals_label('societe',true);
  186. $object->fetch_optionals();
  187. foreach($extrafields->attribute_label as $key=>$label)
  188. {
  189. if($extrafields->attribute_type[$key] == 'price')
  190. {
  191. $object->array_options['options_'.$key] = price($object->array_options['options_'.$key],0,$outputlangs,0,0,-1,$conf->currency);
  192. }
  193. else if($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
  194. {
  195. $object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
  196. }
  197. $array_thirdparty = array_merge($array_thirdparty, array ('company_options_'.$key => $object->array_options ['options_' . $key]));
  198. }
  199. }
  200. return $array_thirdparty;
  201. }
  202. /**
  203. * Define array with couple subtitution key => subtitution value
  204. *
  205. * @param Contact $object contact
  206. * @param Translate $outputlangs object for output
  207. * @param array_key $array_key Name of the key for return array
  208. * @return array of substitution key->code
  209. */
  210. function get_substitutionarray_contact($object, $outputlangs, $array_key = 'object') {
  211. global $conf;
  212. if(empty($object->country) && ! empty($object->country_code))
  213. {
  214. $object->country = $outputlangs->transnoentitiesnoconv("Country" . $object->country_code);
  215. }
  216. if(empty($object->state) && ! empty($object->state_code))
  217. {
  218. $object->state = getState($object->state_code, 0);
  219. }
  220. $array_contact = array (
  221. $array_key . '_fullname' => $object->getFullName($outputlangs, 1),
  222. $array_key . '_lastname' => $object->lastname,
  223. $array_key . '_firstname' => $object->firstname,
  224. $array_key . '_address' => $object->address,
  225. $array_key . '_zip' => $object->zip,
  226. $array_key . '_town' => $object->town,
  227. $array_key . '_state_id' => $object->state_id,
  228. $array_key . '_state_code' => $object->state_code,
  229. $array_key . '_state' => $object->state,
  230. $array_key . '_country_id' => $object->country_id,
  231. $array_key . '_country_code' => $object->country_code,
  232. $array_key . '_country' => $object->country,
  233. $array_key . '_poste' => $object->poste,
  234. $array_key . '_socid' => $object->socid,
  235. $array_key . '_statut' => $object->statut,
  236. $array_key . '_code' => $object->code,
  237. $array_key . '_email' => $object->email,
  238. $array_key . '_jabberid' => $object->jabberid,
  239. $array_key . '_phone_pro' => $object->phone_pro,
  240. $array_key . '_phone_perso' => $object->phone_perso,
  241. $array_key . '_phone_mobile' => $object->phone_mobile,
  242. $array_key . '_fax' => $object->fax,
  243. $array_key . '_birthday' => $object->birthday,
  244. $array_key . '_default_lang' => $object->default_lang,
  245. $array_key . '_note_public' => $object->note_public,
  246. $array_key . '_note_private' => $object->note_private
  247. );
  248. // Retrieve extrafields
  249. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  250. $extrafields = new ExtraFields($this->db);
  251. $extralabels = $extrafields->fetch_name_optionals_label('socpeople', true);
  252. $object->fetch_optionals();
  253. foreach($extrafields->attribute_label as $key => $label)
  254. {
  255. if ($extrafields->attribute_type[$key] == 'price')
  256. {
  257. $object->array_options['options_' . $key] = price($object->array_options ['options_' . $key], 0, $outputlangs, 0, 0, - 1, $conf->currency);
  258. }
  259. elseif($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
  260. {
  261. $object->array_options['options_' . $key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_' . $key]];
  262. }
  263. $array_contact = array_merge($array_contact, array($array_key.'_options_' . $key => $object->array_options['options_'. $key]));
  264. }
  265. return $array_contact;
  266. }
  267. /**
  268. * Define array with couple subtitution key => subtitution value
  269. *
  270. * @param Translate $outputlangs Language object for output
  271. * @return array Array of substitution key->code
  272. */
  273. function get_substitutionarray_other($outputlangs)
  274. {
  275. global $conf;
  276. $now=dol_now('gmt'); // gmt
  277. $array_other = array(
  278. // Date in default language
  279. 'current_date'=>dol_print_date($now,'day','tzuser'),
  280. 'current_datehour'=>dol_print_date($now,'dayhour','tzuser'),
  281. 'current_server_date'=>dol_print_date($now,'day','tzserver'),
  282. 'current_server_datehour'=>dol_print_date($now,'dayhour','tzserver'),
  283. // Date in requested output language
  284. 'current_date_locale'=>dol_print_date($now,'day','tzuser',$outputlangs),
  285. 'current_datehour_locale'=>dol_print_date($now,'dayhour','tzuser',$outputlangs),
  286. 'current_server_date_locale'=>dol_print_date($now,'day','tzserver',$outputlangs),
  287. 'current_server_datehour_locale'=>dol_print_date($now,'dayhour','tzserver',$outputlangs),
  288. );
  289. foreach($conf->global as $key => $val)
  290. {
  291. if (preg_match('/(_pass|password|secret|_key|key$)/i', $key)) $newval = '*****forbidden*****';
  292. else $newval = $val;
  293. $array_other['__['.$key.']__'] = $newval;
  294. }
  295. return $array_other;
  296. }
  297. /**
  298. * Define array with couple substitution key => substitution value
  299. *
  300. * @param Object $object Main object to use as data source
  301. * @param Translate $outputlangs Lang object to use for output
  302. * @param string $array_key Name of the key for return array
  303. * @return array Array of substitution
  304. */
  305. function get_substitutionarray_object($object,$outputlangs,$array_key='object')
  306. {
  307. global $conf;
  308. $sumpayed=$sumdeposit=$sumcreditnote='';
  309. if ($object->element == 'facture')
  310. {
  311. $invoice_source=new Facture($this->db);
  312. if ($object->fk_facture_source > 0)
  313. {
  314. $invoice_source->fetch($object->fk_facture_source);
  315. }
  316. $sumpayed = $object->getSommePaiement();
  317. $sumdeposit = $object->getSumDepositsUsed();
  318. $sumcreditnote = $object->getSumCreditNotesUsed();
  319. }
  320. $date = ($object->element == 'contrat' ? $object->date_contrat : $object->date);
  321. $resarray=array(
  322. $array_key.'_id'=>$object->id,
  323. $array_key.'_ref'=>$object->ref,
  324. $array_key.'_ref_ext'=>$object->ref_ext,
  325. $array_key.'_ref_customer'=>(! empty($object->ref_client) ? $object->ref_client : (empty($object->ref_customer) ? '' : $object->ref_customer)),
  326. $array_key.'_ref_supplier'=>(! empty($object->ref_fournisseur) ? $object->ref_fournisseur : (empty($object->ref_supplier) ? '' : $object->ref_supplier)),
  327. $array_key.'_source_invoice_ref'=>$invoice_source->ref,
  328. // Dates
  329. $array_key.'_hour'=>dol_print_date($date,'hour'),
  330. $array_key.'_date'=>dol_print_date($date,'day'),
  331. $array_key.'_date_rfc'=>dol_print_date($date,'dayrfc'),
  332. $array_key.'_date_limit'=>(! empty($object->date_lim_reglement)?dol_print_date($object->date_lim_reglement,'day'):''),
  333. $array_key.'_date_end'=>(! empty($object->fin_validite)?dol_print_date($object->fin_validite,'day'):''),
  334. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  335. $array_key.'_date_modification'=>(! empty($object->date_modification)?dol_print_date($object->date_modification,'day'):''),
  336. $array_key.'_date_validation'=>(! empty($object->date_validation)?dol_print_date($object->date_validation,'dayhour'):''),
  337. $array_key.'_date_delivery_planed'=>(! empty($object->date_livraison)?dol_print_date($object->date_livraison,'day'):''),
  338. $array_key.'_date_close'=>(! empty($object->date_cloture)?dol_print_date($object->date_cloture,'dayhour'):''),
  339. $array_key.'_payment_mode_code'=>$object->mode_reglement_code,
  340. $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),
  341. $array_key.'_payment_term_code'=>$object->cond_reglement_code,
  342. $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)),
  343. $array_key.'_total_ht_locale'=>price($object->total_ht, 0, $outputlangs),
  344. $array_key.'_total_vat_locale'=>(! empty($object->total_vat)?price($object->total_vat, 0, $outputlangs):price($object->total_tva, 0, $outputlangs)),
  345. $array_key.'_total_localtax1_locale'=>price($object->total_localtax1, 0, $outputlangs),
  346. $array_key.'_total_localtax2_locale'=>price($object->total_localtax2, 0, $outputlangs),
  347. $array_key.'_total_ttc_locale'=>price($object->total_ttc, 0, $outputlangs),
  348. $array_key.'_total_ht'=>price2num($object->total_ht),
  349. $array_key.'_total_vat'=>(! empty($object->total_vat)?price2num($object->total_vat):price2num($object->total_tva)),
  350. $array_key.'_total_localtax1'=>price2num($object->total_localtax1),
  351. $array_key.'_total_localtax2'=>price2num($object->total_localtax2),
  352. $array_key.'_total_ttc'=>price2num($object->total_ttc),
  353. $array_key.'_multicurrency_code' => price2num($object->multicurrency_code),
  354. $array_key.'_multicurrency_tx' => price2num($object->multicurrency_tx),
  355. $array_key.'_multicurrency_total_ht' => price2num($object->multicurrency_total_ht),
  356. $array_key.'_multicurrency_total_tva' => price2num($object->multicurrency_total_tva),
  357. $array_key.'_multicurrency_total_ttc' => price2num($object->multicurrency_total_ttc),
  358. $array_key.'_multicurrency_total_ht_locale' => price($object->multicurrency_total_ht, 0, $outputlangs),
  359. $array_key.'_multicurrency_total_tva_locale' => price($object->multicurrency_total_tva, 0, $outputlangs),
  360. $array_key.'_multicurrency_total_ttc_locale' => price($object->multicurrency_total_ttc, 0, $outputlangs),
  361. $array_key.'_note_private'=>$object->note,
  362. $array_key.'_note_public'=>$object->note_public,
  363. $array_key.'_note'=>$object->note_public, // For backward compatibility
  364. // Payments
  365. $array_key.'_already_payed_locale'=>price($sumpayed, 0, $outputlangs),
  366. $array_key.'_already_payed'=>price2num($sumpayed),
  367. $array_key.'_already_deposit_locale'=>price($sumdeposit, 0, $outputlangs),
  368. $array_key.'_already_deposit'=>price2num($sumdeposit),
  369. $array_key.'_already_creditnote_locale'=>price($sumcreditnote, 0, $outputlangs),
  370. $array_key.'_already_creditnote'=>price2num($sumcreditnote),
  371. $array_key.'_already_payed_all_locale'=>price(price2num($sumpayed + $sumdeposit + $sumcreditnote, 'MT'), 0, $outputlangs),
  372. $array_key.'_already_payed_all'=> price2num(($sumpayed + $sumdeposit + $sumcreditnote), 'MT'),
  373. // Remain to pay with all know infrmation (except open direct debit requests)
  374. $array_key.'_remain_to_pay_locale'=>price(price2num($object->total_ttc - $sumpayed - $sumdeposit - $sumcreditnote, 'MT'), 0, $outputlangs),
  375. $array_key.'_remain_to_pay'=>price2num($object->total_ttc - $sumpayed - $sumdeposit - $sumcreditnote, 'MT')
  376. );
  377. if (method_exists($object, 'getTotalDiscount')) {
  378. $resarray[$array_key.'_total_discount_ht_locale'] = price($object->getTotalDiscount(), 0, $outputlangs);
  379. $resarray[$array_key.'_total_discount_ht'] = price2num($object->getTotalDiscount());
  380. } else {
  381. $resarray[$array_key.'_total_discount_ht_locale'] = '';
  382. $resarray[$array_key.'_total_discount_ht'] = '';
  383. }
  384. // Fetch project information if there is a project assigned to this object
  385. if ($object->element != "project" && ! empty($object->fk_project) && $object->fk_project > 0)
  386. {
  387. if (! is_object($object->project))
  388. {
  389. $object->fetch_projet();
  390. }
  391. $resarray[$array_key.'_project_ref'] = $object->project->ref;
  392. $resarray[$array_key.'_project_title'] = $object->project->title;
  393. $resarray[$array_key.'_project_description'] = $object->project->description;
  394. $resarray[$array_key.'_project_date_start'] = dol_print_date($object->project->date_start, 'day');
  395. $resarray[$array_key.'_project_date_end'] = dol_print_date($object->project->date_end, 'day');
  396. }
  397. // Add vat by rates
  398. if (is_array($object->lines) && count($object->lines)>0)
  399. {
  400. foreach ($object->lines as $line)
  401. {
  402. // $line->tva_tx format depends on database field accuraty, no reliable. This is kept for backward comaptibility
  403. if (empty($resarray[$array_key.'_total_vat_'.$line->tva_tx])) $resarray[$array_key.'_total_vat_'.$line->tva_tx]=0;
  404. $resarray[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  405. $resarray[$array_key.'_total_vat_locale_'.$line->tva_tx]=price($resarray[$array_key.'_total_vat_'.$line->tva_tx]);
  406. // $vatformated is vat without not expected chars (so 20, or 8.5 or 5.99 for example)
  407. $vatformated=vatrate($line->tva_tx);
  408. if (empty($resarray[$array_key.'_total_vat_'.$vatformated])) $resarray[$array_key.'_total_vat_'.$vatformated]=0;
  409. $resarray[$array_key.'_total_vat_'.$vatformated]+=$line->total_tva;
  410. $resarray[$array_key.'_total_vat_locale_'.$vatformated]=price($resarray[$array_key.'_total_vat_'.$vatformated]);
  411. }
  412. }
  413. // Retrieve extrafields
  414. if (is_array($object->array_options) && count($object->array_options))
  415. {
  416. $extrafieldkey=$object->element;
  417. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  418. $extrafields = new ExtraFields($this->db);
  419. $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
  420. $object->fetch_optionals();
  421. $resarray = $this->fill_substitutionarray_with_extrafields($object,$resarray,$extrafields,$array_key,$outputlangs);
  422. }
  423. return $resarray;
  424. }
  425. /**
  426. * Define array with couple substitution key => substitution value
  427. *
  428. * @param array $line Array of lines
  429. * @param Translate $outputlangs Lang object to use for output
  430. * @return array Return a substitution array
  431. */
  432. function get_substitutionarray_lines($line,$outputlangs)
  433. {
  434. global $conf;
  435. $resarray= array(
  436. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  437. 'line_product_ref'=>$line->product_ref,
  438. 'line_product_label'=>$line->product_label,
  439. 'line_product_type'=>$line->product_type,
  440. 'line_desc'=>$line->desc,
  441. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  442. 'line_up'=>price2num($line->subprice),
  443. 'line_up_locale'=>price($line->subprice, 0, $outputlangs),
  444. 'line_qty'=>$line->qty,
  445. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  446. 'line_price_ht'=>price2num($line->total_ht),
  447. 'line_price_ttc'=>price2num($line->total_ttc),
  448. 'line_price_vat'=>price2num($line->total_tva),
  449. 'line_price_ht_locale'=>price($line->total_ht, 0, $outputlangs),
  450. 'line_price_ttc_locale'=>price($line->total_ttc, 0, $outputlangs),
  451. 'line_price_vat_locale'=>price($line->total_tva, 0, $outputlangs),
  452. // Dates
  453. 'line_date_start'=>dol_print_date($line->date_start, 'day', 'tzuser'),
  454. 'line_date_start_locale'=>dol_print_date($line->date_start, 'day', 'tzuser', $outputlangs),
  455. 'line_date_start_rfc'=>dol_print_date($line->date_start, 'dayrfc', 'tzuser'),
  456. 'line_date_end'=>dol_print_date($line->date_end, 'day', 'tzuser'),
  457. 'line_date_end_locale'=>dol_print_date($line->date_end, 'day', 'tzuser', $outputlangs),
  458. 'line_date_end_rfc'=>dol_print_date($line->date_end, 'dayrfc', 'tzuser'),
  459. 'line_multicurrency_code' => price2num($line->multicurrency_code),
  460. 'line_multicurrency_subprice' => price2num($line->multicurrency_subprice),
  461. 'line_multicurrency_total_ht' => price2num($line->multicurrency_total_ht),
  462. 'line_multicurrency_total_tva' => price2num($line->multicurrency_total_tva),
  463. 'line_multicurrency_total_ttc' => price2num($line->multicurrency_total_ttc),
  464. 'line_multicurrency_subprice_locale' => price($line->multicurrency_subprice, 0, $outputlangs),
  465. 'line_multicurrency_total_ht_locale' => price($line->multicurrency_total_ht, 0, $outputlangs),
  466. 'line_multicurrency_total_tva_locale' => price($line->multicurrency_total_tva, 0, $outputlangs),
  467. 'line_multicurrency_total_ttc_locale' => price($line->multicurrency_total_ttc, 0, $outputlangs),
  468. );
  469. // Units
  470. if ($conf->global->PRODUCT_USE_UNITS)
  471. {
  472. $resarray['line_unit']=$outputlangs->trans($line->getLabelOfUnit('long'));
  473. $resarray['line_unit_short']=$outputlangs->trans($line->getLabelOfUnit('short'));
  474. }
  475. // Retrieve extrafields
  476. $extrafieldkey=$line->element;
  477. $array_key="line";
  478. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  479. $extrafields = new ExtraFields($this->db);
  480. $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
  481. $line->fetch_optionals();
  482. $resarray = $this->fill_substitutionarray_with_extrafields($line,$resarray,$extrafields,$array_key=$array_key,$outputlangs);
  483. // Load product data optional fields to the line -> enables to use "line_options_{extrafield}"
  484. if (isset($line->fk_product) && $line->fk_product > 0)
  485. {
  486. $tmpproduct = new Product($this->db);
  487. $result = $tmpproduct->fetch($line->fk_product);
  488. foreach($tmpproduct->array_options as $key=>$label)
  489. $resarray["line_".$key] = $label;
  490. }
  491. return $resarray;
  492. }
  493. /**
  494. * Define array with couple substitution key => substitution value
  495. *
  496. * @param Expedition $object Main object to use as data source
  497. * @param Translate $outputlangs Lang object to use for output
  498. * @param array_key $array_key Name of the key for return array
  499. * @return array Array of substitution
  500. */
  501. function get_substitutionarray_shipment($object,$outputlangs,$array_key='object')
  502. {
  503. global $conf;
  504. dol_include_once('/core/lib/product.lib.php');
  505. $object->list_delivery_methods($object->shipping_method_id);
  506. $calculatedVolume=($object->trueWidth * $object->trueHeight * $object->trueDepth);
  507. $array_shipment=array(
  508. $array_key.'_id'=>$object->id,
  509. $array_key.'_ref'=>$object->ref,
  510. $array_key.'_ref_ext'=>$object->ref_ext,
  511. $array_key.'_ref_customer'=>$object->ref_customer,
  512. $array_key.'_date_delivery'=>dol_print_date($object->date_delivery,'day'),
  513. $array_key.'_hour_delivery'=>dol_print_date($object->date_delivery,'hour'),
  514. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  515. $array_key.'_total_ht'=>price($object->total_ht),
  516. $array_key.'_total_vat'=>price($object->total_tva),
  517. $array_key.'_total_ttc'=>price($object->total_ttc),
  518. $array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
  519. $array_key.'_note_private'=>$object->note_private,
  520. $array_key.'_note'=>$object->note_public,
  521. $array_key.'_tracking_number'=>$object->tracking_number,
  522. $array_key.'_tracking_url'=>$object->tracking_url,
  523. $array_key.'_shipping_method'=>$object->listmeths[0]['libelle'],
  524. $array_key.'_weight'=>$object->trueWeight.' '.measuring_units_string($object->weight_units, 'weight'),
  525. $array_key.'_width'=>$object->trueWidth.' '.measuring_units_string($object->width_units, 'size'),
  526. $array_key.'_height'=>$object->trueHeight.' '.measuring_units_string($object->height_units, 'size'),
  527. $array_key.'_depth'=>$object->trueDepth.' '.measuring_units_string($object->depth_units, 'size'),
  528. $array_key.'_size'=>$calculatedVolume.' '.measuring_units_string(0, 'volume'),
  529. );
  530. // Add vat by rates
  531. foreach ($object->lines as $line)
  532. {
  533. if (empty($array_shipment[$array_key.'_total_vat_'.$line->tva_tx])) $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]=0;
  534. $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  535. }
  536. // Retrieve extrafields
  537. /*if(is_array($object->array_options) && count($object->array_options))
  538. {
  539. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  540. $extrafields = new ExtraFields($this->db);
  541. $extralabels = $extrafields->fetch_name_optionals_label('shipment',true);
  542. $object->fetch_optionals();
  543. $array_shipment = $this->fill_substitutionarray_with_extrafields($object,$array_shipment,$extrafields,$array_key,$outputlangs);
  544. }*/
  545. return $array_shipment;
  546. }
  547. /**
  548. * Define array with couple substitution key => substitution value
  549. *
  550. * @param array $line Array of lines
  551. * @param Translate $outputlangs Lang object to use for output
  552. * @return array Substitution array
  553. */
  554. function get_substitutionarray_shipment_lines($line,$outputlangs)
  555. {
  556. global $conf;
  557. dol_include_once('/core/lib/product.lib.php');
  558. return array(
  559. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  560. 'line_product_ref'=>$line->product_ref,
  561. 'line_product_label'=>$line->product_label,
  562. 'line_desc'=>$line->desc,
  563. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  564. 'line_up'=>price($line->subprice),
  565. 'line_qty'=>$line->qty,
  566. 'line_qty_shipped'=>$line->qty_shipped,
  567. 'line_qty_asked'=>$line->qty_asked,
  568. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  569. 'line_price_ht'=>price($line->total_ht),
  570. 'line_price_ttc'=>price($line->total_ttc),
  571. 'line_price_vat'=>price($line->total_tva),
  572. 'line_weight'=>empty($line->weight) ? '' : $line->weight*$line->qty_shipped.' '.measuring_units_string($line->weight_units, 'weight'),
  573. 'line_length'=>empty($line->length) ? '' : $line->length*$line->qty_shipped.' '.measuring_units_string($line->length_units, 'size'),
  574. 'line_surface'=>empty($line->surface) ? '' : $line->surface*$line->qty_shipped.' '.measuring_units_string($line->surface_units, 'surface'),
  575. 'line_volume'=>empty($line->volume) ? '' : $line->volume*$line->qty_shipped.' '.measuring_units_string($line->volume_units, 'volume'),
  576. );
  577. }
  578. /**
  579. * Define array with couple subtitution key => subtitution value
  580. *
  581. * @param Object $object Dolibarr Object
  582. * @param Translate $outputlangs Language object for output
  583. * @param boolean $recursive Want to fetch child array or child object
  584. * @return array Array of substitution key->code
  585. */
  586. function get_substitutionarray_each_var_object(&$object,$outputlangs,$recursive=true) {
  587. $array_other = array();
  588. if(!empty($object)) {
  589. foreach($object as $key => $value) {
  590. if(!empty($value)) {
  591. if(!is_array($value) && !is_object($value)) {
  592. $array_other['object_'.$key] = $value;
  593. }
  594. if(is_array($value) && $recursive){
  595. $array_other['object_'.$key] = $this->get_substitutionarray_each_var_object($value,$outputlangs,false);
  596. }
  597. }
  598. }
  599. }
  600. return $array_other;
  601. }
  602. /**
  603. * Fill array with couple extrafield key => extrafield value
  604. *
  605. * @param Object $object Object with extrafields (must have $object->array_options filled)
  606. * @param array $array_to_fill Substitution array
  607. * @param Extrafields $extrafields Extrafields object
  608. * @param string $array_key Prefix for name of the keys into returned array
  609. * @param Translate $outputlangs Lang object to use for output
  610. * @return array Substitution array
  611. */
  612. function fill_substitutionarray_with_extrafields($object,$array_to_fill,$extrafields,$array_key,$outputlangs)
  613. {
  614. global $conf;
  615. foreach($extrafields->attribute_label as $key=>$label)
  616. {
  617. if($extrafields->attribute_type[$key] == 'price')
  618. {
  619. $object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key]);
  620. $object->array_options['options_'.$key.'_currency'] = price($object->array_options['options_'.$key],0,$outputlangs,0,0,-1,$conf->currency);
  621. //Add value to store price with currency
  622. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
  623. }
  624. else if($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
  625. {
  626. $object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
  627. }
  628. else if($extrafields->attribute_type[$key] == 'date')
  629. {
  630. if (strlen($object->array_options['options_'.$key])>0)
  631. {
  632. $date = $object->array_options['options_'.$key];
  633. $object->array_options['options_'.$key] = dol_print_date($date,'day'); // using company output language
  634. $object->array_options['options_'.$key.'_locale'] = dol_print_date($date,'day','tzserver',$outputlangs); // using output language format
  635. $object->array_options['options_'.$key.'_rfc'] = dol_print_date($date,'dayrfc'); // international format
  636. }
  637. else
  638. {
  639. $object->array_options['options_'.$key] = '';
  640. $object->array_options['options_'.$key.'_locale'] = '';
  641. $object->array_options['options_'.$key.'_rfc'] = '';
  642. }
  643. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  644. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  645. }
  646. else if($extrafields->attribute_type[$key] == 'datetime')
  647. {
  648. $datetime = $object->array_options['options_'.$key];
  649. $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
  650. $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
  651. $object->array_options['options_'.$key.'_rfc'] = ($datetime!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhourrfc'):''); // international format
  652. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  653. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  654. }
  655. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
  656. }
  657. return $array_to_fill;
  658. }
  659. /**
  660. * Rect pdf
  661. *
  662. * @param PDF $pdf Object PDF
  663. * @param float $x Abscissa of first point
  664. * @param float $y Ordinate of first point
  665. * @param float $l ??
  666. * @param float $h ??
  667. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  668. * @param int $hidebottom Hide bottom
  669. * @return void
  670. */
  671. function printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
  672. {
  673. if (empty($hidetop) || $hidetop==-1) $pdf->line($x, $y, $x+$l, $y);
  674. $pdf->line($x+$l, $y, $x+$l, $y+$h);
  675. if (empty($hidebottom)) $pdf->line($x+$l, $y+$h, $x, $y+$h);
  676. $pdf->line($x, $y+$h, $x, $y);
  677. }
  678. }