Overview
Comment:Adaptation à KD2\AbstractEntity
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 1f3d435c32855b6e511a458425059ebb6810d1ab
User & Date: bohwaz on 2020-01-17 13:23:39
Other Links: branch diff | manifest | tags
Context
2020-01-20
00:12
Garder les anciens schémas SQL pour les migrations qui les utilisent check-in: 757810afbb user: bohwaz tags: dev
2020-01-17
13:23
Adaptation à KD2\AbstractEntity check-in: 1f3d435c32 user: bohwaz tags: dev
13:22
Ajout foreign key id_exercice sur les comptes check-in: 9ac4c86495 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Compta/Compte.php from [c219124f46] to [6715611975].

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
<?php

namespace Garradin\Compta;


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

class Compte extends Entity
{


	const PASSIF = 1;
	const ACTIF = 2;
	const ACTIF_PASSIF = 3;

	const PRODUIT = 4;
	const CHARGE = 5;
	const PRODUIT_CHARGE = 6;


	const BANQUE = 7;
	const CAISSE = 8;

	const A_ENCAISSER = 9;

	protected $id;

	protected $parent;
	protected $libelle;
	protected $position;
	protected $plan_comptable;


	protected $_fields = [









		'id'             => 'required|string|alpha_num',
		'libelle'        => 'required|string',
		'parent'         => 'required|string|alpha_num|in_table:comptes,id',
		'position'       => 'required|integer',
		'plan_comptable' => 'integer|min:0|max:1',

	];
}




>






>
>








>






>




>

|
>
>
>
>
>
>
>
>
>
|

|


>


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
<?php

namespace Garradin\Compta;

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

class Compte extends Entity
{
	const TABLE = 'compta_comptes';

	const PASSIF = 1;
	const ACTIF = 2;
	const ACTIF_PASSIF = 3;

	const PRODUIT = 4;
	const CHARGE = 5;
	const PRODUIT_CHARGE = 6;

	// Types spéciaux de comptes
	const BANQUE = 7;
	const CAISSE = 8;

	const A_ENCAISSER = 9;

	protected $id;
	protected $code;
	protected $parent;
	protected $libelle;
	protected $position;
	protected $plan_comptable;
	protected $id_exercice;

	protected $_types = [
		'code'           => 'string',
		'parent'         => '?int',
		'libelle'        => 'string',
		'position'       => 'int',
		'plan_comptable' => 'int',
		'id_exercice'    => '?int',
	];

	protected $_validation_rules = [
		'id'             => 'required|integer',
		'libelle'        => 'required|string',
		'parent'         => 'required|integer|in_table:compta_comptes,id',
		'position'       => 'required|integer',
		'plan_comptable' => 'integer|min:0|max:1',
		'id_exercice'    => 'integer|in_table:compta_exercices,id'
	];
}

Modified src/include/lib/Garradin/Compta/Comptes.php from [9bf36bdeef] to [8e9c8fd0ed].

1
2
3
4

5
6
7
8
9
10
11
12
13
14
<?php

namespace Garradin\Compta;


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

class Comptes
{
    const CAISSE = '530';

    const CHEQUE_A_ENCAISSER = '5112';
    const CARTE_A_ENCAISSER = '5115';




>
|
|
|







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

namespace Garradin\Compta;

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

class Comptes
{
    const CAISSE = '530';

    const CHEQUE_A_ENCAISSER = '5112';
    const CARTE_A_ENCAISSER = '5115';

Modified src/include/lib/Garradin/Compta/Ligne.php from [b871bf3705] to [b6662c154e].

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;


	protected $_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.');
		}
	}
}









|





>

|
>
>
>
>
>
>
>

|




|

|










<
<
<
<






|
<
|
<
<
<
<
|
<
<
<
<
|


<
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

<?php

namespace Garradin\Compta;

use Garradin\Entity;
use Garradin\ValidationException;

class Ligne extends Entity
{
	const TABLE = 'compta_mouvements_lignes';

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

	protected $_types = [
		'id_mouvement' => 'int',
		'credit'       => 'int',
		'debit'        => 'int',
		'compte'       => 'int',
	];

