commondocgenerator.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * or see http://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/class/commondocgenerator.class.php
  23. * \ingroup core
  24. * \brief File of parent class for documents generators
  25. */
  26. /**
  27. * \class CommonDocGenerator
  28. * \brief Parent class for documents generators
  29. */
  30. abstract class CommonDocGenerator
  31. {
  32. var $error='';
  33. /**
  34. * Define array with couple subtitution key => subtitution value
  35. *
  36. * @param User $user User
  37. * @param Translate $outputlangs Language object for output
  38. * @return array Array of substitution key->code
  39. */
  40. function get_substitutionarray_user($user,$outputlangs)
  41. {
  42. global $conf;
  43. return array(
  44. 'myuser_lastname'=>$user->lastname,
  45. 'myuser_firstname'=>$user->firstname,
  46. 'myuser_login'=>$user->login,
  47. 'myuser_phone'=>$user->office_phone,
  48. 'myuser_address'=>$user->address,
  49. 'myuser_zip'=>$user->zip,
  50. 'myuser_town'=>$user->town,
  51. 'myuser_country'=>$user->country,
  52. 'myuser_country_code'=>$user->country_code,
  53. 'myuser_state'=>$user->state,
  54. 'myuser_state_code'=>$user->state_code,
  55. 'myuser_fax'=>$user->office_fax,
  56. 'myuser_mobile'=>$user->user_mobile,
  57. 'myuser_email'=>$user->email,
  58. 'myuser_web'=>'' // url not exist in $user object
  59. );
  60. }
  61. /**
  62. * Define array with couple subtitution key => subtitution value
  63. *
  64. * @param Societe $mysoc Object thirdparty
  65. * @param Translate $outputlangs Language object for output
  66. * @return array Array of substitution key->code
  67. */
  68. function get_substitutionarray_mysoc($mysoc,$outputlangs)
  69. {
  70. global $conf;
  71. if (empty($mysoc->forme_juridique) && ! empty($mysoc->forme_juridique_code))
  72. {
  73. $mysoc->forme_juridique=getFormeJuridiqueLabel($mysoc->forme_juridique_code);
  74. }
  75. if (empty($mysoc->country) && ! empty($mysoc->country_code))
  76. {
  77. $mysoc->country=$outputlangs->transnoentitiesnoconv("Country".$mysoc->country_code);
  78. }
  79. if (empty($mysoc->state) && ! empty($mysoc->state_code))
  80. {
  81. $mysoc->state=getState($mysoc->state_code,0);
  82. }
  83. $logotouse=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  84. return array(
  85. 'mycompany_logo'=>$logotouse,
  86. 'mycompany_name'=>$mysoc->name,
  87. 'mycompany_email'=>$mysoc->email,
  88. 'mycompany_phone'=>$mysoc->phone,
  89. 'mycompany_fax'=>$mysoc->fax,
  90. 'mycompany_address'=>$mysoc->address,
  91. 'mycompany_zip'=>$mysoc->zip,
  92. 'mycompany_town'=>$mysoc->town,
  93. 'mycompany_country'=>$mysoc->country,
  94. 'mycompany_country_code'=>$mysoc->country_code,
  95. 'mycompany_state'=>$mysoc->state,
  96. 'mycompany_state_code'=>$mysoc->state_code,
  97. 'mycompany_web'=>$mysoc->url,
  98. 'mycompany_juridicalstatus'=>$mysoc->forme_juridique,
  99. 'mycompany_capital'=>$mysoc->capital,
  100. 'mycompany_barcode'=>$mysoc->barcode,
  101. 'mycompany_idprof1'=>$mysoc->idprof1,
  102. 'mycompany_idprof2'=>$mysoc->idprof2,
  103. 'mycompany_idprof3'=>$mysoc->idprof3,
  104. 'mycompany_idprof4'=>$mysoc->idprof4,
  105. 'mycompany_idprof5'=>$mysoc->idprof5,
  106. 'mycompany_idprof6'=>$mysoc->idprof6,
  107. 'mycompany_vatnumber'=>$mysoc->tva_intra,
  108. 'mycompany_note'=>$mysoc->note
  109. );
  110. }
  111. /**
  112. * Define array with couple subtitution key => subtitution value
  113. *
  114. * @param Object $object Object
  115. * @param Translate $outputlangs Language object for output
  116. * @return array Array of substitution key->code
  117. */
  118. function get_substitutionarray_thirdparty($object,$outputlangs)
  119. {
  120. global $conf;
  121. if (empty($object->country) && ! empty($object->country_code))
  122. {
  123. $object->country=$outputlangs->transnoentitiesnoconv("Country".$object->country_code);
  124. }
  125. if (empty($mysoc->state) && ! empty($mysoc->state_code))
  126. {
  127. $object->state=getState($object->state_code,0);
  128. }
  129. $array_thirdparty = array(
  130. 'company_name'=>$object->name,
  131. 'company_email'=>$object->email,
  132. 'company_phone'=>$object->phone,
  133. 'company_fax'=>$object->fax,
  134. 'company_address'=>$object->address,
  135. 'company_zip'=>$object->zip,
  136. 'company_town'=>$object->town,
  137. 'company_country'=>$object->country,
  138. 'company_country_code'=>$object->country_code,
  139. 'company_state'=>$object->state,
  140. 'company_state_code'=>$object->state_code,
  141. 'company_web'=>$object->url,
  142. 'company_barcode'=>$object->barcode,
  143. 'company_vatnumber'=>$object->tva_intra,
  144. 'company_customercode'=>$object->code_client,
  145. 'company_suppliercode'=>$object->code_fournisseur,
  146. 'company_customeraccountancycode'=>$object->code_compta,
  147. 'company_supplieraccountancycode'=>$object->code_compta_fournisseur,
  148. 'company_juridicalstatus'=>$object->forme_juridique,
  149. 'company_capital'=>$object->capital,
  150. 'company_idprof1'=>$object->idprof1,
  151. 'company_idprof2'=>$object->idprof2,
  152. 'company_idprof3'=>$object->idprof3,
  153. 'company_idprof4'=>$object->idprof4,
  154. 'company_idprof5'=>$object->idprof5,
  155. 'company_idprof6'=>$object->idprof6,
  156. 'company_note'=>$object->note
  157. );
  158. // Retrieve extrafields
  159. if(is_array($object->array_options) && count($object->array_options))
  160. {
  161. if(!class_exists('Extrafields'))
  162. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  163. $extrafields = new ExtraFields($this->db);
  164. $extralabels = $extrafields->fetch_name_optionals_label('societe',true);
  165. $object->fetch_optionals($object->id,$extralabels);
  166. foreach($extrafields->attribute_label as $key=>$label)
  167. {
  168. if($extrafields->attribute_type[$key] == 'price')
  169. {
  170. $object->array_options['options_'.$key] = price($object->array_options['options_'.$key]).' '.$outputlangs->getCurrencySymbol($conf->currency);
  171. }
  172. else if($extrafields->attribute_type[$key] == 'select')
  173. {
  174. $object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
  175. }
  176. $array_thirdparty=array_merge($array_thirdparty,array('company_options_'.$key => $object->array_options['options_'.$key]));
  177. }
  178. }
  179. return $array_thirdparty;
  180. }
  181. /**
  182. * Define array with couple subtitution key => subtitution value
  183. *
  184. * @param Translate $outputlangs Language object for output
  185. * @return array Array of substitution key->code
  186. */
  187. function get_substitutionarray_other($outputlangs)
  188. {
  189. global $conf;
  190. $now=dol_now('gmt'); // gmt
  191. $array_other = array(
  192. 'current_date'=>dol_print_date($now,'day','tzuser'),
  193. 'current_datehour'=>dol_print_date($now,'dayhour','tzuser'),
  194. 'current_server_date'=>dol_print_date($now,'day','tzserver'),
  195. 'current_server_datehour'=>dol_print_date($now,'dayhour','tzserver'),
  196. );
  197. return $array_other;
  198. }
  199. /**
  200. * Define array with couple substitution key => substitution value
  201. *
  202. * @param Object $object Main object to use as data source
  203. * @param Translate $outputlangs Lang object to use for output
  204. * @param array_key $array_key Name of the key for return array
  205. * @return array Array of substitution
  206. */
  207. function get_substitutionarray_propal($object,$outputlangs,$array_key='object')
  208. {
  209. global $conf;
  210. $array_propal=array(
  211. $array_key.'_id'=>$object->id,
  212. $array_key.'_ref'=>$object->ref,
  213. $array_key.'_ref_ext'=>$object->ref_ext,
  214. $array_key.'_ref_customer'=>$object->ref_client,
  215. $array_key.'_date'=>dol_print_date($object->date,'day'),
  216. $array_key.'_date_end'=>dol_print_date($object->fin_validite,'day'),
  217. $array_key.'_date_creation'=>dol_print_date($object->date_creation,'day'),
  218. $array_key.'_date_modification'=>dol_print_date($object->date_modification,'day'),
  219. $array_key.'_date_validation'=>dol_print_date($object->date_validation,'dayhour'),
  220. $array_key.'_payment_mode_code'=>$object->mode_reglement_code,
  221. $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),
  222. $array_key.'_payment_term_code'=>$object->cond_reglement_code,
  223. $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),
  224. $array_key.'_total_ht'=>price($object->total_ht2),
  225. $array_key.'_total_vat'=>price($object->total_tva),
  226. $array_key.'_total_ttc'=>price($object->total_ttc),
  227. $array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
  228. $array_key.'_vatrate'=>vatrate($object->tva),
  229. $array_key.'_note_private'=>$object->note,
  230. $array_key.'_note'=>$object->note_public,
  231. );
  232. // Add vat by rates
  233. foreach ($object->lines as $line)
  234. {
  235. if (empty($array_propal[$array_key.'_total_vat_'.$line->tva_tx])) $array_propal[$array_key.'_total_vat_'.$line->tva_tx]=0;
  236. $array_propal[$array_key.'_total_vat_'.$line->tva_tx]+=$line->total_tva;
  237. }
  238. // Retrieve extrafields
  239. if(is_array($object->array_options) && count($object->array_options))
  240. {
  241. if(!class_exists('Extrafields'))
  242. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  243. $extrafields = new ExtraFields($this->db);
  244. $extralabels = $extrafields->fetch_name_optionals_label('propal',true);
  245. $object->fetch_optionals($object->id,$extralabels);
  246. $array_propal = $this->fill_substitutionarray_with_extrafields($object,$array_propal,$extrafields,$array_key,$outputlangs);
  247. }
  248. return $array_propal;
  249. }
  250. /**
  251. * Define array with couple substitution key => substitution value
  252. *
  253. * @param array $line Array of lines
  254. * @param Translate $outputlangs Lang object to use for output
  255. * @return array Substitution array
  256. */
  257. function get_substitutionarray_propal_lines($line,$outputlangs)
  258. {
  259. global $conf;
  260. return array(
  261. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  262. 'line_product_ref'=>$line->product_ref,
  263. 'line_product_label'=>$line->product_label,
  264. 'line_desc'=>$line->desc,
  265. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  266. 'line_up'=>price($line->subprice),
  267. 'line_qty'=>$line->qty,
  268. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  269. 'line_price_ht'=>price($line->total_ht),
  270. 'line_price_ttc'=>price($line->total_ttc),
  271. 'line_price_vat'=>price($line->total_tva),
  272. 'line_date_start'=>$line->date_start,
  273. 'line_date_end'=>$line->date_end
  274. );
  275. }
  276. /**
  277. * Fill array with couple extrafield key => extrafield value
  278. *
  279. * @param Object $object Object with extrafields (must have $object->array_options filled)
  280. * @param array $array_to_fill Substitution array
  281. * @param Extrafields $extrafields Extrafields object
  282. * @param array_key $array_key Name of the key for return array
  283. * @param Translate $outputlangs Lang object to use for output
  284. * @return array Substitution array
  285. */
  286. function fill_substitutionarray_with_extrafields($object,$array_to_fill,$extrafields,$array_key,$outputlangs)
  287. {
  288. global $conf;
  289. foreach($extrafields->attribute_label as $key=>$label)
  290. {
  291. if($extrafields->attribute_type[$key] == 'price')
  292. {
  293. $object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key],2);
  294. $object->array_options['options_'.$key.'_currency'] = $object->array_options['options_'.$key].' '.$outputlangs->getCurrencySymbol($conf->currency);
  295. //Add value to store price with currency
  296. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
  297. }
  298. else if($extrafields->attribute_type[$key] == 'select')
  299. {
  300. $object->array_options['options_'.$key] = $extrafields->attribute_param[$key]['options'][$object->array_options['options_'.$key]];
  301. }
  302. else if($extrafields->attribute_type[$key] == 'date')
  303. {
  304. $object->array_options['options_'.$key] = (strlen($object->array_options['options_'.$key])>0?dol_print_date($object->array_options['options_'.$key],'day'):'');
  305. }
  306. else if($extrafields->attribute_type[$key] == 'datetime')
  307. {
  308. $object->array_options['options_'.$key] = ($object->array_options['options_'.$key]!="0000-00-00 00:00:00"?dol_print_date($object->array_options['options_'.$key],'dayhour'):'');
  309. }
  310. $array_to_fill=array_merge($array_to_fill,array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
  311. }
  312. return $array_to_fill;
  313. }
  314. /**
  315. * Rect pdf
  316. *
  317. * @param PDF $pdf Object PDF
  318. * @param float $x Abscissa of first point
  319. * @param float $y Ordinate of first point
  320. * @param float $l ??
  321. * @param float $h ??
  322. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  323. * @param int $hidebottom Hide bottom
  324. * @return void
  325. */
  326. function printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
  327. {
  328. if (empty($hidetop) || $hidetop==-1) $pdf->line($x, $y, $x+$l, $y);
  329. $pdf->line($x+$l, $y, $x+$l, $y+$h);
  330. if (empty($hidebottom)) $pdf->line($x+$l, $y+$h, $x, $y+$h);
  331. $pdf->line($x, $y+$h, $x, $y);
  332. }
  333. }
  334. ?>