-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
330 lines (251 loc) · 9.26 KB
/
import.php
File metadata and controls
330 lines (251 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
require_once(dirname(__FILE__) .'/inc/init.inc.php');
//header('Content-Type: application/json; charset=UTF-8');
$response = array();
//cancel previous import by ID
if(isset($_GET['undoid']) && $_GET['undoid'] != ''){
$q = "SELECT COUNT(*) AS nb_transactions FROM ".db_table_name('transactions')." WHERE id_import = '".$_GET['undoid']."'";
$res = mysql_query($q);
$result = mysql_fetch_assoc($res);
$nb_res = $result['nb_transactions'];
$q = "DELETE FROM ".db_table_name('transactions')." WHERE id_import = '".$_GET['undoid']."'";
$res = mysql_query($q);
if (!$res) {
//throw new MyException('Invalid query: '.$q . mysql_error());
$response['error'] = _("Unable to delete transactions")." !! $q";
$response['status'] = false;
echo json_encode($response);
exit;
}
$response['nb_transactions'] = $nb_res;
$response['status'] = true;
echo json_encode($response);
exit;
}
if(count($_FILES) != 1){
//header("Location:afficher.php?import=ko&error=no_file#".$_POST['id_compte']);
$response['error'] = _("No file to process")." !!";
$response['status'] = false;
echo json_encode($response);
exit;
}
if ($_FILES["file"]["error"] > 0){
//throw new MyException('Erreur : ' . $_FILES["file"]["error"]);
$response['error'] = _("Unable to process file")." : ".$_FILES["file"]["error"]." !!";
$response['status'] = false;
echo json_encode($response);
exit;
}
//recupération du compte
try{
$c = new Compte($_GET['idc']);
}catch(Exception $e){
$response['error'] = _("Account not found")." : " . $e->getMessage();
$response['status'] = false;
echo json_encode($response);
exit;
}
//debug($c);
//exit;
//détection du type de traitement à appliquer
switch($_FILES['file']['type']){
case 'application/vnd.ms-excel':
case 'application/x-ecriture-txt':
$file_type = 'CSV';
break;
case 'application/octet-stream':
case 'application/x-ecriture-qif':
//if(strtolower(end(explode('.', $_FILES['file']['name']))) == 'qif'){
$file_type = 'QIF';
//}if(strtolower(end(explode('.', $_FILES['file']['name']))) == 'csv'){
// $file_type = 'CSV';
//}else{
//}
break;
default :
break;
}
switch($file_type){
case 'CSV':
$csv_file_path = $_FILES['file']['tmp_name'];
$import_id = md5($csv_file_path.time());
$possible_duplicates = array(); // transaction deja existantes
$nb_transac = 0;
if (($handle = fopen("$csv_file_path", "r")) !== FALSE) {
//saute 2 lignes
$data = fgetcsv($handle, 1000, ",");
$data = fgetcsv($handle, 1000, ",");
//test type import SG (carte ou compte)
$data = fgetcsv($handle, 1000, ",");
if($data[0] == "date d'effet"){ //import d'un fichier CARTE
$fichier_carte = true;
}else{
$fichier_carte = false;
}
$date_min_transac = '2081-10-26';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
$t = new Transaction();
$t->id_compte = $c->id;
$t->libelle = utf8_encode($data[2]);
$t->montant = str_replace(',', '.', $data[3]);
$t->devise = $data[4];
if($fichier_carte){
$t->date_transaction = substr($data[1],6,4)."-".substr($data[1],3,2)."-".substr($data[1],0,2);
}else{
$t->date_transaction = substr($data[0],6,4)."-".substr($data[0],3,2)."-".substr($data[0],0,2);
}
if($t->date_transaction < $date_min_transac) $date_min_transac = $t->date_transaction;
//retraitement date des opérations CARTE VISA
$t->id_import = $import_id;
try{
$t->ajouter();
$c->transactions[] = $t;
}catch(DuplicateTransactionException $de){
$possible_duplicates[] = $t;
}catch(Exception $e){
$response['error'] = _("Error adding transaction, import canceled")." : " . $e->getMessage();
$response['status'] = false;
$q = "DELETE FROM ".db_table_name('transactions')." WHERE id_import = '".$import_id."'";
$res = mysql_query($q);
if (!$res) {
//throw new MyException('Invalid query: '.$q . mysql_error());
$response['error'] .= _("Unable to delete transaction")." (".count($c->transactions)." "._("transaction(s) imported"). ") !! $q";
$response['status'] = false;
}
echo json_encode($response);
exit;
}
}
fclose($handle);
try{
$c->getLatestTransactionDate();
$c->getLatestImportDate();
$c->actualiserSolde();
if($c->is_synthese){
$c->setTransactionsFutures();
}
$c->transactions = array_reverse($c->transactions);
$possible_duplicates = array_reverse($possible_duplicates);
$response['status'] = true;
$response['nb_transactions'] = count($c->transactions);
$response['nb_duplicates'] = count($possible_duplicates);
$response['compte'] = $c;
$response['duplicates'] = $possible_duplicates;
$response['import_id'] = $import_id;
}catch(Exception $e){
$response['error'] = _("Error when updating account data")." : " + $e->getMessage();
$response['status'] = false;
echo json_encode($response);
exit;
}
}else{
//throw new MyException("impossible d'ouvrir le fichier $csv_file_path");
$response['status'] = false;
$response['error'] = _("Impossible to open file")." !!";
}
break;
case 'QIF':
//function to get values from QIF file
function get_qif_value($tag, $trn_str){
foreach($trn_str as $line){
if(substr($line,0,1) == $tag){
return trim(substr($line,1,strlen($line)-1));
}
}
}
$qif_file_path = $_FILES['file']['tmp_name'];
$import_id = md5($qif_file_path.time());
$possible_duplicates = array(); // transaction deja existantes
$nb_transac = 0;
if (($handle = fopen($qif_file_path, "r")) !== FALSE) {
$data = fread( $handle, filesize($qif_file_path) );
fclose($handle);
//http://forums.devshed.com/php-development-5/parse-financial-ofx-qfx-qif-file-556764-2.html
if(substr_count(strtolower($data),'!type:bank') == 1 || substr_count(strtolower($data),'!type:ccard') == 1){
//QIF file import
$nb_trn = substr_count($data,"^");
$start_trn_str = strpos($data,"\nD");
$len_trn_str = strpos($data,"^",$start_trn_str) - $start_trn_str;
$i = 0;
$cpt_duplicates = 0;
$transactions = array();
while($i < $nb_trn){
$t = new Transaction();
$t->id_compte = $c->id;
$trn_str = explode("\n",substr($data,$start_trn_str,$len_trn_str));
$t->montant= str_replace(',','',str_replace('+','',get_qif_value('T', $trn_str)));
if(get_qif_value('M', $trn_str) != ''){
$t->libelle= utf8_encode(get_qif_value('M', $trn_str));
}else{
$t->libelle= utf8_encode(get_qif_value('P', $trn_str));
}
$t_date = explode('/', get_qif_value('D', $trn_str));
if(strlen(end($t_date)) == 2 ) $t_date[2] = '20'.$t_date[2];
if(checkdate($t_date[1], $t_date[0], $t_date[2])){
$t->date_transaction = $t_date[2]."-".$t_date[1]."-".$t_date[0];
}else{
$t->date_transaction = "0000-00-00";
}
$t->id_import = $import_id;
try{
$t->ajouter();
$c->transactions[] = $t;
}catch(DuplicateTransactionException $de){
$cpt_duplicates++;
$possible_duplicates[$cpt_duplicates - 1]['transaction'] = new Transaction($t->getDuplicateID());
$possible_duplicates[$cpt_duplicates - 1]['duplicate'] = $t;
}catch(Exception $e){
$response['error'] = _("Error adding transaction, import canceled")." : " . $e->getMessage();
$response['status'] = false;
$q = "DELETE FROM ".db_table_name('transactions')." WHERE id_import = '".$import_id."'";
$res = mysql_query($q);
if (!$res) {
//throw new MyException('Invalid query: '.$q . mysql_error());
$response['error'] .= _("Unable to delete transactions")." !! $q";
$response['status'] = false;
}
echo json_encode($response);
exit;
}
$start_trn_str = strpos($data,"\nD",$start_trn_str +2);
$len_trn_str = strpos($data,"^",$start_trn_str) - $start_trn_str;
$i++;
}
try{
$c->getLatestTransactionDate();
$c->getLatestImportDate();
$c->actualiserSolde();
if($c->is_synthese){
$c->setTransactionsFutures();
}
$c->transactions = array_reverse($c->transactions);
$response['status'] = true;
$response['nb_transactions'] = count($c->transactions);
$response['compte'] = $c;
$response['duplicates'] = $possible_duplicates;
$response['nb_duplicates'] = $cpt_duplicates;
$response['import_id'] = $import_id;
}catch(Exception $e){
$response['error'] = _("Error when updating account data")." : " + $e->getMessage();
$response['status'] = false;
echo json_encode($response);
exit;
}
}else{
$response['status'] = false;
$response['error'] = _("QIF file format incorrect")." !!";
}
}else{
//throw new MyException( "impossible d'ouvrir le fichier ".$qif_file_path);;
$response['status'] = false;
$response['error'] = _("Impossible to open file")." !!";
}
break;
default:
//header("Location:afficher.php?import=ko&error=file_type#".$_POST['id_compte']);
$response['status'] = false;
$response['error'] = _("File format incompatible with import feature")." (".$_FILES['file']['type'].") !!";
}
echo json_encode($response);
?>