Overview
Comment:Export ODS compta et membres
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA1: e8d51c046abba20465b522b23b06b78870cd8b62
User & Date: bohwaz on 2018-07-09 15:27:50
Other Links: manifest | tags
Context
2018-07-11
10:55
Fix : identité du membre n'était pas affichée sur la page d'accueil check-in: 285a6b0597 user: bohwaz tags: trunk, stable
2018-07-09
15:27
Export ODS compta et membres check-in: e8d51c046a user: bohwaz tags: trunk, stable
15:16
Corrige affichage des cotisations par période, signalé par @Daniel check-in: ede5c3863e user: bohwaz tags: trunk
Changes

Modified src/include/lib/Garradin/Compta/Comptes.php from [f2bc5722a8] to [413403054e].

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        }

        if ($db->test('compta_comptes_bancaires', $db->where('id', $id)))
        {
            throw new UserException('Ce compte ne peut être supprimé car il est lié à un compte bancaire.');
        }

        if ($db->test('compta_categories', $db->where('compte', $id)))
        {
            throw new UserException('Ce compte ne peut être supprimé car des catégories y sont liées.');
        }

        $db->delete('compta_comptes', $db->where('id', $id));

        return true;
    }

    /**
     * Peut-on supprimer ce compte ? (OUI s'il n'a pas d'écriture liée)







<
<
<
<
<







159
160
161
162
163
164
165





166
167
168
169
170
171
172
        }

        if ($db->test('compta_comptes_bancaires', $db->where('id', $id)))
        {
            throw new UserException('Ce compte ne peut être supprimé car il est lié à un compte bancaire.');
        }






        $db->delete('compta_comptes', $db->where('id', $id));

        return true;
    }

    /**
     * Peut-on supprimer ce compte ? (OUI s'il n'a pas d'écriture liée)

Modified src/include/lib/Garradin/Compta/Import.php from [5efe3ab736] to [434894af9b].

1
2
3
4
5
6
7
8


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace Garradin\Compta;

use \Garradin\DB;
use \Garradin\Utils;
use \Garradin\UserException;



class Import
{
	protected $csv_header = [
		'Numéro mouvement',
		'Date',
		'Type de mouvement',
		'Catégorie',
		'Libellé',
		'Montant',
		'Compte de débit - numéro',
		'Compte de débit - libellé',
		'Compte de crédit - numéro',
		'Compte de crédit - libellé',
		'Moyen de paiement',
		'Numéro de chèque',
		'Numéro de pièce',
		'Remarques'
	];

	public function toCSV($exercice)
	{
		$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,








>
>


|
















|

|
<
<







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


33
34
35
36
37
38
39
<?php

namespace Garradin\Compta;

use \Garradin\DB;
use \Garradin\Utils;
use \Garradin\UserException;

use KD2\ODSWriter;

class Import
{
	protected $header = [
		'Numéro mouvement',
		'Date',
		'Type de mouvement',
		'Catégorie',
		'Libellé',
		'Montant',
		'Compte de débit - numéro',
		'Compte de débit - libellé',
		'Compte de crédit - numéro',
		'Compte de crédit - libellé',
		'Moyen de paiement',
		'Numéro de chèque',
		'Numéro de pièce',
		'Remarques'
	];

	protected function export($exercice)
	{
		return DB::getInstance()->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,
48
49
50
51
52
53
54





55
56
57
58
59
60
61
62
63
64
65
66
67
68

















69
70
71
72
73
74
75
				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
			WHERE id_exercice = '.(int)$exercice.'
			ORDER BY journal.date;
		')->execute();






		$fp = fopen('php://output', 'w');

		fputcsv($fp, $this->csv_header);

		while ($row = $res->fetchArray(SQLITE3_ASSOC))
		{
			fputcsv($fp, $row);
		}

		fclose($fp);

		return true;
	}


















	public function fromCSV($path)
	{
		if (!file_exists($path) || !is_readable($path))
		{
			throw new \RuntimeException('Fichier inconnu : '.$path);
		}







>
>
>
>
>



|










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
				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
			WHERE id_exercice = '.(int)$exercice.'
			ORDER BY journal.date;
		')->execute();
	}

	public function toCSV($exercice)
	{
		$res = $this->export($exercice);

		$fp = fopen('php://output', 'w');

		fputcsv($fp, $this->header);

		while ($row = $res->fetchArray(SQLITE3_ASSOC))
		{
			fputcsv($fp, $row);
		}

		fclose($fp);

		return true;
	}

    public function toODS($exercice)
    {
    	$result = $this->export($exercice);
        $ods = new ODSWriter;
        $ods->table_name = 'Journal';

        $ods->add($this->header);

        while ($row = $result->fetchArray(SQLITE3_ASSOC))
        {
        	unset($row->passe);
        	$ods->add($row);
        }

        $ods->output();
    }

	public function fromCSV($path)
	{
		if (!file_exists($path) || !is_readable($path))
		{
			throw new \RuntimeException('Fichier inconnu : '.$path);
		}

Modified src/include/lib/Garradin/Membres/Import.php from [657b6765f3] to [032b787733].

1
2
3
4
5
6
7
8
9
10


11
12
13
14
15
16
17
<?php

namespace Garradin\Membres;

use Garradin\Membres;
use Garradin\Config;
use Garradin\DB;
use Garradin\Utils;
use Garradin\UserException;



class Import
{
	/**
	 * Champs du CSV de Galette
	 * les lignes vides ('') ne seront pas proposées à l'import
	 * @var array
	 */










