Update fields with different number of values - odoo

In res.partner model I have a field
property_product_pricelist = fields.Many2many('product.pricelist')
and in sale.order
alternative_pricelist_ids = fields.Many2many(
'product.pricelist')
Partner can have more than one price list so my goal is to add the first pricelist to the pricelist_id field and other pricelists to alternative_pricelist_ids. The things are that how I wrote code is not really good, as you can see I will get an error if there are more than 4 pricelists. So how can I avoid it and write it another way?
#api.multi
#api.onchange('partner_id')
def onchange_partner_id(self):
super(SaleOrder,self).onchange_partner_id()
values = { 'pricelist_id': self.partner_id.property_product_pricelist[0] and self.partner_id.property_product_pricelist.id[0] or False,
'alternative_pricelist_ids': self.partner_id.property_product_pricelist[1] and self.partner_id.property_product_pricelist[2] and self.partner_id.property_product_pricelist[3] or False,
}
self.update(values)

Try this :
#api.multi
#api.onchange('partner_id')
def onchange_partner_id(self):
super(SaleOrder, self).onchange_partner_()
for record in self:
pricelist_id = False
alternative_ids = []
for pricelist in record.partner_id.property_product_pricelist:
if not pricelist_id:
pricelist_id = pricelist.id
else:
alternative_ids.append(pricelist.id)
record.pricelist_id = pricelist_id
record.alternative_pricelist_ids = [(6, 0, alternative_ids)]

Related

How to use name_get condition in odoo 12

class ResPartnerInherit(models.Model):
_inherit = 'res.partner'
is_specialist = fields.Boolean(string='Is Specialist')
specialized_in = fields.Many2one('specialization',string='Specialization')
hospital = fields.Char(string='Hospital')
#api.depends('is_specialist')
#api.multi
def name_get(self):
res = []
self.browse(self.ids).read(['name', 'hospital'])
for rec in self:
res.append((rec.id, '%s - %s' % (rec.name, rec.hospital)))
return res
What I'm trying to do here is, using name_get function When selecting a specialist his hospital needs to be shown, so I wanna give condition only for a specialist,there is a boolean field named is_specialist.so I wanna get the condition only when boolean is true
You just need to check is the partner is a specialist when building his name and if yes show also the hospital.
#api.multi
def name_get(self):
res = []
for rec in self:
res.append((rec.id, '%s - %s' % (rec.name, rec.hospital) if rec.is_specialist else rec.name))
return res

Group button in res.partner not working in odoo 10

I want to add a group selection button and is_group boolean field to inherited res.partner model but when added the group button the function compute_company_type not working
My code is
class Partner(models.Model):
_inherit = 'res.partner'
company_type = fields.Selection(selection_add=[('group', 'Group')])
refered = fields.Many2one('res.partner',string="Refered By")
import1 = fields.Float(string="Import")
temp_import = fields.Float(string="Temporary Import")
export = fields.Float(string="Export")
temp_export = fields.Float(string="Temporary Export")
transit = fields.Float(string="Transit")
group_id = fields.Many2one('res.group', string='Related Group', index=True)
is_group = fields.Boolean(string='Is a group', default=False)
is_company = fields.Boolean(string='Is a company', default=False)
#api.depends('is_company', 'is_group')
def _compute_company_type(self):
for partner in self:
if partner.is_group:
partner.company_type = 'group'
elif partner.is_company:
partner.company_type = 'company'
else:
partner.company_type = 'person'
def _write_company_type(self):
for partner1 in self:
if partner1.company_type == 'group':
partner1.is_group = partner1.company_type
elif partner1.company_type == 'company':
partner1.is_company = partner1.company_type
else:
partner1.company_type = 'person'
#api.onchange('company_type')
def onchange_company_type(self):
for partner in self:
if partner.company_type == 'group':
partner.is_group = (partner.company_type == 'group')
elif partner.company_type == 'company':
partner.is_company = (partner.company_type == 'company')
else:
partner.company_type = 'person'
I need one more company type button ie group if group button is TRUE need to change fields accordingly in res.partner form
If you want to rewrite _compute_company_type function from res.partner you must inherit your custom model from res.partner, something like this:
from odoo.addons.base.res import res_partner as res_partner
class CRMLeadPropertyMulti(res_partner.Partner):
_inherit = 'res.partner'
## Your Code ##
#api.depends('is_company', 'is_group')
def _compute_company_type(self):
for partner in self:
partner.company_type = 'company' if partner.is_company else 'group' if partner.is_group else 'person'
#api.onchange('company_type')
def onchange_company_type(self):
self.is_company = (self.company_type == 'company')
self.is_group = (self.company_type == 'group')
I think it could be a good idea to make false or true each case evaluating as bool if is a group or a company, that way if one is true the other is false, this can guard the consistency.
Your field is_group is a boolean. I don't know what you meant by this if partner.is_group:.
Boolean field is have only two values and they are true or false. Without checking true or false you won't get any result.
Try with the true or false and let me know.
Thanks and regards in advance.

