dolibarr  7.0.0-beta
export_csv.php
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 
24 require '../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
26 require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
27 
28 $langs->load("users");
29 $langs->load("trips");
30 
31 if(!$user->rights->expensereport->export_csv) {
33  exit();
34 }
35 
36 // Security check
37 $socid = $_GET["socid"]?$_GET["socid"]:'';
38 if ($user->societe_id) $socid=$user->societe_id;
39 $result = restrictedArea($user, 'expensereport','','');
40 
41 $req = "SELECT * FROM ".MAIN_DB_PREFIX."rights_def WHERE id = '178'";
42 $result = $db->query($req);
43 $num = $db->num_rows($result);
44 
45 if($num < 1) {
46 
47  $insert = "INSERT INTO ".MAIN_DB_PREFIX."rights_def (";
48  $insert.= "`id` ,";
49  $insert.= "`libelle` ,";
50  $insert.= "`module` ,";
51  $insert.= "`entity` ,";
52  $insert.= "`perms` ,";
53  $insert.= "`subperms` ,";
54  $insert.= "`type` ,";
55  $insert.= "`bydefault`";
56  $insert.= ")";
57  $insert.= "VALUES (";
58  $insert.= "'178', 'Exporter les notes de frais au format CSV', 'expensereport', '1', 'export_csv', NULL , 'r', '0'";
59  $insert.= ")";
60 
61  $req = $db->query($insert);
62 
63 }
64 
65 
66 /*
67  * View
68  */
69 
70 llxHeader();
71 
72 print load_fiche_titre($langs->trans("ExportTripCSV"));
73 
74 print '<div class="tabBar">';
75 
76 print '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
77 print '<input type="hidden" name="action" value="export"/>';
78 print '<p>Choisir le mois à exporter : ';
79 
80 $year = date('Y', time());
81 $month = date('m', time());
82 
83 print '<select name="mois">';
84 
85 for($i=1;$i<13;$i++) {
86  $mois = str_pad($i, 2, "0", STR_PAD_LEFT);
87  if($month == $mois) {
88  print '<option value="'.$mois.'" selected>'.$mois.'</option>';
89  } else {
90  print '<option value="'.$mois.'">'.$mois.'</option>';
91  }
92 }
93 
94 print '</select> ';
95 
96 print '<select name="annee">';
97 
98 for($i=2009;$i<$year+1;$i++) {
99  if($year == $i) {
100  print '<option value="'.$i.'" selected>'.$i.'</option>';
101  } else {
102  print '<option value="'.$i.'">'.$i.'</option>';
103  }
104 }
105 
106 print '</select> ';
107 
108 print '<input type="submit" class="button" value="Exporter" />';
109 print '</p>';
110 print '</form>'."\n";
111 
112 // Si c'est une action
113 if (isset($_POST['action']))
114 {
115  if($_POST['action'] == 'export')
116  {
117  $select_date = $_POST['annee'].'-'.$_POST['mois'];
118 
119  //var_dump($conf->expensereport->dir_output.'/export/');
120  if (!file_exists($conf->expensereport->dir_output.'/export/'))
121  {
122  dol_mkdir($conf->expensereport->dir_output.'/export/');
123  }
124 
125  $dir = $conf->expensereport->dir_output.'/export/expensereport-'.$select_date.'.csv';
126  $outputlangs = $langs;
127  $outputlangs->charset_output = 'UTF-8';
128 
129  $sql = "SELECT d.rowid, d.ref, d.total_ht, d.total_tva, d.total_ttc";
130  $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as d";
131  $sql.= ' AND d.entity IN ('.getEntity('expensereport').')';
132  $sql.= " ORDER BY d.rowid";
133 
134  $result = $db->query($sql);
135  $num = $db->num_rows($result);
136  if ($num)
137  {
138  $open = fopen($dir,"w+");
139 
140  $ligne = "ID, Référence, ----, Date paiement, Montant HT, TVA, Montant TTC\n";
141  for ($i = 0; $i < $num; $i++)
142  {
143  $ligne.= "----, ----, ----, ----, ----, ----, ----\n";
144  $objet = $db->fetch_object($result);
145  $objet->total_ht = number_format($objet->total_ht,2);
146  $objet->total_tva = number_format($objet->total_tva,2);
147  $objet->total_ttc = number_format($objet->total_ttc,2);
148  $objet->ref = trim($objet->ref);
149  $ligne.= "{$objet->rowid}, {$objet->ref}, ----, {$objet->total_ht}, {$objet->total_tva}, {$objet->total_ttc}\n";
150 
151  $ligne.= "--->, Ligne, Type, Description, ----, ----, ----\n";
152 
153 
154  $sql2 = "SELECT de.rowid, t.label as libelle, de.comments, de.total_ht, de.total_tva, de.total_ttc";
155  $sql2.= " FROM ".MAIN_DB_PREFIX."expensereport_det as de,";
156  $sql2.= " ".MAIN_DB_PREFIX."c_type_fees as t";
157  $sql2.= " WHERE de.fk_c_type_fees = t.id";
158  $sql2.= " AND de.fk_expensereport = '".$objet->rowid."'";
159  $sql2.= " ORDER BY de.date";
160 
161  $result2 = $db->query($sql2);
162  $num2 = $db->num_rows($result2);
163 
164  if($num2) {
165  for ($a = 0; $a < $num2; $a++)
166  {
167  $objet2 = $db->fetch_object($result2);
168  $objet2->total_ht = number_format($objet2->total_ht,2);
169  $objet2->total_tva = number_format($objet2->total_tva,2);
170  $objet2->total_ttc = number_format($objet2->total_ttc,2);
171  $objet2->comments = str_replace(',',';',$objet2->comments);
172  $objet2->comments = str_replace("\r\n",' ',$objet2->comments);
173  $objet2->comments = str_replace("\n",' ',$objet2->comments);
174 
175  $ligne.= "--->, {$objet2->rowid}, {$objet2->libelle}, {$objet2->comments}, {$objet2->total_ht}, {$objet2->total_tva}, {$objet2->total_ttc}\n";
176  }
177  }
178 
179  }
180 
181  $ligne = $outputlangs->convToOutputCharset($ligne);
182 
183  fwrite($open,$ligne);
184  fclose($open);
185 
186  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>';
187 
188  } else {
189 
190  print '<b>'.$langs->trans('NoTripsToExportCSV').'</b>';
191 
192  }
193  }
194 }
195 
196 print '</div>';
197 
198 llxFooter();
199 
200 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
print
Draft customers invoices.
Definition: index.php:91
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.