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.

0

How to override def _amount_all(self, cr, uid, ids, field_name, arg, context=None): from Purchase order form odoo 8

By
Odoo
on 2/12/15, 5:50 AM 2,145 views

Hi all,

edited:16/2/2014

 I am facing some  issues in Purchase order customization module  ,issues are ..

1)     My custom Purchase order total amount is not reflected in the Accounting ,Supplier Invoices ...
2)    If I fill the Purchase order line first , after that I enter my extra cost(custom field) value then my Total in the Purchase order          line is not updated But If I enter my extra cost first ,then fill the purchase order line .It is working fine and Updated the Total

3)   If I add one more products into my purchase order line then  it automatically adds value from my previous  subtotal value( value of  price_subtotal from the previous line..)

4)  If I press Confirm order button then again update the amount_total  with previous price_subtotal value from Purchase Order 

How to solve these issuess...

Here is my code-----------

.py file---

from openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp


class purchase_order(osv.osv):
    _name='purchase.order'
    _inherit = 'purchase.order'
    
    def _calculate_total_amount(self, cr, uid, ids, context=None):
        total=0.0
        for cost in self.browse(cr, uid, ids, context=context):
            total = 0.00
            for line in cost.freight_ids:
                total += line.cost                    
            return total


    def _total_amount(self, cr, uid, ids, name, args, context=None): 
        result={}        
        for cost in self.browse(cr, uid, ids, context=context):
            result[cost.id] =self._calculate_total_amount(cr, uid, ids, context)
        return result
        
    def _amount_all(self, cr, uid, ids, field_name, arg, context=None):  
        print "Test########################"
      
        res = {}
        cur_obj=self.pool.get('res.currency')
        for order in self.browse(cr, uid, ids, context=context):
            res[order.id] = {
                'amount_untaxed': 0.0,
                'amount_tax': 0.0,
                'amount_total': 0.0,
            }
            val = val1 = 0.0
            cur = order.pricelist_id.currency_id
            for line in order.order_line:
               val1 += line.price_subtotal
               for c in self.pool.get('account.tax').compute_all(cr, uid, line.taxes_id, line.price_unit, line.product_qty, line.product_id, order.partner_id)['taxes']:
                    val += c.get('amount', 0.0)
            res[order.id]['amount_tax']=cur_obj.round(cr, uid, cur, val)
            res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1)
        res = super(purchase_order, self)._amount_all(cr, uid, ids, field_name, arg, context)
        amount_subtotal=self._calculate_total_amount(cr, uid, ids, context)
        for order in self.browse(cr, uid, ids, context=context):
            
            res[order.id]['amount_total'] += amount_subtotal+order.amount_untaxed
        return res
    
    def _get_order(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('purchase.order.line').browse(cr, uid, ids,
                                                                context):
            result[line.order_id.id] = True
        return result.keys()
    

 

    _columns={
              
            'freight_ids':fields.one2many('freight.cost','cost_id','Extra Cost Details'),
            'amount_subtotal': fields.function(_total_amount, type='float', string='Total',store=True),
            'amount_untaxed': fields.function(_amount_all, method=True,
            digits_compute=dp.get_precision('Account'),
            string='Untaxed Amount',
            store={
                'purchase.order.line': (_get_order, None, 10),
            }, multi="sums", help="The amount without tax"),
        'amount_tax': fields.function(_amount_all, method=True,
            digits_compute=dp.get_precision('Account'), string='Taxes',
            store={
                'purchase.order.line': (_get_order, None, 10),
            }, multi="sums", help="The tax amount"),
        'amount_total': fields.function(_amount_all, method=True,
                digits_compute=dp.get_precision('Account'), string='Total',
            store={
                'purchase.order.line': (_get_order, None, 10),
                }, multi="sums", help="The total amount"),
        'amount_untaxed': fields.function(_amount_all,
            digits_compute=dp.get_precision('Account'),
            string='Untaxed Amount',
            store={
                'purchase.order.line': (_get_order, None, 10),
            }, multi="sums", help="The amount without tax",
            track_visibility='always'),
        'amount_tax': fields.function(_amount_all,
            digits_compute=dp.get_precision('Account'), string='Taxes',
            store={
                'purchase.order.line': (_get_order, None, 10),
            }, multi="sums", help="The tax amount"),
        'amount_total': fields.function(_amount_all,
            digits_compute=dp.get_precision('Account'), string='Total',
            store={
                'purchase.order.line': (_get_order, None, 10),
            }, multi="sums", help="The total amount"),
    
                            }
    
    def dummy(self, cr, uid, ids, context=None):
        print "DDDDDDDDDDDDDDDD"
        return True
    


    
   
class freight_cost(osv.osv):
    
    _name='freight.cost'
    
    _columns={
              'cost_id':fields.many2one('purchase.order','name'),
              'name':fields.char('Name',size=64),
              'cost':fields.float('Price'),
              'description':fields.char('Description')
              }
    
    
    

.xml file--------

<record id="freight_tree_view" model="ir.ui.view">
                <field name="name">freight.cost.tree</field>
                <field name="model">freight.cost</field>
                <field name="arch" type="xml">
                    <tree string="Freight Cost tree">                                    
                        <field name="name"/>
                        <field name="cost"/>
                        <field name="description"/>
                        
                    </tree>
                </field>    
    </record>
    
        <record id="freight_form_view" model="ir.ui.view">
                        <field name="name">freight.cost.form</field>
                        <field name="model">freight.cost</field>
                        <field name="arch" type="xml">
                            <form string="Freight Cost form ">                                    
                                <group><field name="name"/>
                        <field name="cost"/>
                        <field name="description"/></group>
                            </form>
                        </field>    
        </record>
            
        <record model="ir.ui.view" id="purchase_freight_order_form">
         <field name="name">purchase.freight.order.form</field>
         <field name="model">purchase.order</field>
         <field name="type">form</field>
         <field name="inherit_id" ref="purchase.purchase_order_form"/>
         <field name="arch" type="xml">
              <xpath expr="/form/sheet/notebook/page[@string='Products']" position="after">
                  <page string="Freight Charges">
                <field name="freight_ids" colspan="4" />
            </page>
                 </xpath>
         </field>
     </record>

 

0
Sea
On 2/13/15, 4:39 AM

Hi,

The problem here looks like, you are trying to use a functional field which will be evaluated after the save along with other functional fields.

res[order.id]['amount_total'] += order.amount_subtotal+order.amount_untaxed --> for this line, you have to calculate the value for amount_subtotal field in  _amount_all method instead of taking it from order.amount_subtotal.

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):   #This code is not working and ,is this calculation is crct?
        print "Test########################"
        res = super(purchase_order, self)._amount_all(cr, uid, ids, field_name, arg, context)
        for order in self.browse(cr, uid, ids, context=context):
            res[order.id]['amount_total'] += order.amount_subtotal+order.amount_untaxed --> calculate the value for amount_subtotal in this method. don't take from order. It does not have any value by this time
        return res

 

