From f7ee0ae730f47bfef173c394fa4db71a22dece61 Mon Sep 17 00:00:00 2001 From: Julien Moutinho Date: Fri, 26 Sep 2014 22:47:07 +0200 Subject: [PATCH] =?utf8?q?Ajout=20du=20support=20des=20=C3=A9critures=20ve?= =?utf8?q?ntil=C3=A9es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/class.compta_comptes.php | 20 +- include/class.compta_comptes_bancaires.php | 5 +- include/class.compta_exercices.php | 70 +++--- include/class.compta_import.php | 205 ++++++++++++------ include/class.compta_journal.php | 113 +++++----- include/class.compta_stats.php | 14 +- include/class.cotisations_membres.php | 8 +- include/class.db.php | 3 +- include/class.membres.php | 2 +- include/data/schema.sql | 22 +- include/lib.template.php | 6 +- templates/admin/compta/comptes/journal.tpl | 16 +- templates/admin/compta/exercices/bilan.tpl | 10 +- .../compta/exercices/compte_resultat.tpl | 10 +- .../admin/compta/exercices/grand_livre.tpl | 28 ++- templates/admin/compta/exercices/journal.tpl | 29 +-- templates/admin/compta/import.tpl | 4 +- templates/admin/compta/operations/index.tpl | 4 +- .../admin/compta/operations/modifier.tpl | 93 +++++++- templates/admin/compta/operations/saisir.tpl | 93 +++++++- templates/admin/compta/operations/voir.tpl | 27 ++- templates/admin/config/membres.tpl | 10 +- www/admin/compta/comptes/journal.php | 13 +- .../compta/exercices/compte_resultat.php | 3 +- www/admin/compta/exercices/journal.php | 18 +- www/admin/compta/import.php | 17 +- www/admin/compta/operations/index.php | 16 +- www/admin/compta/operations/membre.php | 2 +- www/admin/compta/operations/modifier.php | 45 +++- www/admin/compta/operations/saisir.php | 52 ++++- www/admin/compta/operations/voir.php | 14 +- www/admin/config/membres.php | 5 +- www/admin/static/admin.css | 6 +- www/admin/static/handheld.css | 6 +- 34 files changed, 655 insertions(+), 334 deletions(-) diff --git a/include/class.compta_comptes.php b/include/class.compta_comptes.php index 02021a2..0ec751e 100644 --- a/include/class.compta_comptes.php +++ b/include/class.compta_comptes.php @@ -11,9 +11,9 @@ class Compta_Comptes const PRODUIT = 0x04; const CHARGE = 0x08; - public function importPlan() + public function importPlan($file = NULL) { - $plan = json_decode(file_get_contents(ROOT . '/include/data/plan_comptable.json'), true); + $plan = json_decode(file_get_contents($file ? $file : ROOT.'/include/data/plan_comptable.json'), true); $db = DB::getInstance(); $db->exec('BEGIN;'); @@ -44,7 +44,7 @@ class Compta_Comptes } } - $db->exec('DELETE FROM compta_comptes WHERE id NOT IN(\''.implode('\', \'', $ids).'\') AND plan_comptable = 1;'); + //$db->exec('DELETE FROM compta_comptes WHERE id NOT IN(\''.implode('\', \'', $ids).'\') AND plan_comptable = 1;'); $db->exec('END;'); @@ -131,7 +131,7 @@ class Compta_Comptes $db = DB::getInstance(); // Ne pas supprimer un compte qui est utilisé ! - if ($db->simpleQuerySingle('SELECT 1 FROM compta_journal WHERE compte_debit = ? OR compte_debit = ? LIMIT 1;', false, $id, $id)) + if ($db->simpleQuerySingle('SELECT 1 FROM compta_flux WHERE compte = ? LIMIT 1;', false, $id)) { throw new UserException('Ce compte ne peut être supprimé car des opérations comptables y sont liées.'); } @@ -160,8 +160,8 @@ class Compta_Comptes { $db = DB::getInstance(); - if ($db->simpleQuerySingle('SELECT 1 FROM compta_journal - WHERE compte_debit = ? OR compte_debit = ? LIMIT 1;', false, $id, $id)) + if ($db->simpleQuerySingle('SELECT 1 FROM compta_flux + WHERE compte = ? LIMIT 1;', false, $id)) { return false; } @@ -184,8 +184,9 @@ class Compta_Comptes $db = DB::getInstance(); if ($db->simpleQuerySingle('SELECT 1 FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal WHERE id_exercice = (SELECT id FROM compta_exercices WHERE cloture = 0 LIMIT 1) - AND (compte_debit = ? OR compte_debit = ?) LIMIT 1;', false, $id, $id)) + AND compte = ? LIMIT 1;', false, $id)) { return false; } @@ -211,8 +212,9 @@ class Compta_Comptes // Ne pas désactiver un compte utilisé dans l'exercice courant if ($db->simpleQuerySingle('SELECT 1 FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal WHERE id_exercice = (SELECT id FROM compta_exercices WHERE cloture = 0 LIMIT 1) - AND (compte_debit = ? OR compte_debit = ?) LIMIT 1;', false, $id, $id)) + AND compte = ? LIMIT 1;', false, $id)) { throw new UserException('Ce compte ne peut être désactivé car des écritures y sont liées sur l\'exercice courant. ' . 'Il faut supprimer ou ré-attribuer ces écritures avant de pouvoir supprimer le compte.'); @@ -322,4 +324,4 @@ class Compta_Comptes } } -?> \ No newline at end of file +?> diff --git a/include/class.compta_comptes_bancaires.php b/include/class.compta_comptes_bancaires.php index 1ac1213..88eb210 100644 --- a/include/class.compta_comptes_bancaires.php +++ b/include/class.compta_comptes_bancaires.php @@ -75,8 +75,9 @@ class Compta_Comptes_Bancaires extends Compta_Comptes // Ne pas supprimer/désactiver un compte qui est utilisé dans l'exercice courant if ($db->simpleQuerySingle('SELECT 1 FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal WHERE id_exercice = (SELECT id FROM compta_exercices WHERE cloture = 0 LIMIT 1) - AND (compte_debit = ? OR compte_debit = ?) LIMIT 1;', false, $id, $id)) + AND compte = ? LIMIT 1;', false, $id)) { throw new UserException('Ce compte ne peut être supprimé car des écritures y sont liées sur l\'exercice courant. ' . 'Il faut supprimer ou ré-attribuer ces écritures avant de pouvoir supprimer le compte.'); @@ -161,4 +162,4 @@ class Compta_Comptes_Bancaires extends Compta_Comptes } } -?> \ No newline at end of file +?> diff --git a/include/class.compta_exercices.php b/include/class.compta_exercices.php index 02edb14..e115a35 100644 --- a/include/class.compta_exercices.php +++ b/include/class.compta_exercices.php @@ -204,9 +204,10 @@ class Compta_Exercices $journal->add([ 'libelle' => 'Résultat de l\'exercice précédent', 'date' => $date, - 'montant' => abs($resultat), - 'compte_debit' => $resultat < 0 ? 129 : NULL, - 'compte_credit' => $resultat > 0 ? 120 : NULL, + 'fluxs' => + [ [ 'compte' => $resultat > 0 ? 129 : 120 + , 'montant' => abs($resultat) ] + ] ]); } @@ -283,8 +284,11 @@ class Compta_Exercices public function getJournal($exercice) { $db = DB::getInstance(); - $query = 'SELECT *, strftime(\'%s\', date) AS date FROM compta_journal - WHERE id_exercice = '.(int)$exercice.' ORDER BY date, id;'; + $query = 'SELECT *, strftime(\'%s\', date) AS date, -montant AS montant_oppose + FROM compta_flux + LEFT JOIN compta_journal ON compta_journal.id = compta_flux.id_journal + WHERE compta_journal.id_exercice = '.(int)$exercice.' + ORDER BY date, id;'; return $db->simpleStatementFetch($query); } @@ -293,12 +297,9 @@ class Compta_Exercices $db = DB::getInstance(); $livre = ['classes' => [], 'debit' => 0.0, 'credit' => 0.0]; - $res = $db->prepare('SELECT compte FROM - (SELECT compte_debit AS compte FROM compta_journal - WHERE id_exercice = '.(int)$exercice.' GROUP BY compte_debit - UNION - SELECT compte_credit AS compte FROM compta_journal - WHERE id_exercice = '.(int)$exercice.' GROUP BY compte_credit) + $res = $db->prepare('SELECT compte FROM compta_flux + LEFT JOIN compta_journal ON compta_journal.id = compta_flux.id_journal + WHERE id_exercice = '.(int)$exercice.' ORDER BY base64(compte) COLLATE BINARY ASC;' )->execute(); @@ -328,19 +329,20 @@ class Compta_Exercices $livre['classes'][$classe][$parent]['comptes'][$compte] = ['debit' => 0.0, 'credit' => 0.0, 'journal' => []]; $livre['classes'][$classe][$parent]['comptes'][$compte]['journal'] = $db->simpleStatementFetch( - 'SELECT *, strftime(\'%s\', date) AS date FROM ( - SELECT * FROM compta_journal WHERE compte_debit = :compte AND id_exercice = '.(int)$exercice.' - UNION - SELECT * FROM compta_journal WHERE compte_credit = :compte AND id_exercice = '.(int)$exercice.' - ) - ORDER BY date, numero_piece, id;', SQLITE3_ASSOC, ['compte' => $compte]); + 'SELECT *, strftime(\'%s\', date) AS date FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE compte = :compte AND id_exercice = '.(int)$exercice.' + ORDER BY date, numero_piece, id;', SQLITE3_ASSOC, ['compte' => $compte]); $debit = (float) $db->simpleQuerySingle( - 'SELECT SUM(montant) FROM compta_journal WHERE compte_debit = ? AND id_exercice = '.(int)$exercice.';', + 'SELECT SUM(montant) FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE compte = ? AND montant > 0 AND id_exercice = '.(int)$exercice.';', false, $compte); - $credit = (float) $db->simpleQuerySingle( - 'SELECT SUM(montant) FROM compta_journal WHERE compte_credit = ? AND id_exercice = '.(int)$exercice.';', + 'SELECT -SUM(montant) FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE compte = ? AND montant < 0 AND id_exercice = '.(int)$exercice.';', false, $compte); $livre['classes'][$classe][$parent]['comptes'][$compte]['debit'] = $debit; @@ -368,19 +370,25 @@ class Compta_Exercices $res = $db->prepare('SELECT compte, SUM(debit), SUM(credit) FROM - (SELECT compte_debit AS compte, SUM(montant) AS debit, 0 AS credit - FROM compta_journal WHERE id_exercice = '.(int)$exercice.' GROUP BY compte_debit + (SELECT compte, SUM(montant) AS debit, 0 AS credit + FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE montant > 0 AND id_exercice = '.(int)$exercice.' GROUP BY compte UNION - SELECT compte_credit AS compte, 0 AS debit, SUM(montant) AS credit - FROM compta_journal WHERE id_exercice = '.(int)$exercice.' GROUP BY compte_credit) + SELECT compte, 0 AS debit, -SUM(montant) AS credit + FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE montant < 0 AND id_exercice = '.(int)$exercice.' GROUP BY compte) WHERE compte LIKE \'6%\' OR compte LIKE \'7%\' GROUP BY compte ORDER BY base64(compte) COLLATE BINARY ASC;' )->execute(); + while ($row = $res->fetchArray(SQLITE3_NUM)) { list($compte, $debit, $credit) = $row; + print_r([$compte, $debit, $credit]); $classe = substr($compte, 0, 1); $parent = substr($compte, 0, 2); @@ -466,11 +474,15 @@ class Compta_Exercices // soit fait au niveau du SQL, mais pour le moment ça marche $res = $db->prepare('SELECT compte, debit, credit, (SELECT position FROM compta_comptes WHERE id = compte) AS position FROM - (SELECT compte_debit AS compte, SUM(montant) AS debit, NULL AS credit - FROM compta_journal WHERE id_exercice = '.(int)$exercice.' GROUP BY compte_debit + (SELECT compte, SUM(montant) AS debit, NULL AS credit + FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE montant > 0 AND id_exercice = '.(int)$exercice.' GROUP BY compte UNION - SELECT compte_credit AS compte, NULL AS debit, SUM(montant) AS credit - FROM compta_journal WHERE id_exercice = '.(int)$exercice.' GROUP BY compte_credit) + SELECT compte, NULL AS debit, SUM(montant) AS credit + FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE montant < 0 AND id_exercice = '.(int)$exercice.' GROUP BY compte) WHERE compte IN (SELECT id FROM compta_comptes WHERE position IN ('.implode(', ', $include).')) ORDER BY base64(compte) COLLATE BINARY ASC;' )->execute(); @@ -566,4 +578,4 @@ class Compta_Exercices } } -?> \ No newline at end of file +?> diff --git a/include/class.compta_import.php b/include/class.compta_import.php index 73fd99b..ec1688d 100644 --- a/include/class.compta_import.php +++ b/include/class.compta_import.php @@ -25,28 +25,30 @@ class Compta_Import { $db = DB::getInstance(); - $res = $db->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) AS cat, - journal.libelle, - montant, - compte_debit, - debit.libelle AS libelle_debit, - compte_credit, - credit.libelle AS libelle_credit, - (CASE moyen_paiement WHEN NULL THEN \'\' ELSE moyen.nom END) AS moyen, - numero_cheque, - numero_piece, - remarques + $retro_comptatibility = 'SELECT count(montant) AS montants FROM compta_flux WHERE compta_flux.id_journal = journal.id'; + $res = $db->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_comptes AS debit ON debit.id = journal.compte_debit - LEFT JOIN compta_comptes AS credit ON credit.id = journal.compte_credit 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.date; + ORDER BY journal.id; ')->execute(); $fp = fopen('php://output', 'w'); @@ -101,11 +103,15 @@ class Compta_Import }; $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)) @@ -135,77 +141,136 @@ class Compta_Import 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'); - $date = $col('Date'); - - if (!preg_match('!^\d{2}/\d{2}/\d{4}$!', $date)) + if ($id == $last_id) { - $db->exec('ROLLBACK;'); - throw new UserException('Erreur sur la ligne ' . $line . ' : la date n\'est pas au format jj/mm/aaaa.'); + $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]; + $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; - } + // 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; + } - $debit = $col('Compte de débit - numéro'); - $credit = $col('Compte de crédit - numéro'); + $cat = $col('Catégorie'); + $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2)); - if (trim($debit) == '' && trim($credit) != '') - { - $debit = null; - } - elseif (trim($debit) != '' && trim($credit) == '') - { - $credit = null; - } + if (!$moyen || !array_key_exists($moyen, $liste_moyens)) + { + $moyen = false; + $cat = false; + } - $cat = $col('Catégorie'); - $moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2)); + if ($cat && !array_key_exists($cat, $liste_cats)) + { + $cat = $moyen = false; + } - if (!$moyen || !array_key_exists($moyen, $liste_moyens)) - { - $moyen = false; - $cat = false; - } + $montant = $col('Montant'); + $debit = $col('Compte de débit - numéro'); + $credit = $col('Compte de crédit - numéro'); - if ($cat && !array_key_exists($cat, $liste_cats)) - { - $cat = $moyen = false; - } + 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é'), - 'montant' => (float) $col('Montant'), - 'date' => $date, - 'compte_credit' => $credit, - 'compte_debit' => $debit, - 'numero_piece' => $col('Numéro de pièce'), - 'remarques' => $col('Remarques'), - ]; + $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 ($cat) + { + $data['moyen_paiement'] = $moyen; + $data['numero_cheque'] = $col('Numéro de chèque'); + $data['id_categorie'] = $liste_cats[$cat]; + } } - - if (empty($id)) + } + 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;'); @@ -384,4 +449,4 @@ class Compta_Import } } -?> \ No newline at end of file +?> diff --git a/include/class.compta_journal.php b/include/class.compta_journal.php index 9ff8c95..12c51e9 100644 --- a/include/class.compta_journal.php +++ b/include/class.compta_journal.php @@ -45,50 +45,26 @@ class Compta_Journal ? 'LIKE \'' . $db->escapeString(trim($id_compte)) . '%\'' : '= \'' . $db->escapeString(trim($id_compte)) . '\''; - $debit = 'COALESCE((SELECT SUM(montant) FROM compta_journal WHERE compte_debit '.$compte.' AND id_exercice = '.(int)$exercice.'), 0)'; - $credit = 'COALESCE((SELECT SUM(montant) FROM compta_journal WHERE compte_credit '.$compte.' AND id_exercice = '.(int)$exercice.'), 0)'; + $solde = 'COALESCE((SELECT SUM(montant) FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE compte '.$compte.' AND id_exercice = '.(int)$exercice.'), 0)'; - // L'actif augmente au débit, le passif au crédit - $position = $db->simpleQuerySingle('SELECT position FROM compta_comptes WHERE id = ?;', false, $id_compte); - - if (($position & Compta_Comptes::ACTIF) || ($position & Compta_Comptes::CHARGE)) - { - $query = $debit . ' - ' . $credit; - } - else - { - $query = $credit . ' - ' . $debit; - } - - return $db->querySingle('SELECT ' . $query . ';'); + return $db->querySingle('SELECT ' . $solde . ';'); } public function getJournalCompte($compte, $inclure_sous_comptes = false) { $db = DB::getInstance(); - $position = $db->simpleQuerySingle('SELECT position FROM compta_comptes WHERE id = ?;', false, $compte); - $exercice = $this->_getCurrentExercice(); $compte = $inclure_sous_comptes ? 'LIKE \'' . $db->escapeString(trim($compte)) . '%\'' : '= \'' . $db->escapeString(trim($compte)) . '\''; - // L'actif et les charges augmentent au débit, le passif et les produits au crédit - if (($position & Compta_Comptes::ACTIF) || ($position & Compta_Comptes::CHARGE)) - { - $d = ''; - $c = '-'; - } - else - { - $d = '-'; - $c = ''; - } - - $query = 'SELECT *, strftime(\'%s\', date) AS date, - running_sum(CASE WHEN compte_debit '.$compte.' THEN '.$d.'montant ELSE '.$c.'montant END) AS solde - FROM compta_journal WHERE (compte_debit '.$compte.' OR compte_credit '.$compte.') AND id_exercice = '.(int)$exercice.' + $query = 'SELECT *, strftime(\'%s\', date) AS date, running_sum(compta_flux.montant) AS solde + FROM compta_flux + LEFT JOIN compta_journal ON compta_journal.id = compta_flux.id_journal + WHERE compta_flux.compte '.$compte.' AND compta_journal.id_exercice = '.(int)$exercice.' ORDER BY date ASC;'; // Obligatoire pour bien taper dans l'index de la date @@ -108,9 +84,18 @@ class Compta_Journal $data['id_exercice'] = $this->_getCurrentExercice(); + $fluxs = $data['fluxs']; + unset($data['fluxs']); + $db->simpleInsert('compta_journal', $data); $id = $db->lastInsertRowId(); + foreach ($fluxs as $flux) + { + $db->simpleInsert('compta_flux', $flux + ['id_journal' => $id]); + } + $data['fluxs'] = $fluxs; + return $id; } @@ -126,8 +111,17 @@ class Compta_Journal $this->_checkFields($data); - $db->simpleUpdate('compta_journal', $data, - 'id = \''.trim($id).'\''); + $fluxs = $data['fluxs']; + unset($data['fluxs']); + + $db->simpleUpdate('compta_journal', $data, 'id = \''.trim($id).'\''); + + $db->simpleExec('DELETE FROM compta_flux WHERE id_journal = ?;', (int)$id); + foreach ($fluxs as $flux) + { + $db->simpleInsert('compta_flux', $flux + ['id_journal' => (int)$id]); + } + $data['fluxs'] = $fluxs; return true; } @@ -151,7 +145,10 @@ class Compta_Journal public function get($id) { $db = DB::getInstance(); - return $db->simpleQuerySingle('SELECT *, strftime(\'%s\', date) AS date FROM compta_journal WHERE id = ?;', true, $id); + $journal = $db->simpleQuerySingle('SELECT *, strftime(\'%s\', date) AS date FROM compta_journal WHERE id = ?;', true, $id); + $fluxs = $db->simpleStatementFetch('SELECT * FROM compta_flux WHERE id_journal = ? + ORDER BY base64(compte) COLLATE BINARY ASC;', true, $id); + return ($journal + ['fluxs' => $fluxs]); } public function countForMember($id) @@ -164,8 +161,11 @@ class Compta_Journal public function listForMember($id, $exercice) { $db = DB::getInstance(); - return $db->simpleStatementFetch('SELECT * FROM compta_journal + $journal = $db->simpleStatementFetch('SELECT * FROM compta_journal WHERE id_auteur = ? AND id_exercice = ?;', \SQLITE3_ASSOC, (int)$id, (int)$exercice); + $fluxs = $db->simpleStatementFetch('SELECT * FROM compta_flux WHERE id_journal = ? + ORDER BY base64(compte) COLLATE BINARY ASC;', true, $journal['id']); + return ($journal + ['fluxs' => $fluxs]); } protected function _checkFields(&$data) @@ -217,13 +217,6 @@ class Compta_Journal } } - $data['montant'] = str_replace(',', '.', $data['montant']); - $data['montant'] = (float)$data['montant']; - - if ($data['montant'] <= 0) - { - throw new UserException('Le montant ne peut être égal ou inférieur à zéro.'); - } foreach (['remarques', 'numero_piece', 'numero_cheque'] as $champ) { @@ -237,26 +230,26 @@ class Compta_Journal } } - if (!array_key_exists('compte_debit', $data) || - (!is_null($data['compte_debit']) && - !$db->simpleQuerySingle('SELECT 1 FROM compta_comptes WHERE id = ?;', false, $data['compte_debit']))) + $sum_montant = 0; + foreach ($data['fluxs'] as $flux) { - throw new UserException('Compte débité inconnu.'); + if (!array_key_exists('compte', $flux) || + (!is_null($flux['compte']) && + !$db->simpleQuerySingle('SELECT 1 FROM compta_comptes WHERE id = ?;', false, $flux['compte']))) + { + throw new UserException('Compte débité inconnu.'); + } + $flux['compte'] = is_null($flux['compte']) ? null : strtoupper(trim($flux['compte'])); + + $flux['montant'] = str_replace(',', '.', $flux['montant']); + $flux['montant'] = (float)$flux['montant']; + + $sum_montant = round($sum_montant + $flux['montant'], 2); } - if (!array_key_exists('compte_credit', $data) || - (!is_null($data['compte_credit']) && - !$db->simpleQuerySingle('SELECT 1 FROM compta_comptes WHERE id = ?;', false, $data['compte_credit']))) - { - throw new UserException('Compte crédité inconnu.'); - } - - $data['compte_credit'] = is_null($data['compte_credit']) ? null : strtoupper(trim($data['compte_credit'])); - $data['compte_debit'] = is_null($data['compte_debit']) ? null : strtoupper(trim($data['compte_debit'])); - - if ($data['compte_credit'] == $data['compte_debit']) + if ($sum_montant != 0) { - throw new UserException('Compte crédité identique au compte débité.'); + throw new UserException('Somme non-nulle des montants des flux: '.print_r($data, true)); } if (isset($data['id_categorie'])) @@ -360,4 +353,4 @@ class Compta_Journal } } -?> \ No newline at end of file +?> diff --git a/include/class.compta_stats.php b/include/class.compta_stats.php index 335fbe0..b6f7c18 100644 --- a/include/class.compta_stats.php +++ b/include/class.compta_stats.php @@ -28,6 +28,7 @@ class Compta_Stats { return $this->getStats('SELECT strftime(\'%Y%m\', date) AS date, SUM(montant) FROM compta_journal + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal WHERE id_categorie IN (SELECT id FROM compta_categories WHERE type = '.$type.') AND id_exercice = (SELECT id FROM compta_exercices WHERE cloture = 0) GROUP BY strftime(\'%Y-%m\', date) ORDER BY date;'); @@ -43,7 +44,7 @@ class Compta_Stats return $this->_parType(Compta_Categories::DEPENSES); } - public function soldeCompte($compte, $augmente = 'debit', $diminue = 'credit') + public function soldeCompte($compte) { $db = DB::getInstance(); @@ -58,16 +59,13 @@ class Compta_Stats $stats = $this->getStats('SELECT strftime(\'%Y%m\', date) AS date, (COALESCE((SELECT SUM(montant) FROM compta_journal - WHERE compte_'.$augmente.' '.$compte.' AND id_exercice = cj.id_exercice - AND date >= strftime(\'%Y-%m-01\', cj.date) - AND date <= strftime(\'%Y-%m-31\', cj.date)), 0) - - COALESCE((SELECT SUM(montant) FROM compta_journal - WHERE compte_'.$diminue.' '.$compte.' AND id_exercice = cj.id_exercice + LEFT JOIN compta_flux ON compta_journal.id = compta_flux.id_journal + WHERE compte '.$compte.' AND id_exercice = cj.id_exercice AND date >= strftime(\'%Y-%m-01\', cj.date) AND date <= strftime(\'%Y-%m-31\', cj.date)), 0) ) AS solde FROM compta_journal AS cj - WHERE (compte_debit '.$compte.' OR compte_credit '.$compte.') + WHERE (compte '.$compte.') AND id_exercice = (SELECT id FROM compta_exercices WHERE cloture = 0) GROUP BY strftime(\'%Y-%m\', date) ORDER BY date;'); @@ -119,4 +117,4 @@ class Compta_Stats } } -?> \ No newline at end of file +?> diff --git a/include/class.cotisations_membres.php b/include/class.cotisations_membres.php index a895ee2..1dc9029 100644 --- a/include/class.cotisations_membres.php +++ b/include/class.cotisations_membres.php @@ -193,12 +193,12 @@ class Cotisations_Membres $id_operation = $journal->add([ 'libelle' => $data['libelle'], - 'montant' => $data['montant'], 'date' => $data['date'], + 'fluxs' => + [ ['compte'=>$credit, 'montant' => - $data['montant'] ] + , ['compte'=>$debit, 'montant' => $data['montant'] ] ], 'moyen_paiement'=> $data['moyen_paiement'], 'numero_cheque' => isset($data['numero_cheque']) ? $data['numero_cheque'] : null, - 'compte_debit' => $debit, - 'compte_credit' => $credit, 'id_categorie' => (int)$data['id_categorie'], 'id_auteur' => (int)$data['id_auteur'], ]); @@ -333,4 +333,4 @@ class Cotisations_Membres return $db->simpleQuerySingle('SELECT COUNT(DISTINCT id_cotisation) FROM cotisations_membres WHERE id_membre = ?;', false, (int)$id); } -} \ No newline at end of file +} diff --git a/include/class.db.php b/include/class.db.php index c560c59..4de2fda 100644 --- a/include/class.db.php +++ b/include/class.db.php @@ -191,6 +191,7 @@ class DB extends \SQLite3 public function simpleStatement($query, $args = []) { + //print_r(['query' => $query, 'args' => $args]); $statement = $this->prepare($query); $nb = $statement->paramCount(); @@ -408,4 +409,4 @@ class DB extends \SQLite3 } } -?> \ No newline at end of file +?> diff --git a/include/class.membres.php b/include/class.membres.php index 0136d6d..a0f6748 100644 --- a/include/class.membres.php +++ b/include/class.membres.php @@ -789,4 +789,4 @@ class Membres } } -?> \ No newline at end of file +?> diff --git a/include/data/schema.sql b/include/data/schema.sql index 2cc846f..0c2bfba 100644 --- a/include/data/schema.sql +++ b/include/data/schema.sql @@ -247,30 +247,34 @@ CREATE TABLE compta_journal remarques TEXT, numero_piece TEXT, -- N° de pièce comptable - montant REAL, - date TEXT DEFAULT CURRENT_DATE, moyen_paiement TEXT DEFAULT NULL, numero_cheque TEXT DEFAULT NULL, - compte_debit TEXT, -- N° du compte dans le plan - compte_credit TEXT, -- N° du compte dans le plan - id_exercice INTEGER NULL DEFAULT NULL, -- En cas de compta simple, l'exercice est permanent (NULL) id_auteur INTEGER NULL, id_categorie INTEGER NULL, -- Numéro de catégorie (en mode simple) FOREIGN KEY(moyen_paiement) REFERENCES compta_moyens_paiement(code), - FOREIGN KEY(compte_debit) REFERENCES compta_comptes(id), - FOREIGN KEY(compte_credit) REFERENCES compta_comptes(id), FOREIGN KEY(id_exercice) REFERENCES compta_exercices(id), FOREIGN KEY(id_auteur) REFERENCES membres(id), FOREIGN KEY(id_categorie) REFERENCES compta_categories(id) ); +CREATE TABLE compta_flux +( + id_journal INTEGER, + + compte TEXT, -- N° du compte dans le plan + montant REAL, + + FOREIGN KEY(id_journal) REFERENCES compta_journal(id) ON DELETE CASCADE, + FOREIGN KEY(compte) REFERENCES compta_comptes(id) ON DELETE CASCADE +); + CREATE INDEX compta_operations_exercice ON compta_journal (id_exercice); CREATE INDEX compta_operations_date ON compta_journal (date); -CREATE INDEX compta_operations_comptes ON compta_journal (compte_debit, compte_credit); +--CREATE INDEX compta_operations_comptes ON compta_journal (compte_debit, compte_credit); CREATE INDEX compta_operations_auteur ON compta_journal (id_auteur); CREATE TABLE compta_moyens_paiement @@ -313,4 +317,4 @@ CREATE TABLE plugins version TEXT NOT NULL, menu INTEGER NOT NULL DEFAULT 0, config TEXT -); \ No newline at end of file +); diff --git a/include/lib.template.php b/include/lib.template.php index 6f9f8e7..87eb677 100644 --- a/include/lib.template.php +++ b/include/lib.template.php @@ -336,8 +336,10 @@ function tpl_select_compte($params) $name = $params['name']; $comptes = $params['comptes']; $selected = isset($params['data'][$params['name']]) ? $params['data'][$params['name']] : utils::post($name); + $id = isset($params['id']) ? $params['id'] : "f_$name"; + $form_name = isset($params['form_name']) ? $params['form_name'] : $name; - $out = ''; foreach ($comptes as $compte) { @@ -601,4 +603,4 @@ $tpl->register_modifier('format_bytes', function ($size) { $tpl->register_modifier('strftime_fr', 'Garradin\tpl_strftime_fr'); $tpl->register_modifier('date_fr', 'Garradin\tpl_date_fr'); -?> \ No newline at end of file +?> diff --git a/templates/admin/compta/comptes/journal.tpl b/templates/admin/compta/comptes/journal.tpl index 444b7f1..5b08a1d 100644 --- a/templates/admin/compta/comptes/journal.tpl +++ b/templates/admin/compta/comptes/journal.tpl @@ -20,11 +20,11 @@ - - - Date - Montant - Solde cumulé + + + Date + Montant + Solde cumulé Libellé @@ -38,9 +38,9 @@ {/if} {$ligne.date|date_fr:'d/m/Y'|escape} - {if $ligne.compte_credit == $compte.id}{$credit}{else}{$debit}{/if}{$ligne.montant|html_money} + {$ligne.montant|html_money} {$ligne.solde|html_money} - {$ligne.libelle|escape} + {$ligne.libelle|escape} {/foreach} @@ -53,4 +53,4 @@ -{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/exercices/bilan.tpl b/templates/admin/compta/exercices/bilan.tpl index 4704b04..7616aca 100644 --- a/templates/admin/compta/exercices/bilan.tpl +++ b/templates/admin/compta/exercices/bilan.tpl @@ -19,12 +19,12 @@ {foreach from=$bilan.actif.comptes key="parent_code" item="parent"} - {$parent_code|get_nom_compte|escape} + {$parent_code|escape} — {$parent_code|get_nom_compte|escape} {$parent.solde|html_money} {foreach from=$parent.comptes item="solde" key="compte"} - {$compte|get_nom_compte|escape} + {$compte|escape} — {$compte|get_nom_compte|escape} {$solde|html_money} {/foreach} @@ -38,12 +38,12 @@ {foreach from=$bilan.passif.comptes key="parent_code" item="parent"} - {$parent_code|get_nom_compte|escape} + {$parent_code|escape} — {$parent_code|get_nom_compte|escape} {$parent.solde|html_money} {foreach from=$parent.comptes item="solde" key="compte"} - {$compte|get_nom_compte|escape} + {$compte|escape} — {$compte|get_nom_compte|escape} {$solde|html_money} {/foreach} @@ -81,4 +81,4 @@

