commondocgenerator.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. return 1;
  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($object->id,$extralabels);
  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. global $conf;
  213. if(empty($object->country) && ! empty($object->country_code))
  214. {
  215. $object->country = $outputlangs->transnoentitiesnoconv("Country" . $object->country_code);
  216. }
  217. if(empty($object->state) && ! empty($object->state_code))
  218. {
  219. $object->state = getState($object->state_code, 0);
  220. }
  221. $array_contact = array (
  222. $array_key . '_fullname' => $object->getFullName($outputlangs, 1),
  223. $array_key . '_lastname' => $object->lastname,
  224. $array_key . '_firstname' => $object->firstname,
  225. $array_key . '_address' => $object->address,
  226. $array_key . '_zip' => $object->zip,
  227. $array_key . '_town' => $object->town,
  228. $array_key . '_state_id' => $object->state_id,
  229. $array_key . '_state_code' => $object->state_code,
  230. $array_key . '_state' => $object->state,
  231. $array_key . '_country_id' => $object->country_id,
  232. $array_key . '_country_code' => $object->country_code,
  233. $array_key . '_country' => $object->country,
  234. $array_key . '_poste' => $object->poste,
  235. $array_key . '_socid' => $object->socid,
  236. $array_key . '_statut' => $object->statut,
  237. $array_key . '_code' => $object->code,
  238. $array_key . '_email' => $object->email,
  239. $array_key . '_jabberid' => $object->jabberid,
  240. $array_key . '_phone_pro' => $object->phone_pro,
  241. $array_key . '_phone_perso' => $object->phone_perso,
  242. $array_key . '_phone_mobile' => $object->phone_mobile,
  243. $array_key . '_fax' => $object->fax,
  244. $array_key . '_birthday' => $object->birthday,
  245. $array_key . '_default_lang' => $object->default_lang,
  246. $array_key . '_note_public' => $object->note_public,
  247. $array_key . '_note_private' => $object->note_private
  248. );
  249. // Retrieve extrafields
  250. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  251. $extrafields = new ExtraFields($this->db);
  252. $extralabels = $extrafields->fetch_name_optionals_label('socpeople', true);
  253. $object->fetch_optionals($object->id, $extralabels);
  254. foreach($extrafields->attribute_label as $key => $label)
  255. {
  256. if ($extrafields->attribute_type[$key] == 'price')
  257. {
  258. $object->array_options['options_' . $key] = price($object->array_options ['options_' . $key], 0, $outputlangs, 0, 0, - 1, $conf->currency);
  259. }
  260. elseif($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
  261. {
  262. $object->array_options['options_' . $key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_' . $key]];
  263. }
  264. $array_contact = array_merge($array_contact, array($array_key.'_options_' . $key => $object->array_options['options_'. $key]));
  265. }
  266. return $array_contact;
  267. }
  268. /**
  269. * Define array with couple subtitution key => subtitution value
  270. *
  271. * @param Translate $outputlangs Language object for output
  272. * @return array Array of substitution key->code
  273. */
  274. function get_substitutionarray_other($outputlangs)
  275. {
  276. global $conf;
  277. $now=dol_now('gmt'); // gmt
  278. $array_other = array(
  279. // Date in default language
  280. 'current_date'=>dol_print_date($now,'day','tzuser'),
  281. 'current_datehour'=>dol_print_date($now,'dayhour','tzuser'),
  282. 'current_server_date'=>dol_print_date($now,'day','tzserver'),
  283. 'current_server_datehour'=>dol_print_date($now,'dayhour','tzserver'),
  284. // Date in requested output language
  285. 'current_date_locale'=>dol_print_date($now,'day','tzuser',$outputlangs),
  286. 'current_datehour_locale'=>dol_print_date($now,'dayhour','tzuser',$outputlangs),
  287. 'current_server_date_locale'=>dol_print_date($now,'day','tzserver',$outputlangs),
  288. 'current_server_datehour_locale'=>dol_print_date($now,'dayhour','tzserver',$outputlangs),
  289. );
  290. return $array_other;
  291. }
  292. /**
  293. * Define array with couple subtitution key => subtitution value
  294. *
  295. * @param Object $object Dolibarr Object
  296. * @param Translate $outputlangs Language object for output
  297. * @param boolean $recursive Want to fetch child array or child object
  298. * @return array Array of substitution key->code
  299. */
  300. function get_substitutionarray_each_var_object(&$object,$outputlangs,$recursive=true) {
  301. $array_other = array();
  302. if(!empty($object)) {
  303. foreach($object as $key => $value) {
  304. if(!empty($value)) {
  305. if(!is_array($value) && !is_object($value)) {
  306. $array_other['object_'.$key] = $value;
  307. }
  308. if(is_array($value) && $recursive){
  309. $array_other['object_'.$key] = $this->get_substitutionarray_each_var_object($value,$outputlangs,false);
  310. }
  311. }
  312. }
  313. }
  314. return $array_other;
  315. }
  316. /**
  317. * Define array with couple substitution key => substitution value
  318. *
  319. * @param Object $object Main object to use as data source
  320. * @param Translate $outputlangs Lang object to use for output
  321. * @param string $array_key Name of the key for return array
  322. * @return array Array of substitution
  323. */
  324. function get_substitutionarray_object($object,$outputlangs,$array_key='object')
  325. {
  326. global $conf;
  327. $sumpayed=$sumdeposit=$sumcreditnote='';
  328. if ($object->element == 'facture')
  329. {
  330. $invoice_source=new Facture($this->db);
  331. if ($object->fk_facture_source > 0)
  332. {
  333. $invoice_source->fetch($object->fk_facture_source);
  334. }
  335. $sumpayed = $object->getSommePaiement();
  336. $sumdeposit = $object->getSumDepositsUsed();
  337. $sumcreditnote = $object->getSumCreditNotesUsed();
  338. }
  339. $resarray=array(
  340. $array_key.'_id'=>$object->id,
  341. $array_key.'_ref'=>$object->ref,
  342. $array_key.'_ref_ext'=>$object->ref_ext,
  343. $array_key.'_ref_customer'=>$object->ref_client,
  344. $array_key.'_ref_supplier'=>(! empty($object->ref_fournisseur)?$object->ref_fournisseur:''),
  345. $array_key.'_source_invoice_ref'=>$invoice_source->ref,
  346. // Dates
  347. $array_key.'_hour'=>dol_print_date($object->date,'hour'),
  348. $array_key.'_date'=>dol_print_date($object->date,'day'),
  349. $array_key.'_date_rfc'=>dol_print_date($object->date,'dayrfc'),
  350. $array_key.'_date_limit'=>(! empty($object->date_lim_reglement)?dol_print_date($object->date_lim_reglement,'day'):''),
  351. $array_key.'_date_end'=>(! empty($object->fin_validite)?dol_print_date($object->fin_validite,'day'):''),
  352. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  353. $array_key.'_date_modification'=>(! empty($object->date_modification)?dol_print_date($object->date_modification,'day'):''),
  354. $array_key.'_date_validation'=>(! empty($object->date_validation)?dol_print_date($object->date_validation,'dayhour'):''),
  355. $array_key.'_date_delivery_planed'=>(! empty($object->date_livraison)?dol_print_date($object->date_livraison,'day'):''),
  356. $array_key.'_date_close'=>(! empty($object->date_cloture)?dol_print_date($object->date_cloture,'dayhour'):''),
  357. $array_key.'_payment_mode_code'=>$object->mode_reglement_code,
  358. $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),
  359. $array_key.'_payment_term_code'=>$object->cond_reglement_code,
  360. $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),
  361. $array_key.'_total_ht_locale'=>price($object->total_ht, 0, $outputlangs),
  362. $array_key.'_total_vat_locale'=>(! empty($object->total_vat)?price($object->total_vat, 0, $outputlangs):price($object->total_tva, 0, $outputlangs)),
  363. $array_key.'_total_localtax1_locale'=>price($object->total_localtax1, 0, $outputlangs),
  364. $array_key.'_total_localtax2_locale'=>price($object->total_localtax2, 0, $outputlangs),
  365. $array_key.'_total_ttc_locale'=>price($object->total_ttc, 0, $outputlangs),
  366. $array_key.'_total_discount_ht_locale' => price($object->getTotalDiscount(), 0, $outputlangs),
  367. $array_key.'_total_ht'=>price2num($object->total_ht),
  368. $array_key.'_total_vat'=>(! empty($object->total_vat)?price2num($object->total_vat):price2num($object->total_tva)),
  369. $array_key.'_total_localtax1'=>price2num($object->total_localtax1),
  370. $array_key.'_total_localtax2'=>price2num($object->total_localtax2),
  371. $array_key.'_total_ttc'=>price2num($object->total_ttc),
  372. $array_key.'_total_discount_ht' => price2num($object->getTotalDiscount()),
  373. $array_key.'_note_private'=>$object->note,
  374. $array_key.'_note_public'=>$object->note_public,
  375. $array_key.'_note'=>$object->note_public, // For backward compatibility
  376. // Payments
  377. $array_key.'_already_payed_locale'=>price($sumpayed, 0, $outputlangs),
  378. $array_key.'_already_payed'=>price2num($sumpayed),
  379. $array_key.'_already_deposit_locale'=>price($sumdeposit, 0, $outputlangs),
  380. $array_key.'_already_deposit'=>price2num($sumdeposit),
  381. $array_key.'_already_creditnote_locale'=>price($sumcreditnote, 0, $outputlangs),
  382. $array_key.'_already_creditnote'=>price2num($sumcreditnote),
  383. $array_key.'_already_payed_all_locale'=>price(price2num($sumpayed + $sumdeposit + $sumcreditnote, 'MT'), 0, $outputlangs),
  384. $array_key.'already_payed_all'=> price2num(($sumpayed + $sumdeposit + $sumcreditnote), 'MT'),
  385. // Remain to pay with all know infrmation (except open direct debit requests)
  386. $array_key.'_remain_to_pay_locale'=>price(price2num($object->total_ttc - $sumpayed - $sumdeposit - $sumcreditnote, 'MT'), 0, $outputlangs),
  387. $array_key.'_remain_to_pay'=>price2num($object->total_ttc - $sumpayed - $sumdeposit - $sumcreditnote, 'MT')
  388. );
  389. // Add vat by rates
  390. foreach ($object->lines as $line)
  391. {
  392. // $line->tva_tx format depends on database field accuraty, no reliable. This is kept for backward comaptibility
  393. if (empty($resarray[$array_key.'_total_vat_'.$line->tva_tx])) $resarray[$array_key.'_total_vat_'.$line->tva_tx]=0;
  394. $resarray[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  395. $resarray[$array_key.'_total_vat_locale_'.$line->tva_tx]=price($resarray[$array_key.'_total_vat_'.$line->tva_tx]);
  396. // $vatformated is vat without not expected chars (so 20, or 8.5 or 5.99 for example)
  397. $vatformated=vatrate($line->tva_tx);
  398. if (empty($resarray[$array_key.'_total_vat_'.$vatformated])) $resarray[$array_key.'_total_vat_'.$vatformated]=0;
  399. $resarray[$array_key.'_total_vat_'.$vatformated]+=$line->total_tva;
  400. $resarray[$array_key.'_total_vat_locale_'.$vatformated]=price($resarray[$array_key.'_total_vat_'.$vatformated]);
  401. }
  402. // Retrieve extrafields
  403. if (is_array($object->array_options) && count($object->array_options))
  404. {
  405. $extrafieldkey=$object->element;
  406. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  407. $extrafields = new ExtraFields($this->db);
  408. $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
  409. $object->fetch_optionals($object->id,$extralabels);
  410. $resarray = $this->fill_substitutionarray_with_extrafields($object,$resarray,$extrafields,$array_key,$outputlangs);
  411. }
  412. return $resarray;
  413. }
  414. /**
  415. * Define array with couple substitution key => substitution value
  416. *
  417. * @param array $line Array of lines
  418. * @param Translate $outputlangs Lang object to use for output
  419. * @return array Return a substitution array
  420. */
  421. function get_substitutionarray_lines($line,$outputlangs)
  422. {
  423. global $conf;
  424. $resarray= array(
  425. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  426. 'line_product_ref'=>$line->product_ref,
  427. 'line_product_label'=>$line->product_label,
  428. 'line_product_type'=>$line->product_type,
  429. 'line_desc'=>$line->desc,
  430. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  431. 'line_up'=>price2num($line->subprice),
  432. 'line_up_locale'=>price($line->subprice, 0, $outputlangs),
  433. 'line_qty'=>$line->qty,
  434. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  435. 'line_price_ht'=>price2num($line->total_ht),
  436. 'line_price_ttc'=>price2num($line->total_ttc),
  437. 'line_price_vat'=>price2num($line->total_tva),
  438. 'line_price_ht_locale'=>price($line->total_ht, 0, $outputlangs),
  439. 'line_price_ttc_locale'=>price($line->total_ttc, 0, $outputlangs),
  440. 'line_price_vat_locale'=>price($line->total_tva, 0, $outputlangs),
  441. // Dates
  442. 'line_date_start'=>dol_print_date($line->date_start, 'day', 'tzuser'),
  443. 'line_date_start_locale'=>dol_print_date($line->date_start, 'day', 'tzuser', $outputlangs),
  444. 'line_date_start_rfc'=>dol_print_date($line->date_start, 'dayrfc', 'tzuser'),
  445. 'line_date_end'=>dol_print_date($line->date_end, 'day', 'tzuser'),
  446. 'line_date_end_locale'=>dol_print_date($line->date_end, 'day', 'tzuser', $outputlangs),
  447. 'line_date_end_rfc'=>dol_print_date($line->date_end, 'dayrfc', 'tzuser'),
  448. );
  449. // Retrieve extrafields
  450. $extrafieldkey=$line->element;
  451. $array_key="line";
  452. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  453. $extrafields = new ExtraFields($this->db);
  454. $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
  455. $line->fetch_optionals($line->rowid,$extralabels);
  456. $resarray = $this->fill_substitutionarray_with_extrafields($line,$resarray,$extrafields,$array_key=$array_key,$outputlangs);
  457. return $resarray;
  458. }
  459. /**
  460. * Define array with couple substitution key => substitution value
  461. *
  462. * @param Expedition $object Main object to use as data source
  463. * @param Translate $outputlangs Lang object to use for output
  464. * @param array_key $array_key Name of the key for return array
  465. * @return array Array of substitution
  466. */
  467. function get_substitutionarray_shipment($object,$outputlangs,$array_key='object')
  468. {
  469. global $conf;
  470. dol_include_once('/core/lib/product.lib.php');
  471. $object->list_delivery_methods($object->shipping_method_id);
  472. $calculatedVolume=($object->trueWidth * $object->trueHeight * $object->trueDepth);
  473. $array_shipment=array(
  474. $array_key.'_id'=>$object->id,
  475. $array_key.'_ref'=>$object->ref,
  476. $array_key.'_ref_ext'=>$object->ref_ext,
  477. $array_key.'_ref_customer'=>$object->ref_customer,
  478. $array_key.'_date_delivery'=>dol_print_date($object->date_delivery,'day'),
  479. $array_key.'_hour_delivery'=>dol_print_date($object->date_delivery,'hour'),
  480. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  481. $array_key.'_total_ht'=>price($object->total_ht),
  482. $array_key.'_total_vat'=>price($object->total_tva),
  483. $array_key.'_total_ttc'=>price($object->total_ttc),
  484. $array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
  485. $array_key.'_note_private'=>$object->note_private,
  486. $array_key.'_note'=>$object->note_public,
  487. $array_key.'_tracking_number'=>$object->tracking_number,
  488. $array_key.'_tracking_url'=>$object->tracking_url,
  489. $array_key.'_shipping_method'=>$object->listmeths[0]['libelle'],
  490. $array_key.'_weight'=>$object->trueWeight.' '.measuring_units_string($object->weight_units, 'weight'),
  491. $array_key.'_width'=>$object->trueWidth.' '.measuring_units_string($object->width_units, 'size'),
  492. $array_key.'_height'=>$object->trueHeight.' '.measuring_units_string($object->height_units, 'size'),
  493. $array_key.'_depth'=>$object->trueDepth.' '.measuring_units_string($object->depth_units, 'size'),
  494. $array_key.'_size'=>$calculatedVolume.' '.measuring_units_string(0, 'volume'),
  495. );
  496. // Add vat by rates
  497. foreach ($object->lines as $line)
  498. {
  499. if (empty($array_shipment[$array_key.'_total_vat_'.$line->tva_tx])) $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]=0;
  500. $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  501. }
  502. // Retrieve extrafields
  503. /*if(is_array($object->array_options) && count($object->array_options))
  504. {
  505. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  506. $extrafields = new ExtraFields($this->db);
  507. $extralabels = $extrafields->fetch_name_optionals_label('shipment',true);
  508. $object->fetch_optionals($object->id,$extralabels);
  509. $array_shipment = $this->fill_substitutionarray_with_extrafields($object,$array_shipment,$extrafields,$array_key,$outputlangs);
  510. }*/
  511. return $array_shipment;
  512. }
  513. /**
  514. * Define array with couple substitution key => substitution value
  515. *
  516. * @param array $line Array of lines
  517. * @param Translate $outputlangs Lang object to use for output
  518. * @return array Substitution array
  519. */
  520. function get_substitutionarray_shipment_lines($line,$outputlangs)
  521. {
  522. global $conf;
  523. dol_include_once('/core/lib/product.lib.php');
  524. return array(
  525. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  526. 'line_product_ref'=>$line->product_ref,
  527. 'line_product_label'=>$line->product_label,
  528. 'line_desc'=>$line->desc,
  529. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  530. 'line_up'=>price($line->subprice),
  531. 'line_qty'=>$line->qty,
  532. 'line_qty_shipped'=>$line->qty_shipped,
  533. 'line_qty_asked'=>$line->qty_asked,
  534. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  535. 'line_price_ht'=>price($line->total_ht),
  536. 'line_price_ttc'=>price($line->total_ttc),
  537. 'line_price_vat'=>price($line->total_tva),
  538. 'line_weight'=>empty($line->weight) ? '' : $line->weight*$line->qty_shipped.' '.measuring_units_string($line->weight_units, 'weight'),
  539. 'line_length'=>empty($line->length) ? '' : $line->length*$line->qty_shipped.' '.measuring_units_string($line->length_units, 'size'),
  540. 'line_surface'=>empty($line->surface) ? '' : $line->surface*$line->qty_shipped.' '.measuring_units_string($line->surface_units, 'surface'),
  541. 'line_volume'=>empty($line->volume) ? '' : $line->volume*$line->qty_shipped.' '.measuring_units_string($line->volume_units, 'volume'),
  542. );
  543. }
  544. /**
  545. * Fill array with couple extrafield key => extrafield value
  546. *
  547. * @param Object $object Object with extrafields (must have $object->array_options filled)
  548. * @param array $array_to_fill Substitution array
  549. * @param Extrafields $extrafields Extrafields object
  550. * @param string $array_key Prefix for name of the keys into returned array
  551. * @param Translate $outputlangs Lang object to use for output
  552. * @return array Substitution array
  553. */
  554. function fill_substitutionarray_with_extrafields($object,$array_to_fill,$extrafields,$array_key,$outputlangs)
  555. {
  556. global $conf;
  557. foreach($extrafields->attribute_label as $key=>$label)
  558. {
  559. if($extrafields->attribute_type[$key] == 'price')
  560. {
  561. $object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key]);
  562. $object->array_options['options_'.$key.'_currency'] = price($object->array_options['options_'.$key],0,$outputlangs,0,0,-1,$conf->currency);
  563. //Add value to store price with currency
  564. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
  565. }
  566. else if($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
  567. {
  568. $object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
  569. }
  570. else if($extrafields->attribute_type[$key] == 'date')
  571. {
  572. if (strlen($object->array_options['options_'.$key])>0)
  573. {
  574. $date = $object->array_options['options_'.$key];
  575. $object->array_options['options_'.$key] = dol_print_date($date,'day'); // using company output language
  576. $object->array_options['options_'.$key.'_locale'] = dol_print_date($date,'day','tzserver',$outputlangs); // using output language format
  577. $object->array_options['options_'.$key.'_rfc'] = dol_print_date($date,'dayrfc'); // international format
  578. }
  579. else
  580. {
  581. $object->array_options['options_'.$key] = '';
  582. $object->array_options['options_'.$key.'_locale'] = '';
  583. $object->array_options['options_'.$key.'_rfc'] = '';
  584. }
  585. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  586. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  587. }
  588. else if($extrafields->attribute_type[$key] == 'datetime')
  589. {
  590. $datetime = $object->array_options['options_'.$key];
  591. $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
  592. $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
  593. $object->array_options['options_'.$key.'_rfc'] = ($datetime!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhourrfc'):''); // international format
  594. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  595. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  596. }
  597. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
  598. }
  599. return $array_to_fill;
  600. }
  601. /**
  602. * Rect pdf
  603. *
  604. * @param PDF $pdf Object PDF
  605. * @param float $x Abscissa of first point
  606. * @param float $y Ordinate of first point
  607. * @param float $l ??
  608. * @param float $h ??
  609. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  610. * @param int $hidebottom Hide bottom
  611. * @return void
  612. */
  613. function printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
  614. {
  615. if (empty($hidetop) || $hidetop==-1) $pdf->line($x, $y, $x+$l, $y);
  616. $pdf->line($x+$l, $y, $x+$l, $y+$h);
  617. if (empty($hidebottom)) $pdf->line($x+$l, $y+$h, $x, $y+$h);
  618. $pdf->line($x, $y+$h, $x, $y);
  619. }
  620. }