Raise UserError only if none fields selected

I added my custom field to res.partner model and when I'm creating partner if branch_id is checked I want to that one of 3 fields should be selected. If some of the fields are not selected then I want to raise UserError.
But now it raises UserError even if I one of the fields is selected.
class ResPartner(models.Model):
_inherit = 'res.partner'
branch_id = fields.Many2one('branch.responsibility','Responsibility by branch')
drawer_id = fields.Many2one(
'furniture.parts.type', string='Drawer Type',
domain=[('type', '=', 'drawer')], )
flexible_id = fields.Many2one(
'furniture.parts.type', string='Flexible Type',
domain=[('type', '=', 'flexible')],)
runner_id = fields.Many2one(
'furniture.parts.type', string='Runner Type',
domain=[('type', '=', 'runner')], )
#api.model
def create(self, vals):
if vals.get('branch_id'):
if not vals['drawer_id'] or not vals['flexible_id'] or not vals['runner_id']:
raise UserError('You need to chose values from notebook raport data')
return super(ResPartner, self).create(vals)
UPDATE.
for write method, I tried this as CZoellner suggested for create but always get True True True for fields_to_check_in_vals
#api.multi
def write(self, vals):
if vals.get('branch_id') or vals.get('drawer_id') or vals.get('flexible_id') or vals.get('runner_id') or
vals.get('group_1_id') or vals.get('group_2_id')or
vals.get('group_3_id'):
fields_to_check = ['drawer_id', 'flexible_id', 'runner_id', 'group_1_id', 'group_2_id', 'group_3_id']
fields_to_check_in_vals = [f in self for f in fields_to_check]
if not any(fields_to_check_in_vals):
raise UserError('If branch is selected then you need to select one of the fields from data raport')
> return super(ResPartner, self).write(vals)
Your logic says, that all 3 fields have to be set. You could use any here:
if vals.get('branch_id'):
fields_to_check = ['drawer_id', 'flexible_id', 'runner_id']
fields_to_check_in_vals = [f in vals for f in fields_to_check]
if not any(fields_to_check_in_vals):
raise UserError()
Edit: the write method is a bit more tricky. Usually it is a multi record method, so a for each loop on self should be implemented. Then you'll need to use either getattr or just (it's suggested in the Odoo doc) treat the recordset like a dict. And then it could be, that the change happens right on the write call. So you have to check both the persisted and the new values:
for record in self:
if vals.get('branch_id'):
fields_to_check = ['drawer_id', 'flexible_id', 'runner_id']
fields_to_check_dict = {f:record[f] for f in fields_to_check}
fields_to_check_dict.update({f:f in vals for f in fields_to_check})
if not any(fields_to_check_dict.values()):
raise UserError()
That's a bit complicated. You could also first call super and just check after that. Raising an exception will rollback all changes, so nothing will happen.
res = super(MyModel, self).write(vals)
for record in self:
if vals.get('branch_id'):
fields_to_check = ['drawer_id', 'flexible_id', 'runner_id']
fields_to_check_in_record = [record[f] for f in fields_to_check]
if not any(fields_to_check_in_record):
raise UserError()
return res

what should I add to display the value of fields correctly?

