Overview
Comment:Ajout gestion des lignes dans les mouvements
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 92219561f956e07ee08896c3a119fddf76f4e38d
User & Date: bohwaz on 2019-02-18 17:34:02
Other Links: branch diff | manifest | tags
Context
2019-02-19
16:17
Méthode inutilisée check-in: 88062b512b user: bohwaz tags: dev
2019-02-18
17:34
Ajout gestion des lignes dans les mouvements check-in: 92219561f9 user: bohwaz tags: dev
2019-02-15
16:33
Débuts de manuel check-in: 82f49f41ab user: bohwaz tags: dev
Changes

Modified src/include/data/0.10.0.sql from [2f94b4a011] to [d652ec7957].

1
2
3
4
5
6
7
8
9
10
11
12
ALTER TABLE compta_journal RENAME TO compta_journal_old;

.read schema.sql

INSERT INTO compta_journal (id, libelle, remarques, numero_piece, date, moyen_paiement, numero_cheque, id_exercice, id_auteur, id_categorie, id_projet)
	SELECT id, libelle, remarques, numero_piece, date, moyen_paiement, numero_cheque, id_exercice, id_auteur, id_categorie, id_projet FROM compta_journal_old;

INSERT INTO compta_journal_ecritures (id_journal, compte, debit, credit, montant)
	SELECT id, compte_credit, 0, CAST(montant * 100 AS INT) FROM compta_journal_old;

INSERT INTO compta_journal_ecritures (id_journal, compte, debit, credit, montant)
	SELECT id, compte_debit, CAST(montant * 100 AS INT), 0 FROM compta_journal_old;




|







1
2
3
4
5
6
7
8
9
10
11
12
ALTER TABLE compta_journal RENAME TO compta_journal_old;

.read schema.sql

INSERT INTO compta_journal (id, libelle, remarques, numero_piece, date, moyen_paiement, reference_paiement, id_exercice, id_auteur, id_categorie, id_projet)
	SELECT id, libelle, remarques, numero_piece, date, moyen_paiement, numero_cheque, id_exercice, id_auteur, id_categorie, id_projet FROM compta_journal_old;

INSERT INTO compta_journal_ecritures (id_journal, compte, debit, credit, montant)
	SELECT id, compte_credit, 0, CAST(montant * 100 AS INT) FROM compta_journal_old;

INSERT INTO compta_journal_ecritures (id_journal, compte, debit, credit, montant)
	SELECT id, compte_debit, CAST(montant * 100 AS INT), 0 FROM compta_journal_old;

Modified src/include/data/schema.sql from [ae9748129e] to [67ca2845bf].

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

    libelle TEXT NOT NULL,
    remarques TEXT NULL,
    numero_piece TEXT NULL, -- N° de pièce comptable

    date TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date) IS NOT NULL AND date(date) = date),
    moyen_paiement TEXT NULL,
    numero_cheque TEXT NULL,

    validation INTEGER NOT NULL DEFAULT 0, -- 1 = écriture validée, non modifiable

    hash TEXT NULL,
    prev_hash TEXT NULL,

    id_exercice INTEGER NULL DEFAULT NULL, -- En cas de compta simple, l'exercice est permanent (NULL)







|







249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

    libelle TEXT NOT NULL,
    remarques TEXT NULL,
    numero_piece TEXT NULL, -- N° de pièce comptable

    date TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date) IS NOT NULL AND date(date) = date),
    moyen_paiement TEXT NULL,
    reference_paiement TEXT NULL,

    validation INTEGER NOT NULL DEFAULT 0, -- 1 = écriture validée, non modifiable

    hash TEXT NULL,
    prev_hash TEXT NULL,

    id_exercice INTEGER NULL DEFAULT NULL, -- En cas de compta simple, l'exercice est permanent (NULL)

Added src/include/lib/Garradin/Compta/Ligne.php version [a0e19da1f4].





























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php

namespace Garradin\Compta;

use Garradin\Entity;
use Garradin\ValidationException;

class Ligne extends Entity
{
	protected $table = 'compta_mouvements_lignes';

	protected $id;
	protected $id_mouvement;
	protected $credit = 0;
	protected $debit = 0;

	const FIELDS = [
		'id_mouvement' => 'required|integer|in_table:compta_mouvements,id',
		'compte'       => 'required|alpha_num|in_table:compta_comptes,id',
		'credit'       => 'required|integer|min:0',
		'debit'        => 'required|integer|min:0'
	];

	public function filterUserEntry($key, $value)
	{
		$value = parent::filterUserEntry($key, $value);

		if ($key == 'credit' || $key == 'debit')
		{
			if (!preg_match('/^(\d+)(?:[,.](\d{2}))?$/', $value, $match))
			{
				throw new ValidationException('Le format du montant est invalide. Format accepté, exemple : 142,02');
			}

			$value = $match[1] . sprintf('%02d', $match[2]);
		}
		elseif ($key == 'compte')
		{
			$value = strtoupper($compte);
		}

		return $value;
	}

