This question has been flagged
2 Replies
1058 Views

I'm new to odoo and trying to add a new module to sales.
I have added a module and a field called job_no, it is placed in the sales order tree view.
my goal is to automatically generate sequential values in the job_no field when the confirm button is pressed.

but I'm having trouble connecting the confirmation button that is already in odoo.
here is the code i have made:
this is a .py file

from odoo import api, fields, models, _

class sale_warisan(models.Model):
_inherit = 'sale.order'

jenis_pekerjaan = fields.Selection([
('jasa', 'Jasa'),
('proyek', 'Proyek'),
('belt', 'Belt'),
('aksesoris', 'Aksesoris'),
], required=True, default='aksesoris', tracking=True)

job_no = fields.Char(string='Job Number', tracking=True, copy=False, readonly=True,
default=lambda self: _('New'))

# ignore this one, because I'm still looking for tutorials on the web and youtube, but I haven't been able to get it working
# def create(self, vals):
# if vals.get('job_no', _('New')) == _('New'):
# vals['job_no'] = self.env['ir.sequence'].next_by_code('sales.order') or _('New')
# res = super(sale_warisan, self).create(vals)
# return res


and this .xml

xml version="1.0" encoding="UTF-8" ?>



id="tampilan_quotation_form_warisan" model="ir.ui.view">
name="name">tampilan.quotation.warisan
name="model">sale.order
name="inherit_id" ref="sale.view_order_form"/>
name="arch" type="xml">
expr="//field[@name='partner_id']" position="after">
name="jenis_pekerjaan"/>





id="tampilan_quotation_tree_warisan" model="ir.ui.view">
name="name">tampilan.quotation.warisan
name="model">sale.order
name="inherit_id" ref="sale.view_quotation_tree"/>
name="arch" type="xml">
expr="//field[@name='partner_id']" position="after">
name="jenis_pekerjaan"/>






id="urutan_job_no" model="ir.sequence">
name="name">Job Number
name="code">sale.order
name="padding">4




id="tampilan_order_tree_warisan" model="ir.ui.view">
name="name">tampilan.order.warisan
name="model">sale.order
name="inherit_id" ref="sale.view_order_tree"/>
name="arch" type="xml">
expr="//field[@name='name']" position="before">
name="job_no"/>





Avatar
Discard
Author Best Answer

Hi,

thanks for the answer, I've tried it but the message appears: TypeError: object 'bool' does not support item assignment.

Avatar
Discard
Author

after i tried again it worked.

with the following code:
def action_confirm(self):
res = super(SaleOrder, self).action_confirm()
if self.env.su:
self = self.with_user(SUPERUSER_ID)

for jb in self:
if jb.job_no == _('Offer'):
jb.job_no = self.env['ir.sequence'].next_by_code('job.order') or _('Offer')
return res

Best Answer

Hi,

If you are looking to generate the sequence number in the newly added field on confirming the sales order, instead of inheriting the create method, you have to inherit the function that get executed on clicking the confirm button.


In sale.order model on clicking the confirm button action_confirm method is getting executed, thus super this method in your module and add the logic to generate the sequence.


def action_confirm(self):
res = super(SaleOrder, self).action_confirm()
res.job_no = self.env['ir.sequence'].next_by_code('sales.order') or _('New')
return res



Thanks

Avatar
Discard