Hi , LIBU

Try this out:

 

    def _calculate_total_amount(self, cr, uid, ids, context=None): #This is My calculation for find the extra cost of a single #purchase
        total=0.0
        for cost in self.browse(cr, uid, ids, context=context):
            total = 0.00
            for line in cost.freight_ids:
                total += line.cost                    
            return total


    def _total_amount(self, cr, uid, ids, name, args, context=None): #This is My calculation for find the extra cost of a single #purchase
        result={}        
        for cost in self.browse(cr, uid, ids, context=context):
            result[cost.id] =self._calculate_total_amount(cr, uid, ids, context)
        return result
        
    def _amount_all(self, cr, uid, ids, field_name, arg, context=None):  
        print "Test########################"
        res = super(purchase_order, self)._amount_all(cr, uid, ids, field_name, arg, context)
        amount_subtotal=self._calculate_total_amount(cr, uid, ids, context)
        for order in self.browse(cr, uid, ids, context=context):
            
            res[order.id]['amount_total'] += amount_subtotal+order.amount_untaxed
        return res

Hi I can get the idea But then How to change my code for calculate the value for amount_subtotal in this method ?

Odoo
on 2/13/15, 5:21 AM

Hi , LIBU Try this out: def _calculate_total_amount(self, cr, uid, ids, context=None): #This is My calculation for find the extra cost of a single #purchase total=0.0 for cost in self.browse(cr, uid, ids, context=context): total = 0.00 for line in cost.freight_ids: total += line.cost return total def _total_amount(self, cr, uid, ids, name, args, context=None): #This is My calculation for find the extra cost of a single #purchase result={} for cost in self.browse(cr, uid, ids, context=context): result[cost.id] =self._calculate_total_amount(cr, uid, ids, context) return result def _amount_all(self, cr, uid, ids, field_name, arg, context=None): print "Test########################" res = super(purchase_order, self)._amount_all(cr, uid, ids, field_name, arg, context) amount_subtotal=self._calculate_total_amount(cr, uid, ids, context) for order in self.browse(cr, uid, ids, context=context): res[order.id]['amount_total'] += amount_subtotal+order.amount_untaxed return res

Sea
on 2/13/15, 7:05 AM

Hi Sea,......first thanks for your answers..and your last answer is working fine.But Some issues in that code. I rewrite my question in the begining..can u please..check..thanks one more time...

Odoo
on 2/13/15, 10:37 PM

I have 3 issues , If I fill the Purchase order line first , after that I enter my extra cost(custom field) value then my Total in the Purchase order line is not updated But If I enter my extra cost first ,then fill the purchase order line .It is working fine and Updated the Total Second Problem is If I add one more products into my purchase order line then two time adds my subtotal value(douled the value of subtotal from the previous line..) Third problem is If I press Confirm order button then again update the Total from Purchase Order

Odoo
on 2/13/15, 10:43 PM

These are the issues..How to resolve this issues

Odoo
on 2/13/15, 10:44 PM
0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 2/13/15, 5:36 AM

calling super method works within a functional field definition
check code here: https://github.com/OCA/stock-logistics-warehouse/blob/7.0/stock_available_immediately/product.py#L52

The code changes you have made only affects for newly created Purchase order and does not affect old Purchase order.  The reason is that in the functional field, we have set it to only re-compute the field if Purchase order line is modified.

store={
      'purchase.order.line': (_get_order, None, 10),
}

NOTE:
If a computation is dependent on another functional field, then use multi to do the computation at once and not two independent functions

    def _amount_all(self, cursor, user, ids, field_name, arg, context = None):

        if context is None:
            context = {}
        res = {}
        res = super(SaleOrder, self)._amount_all(cursor, user, ids, field_name, arg, context)

        for order in self.browse(cursor, user, ids):
            total = 0.00
            for line in cost.freight_ids:
                total += line.cost
            res[order.id]['markup_rate'] = total
        return res

'amount_subtotal': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Sub Total',
            store={
                'purchase.order.line': (_get_order, None, 10),
            }, multi="sums"),

does your code work for new PO?

Atchuthan - Technical Consultant, Sodexis Inc
on 2/13/15, 5:43 AM

no this amount_subtotal... is not in the purchase order line can you check my xml file ,then u can understand...please help,I want to calculate the untaxed amount with my custom field('amount_subtotal') ,thats my aim .when I click on the save or update button..

Odoo
on 2/13/15, 5:52 AM

that means,Untaxed Amount = Total of subtotal field+ amount_subtotal(my custom field)

Odoo
on 2/13/15, 5:54 AM

before the tax calculation

Odoo
on 2/13/15, 5:54 AM

I Tried but no solution..

Odoo
on 2/13/15, 3:06 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

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

Question tools

1 follower(s)

Stats

Asked: 2/12/15, 5:50 AM
Seen: 2145 times
Last updated: 3/16/15, 8:10 AM