Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
1 Antworten
1335 Ansichten

Hello everyone so i tried to extend python function but its not working. the function i want to extend is from model 'sale.order' and the function is like this

def _prepare_purchase_order_data(self, company, company_partner):
""" Generate purchase order values, from the SO (self)
:param company_partner : the partner representing the company of the SO
:rtype company_partner : res.partner record
:param company : the company in which the PO line will be created
:rtype company : res.company record
"""
self.ensure_one()
# find location and warehouse, pick warehouse from company object
warehouse = company.warehouse_id and company.warehouse_id.company_id.id == company.id and company.warehouse_id or False
if not warehouse:
raise UserError(_('Configure correct warehouse for company(%s) from Menu: Settings/Users/Companies', company.name))
picking_type_id = self.env['stock.picking.type'].search([
('code', '=', 'incoming'), ('warehouse_id', '=', warehouse.id)
], limit=1)
if not picking_type_id:
intercompany_uid = company.intercompany_user_id.id
picking_type_id = self.env['purchase.order'].with_user(intercompany_uid)._default_picking_type()
return {
'name': self.env['ir.sequence'].sudo().next_by_code('purchase.order'),
'origin': self.name,
'partner_id': company_partner.id,
'picking_type_id': picking_type_id.id,
'date_order': self.date_order,
'company_id': company.id,
'fiscal_position_id': company_partner.property_account_position_id.id,
'payment_term_id': company_partner.property_supplier_payment_term_id.id,
'auto_generated': True,
'auto_sale_order_id': self.id,
'partner_ref': self.name,
'currency_id': self.currency_id.id,
'order_line': [],
}


i want to extend using below code, just want to add another line to export that is custom_internal

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

custom_internal = fields.Boolean(string='Internal', default=False)
 
def _prepare_purchase_order_data(self, company, company_partner):
# Call the original function to get the dictionary
print('debug1')
res = super(SaleOrder, self)._prepare_purchase_order_data(company, company_partner)
print('debug2')
# Update internal field
res.update({'custom_internal': self.custom_internal})
print('debug3')
return res


When odoo default function try to call the function above, my code didn't run and get ignored (the 3 debug print didnt come out at all). iam really confused now and would welcome any help, thanks!

Avatar
Verwerfen
Beste Antwort

Hi Aldi,

Can you confirm you have imported the python file to __init__.py?

Thanks

=============

UPDATE:

I would suggest adding the "sale_purchase_inter_company_rules" module to your dependencies if it is not already included, and verifying that it has been properly installed and configured.

Avatar
Verwerfen
Autor

Yes, i already imported the python file to __init__.py and my custom field (custom_internal) inserted in that python file can be showed into Odoo GUI. so i think the problem isnt at my python file doesn't scanned by odoo

Autor

and one more thing, the function is in fact called. I tried editting the function itself without using that extension and that field is called (custom_internal).

Autor

here is the change i expected in the original function:

def _prepare_purchase_order_data(self, company, company_partner):
""" Generate purchase order values, from the SO (self)
:param company_partner : the partner representing the company of the SO
:rtype company_partner : res.partner record
:param company : the company in which the PO line will be created
:rtype company : res.company record
"""
self.ensure_one()
# find location and warehouse, pick warehouse from company object
warehouse = company.warehouse_id and company.warehouse_id.company_id.id == company.id and company.warehouse_id or False
if not warehouse:
raise UserError(_('Configure correct warehouse for company(%s) from Menu: Settings/Users/Companies', company.name))
picking_type_id = self.env['stock.picking.type'].search([
('code', '=', 'incoming'), ('warehouse_id', '=', warehouse.id)
], limit=1)
if not picking_type_id:
intercompany_uid = company.intercompany_user_id.id
picking_type_id = self.env['purchase.order'].with_user(intercompany_uid)._default_picking_type()
return {
'name': self.env['ir.sequence'].sudo().next_by_code('purchase.order'),
'origin': self.name,
'partner_id': company_partner.id,
'picking_type_id': picking_type_id.id,
'date_order': self.date_order,
'company_id': company.id,
'fiscal_position_id': company_partner.property_account_position_id.id,
'payment_term_id': company_partner.property_supplier_payment_term_id.id,
'auto_generated': True,
'auto_sale_order_id': self.id,
'partner_ref': self.name,
'currency_id': self.currency_id.id,
'order_line': [],
'custom_internal': self.custom_internal,
}

Autor

SOLVED :

I forgot to add the depends of the original function, after adding the depends in the manifest, its working like a charm. Thanks