73fd99b2e5c1b80d4cd52bd591c3115f9b7ba125
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 $res = $db->prepare('SELECT
30 strftime(\'%d/%m/%Y\', date) AS date,
31 (CASE cat.type WHEN 1 THEN \'Recette\' WHEN -1 THEN \'Dépense\' ELSE \'Autre\' END) AS type,
32 (CASE cat.intitule WHEN NULL THEN \'\' ELSE cat.intitule END) AS cat,
36 debit.libelle AS libelle_debit,
38 credit.libelle AS libelle_credit,
39 (CASE moyen_paiement WHEN NULL THEN \'\' ELSE moyen.nom END) AS moyen,
43 FROM compta_journal AS journal
44 LEFT JOIN compta_categories AS cat ON cat.id = journal.id_categorie
45 LEFT JOIN compta_comptes AS debit ON debit.id = journal.compte_debit
46 LEFT JOIN compta_comptes AS credit ON credit.id = journal.compte_credit
47 LEFT JOIN compta_moyens_paiement AS moyen ON moyen.code = journal.moyen_paiement
48 WHERE id_exercice = '.(int)$exercice.'
49 ORDER BY journal.date;
52 $fp = fopen('php://output', 'w');
54 fputcsv($fp, $this->csv_header
);
56 while ($row = $res->fetchArray(SQLITE3_ASSOC
))
66 public function fromCSV($path)
68 if (!file_exists($path) ||
!is_readable($path))
70 throw new \
RuntimeException('Fichier inconnu : '.$path);
73 $fp = fopen($path, 'r');
80 $db = DB
::getInstance();
82 $comptes = new Compta_Comptes
;
83 $banques = new Compta_Comptes_Bancaires
;
84 $cats = new Compta_Categories
;
85 $journal = new Compta_Journal
;
87 $columns = array_flip($this->csv_header
);
88 $liste_comptes = $db->simpleStatementFetchAssoc('SELECT id, id FROM compta_comptes;');
89 $liste_cats = $db->simpleStatementFetchAssoc('SELECT intitule, id FROM compta_categories;');
90 $liste_moyens = $cats->listMoyensPaiement();
92 $col = function($column) use (&$row, &$columns)
94 if (!isset($columns[$column]))
97 if (!isset($row[$columns[$column]]))
100 return $row[$columns[$column]];
104 $delim = utils
::find_csv_delim($fp);
108 $row = fgetcsv($fp, 4096, $delim);
118 if (trim($row[0]) != 'Numéro mouvement')
120 throw new UserException('Erreur sur la ligne ' . $line . ' : l\'entête des colonnes est absent ou incorrect.');
126 if (count($row) != count($columns))
128 $db->exec('ROLLBACK;');
129 throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.');
132 if (trim($row[0]) !== '' && !is_numeric($row[0]))
134 $db->exec('ROLLBACK;');
135 throw new UserException('Erreur sur la ligne ' . $line . ' : la première colonne doit être vide ou contenir le numéro unique d\'opération.');
138 $id = $col('Numéro mouvement');
139 $date = $col('Date');
141 if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date))
143 $db->exec('ROLLBACK;');
144 throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.');
147 $date = explode('/', $date);
148 $date = $date[2] . '-' . $date[1] . '-' . $date[0];
150 // En dehors de l'exercice courant
151 if ($db->simpleQuerySingle('SELECT 1 FROM compta_exercices
152 WHERE (? < debut OR ? > fin) AND cloture = 0;', false, $date, $date))
157 $debit = $col('Compte de débit - numéro');
158 $credit = $col('Compte de crédit - numéro');
160 if (trim($debit) == '' && trim($credit) != '')
164 elseif (trim($debit) != '' && trim($credit) == '')
169 $cat = $col('Catégorie');
170 $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2));
172 if (!$moyen ||
!array_key_exists($moyen, $liste_moyens))
178 if ($cat && !array_key_exists($cat, $liste_cats))
180 $cat = $moyen = false;
184 'libelle' => $col('Libellé'),
185 'montant' => (float) $col('Montant'),
187 'compte_credit' => $credit,
188 'compte_debit' => $debit,
189 'numero_piece' => $col('Numéro de pièce'),
190 'remarques' => $col('Remarques'),
195 $data['moyen_paiement'] = $moyen;
196 $data['numero_cheque'] = $col('Numéro de chèque');
197 $data['id_categorie'] = $liste_cats[$cat];
202 $journal->add($data);
206 $journal->edit($id, $data);
216 public function fromCitizen($path)
218 if (!file_exists($path) ||
!is_readable($path))
220 throw new \
RuntimeException('Fichier inconnu : '.$path);
223 $fp = fopen($path, 'r');
230 $db = DB
::getInstance();
232 $comptes = new Compta_Comptes
;
233 $banques = new Compta_Comptes_Bancaires
;
234 $cats = new Compta_Categories
;
235 $journal = new Compta_Journal
;
238 $liste_comptes = $db->simpleStatementFetchAssoc('SELECT id, id FROM compta_comptes;');
239 $liste_cats = $db->simpleStatementFetchAssoc('SELECT intitule, id FROM compta_categories;');
240 $liste_moyens = $cats->listMoyensPaiement();
242 $get_compte = function ($compte, $intitule) use (&$liste_comptes, &$comptes, &$banques)
244 if (substr($compte, 0, 2) == '51')
246 $compte = '512' . substr($compte, -1);
250 if (!array_key_exists($compte, $liste_comptes))
252 if (substr($compte, 0, 3) == '512')
254 $liste_comptes[$compte] = $banques->add([
255 'libelle' => $intitule,
256 'banque' => 'Inconnue',
261 $liste_comptes[$compte] = $comptes->add([
263 'libelle' => $intitule,
264 'parent' => substr($compte, 0, -1)
272 $col = function($column) use (&$row, &$columns)
274 if (!isset($columns[$column]))
277 if (!isset($row[$columns[$column]]))
280 return $row[$columns[$column]];
284 $delim = utils
::find_csv_delim($fp);
288 $row = fgetcsv($fp, 4096, $delim);
299 $columns = array_flip($columns);
303 $date = $col('Date');
305 if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date))
307 $db->exec('ROLLBACK;');
308 throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.');
311 $date = explode('/', $date);
312 $date = $date[2] . '-' . $date[1] . '-' . $date[0];
314 if ($db->simpleQuerySingle('SELECT 1 FROM compta_exercices
315 WHERE (? < debut OR ? > fin) AND cloture = 0;', false, $date, $date))
320 $debit = $get_compte($col('Compte débité - Numéro'), $col('Compte débité - Intitulé'));
321 $credit = $get_compte($col('Compte crédité - Numéro'), $col('Compte crédité - Intitulé'));
323 $cat = $col('Rubrique');
324 $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2));
326 if (!$moyen ||
!array_key_exists($moyen, $liste_moyens))
332 if ($cat && !array_key_exists($cat, $liste_cats))
334 if ($col('Nature') == 'Recette')
336 $type = $cats::RECETTES
;
339 elseif ($col('Nature') == 'Dépense')
341 $type = $cats::DEPENSES
;
346 $type = $cats::AUTRES
;
350 if ($type != $cats::AUTRES
)
352 $liste_cats[$cat] = $cats->add([
361 'libelle' => $col('Libellé'),
362 'montant' => $col('Montant'),
364 'compte_credit' => $credit,
365 'compte_debit' => $debit,
366 'numero_piece' => $col('Numéro de pièce'),
367 'remarques' => $col('Remarques'),
372 $data['moyen_paiement'] = $moyen;
373 $data['numero_cheque'] = $col('Numéro de chèque');
374 $data['id_categorie'] = $liste_cats[$cat];
377 $journal->add($data);