0
votes

I have customer and deposit field in customer form which is one2many actually but user add a line in deposit and submit or edit some existing. I want to do some calculations on that event . I tried onchange and compute both but its not working.

1
Can you add an excerpt from your code with the XML fields and the Python fields with what you've tried?travisw

1 Answers

0
votes

add or edit One2many fields is FormView.

so you just put the field what you what to do calculate in FormView *.xml

Ex. with my SUM fields

        <record model="ir.ui.view" id="view_nstda_bst_dbill_form">
            <field name="name">nstda.bst.dbill.form</field>
            <field name="model">nstda.bst.dbill</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="nstda_bst_dbill" class="nstda_bst_dbill_form" >
                    <group>
                        <field name="status" invisible="1" />
                        <field name="matno" />
<!--                        <field name="matdesc" /> -->

                        <label for="balance" />
                        <div>
                            <field name="balance" style="width: 20%%" />
                            <field name="uom_1" style="width: 10%%;" 
                                attrs="{'invisible':[('matno','=',False)]}" />
                        </div>

                        <label for="balance_rs" />
                        <div>
                            <field name="balance_rs" style="color:Red;width: 20%%" />
                            <field name="uom_2" style="width: 10%%;" 
                                attrs="{'invisible':[('matno','=',False)]}" />
                        </div>

                        <label for="qty" />
                        <div>
                            <field name="qty" style="width: 20%%" 
                                attrs="{'required':[('status','in',['draft','edit',False])]}" />
                            <field name="uom" style="width: 10%%;" 
                                attrs="{'invisible':[('matno','=',False)]}" />
                        </div>

<!--                        <field name="unitprice" /> -->
                        <label for="unitprice" />
                        <div>
                            <field name="unitprice" style="width: 20%%" />
                            <field name="currency" style="width: 10%%;" />
                        </div>

                        <field name="sum" invisible="1" />
                        <field name="dbill_discount_sum" invisible="1" />
                    </group>
                </form>
            </field>
        </record>

and the *.py side is...

Ex.

@api.one
@api.onchange('qty','matno')
@api.depends('qty','matno')
def _set_sum(self):
    self.sum  = self.unitprice * self.qty


sum = fields.Float(string="summary", store=True, compute='_set_sum')
matno = fields.Many2one('bst.stock', 'matno')
qty = fields.Integer('qty')