Index: src/include/class.cotisations_membres.php ================================================================== --- src/include/class.cotisations_membres.php +++ src/include/class.cotisations_membres.php @@ -9,11 +9,11 @@ /** * Vérification des champs fournis pour la modification de donnée * @param array $data Tableau contenant les champs à ajouter/modifier * @return void */ - protected function _checkFields(&$data) + protected function _checkFields(&$data, $compta = false) { $db = DB::getInstance(); if (empty($data['date']) || !utils::checkDate($data['date'])) { @@ -33,10 +33,32 @@ { throw new UserException('Membre inconnu ou invalide.'); } $data['id_membre'] = (int) $data['id_membre']; + + if ($compta) + { + 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.'); + } + } + } } /** * Enregistrer un événement de cotisation * @param array $data Tableau des champs à insérer @@ -44,11 +66,23 @@ */ public function add($data) { $db = DB::getInstance(); - $this->_checkFields($data); + $co = $db->simpleQuerySingle('SELECT * FROM cotisations WHERE id = ?;', + true, (int)$data['id_cotisation']); + + $this->_checkFields($data, !empty($co['id_categorie_compta'])); + + $check = $db->simpleQuerySingle('SELECT 1 FROM cotisations_membres + WHERE id_cotisation = ? AND id_membre = ? AND date = ?;', + false, (int)$data['id_cotisation'], (int)$data['id_membre'], $data['date']); + + if ($check) + { + throw new UserException('Cette cotisation a déjà été enregistrée pour ce jour-ci et ce membre-ci.'); + } $db->exec('BEGIN;'); $db->simpleInsert('cotisations_membres', [ 'date' => $data['date'], @@ -56,25 +90,22 @@ 'id_membre' => $data['id_membre'], ]); $id = $db->lastInsertRowId(); - $co = $db->simpleQuerySingle('SELECT * FROM cotisations WHERE id = ?;', - true, (int)$data['id_cotisation']); - - if ($co['id_categorie_compta']) + if ($co['id_categorie_compta'] && $co['montant'] > 0) { try { $id_operation = $this->addOperationCompta($id, [ 'id_categorie' => $co['id_categorie_compta'], 'libelle' => 'Cotisation (automatique)', 'montant' => $co['montant'], 'date' => $data['date'], 'moyen_paiement'=> $data['moyen_paiement'], - 'numero_cheque' => $data['numero_cheque'], + 'numero_cheque' => isset($data['numero_cheque']) ? $data['numero_cheque'] : null, 'id_auteur' => $data['id_auteur'], - 'banque' => $data['banque'], + 'banque' => isset($data['banque']) ? $data['banque'] : null, 'id_membre' => $data['id_membre'], ]); } catch (\Exception $e) { @@ -141,33 +172,12 @@ 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; Index: src/templates/admin/membres/cotisations/ajout.tpl ================================================================== --- src/templates/admin/membres/cotisations/ajout.tpl +++ src/templates/admin/membres/cotisations/ajout.tpl @@ -84,11 +84,10 @@
{include file="admin/_foot.tpl"} Index: src/www/admin/membres/cotisations/ajout.php ================================================================== --- src/www/admin/membres/cotisations/ajout.php +++ src/www/admin/membres/cotisations/ajout.php @@ -46,20 +46,16 @@ try { $data = [ 'date' => utils::post('date'), 'id_cotisation' => utils::post('id_cotisation'), 'id_membre' => utils::post('id_membre'), + 'id_auteur' => $user['id'], + 'moyen_paiement' => utils::post('moyen_paiement'), + 'numero_cheque' => utils::post('numero_cheque'), + 'banque' => utils::post('banque'), ]; - if (!empty($categorie['id_cotisation_obligatoire'])) - { - $data['id_auteur'] = $user['id']; - $data['moyen_paiement'] = utils::post('moyen_paiement'); - $data['numero_cheque'] = utils::post('numero_cheque'); - $data['banque'] = utils::post('banque'); - } - $m_cotisations->add($data); utils::redirect('/admin/membres/cotisations.php?id=' . (int)utils::post('id_membre')); } catch (UserException $e)