Search code examples
pythonauto-incrementconfirmodoo-9

Make a 'ref' field to auto increment when I press CONFIRM SALE button


confrm_sale

I have the problem how to make a 'ref' field to be auto increment every time I press the Confirm Sale Button.

In my first case I made this field to be auto increment every time I create a new customer with the following code:

Python code:

@api.model
def create(self, vals):
    vals['ref'] = self.env['ir.sequence'].get('res.debt')
    return super(Partner, self).create(vals)

XML code:

<record id="your_sequence_id" model="ir.sequence">
          <field name="name">Reference</field>
          <field name="padding">3</field>
          <field name="code">res.debt</field>
      </record>

Now my problem is that I want this field also to be auto incremented, but not when I create the customer, only when I press the Confirm Sale button.

For example, I create a new sale order and I create a new customer for that order and I click save. When I now press Confirm Sale, that action needs to trigger auto increment for the internal reference field (ref).

If I make an order for existing customer than it should take the old sequence for that customer.

I have this code for the action_confirm():

@api.multi
def action_confirm(self):
    for order in self:
        order.state = 'sale'
        if self.env.context.get('send_email'):
            self.force_quotation_send()
        order.order_line._action_procurement_create()
        if not order.project_id:
            for line in order.order_line:
                if line.product_id.invoice_policy == 'cost':
                    order._create_analytic_account()
                    break
    if self.env['ir.values'].get_default('sale.config.settings', 'auto_done_setting'):
        self.action_done()
    return True

Probably my first code for the auto increment I should add in this second code but I don't know how to do that.

Can anyone help? Thank you in advance.


Solution

  • If I get your requirement right, I guess you should do something like inside your loop on orders:

    order.partner_id.ref = self.env['ir.sequence'].get('res.debt')