Overview
Comment:Move account import to Accounts::import
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: ee3811b07697226744b333da204130824a9ac73d
User & Date: bohwaz on 2020-10-18 10:45:06
Other Links: branch diff | manifest | tags
Context
2020-10-18
10:46
Make sure that we get a valid year check-in: ab80480535 user: bohwaz tags: dev
10:45
Move account import to Accounts::import check-in: ee3811b076 user: bohwaz tags: dev
2020-10-17
17:01
Add more graphs check-in: fa963fe834 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Accounting/Accounts.php from [d388bab2b8] to [f219952a59].

8
9
10
11
12
13
14


15
16
17
18
19
20
21
use KD2\DB\EntityManager;

class Accounts
{
	protected $chart_id;
	protected $em;



	public function __construct(int $chart_id)
	{
		$this->chart_id = $chart_id;
		$this->em = EntityManager::getInstance(Account::class);
	}

	static public function get(int $id)







>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use KD2\DB\EntityManager;

class Accounts
{
	protected $chart_id;
	protected $em;

	const EXPECTED_CSV_COLUMNS = ['code', 'label', 'description', 'position', 'type'];

	public function __construct(int $chart_id)
	{
		$this->chart_id = $chart_id;
		$this->em = EntityManager::getInstance(Account::class);
	}

	static public function get(int $id)
141
142
143
144
145
146
147
148


































