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 can i create my own scheduler in openerp 6.1

By
katty
on 10/20/14, 6:26 AM 2,099 views

In invoice i want to add penalty aftr evry 30 days i hv a module of add penalty n also hv a add penlty button but i want it add penlty in invoce automatically aftr 30days so any one can help me to add penalty automatically through scheduler.help mee  plss

1

In order to create and execute Scheduler, Here I define steps.

1) Create a record for ir.cron model. That can be done by 2 ways.

a. You can direct go to menu Settings >> Configuration >> Scheduler >> Schedule Actions and then you can create your record.

b. Same record you create from your xml file. In order to do that, in your xml file add following kind of xml code,

        <record model="ir.cron" id="account_modify_penalty">
            <field name="name">Update Customer Panelty</field>
            <field name="interval_number">1</field>
            <field name="interval_type">months</field>
            <field name="numbercall">-1</field>
            <field name="doall" eval="False"/>
            <field name="model" eval="'account.invoice'"/> ( Here you need to give the model in which you want to execute your method.) 
            <field name="function" eval="'YOUR CUSTOM FUNCTION'"/> ( Here give your custom function. )
            <field name="args" eval="'()'" />
        </record>

2) Create your custom function in any of model.  As per step 1, put that function name and model over there.

3) In order to provide custom arguments, use "args" field. 

4) Once you update your module, You can see your Schedule Action will be,

Settings >> Configuration >> Scheduler >> Schedule Actions menu.

Please note : The scheduled actions which are not active, they will not able to visible in list. You need to go to Advance search and find them.

Thanks but All Alrdy did all thse things but when i check the database it does not add panelty automatically.,so for that what can i do..??

katty
on 10/20/14, 7:16 AM

You might not added xml file under __openerp__.py file ?

Hiren Vora - CEO at Emipro Technologies Pvt. Ltd. India
on 10/20/14, 7:20 AM

also do you mean that you were not able to see your schedule job record in Settings >> Configuration >> Scheduler >> Schedule Actions menu ??

Hiren Vora - CEO at Emipro Technologies Pvt. Ltd. India
on 10/20/14, 7:21 AM

no ihv made a auto panelty shcedulr and i m able to see it in schedulr action menu but it is not working like in next execution date i gave the today date and time n check on the trmnl but it is not working thr n not update the penlty in database

katty
on 10/20/14, 7:26 AM

i simply add this in the .xml file " Auto Penalty 1 months -1 add.penalty test_amount () "

katty
on 10/20/14, 7:27 AM

Please post the screenshot ... because this seems not a problem of Scheduler ... it is problem of your program. May be you haven't write exact function name ....

Hiren Vora - CEO at Emipro Technologies Pvt. Ltd. India
on 10/20/14, 7:29 AM

i hv post the code thr pls see it and give the propewr solution Thanks

katty
on 10/20/14, 7:57 AM

when it runs than it give the error on the terminal like "test_amount() takes at least 4 arguments (3 given)" what is this..???

katty
on 10/20/14, 8:34 AM

Hiii Hiren Vora pls give the solution

katty
on 10/21/14, 12:30 AM

First of all in your method as follows, def test_amount(self, cr, uid, ids,context=None): Argument Ids is wrong, when you call any method from Scheduler, you can't pass Ids argument. Thats why you are getting error. Now If Ids argument is must needed in your function, then pass "False" from Schedular as Argument. For example, please see "Run MRP schedular" cron. You will see "(False,)" as in argument. People can guide you, but can't write code for you. I think you seems not a technical programmer. Please contact a technical programmer and assign him your work.

Hiren Vora - CEO at Emipro Technologies Pvt. Ltd. India
on 10/21/14, 7:58 AM

Thanks Mr Vora for ur help By ur help i get it resolve thanks.,hope u r also available in future for my help

katty
on 10/30/14, 6:19 AM
0
katty
On 10/20/14, 7:42 AM

