Overview
Comment: | Implement import multi-line transactions CSV |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | dev |
Files: | files | file ages | folders |
SHA1: |
6b024f84c46598da66dcff801ae95cf3 |
User & Date: | bohwaz on 2020-10-18 16:31:51 |
Other Links: | branch diff | manifest | tags |
Context
2020-10-21
| ||
13:00 | UX tweaks check-in: d931eececf user: bohwaz tags: dev | |
2020-10-18
| ||
16:31 | Implement import multi-line transactions CSV check-in: 6b024f84c4 user: bohwaz tags: dev | |
15:22 | Implement CSV transaction import check-in: e3e674f914 user: bohwaz tags: dev | |
Changes
Modified src/include/lib/Garradin/Accounting/Accounts.php from [f219952a59] to [d01d836e4c].
︙ | ︙ | |||
152 153 154 155 156 157 158 | public function importCSV(array $file): void { $db = DB::getInstance(); $positions = array_flip(Account::POSITIONS_NAMES); $types = array_flip(Account::TYPES_NAMES); $db->begin(); | < | 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | public function importCSV(array $file): void { $db = DB::getInstance(); $positions = array_flip(Account::POSITIONS_NAMES); $types = array_flip(Account::TYPES_NAMES); $db->begin(); 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']]; |
︙ | ︙ |
Modified src/include/lib/Garradin/Accounting/Transactions.php from [a635075726] to [f44648ec0e].
︙ | ︙ | |||
133 134 135 136 137 138 139 | $status = []; foreach (Transaction::STATUS_NAMES as $k => $v) { if ($row->status & $k) { $status[] = $v; } } | | > > > | | | | | < < < | < < < < < | < < < < < | | | 133 134 135 136 137 138 139 140 141 142 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 | $status = []; foreach (Transaction::STATUS_NAMES as $k => $v) { if ($row->status & $k) { $status[] = $v; } } $row->status = implode(', ', $status); $row->date = \DateTime::createFromFormat('Y-m-d', $row->date); $row->date = $row->date->format('d/m/Y'); } $row->credit = Utils::money_format($row->credit, ',', ''); $row->debit = Utils::money_format($row->debit, ',', ''); $previous_id = $row->id; yield $row; } } static public function importCSV(Year $year, array $file) { if ($year->closed) { throw new \InvalidArgumentException('Closed year'); } $db = DB::getInstance(); $db->begin(); $accounts = $year->accounts(); $transaction = null; $types = array_flip(Transaction::TYPES_NAMES); try { foreach (CSV::import($file, self::EXPECTED_CSV_COLUMNS_SELF) as $l => $row) { $row = (object) $row; $has_transaction = !empty($row->id) || !empty($row->type) || !empty($row->status) || !empty($row->label) || !empty($row->date) || !empty($row->notes) || !empty($row->reference); if (null !== $transaction && $has_transaction) { $transaction->save(); $transaction = null; } |
︙ | ︙ | |||
196 197 198 199 200 201 202 | if (!$transaction) { throw new UserException(sprintf('Ligne %d : l\'écriture n°%d est introuvable', $l, $row->id)); } if ($transaction->validated) { throw new UserException(sprintf('Ligne %d : l\'écriture n°%d est validée et ne peut être modifiée', $l, $row->id)); } | < < | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 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 | if (!$transaction) { throw new UserException(sprintf('Ligne %d : l\'écriture n°%d est introuvable', $l, $row->id)); } if ($transaction->validated) { throw new UserException(sprintf('Ligne %d : l\'écriture n°%d est validée et ne peut être modifiée', $l, $row->id)); } } else { $transaction = new Transaction; $transaction->id_user = $user_id; } $row->type = $types[$row->type]; $fields = array_intersect_key((array)$row, array_flip(['label', 'date', 'notes', 'reference'])); $transaction->importForm($fields); } $row->account = $accounts->getIdFromCode($row->account); if ($row->line_id) { $line = $transaction->getLine($row->line_id); } else { $line = new Line; } $line->importForm([ 'credit' => $row->credit ?: 0, 'debit' => $row->debit ?: 0, 'id_account' => $row->account, 'reference' => $row->line_reference, 'label' => $row->line_label, 'reconciled' => $row->reconciled, ]); if (!$row->line_id) { $transaction->addLine($line); } } if (null !== $transaction) { var_dump($transaction); exit; $transaction->save(); } } catch (UserException $e) { $db->rollback(); throw new UserException(sprintf('Erreur sur la ligne %d : %s', $l, $e->getMessage())); } $db->commit(); } static public function importArray(Year $year, array $table, array $translation_table, int $skip_lines, int $user_id) { if ($year->closed) { throw new \InvalidArgumentException('Closed year'); } $db = DB::getInstance(); $db->begin(); if ($skip_lines) { $table = array_slice($table, $skip_lines, null, true); } $accounts = $year->accounts(); try { foreach ($table as $l => $row) { $row = (object) array_combine(self::EXPECTED_CSV_COLUMNS_SELF, $row); if ($row->id) { $transaction = self::get((int)$row->id); if (!$transaction) { throw new UserException(sprintf('Ligne %d : l\'écriture n°%d est introuvable', $l, $row->id)); } if ($transaction->validated) { throw new UserException(sprintf('Ligne %d : l\'écriture n°%d est validée et ne peut être modifiée', $l, $row->id)); } $transaction->resetLines(); } else { $transaction = new Transaction; $transaction->type = Transaction::TYPE_ADVANCED; $transaction->id_user = $user_id; } $fields = array_intersect_key((array)$row, array_flip(['label', 'date', 'notes', 'reference'])); $transaction->importForm($fields); $row->credit_account = $accounts->getIdFromCode($row->credit_account); $row->debit_account = $accounts->getIdFromCode($row->debit_account); $line = new Line; $line->importForm([ 'credit' => $row->amount, |
︙ | ︙ | |||
229 230 231 232 233 234 235 | $line->importForm([ 'credit' => 0, 'debit' => $row->amount, 'id_account' => $row->debit_account, 'reference' => $row->p_reference, ]); $transaction->addLine($line); | < < < | 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | $line->importForm([ 'credit' => 0, 'debit' => $row->amount, 'id_account' => $row->debit_account, 'reference' => $row->p_reference, ]); $transaction->addLine($line); $transaction->save(); } } catch (UserException $e) { $db->rollback(); throw new UserException(sprintf('Erreur sur la ligne %d : %s', $l, $e->getMessage())); } $db->commit(); } } |
Modified src/include/lib/Garradin/Entities/Accounting/Transaction.php from [6f7f99dbd7] to [0000dc7f99].
︙ | ︙ | |||
125 126 127 128 129 130 131 132 133 134 135 136 137 138 | } public function resetLines() { $this->_old_lines = $this->getLines(); $this->_lines = []; } /* public function getHash() { if (!$this->id_year) { throw new \LogicException('Il n\'est pas possible de hasher un mouvement qui n\'est pas associé à un exercice'); } | > > > > > > > > > > > | 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | } public function resetLines() { $this->_old_lines = $this->getLines(); $this->_lines = []; } public function getLine(int $id) { foreach ($this->getLines() as $line) { if ($line->id === $id) { return $line; } } return null; } /* public function getHash() { if (!$this->id_year) { throw new \LogicException('Il n\'est pas possible de hasher un mouvement qui n\'est pas associé à un exercice'); } |
︙ | ︙ |
Modified src/templates/acc/years/import.tpl from [64c02f83e7] to [1f5102b5b7].
1 2 3 4 5 6 | {include file="admin/_head.tpl" title="Importer des écritures" current="acc/years"} {include file="acc/_year_select.tpl"} <nav class="tabs"> <ul> | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | {include file="admin/_head.tpl" title="Importer des écritures" current="acc/years"} {include file="acc/_year_select.tpl"} <nav class="tabs"> <ul> <li class="current"><a href="{$admin_url}admin/acc/years/import.php?id={$year.id}">Import</a></li> <li><a href="{$admin_url}acc/years/import.php?id={$year.id}&export=csv">Export journal général - CSV</a></li> <li><a href="{$admin_url}acc/years/import.php?id={$year.id}&export=ods">Export journal général - tableur</a></li> </ul> </nav> {form_errors} <form method="post" action="{$self_url}" enctype="multipart/form-data"> |
︙ | ︙ |
Modified src/www/admin/acc/years/import.php from [614104135a] to [2a0e7166e0].
︙ | ︙ | |||
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | $csv_file = $session->get('acc_import_csv'); if (f('import') && $csv_file && $form->check('acc_years_import_' . $year->id(), ['translate' => 'array|required'])) { try { Transactions::importArray($year, $csv_file, f('translate'), (int) f('skip_first_line'), $user->id); Utils::redirect(ADMIN_URL . 'acc/years/'); } catch (UserException $e) { $form->addError($e->getMessage()); } } elseif (f('import') && $form->check('acc_years_import_' . $year->id(), ['file' => 'file|required'])) { try { if (f('type') === 'csv') { $csv = CSV::readAsArray($_FILES['file']['tmp_name']); $session->set('acc_import_csv', $csv); Utils::redirect(Utils::getSelfURI()); } else { | > | | 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 | $csv_file = $session->get('acc_import_csv'); if (f('import') && $csv_file && $form->check('acc_years_import_' . $year->id(), ['translate' => 'array|required'])) { try { Transactions::importArray($year, $csv_file, f('translate'), (int) f('skip_first_line'), $user->id); $session->set('acc_import_csv', null); Utils::redirect(ADMIN_URL . 'acc/years/'); } catch (UserException $e) { $form->addError($e->getMessage()); } } elseif (f('import') && $form->check('acc_years_import_' . $year->id(), ['file' => 'file|required'])) { try { if (f('type') === 'csv') { $csv = CSV::readAsArray($_FILES['file']['tmp_name']); $session->set('acc_import_csv', $csv); Utils::redirect(Utils::getSelfURI()); } else { Transactions::importCSV($year, $_FILES['file']); } Utils::redirect(ADMIN_URL . 'acc/years/'); } catch (UserException $e) { $form->addError($e->getMessage()); |
︙ | ︙ |