	public function selfCheck()
	{
		if (!$this->credit && !$this->debit)
		{
			throw new ValidationException('Aucun montant au débit ou au crédit.');
		}

		if (($this->credit * $this->debit) !== 0 || ($this->credit + $this->debit) !== 0)
		{
			throw new ValidationException('Ligne non équilibrée : crédit ou débit doit valoir zéro.');
		}

		if (!$this->id_mouvement)
		{
			throw new ValidationException('Aucun mouvement n\'a été indiqué pour cette ligne.');
		}
	}
}

Modified src/include/lib/Garradin/Compta/Mouvement.php from [0ef83a74a5] to [d2c5a12a70].

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
40
41


































42
43
44
45
46
47
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
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace Garradin\Compta;

use Garradin\Entity;



class Mouvement extends Entity
{
	protected $table = 'compta_mouvements';

	protected $id;
	protected $libelle;
	protected $remarques;
	protected $numero_piece;

	protected $date;
	protected $moyen_paiement;
	protected $numero_cheque;

	protected $validation;

	protected $hash;
	protected $prev_hash;

	protected $id_exercice;
	protected $id_auteur;
	protected $id_categorie;
	protected $id_projet;















	protected $lignes = [];

	public function getLignes()
	{
		$db = DB::getInstance();
		return $db->toObject($db->get('SELECT * FROM compta_mouvements_lignes WHERE id_mouvement = ? ORDER BY id;', $this->id), Ligne::class);
	}

	public function add(Ligne $ligne)
	{
		$this->lignes[] = $ligne;
	}



































	public function save()
	{
		if (!parent::save())
		{
			return false;
		}

		foreach ($this->lignes as $ligne)
		{
			$ligne->id_mouvement = $this->id;
			$ligne->save();
		}
	}

	public function validate($key, $value)
	{
		switch ($key)
		{
			case 'date':
				if (!($value instanceof \DateTime))
				{
					throw new ValidationException('La date est invalide.');
				}
				break;
			case 'moyen_paiement':
				if (!$db->test('compta_moyens_paiement', 'code', $value))
				{
					throw new ValidationException('Moyen de paiement inconnu.');

				}
				break;
			case 'id_exercice':
				if (null !== $value && !$db->test('compta_exercices', 'id', $value))
				{
					throw new ValidationException('Numéro d\'exercice invalide.');

				}
				break;
			default:
				break;
		}

		return true;
	}

	public function selfCheck()
	{
		if (trim($this->libelle) === '')
		{
			throw new ValidationException('Le libellé ne peut rester vide.');
		}

		if (null === $this->date)
		{
			throw new ValidationException('Le date ne peut rester vide.');
		}


		if (null === $this->id_exercice && $config->get('compta_expert'))
		{
			throw new ValidationException('Aucun exercice spécifié.');
		}

		if (null !== $this->id_exercice 
			&& !$db->test('compta_exercices', 'id = ? AND debut <= ? AND fin >= ?;', $this->id_exercice, $this->date, $this->date))
		{
			throw new ValidationException('La date ne correspond pas à l\'exercice sélectionné.');
		}
	}
}





>
>












|










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













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















|

<
<
<
|
|
<
<
<
|
<
|
<
>
|
<
<
|
|
<
>
|
<
<
<
|
<
|




<
<
<
<
|
<
<
<
<
>






|






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
40
41
42
43
44
45
46
47
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
98
99
100
101
102
103
104
105
106
107
108



109
110



111

112

113
114


115
116

117
118



119

120
121
122
123
124




125




126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

namespace Garradin\Compta;

use Garradin\Entity;
use Garradin\ValidationException;
use Garradin\DB;

class Mouvement extends Entity
{
	protected $table = 'compta_mouvements';

	protected $id;
	protected $libelle;
	protected $remarques;
	protected $numero_piece;

	protected $date;
	protected $moyen_paiement;
	protected $reference_paiement;

	protected $validation;

	protected $hash;
	protected $prev_hash;

	protected $id_exercice;
	protected $id_auteur;
	protected $id_categorie;
	protected $id_projet;

	const FIELDS = [
		'libelle'            => 'required|string',
		'remarques'          => 'string|max:20000',
		'numero_piece'       => 'string|max:200',
		'reference_paiement' => 'string|max:200',
		'date'               => 'required|date',
		'moyen_paiement'     => 'string|in_table:compta_moyens_paiement,code|required_with:id_categorie',
		'validation'         => 'bool',
		'id_exercice'        => 'integer|in_table:compta_exercices,id',
		'id_auteur'          => 'integer|in_table:membres,id',
		'id_categorie'       => 'integer|in_table:compta_categories,id',
		'id_projet'          => 'integer|in_table:compta_projets,id'
	];

	protected $lignes = [];

	public function getLignes()
	{
		$db = DB::getInstance();
		return $db->toObject($db->get('SELECT * FROM compta_mouvements_lignes WHERE id_mouvement = ? ORDER BY id;', $this->id), Ligne::class);
	}

	public function add(Ligne $ligne)
	{
		$this->lignes[] = $ligne;
	}

