commondocgenerator.class.php 36 KB

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