rapport.class.php 12 KB

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