commondocgenerator.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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 substitution key => substitution value
  294. *
  295. * @param Object $object Main object to use as data source
  296. * @param Translate $outputlangs Lang object to use for output
  297. * @param string $array_key Name of the key for return array
  298. * @return array Array of substitution
  299. */
  300. function get_substitutionarray_object($object,$outputlangs,$array_key='object')
  301. {
  302. global $conf;
  303. $sumpayed=''; $alreadypayed='';
  304. if ($object->element == 'facture')
  305. {
  306. $invoice_source=new Facture($this->db);
  307. if ($object->fk_facture_source > 0)
  308. {
  309. $invoice_source->fetch($object->fk_facture_source);
  310. }
  311. $sumpayed = $object->getSommePaiement();
  312. $alreadypayed=price($sumpayed,0,$outputlangs);
  313. }
  314. $resarray=array(
  315. $array_key.'_id'=>$object->id,
  316. $array_key.'_ref'=>$object->ref,
  317. $array_key.'_ref_ext'=>$object->ref_ext,
  318. $array_key.'_ref_customer'=>$object->ref_client,
  319. $array_key.'_ref_supplier'=>(! empty($object->ref_fournisseur)?$object->ref_fournisseur:''),
  320. $array_key.'_source_invoice_ref'=>$invoice_source->ref,
  321. // Dates
  322. $array_key.'_hour'=>dol_print_date($object->date,'hour'),
  323. $array_key.'_date'=>dol_print_date($object->date,'day'),
  324. $array_key.'_date_rfc'=>dol_print_date($object->date,'dayrfc'),
  325. $array_key.'_date_limit'=>(! empty($object->date_lim_reglement)?dol_print_date($object->date_lim_reglement,'day'):''),
  326. $array_key.'_date_end'=>(! empty($object->fin_validite)?dol_print_date($object->fin_validite,'day'):''),
  327. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  328. $array_key.'_date_modification'=>(! empty($object->date_modification)?dol_print_date($object->date_modification,'day'):''),
  329. $array_key.'_date_validation'=>(! empty($object->date_validation)?dol_print_date($object->date_validation,'dayhour'):''),
  330. $array_key.'_date_delivery_planed'=>(! empty($object->date_livraison)?dol_print_date($object->date_livraison,'day'):''),
  331. $array_key.'_date_close'=>(! empty($object->date_cloture)?dol_print_date($object->date_cloture,'dayhour'):''),
  332. $array_key.'_payment_mode_code'=>$object->mode_reglement_code,
  333. $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),
  334. $array_key.'_payment_term_code'=>$object->cond_reglement_code,
  335. $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),
  336. $array_key.'_total_ht_locale'=>price($object->total_ht, 0, $outputlangs),
  337. $array_key.'_total_vat_locale'=>(! empty($object->total_vat)?price($object->total_vat, 0, $outputlangs):price($object->total_tva, 0, $outputlangs)),
  338. $array_key.'_total_localtax1_locale'=>price($object->total_localtax1, 0, $outputlangs),
  339. $array_key.'_total_localtax2_locale'=>price($object->total_localtax2, 0, $outputlangs),
  340. $array_key.'_total_ttc_locale'=>price($object->total_ttc, 0, $outputlangs),
  341. $array_key.'_total_discount_ht_locale' => price($object->getTotalDiscount(), 0, $outputlangs),
  342. $array_key.'_total_ht'=>price2num($object->total_ht),
  343. $array_key.'_total_vat'=>(! empty($object->total_vat)?price2num($object->total_vat):price2num($object->total_tva)),
  344. $array_key.'_total_localtax1'=>price2num($object->total_localtax1),
  345. $array_key.'_total_localtax2'=>price2num($object->total_localtax2),
  346. $array_key.'_total_ttc'=>price2num($object->total_ttc),
  347. $array_key.'_total_discount_ht' => price2num($object->getTotalDiscount()),
  348. $array_key.'_note_private'=>$object->note,
  349. $array_key.'_note_public'=>$object->note_public,
  350. $array_key.'_note'=>$object->note_public, // For backward compatibility
  351. // Payments
  352. $array_key.'_already_payed_locale'=>price($alreadypayed, 0, $outputlangs),
  353. $array_key.'_remain_to_pay_locale'=>price($object->total_ttc - $sumpayed, 0, $outputlangs),
  354. $array_key.'_already_payed'=>$alreadypayed,
  355. $array_key.'_remain_to_pay'=>price2num($object->total_ttc - $sumpayed)
  356. );
  357. // Add vat by rates
  358. foreach ($object->lines as $line)
  359. {
  360. // $line->tva_tx format depends on database field accuraty, no reliable. This is kept for backward comaptibility
  361. if (empty($resarray[$array_key.'_total_vat_'.$line->tva_tx])) $resarray[$array_key.'_total_vat_'.$line->tva_tx]=0;
  362. $resarray[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  363. $resarray[$array_key.'_total_vat_locale_'.$line->tva_tx]=price($resarray[$array_key.'_total_vat_'.$line->tva_tx]);
  364. // $vatformated is vat without not expected chars (so 20, or 8.5 or 5.99 for example)
  365. $vatformated=vatrate($line->tva_tx);
  366. if (empty($resarray[$array_key.'_total_vat_'.$vatformated])) $resarray[$array_key.'_total_vat_'.$vatformated]=0;
  367. $resarray[$array_key.'_total_vat_'.$vatformated]+=$line->total_tva;
  368. $resarray[$array_key.'_total_vat_locale_'.$vatformated]=price($resarray[$array_key.'_total_vat_'.$vatformated]);
  369. }
  370. // Retrieve extrafields
  371. if (is_array($object->array_options) && count($object->array_options))
  372. {
  373. $extrafieldkey=$object->element;
  374. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  375. $extrafields = new ExtraFields($this->db);
  376. $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
  377. $object->fetch_optionals($object->id,$extralabels);
  378. $resarray = $this->fill_substitutionarray_with_extrafields($object,$resarray,$extrafields,$array_key,$outputlangs);
  379. }
  380. return $resarray;
  381. }
  382. /**
  383. * Define array with couple substitution key => substitution value
  384. *
  385. * @param array $line Array of lines
  386. * @param Translate $outputlangs Lang object to use for output
  387. * @return array Return a substitution array
  388. */
  389. function get_substitutionarray_lines($line,$outputlangs)
  390. {
  391. global $conf;
  392. $resarray= array(
  393. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  394. 'line_product_ref'=>$line->product_ref,
  395. 'line_product_label'=>$line->product_label,
  396. 'line_product_type'=>$line->product_type,
  397. 'line_desc'=>$line->desc,
  398. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  399. 'line_up'=>price2num($line->subprice),
  400. 'line_up_locale'=>price($line->subprice, 0, $outputlangs),
  401. 'line_qty'=>$line->qty,
  402. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  403. 'line_price_ht'=>price2num($line->total_ht),
  404. 'line_price_ttc'=>price2num($line->total_ttc),
  405. 'line_price_vat'=>price2num($line->total_tva),
  406. 'line_price_ht_locale'=>price($line->total_ht, 0, $outputlangs),
  407. 'line_price_ttc_locale'=>price($line->total_ttc, 0, $outputlangs),
  408. 'line_price_vat_locale'=>price($line->total_tva, 0, $outputlangs),
  409. // Dates
  410. 'line_date_start'=>dol_print_date($line->date_start, 'day', 'tzuser'),
  411. 'line_date_start_locale'=>dol_print_date($line->date_start, 'day', 'tzuser', $outputlangs),
  412. 'line_date_start_rfc'=>dol_print_date($line->date_start, 'dayrfc', 'tzuser'),
  413. 'line_date_end'=>dol_print_date($line->date_end, 'day', 'tzuser'),
  414. 'line_date_end_locale'=>dol_print_date($line->date_end, 'day', 'tzuser', $outputlangs),
  415. 'line_date_end_rfc'=>dol_print_date($line->date_end, 'dayrfc', 'tzuser'),
  416. );
  417. // Retrieve extrafields
  418. $extrafieldkey=$line->element;
  419. $array_key="line";
  420. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  421. $extrafields = new ExtraFields($this->db);
  422. $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
  423. $line->fetch_optionals($line->rowid,$extralabels);
  424. $resarray = $this->fill_substitutionarray_with_extrafields($line,$resarray,$extrafields,$array_key=$array_key,$outputlangs);
  425. return $resarray;
  426. }
  427. /**
  428. * Define array with couple substitution key => substitution value
  429. *
  430. * @param Expedition $object Main object to use as data source
  431. * @param Translate $outputlangs Lang object to use for output
  432. * @param array_key $array_key Name of the key for return array
  433. * @return array Array of substitution
  434. */
  435. function get_substitutionarray_shipment($object,$outputlangs,$array_key='object')
  436. {
  437. global $conf;
  438. dol_include_once('/core/lib/product.lib.php');
  439. $object->list_delivery_methods($object->shipping_method_id);
  440. $calculatedVolume=($object->trueWidth * $object->trueHeight * $object->trueDepth);
  441. $array_shipment=array(
  442. $array_key.'_id'=>$object->id,
  443. $array_key.'_ref'=>$object->ref,
  444. $array_key.'_ref_ext'=>$object->ref_ext,
  445. $array_key.'_ref_customer'=>$object->ref_customer,
  446. $array_key.'_date_delivery'=>dol_print_date($object->date_delivery,'day'),
  447. $array_key.'_hour_delivery'=>dol_print_date($object->date_delivery,'hour'),
  448. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  449. $array_key.'_total_ht'=>price($object->total_ht),
  450. $array_key.'_total_vat'=>price($object->total_tva),
  451. $array_key.'_total_ttc'=>price($object->total_ttc),
  452. $array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
  453. $array_key.'_note_private'=>$object->note_private,
  454. $array_key.'_note'=>$object->note_public,
  455. $array_key.'_tracking_number'=>$object->tracking_number,
  456. $array_key.'_tracking_url'=>$object->tracking_url,
  457. $array_key.'_shipping_method'=>$object->listmeths[0]['libelle'],
  458. $array_key.'_weight'=>$object->trueWeight.' '.measuring_units_string($object->weight_units, 'weight'),
  459. $array_key.'_width'=>$object->trueWidth.' '.measuring_units_string($object->width_units, 'size'),
  460. $array_key.'_height'=>$object->trueHeight.' '.measuring_units_string($object->height_units, 'size'),
  461. $array_key.'_depth'=>$object->trueDepth.' '.measuring_units_string($object->depth_units, 'size'),
  462. $array_key.'_size'=>$calculatedVolume.' '.measuring_units_string(0, 'volume'),
  463. );
  464. // Add vat by rates
  465. foreach ($object->lines as $line)
  466. {
  467. if (empty($array_shipment[$array_key.'_total_vat_'.$line->tva_tx])) $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]=0;
  468. $array_shipment[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  469. }
  470. // Retrieve extrafields
  471. /*if(is_array($object->array_options) && count($object->array_options))
  472. {
  473. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  474. $extrafields = new ExtraFields($this->db);
  475. $extralabels = $extrafields->fetch_name_optionals_label('shipment',true);
  476. $object->fetch_optionals($object->id,$extralabels);
  477. $array_shipment = $this->fill_substitutionarray_with_extrafields($object,$array_shipment,$extrafields,$array_key,$outputlangs);
  478. }*/
  479. return $array_shipment;
  480. }
  481. /**
  482. * Define array with couple substitution key => substitution value
  483. *
  484. * @param array $line Array of lines
  485. * @param Translate $outputlangs Lang object to use for output
  486. * @return array Substitution array
  487. */
  488. function get_substitutionarray_shipment_lines($line,$outputlangs)
  489. {
  490. global $conf;
  491. dol_include_once('/core/lib/product.lib.php');
  492. return array(
  493. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  494. 'line_product_ref'=>$line->product_ref,
  495. 'line_product_label'=>$line->product_label,
  496. 'line_desc'=>$line->desc,
  497. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  498. 'line_up'=>price($line->subprice),
  499. 'line_qty'=>$line->qty,
  500. 'line_qty_shipped'=>$line->qty_shipped,
  501. 'line_qty_asked'=>$line->qty_asked,
  502. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  503. 'line_price_ht'=>price($line->total_ht),
  504. 'line_price_ttc'=>price($line->total_ttc),
  505. 'line_price_vat'=>price($line->total_tva),
  506. 'line_weight'=>empty($line->weight) ? '' : $line->weight*$line->qty_shipped.' '.measuring_units_string($line->weight_units, 'weight'),
  507. 'line_length'=>empty($line->length) ? '' : $line->length*$line->qty_shipped.' '.measuring_units_string($line->length_units, 'size'),
  508. 'line_surface'=>empty($line->surface) ? '' : $line->surface*$line->qty_shipped.' '.measuring_units_string($line->surface_units, 'surface'),
  509. 'line_volume'=>empty($line->volume) ? '' : $line->volume*$line->qty_shipped.' '.measuring_units_string($line->volume_units, 'volume'),
  510. );
  511. }
  512. /**
  513. * Fill array with couple extrafield key => extrafield value
  514. *
  515. * @param Object $object Object with extrafields (must have $object->array_options filled)
  516. * @param array $array_to_fill Substitution array
  517. * @param Extrafields $extrafields Extrafields object
  518. * @param string $array_key Prefix for name of the keys into returned array
  519. * @param Translate $outputlangs Lang object to use for output
  520. * @return array Substitution array
  521. */
  522. function fill_substitutionarray_with_extrafields($object,$array_to_fill,$extrafields,$array_key,$outputlangs)
  523. {
  524. global $conf;
  525. foreach($extrafields->attribute_label as $key=>$label)
  526. {
  527. if($extrafields->attribute_type[$key] == 'price')
  528. {
  529. $object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key]);
  530. $object->array_options['options_'.$key.'_currency'] = price($object->array_options['options_'.$key],0,$outputlangs,0,0,-1,$conf->currency);
  531. //Add value to store price with currency
  532. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
  533. }
  534. else if($extrafields->attribute_type[$key] == 'select' || $extrafields->attribute_type[$key] == 'checkbox')
  535. {
  536. $object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
  537. }
  538. else if($extrafields->attribute_type[$key] == 'date')
  539. {
  540. if (strlen($object->array_options['options_'.$key])>0)
  541. {
  542. $date = $object->array_options['options_'.$key];
  543. $object->array_options['options_'.$key] = dol_print_date($date,'day'); // using company output language
  544. $object->array_options['options_'.$key.'_locale'] = dol_print_date($date,'day','tzserver',$outputlangs); // using output language format
  545. $object->array_options['options_'.$key.'_rfc'] = dol_print_date($date,'dayrfc'); // international format
  546. }
  547. else
  548. {
  549. $object->array_options['options_'.$key] = '';
  550. $object->array_options['options_'.$key.'_locale'] = '';
  551. $object->array_options['options_'.$key.'_rfc'] = '';
  552. }
  553. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  554. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  555. }
  556. else if($extrafields->attribute_type[$key] == 'datetime')
  557. {
  558. $datetime = $object->array_options['options_'.$key];
  559. $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
  560. $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
  561. $object->array_options['options_'.$key.'_rfc'] = ($datetime!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhourrfc'):''); // international format
  562. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
  563. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
  564. }
  565. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
  566. }
  567. return $array_to_fill;
  568. }
  569. /**
  570. * Rect pdf
  571. *
  572. * @param PDF $pdf Object PDF
  573. * @param float $x Abscissa of first point
  574. * @param float $y Ordinate of first point
  575. * @param float $l ??
  576. * @param float $h ??
  577. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  578. * @param int $hidebottom Hide bottom
  579. * @return void
  580. */
  581. function printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
  582. {
  583. if (empty($hidetop) || $hidetop==-1) $pdf->line($x, $y, $x+$l, $y);
  584. $pdf->line($x+$l, $y, $x+$l, $y+$h);
  585. if (empty($hidebottom)) $pdf->line($x+$l, $y+$h, $x, $y+$h);
  586. $pdf->line($x, $y+$h, $x, $y);
  587. }
  588. }