prepare(' SELECT journal.id, strftime(\'%d/%m/%Y\', date) AS date, (CASE cat.type WHEN 1 THEN \'Recette\' WHEN -1 THEN \'Dépense\' ELSE \'Autre\' END) AS type, (CASE cat.intitule WHEN NULL THEN \'\' ELSE cat.intitule END), journal.libelle, abs(flux.montant) AS montant, (CASE WHEN flux.montant >= 0 THEN flux.compte ELSE \'\' END), (CASE WHEN flux.montant >= 0 THEN flux_compte.libelle ELSE \'\' END) AS compte_debit_libelle, (CASE WHEN flux.montant < 0 THEN flux.compte ELSE \'\' END), (CASE WHEN flux.montant < 0 THEN flux_compte.libelle ELSE \'\' END) AS compte_credit_libelle, (CASE moyen_paiement WHEN NULL THEN \'\' ELSE moyen.nom END) AS moyen, numero_cheque, numero_piece, remarques FROM compta_journal AS journal LEFT JOIN compta_categories AS cat ON cat.id = journal.id_categorie LEFT JOIN compta_moyens_paiement AS moyen ON moyen.code = journal.moyen_paiement LEFT JOIN compta_flux AS flux ON flux.id_journal = journal.id LEFT JOIN compta_comptes AS flux_compte ON flux_compte.id = flux.compte WHERE id_exercice = '.(int)$exercice.' ORDER BY journal.id; ')->execute(); $fp = fopen('php://output', 'w'); fputcsv($fp, $this->csv_header); while ($row = $res->fetchArray(SQLITE3_ASSOC)) { fputcsv($fp, $row); } fclose($fp); return true; } public function fromCSV($path) { if (!file_exists($path) || !is_readable($path)) { throw new \RuntimeException('Fichier inconnu : '.$path); } $fp = fopen($path, 'r'); if (!$fp) { return false; } $db = DB::getInstance(); $db->exec('BEGIN;'); $comptes = new Compta_Comptes; $banques = new Compta_Comptes_Bancaires; $cats = new Compta_Categories; $journal = new Compta_Journal; $columns = array_flip($this->csv_header); $liste_comptes = $db->simpleStatementFetchAssoc('SELECT id, id FROM compta_comptes;'); $liste_cats = $db->simpleStatementFetchAssoc('SELECT intitule, id FROM compta_categories;'); $liste_moyens = $cats->listMoyensPaiement(); $col = function($column) use (&$row, &$columns) { if (!isset($columns[$column])) return null; if (!isset($row[$columns[$column]])) return null; return $row[$columns[$column]]; }; $line = 0; $last_id = 0; $id = 0; $delim = utils::find_csv_delim($fp); unset($data); while (!feof($fp)) { $row = fgetcsv($fp, 4096, $delim); print_r(["row" => $row]); $line++; if (empty($row)) { continue; } if ($line === 1) { if (trim($row[0]) != 'Numéro mouvement') { throw new UserException('Erreur sur la ligne ' . $line . ' : l\'entête des colonnes est absent ou incorrect.'); } continue; } if (count($row) != count($columns)) { $db->exec('ROLLBACK;'); throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.'); } if (trim($row[0]) !== '' && !is_numeric($row[0])) { $db->exec('ROLLBACK;'); throw new UserException('Erreur sur la ligne ' . $line . ' : la première colonne doit être vide ou contenir le numéro unique d\'opération.'); } $last_id = $id; $id = $col('Numéro mouvement'); if ($id == $last_id) { $montant = $col('Montant'); $debit = $col('Compte de débit - numéro'); $credit = $col('Compte de crédit - numéro'); if (trim($debit) == '' && trim($credit) != '') { $debit = null; $fluxs[] = [ 'compte' => $credit , 'montant' => - $montant ]; } if (trim($debit) != '' && trim($credit) == '') { $credit = null; $fluxs[] = [ 'compte' => $debit , 'montant' => $montant ]; } } else { if (isset($data)) { // NOTE: import previously collected $last_id $data['fluxs'] = $fluxs; $id_journal = $db->simpleQuerySingle('SELECT id FROM compta_journal WHERE id = ?;', false, $last_id); if (empty($last_id) || empty($id_journal)) { print_r(["data" => $data]); $journal->add($data); } else { print_r(['id' => $last_id, "data" => $data]); $journal->edit($last_id, $data); } unset($data); } // NOTE: start collecting $id $fluxs = []; $date = $col('Date'); if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date)) { $db->exec('ROLLBACK;'); throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.'); } $date = explode('/', $date); $date = $date[2] . '-' . $date[1] . '-' . $date[0]; // En dehors de l'exercice courant if ($db->simpleQuerySingle('SELECT 1 FROM compta_exercices WHERE (? < debut OR ? > fin) AND cloture = 0;', false, $date, $date)) { continue; } $cat = $col('Catégorie'); $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2)); if (!$moyen || !array_key_exists($moyen, $liste_moyens)) { $moyen = false; $cat = false; } if ($cat && !array_key_exists($cat, $liste_cats)) { $cat = $moyen = false; } $montant = $col('Montant'); $debit = $col('Compte de débit - numéro'); $credit = $col('Compte de crédit - numéro'); if (trim($debit) == '' && trim($credit) != '') { $debit = null; $fluxs[] = [ 'compte' => $credit , 'montant' => - $montant ]; } if (trim($debit) != '' && trim($credit) == '') { $credit = null; $fluxs[] = [ 'compte' => $debit , 'montant' => $montant ]; } $data = [ 'libelle' => $col('Libellé'), 'date' => $date, 'numero_piece' => $col('Numéro de pièce'), 'remarques' => $col('Remarques'), ]; if ($cat) { $data['moyen_paiement'] = $moyen; $data['numero_cheque'] = $col('Numéro de chèque'); $data['id_categorie'] = $liste_cats[$cat]; } } } if (isset($data)) { // NOTE: import previously collected $id $data['fluxs'] = $fluxs; $id_journal = $db->simpleQuerySingle('SELECT id FROM compta_journal WHERE id = ?;', false, $id); if (empty($id) || empty($id_journal)) { print_r(["data" => $data]); $journal->add($data); } else { print_r(['id (end)' => $id, "data" => $data]); $journal->edit($id, $data); } unset($data); } $db->exec('END;'); fclose($fp); return true; } public function fromCitizen($path) { if (!file_exists($path) || !is_readable($path)) { throw new \RuntimeException('Fichier inconnu : '.$path); } $fp = fopen($path, 'r'); if (!$fp) { return false; } $db = DB::getInstance(); $db->exec('BEGIN;'); $comptes = new Compta_Comptes; $banques = new Compta_Comptes_Bancaires; $cats = new Compta_Categories; $journal = new Compta_Journal; $columns = []; $liste_comptes = $db->simpleStatementFetchAssoc('SELECT id, id FROM compta_comptes;'); $liste_cats = $db->simpleStatementFetchAssoc('SELECT intitule, id FROM compta_categories;'); $liste_moyens = $cats->listMoyensPaiement(); $get_compte = function ($compte, $intitule) use (&$liste_comptes, &$comptes, &$banques) { if (substr($compte, 0, 2) == '51') { $compte = '512' . substr($compte, -1); } // Création comptes if (!array_key_exists($compte, $liste_comptes)) { if (substr($compte, 0, 3) == '512') { $liste_comptes[$compte] = $banques->add([ 'libelle' => $intitule, 'banque' => 'Inconnue', ]); } else { $liste_comptes[$compte] = $comptes->add([ 'id' => $compte, 'libelle' => $intitule, 'parent' => substr($compte, 0, -1) ]); } } return $compte; }; $col = function($column) use (&$row, &$columns) { if (!isset($columns[$column])) return null; if (!isset($row[$columns[$column]])) return null; return $row[$columns[$column]]; }; $line = 0; $delim = utils::find_csv_delim($fp); while (!feof($fp)) { $row = fgetcsv($fp, 4096, $delim); $line++; if (empty($row)) { continue; } if (empty($columns)) { $columns = $row; $columns = array_flip($columns); continue; } $date = $col('Date'); if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date)) { $db->exec('ROLLBACK;'); throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.'); } $date = explode('/', $date); $date = $date[2] . '-' . $date[1] . '-' . $date[0]; if ($db->simpleQuerySingle('SELECT 1 FROM compta_exercices WHERE (? < debut OR ? > fin) AND cloture = 0;', false, $date, $date)) { continue; } $debit = $get_compte($col('Compte débité - Numéro'), $col('Compte débité - Intitulé')); $credit = $get_compte($col('Compte crédité - Numéro'), $col('Compte crédité - Intitulé')); $cat = $col('Rubrique'); $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2)); if (!$moyen || !array_key_exists($moyen, $liste_moyens)) { $moyen = false; $cat = false; } if ($cat && !array_key_exists($cat, $liste_cats)) { if ($col('Nature') == 'Recette') { $type = $cats::RECETTES; $compte = $credit; } elseif ($col('Nature') == 'Dépense') { $type = $cats::DEPENSES; $compte = $debit; } else { $type = $cats::AUTRES; $cat = false; } if ($type != $cats::AUTRES) { $liste_cats[$cat] = $cats->add([ 'intitule' => $cat, 'type' => $type, 'compte' => $compte ]); } } $data = [ 'libelle' => $col('Libellé'), 'montant' => $col('Montant'), 'date' => $date, 'compte_credit' => $credit, 'compte_debit' => $debit, 'numero_piece' => $col('Numéro de pièce'), 'remarques' => $col('Remarques'), ]; if ($cat) { $data['moyen_paiement'] = $moyen; $data['numero_cheque'] = $col('Numéro de chèque'); $data['id_categorie'] = $liste_cats[$cat]; } $journal->add($data); } $db->exec('END;'); fclose($fp); return true; } } ?>