Overview
Comment: | Refactoring: remove getClosingSumsWithAccounts, use getAccountsBalance, fix graphs |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
a1ef61e8d71e32281cf35566f261c617 |
User & Date: | bohwaz on 2022-03-03 18:59:38 |
Other Links: | manifest | tags |
Context
2022-03-04
| ||
00:26 | Try to always use debit - credit as the default case check-in: 89cbd2f12d user: bohwaz tags: trunk | |
2022-03-03
| ||
18:59 | Refactoring: remove getClosingSumsWithAccounts, use getAccountsBalance, fix graphs check-in: a1ef61e8d7 user: bohwaz tags: trunk | |
18:53 | Fix PHP 8.1 warning on float conversion check-in: 6264b5c4fe user: bohwaz tags: trunk | |
Changes
Modified src/include/data/schema.sql from [25952763e8] to [1273891055].
︙ | ︙ | |||
166 167 168 169 170 171 172 | CASE WHEN position IN (1, 4) -- 1 = asset, 4 = expense OR (position = 3 AND (credit - debit) < 0) THEN debit - credit ELSE credit - debit | | > | 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | CASE WHEN position IN (1, 4) -- 1 = asset, 4 = expense OR (position = 3 AND (credit - debit) < 0) THEN debit - credit ELSE credit - debit END AS balance, CASE WHEN debit - credit > 0 THEN 1 ELSE 0 END AS is_debt FROM ( SELECT t.id_year, a.id, a.label, a.code, a.position, a.type, SUM(l.credit) AS credit, SUM(l.debit) AS debit FROM acc_accounts a INNER JOIN acc_transactions_lines l ON l.id_account = a.id INNER JOIN acc_transactions t ON t.id = l.id_transaction |
︙ | ︙ |
Modified src/include/lib/Garradin/Accounting/Graph.php from [fbdb0c0600] to [dabf37ba31].
︙ | ︙ | |||
31 32 33 34 35 36 37 | ADMIN_URL . 'acc/reports/graph_pie.php?type=revenue&%s' => 'Répartition recettes', ADMIN_URL . 'acc/reports/graph_pie.php?type=expense&%s' => 'Répartition dépenses', ADMIN_URL . 'acc/reports/graph_pie.php?type=assets&%s' => 'Répartition actif', ]; const PLOT_TYPES = [ 'assets' => [ | | | | 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | ADMIN_URL . 'acc/reports/graph_pie.php?type=revenue&%s' => 'Répartition recettes', ADMIN_URL . 'acc/reports/graph_pie.php?type=expense&%s' => 'Répartition dépenses', ADMIN_URL . 'acc/reports/graph_pie.php?type=assets&%s' => 'Répartition actif', ]; const PLOT_TYPES = [ 'assets' => [ 'Total' => ['type' => [Account::TYPE_BANK, Account::TYPE_CASH, Account::TYPE_OUTSTANDING], 'exclude_position' => [Account::LIABILITY]], 'Banques' => ['type' => Account::TYPE_BANK], 'Caisses' => ['type' => Account::TYPE_CASH], 'En attente' => ['type' => Account::TYPE_OUTSTANDING, 'exclude_position' => [Account::LIABILITY]], ], 'result' => [ 'Recettes' => ['position' => Account::REVENUE], 'Dépenses' => ['position' => Account::EXPENSE], ], 'debts' => [ 'Comptes de tiers' => ['type' => Account::TYPE_THIRD_PARTY], |
︙ | ︙ | |||
126 127 128 129 130 131 132 | if (!array_key_exists($type, self::PIE_TYPES)) { throw new \InvalidArgumentException('Unknown type'); } $pie = new Pie(700, 300); $pie_criterias = self::PIE_TYPES[$type]; | | < | | | | | 126 127 128 129 130 131 132 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 | if (!array_key_exists($type, self::PIE_TYPES)) { throw new \InvalidArgumentException('Unknown type'); } $pie = new Pie(700, 300); $pie_criterias = self::PIE_TYPES[$type]; $data = Reports::getAccountsBalances(array_merge($criterias, $pie_criterias), 'balance DESC'); $others = 0; $colors = self::getColors(); $max = count($colors); $total = 0; $count = 0; $i = 0; foreach ($data as $row) { $total += $row->balance; } foreach ($data as $row) { if ($i++ >= $max || $count > $total*0.95) { $others += $row->balance; } else { $label = strlen($row->label) > 40 ? substr($row->label, 0, 38) . '…' : $row->label; $pie->add(new Pie_Data(abs($row->balance) / 100, $label, $colors[$i-1])); } $count += $row->balance; } if ($others != 0) { $pie->add(new Pie_Data(abs($others) / 100, 'Autres', '#ccc')); } |
︙ | ︙ | |||
191 192 193 194 195 196 197 | $line_criterias = array_merge($criterias, $line_criterias); $years = Reports::getSumsPerYear($line_criterias); if (count($years) < 1) { continue; } | < < < < < < < | | 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 | $line_criterias = array_merge($criterias, $line_criterias); $years = Reports::getSumsPerYear($line_criterias); if (count($years) < 1) { continue; } foreach ($years as $year) { $start = Utils::date_fr($year->start_date, 'Y'); $end = Utils::date_fr($year->end_date, 'Y'); $year_label = $start == $end ? $start : sprintf('%s-%s', $start, substr($end, -2)); $year_id = $year_label . '-' . $year->id; if (!isset($data[$year_id])) { $data[$year_id] = new Bar_Data_Set($year_label); } $data[$year_id]->add((int) $year->balance / 100, $label, $color); } } ksort($data); foreach ($data as $group) { $bar->add($group); |
︙ | ︙ |
Modified src/include/lib/Garradin/Accounting/Reports.php from [8d559cc680] to [a98c174ebd].
︙ | ︙ | |||
20 21 22 23 24 25 26 | $accounts_alias = $accounts_alias ? $accounts_alias . '.' : ''; if (!empty($criterias['year'])) { $where[] = sprintf($transactions_alias . 'id_year = %d', $criterias['year']); } if (!empty($criterias['position'])) { | | | < | > | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | $accounts_alias = $accounts_alias ? $accounts_alias . '.' : ''; if (!empty($criterias['year'])) { $where[] = sprintf($transactions_alias . 'id_year = %d', $criterias['year']); } if (!empty($criterias['position'])) { $criterias['position'] = array_map('intval', (array)$criterias['position']); $where[] = sprintf($accounts_alias . 'position IN (%s)', implode(',', $criterias['position'])); } if (!empty($criterias['exclude_position'])) { $criterias['exclude_position'] = array_map('intval', (array)$criterias['exclude_position']); $where[] = sprintf($accounts_alias . 'position NOT IN (%s)', implode(',', $criterias['exclude_position'])); } if (!empty($criterias['type'])) { $criterias['type'] = array_map('intval', (array)$criterias['type']); $where[] = sprintf($accounts_alias . 'type IN (%s)', implode(',', $criterias['type'])); } |
︙ | ︙ | |||
168 169 170 171 172 173 174 | yield $current; } static public function getSumsPerYear(array $criterias): array { $where = self::getWhereClause($criterias); | | | 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | yield $current; } static public function getSumsPerYear(array $criterias): array { $where = self::getWhereClause($criterias); $sql = sprintf('SELECT y.id, y.start_date, y.end_date, y.label, SUM(b.balance) AS balance FROM acc_accounts_balances b INNER JOIN acc_years y ON y.id = b.id_year WHERE %s GROUP BY b.id_year ORDER BY y.end_date;', $where); return DB::getInstance()->getGrouped($sql); } |
︙ | ︙ | |||
247 248 249 250 251 252 253 | * Returns accounts balances according to $criterias * @param array $criterias List of criterias, see self::getWhereClause * @param string|null $order Order of rows (SQL clause), if NULL will order by CODE * @param bool $remove_zero Remove accounts where the balance is zero from the list */ static public function getAccountsBalances(array $criterias, ?string $order = null, bool $remove_zero = true) { | > > > > > > | > > > > > > > > > < > > > > | | | | | < | > > | 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 289 290 291 | * Returns accounts balances according to $criterias * @param array $criterias List of criterias, see self::getWhereClause * @param string|null $order Order of rows (SQL clause), if NULL will order by CODE * @param bool $remove_zero Remove accounts where the balance is zero from the list */ static public function getAccountsBalances(array $criterias, ?string $order = null, bool $remove_zero = true) { $order = $order ?: 'code COLLATE NOCASE'; $remove_zero = $remove_zero ? 'code HAVING balance != 0' : ''; // Specific queries that can't rely on acc_accounts_balances if (!empty($criterias['user']) || !empty($criterias['creator']) || !empty($criterias['subscription']) || !empty($criterias['analytical']) || !empty($criterias['analytical_only'])) { $where = self::getWhereClause($criterias, 't', 'l', 'a'); $remove_zero = $remove_zero ? ', ' . $remove_zero : ''; $query = 'SELECT a.code, a.id, a.label, a.position, SUM(l.credit) AS credit, SUM(l.debit) AS debit, SUM(l.credit - l.debit) AS balance FROM %s l INNER JOIN %s t ON t.id = l.id_transaction INNER JOIN %s a ON a.id = l.id_account WHERE %s GROUP BY l.id_account %s ORDER BY %s'; $sql = sprintf($query, Line::TABLE, Transaction::TABLE, Account::TABLE, $where, $remove_zero, $order); } else { $where = self::getWhereClause($criterias); $remove_zero = $remove_zero ? 'GROUP BY ' . $remove_zero : ''; $query = 'SELECT * FROM acc_accounts_balances WHERE %s %s ORDER BY %s'; $sql = sprintf($query, $where, $remove_zero, $order); } $db = DB::getInstance(); // SQLite does not support OUTER JOIN yet :( if (isset($criterias['compare_year'])) { $sql2 = 'SELECT a.id, a.code AS code, a.label, a.position, a.type, a.debit, a.credit, a.balance, IFNULL(b.balance, 0) AS balance2, IFNULL(a.balance - b.balance, a.balance) AS change FROM (%s) AS a |
︙ | ︙ | |||
281 282 283 284 285 286 287 | ORDER BY code COLLATE NOCASE;'; $sql = sprintf($sql2, $sql, $criterias['year'], $criterias['compare_year'], $criterias['position']); } $out = $db->get($sql); | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | ORDER BY code COLLATE NOCASE;'; $sql = sprintf($sql2, $sql, $criterias['year'], $criterias['compare_year'], $criterias['position']); } $out = $db->get($sql); return $out; } static public function getTrialBalance(array $criterias): \Iterator { unset($criterias['compare_year']); $out = self::getAccountsBalances($criterias, null, false); |
︙ | ︙ | |||
565 566 567 568 569 570 571 | $account->all_credit = $credit; yield $account; } static public function getJournal(array $criterias): \Generator { | | | 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | $account->all_credit = $credit; yield $account; } static public function getJournal(array $criterias): \Generator { $where = self::getWhereClause($criterias, 't', 'l', 'a'); $sql = sprintf('SELECT t.id_year, l.id_account, l.debit, l.credit, t.id, t.date, t.reference, l.reference AS line_reference, t.label, l.label AS line_label, a.label AS account_label, a.code AS account_code FROM acc_transactions t INNER JOIN acc_transactions_lines l ON l.id_transaction = t.id |
︙ | ︙ |
Modified src/templates/acc/transactions/service_user.tpl from [acb7bfcb58] to [a9660dff87].
︙ | ︙ | |||
16 17 18 19 20 21 22 | <h2 class="ruler">Solde des comptes</h2> <table class="list"> <thead> <tr> <td>Numéro</td> <th>Compte</th> | | < | < | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <h2 class="ruler">Solde des comptes</h2> <table class="list"> <thead> <tr> <td>Numéro</td> <th>Compte</th> <td class="money">Solde</td> </tr> </thead> <tbody> {foreach from=$balance item="account"} <tr> <td class="num"><a href="{$admin_url}acc/accounts/journal.php?id={$account.id}">{$account.code}</a></td> <th>{$account.label}</th> <td class="money">{$account.balance|raw|money:false}</td> </tr> {/foreach} </tbody> </table> {/if} {include file="admin/_foot.tpl"} |
Modified src/templates/acc/transactions/user.tpl from [4c90bde47d] to [ebafbd05ec].
︙ | ︙ | |||
22 23 24 25 26 27 28 | </form> <p class="block help">Cette liste représente le solde des comptes uniquement pour les écritures liées à ce membre.</p> <table class="list"> <thead> <tr> | | | < | < | 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | </form> <p class="block help">Cette liste représente le solde des comptes uniquement pour les écritures liées à ce membre.</p> <table class="list"> <thead> <tr> <td class="num">Numéro</td> <th>Compte</th> <td class="money">Solde</td> </tr> </thead> <tbody> {foreach from=$balance item="account"} <tr> <td class="num"><a href="{$admin_url}acc/accounts/journal.php?id={$account.id}">{$account.code}</a></td> <th>{$account.label}</th> <td class="money">{$account.balance|raw|money}</td> </tr> {/foreach} </tbody> </table> {include file="admin/_foot.tpl"} |
Modified src/www/admin/acc/transactions/service_user.php from [1ded97c2af] to [6ef72d23d2].
1 2 3 4 5 6 7 8 9 10 11 12 | <?php namespace Garradin; use Garradin\Accounting\Reports; use Garradin\Accounting\Years; require_once __DIR__ . '/../../_inc.php'; $session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ); $criterias = ['subscription' => (int)qg('id')]; | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php namespace Garradin; use Garradin\Accounting\Reports; use Garradin\Accounting\Years; require_once __DIR__ . '/../../_inc.php'; $session->requireAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ); $criterias = ['subscription' => (int)qg('id')]; $tpl->assign('balance', Reports::getAccountsBalances($criterias)); $tpl->assign('journal', Reports::getJournal($criterias)); $tpl->assign('user_id', qg('user')); $tpl->assign('service_user_id', qg('id')); $tpl->display('acc/transactions/service_user.tpl'); |
Modified src/www/admin/acc/transactions/user.php from [4b78a375e7] to [c553e49b64].
︙ | ︙ | |||
16 17 18 19 20 21 22 | $years = Years::listAssoc(); end($years); $year = (int)qg('year') ?: key($years); $criterias = ['user' => $u->id]; | | | 16 17 18 19 20 21 22 23 24 25 26 27 28 | $years = Years::listAssoc(); end($years); $year = (int)qg('year') ?: key($years); $criterias = ['user' => $u->id]; $tpl->assign('balance', Reports::getAccountsBalances($criterias + ['year' => $year], null, false)); $tpl->assign('journal', Reports::getJournal($criterias)); $tpl->assign(compact('years', 'year')); $tpl->assign('transaction_user', $u); $tpl->display('acc/transactions/user.tpl'); |
Modified src/www/admin/acc/years/balance.php from [7989b85f72] to [60df387c7b].
︙ | ︙ | |||
61 62 63 64 65 66 67 | if (!$previous_year) { throw new UserException('Année précédente invalide'); } } if ($previous_year) { | | | 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | if (!$previous_year) { throw new UserException('Année précédente invalide'); } } if ($previous_year) { $lines = Reports::getAccountsBalances(['year' => $previous_year->id(), 'exclude_position' => [Account::EXPENSE, Account::REVENUE]]); if ($previous_year->id_chart != $year->id_chart) { $chart_change = true; $codes = []; foreach ($lines as $line) { $codes[] = $line->code; |
︙ | ︙ | |||
93 94 95 96 97 98 99 | 'id' => null, 'code' => null, 'label' => null, ]; } $lines[] = (object) [ | | > | | | 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 'id' => null, 'code' => null, 'label' => null, ]; } $lines[] = (object) [ 'balance' => $result, 'id' => $account->id, 'code' => $account->code, 'label' => $account->label, 'is_debt' => $result < 0, ]; foreach ($lines as $k => &$line) { $line->credit = !$line->is_debt ? abs($line->balance) : 0; $line->debit = $line->is_debt ? abs($line->balance) : 0; if ($chart_change) { if (array_key_exists($line->code, $matching_accounts)) { $acc = $matching_accounts[$line->code]; $line->account = [$acc->id => sprintf('%s — %s', $acc->code, $acc->label)]; } } |
︙ | ︙ |