Toutes les opérations sont libellées en {$config.monnaie|escape}.

-{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/exercices/compte_resultat.tpl b/templates/admin/compta/exercices/compte_resultat.tpl index 7ca80df..5bf8665 100644 --- a/templates/admin/compta/exercices/compte_resultat.tpl +++ b/templates/admin/compta/exercices/compte_resultat.tpl @@ -19,12 +19,12 @@ {foreach from=$compte_resultat.charges.comptes key="parent_code" item="parent"} - {$parent_code|get_nom_compte|escape} + {$parent_code} — {$parent_code|get_nom_compte|escape} {$parent.solde|html_money} {foreach from=$parent.comptes item="solde" key="compte"} - {$compte|get_nom_compte|escape} + {$compte|escape} — {$compte|get_nom_compte|escape} {$solde|html_money} {/foreach} @@ -38,12 +38,12 @@ {foreach from=$compte_resultat.produits.comptes key="parent_code" item="parent"} - {$parent_code|get_nom_compte|escape} + {$parent_code} — {$parent_code|get_nom_compte|escape} {$parent.solde|html_money} {foreach from=$parent.comptes item="solde" key="compte"} - {$compte|get_nom_compte|escape} + {$compte|escape} — {$compte|get_nom_compte|escape} {$solde|html_money} {/foreach} @@ -107,4 +107,4 @@

Toutes les opérations sont libellées en {$config.monnaie|escape}.

-{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/exercices/grand_livre.tpl b/templates/admin/compta/exercices/grand_livre.tpl index 4aac7a5..2b88a12 100644 --- a/templates/admin/compta/exercices/grand_livre.tpl +++ b/templates/admin/compta/exercices/grand_livre.tpl @@ -12,37 +12,41 @@ {foreach from=$comptes item="compte" key="code"} {foreach from=$compte.comptes item="souscompte" key="souscode"} - + - + + - + + - - + + {foreach from=$souscompte.journal item="ligne"} + - - + + {/foreach} - - - + + + +

{$souscode|escape} — {$souscode|get_nom_compte|escape}

{$souscode|escape} — {$souscode|get_nom_compte|escape}

DateN°Date IntituléDébitCréditDébitCrédit
{$ligne.id|escape} {$ligne.date|date_fr:'d/m/Y'|escape} {$ligne.libelle|escape}{if $ligne.compte_debit == $souscode}{$ligne.montant|html_money}{/if}{if $ligne.compte_credit == $souscode}{$ligne.montant|html_money}{/if}{if $ligne.compte == $souscode && $ligne.montant > 0}{$ligne.montant|html_money}{/if}{if $ligne.compte == $souscode && $ligne.montant < 0}{$ligne.montant|abs|html_money}{/if}
Solde final{if $souscompte.debit > 0}{$souscompte.debit|html_money}{/if}{if $souscompte.credit > 0}{$souscompte.credit|html_money}{/if}Solde final{if $souscompte.debit != 0}{$souscompte.debit|html_money}{/if}{if $souscompte.credit != 0}{$souscompte.credit|html_money}{/if}
@@ -58,7 +62,7 @@ Total - {$code|get_nom_compte|escape} + {$code|escape} — {$code|get_nom_compte|escape} {if $compte.total > 0}{$compte.total|abs|html_money}{/if} {if $compte.total < 0}{$compte.total|abs|html_money}{/if} @@ -86,4 +90,4 @@

Toutes les opérations sont libellées en {$config.monnaie|escape}.

-{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/exercices/journal.tpl b/templates/admin/compta/exercices/journal.tpl index 5e0e7e6..0eee3ac 100644 --- a/templates/admin/compta/exercices/journal.tpl +++ b/templates/admin/compta/exercices/journal.tpl @@ -9,26 +9,29 @@ - + + - - - + + + {foreach from=$journal item="ligne"} - - - - + + + + + + + {if $ligne.montant > 0} - - - + {else} - + + {/if} {/foreach} @@ -36,4 +39,4 @@

Toutes les opérations sont libellées en {$config.monnaie|escape}.

-{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/import.tpl b/templates/admin/compta/import.tpl index c00a844..d89b359 100644 --- a/templates/admin/compta/import.tpl +++ b/templates/admin/compta/import.tpl @@ -22,6 +22,8 @@
obligatoire
+
optionnel
+
obligatoire
@@ -58,4 +60,4 @@ -{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/operations/index.tpl b/templates/admin/compta/operations/index.tpl index 015be37..7a23ddb 100644 --- a/templates/admin/compta/operations/index.tpl +++ b/templates/admin/compta/operations/index.tpl @@ -47,7 +47,7 @@
- + {if !$categorie && $type} {/if} @@ -74,4 +74,4 @@
DateN°Date IntituléComptesDébitCréditComptesDébitCrédit
{$ligne.date|date_fr:'d/m/Y'|escape}{$ligne.libelle|escape}{$ligne.compte_debit|escape} - {$ligne.compte_debit|get_nom_compte|escape}
{if isset($ligne.date)}{$ligne.id|escape}{/if}{if isset($ligne.date)}{$ligne.date|date_fr:'d/m/Y'|escape}{/if}{if isset($ligne.date)}{$ligne.libelle|escape}{/if}{$ligne.compte|escape}{$ligne.compte|get_nom_compte|escape}{$ligne.montant|html_money}
{$ligne.compte_credit|escape} - {$ligne.compte_credit|get_nom_compte|escape}{$ligne.montant|html_money}{$ligne.montant_oppose|html_money}
{$ligne.date|date_fr:'d/m/Y'|escape} {$ligne.montant|html_money} {$config.monnaie|escape}{$ligne.libelle|escape}{$ligne.libelle|escape}{$ligne.categorie|escape}
-{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/operations/modifier.tpl b/templates/admin/compta/operations/modifier.tpl index 9d6e2f2..c992605 100644 --- a/templates/admin/compta/operations/modifier.tpl +++ b/templates/admin/compta/operations/modifier.tpl @@ -6,6 +6,12 @@

{/if} +{if $debug} +

+ {$debug|escape} +

+{/if} +
@@ -15,18 +21,32 @@
obligatoire
-
obligatoire
-
{$config.monnaie|escape}
{if is_null($type)} -
obligatoire
-
- {select_compte comptes=$comptes name="compte_debit" data=$operation} -
-
obligatoire
-
- {select_compte comptes=$comptes name="compte_credit" data=$operation} -
+
+ Flux obligatoire + + + + + + + + + {foreach from=$fluxs item="flux" key="flux_n" name="fluxs"} + + + {if !$tpl.foreach.fluxs.last} + + {/if} + + {/foreach} + +
{select_compte comptes=$comptes name="compte" form_name="compte_$flux_n" id="f_compte_$flux_n" data=$flux} {$config.monnaie|escape}
+
{else}
obligatoire
@@ -55,7 +75,56 @@
-{if !is_null($type)} +{if is_null($type)} + +{else}
Catégorie
@@ -97,4 +166,4 @@ -{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/operations/saisir.tpl b/templates/admin/compta/operations/saisir.tpl index 30c103e..ff6efd9 100644 --- a/templates/admin/compta/operations/saisir.tpl +++ b/templates/admin/compta/operations/saisir.tpl @@ -6,6 +6,12 @@

{/if} +{if $debug} +

+ {$debug|escape} +

+{/if} + {if $ok}

L'opération numéro {$ok|escape} a été ajoutée. @@ -30,18 +36,32 @@

obligatoire
-
obligatoire
-
{$config.monnaie|escape}
{if is_null($type)} -
obligatoire
-
- {select_compte comptes=$comptes name="compte_debit"} -
-
obligatoire
-
- {select_compte comptes=$comptes name="compte_credit"} -
+
+ Flux obligatoire + + + + + + + + + {foreach from=$fluxs item="flux" key="flux_n" name="fluxs"} + + + {if !$tpl.foreach.fluxs.last} + + {/if} + + {/foreach} + +
{select_compte comptes=$comptes name="compte" form_name="compte_$flux_n" id="f_compte_$flux_n" data=$flux} {$config.monnaie|escape}
+
{elseif $type === 'virement'}
@@ -98,7 +118,56 @@
-{if $type == Garradin\Compta_Categories::DEPENSES || $type == Garradin\Compta_Categories::RECETTES || $type == 'dette'} +{if is_null($type)} + +{elseif $type == Garradin\Compta_Categories::DEPENSES || $type == Garradin\Compta_Categories::RECETTES || $type == 'dette'}
Catégorie
@@ -140,4 +209,4 @@ -{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/compta/operations/voir.tpl b/templates/admin/compta/operations/voir.tpl index 2d9e37a..3258591 100644 --- a/templates/admin/compta/operations/voir.tpl +++ b/templates/admin/compta/operations/voir.tpl @@ -12,8 +12,6 @@
{$operation.date|date_fr:'l j F Y (d/m/Y)'}
Libellé
{$operation.libelle|escape}
-
Montant
-
{$operation.montant|html_money} {$config.monnaie|escape}
Numéro pièce comptable
{if trim($operation.numero_piece)}{$operation.numero_piece|escape}{else}Non renseigné{/if}
@@ -63,28 +61,29 @@
{if trim($operation.remarques)}{$operation.remarques|escape}{else}Non renseigné{/if}
- +
- - + + + {foreach from=$fluxs item="flux"} - - - + + + {if $flux.montant > 0} + - - - - + {else} - + + {/if} + {/foreach}
ComptesDébitCréditDébitCrédit
{$operation.compte_debit|escape}{$nom_compte_debit}{$operation.montant|html_money} {$config.monnaie|escape}{$flux.compte|escape}{$flux.compte_libelle}{$flux.montant|html_money} {$config.monnaie|escape}
{$operation.compte_credit|escape}{$nom_compte_credit}{$operation.montant|html_money} {$config.monnaie|escape}{$flux.montant_oppose|html_money} {$config.monnaie|escape}
-{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/templates/admin/config/membres.tpl b/templates/admin/config/membres.tpl index 8a6649b..7e2a548 100644 --- a/templates/admin/config/membres.tpl +++ b/templates/admin/config/membres.tpl @@ -18,6 +18,14 @@ {/if} {/if} +{if $debug} +

+ TOTO + {$debug|escape} +

+{/if} + + {if $review}
Fiche membre exemple @@ -350,4 +358,4 @@ {/if} -{include file="admin/_foot.tpl"} \ No newline at end of file +{include file="admin/_foot.tpl"} diff --git a/www/admin/compta/comptes/journal.php b/www/admin/compta/comptes/journal.php index 9d96838..2491d69 100644 --- a/www/admin/compta/comptes/journal.php +++ b/www/admin/compta/comptes/journal.php @@ -14,21 +14,10 @@ $journal = new Compta_Journal; $solde = $journal->getSolde($compte['id']); -if (($compte['position'] & Compta_Comptes::ACTIF) || ($compte['position'] & Compta_Comptes::CHARGE)) -{ - $tpl->assign('credit', '-'); - $tpl->assign('debit', '+'); -} -else -{ - $tpl->assign('credit', '+'); - $tpl->assign('debit', '-'); -} - $tpl->assign('compte', $compte); $tpl->assign('solde', $solde); $tpl->assign('journal', $journal->getJournalCompte($compte['id'])); $tpl->display('admin/compta/comptes/journal.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/exercices/compte_resultat.php b/www/admin/compta/exercices/compte_resultat.php index f3e0f12..e3b003b 100644 --- a/www/admin/compta/exercices/compte_resultat.php +++ b/www/admin/compta/exercices/compte_resultat.php @@ -21,6 +21,7 @@ function get_nom_compte($compte) } $tpl->register_modifier('get_nom_compte', 'Garradin\get_nom_compte'); + $tpl->assign('compte_resultat', $exercices->getCompteResultat($exercice['id'])); $tpl->assign('cloture', $exercice['cloture'] ? $exercice['fin'] : time()); @@ -28,4 +29,4 @@ $tpl->assign('exercice', $exercice); $tpl->display('admin/compta/exercices/compte_resultat.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/exercices/journal.php b/www/admin/compta/exercices/journal.php index 8885012..af51a83 100644 --- a/www/admin/compta/exercices/journal.php +++ b/www/admin/compta/exercices/journal.php @@ -24,11 +24,25 @@ function get_nom_compte($compte) } $tpl->register_modifier('get_nom_compte', 'Garradin\get_nom_compte'); -$tpl->assign('journal', $exercices->getJournal($exercice['id'])); +$journal = $exercices->getJournal($exercice['id']); +$id = -1; +foreach ($journal as &$ligne) +{ + if ($ligne['id'] == $id) + { + unset($ligne['date']); + unset($ligne['libelle']); + } + else + { + $id = $ligne['id']; + } +} +$tpl->assign('journal', $journal); $tpl->assign('cloture', $exercice['cloture'] ? $exercice['fin'] : time()); $tpl->assign('exercice', $exercice); $tpl->display('admin/compta/exercices/journal.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/import.php b/www/admin/compta/import.php index 22a1137..b14fe02 100644 --- a/www/admin/compta/import.php +++ b/www/admin/compta/import.php @@ -10,6 +10,7 @@ if ($user['droits']['compta'] < Membres::DROIT_ADMIN) $e = new Compta_Exercices; $import = new Compta_Import; +$comptes = new Compta_Comptes; if (isset($_GET['export'])) { @@ -27,7 +28,7 @@ if (!empty($_POST['import'])) { $error = 'Une erreur est survenue, merci de renvoyer le formulaire.'; } - elseif (empty($_FILES['upload']['tmp_name'])) + if (empty($_FILES['upload']['tmp_name'])) { $error = 'Aucun fichier fourni.'; } @@ -35,13 +36,23 @@ if (!empty($_POST['import'])) { try { + if (isset($_FILES['upload-plan-comptable'])) + { + if (!$comptes->importPlan($_FILES['upload-plan-comptable']['tmp_name'])) + { + throw new UserException('Erreur lors de l’import du plan comptable'); + } + } if (utils::post('type') == 'citizen') { $import->fromCitizen($_FILES['upload']['tmp_name']); } elseif (utils::post('type') == 'garradin') { - $import->fromCSV($_FILES['upload']['tmp_name']); + if (!$import->fromCSV($_FILES['upload']['tmp_name'])) + { + throw new UserException('Erreur lors de l’import'); + } } else { @@ -62,4 +73,4 @@ $tpl->assign('ok', isset($_GET['ok']) ? true : false); $tpl->display('admin/compta/import.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/operations/index.php b/www/admin/compta/operations/index.php index e257d68..e225b10 100644 --- a/www/admin/compta/operations/index.php +++ b/www/admin/compta/operations/index.php @@ -32,7 +32,6 @@ $journal = new Compta_Journal; $list = $journal->getListForCategory($type === Compta_Categories::AUTRES ? null : $type, $cat ? $cat['id'] : null); $tpl->assign('categorie', $cat); -$tpl->assign('journal', $list); $tpl->assign('type', $type); if ($type !== Compta_Categories::AUTRES) @@ -41,14 +40,19 @@ if ($type !== Compta_Categories::AUTRES) } $total = 0.0; - -foreach ($list as $row) +foreach ($list as &$row) { - $total += (float) $row['montant']; + $db = DB::getInstance(); + $row['montant'] = + $db->simpleQuerySingle(' + SELECT sum(CASE WHEN montant > 0 THEN montant ELSE 0 END) + FROM compta_flux + WHERE compta_flux.id_journal = ?', false, $row['id']); + $total += round((float) $row['montant'], 2); } - +$tpl->assign('journal', $list); $tpl->assign('total', $total); $tpl->display('admin/compta/operations/index.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/operations/membre.php b/www/admin/compta/operations/membre.php index 934f203..9e925c2 100644 --- a/www/admin/compta/operations/membre.php +++ b/www/admin/compta/operations/membre.php @@ -48,4 +48,4 @@ $tpl->assign('membre', $membre); $tpl->display('admin/compta/operations/membre.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/operations/modifier.php b/www/admin/compta/operations/modifier.php index 660b879..42f5f0b 100644 --- a/www/admin/compta/operations/modifier.php +++ b/www/admin/compta/operations/modifier.php @@ -19,6 +19,8 @@ if (!$operation) throw new UserException("L'opération demandée n'existe pas."); } +$debug = "_POST=".print_r($_POST, true); + if ($operation['id_categorie']) { $categorie = $cats->get($operation['id_categorie']); @@ -37,6 +39,13 @@ else $type = null; } +foreach ($operation['fluxs'] as $key => &$flux) +{ + $compte = $comptes->get($flux['compte']); + $flux['compte_libelle'] = $compte['libelle']; +} +$tpl->assign('fluxs', $operation['fluxs']); + $error = false; if (!empty($_POST['save'])) @@ -51,12 +60,33 @@ if (!empty($_POST['save'])) { if (is_null($type)) { + $fluxs = []; + $solde = 0.0; + for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++) + { + $compte = $_POST['compte_'.$n]; + if (array_key_exists('montant_'.$n, $_POST)) { + $montant = $_POST['montant_'.$n]; + if ($montant == 0.0) + { + continue; + } + else + { + $solde += $montant; + $fluxs[] = ['compte' => $compte, 'montant' => $montant]; + } + } else { + if ($solde != 0.0 || $n < 1) { + $fluxs[] = ['compte' => $compte, 'montant' => - $solde]; + } + break; + } + } $journal->edit($operation['id'], [ 'libelle' => utils::post('libelle'), - 'montant' => utils::post('montant'), + 'fluxs' => $fluxs, 'date' => utils::post('date'), - 'compte_credit' => utils::post('compte_credit'), - 'compte_debit' => utils::post('compte_debit'), 'numero_piece' => utils::post('numero_piece'), 'remarques' => utils::post('remarques'), ]); @@ -109,12 +139,12 @@ if (!empty($_POST['save'])) $journal->edit($operation['id'], [ 'libelle' => utils::post('libelle'), - 'montant' => utils::post('montant'), 'date' => utils::post('date'), + 'fluxs' => + [ ['compte'=>$credit, 'montant' => - utils::post('montant')] + , ['compte'=>$debit, 'montant' => utils::post('montant')] ], 'moyen_paiement'=> utils::post('moyen_paiement'), 'numero_cheque' => utils::post('numero_cheque'), - 'compte_credit' => $credit, - 'compte_debit' => $debit, 'numero_piece' => utils::post('numero_piece'), 'remarques' => utils::post('remarques'), 'id_categorie' => (int)$cat['id'], @@ -145,8 +175,9 @@ else $tpl->assign('comptes_bancaires', $banques->getList()); } +$tpl->assign('debug', $debug); $tpl->assign('operation', $operation); $tpl->display('admin/compta/operations/modifier.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/operations/saisir.php b/www/admin/compta/operations/saisir.php index 8847861..57a91dd 100644 --- a/www/admin/compta/operations/saisir.php +++ b/www/admin/compta/operations/saisir.php @@ -27,6 +27,7 @@ else $type = Compta_Categories::RECETTES; $error = false; +$debug = "_POST=".print_r($_POST, true); if (!empty($_POST['save'])) { @@ -40,12 +41,34 @@ if (!empty($_POST['save'])) { if (is_null($type)) { + $fluxs = []; + $solde = 0.0; + for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++) + { + $compte = $_POST['compte_'.$n]; + if (array_key_exists('montant_'.$n, $_POST)) { + $montant = $_POST['montant_'.$n]; + if ($montant == 0.0) + { + continue; + } + else + { + $solde += $montant; + $fluxs[] = ['compte' => $compte, 'montant' => $montant]; + } + } else { + if ($solde != 0.0) { + $fluxs[] = ['compte' => $compte, 'montant' => - $solde]; + } + break; + } + } + $debug .= "
fluxs = ".print_r($fluxs, true); $id = $journal->add([ 'libelle' => utils::post('libelle'), - 'montant' => utils::post('montant'), 'date' => utils::post('date'), - 'compte_credit' => utils::post('compte_credit'), - 'compte_debit' => utils::post('compte_debit'), + 'fluxs' => $fluxs, 'numero_piece' => utils::post('numero_piece'), 'remarques' => utils::post('remarques'), 'id_auteur' => $user['id'], @@ -55,10 +78,10 @@ if (!empty($_POST['save'])) { $id = $journal->add([ 'libelle' => utils::post('libelle'), - 'montant' => utils::post('montant'), 'date' => utils::post('date'), - 'compte_credit' => utils::post('compte1'), - 'compte_debit' => utils::post('compte2'), + 'fluxs' => + [ ['compte'=>utils::post('compte1'), 'montant' => - utils::post('montant')] + , ['compte'=>utils::post('compte2'), 'montant' => utils::post('montant')] ], 'numero_piece' => utils::post('numero_piece'), 'remarques' => utils::post('remarques'), 'id_auteur' => $user['id'], @@ -123,12 +146,12 @@ if (!empty($_POST['save'])) $id = $journal->add([ 'libelle' => utils::post('libelle'), - 'montant' => utils::post('montant'), 'date' => utils::post('date'), + 'fluxs' => + [ ['compte'=>$credit, 'montant' => - utils::post('montant')] + , ['compte'=>$debit, 'montant' => utils::post('montant')] ], 'moyen_paiement'=> ($type === 'dette') ? null : utils::post('moyen_paiement'), 'numero_cheque' => ($type === 'dette') ? null : utils::post('numero_cheque'), - 'compte_credit' => $credit, - 'compte_debit' => $debit, 'numero_piece' => utils::post('numero_piece'), 'remarques' => utils::post('remarques'), 'id_categorie' => ($type === 'dette') ? null : (int)$cat['id'], @@ -155,12 +178,17 @@ if (!empty($_POST['save'])) } $tpl->assign('error', $error); - +$tpl->assign('debug', $debug."END"); $tpl->assign('type', $type); -if ($type === null) +if (is_null($type)) { $tpl->assign('comptes', $comptes->listTree()); + $tpl->assign('fluxs', + [ 0 => ['compte' => '', 'montant' => 0.0] + , 1 => ['compte' => '', 'montant' => 0.0] + ] + ); } else { @@ -191,4 +219,4 @@ $tpl->assign('ok', (int) utils::get('ok')); $tpl->display('admin/compta/operations/saisir.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/compta/operations/voir.php b/www/admin/compta/operations/voir.php index 57d2ca3..ea02e7f 100644 --- a/www/admin/compta/operations/voir.php +++ b/www/admin/compta/operations/voir.php @@ -15,11 +15,13 @@ $exercices = new Compta_Exercices; $tpl->assign('operation', $operation); -$credit = $comptes->get($operation['compte_credit']); -$tpl->assign('nom_compte_credit', $credit['libelle']); - -$debit = $comptes->get($operation['compte_debit']); -$tpl->assign('nom_compte_debit', $debit['libelle']); +foreach ($operation['fluxs'] as $key => &$flux) +{ + $compte = $comptes->get($flux['compte']); + $flux['compte_libelle'] = $compte['libelle']; + $flux['montant_oppose'] = - $flux['montant']; +} +$tpl->assign('fluxs', $operation['fluxs']); $tpl->assign('exercice', $exercices->get($operation['id_exercice'])); @@ -50,4 +52,4 @@ if ($operation['id_auteur']) $tpl->display('admin/compta/operations/voir.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/config/membres.php b/www/admin/config/membres.php index b0d8653..a761d8a 100644 --- a/www/admin/config/membres.php +++ b/www/admin/config/membres.php @@ -25,6 +25,8 @@ if (isset($_GET['ok'])) $error = 'OK'; } +$debug = "_POST=".print_r($_POST, true); + if (!empty($_POST['save']) || !empty($_POST['add']) || !empty($_POST['review']) || !empty($_POST['reset'])) { if (!utils::CSRF_check('config_membres')) @@ -121,6 +123,7 @@ if (!empty($_POST['save']) || !empty($_POST['add']) || !empty($_POST['review']) } $tpl->assign('error', $error); +$tpl->assign('debug', $debug); $tpl->assign('review', isset($_GET['review']) ? true : false); $types = $champs->getTypes(); @@ -139,4 +142,4 @@ $tpl->assign('csrf_value', utils::CSRF_create('config_membres')); $tpl->display('admin/config/membres.tpl'); -?> \ No newline at end of file +?> diff --git a/www/admin/static/admin.css b/www/admin/static/admin.css index 21762a9..592039d 100644 --- a/www/admin/static/admin.css +++ b/www/admin/static/admin.css @@ -170,7 +170,9 @@ dl dt label { font-weight: bold; } -fieldset dl dt b { +fieldset dl dt b, +fieldset table th b, +fieldset legend b { color: #900; font-size: 0.7em; font-weight: normal; @@ -418,7 +420,7 @@ table.list.multi tr:nth-child(even) { background: inherit; } -table.list.multi tr:nth-child(4n+1), table.list.multi tr:nth-child(4n+2) { +table.list.multi tr.odd { background: rgb(255, 174, 80); background: rgba(217, 134, 40, 0.2); } diff --git a/www/admin/static/handheld.css b/www/admin/static/handheld.css index 1229829..1f6f5d1 100644 --- a/www/admin/static/handheld.css +++ b/www/admin/static/handheld.css @@ -122,6 +122,10 @@ dl.describe dt, dl.describe dd { width: auto !important; } + table.list.table td, table.list.table th { + display: table-cell !important; + } + colgroup { /* Hack pour désactiver les largeurs de colonnes */ display: none; @@ -135,4 +139,4 @@ dl.describe dt, dl.describe dd { float: none; width: auto; } -} \ No newline at end of file +} -- 2.20.1