rapport.class.php 12 KB

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