export_csv.php 5.8 KB

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