Index: src/include/class.membres_transactions.php ================================================================== --- src/include/class.membres_transactions.php +++ src/include/class.membres_transactions.php @@ -76,60 +76,31 @@ 'id_transaction' => $data['id_transaction'], 'montant' => $data['montant'], 'libelle' => $data['libelle'], 'id_membre' => $data['id_membre'], ]); - + $id = $db->lastInsertRowId(); $id_cat = $db->simpleQuerySingle('SELECT id_categorie_compta FROM transactions WHERE id = ?;', false, (int)$data['id_transaction']); if ($id_cat) { - $journal = new Compta_Journal; - try { - if ($data['moyen_paiement'] != 'ES') - { - if (trim($data['banque']) == '') - { - throw new UserException('Le compte bancaire choisi est invalide.'); - } - - if (!$db->simpleQuerySingle('SELECT 1 FROM compta_comptes_bancaires WHERE id = ?;', - false, $data['banque'])) - { - throw new UserException('Le compte bancaire choisi n\'existe pas.'); - } - - $debit = $data['banque']; - } - else - { - $debit = Compta_Comptes::CAISSE; - } - - $credit = $db->simpleQuerySingle('SELECT compte FROM compta_categories WHERE id = ?;', - false, $id_cat); - - $id_operation = $journal->add([ - 'libelle' => $data['libelle'], - 'montant' => $data['montant'], - 'date' => $data['date'], - 'moyen_paiement'=> $data['moyen_paiement'], - 'numero_cheque' => $data['numero_cheque'], - 'compte_debit' => $debit, - 'compte_credit' => $credit, - 'id_categorie' => (int)$id_cat, - 'id_auteur' => $data['id_auteur'], - ]); - - $db->simpleInsert('membres_transactions_operations', [ - 'id_operation' => $id_operation, - 'id_membre_transaction' => $id, - ]); + $this->addOperationCompta($id, [ + 'id_categorie' => $id_cat, + 'libelle' => $data['libelle'], + 'montant' => $data['montant'], + 'date' => $data['date'], + 'moyen_paiement'=> $data['moyen_paiement'], + 'numero_cheque' => $data['numero_cheque'], + 'id_auteur' => $data['id_auteur'], + 'moyen_paiement'=> $data['moyen_paiement'], + 'numero_cheque' => $data['numero_cheque'], + 'banque' => $data['banque'], + ]); } catch (\Exception $e) { $db->exec('ROLLBACK;'); throw $e; @@ -190,24 +161,96 @@ * @param int $id Numéro de la transaction * @param array $data Données */ public function addOperationCompta($id, $data) { + $journal = new Compta_Journal; + $db = DB::getInstance(); + + if (!isset($data['libelle']) || trim($data['libelle']) == '') + { + throw new UserException('Le libellé ne peut rester vide.'); + } + + $data['libelle'] = trim($data['libelle']); + + if (!isset($data['montant']) || !is_numeric($data['montant']) || (float)$data['montant'] < 0) + { + throw new UserException('Le montant doit être un nombre positif et valide.'); + } + + $data['montant'] = (float) $data['montant']; + + if (empty($data['date']) || !utils::checkDate($data['date'])) + { + throw new UserException('Date vide ou invalide.'); + } + + if (!isset($data['moyen_paiement']) || trim($data['moyen_paiement']) === '') + { + throw new UserException('Moyen de paiement inconnu ou invalide.'); + } + + if ($data['moyen_paiement'] != 'ES') + { + if (trim($data['banque']) == '') + { + throw new UserException('Le compte bancaire choisi est invalide.'); + } + + if (!$db->simpleQuerySingle('SELECT 1 FROM compta_comptes_bancaires WHERE id = ?;', + false, $data['banque'])) + { + throw new UserException('Le compte bancaire choisi n\'existe pas.'); + } + + $debit = $data['banque']; + } + else + { + $debit = Compta_Comptes::CAISSE; + } + + $credit = $db->simpleQuerySingle('SELECT compte FROM compta_categories WHERE id = ?;', + false, $data['id_categorie']); + + $id_operation = $journal->add([ + 'libelle' => $data['libelle'], + 'montant' => $data['montant'], + 'date' => $data['date'], + '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'], + ]); + + $db->simpleInsert('membres_transactions_operations', [ + 'id_operation' => $id_operation, + 'id_membre_transaction' => $id, + ]); + return $id_operation; } + /** + * Renvoie un paiement membre + * @param integer $id Numéro du paiement + * @return array Données du paiement + */ public function get($id) { $db = DB::getInstance(); return $db->simpleQuerySingle('SELECT * FROM membres_transactions WHERE id = ?;', true, (int) $id); } public function listForMember($id) { $db = DB::getInstance(); - return $db->simpleStatementFetch('SELECT mtr.*, - tr.intitule, tr.duree, tr.debut, tr.fin + return $db->simpleStatementFetch('SELECT mtr.*, tr.intitule, tr.duree, tr.debut, tr.fin, + (SELECT COUNT(*) FROM membres_transactions_operations WHERE id_membre_transaction = mtr.id) AS nb_operations FROM membres_transactions AS mtr LEFT JOIN transactions AS tr ON tr.id = mtr.id_transaction WHERE mtr.id_membre = ? ORDER BY mtr.date DESC;', \SQLITE3_ASSOC, (int)$id); } @@ -230,12 +273,11 @@ } public function isMemberUpToDate($id, $id_transaction) { $db = DB::getInstance(); - return $db->simpleQuerySingle(' - SELECT + return $db->simpleQuerySingle('SELECT SUM(mtr.montant) AS total, tr.montant, tr.montant - SUM(mtr.montant) AS a_payer, tr.intitule, tr.duree, tr.debut, tr.fin, CASE WHEN tr.duree IS NOT NULL THEN date(mtr.date, \'+\'||tr.duree||\' days\') WHEN tr.fin IS NOT NULL THEN tr.fin ELSE NULL END AS expiration FROM transactions AS tr Index: src/templates/admin/membres/transactions.tpl ================================================================== --- src/templates/admin/membres/transactions.tpl +++ src/templates/admin/membres/transactions.tpl @@ -64,13 +64,14 @@ {if !empty($paiements)}
Date | -Intitulé | +Intitulé | Montant | Activité ou cotisation liée | +Écritures liées | {foreach from=$paiements item="p"} |
---|---|
+ {if $p.nb_operations > 0} + {$p.nb_operations|escape} + {else} + 0 {/if} | ✎ ✘ |