	public function copyFrom(int $id)
	{
		$db = DB::getInstance();
		return $db->exec(sprintf('INSERT INTO %s (id_chart, code, label, description, position, type, user)
			SELECT %d, code, label, description, position, type, user FROM %1$s WHERE id_chart = %d;', Account::TABLE, $this->chart_id, $id));
	}
}








































|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

	public function copyFrom(int $id)
	{
		$db = DB::getInstance();
		return $db->exec(sprintf('INSERT INTO %s (id_chart, code, label, description, position, type, user)
			SELECT %d, code, label, description, position, type, user FROM %1$s WHERE id_chart = %d;', Account::TABLE, $this->chart_id, $id));
	}


	public function importCSV(array $file): void
	{
		$db = DB::getInstance();
		$positions = array_flip(Account::POSITIONS_NAMES);
		$types = array_flip(Account::TYPES_NAMES);

		$db->begin();
		$this->save();

		try {
			foreach (Utils::fromCSV($file, self::EXPECTED_CSV_COLUMNS) as $line => $row) {
				$account = new Account;
				$account->id_chart = $this->chart_id;
				try {
					$row['position'] = $positions[$row['position']];
					$row['type'] = $types[$row['type']];
					$account->importForm($row);
					$account->save();
				}
				catch (ValidationException $e) {
					throw new UserException(sprintf('Ligne %d : %s', $line, $e->getMessage()));
				}
			}

			$db->commit();
		}
		catch (\Exception $e) {
			$db->rollback();
			throw $e;
		}
	}
}

Modified src/include/lib/Garradin/Entities/Accounting/Chart.php from [b214214dc3] to [297b8e2f31].

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

	protected $_form_rules = [
		'label'    => 'required|string|max:200',
		'country'  => 'required|string|size:2',
		'archived' => 'numeric|min:0|max:1'
	];

	const EXPECTED_CSV_COLUMNS = ['code', 'label', 'description', 'position', 'type'];

	public function selfCheck(): void
	{
		parent::selfCheck();
		$this->assert(Utils::getCountryName($this->country), 'Le code pays doit être un code ISO valide');
		$this->assert($this->archived === 0 || $this->archived === 1);
	}

	public function accounts()
	{
		return new Accounts($this->id());
	}

	public function canDelete()
	{
		return !DB::getInstance()->firstColumn(sprintf('SELECT 1 FROM %s WHERE id_chart = ? LIMIT 1;', Year::TABLE), $this->id());
	}

	public function importCSV(array $file): void
	{
		$db = DB::getInstance();
		$positions = array_flip(Account::POSITIONS_NAMES);
		$types = array_flip(Account::TYPES_NAMES);

		$db->begin();
		$this->save();

		try {
			foreach (Utils::fromCSV($file, self::EXPECTED_CSV_COLUMNS) as $line => $row) {
				$account = new Account;
				$account->id_chart = $this->id();
				try {
					$row['position'] = $positions[$row['position']];
					$row['type'] = $types[$row['type']];
					$account->importForm($row);
					$account->save();
				}
				catch (ValidationException $e) {
					throw new UserException(sprintf('Ligne %d : %s', $line, $e->getMessage()));
				}
			}

			$db->commit();
		}
		catch (\Exception $e) {
			$db->rollback();
			throw $e;
		}
	}
}







<
<
















|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
29
30
31
32
33
34
35


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

































	protected $_form_rules = [
		'label'    => 'required|string|max:200',
		'country'  => 'required|string|size:2',
		'archived' => 'numeric|min:0|max:1'
	];



	public function selfCheck(): void
	{
		parent::selfCheck();
		$this->assert(Utils::getCountryName($this->country), 'Le code pays doit être un code ISO valide');
		$this->assert($this->archived === 0 || $this->archived === 1);
	}

	public function accounts()
	{
		return new Accounts($this->id());
	}

	public function canDelete()
	{
		return !DB::getInstance()->firstColumn(sprintf('SELECT 1 FROM %s WHERE id_chart = ? LIMIT 1;', Year::TABLE), $this->id());
	}
}
































Modified src/templates/acc/charts/import.tpl from [8e3108b717] to [3969642f77].

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
				Règles à suivre pour créer le fichier CSV&nbsp;:
				<ul>
					<li>Il est recommandé d'utiliser LibreOffice pour créer le fichier CSV</li>
					<li>Le fichier doit être en UTF-8</li>
					<li>Le séparateur doit être le point-virgule ou la virgule</li>
					<li>Cocher l'option <em>"Mettre en guillemets toutes les cellules du texte"</em></li>
					<li>Le fichier doit comporter les colonnes suivantes : <em>{$columns}</em></li>
					<li>Pour obtenir un exemple du format attendu, faire un export d'un exercice existant</li>
				</ul>
			</dd>
		</dl>
		<p class="submit">
			{csrf_field key="acc_charts_import"}
			<input type="submit" name="import" value="Importer &rarr;" />
		</p>
	</fieldset>
</form>

{include file="admin/_foot.tpl"}







|











13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
				Règles à suivre pour créer le fichier CSV&nbsp;:
				<ul>
					<li>Il est recommandé d'utiliser LibreOffice pour créer le fichier CSV</li>
					<li>Le fichier doit être en UTF-8</li>
					<li>Le séparateur doit être le point-virgule ou la virgule</li>
					<li>Cocher l'option <em>"Mettre en guillemets toutes les cellules du texte"</em></li>
					<li>Le fichier doit comporter les colonnes suivantes : <em>{$columns}</em></li>
					<li>Pour obtenir un exemple du format attendu, faire un export d'un plan comptable existant</li>
				</ul>
			</dd>
		</dl>
		<p class="submit">
			{csrf_field key="acc_charts_import"}
			<input type="submit" name="import" value="Importer &rarr;" />
		</p>
	</fieldset>
</form>

{include file="admin/_foot.tpl"}

Modified src/www/admin/acc/charts/import.php from [c2f87310e2] to [0fac8cf190].

16
17
18
19
20
21
22
23
24
25
26
		Utils::redirect(ADMIN_URL . 'acc/charts/');
	}
	catch (UserException $e) {
		$form->addError($e->getMessage());
	}
}

$tpl->assign('columns', implode(', ', Chart::EXPECTED_CSV_COLUMNS));
$tpl->assign('country_list', Utils::getCountryList());

$tpl->display('acc/charts/import.tpl');







|



16
17
18
19
20
21
22
23
24
25
26
		Utils::redirect(ADMIN_URL . 'acc/charts/');
	}
	catch (UserException $e) {
		$form->addError($e->getMessage());
	}
}

$tpl->assign('columns', implode(', ', Accounts::EXPECTED_CSV_COLUMNS));
$tpl->assign('country_list', Utils::getCountryList());

$tpl->display('acc/charts/import.tpl');