Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Function for total discount [Closed]

By
Susi
on 1/15/15, 6:13 AM 1,024 views

The Question has been closed

by
Susi
on 01/21/2016 23:14:59

i have field in subsidi barang.line

                'diskon_ahm':fields.float("Diskon AHM"),
                'diskon_md':fields.float("Diskon MD"),
                'diskon_dealer':fields.float("Diskon Dealer"),
                'diskon_finco':fields.float("Diskon Finco"),
                'diskon_others':fields.float("Diskon Others"),
                'total_diskon':fields.float("Total Diskon"),

total diskon is amount for diskon_ahm+diskon_md+diskon_dealer+diskon_finco+diskon_others

how to code this  function...anyone help me please

2

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 1/15/15, 7:22 AM

Make total_diskon as a function field.  You can find a lot of examples of function fields in the official addons, one pretty simple example is 'price_subtotal' of sale.order.line.

but its different... in sale.order.line have tax_obj = self.pool.get('account.tax') cur_obj = self.pool.get('res.currency') and in my form nothing field many2one account.tax and res.currency

Susi
on 1/15/15, 7:32 AM

Of course it is different. The one I am giving you is an example so that you can learn how field.function work.

Ivan
on 1/15/15, 7:34 AM
1
Susi
On 1/15/15, 11:33 PM

to ivan

i have code like this but its doesnt work..

class wtc_program_subsidi_line(osv.osv):
    _name = 'wtc.program.subsidi.line'
    def _amount_line(self, cr, uid, ids, field_name, arg, context=None):
        if context is None:
            context = {}
        for line in self.browse(cr, uid, ids, context=context):
            price = (1-(line.diskon_ahm or 0.0)+(line.diskon_md or 0.0) + (line.diskon_dealer or 0.0) +(line.diskon_finco or 0.0)+(line.diskon_others or 0.0) / 100.0)
            total = tax_obj.compute_all(cr, uid, line.diskon_ahm, line.diskon_md, line.diskon_dealer,line.diskon_finco,line.diskon_others)
            res[line.id]=price['total_diskon']
        return res
    _columns = {
                'program_subsidi_id':fields.many2one('wtc.program.subsidi',"Program Subsidi",required=True),
                'product_template_id': fields.many2one('product.template', 'Product', required=True),
                'tipe_dp': fields.selection([('min','Min'),('max','Max')], 'Tipe DP', change_default=True, select=True),
                'amount_dp':fields.float("DP Minimal"),
                'diskon_ahm':fields.float("Diskon AHM"),
                'diskon_md':fields.float("Diskon MD"),
                'diskon_dealer':fields.float("Diskon Dealer"),
                'diskon_finco':fields.float("Diskon Finco"),
                'diskon_others':fields.float("Diskon Others"),
                'total_diskon':fields.function(_amount_line, string='Total Diskon', digits_compute= dp.get_precision('Account')),
                
                }

and i dont understand how to use function digits_compute=dp.get_precision('Account') what should i fill in dp.get_precision(?)

thanks before

 

Are you trying to calculate just the total of the field or you need more calculation on top? digits_compute is optional, but it is a good practise to put in all float fields to specify the number of decimal points to keep for this field. Account is a configuration name in Decimal Precision (dp, you can access it from Settings >> Technical >> Database Structure >> Decimal Accuracy menu) that is used to set the number of decimal points for monetary amounts in general. So, if the number in the diskon_* fields and total_diskon field are monetary amounts, then it is a good choice, if it is meant for other things, then you might want to set a different configuration. Now, the function. Do you understand what price = (1-(line.diskon_ahm or 0.0)+(line.diskon_md or 0.0) + (line.diskon_dealer or 0.0) +(line.diskon_finco or 0.0)+(line.diskon_others or 0.0) / 100.0 does? This is just a pure calculation. tax_obj.compute_all is used if you want to calculate the tax composition of a price against a tax. If you don't need tax component, you do not need to call this. If you need to calculate the tax component, you are calling it in an incorrect way. If you take a look at the original code: tax_obj.compute_all(cr, uid, line.tax_id, price, line.product_uom_qty, line.product_id, line.order_id.partner_id), it needs a cr, uid, tax object, the price (float), quantity (float), product object, and partner object. Also, compute_all will not have 'total_diskon' component in it. For a function field to work, you need to specify the function with the correct signature and provide a correct return value. In this case, the return value will need to be a dictionary whose key is the object's database ID and the dictionary value is the value of the field of that object.

Ivan
on 1/16/15, 12:36 AM

i just want to get total_diskon without tax, i just want to sum diskon_ahm+diskon_md+diskon_dealer+diskon_finco+diskon_others

Susi
on 1/16/15, 4:56 AM

Then you need to assign just that to the res[line.id]

Ivan
on 1/16/15, 6:29 AM

like this res[line.id]=price['total_diskon'] ? but its error res[line.id]=price['total_diskon'] TypeError: 'float' object has no attribute '__getitem__'.. thanks before

Susi
on 1/16/15, 6:47 AM

i see,,,now i understand thanks

Susi
on 1/16/15, 10:24 AM

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/15/15, 6:13 AM
Seen: 1024 times
Last updated: 1/21/16, 11:14 PM