what should I add to display user_id and cat correctly
#api.model
def create(self, vals):
record=super(test, self).create(vals)
if vals['total'] > 0:
vals['date'] = fields.Datetime.now()
self.env['journal'].create({
'user_id': record.patient_id,
'cat': record.cat,})
....
.....
on the tree view (journal):
user_id is displayed as test.user(6,)
cat is displayed as cat1
EDITS:
class test(models.Model):
_name = 'test'
cat = fields.Selection(
required=True,
related='test_type_cat.name',
store=True,
)
user_id = fields.Many2one('res.users', string='user', readonly=True,)
.....
#api.model
def create(self, vals):
record=super(test, self).create(vals)
if vals['total'] > 0:
vals['date'] = fields.Datetime.now()
self.env['journal'].create({
'patient_id': record.patient_id.name,
'cat': record.cat,
'user_id': record.user_id.name,
})
record.total = 0
return record
why does it work with .name and not .id ?
for m2o field should I pass the integer value ? if it is the case why does it work here with .name ? and what about m2m and o2m?
this worked for you because you are creating a record in model: journal not in test model.
and if you go to journal model you will find that patient_id is Char field not a many2one field.
so if you pass: record.patient_id you are passing an object and it's converted to char this is why you get test(1,). because pateint_id is a many2one field in test model witch mean is an object.
Hope this clear thing little bit for you.

TypeError: generate_purchase_order() takes exactly 1 argument (5 given)

I am trying to convert openerp 7 code to odoo8. In V7 browse() method had several parameters like self,cr,uid,ids,context but in V8 I think none of these is needed.In my custom module I am trying to create a purchase order based on the information obtained from mrp.I have done a custom calculation for how many Kgs of paper needed for 10000 quantities of books to be manufactured.After calculation this info should go to purchase invoice.Products will be obtained from bill of materials,quantity from no of kgs of paper needed and unit price from product cost attribute.I am unable to solve this error "method takes exactly 1 argument (5 given)"
mrp.py,
from openerp import models
class mrp_production(models.Model):
_inherit = 'mrp.production'
def generate_purchase_order(self,supplier_id,warehouse_id):
purchase_obj = self.env['purchase.order']
purchase_line_obj = self.env['purchase.order.line']
warehouse_obj = self.env['stock.warehouse']
warehouse = warehouse_obj.browse(warehouse_id)
if not warehouse:
return False
if isinstance(warehouse, list):
warehouse = warehouse[0]
for order in self:
vals = {}
vals = purchase_obj.onchange_partner_id()
vals['origin'] = order.name
vals['partner_id'] = supplier_id
vals['warehouse_id'] = warehouse_id
vals['location_id'] = warehouse.lot_stock_id.id
vals['date_order'] = order.date_planned
purchase_id = purchase_obj.create(vals)
for line in self.bom_id.bom_line_ids:
if not line.product_id:
continue
line_vals = purchase_line_obj.onchange_product_id(line.product_id.id,
line.product_uom_qty, line.product_uom.id, supplier_id,
date_planned=line.date_planned)['value']
line_vals['name'] = line.name
line_vals['product_id'] = line.product_id.id
if not line_vals.get('price_unit', False):
line_vals['price_unit'] = line.product_id.list_price
line_vals['product_uom'] = line.product_uom.id
line_vals['product_uom_qty'] = 181.13
line_vals['order_id'] = purchase_id
purchase_line_obj.create(line_vals)
return True
generate_purchase_order.py,
from openerp import models,fields,api
class generate_purchase_order(models.Model):
_name = 'mrp_to_purchase_order.generate_purchase_order'
_description = 'Generate Purchase Order'
partner_id = fields.Many2one('res.partner', 'Supplier', required=True, domain="[('supplier','=',True)]")
warehouse_id = fields.Many2one('stock.warehouse', 'Warehouse', required=True)
#api.multi
def onchange_partner_id(self,partner_id):
return {}
def generate_purchase_order(self):
for wizard in self:
#mrp_order_ids = [context['active_id']]
mrp_obj = self.env['mrp.production']
mrp_obj.generate_purchase_order(wizard.partner_id.id, wizard.warehouse_id.id)
return { 'type': 'ir.actions.act_window_close'}
If you are calling the generate_purchase_order method from some other method in your model, then use the decorator #api.multi for that method.
Also in your generate_purchase_order method, replace
for order in self.browse():
by
for order in self:
EDIT
#api.multi
def generate_purchase_order(self):
for wizard in self:
#mrp_order_ids = [context['active_id']]
mrp_obj = self.env['mrp.production']
# Get the mrp record
mrp_rec = code to get mrp record
mrp_rec.generate_purchase_order(wizard.partner_id.id, wizard.warehouse_id.id)
return { 'type': 'ir.actions.act_window_close'}