>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

namespace Garradin\Membres;

use Garradin\Membres;
use Garradin\Config;
use Garradin\DB;
use Garradin\Utils;
use Garradin\UserException;

use KD2\ODSWriter;

class Import
{
	/**
	 * Champs du CSV de Galette
	 * les lignes vides ('') ne seront pas proposées à l'import
	 * @var array
	 */
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274







275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296


















		$db->commit();

		fclose($fp);
		return true;
	}

    public function toCSV($excel = false)
    {
        $db = DB::getInstance();

        $champs = Config::getInstance()->get('champs_membres')->getKeys();
        $champs = 'm.' . implode(', m.', $champs);

        $res = $db->prepare('SELECT ' . $champs . ', c.nom AS categorie FROM membres AS m 
            LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;')->execute();








        $fp = fopen('php://output', 'w');
        fputs($fp, Utils::csv_header($excel));
        $header = false;

        while ($row = $res->fetchArray(SQLITE3_ASSOC))
        {
            unset($row->passe);

            if (!$header)
            {
                fputs($fp, Utils::row_to_csv(array_keys($row), $excel));
                $header = true;
            }

            fputs($fp, Utils::row_to_csv($row, $excel));
        }

        fclose($fp);

        return true;
    }
}
























|




|

|


>
>
>
>
>
>
>

<


|





|



|






|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

		$db->commit();

		fclose($fp);
		return true;
	}

	protected function export()
    {
        $db = DB::getInstance();

        $champs = Config::getInstance()->get('champs_membres')->getKeys();
        $champs_sql = 'm.' . implode(', m.', $champs);

        $res = $db->prepare('SELECT ' . $champs_sql . ', c.nom AS categorie FROM membres AS m 
            LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;')->execute();

        return [array_merge($champs, ['categorie']), $res];
	}

    public function toCSV()
    {
    	list($champs, $result) = $this->export();

        $fp = fopen('php://output', 'w');

        $header = false;

        while ($row = $result->fetchArray(SQLITE3_ASSOC))
        {
            unset($row->passe);

            if (!$header)
            {
                fputs($fp, Utils::row_to_csv(array_keys($row)));
                $header = true;
            }

            fputs($fp, Utils::row_to_csv($row));
        }

        fclose($fp);

        return true;
    }

    public function toODS()
    {
    	list($champs, $result) = $this->export();
        $ods = new ODSWriter;
        $ods->table_name = 'Membres';

        $ods->add($champs);

        while ($row = $result->fetchArray(SQLITE3_ASSOC))
        {
        	unset($row->passe);
        	$ods->add($row);
        }

        $ods->output();
    }
}

Modified src/include/lib/Garradin/Utils.php from [56babf6e61] to [cb5d0acf56].

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
        // Skip BOM
        if (fgets($fp, 4) !== chr(0xEF) . chr(0xBB) . chr(0xBF))
        {
            fseek($fp, 0);
        }
    }

    static public function row_to_csv($row, $excel = false)
    {
        $row = (array) $row;

        $eol = "\r\n";
        // Excel ne comprends pas les virgules en france, le c*****!
        $separator = $excel ? '";"' : '","';

        array_walk($row, function ($field) {
            return strtr($field, ['"' => '""', "\r\n" => "\n"]);
        });

        $out = '"' . implode($separator, $row) . '"' . $eol;

        return $out;
    }

    static public function csv_header($excel = false)
    {
        if ($excel)
        {
            // BOM spécifique pour Excel sinon il ne sait pas lire l'UTF8!
            return chr(0xEF) . chr(0xBB) . chr(0xBF);
        }

        return '';
    }
}







|



<
<
<
<




<
|
<

|
<
<
<
<
<
<
<
<
<
<
<
719
720
721
722
723
724
725
726
727
728
729




730
731
732
733

734

735
736











        // Skip BOM
        if (fgets($fp, 4) !== chr(0xEF) . chr(0xBB) . chr(0xBF))
        {
            fseek($fp, 0);
        }
    }

    static public function row_to_csv($row)
    {
        $row = (array) $row;





        array_walk($row, function ($field) {
            return strtr($field, ['"' => '""', "\r\n" => "\n"]);
        });


        return sprintf("\"%s\"\r\n", implode('","', $row));

    }
}











Modified src/templates/admin/compta/import.tpl from [41a1fc641c] to [d8c04dcc34].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
{include file="admin/_head.tpl" title="Import / Export" current="compta"}

