Two value in Many2one field odoo 9 - odoo

In Many2one field I Want view invoice name and invoice ammount_total how add this?
customer_invoice = fields.Many2one('account.invoice', 'Customer Inv', select=True)
Now after open customer_invoice field I view eg. INV/2017/0001, INV/2017/0002 I want INV/2017/0001 100€, INV/2017/0002 200€
Is it possible?

This methode change the default name, just add it in invoice class
#api.multi
def name_get(self):
result = []
for record in self:
name = record.name
if record.ammount_total :
name = record.name + ' ' + str(record.ammount_total)
result.append((record.id, name))
return result

Related

Odoo Prevent Selected Duplicate Record in One2many Field

I want to prevent the selected record to show again in the combo box.
As you can see, the 710 - Maleo show again after I selected that record before.
Field declaration for One2many field
class RMReservationOrderLine(models.Model):
_name = "rm.reservation.order.line"
_description = "Reservation Order Line"
room_line_ids = fields.One2many('rm.reservation.room.line', 'order_id', string='Rooms')
Model class for One2many field
class RMReservationRoomLine(models.Model):
_name = "rm.reservation.room.line"
_description = "Reservation Room Line"
order_id = fields.Many2one('rm.reservation.order.line', string='Order', required=True, ondelete='cascade')
room_id = fields.Many2one('rm.room', string='Room', required=True)
UPDATE
Since my model class for the One2many field just have a single field, room_id, I just change the One2many field to Many2many. Because by default Many2many field prevent duplicate record.
But I still want to know how to prevent duplicate records if I use the One2many field, In case I have more than 1 field in the model class for One2many.
I think this is the same case as you want.
I already modified the Sales Order, so when the product in the sales order line it's already selected then the product will not be shown again in the selected product.
I used odoo-14 and inherited the sales.order.line and modified the function product_id_change() to become:
#api.onchange('product_id')
def product_id_change(self):
values = super(SaleOrderLine, self).product_id_change()
filter_product_ids = [data.product_id.id for data in self.order_id.order_line]
if values is None:
values = {}
values['domain'] = {'product_id' : [('id', 'not in', filter_product_ids)]}
return values

How to get current record ID?

I have a compute field and function :
When I click on res.partner list view or kanban view, the current record id's data should get calculated and display in smart button, am facing problem in getting the current ID.
account_info = fields.Integer(compute='_credit_debit_info', string='# Credits and Debits')
#api.multi
def _credit_debit_info(self):
print "...Self...",self.ids
print "...context..",self.env.context.get('active_id')
print "...context..", self.env.context
for partner in self:
if partner.with_context(active_id=True):
PartnerInfo = self.env['account.move.line'].with_context(active_test=False).search([('partner_id', '=', partner.id),
('account_id', 'in', (partner.property_account_receivable_id.id, partner.property_account_payable_id.id))])
for acco in PartnerInfo:
cre = sum(acco.mapped('credit'))
debit = sum(acco.mapped('debit'))
partner.account_info = cre - debit
You could use self.id or self._ids or self._context.get('active_id').

how get id with onchange for filtering

how can i retrieve the value of a many2one field or its ID from another model
for exemple:
class Contrat(models.Model):
_name = 'facturation.contrat'
contrat_parent_id = fields.Many2one('facturation.contrat', string='Numéro Contrat Client',
domain=[('is_prestataire', '=', False)])
class Lot(models.Model):
contrat_id = fields.Many2one('facturation.contrat', ondelete='cascade')
articlecontrat_ids = fields.Many2many('facturation.articleouvrage',string='Article Lot')
91/5000
i want that when i change contrat_parent_id i get it back to use it and filter my articles for field 'articlecontrat_ids'
here you need to use onchange event i'm assuming that facturation.articleouvrage have a m2o field named contrat_id
# in onchange event always put the name of the field that trigger the event
#api.onchange('contrat_parent_id ')
def onchange_contrat(self):
"""update the domain when we change the contrat"""
if self.contrat_parent_id :
# always check if the field is not empty
# return the domain like this but i don't know what you need exactly
return {'domain': {'articlecontrat_ids ' : [('contrat_id ', '=', self.contrat_parent_id.contract.id)]}}
else: # remove the domain
return {'domain': {'articlecontrat_ids ' : []}}
if you want to remove all records when user change the contrat_id but i think you make the user ungry
to reselect all this records.
self.articlecontrat_ids = [(5, 0, 0)]

_sql_constraints not working in Odoo v10

I have a model defined as-
class ModelName(models.Model):
_name = 'model_name'
student_id = fields.Many2one('op.student', 'Student')
I want that student should be unique. So i added -
_sql_constraints = [
('student_unique',
'UNIQUE(student_id)', 'Some message!')
]
but it's not doing anything. I can still select same student and save the form.
I want when i click create button, students for which record has already been saved should not be shown.
What can i do to achieve this??
Try this below domain function in your field, I'm sure you will get your required output.
student_id = fields.Many2one('op.student',domain=lambda self: self.get_not_existing_student_id(), string='Student')
#api.model
def get_not_existing_student_id(self):
self.env.cr.execute("select id from op_student where id not in (select student_id from model_name)")
datas = self.env.cr.fetchall()
student_ids = []
for record in datas:
student_ids.append(record[0])
return [('id','in',student_ids)]
Other Way:
student_id = fields.Many2one('op.student',string='Student')
put it in a view like:
<field name="student_id" context="{'find_existed':1}" options="{'no_create':1}"/>
Then inherit the method name_get() in op.student model like this.
class op_student(models.Model):
_inherit ="op.student"
#api.multi
def name_get(self):
if self._context.get("find_existed",False):
self.env.cr.execute("select id from op_student where id not in (select student_id from model_name)")
datas = self.env.cr.fetchall()
student_ids = []
for record in datas:
student_ids.append(record[0])
for student in self:
if student in student_ids:
res.append((student.id,student.name))
else:
res=super(op_student,self).name_get()
return res

I Insert values in stock.picking move_lines but after click on save it Error The Following fields are in valid stock.move in odoo10

This button
<span class="o_stat_value"><field name="test_field"/>Material Out</span>
class SkylineJobOrder(models.Model):
_name = 'skyline.job.order'
_inherit = 'purchase.order'
#api.multi
def action_workorder_out(self):
x = 0
""" This opens the xml view specified in xml_id for the current Work Order in Manufacturing """
self.ensure_one()
xml_id = self.env.context.get('xml_id')
if xml_id:
res = self.env['ir.actions.act_window'].for_xml_id('stock', xml_id)
production = self.env['mrp.production'].search([('product_id','=',self.product_id.id)])
for item in production:
x = item.bom_id.id
bomline = self.env['mrp.bom.line'].search([('bom_id','=',x)])
for record in bomline:
res.update(
context={'default_states':'draft','default_partner_id': self.partner_id.id,'default_move_lines': [(0,0, {'address_in_id':self.partner_id.id,'product_id':record.product_id.id,'product_uom': record.product_uom_id.id,'product_uom_qty':record.product_qty,'scrapped':False,'state':'draft','picking_id':False,'name': 'test','procurement_id': record.operation_id.id,'split_from': self.id,'no_open': True,'no_create':True,'availability':1,'location_dest_id':8,'picking_id':35})]}
)
print res
return res
return False
I inherit purchase.order and create a button and put action of
stock.picking . The working of button is to stockout the values of raw
material of product .so when click on save button is shows that the
following fields are invalid stock.move.
Blockquote
value inserted form bom.line.ids to stock.move through stock.picking using picking_id, location_id,lication_dest_id and 'purchase=ok, in purchase_id.