Hi all,
I have following fields and logic:
1. receipt - expense = balance
2. receipt - expense + 'balance from previous row' = 'balance in this row'
3. receipt - expense + 'balance from previous row' = 'balance in this row'
balance is compute field not stored in the database, only visible in the view
@api.depends('receipt', 'expense')
def _compute_balance(self):
for record in self:
record.balance = record.receipt - record.expense
view row number | Receipt | Expense | Balance |
1. | 5 | 4 | 1 |
2. | 10 | 15 | -4 |
3. | 0 | 5 | -9 |
4. | 10 | 0 | 1 |
5. | 10 | 11 | 0 |
Hi Prakash,
thank you for your effort, can you please help me a little bit more so I could better understand!?
Here is my class and view code
class cashflow(models.Model):
_name = 'singleentrybookkeeping.cashflow'
_description = "Knjiga blagajne"
_order = 'date'
date = fields.Date(string="Datum", default=fields.Date.today)
accounting_doc = fields.Char("Broj i oznaka knjigovodstvene isprave")
description = fields.Char("Opis")
currency_id = fields.Many2one("res.currency", string="Valuta", default=29)
receipt = fields.Monetary(string="Primitak")
expense = fields.Monetary(string="Izdatak")
balance = fields.Monetary(store='False', compute='_compute_balance', string="Saldo")
@api.depends('receipt', 'expense')
def _compute_balance(self):
balance = 0
for record in self:
res = record.receipt - record.expense
record.balance = res - balance
balance = res
<record model="ir.ui.view" id="cashflow_tree_view">
<field name="name">cashflow.tree</field>
<field name="model">singleentrybookkeeping.cashflow</field>
<field name="arch" type="xml">
<tree string="Cashflow Tree" editable="top">
<field name="date"/>
<field name="accounting_doc"/>
<field name="description"/>
<field name="currency_id" attrs="{'column_invisible': [('parent.field_name','=',False)]}"/>
<field name="receipt" widget="monetary"/>
<field name="expense" widget="monetary"/>
<field name="balance" widget="monetary"/>
</tree>
</field>
</record>
balance is computed not stored field in the database.
I searched for similar solutions and tried to implement those, but something I am missing:
https://stackoverflow.com/questions/40372212/how-to-get-the-previous-data-or-the-latest-data-by-id-in-odoo/40374293
https://www.odoo.com/es_ES/forum/ayuda-1/question/how-to-get-the-last-record-id-from-existing-table-40446
https://www.odoo.com/es_ES/forum/ayuda-1/question/how-to-get-the-value-of-the-previous-row-and-store-it-in-the-next-row-one2many-140982
https://stackoverflow.com/questions/51316091/difference-between-curent-record-and-previous-record-odoo-11
https://www.odoo.com/es_ES/forum/ayuda-1/question/how-can-i-get-the-previous-record-in-a-table-38261
https://www.odoo.com/es_ES/forum/ayuda-1/question/get-data-from-previous-entry-in-tree-view-170889
https://www.odoo.com/fr_FR/forum/aide-1/question/get-value-of-field-from-previous-record-136171
I need dynamic solution, when someone sorts from view by date or any existing column, the calculations must be on the fly: I think that I need first to get previous_balance = sum_of_the_all_previous_receipts - sum_of_the_all_previous_expenses
After that I should calculate current_balance = (current_receipts - current expense) + previous_balance
Thanks,
Ivica