{form_errors}

{if $ok}
    <p class="confirm">
        L'import s'est bien déroulé.
    </p>
{/if}

<ul class="actions">
    <li class="current"><a href="{$admin_url}compta/import.php">Importer</a></li>
    <li><a href="{$admin_url}compta/import.php?export">Exporter en CSV</a></li>

</ul>

<form method="post" action="{$self_url}" enctype="multipart/form-data">

    <fieldset>
        <legend>Importer depuis un fichier</legend>
        <dl>












|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{include file="admin/_head.tpl" title="Import / Export" current="compta"}

{form_errors}

{if $ok}
    <p class="confirm">
        L'import s'est bien déroulé.
    </p>
{/if}

<ul class="actions">
    <li class="current"><a href="{$admin_url}compta/import.php">Importer</a></li>
    <li><a href="{$admin_url}compta/import.php?export=csv">Exporter en CSV</a></li>
    <li><a href="{$admin_url}compta/import.php?export=ods">Exporter en classeur Office</a></li>
</ul>

<form method="post" action="{$self_url}" enctype="multipart/form-data">

    <fieldset>
        <legend>Importer depuis un fichier</legend>
        <dl>

Modified src/templates/admin/membres/import.tpl from [0e9a87ea52] to [239f90c5ba].

1
2
3
4
5
6
7
8
9
10
11
12
13
{include file="admin/_head.tpl" title="Import & export des membres" current="membres" js=1}

<ul class="actions">
    <li class="current"><a href="{$admin_url}membres/import.php">Importer</a></li>
    <li><a href="{$admin_url}membres/import.php?export">Exporter en CSV</a></li>
    <li><a href="{$admin_url}membres/import.php?export=excel">Exporter en CSV (Excel)</a></li>
</ul>

{form_errors}

{if $ok}
    <p class="confirm">
        L'import s'est bien déroulé.




|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
{include file="admin/_head.tpl" title="Import & export des membres" current="membres" js=1}

<ul class="actions">
    <li class="current"><a href="{$admin_url}membres/import.php">Importer</a></li>
    <li><a href="{$admin_url}membres/import.php?export=csv">Exporter en CSV</a></li>
    <li><a href="{$admin_url}membres/import.php?export=ods">Exporter en classeur Office</a></li>
</ul>

{form_errors}

{if $ok}
    <p class="confirm">
        L'import s'est bien déroulé.

Modified src/www/admin/compta/import.php from [cbf52a3a51] to [7e73141123].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16







17
18
19
20
21
22
23
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$session->requireAccess('compta', Membres::DROIT_ADMIN);

$e = new Compta\Exercices;
$import = new Compta\Import;

if (qg('export') !== null)
{
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="Export comptabilité - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.csv"');
    $import->toCSV($e->getCurrentId());
    exit;







}

if (f('import'))
{
    $form->check('compta_import', [
        'upload' => 'file|required',
        'type'   => 'required|in:citizen,garradin',










|





>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$session->requireAccess('compta', Membres::DROIT_ADMIN);

$e = new Compta\Exercices;
$import = new Compta\Import;

if (qg('export') == 'csv')
{
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="Export comptabilité - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.csv"');
    $import->toCSV($e->getCurrentId());
    exit;
}
elseif (qg('export') == 'ods')
{
    header('Content-type: application/vnd.oasis.opendocument.spreadsheet');
    header('Content-Disposition: attachment; filename="Export comptabilité - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.ods"');
    $import->toODS($e->getCurrentId());
    exit;
}

if (f('import'))
{
    $form->check('compta_import', [
        'upload' => 'file|required',
        'type'   => 'required|in:citizen,garradin',

Modified src/www/admin/membres/import.php from [caede8a449] to [414da74c86].

1
2
3
4
5
6
7
8
9
10


11
12
13
14







15
16
17
18
19
20
21
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$session->requireAccess('membres', Membres::DROIT_ADMIN);

$import = new Membres\Import;

if (null !== qg('export'))


{
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="Export membres - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.csv"');
    $import->toCSV(qg('export') == 'excel' ? true : false);







    exit;
}

$champs = $config->get('champs_membres')->getAll();
$champs->date_inscription = (object) ['title' => 'Date inscription', 'type' => 'date'];

if (f('import'))









|
>
>



|
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$session->requireAccess('membres', Membres::DROIT_ADMIN);

$import = new Membres\Import;

$tpl->assign('tab', null !== qg('export') ? 'export' : 'import');

if (qg('export') == 'csv')
{
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="Export membres - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.csv"');
    $import->toCSV();
    exit;
}
elseif (qg('export') == 'ods')
{
    header('Content-type: application/vnd.oasis.opendocument.spreadsheet');
    header('Content-Disposition: attachment; filename="Export membres - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.ods"');
    $import->toODS();
    exit;
}

$champs = $config->get('champs_membres')->getAll();
$champs->date_inscription = (object) ['title' => 'Date inscription', 'type' => 'date'];

if (f('import'))