コード例 #1
0
ファイル: Cotisations.php プロジェクト: kd2org/garradin
 /**
  * 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, $compta = false)
 {
     $db = DB::getInstance();
     if (empty($data['date']) || !Utils::checkDate($data['date'])) {
         throw new UserException('Date vide ou invalide.');
     }
     if (empty($data['id_cotisation']) || !$db->simpleQuerySingle('SELECT 1 FROM cotisations WHERE id = ?;', false, (int) $data['id_cotisation'])) {
         throw new UserException('Cotisation inconnue.');
     }
     $data['id_cotisation'] = (int) $data['id_cotisation'];
     if (empty($data['id_membre']) || !$db->simpleQuerySingle('SELECT 1 FROM membres WHERE id = ?;', false, (int) $data['id_membre'])) {
         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.');
             }
         }
         if (empty($data['montant']) || !is_numeric($data['montant'])) {
             throw new UserException('Le montant indiqué n\'est pas un nombre valide.');
         }
     }
 }
コード例 #2
0
ファイル: Exercices.php プロジェクト: kd2org/garradin
 /**
  * Clôturer un exercice et en ouvrir un nouveau
  * Le report à nouveau n'est pas effectué automatiquement par cette fonction, voir doReports pour ça.
  * @param  integer  $id     ID de l'exercice à clôturer
  * @param  string   $end    Date de clôture de l'exercice au format Y-m-d
  * @return integer          L'ID du nouvel exercice créé
  */
 public function close($id, $end)
 {
     $db = DB::getInstance();
     if (!Utils::checkDate($end)) {
         throw new UserException('Date de fin vide ou invalide.');
     }
     $db->exec('BEGIN;');
     // Clôture de l'exercice
     $db->simpleUpdate('compta_exercices', ['cloture' => 1, 'fin' => $end], 'id = \'' . (int) $id . '\'');
     // Date de début du nouvel exercice : lendemain de la clôture du précédent exercice
     $new_begin = Utils::modifyDate($end, '+1 day');
     // Date de fin du nouvel exercice : un an moins un jour après l'ouverture
     $new_end = Utils::modifyDate($new_begin, '+1 year -1 day');
     // Enfin sauf s'il existe déjà des opérations après cette date, auquel cas la date de fin
     // est fixée à la date de la dernière opération, ceci pour ne pas avoir d'opération
     // orpheline d'exercice
     $last = $db->simpleQuerySingle('SELECT date FROM compta_journal WHERE id_exercice = ? AND date >= ? ORDER BY date DESC LIMIT 1;', false, $id, $new_end);
     $new_end = $last ?: $new_end;
     // Création du nouvel exercice
     $new_id = $this->add(['debut' => $new_begin, 'fin' => $new_end, 'libelle' => 'Nouvel exercice']);
     // Ré-attribution des opérations de l'exercice à clôturer qui ne sont pas dans son
     // intervale au nouvel exercice
     $db->simpleExec('UPDATE compta_journal SET id_exercice = ? WHERE id_exercice = ? AND date >= ?;', $new_id, $id, $new_begin);
     $db->exec('END;');
     return $new_id;
 }
コード例 #3
0
ファイル: Journal.php プロジェクト: kd2org/garradin
 protected function _checkFields(&$data)
 {
     $db = DB::getInstance();
     if (empty($data['libelle']) || !trim($data['libelle'])) {
         throw new UserException('Le libellé ne peut rester vide.');
     }
     $data['libelle'] = trim($data['libelle']);
     if (!empty($data['moyen_paiement']) && !$db->simpleQuerySingle('SELECT 1 FROM compta_moyens_paiement WHERE code = ?;', false, $data['moyen_paiement'])) {
         throw new UserException('Moyen de paiement invalide.');
     }
     if (empty($data['date']) || !Utils::checkDate($data['date'])) {
         throw new UserException('Date vide ou invalide.');
     }
     if (!$db->simpleQuerySingle('SELECT 1 FROM compta_exercices WHERE cloture = 0
         AND debut <= :date AND fin >= :date;', false, ['date' => $data['date']])) {
         throw new UserException('La date ne correspond pas à l\'exercice en cours.');
     }
     if (empty($data['moyen_paiement'])) {
         $data['moyen_paiement'] = null;
         $data['numero_cheque'] = null;
     } else {
         $data['moyen_paiement'] = strtoupper($data['moyen_paiement']);
         if ($data['moyen_paiement'] != 'CH') {
             $data['numero_cheque'] = null;
         }
         if (!$db->simpleQuerySingle('SELECT 1 FROM compta_moyens_paiement WHERE code = ? LIMIT 1;', false, $data['moyen_paiement'])) {
             throw new UserException('Moyen de paiement invalide.');
         }
     }
     $data['montant'] = str_replace(',', '.', $data['montant']);
     $data['montant'] = (double) $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) {
         if (empty($data[$champ]) || !trim($data[$champ])) {
             $data[$champ] = '';
         } else {
             $data[$champ] = trim($data[$champ]);
         }
     }
     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'])) {
         throw new UserException('Compte débité inconnu.');
     }
     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']) {
         throw new UserException('Compte crédité identique au compte débité.');
     }
     if (isset($data['id_categorie'])) {
         if (!$db->simpleQuerySingle('SELECT 1 FROM compta_categories WHERE id = ?;', false, (int) $data['id_categorie'])) {
             throw new UserException('Catégorie inconnue.');
         }
         $data['id_categorie'] = (int) $data['id_categorie'];
     } else {
         $data['id_categorie'] = NULL;
     }
     if (isset($data['id_auteur'])) {
         $data['id_auteur'] = (int) $data['id_auteur'];
     }
     return true;
 }