166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
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
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
|
|
>
|
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
|