Monday 25 July 2016

How to make sequence name Odoo



http://dedyyuristiawan.blogspot.com


Hello world. Today i am gonna share about little trick in Odoo, this is a how to make field name sequence. Actually its basic from Odoo, but i just know when i got task like that. Lol. So just follow my step by step.

1. Declaration code for sequence

For declaration sequence i create xml file like this. the code is test.dedy.yuristiawan, prefix DY, padding is 5, and it would be like this 'DY00001'. Prefix is for first char (DY) and padding is for long number (00001). You can call this sequence with test.dedy.yuristiawan. See this code

  <?xml version="1.0" encoding="utf-8"?>  
  <openerp>  
    <data noupdate="1">  
      <record id="seq_test_dedy_yuristiawan" model="ir.sequence">  
        <field name="name">Test Dedy Yuristiawan</field>  
        <field name="code">test.dedy.yuristiawan</field>  
        <field name="prefix">DY</field>  
        <field name="padding">5</field>  
        <field eval="1" name="number_increment"/>  
      </record>  
    </data>  
  </openerp>  

and then i put this code in test_dedy_yuristiawan.xml

2. Override Function set Field Name, before Create.

Yap, now i override create function like this. This code i set field name readonly=True (anybody can't change) and call  self.env['ir.sequence'].next_by_code('test.input.machine') that i create in xml file. If you don't know about override function i will create specially post for How to Override Function in Odoo in the next post.

name = fields.Char('Code Dedy', readonly=True)

@api.model
def create(self, vals):
    print "+++++++++++++++++++++++++++++++++++++++++++++++++"
    print "prepare inherit create function"
    print "change the name TI with sequence"
    vals['name'] = self.env['ir.sequence'].next_by_code('test.input.machine')
    print "Inherit complete"
    print "+++++++++++++++++++++++++++++++++++++++++++++++++"
    return super(test_input_machine, self).create(vals)


3. Declaration xml file into __openerp__.py

Last step don't forget to declaration xml file to __openerp__.py

'data'          : [
                         'data/test_dedy_yuristiawan.xml',

That it, i hope this is can help you little bit, so if you still confuse about my code, you fill comment bellow and i will try to help you later. Thanks.



Share:
Lokasi: Semarang, Semarang City, Central Java, Indonesia

15 comments:

  1. Hi, can you help me a bit with this? i'm trying to create a sequencer for the pos.order model, i need to put a sequence number in a field whenever the order is not in the 'invoiced' state. hope you can give me some light about it. Thanks!

    ReplyDelete
    Replies
    1. Hi Othawan, maybe you can add condition in function create, you can set sequencer in this condition if self.state != 'invoiced' : . So when state in invoiced, function create will fill default base Odoo. Maybe this can help you.

      Delete
    2. Thanks for your reply i'm just starting developing modules in odoo, so i will need to figure out how to do it.

      Delete
    3. your welcome Othawan, take it easy man, if you know just little bit process and syntax Odoo, you will realise 'develop Odoo its easy'

      Delete
  2. Hello All, In V10, How can I put control on generation of Automatic sequence numbers ? For example, If we take Manufacturing Order. When we create Manufacturing Order, a sequence like MO/0001 is generated. Suppose we discarded it without Saving. Next time when we create a new order the sequence we are getting is MO/0002. How to prevent this?

    please tell me in odoo10

    ReplyDelete
  3. hai.. then how to reset the sequence yearly?

    ReplyDelete
    Replies
    1. hi, thats great question, sorry now i am not working on Odoo environment, that possible to reset the sequence yearly, you can create file xml preparing for next year, and make condition on prython code for next year too, but this is not best practice, i think. Perhaps you can ask to your senior on Port Cities, He is AFK (Akhmad Firstyan Kresna), I know him, i think he is more genius about Odoo. I am actually ex PortCities engineer.

      Delete
    2. No, he doesn't know.. :D dia g ngerti mas gmn caranya.. dia paling jago js.. kemarin aku udh nyoba2 di pythonnya, tp blm bisa juga..

      Delete
  4. kalau gitu makasih ya mas,

    ReplyDelete
  5. Hi' Dedy. May you help me please? I need to change the ticket numbers sequence in POS. I'm using Odoo 8. Thanks in advanced!

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. hi sir. How can i use this test_dedy_yuristiawan.xml sequence? i have my own xml, did i need to put any line codes to my xml to link to your test_dedy_yuristiawan.xml? Hope you can help and answer me. Many thanks

    ReplyDelete
  8. "test.input.machine" is this class in our models ? or something else ?

    ReplyDelete
  9. i want to create a sequence and that model in tree and linked with form with one2many...but i want is boolean is true then generate sequence and if boolean false then dont generate sequence..my code is this..
    @api.model
    def create(self, vals):

    new_record = super(BundlesCuttingTree, self).create(vals)
    print "check1"
    if new_record.barcode_boolean == True:
    print "check2"
    print new_record.barcode
    new_record.barcode = self.env['ir.sequence'].next_by_code('bundles.cutting.tree')
    print type(new_record.barcode)
    print new_record.barcode
    return new_record

    ReplyDelete


<br>


Advice for contacting me at dedyy54@gmail.com