export_csv.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/expensereport/index.php
  20. * \ingroup expensereport
  21. * \brief Page list of expenses
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
  25. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
  26. // Load translation files required by the page
  27. $langs->loadlangs(array('users', 'trips'));
  28. if(!$user->rights->expensereport->export_csv) {
  29. accessforbidden();
  30. exit();
  31. }
  32. // Security check
  33. $socid = $_GET["socid"]?$_GET["socid"]:'';
  34. if ($user->societe_id) $socid=$user->societe_id;
  35. $result = restrictedArea($user, 'expensereport','','');
  36. $req = "SELECT * FROM ".MAIN_DB_PREFIX."rights_def WHERE id = '178'";
  37. $result = $db->query($req);
  38. $num = $db->num_rows($result);
  39. if($num < 1) {
  40. $insert = "INSERT INTO ".MAIN_DB_PREFIX."rights_def (";
  41. $insert.= "`id` ,";
  42. $insert.= "`libelle` ,";
  43. $insert.= "`module` ,";
  44. $insert.= "`entity` ,";
  45. $insert.= "`perms` ,";
  46. $insert.= "`subperms` ,";
  47. $insert.= "`type` ,";
  48. $insert.= "`bydefault`";
  49. $insert.= ")";
  50. $insert.= "VALUES (";
  51. $insert.= "'178', 'Exporter les notes de frais au format CSV', 'expensereport', '1', 'export_csv', NULL , 'r', '0'";
  52. $insert.= ")";
  53. $req = $db->query($insert);
  54. }
  55. /*
  56. * View
  57. */
  58. llxHeader();
  59. print load_fiche_titre($langs->trans("ExportTripCSV"));
  60. print '<div class="tabBar">';
  61. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
  62. print '<input type="hidden" name="action" value="export"/>';
  63. print '<p>Choisir le mois à exporter : ';
  64. $year = date('Y', time());
  65. $month = date('m', time());
  66. print '<select name="mois">';
  67. for($i=1;$i<13;$i++) {
  68. $mois = str_pad($i, 2, "0", STR_PAD_LEFT);
  69. if($month == $mois) {
  70. print '<option value="'.$mois.'" selected>'.$mois.'</option>';
  71. } else {
  72. print '<option value="'.$mois.'">'.$mois.'</option>';
  73. }
  74. }
  75. print '</select> ';
  76. print '<select name="annee">';
  77. for($i=2009;$i<$year+1;$i++) {
  78. if($year == $i) {
  79. print '<option value="'.$i.'" selected>'.$i.'</option>';
  80. } else {
  81. print '<option value="'.$i.'">'.$i.'</option>';
  82. }
  83. }
  84. print '</select> ';
  85. print '<input type="submit" class="button" value="Exporter" />';
  86. print '</p>';
  87. print '</form>'."\n";
  88. // Si c'est une action
  89. if (isset($_POST['action']))
  90. {
  91. if($_POST['action'] == 'export')
  92. {
  93. $dateselected = $_POST['annee'].'-'.$_POST['mois'];
  94. //var_dump($conf->expensereport->dir_output.'/export/');
  95. if (!file_exists($conf->expensereport->dir_output.'/export/'))
  96. {
  97. dol_mkdir($conf->expensereport->dir_output.'/export/');
  98. }
  99. $dir = $conf->expensereport->dir_output.'/export/expensereport-'.$dateselected.'.csv';
  100. $outputlangs = $langs;
  101. $outputlangs->charset_output = 'UTF-8';
  102. $sql = "SELECT d.rowid, d.ref, d.total_ht, d.total_tva, d.total_ttc";
  103. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as d";
  104. $sql.= ' AND d.entity IN ('.getEntity('expensereport').')';
  105. $sql.= " ORDER BY d.rowid";
  106. $result = $db->query($sql);
  107. $num = $db->num_rows($result);
  108. if ($num)
  109. {
  110. $open = fopen($dir,"w+");
  111. $ligne = "ID, Référence, ----, Date paiement, Montant HT, TVA, Montant TTC\n";
  112. for ($i = 0; $i < $num; $i++)
  113. {
  114. $ligne.= "----, ----, ----, ----, ----, ----, ----\n";
  115. $objet = $db->fetch_object($result);
  116. $objet->total_ht = number_format($objet->total_ht,2);
  117. $objet->total_tva = number_format($objet->total_tva,2);
  118. $objet->total_ttc = number_format($objet->total_ttc,2);
  119. $objet->ref = trim($objet->ref);
  120. $ligne.= "{$objet->rowid}, {$objet->ref}, ----, {$objet->total_ht}, {$objet->total_tva}, {$objet->total_ttc}\n";
  121. $ligne.= "--->, Ligne, Type, Description, ----, ----, ----\n";
  122. $sql2 = "SELECT de.rowid, t.label as libelle, de.comments, de.total_ht, de.total_tva, de.total_ttc";
  123. $sql2.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de,";
  124. $sql2.= " ".MAIN_DB_PREFIX."c_type_fees as t";
  125. $sql2.= " WHERE de.fk_c_type_fees = t.id";
  126. $sql2.= " AND de.fk_expensereport = '".$objet->rowid."'";
  127. $sql2.= " ORDER BY de.date";
  128. $result2 = $db->query($sql2);
  129. $num2 = $db->num_rows($result2);
  130. if($num2) {
  131. for ($a = 0; $a < $num2; $a++)
  132. {
  133. $objet2 = $db->fetch_object($result2);
  134. $objet2->total_ht = number_format($objet2->total_ht,2);
  135. $objet2->total_tva = number_format($objet2->total_tva,2);
  136. $objet2->total_ttc = number_format($objet2->total_ttc,2);
  137. $objet2->comments = str_replace(',',';',$objet2->comments);
  138. $objet2->comments = str_replace("\r\n",' ',$objet2->comments);
  139. $objet2->comments = str_replace("\n",' ',$objet2->comments);
  140. $ligne.= "--->, {$objet2->rowid}, {$objet2->libelle}, {$objet2->comments}, {$objet2->total_ht}, {$objet2->total_tva}, {$objet2->total_ttc}\n";
  141. }
  142. }
  143. }
  144. $ligne = $outputlangs->convToOutputCharset($ligne);
  145. fwrite($open,$ligne);
  146. fclose($open);
  147. print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart=expensereport&file=export%2Fexpensereport-'.$dateselected.'.csv" target="_blank">Télécharger le fichier expensereport-'.$dateselected.'.csv</a>';
  148. } else {
  149. print '<b>'.$langs->trans('NoTripsToExportCSV').'</b>';
  150. }
  151. }
  152. }
  153. print '</div>';
  154. // End of page
  155. llxFooter();
  156. $db->close();