This question has been flagged
2 Replies
4749 Views

I write query but don't get data from database , here this def _compute_amount function calculate total field and store data after that i need summation of total field data retrieve to cash field .

How i can i solve this kindly help me

.py file

from odoo import api, models, fields


class CashinBalance(models.Model):
_name = 'cashinbalance.cashinbalance'
_description = 'Cash-in Balance'

atk = fields.Integer('TK.1000')
btk = fields.Integer('TK.500')
ctk = fields.Integer('TK.100')
dtk = fields.Integer('TK.50')
etk = fields.Integer('TK.20')
ftk = fields.Integer('TK.10')
gtk = fields.Integer('TK.5')
htk = fields.Integer('TK.2')
date = fields.Date(required=True, default=fields.Date.context_today)
total = fields.Integer('Total Cash-In Balance', compute='_compute_amount', store=True)
cash = fields.Integer(compute='get_data', store=True)

@api.depends('atk', 'btk', 'ctk', 'dtk', 'etk', 'ftk', 'gtk', 'htk')
def _compute_amount(self):
for expense in self:
expense.total = expense.atk * 1000 + expense.btk * 500 + expense.ctk * 100 + expense.dtk * 50 \
+ expense.etk * 20 + expense.ftk * 10 + expense.gtk * 5 + expense.htk * 2

def get_data(self):
for rec in self:
self._cr.execute("""SELECT SUM(total) from cashinbalance_cashinbalance""")
data = self._cr.dictfetchall()
rec.cash = data[0]['sum']


.xml
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!-- Tree view -->
<record id="cashin_tree_view" model="ir.ui.view">
<field
name="name">cashin.tree.view</field>
<field
name="model">cashinbalance.cashinbalance</field>
<field
name="arch" type="xml">
<tree
string="Cash">
<field
name="atk"/>
<field
name="btk"/>
<field
name="ctk"/>
<field
name="dtk"/>
<field
name="etk"/>
<field
name="ftk"/>
<field
name="gtk"/>
<field
name="htk"/>
<field
name="date"/>
<field
name="cash"/>
</tree>
</field>
</record>
<!-- End Tree View -->

<!-- Form View for -->
<record id="cashin_form_view" model="ir.ui.view">
<field
name="name">cashin.form.view</field>
<field
name="model">cashinbalance.cashinbalance</field>
<field
name="arch" type="xml">
<form>
<sheet>
<group
style="text-align:center" string="Yuko Leather Goods Ltd.">
</group>
<group
style="text-align:center" string="Cash-In Details">
</group>

<group>
<group
string="Tk-Quantity">
</group>
</group>

<group>
<group>
<field
name="atk"/>
<br/>
<field
name="btk"/>
<br/>
<field
name="ctk"/>
<br/>
<field
name="dtk"/>
<br/>
<field
name="etk"/>
<br/>
<field
name="ftk"/>
<br/>
<field
name="gtk"/>
<br/>
<field
name="htk"/>
<br/>
<field
name="date"/>
<br/>
</group>
<group>
<field
name="cash"/>
</group>
</group>
</sheet>
</form>
</field>
</record>


<record
id="action_cashin" model="ir.actions.act_window" >
<field
name="name"> Cash </field>
<field
name="type">ir.actions.act_window</field>
<field
name="res_model">cashinbalance.cashinbalance</field>
<!-- <field name="view_type">form</field>-->
<field name="view_mode">tree,form</field>
</record>

<menuitem
id="Cash_root" name="Cash Balance Monitoring" sequence="1" />
<menuitem
id="cash_in" name="Cash-In" parent="Cash_root" action="action_cashin"/>
</odoo>



Avatar
Discard
Best Answer

I think you want to calculate the total from the previous records just like the cumulative total.

Try the following code:

    @api.depends('total')
def get_data(self):
for rec in self:
if not isinstance(rec.id, models.NewId):
cash = 0
for cashin in self.search([('id', '<', rec.id)]):
cash += cashin.total
cash += rec.total
rec.cash = cash


Avatar
Discard
Author

Thank you so much for your great help