rapport.pdf.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  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 3 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. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  30. /**
  31. * Class to generate event report
  32. */
  33. class CommActionRapport
  34. {
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. /**
  40. * @var string description
  41. */
  42. public $description;
  43. var $date_edition;
  44. var $year;
  45. var $month;
  46. var $title;
  47. var $subject;
  48. var $marge_gauche;
  49. var $marge_droite;
  50. var $marge_haute;
  51. var $marge_basse;
  52. /**
  53. * Constructor
  54. *
  55. * @param DoliDB $db Database handler
  56. * @param int $month Month
  57. * @param int $year Year
  58. */
  59. function __construct($db, $month, $year)
  60. {
  61. global $conf,$langs;
  62. $langs->load("commercial");
  63. $langs->load("projects");
  64. $this->db = $db;
  65. $this->description = "";
  66. $this->date_edition = time();
  67. $this->month = $month;
  68. $this->year = $year;
  69. // Dimension page pour format A4
  70. $this->type = 'pdf';
  71. $formatarray=pdf_getFormat();
  72. $this->page_largeur = $formatarray['width'];
  73. $this->page_hauteur = $formatarray['height'];
  74. $this->format = array($this->page_largeur,$this->page_hauteur);
  75. $this->marge_gauche=isset($conf->global->MAIN_PDF_MARGIN_LEFT)?$conf->global->MAIN_PDF_MARGIN_LEFT:10;
  76. $this->marge_droite=isset($conf->global->MAIN_PDF_MARGIN_RIGHT)?$conf->global->MAIN_PDF_MARGIN_RIGHT:10;
  77. $this->marge_haute =isset($conf->global->MAIN_PDF_MARGIN_TOP)?$conf->global->MAIN_PDF_MARGIN_TOP:10;
  78. $this->marge_basse =isset($conf->global->MAIN_PDF_MARGIN_BOTTOM)?$conf->global->MAIN_PDF_MARGIN_BOTTOM:10;
  79. $this->title=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
  80. $this->subject=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
  81. }
  82. /**
  83. * Write the object to document file to disk
  84. *
  85. * @param int $socid Thirdparty id
  86. * @param int $catid Cat id
  87. * @param Translate $outputlangs Lang object for output language
  88. * @return int 1=OK, 0=KO
  89. */
  90. function write_file($socid = 0, $catid = 0, $outputlangs='')
  91. {
  92. global $user,$conf,$langs,$hookmanager;
  93. if (! is_object($outputlangs)) $outputlangs=$langs;
  94. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  95. if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
  96. // Load traductions files requiredby by page
  97. $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products"));
  98. $dir = $conf->agenda->dir_temp."/";
  99. $file = $dir . "actions-".$this->month."-".$this->year.".pdf";
  100. if (! file_exists($dir))
  101. {
  102. if (dol_mkdir($dir) < 0)
  103. {
  104. $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
  105. return 0;
  106. }
  107. }
  108. if (file_exists($dir))
  109. {
  110. // Add pdfgeneration hook
  111. if (! is_object($hookmanager))
  112. {
  113. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  114. $hookmanager=new HookManager($this->db);
  115. }
  116. $hookmanager->initHooks(array('pdfgeneration'));
  117. $parameters=array('file'=>$file, 'outputlangs'=>$outputlangs);
  118. global $action;
  119. $reshook=$hookmanager->executeHooks('beforePDFCreation',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  120. $pdf=pdf_getInstance($this->format);
  121. $heightforinfotot = 50; // Height reserved to output the info and total part
  122. $heightforfreetext= (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT)?$conf->global->MAIN_PDF_FREETEXT_HEIGHT:5); // Height reserved to output the free text on last page
  123. $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
  124. $pdf->SetAutoPageBreak(1,0);
  125. if (class_exists('TCPDF'))
  126. {
  127. $pdf->setPrintHeader(false);
  128. $pdf->setPrintFooter(false);
  129. }
  130. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  131. $pdf->Open();
  132. $pagenb=0;
  133. $pdf->SetDrawColor(128,128,128);
  134. $pdf->SetFillColor(220,220,220);
  135. $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
  136. $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
  137. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  138. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  139. $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
  140. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  141. $nbpage = $this->_pages($pdf, $outputlangs);
  142. if (method_exists($pdf,'AliasNbPages')) $pdf->AliasNbPages();
  143. $pdf->Close();
  144. $pdf->Output($file,'F');
  145. // Add pdfgeneration hook
  146. if (! is_object($hookmanager))
  147. {
  148. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  149. $hookmanager=new HookManager($this->db);
  150. }
  151. $hookmanager->initHooks(array('pdfgeneration'));
  152. $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
  153. global $action;
  154. $reshook=$hookmanager->executeHooks('afterPDFCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  155. if (! empty($conf->global->MAIN_UMASK))
  156. @chmod($file, octdec($conf->global->MAIN_UMASK));
  157. $this->result = array('fullpath'=>$file);
  158. return 1;
  159. }
  160. }
  161. /**
  162. * Write content of pages
  163. *
  164. * @param PDF $pdf Object pdf
  165. * @param Translate $outputlangs Object langs
  166. * @return int 1
  167. */
  168. function _pages(&$pdf, $outputlangs)
  169. {
  170. global $conf;
  171. $height=3; // height for text separation
  172. $pagenb=1;
  173. $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
  174. $y++;
  175. $pdf->SetFont('','',8);
  176. $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
  177. $sql.= " a.id, a.datep as dp, a.datep2 as dp2,";
  178. $sql.= " a.fk_contact, a.note, a.percent as percent, a.label, a.fk_project,";
  179. $sql.= " c.code, c.libelle,";
  180. $sql.= " u.login";
  181. $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
  182. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
  183. $sql.= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
  184. $sql.= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year,$this->month,false))."'";
  185. $sql.= " AND '".$this->db->idate(dol_get_last_day($this->year,$this->month,false))."'";
  186. $sql.= " AND a.entity = ".$conf->entity;
  187. $sql.= " ORDER BY a.datep DESC";
  188. $eventstatic=new ActionComm($this->db);
  189. $projectstatic=new Project($this->db);
  190. dol_syslog(get_class($this)."::_page", LOG_DEBUG);
  191. $resql=$this->db->query($sql);
  192. if ($resql)
  193. {
  194. $num = $this->db->num_rows($resql);
  195. $i = 0;
  196. $y0=$y1=$y2=$y3=0;
  197. while ($i < $num)
  198. {
  199. $obj = $this->db->fetch_object($resql);
  200. $eventstatic->id=$obj->id;
  201. $eventstatic->percentage=$obj->percent;
  202. $eventstatic->fulldayevent=$obj->fulldayevent;
  203. $eventstatic->punctual=$obj->punctual;
  204. $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
  205. // Calculate height of text
  206. $text='';
  207. if (! preg_match('/^'.preg_quote($obj->label).'/',$obj->note)) $text=$obj->label."\n";
  208. $text.=$obj->note;
  209. $text=dol_trunc(dol_htmlentitiesbr_decode($text),150);
  210. // Add status to text
  211. $text.="\n";
  212. $status=dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1,1));
  213. $text.=$status;
  214. if ($obj->fk_project > 0)
  215. {
  216. $projectstatic->fetch($obj->fk_project);
  217. $text.=($status?' - ':'').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->getNomUrl(0, 'nolink'));
  218. }
  219. //print 'd'.$text; exit;
  220. $nboflines=dol_nboflines($text);
  221. $heightlinemax=max(2*$height,$nboflines*$height);
  222. // Check if there is enough space to print record
  223. if ((1+$y+$heightlinemax) >= ($this->page_hauteur - $this->marge_haute))
  224. {
  225. // We need to break page
  226. $pagenb++;
  227. $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
  228. $y++;
  229. $pdf->SetFont('','',8);
  230. }
  231. $y++;
  232. // Date
  233. $pdf->SetXY($this->marge_gauche, $y);
  234. $textdate = dol_print_date($this->db->jdate($obj->dp),"day")."\n".dol_print_date($this->db->jdate($obj->dp),"hour");
  235. if ($obj->dp2) {
  236. if (dol_print_date($this->db->jdate($obj->dp),"day") != dol_print_date($this->db->jdate($obj->dp2),"day"))
  237. $textdate.= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
  238. else
  239. $textdate.= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
  240. }
  241. $pdf->MultiCell(22, $height, $textdate, 0, 'L', 0);
  242. $y0 = $pdf->GetY();
  243. // Third party
  244. $pdf->SetXY(26, $y);
  245. $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty),32), 0, 'L', 0);
  246. $y1 = $pdf->GetY();
  247. // Action code
  248. $code=$obj->code;
  249. if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
  250. {
  251. if ($code == 'AC_OTH') $code='AC_MANUAL';
  252. if ($code == 'AC_OTH_AUTO') $code='AC_AUTO';
  253. }
  254. $pdf->SetXY(60,$y);
  255. $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
  256. $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
  257. $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort),32), 0, 'L', 0);
  258. $y2 = $pdf->GetY();
  259. // Description of event
  260. $pdf->SetXY(106,$y);
  261. $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset($text), 0, 'L', 0);
  262. $y3 = $pdf->GetY();
  263. $i++;
  264. }
  265. }
  266. return 1;
  267. }
  268. /**
  269. * Show top header of page.
  270. *
  271. * @param PDF $pdf Object PDF
  272. * @param Translate $outputlangs Object lang for output
  273. * @param int $pagenb Page nb
  274. * @return integer
  275. */
  276. function _pagehead(&$pdf, $outputlangs, $pagenb)
  277. {
  278. global $conf,$langs;
  279. // Do not add the BACKGROUND as this is a report
  280. //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
  281. // New page
  282. $pdf->AddPage();
  283. // Show title
  284. $pdf->SetFont('','B',10);
  285. $pdf->SetXY($this->marge_gauche, $this->marge_haute);
  286. $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
  287. // Show page nb only on iso languages (so default Helvetica font)
  288. if (pdf_getPDFFont($outputlangs) == 'Helvetica')
  289. {
  290. $pdf->SetXY($this->page_largeur-$this->marge_droite-40, $this->marge_haute);
  291. $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
  292. }
  293. $y=$pdf->GetY()+2;
  294. $pdf->Rect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse));
  295. $y=$pdf->GetY()+1;
  296. return $y;
  297. }
  298. }