7 protected $csv_header = [
14 'Compte de débit - numéro',
15 'Compte de débit - libellé',
16 'Compte de crédit - numéro',
17 'Compte de crédit - libellé',
24 public function toCSV($exercice)
26 $db = DB
::getInstance();
28 $retro_comptatibility = 'SELECT count(montant) AS montants FROM compta_flux WHERE compta_flux.id_journal = journal.id';
32 strftime(\'%d/%m/%Y\', date) AS date,
33 (CASE cat.type WHEN 1 THEN \'Recette\' WHEN -1 THEN \'Dépense\' ELSE \'Autre\' END) AS type,
34 (CASE cat.intitule WHEN NULL THEN \'\' ELSE cat.intitule END),
36 abs(flux.montant) AS montant,
37 (CASE WHEN flux.montant >= 0 THEN flux.compte ELSE \'\' END),
38 (CASE WHEN flux.montant >= 0 THEN flux_compte.libelle ELSE \'\' END) AS compte_debit_libelle,
39 (CASE WHEN flux.montant < 0 THEN flux.compte ELSE \'\' END),
40 (CASE WHEN flux.montant < 0 THEN flux_compte.libelle ELSE \'\' END) AS compte_credit_libelle,
41 (CASE moyen_paiement WHEN NULL THEN \'\' ELSE moyen.nom END) AS moyen,
45 FROM compta_journal AS journal
46 LEFT JOIN compta_categories AS cat ON cat.id = journal.id_categorie
47 LEFT JOIN compta_moyens_paiement AS moyen ON moyen.code = journal.moyen_paiement
48 LEFT JOIN compta_flux AS flux ON flux.id_journal = journal.id
49 LEFT JOIN compta_comptes AS flux_compte ON flux_compte.id = flux.compte
50 WHERE id_exercice = '.(int)$exercice.'
54 $fp = fopen('php://output', 'w');
56 fputcsv($fp, $this->csv_header
);
58 while ($row = $res->fetchArray(SQLITE3_ASSOC
))
68 public function fromCSV($path)
70 if (!file_exists($path) ||
!is_readable($path))
72 throw new \
RuntimeException('Fichier inconnu : '.$path);
75 $fp = fopen($path, 'r');
82 $db = DB
::getInstance();
84 $comptes = new Compta_Comptes
;
85 $banques = new Compta_Comptes_Bancaires
;
86 $cats = new Compta_Categories
;
87 $journal = new Compta_Journal
;
89 $columns = array_flip($this->csv_header
);
90 $liste_comptes = $db->simpleStatementFetchAssoc('SELECT id, id FROM compta_comptes;');
91 $liste_cats = $db->simpleStatementFetchAssoc('SELECT intitule, id FROM compta_categories;');
92 $liste_moyens = $cats->listMoyensPaiement();
94 $col = function($column) use (&$row, &$columns)
96 if (!isset($columns[$column]))
99 if (!isset($row[$columns[$column]]))
102 return $row[$columns[$column]];
108 $delim = utils
::find_csv_delim($fp);
113 $row = fgetcsv($fp, 4096, $delim);
114 print_r(["row" => $row]);
124 if (trim($row[0]) != 'Numéro mouvement')
126 throw new UserException('Erreur sur la ligne ' . $line . ' : l\'entête des colonnes est absent ou incorrect.');
132 if (count($row) != count($columns))
134 $db->exec('ROLLBACK;');
135 throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.');
138 if (trim($row[0]) !== '' && !is_numeric($row[0]))
140 $db->exec('ROLLBACK;');
141 throw new UserException('Erreur sur la ligne ' . $line . ' : la première colonne doit être vide ou contenir le numéro unique d\'opération.');
145 $id = $col('Numéro mouvement');
148 $montant = $col('Montant');
149 $debit = $col('Compte de débit - numéro');
150 $credit = $col('Compte de crédit - numéro');
152 if (trim($debit) == '' && trim($credit) != '')
156 [ 'compte' => $credit
157 , 'montant' => - $montant
160 if (trim($debit) != '' && trim($credit) == '')
165 , 'montant' => $montant
172 // NOTE: import previously collected $last_id
173 $data['fluxs'] = $fluxs;
174 $id_journal = $db->simpleQuerySingle('SELECT id FROM compta_journal WHERE id = ?;', false, $last_id);
175 if (empty($last_id) ||
empty($id_journal))
177 print_r(["data" => $data]);
178 $journal->add($data);
182 print_r(['id' => $last_id, "data" => $data]);
183 $journal->edit($last_id, $data);
188 // NOTE: start collecting $id
190 $date = $col('Date');
192 if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date))
194 $db->exec('ROLLBACK;');
195 throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.');
198 $date = explode('/', $date);
199 $date = $date[2] . '-' . $date[1] . '-' . $date[0];
201 // En dehors de l'exercice courant
202 if ($db->simpleQuerySingle('SELECT 1 FROM compta_exercices
203 WHERE (? < debut OR ? > fin) AND cloture = 0;', false, $date, $date))
208 $cat = $col('Catégorie');
209 $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2));
211 if (!$moyen ||
!array_key_exists($moyen, $liste_moyens))
217 if ($cat && !array_key_exists($cat, $liste_cats))
219 $cat = $moyen = false;
222 $montant = $col('Montant');
223 $debit = $col('Compte de débit - numéro');
224 $credit = $col('Compte de crédit - numéro');
226 if (trim($debit) == '' && trim($credit) != '')
230 [ 'compte' => $credit
231 , 'montant' => - $montant
234 if (trim($debit) != '' && trim($credit) == '')
239 , 'montant' => $montant
244 'libelle' => $col('Libellé'),
246 'numero_piece' => $col('Numéro de pièce'),
247 'remarques' => $col('Remarques'),
252 $data['moyen_paiement'] = $moyen;
253 $data['numero_cheque'] = $col('Numéro de chèque');
254 $data['id_categorie'] = $liste_cats[$cat];
259 // NOTE: import previously collected $id
260 $data['fluxs'] = $fluxs;
261 $id_journal = $db->simpleQuerySingle('SELECT id FROM compta_journal WHERE id = ?;', false, $id);
262 if (empty($id) ||
empty($id_journal))
264 print_r(["data" => $data]);
265 $journal->add($data);
269 print_r(['id (end)' => $id, "data" => $data]);
270 $journal->edit($id, $data);
281 public function fromCitizen($path)
283 if (!file_exists($path) ||
!is_readable($path))
285 throw new \
RuntimeException('Fichier inconnu : '.$path);
288 $fp = fopen($path, 'r');
295 $db = DB
::getInstance();
297 $comptes = new Compta_Comptes
;
298 $banques = new Compta_Comptes_Bancaires
;
299 $cats = new Compta_Categories
;
300 $journal = new Compta_Journal
;
303 $liste_comptes = $db->simpleStatementFetchAssoc('SELECT id, id FROM compta_comptes;');
304 $liste_cats = $db->simpleStatementFetchAssoc('SELECT intitule, id FROM compta_categories;');
305 $liste_moyens = $cats->listMoyensPaiement();
307 $get_compte = function ($compte, $intitule) use (&$liste_comptes, &$comptes, &$banques)
309 if (substr($compte, 0, 2) == '51')
311 $compte = '512' . substr($compte, -1);
315 if (!array_key_exists($compte, $liste_comptes))
317 if (substr($compte, 0, 3) == '512')
319 $liste_comptes[$compte] = $banques->add([
320 'libelle' => $intitule,
321 'banque' => 'Inconnue',
326 $liste_comptes[$compte] = $comptes->add([
328 'libelle' => $intitule,
329 'parent' => substr($compte, 0, -1)
337 $col = function($column) use (&$row, &$columns)
339 if (!isset($columns[$column]))
342 if (!isset($row[$columns[$column]]))
345 return $row[$columns[$column]];
349 $delim = utils
::find_csv_delim($fp);
353 $row = fgetcsv($fp, 4096, $delim);
364 $columns = array_flip($columns);
368 $date = $col('Date');
370 if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date))
372 $db->exec('ROLLBACK;');
373 throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.');
376 $date = explode('/', $date);
377 $date = $date[2] . '-' . $date[1] . '-' . $date[0];
379 if ($db->simpleQuerySingle('SELECT 1 FROM compta_exercices
380 WHERE (? < debut OR ? > fin) AND cloture = 0;', false, $date, $date))
385 $debit = $get_compte($col('Compte débité - Numéro'), $col('Compte débité - Intitulé'));
386 $credit = $get_compte($col('Compte crédité - Numéro'), $col('Compte crédité - Intitulé'));
388 $cat = $col('Rubrique');
389 $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2));
391 if (!$moyen ||
!array_key_exists($moyen, $liste_moyens))
397 if ($cat && !array_key_exists($cat, $liste_cats))
399 if ($col('Nature') == 'Recette')
401 $type = $cats::RECETTES
;
404 elseif ($col('Nature') == 'Dépense')
406 $type = $cats::DEPENSES
;
411 $type = $cats::AUTRES
;
415 if ($type != $cats::AUTRES
)
417 $liste_cats[$cat] = $cats->add([
426 'libelle' => $col('Libellé'),
427 'montant' => $col('Montant'),
429 'compte_credit' => $credit,
430 'compte_debit' => $debit,
431 'numero_piece' => $col('Numéro de pièce'),
432 'remarques' => $col('Remarques'),
437 $data['moyen_paiement'] = $moyen;
438 $data['numero_cheque'] = $col('Numéro de chèque');
439 $data['id_categorie'] = $liste_cats[$cat];
442 $journal->add($data);