Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add penalty in customer invoice in OpenERP-6.1?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 701
    Comment on it

    In OpenERP first, install the account module and then create the own module and relate it to the fields payment term in account module.

    Follow these steps given below:

    Step1- First create add_penalty.py file in your own module and pass this file in your __init__.py file. Then create a function and pass this function in button.

    Use this code given below:

    import time
    from lxml import etree
    import decimal_precision as dp
    from tools.translate import _
    import netsvc
    import pooler
    from osv import fields, osv, orm
    import datetime
    from datetime import datetime
    from datetime import date, timedelta
    import types
    class add_penalty(osv.osv):
        _name = 'add.penalty'
        def _get_result(self,cr,uid,ids,name,args,context=None):
            res={}
            invoice = self.pool.get('account.invoice')
            cr.execute("select sum(amount_total) from account_invoice")
            invoices = cr.fetchone()
            if invoices:
                name = invoices[0]
                for inv in self.pool.get('add.penalty').browse(cr, uid, ids, context=context):
                    res[inv.id] = name
            return res    
        _columns ={
                    'date':fields.date('Date'),
                    'test':fields.function(_get_result,'Grand Total', store = True),
                }
        def get_result(self,cr,uid,ids,context=None):     
            invoice = self.pool.get('account.invoice')
            cr.execute("select sum(amount_total) from account_invoice")
            invoices = cr.fetchone()
            if invoices:
                name = invoices[0]            
            raise orm.except_orm(_('Sum'), str(name))
            return True
        def test_amount(self, cr, uid, ids,context=None):   
            res = {}
            date_diff = 0
            now = datetime.now()        
            now = now.strftime('%Y-%m-%d')
            date_now = datetime.strptime(now,'%Y-%m-%d')
            dt = date_now
            name = 'CD Break'
            comment = 'Write-Off'
            cd_flag = True
            tname = 'None'
            qty = 1
            rate = 1.00
            amount = 0.0
            amt = 0.0
            pay_option = 'without_writeoff'
            mline_state = 'valid'
            move_state = 'draft'
            ptype = 'dr'
            v_state = 'draft'
            v_type = 'sale'         
            seq_id = self.pool.get('ir.sequence').search(cr, uid, [('name','=','Sales Journal')])[0]
            vnumber = self.pool.get('ir.sequence').next_by_id(cr, uid, seq_id)
            ref = vnumber.replace('/','')
            account = self.pool.get('account.account')
            acc_id = 11
            acc_id1 = 39
            j_id = 1
            invoice = self.pool.get('account.invoice')        
            cr.execute("select * from account_invoice where state = 'open' and payment_term > 0 and cash_credit='Credit Tax Invoice'")
            invoices = invoice.browse(cr, uid, map(lambda x: x[0], cr.fetchall()) )
            for inv in invoices:            
                if (inv.penalty_date):
                    dt = datetime.strptime(inv.penalty_date,'%Y-%m-%d')
                elif inv.date_due:
                    dt = datetime.strptime(inv.date_due,'%Y-%m-%d')
                date_diff = date_now - dt
                if inv.state == 'open' and date_diff.days >=30:
                    amount = inv.residual * 0.03
                    inv.residual += amount
                    inv.amount_total += amount
                    orig = inv.origin              
                    if orig:
                        line = self.pool.get('account.move.line')
                        cr.execute("SELECT id FROM account_move_line WHERE ref = %s",(orig,))
                        lines = line.browse(cr, uid, map(lambda x: x[0], cr.fetchall()) )
                        product = self.pool.get('product.product')                    
                        credit_flag = 'true'                    
                        serial = str(name + '-' + inv.serial_number)
                        for l in lines:                        
                            if l.debit > 0.00:                            
                                l.debit += amount                            
                            if l.credit > 0.00 and l.product_id.id > 0 and credit_flag == 'true':
                                credit_flag = 'false'                                               
                                l.credit += amount
                                cr.execute('insert into account_invoice_line (origin,account_id,name,invoice_id,price_unit,quantity,partner_id,price_subtotal,tax_name) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)', (orig,inv.account_id.id,name,inv.id,amount,qty,inv.partner_id.id,amount,tname)) 
                                cr.execute('insert into account_voucher (create_date,date,type,partner_id,comment,company_id,account_id,period_id,move_id,journal_id,amount,payment_rate_currency_id,payment_rate,payment_option,state,number) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING id', (datetime.now(),datetime.now(),v_type,inv.partner_id.id,comment,inv.company_id.id,acc_id,inv.period_id.id,inv.move_id.id,inv.journal_id.id,amount,inv.currency_id.id,rate,pay_option,v_state,vnumber,))
                                v_id = cr.fetchone()
                                cr.execute('insert into account_voucher_line (create_date,voucher_id,company_id,account_id,name,amount,type) values (%s,%s,%s,%s,%s,%s,%s)',(datetime.now(),v_id,inv.company_id.id,acc_id1,name,amount,ptype))
                                cr.execute('insert into account_move (name,journal_id,period_id,partner_id,date,state,ref) values(%s,%s,%s,%s,%s,%s,%s) RETURNING id' ,(vnumber,j_id,inv.period_id.id,inv.partner_id.id,datetime.now(),move_state,ref))
                                move = cr.fetchone()
                                cr.execute('insert into account_move_line (create_date,journal_id,partner_id,credit,debit,ref,account_id,period_id,company_id,date,move_id,name,state) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',(datetime.now(),j_id,inv.partner_id.id,amt,amount,ref,acc_id,inv.period_id.id,qty,datetime.now(),move,serial,mline_state))
                                cr.execute('insert into account_move_line (create_date,journal_id,partner_id,credit,debit,ref,account_id,period_id,company_id,date,move_id,name,state) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',(datetime.now(),j_id,inv.partner_id.id,amount,amt,ref,acc_id1,inv.period_id.id,qty,datetime.now(),move,name,mline_state))
                        cr.execute("update account_invoice set penalty_date=%s, cd_break=%s where id=%s",(datetime.now(),cd_flag,inv.id,))
            return True
    
        def button_compute(self, cr, uid, ids, context=None, set_total=False):
            for line in self.browse(cr, uid, ids, context=context):
                if set_total:
                    self.pool.get('add.penalty').write(cr, uid, [line.id], {'name': line.name})
            return True
    
    add_penalty()
    

     

    Step2- Then create add_penalty.xml file in your own module and pass this file in __openerp__.py file.

    Use this code given below:

    <?xml version="1.0" ?>
    <openerp>
        <data>       
            <record id="view_invoice_penalty_form" model="ir.ui.view">
                <field name="name">penalty.form</field>
                <field name="model">add.penalty</field>
                <field name="type">form</field>
                <field name="arch" type="xml">
                    <form string="Penalty" >
    					<field name="date"/>
                        <button name="test_amount" string="Penalty" icon="gtk-execute" type="object"/>			
                    </form>
                </field>
            </record>
             
             <record id="ir_cron_mail_gateway_action" model="ir.cron">
                <field name="name">Auto Penalty</field>
                <field name="interval_number">1</field>
                <field name="interval_type">days</field>
                <field name="numbercall">-1</field>
                <field name="doall" eval="False"/>
                <field name="model">add.penalty</field>
                <field name="function">test_amount</field>
               <field eval="'()'" name="args"/>
            </record>           	
    		<record model="ir.actions.act_window" id="action_penalty_form">
    			<field name="name">Add penalty</field>
    			<field name="res_model">add.penalty</field>
    		</record>		
    		<menuitem name="Add Penalty" id="menu_penalty" parent="account.menu_finance" action="action_penalty_form" sequence="2"/>		
    	</data>
    </openerp>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: