コード例 #1
0
ファイル: Import.php プロジェクト: kd2org/garradin
    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 Comptes();
        $banques = new Comptes_Bancaires();
        $cats = new Categories();
        $journal = new 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;
    }
コード例 #2
0
ファイル: Import.php プロジェクト: kd2org/garradin
 /**
  * Importer un CSV de la liste des membres depuis un export Garradin
  * @param  string $path 	Chemin vers le CSV
  * @return boolean          TRUE en cas de succès
  */
 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;');
     $membres = new Membres();
     // On récupère les champs qu'on peut importer
     $champs = Config::getInstance()->get('champs_membres')->getAll();
     $champs = array_keys($champs);
     $champs[] = 'date_inscription';
     //$champs[] = 'date_connexion';
     $champs[] = 'id';
     //$champs[] = 'id_categorie';
     $line = 0;
     $delim = Utils::find_csv_delim($fp);
     while (!feof($fp)) {
         $row = fgetcsv($fp, 4096, $delim);
         $line++;
         if (empty($row)) {
             continue;
         }
         if ($line == 1) {
             if (is_numeric($row[0])) {
                 throw new UserException('Erreur sur la ligne 1 : devrait contenir l\'en-tête des colonnes.');
             }
             $columns = array_flip($row);
             continue;
         }
         if (count($row) != count($columns)) {
             $db->exec('ROLLBACK;');
             throw new UserException('Erreur sur la ligne ' . $line . ' : le nombre de colonnes est incorrect.');
         }
         $data = [];
         foreach ($columns as $name => $id) {
             $name = trim($name);
             // Champs qui n'existent pas dans le schéma actuel
             if (!in_array($name, $champs)) {
                 continue;
             }
             if (trim($row[$id]) !== '') {
                 $data[$name] = $row[$id];
             }
         }
         if (!empty($data['id'])) {
             $id = (int) $data['id'];
             unset($data['id']);
         } else {
             $id = false;
         }
         try {
             if ($id) {
                 $membres->edit($id, $data);
             } else {
                 $membres->add($data);
             }
         } catch (UserException $e) {
             $db->exec('ROLLBACK;');
             throw new UserException('Erreur sur la ligne ' . $line . ' : ' . $e->getMessage());
         }
     }
     $db->exec('END;');
     fclose($fp);
     return true;
 }