PageRenderTime 112ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/htdocs/core/modules/action/rapport.pdf.php

https://github.com/asterix14/dolibarr
PHP | 265 lines | 161 code | 44 blank | 60 comment | 12 complexity | 583e9f2b4db84645f1674eb49be8560f MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/action/rapport.pdf.php
  22. * \ingroup commercial
  23. * \brief File to build PDF with events
  24. */
  25. require_once(DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php');
  26. require_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php');
  27. require_once(DOL_DOCUMENT_ROOT."/core/lib/company.lib.php");
  28. /**
  29. * \class CommActionRapport
  30. * \brief Classe permettant la generation des rapports d'actions
  31. */
  32. class CommActionRapport
  33. {
  34. var $db;
  35. var $description;
  36. var $date_edition;
  37. var $year;
  38. var $month;
  39. var $title;
  40. var $subject;
  41. function CommActionRapport($db=0, $month, $year)
  42. {
  43. global $langs;
  44. $langs->load("commercial");
  45. $this->db = $db;
  46. $this->description = "";
  47. $this->date_edition = time();
  48. $this->month = $month;
  49. $this->year = $year;
  50. // Dimension page pour format A4
  51. $this->type = 'pdf';
  52. $formatarray=pdf_getFormat();
  53. $this->page_largeur = $formatarray['width'];
  54. $this->page_hauteur = $formatarray['height'];
  55. $this->format = array($this->page_largeur,$this->page_hauteur);
  56. $this->marge_gauche=5;
  57. $this->marge_droite=5;
  58. $this->marge_haute=10;
  59. $this->marge_basse=10;
  60. $this->title=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
  61. $this->subject=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
  62. }
  63. /**
  64. * Write the object to document file to disk
  65. * @param socid
  66. * @param catid
  67. * @param outputlangs Lang object for output language
  68. * @return int 1=OK, 0=KO
  69. */
  70. function write_file($socid = 0, $catid = 0, $outputlangs='')
  71. {
  72. global $user,$conf,$langs;
  73. if (! is_object($outputlangs)) $outputlangs=$langs;
  74. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  75. if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
  76. $outputlangs->load("main");
  77. $outputlangs->load("dict");
  78. $outputlangs->load("companies");
  79. $outputlangs->load("bills");
  80. $outputlangs->load("products");
  81. $dir = $conf->agenda->dir_temp."/";
  82. $file = $dir . "actions-".$this->month."-".$this->year.".pdf";
  83. if (! file_exists($dir))
  84. {
  85. if (create_exdir($dir) < 0)
  86. {
  87. $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
  88. return 0;
  89. }
  90. }
  91. if (file_exists($dir))
  92. {
  93. $pdf=pdf_getInstance($this->format);
  94. if (class_exists('TCPDF'))
  95. {
  96. $pdf->setPrintHeader(false);
  97. $pdf->setPrintFooter(false);
  98. }
  99. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  100. $pdf->Open();
  101. $pagenb=0;
  102. $pdf->SetDrawColor(128,128,128);
  103. $pdf->SetFillColor(220,220,220);
  104. $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
  105. $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
  106. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  107. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  108. $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
  109. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  110. $pdf->SetAutoPageBreak(1,0);
  111. $nbpage = $this->_pages($pdf, $outputlangs);
  112. $pdf->AliasNbPages();
  113. $pdf->Close();
  114. $pdf->Output($file,'F');
  115. if (! empty($conf->global->MAIN_UMASK))
  116. @chmod($file, octdec($conf->global->MAIN_UMASK));
  117. return 1;
  118. }
  119. }
  120. /**
  121. * Write content of pages
  122. *
  123. * @param $pdf
  124. * @param $outputlangs
  125. * @return int 1
  126. */
  127. function _pages(&$pdf, $outputlangs)
  128. {
  129. $height=3; // height for text separation
  130. $pagenb=1;
  131. $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
  132. $y++;
  133. $pdf->SetFont('','',8);
  134. $sql = "SELECT s.nom as societe, s.rowid as socid, s.client,";
  135. $sql.= " a.id, a.datep as dp, a.datep2 as dp2,";
  136. $sql.= " a.fk_contact, a.note, a.percent as percent,";
  137. $sql.= " c.libelle,";
  138. $sql.= " u.login";
  139. $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
  140. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
  141. $sql.= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
  142. $sql.= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year,$this->month,false))."'";
  143. $sql.= " AND '".$this->db->idate(dol_get_last_day($this->year,$this->month,false))."'";
  144. $sql.= " ORDER BY a.datep DESC";
  145. dol_syslog("Rapport.pdf::_page sql=".$sql);
  146. $resql=$this->db->query($sql);
  147. if ($resql)
  148. {
  149. $num = $this->db->num_rows($resql);
  150. $i = 0;
  151. $y0=$y1=$y2=$y3=0;
  152. while ($i < $num)
  153. {
  154. $obj = $this->db->fetch_object($resql);
  155. $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
  156. // Calculate height of text
  157. $text=dol_trunc(dol_htmlentitiesbr_decode($obj->note),150);
  158. //print 'd'.$text; exit;
  159. $nboflines=dol_nboflines($text);
  160. $heightlinemax=max(2*$height,$nboflines*$height);
  161. // Check if there is enough space to print record
  162. if ((1+$y+$heightlinemax) >= ($this->page_hauteur - $this->marge_haute))
  163. {
  164. // We need to break page
  165. $pagenb++;
  166. $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
  167. $y++;
  168. $pdf->SetFont('','',8);
  169. }
  170. $y++;
  171. $pdf->SetXY($this->marge_gauche, $y);
  172. $pdf->MultiCell(22, $height, dol_print_date($this->db->jdate($obj->dp),"day")."\n".dol_print_date($this->db->jdate($obj->dp),"hour"), 0, 'L', 0);
  173. $y0 = $pdf->GetY();
  174. $pdf->SetXY(26, $y);
  175. $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($obj->societe),32), 0, 'L', 0);
  176. $y1 = $pdf->GetY();
  177. $pdf->SetXY(60,$y);
  178. $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($obj->libelle),32), 0, 'L', 0);
  179. $y2 = $pdf->GetY();
  180. $pdf->SetXY(106,$y);
  181. $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset($text), 0, 'L', 0);
  182. $y3 = $pdf->GetY();
  183. //$pdf->MultiCell(94,2,"y=$y y3=$y3",0,'L',0);
  184. $i++;
  185. }
  186. }
  187. return 1;
  188. }
  189. /**
  190. * Show page head
  191. *
  192. * @param pdf Objet PDF
  193. * @param outputlangs Objet lang cible
  194. * @param pagenb Page nb
  195. */
  196. function _pagehead(&$pdf, $outputlangs, $pagenb)
  197. {
  198. global $conf,$langs;
  199. // Do not add the BACKGROUND as this is a report
  200. //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
  201. // New page
  202. $pdf->AddPage();
  203. // Show title
  204. $pdf->SetFont('','B',10);
  205. $pdf->SetXY($this->marge_gauche, $this->marge_haute);
  206. $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
  207. // Show page nb only on iso languages (so default Helvetica font)
  208. if (pdf_getPDFFont($outputlangs) == 'Helvetica')
  209. {
  210. $pdf->SetXY($this->page_largeur-$this->marge_droite-40, $this->marge_haute);
  211. $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
  212. }
  213. $y=$pdf->GetY()+2;
  214. $pdf->Rect($this->marge_gauche, $y,
  215. $this->page_largeur - $this->marge_gauche - $this->marge_droite,
  216. $this->page_hauteur - $this->marge_haute - $this->marge_basse);
  217. $y=$pdf->GetY()+1;
  218. return $y;
  219. }
  220. }
  221. ?>