This is my pewnlty code : .py file

 


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
        #raise orm.except_orm(_('Sum'), str(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')
        print".............today date..............",date_now
        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('/','')
        #raise orm.except_orm(_('Account'), str(ref))
        account = self.pool.get('account.account')
##        cr.execute("select id from account_account where account_account.name = 'Sundry Creditors Account'")
##        acc_id = cr.fetchone()
        acc_id = 11
##        raise orm.except_orm(_('Account'), (acc_id))
##        cr.execute("select id from account_account where account_account.name = 'Indirect Expenses'")
##        acc_id1 = cr.fetchone()
        acc_id1 = 39
##        journal = self.pool.get('account.journal')
##        cr.execute("select id from account_journal where name = 'Purchase Journal'")
##        j_id = cr.fetchone()
        j_id = 1
        #raise orm.except_orm(_('Account'), str(acc_id))
        invoice = self.pool.get('account.invoice')        
        cr.execute("select * from account_invoice where state = 'open' and payment_term > 0 and cash_credit='Credit Memo'")
        invoices = invoice.browse(cr, uid, map(lambda x: x[0], cr.fetchall()) )
        for inv in invoices:            
            if (inv.penalty_date):
                #raise orm.except_orm(_('Unknown Error'), str(inv.origin))
                dt = datetime.strptime(inv.penalty_date,'%Y-%m-%d')
                #raise orm.except_orm(_('Unknown Error'), str(dt))
            elif inv.date_due:
                dt = datetime.strptime(inv.date_due,'%Y-%m-%d')
                print "............................due date.............",dt
                #raise orm.except_orm(_('Unknown Error'), str(dt))
            date_diff = date_now - dt
            
            if inv.state == 'open' and date_diff.days >=30:
                #raise orm.except_orm(_('Unknown Error'), str(inv.id))
                amount = inv.residual * 0.03
  
                #raise orm.except_orm(_('Fees amount'), str(amount))
                inv.residual += amount
                inv.amount_total += amount
                #raise orm.except_orm(_('Fees amount and total'),str(inv.amount_total))
                orig = inv.origin
                #raise orm.except_orm(_('Fees amount'), str(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)
                   # raise orm.except_orm(_('Fees amount'), str(inv.serial_number)) 
                    for l in lines:                        
                        if l.debit > 0.00:                            
                            l.debit += amount                            
                            #cr.execute('update account_move_line set debit= %s where id= %s',(l.debit,l.id,))
                            #raise orm.except_orm(_('Fees amount'), str(l.id))
                        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('update account_move_line set credit= %s where id= %s',(l.credit,l.id,))
                            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()

 

 

 

This is .xml file:

<?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"/>
                    <!-- <field name="test"/> -->
                    <button name="test_amount" string="Penalty" icon="gtk-execute" type="object"/>
                    
                    <!-- <button name="get_result" string="Grand Total" 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">months</field>
            <field name="numbercall">-1</field>
            <field name="doall" eval="False"/>
            <field name="model">account.invoice</field>
            <field name="function">test_amount</field>
            <field name="args">()</field>
        </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>

 

i alsi try the module name "add.penalty"but it works

Solve it pls i tried it from 2 days

 

I got the problem. You define class, class add_penalty(osv.osv): _name = 'add.penalty' Instead you need to define your class like, class add_penalty(osv.osv): _inherit = 'account.invoice' the reason is, as per your scheduler xml code, you supposed to use account.invoice class's test_amount method.

Hiren Vora - CEO at Emipro Technologies Pvt. Ltd. India
on 10/20/14, 8:05 AM

to create a diffrnt table can i use like this class add_penalty(osv.osv): _name='add.penalty' _inherit='account_invoice' tell pls

katty
on 10/20/14, 8:11 AM

when it runs than it give the error on the terminal like "test_amount() takes at least 4 arguments (3 given)" what is this

katty
on 10/20/14, 8:22 AM

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: 10/20/14, 6:26 AM
Seen: 2099 times
Last updated: 3/16/15, 8:10 AM