This question has been flagged
2342 Views

Hi all,

 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>

Avatar
Discard