	protected $_validation_rules = [
		'id_mouvement' => 'required|integer|in_table:compta_mouvements,id',
		'compte'       => 'required|integer|in_table:compta_comptes,id',
		'credit'       => 'required|integer|min:0',
		'debit'        => 'required|integer|min:0'
	];

	public function filterUserValue(string $key, $value, array $source)
	{
		$value = parent::filterUserValue($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]);
		}





		return $value;
	}

	public function selfCheck()
	{
		parent::selfCheck();

		$this->assert($this->credit || $this->debit, 'Aucun montant au débit ou au crédit.');




		$this->assert(($this->credit * $this->debit) === 0 && ($this->credit + $this->debit) > 0, 'Ligne non équilibrée : crédit ou débit doit valoir zéro.');




		$this->assert($this->id_mouvement, 'Aucun mouvement n\'a été indiqué pour cette ligne.');
		}
	}

Modified src/include/lib/Garradin/Compta/Mouvement.php from [e4b2eec8c2] to [b67e1a34c6].

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
<?php

namespace Garradin\Compta;

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

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;

	protected $_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'











|








<











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



<







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
<?php

namespace Garradin\Compta;

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

class Mouvement extends Entity
{
	const TABLE = 'compta_mouvements';

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

	protected $date;
	protected $moyen_paiement;


	protected $validation;

	protected $hash;
	protected $prev_hash;

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

	protected $_types = [
		'libelle'        => 'string',
		'remarques'      => '?string',
		'numero_piece'   => '?string',
		'date'           => 'date',
		'moyen_paiement' => '?string',
		'validation'     => 'bool',
		'hash'           => '?string',
		'prev_hash'      => '?string',
		'id_exercice'    => '?int',
		'id_auteur'      => '?int',
		'id_categorie'   => '?int',
		'id_projet'      => '?int',
	];

	protected $_validation_rules = [
		'libelle'            => 'required|string',
		'remarques'          => 'string|max:20000',
		'numero_piece'       => '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'

Modified src/include/lib/Garradin/Entity.php from [60b139db0d] to [e94008659f].

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
140
141
142
143
144
145
146
147
<?php

namespace Garradin;

use KD2\Form;


class Entity
{
	const FIELDS = [];

	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;
	}

	/**
	 * Vérifier la cohérence de l'objet avant enregistrement
	 */
	public function selfCheck()
	{
		return true;
	}

	/**
	 * Valider les champs avant enregistrement
	 * @throws ValidationException Si une erreur de validation survient
	 */
	public function selfValidate()
	{
		$errors = [];

		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 __isset($key)
	{
		return property_exists($this, $key);
	}

	/**
	 * Filtrer/sanitiser la valeur entrée par l'utilisateur pour un champ de l'entité
	 * (effectué au set)
	 */
	public function filterUserEntry($key, $value)
	{
		return trim($value);
	}

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

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

		return $out;
	}
}





>

|

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




















|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
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;

use KD2\Form;
use KD2\AbstractEntity;

class Entity extends AbstractEntity
{





























































	/**
	 * Valider les champs avant enregistrement
	 * @throws ValidationException Si une erreur de validation survient
	 */
	public function selfValidate()
	{
		$errors = [];

		if (!Form::validate($this->_fields, $errors, $this->toArray()))
		{
			$messages = [];

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

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

























































Modified src/include/lib/dependencies.list from [7b518732e5] to [b0f8f3486c].

1
2
3


4
5
6
7
8
9
10
KD2/data/
KD2/DB.php
KD2/DB_SQLite3.php


KD2/ErrorManager.php
KD2/FileInfo.php
KD2/Form.php
KD2/Helpers.php
KD2/Image.php
KD2/MiniSkel.php
KD2/QRCode.php

|
|
>
>







1
2
3
4
5
6
7
8
9
10
11
12
KD2/data/
KD2/DB/DB.php
KD2/DB/SQLite3.php
KD2/DB/Entity.php
KD2/DB/Entity_Manager.php
KD2/ErrorManager.php
KD2/FileInfo.php
KD2/Form.php
KD2/Helpers.php
KD2/Image.php
KD2/MiniSkel.php
KD2/QRCode.php