	public function simple($montant, $moyen, $compte)
	{
		$this->moyen_paiement = $moyen;
		$categorie = new Categorie($this->id_categorie);

		if ($categorie->type == Categorie::DEPENSE)
		{
			$from = $categorie->compte;
			$to = $compte;
		}
		else
		{
			$from = $compte;
			$to = $categorie->compte;
		}

		return $this->transfer($montant, $from, $to);
	}

	public function transfer($amount, $from, $to)
	{
		$ligne1 = new Ligne;
		$ligne1->compte = $from;
		$ligne1->debit = $amount;
		$ligne1->credit = 0;

		$ligne2 = new Ligne;
		$ligne1->compte = $to;
		$ligne1->debit = 0;
		$ligne1->credit = $amount;

		return $this->add($ligne1) && $this->add($ligne2);
	}

	public function save()
	{
		if (!parent::save())
		{
			return false;
		}

		foreach ($this->lignes as $ligne)
		{
			$ligne->id_mouvement = $this->id;
			$ligne->save();
		}
	}

	public function filterUserEntry($key, $value)
	{



		$value = parent::filterUserEntry($key, $value);




		if ($key == 'moyen_paiement')

		{

			$value = strtoupper($value);
		}


		elseif ($key == 'date' && !is_object($value))
		{

			$value = new \DateTimeImmutable($value);
		}





		return $value;
	}

	public function selfCheck()
	{




		$db = DB::getInstance();




		$config = Config::getInstance();

		if (null === $this->id_exercice && $config->get('compta_expert'))
		{
			throw new ValidationException('Aucun exercice spécifié.');
		}

		if (null !== $this->id_exercice
			&& !$db->test('compta_exercices', 'id = ? AND debut <= ? AND fin >= ?;', $this->id_exercice, $this->date, $this->date))
		{
			throw new ValidationException('La date ne correspond pas à l\'exercice sélectionné.');
		}
	}
}

Modified src/include/lib/Garradin/Entity.php from [885673b2ef] to [3a141ae570].

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
40
41
42
43














44

45
46
47
48
49
50
51












<?php

namespace Garradin;

class Entity
{
	protected $id;
	protected $table;




















	public function save()
	{








		$db = DB::getInstance();

		if (null === $this->id)
		{
			if ($return = $db->insert($this->table, $this->toArray()))
			{
				$this->id = $db->lastInsertId();
			}
		}
		else
		{
			$return = $db->update($this->table, $this->toArray(), 'id = :id', ['id' => $this->id]);
		}



		return $return;
	}






	public function set($key, $value = null)

	{


		if (is_array($key))



		{


			foreach ($key as $_key => $_value)
			{
				if (!$this->set($_key, $value))
				{
					return false;
				}
			}

			return true;
		}















		$this->$key = $value;

	}

	public function filterUserEntry($key, $value)
	{
		return trim($value);
	}
}




















>

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


>
>
>
>
>
>
>
>











|


>
>



>
>
>
>
>
|
>
|
>
>
|
>
>
>
|
>
>
|
|
|
|
|
|
|

|
|

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

>






|
>
>
>
>
>
>
>
>
>
>
>
>
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
40
41
42
43
44
45
46
47
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

namespace Garradin;

class Entity
{
	protected $id;
	protected $table;
	protected $modified = [];

	public function __construct($id = null)
	{
		if (null === $this->table)
		{
			throw new \LogicException('Aucun nom de table spécifié.');
		}

		if (null !== $id)
		{
			$result = DB::getInstance()->first('SELECT * FROM ' . $this->table . ' WHERE id = ?;', $id);

			foreach ($result as $key => $value)
			{
				$this->$key = $value;
			}
		}
	}

	public function save()
	{
		if (!count($this->modified))
		{
			return true;
		}

		$this->selfValidate();
		$this->selfCheck();

		$db = DB::getInstance();

		if (null === $this->id)
		{
			if ($return = $db->insert($this->table, $this->toArray()))
			{
				$this->id = $db->lastInsertId();
			}
		}
		else
		{
			$return = $db->update($this->table, $this->modified, 'id = :id', ['id' => $this->id]);
		}

		$this->modified = [];

		return $return;
	}

	final protected function selfValidate()
	{
		if (!Form::validate($this::FIELDS, $errors, $this->toArray()))
		{
			$messages = [];

			foreach ($errors as $error)
			{
				$messages[] = $this->getValidationMessage($error);
			}

			throw new ValidationException(implode("\n", $messages));
		}
	}

	public function set($fields = null)
	{
		foreach ($fields as $key => $value)
		{
			if (!$this->__set($key, $value))
			{
				return false;
			}
		}

		return true;
	}

	public function __get($key)
	{
		return $this->$key;
	}

	public function __set($key, $value)
	{
		if (!in_array($key, $this::FIELDS))
		{
			throw new ValidationException(sprintf('Le champ "%s" ne peut être modifié.', $key));
		}

		$value = $this->filterUserEntry($key, $value);

		$this->$key = $value;
		$this->modified[$key] = $value;
	}

	public function filterUserEntry($key, $value)
	{
		return trim($value);
	}

	public function toArray()
	{
		$out = [];

		foreach ($this::FIELDS as $key)
		{
			$out[$key] = $this->$key;
		}

		return $out;
	}
}