auto filled field to different field in odoo - odoo

how to make field become auto fill when i create new record from bank_account.py field to account_account.py field.
bank_account.py
class BankAccounAccount(models.Model):
_name = 'bank.account.account'
_description = "Bank Account Account"
_rec_name = 'acc_number'
acc_number = fields.Char(string="Account Number", required=True)
bank_id = fields.Many2one('res.bank', string="Bank")
bank_bic = fields.Char(string="Bank Identifier Code")
company_id = fields.Many2one('res.company', string="Company", default=lambda self: self.env.user.company_id.id)
branch_id = fields.Many2one('res.branch', string="Branch")
account_account.py
class AccountJournal(models.Model):
_inherit = "account.journal"
name = fields.Char(string='Journal Name', required=True,
tracking=True)
company_partner_id = fields.Many2one('res.partner', related='company_id.partner_id', string='Account Holder', readonly=True, store=False)
bank_account_id = fields.Many2one('res.partner.bank',
string="Bank Account",
ondelete='restrict', copy=False,
check_company=True,
domain="[('partner_id','=', company_partner_id), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
tracking=True)
company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, index=True, default=lambda self: self.env.company,
help="Company related to this journal",
tracking=True)
bank_statements_source = fields.Selection(selection=_get_bank_statements_available_sources, string='Bank Feeds', default='undefined', help="Defines how the bank statements will be registered",
tracking=True)``
bank_acc_number = fields.Char(related='bank_account_id.acc_number', readonly=False,
tracking=True)
bank_id = fields.Many2one('res.bank', related='bank_account_id.bank_id', readonly=False,
tracking=True)
`
when created new account 'acc_number' record auto filled into name field and company_id to company id

Related

How do I make a field that has a hierarchy, such as the product category field of the product model?

I would like to create a field that has several categories of professions (parent elements) and at the end the profession corresponding to that category appears, as in the product category field of the product model.
How can I do this?
You can define a computed field that shows the parent categories' names and set it as the _rec_name to let Odoo use it as a display name in the Many2one fields.
Example:
class ProfessionCategory(models.Model):
_name = "profession.category"
_description = "Profession Category"
_rec_name = 'complete_name'
name = fields.Char('Name', index=True, required=True)
complete_name = fields.Char(
'Complete Name', compute='_compute_complete_name', recursive=True,
store=True)
parent_id = fields.Many2one('profession.category', 'Parent Category', index=True, ondelete='cascade')
#api.depends('name', 'parent_id.complete_name')
def _compute_complete_name(self):
for category in self:
if category.parent_id:
category.complete_name = '%s / %s' % (category.parent_id.complete_name, category.name)
else:
category.complete_name = category.name

Move Many2one variable to another Model

I have 2 models "Inventory" and "Scrap". There is a button. I want to, if i click the button the data move to scrap model. It is okay i did that. But except one variable, "Many2one".
So I'm getting this error, when i click the button: "psycopg2.ProgrammingError: can't adapt type 'ware.houses' odoo".
This is my Inventory Module.
class InventoryMenu(models.Model):
_name = "inventory.menu"
_description = "Inventory Menü"
ref_code = fields.Char(string="Referans Numarası: ", required=True, tracking=True, default="000000")
product_name = fields.Char(string="Ürün Adı: ", required=True, tracking=True,)
product_description = fields.Char(string="Ürün Tanımı: ", required=True, tracking=True,)
teslim_alan = fields.Char(string="Teslim Alan: ", required=True, tracking=True,)
teslim_eden = fields.Char(string="Teslim Eden: ", required=True, tracking=True,)
quantity = fields.Float(string="Miktar: ", required=True, tracking=True,)
price = fields.Float(string="Fiyat: ", required=True, tracking=True,)
unit_price = fields.Float(string="Birim Fiyat: ", compute="_unitPriceCalcuteFunc")
warehouse_id = fields.Many2one('ware.houses', string='Depo Adı: ')
#api.depends('quantity', 'price')
def _unitPriceCalcuteFunc(self):
for record in self:
record.unit_price = record.price * record.quantity
def action_delete(self):
vals = {
'ref_code': self.ref_code,
'product_name': self.product_name,
'product_description': self.product_description,
'teslim_alan': self.teslim_alan,
'teslim_eden': self.teslim_eden,
'quantity': self.quantity,
'price': self.price,
'unit_price': self.unit_price,
'warehouse_id': self.warehouse_id
}
self.env['scrap'].create(vals)
return super(InventoryMenu, self).unlink()
This is my Scrap module:
class Scrap(models.Model):
_name = "scrap"
_description = "Scrap"
ref_code = fields.Char(string="Referans Numarası: ", required=True, tracking=True, default="000000")
product_name = fields.Char(string="Ürün Adı: ", required=True, tracking=True, )
product_description = fields.Char(string="Ürün Tanımı: ", required=True, tracking=True, )
teslim_alan = fields.Char(string="Teslim Alan: ", required=True, tracking=True, )
teslim_eden = fields.Char(string="Teslim Eden: ", required=True, tracking=True, )
quantity = fields.Float(string="Miktar: ", required=True, tracking=True, )
price = fields.Float(string="Fiyat: ", required=True, tracking=True, )
unit_price = fields.Float(string="Birim Fiyat: ", compute="_unitPriceCalcuteFunc")
warehouse_id = fields.Many2one('ware.houses', string='Depo Adı: ')
#api.depends('quantity', 'price')
def _unitPriceCalcuteFunc(self):
for record in self:
record.unit_price = record.price * record.quantity
def action_getback(self):
vals = {
'ref_code': self.ref_code,
'product_name': self.product_name,
'product_description': self.product_description,
'teslim_alan': self.teslim_alan,
'teslim_eden': self.teslim_eden,
'quantity': self.quantity,
'price': self.price,
'unit_price': self.unit_price,
'warehouse_id': self.warehouse_id
}
self.env['inventory.menu'].create(vals)
return super(Scrap, self).unlink()
Here is the solution for above code:
'warehouse_id': self.warehouse_id
Please change this line to:
'warehouse_id': self.warehouse_id.id
While creating your vals in action_delete and action_getback methods.
Root Cause: When you pass dictionary of values which you want to create in your model, make sure that every field have correct value.
For EX: If field is Many2one than you need to pass the record_id (type=int), because odoo will directly browse the id to the related model and place the record to that field.

I am trying to fetch the product line of custom module in product line of invoice module in odoo 15

I have inherited account.move model and added job_card_id field(many2one) in it, as shown as below :
Image
Below given is Image of Selected Job Card :
Image
Below given is code of my model and I also tried creating function below fields :
class JobCard(models.Model):
_name = "job.card"
_inherit = ['mail.thread', 'mail.activity.mixin']
_description = "Job Card Master"
_rec_name = 'job_card_number'
job_card_number = fields.Char(string='Job Card No.', readonly=True)
customer_id = fields.Many2one('res.partner', string="Customer Name", tracking=True)
vehicle_id = fields.Many2one('res.partner.line', string="Vehicle", tracking=True,
domain="[('x_customer_id','=',customer_id)]")
date_time_of_invoice = fields.Datetime(string='Date & Time of Invoice', tracking=True, default=fields.Datetime.now)
start_date_time = fields.Datetime(string='Start Date & Time', tracking=True)
end_date_time = fields.Datetime(string='End Date & Time', tracking=True)
priority = fields.Selection([
('0', 'Normal'),
('1', 'Low'),
('2', 'High'),
('3', 'Very High')], string="Priority") # priority widget
state = fields.Selection([
('draft', 'Draft'),
('in_progress', 'In Progress'),
('done', 'Done'),
('cancel', 'Cancelled')], string="Status", default='draft', required=True) # status bar
active = fields.Boolean(string="Active", default=True, tracking=True)
x_product_ids = fields.Many2many('job.card.line', 'product_id', string="Job Card Details")
x_quantity_ids = fields.One2many('job.card.line', 'quantity', string="Job Card Details")
x_price_ids = fields.One2many('job.card.line', 'price', string="Job Card Details")
x_total_ids = fields.One2many('job.card.line', 'total', string="Job Card Details")
x_employee_ids = fields.One2many('job.card.line', 'employee_id', string="Job Card Details")
x_job_card_ids = fields.One2many('job.card.line', 'job_card_id', string="Job Card Details")
job_card_count = fields.Integer(compute='compute_job_card_count', string='Job Card Count')
def get_invoice_line_vals(self):
vals_list = []
for job_card_line in self.x_product_ids:
vals_list.append({
' price_unit': job_card_line.price_unit,
'quantity': job_card_line.quantity
})
return vals_list
Below given is code of inherited model and also added onchange function :
class CustomInvoice(models.Model):
_inherit = "account.move"
job_card_id = fields.Many2one('job.card', string="Job Card", domain="[('customer_id','=',partner_id)]",
tracking=True)
#api.onchange('job_card_id')
def _onchange_job_card_id(self):
# creates your invoice lines vals according to your values
invoice_lines_vals = self.job_card_id.get_invoice_line_vals()
self.update({'invoice_line_ids': [(5, 0)] + [(0, 0, vals) for vals in invoice_lines_vals]})
Below given is code of my job card line :
class JobCardLine(models.Model):
_name = "job.card.line"
job_card_id = fields.Many2one('job.card', string="Job Card Id", tracking=True)
product_id = fields.Many2one('product.template', string="Product", tracking=True)
quantity = fields.Integer(string="Quantity", tracking=True)
# price = fields.Char(string="Price")
price = fields.Float(string="Price")
total = fields.Integer(string='Total', compute='_compute_total', tracking=True,
help="This field will be calculated from quantity and price !")
employee_id = fields.Many2one('hr.employee', string="Employee", tracking=True)
x_job_card_id = fields.Many2one('res.partner', string="Vehicle Details")
#api.onchange('product_id')
def _on_change_product_id(self):
self.price = self.product_id.list_price
#api.depends('quantity', 'price')
def _compute_total(self):
print("self........", self)
for rec in self:
rec.total = rec.quantity * rec.price
Actually I wanted to add product line of selected job card into Invoice product line automatically when I select the job card.
But I am getting error as shown below :
Error
You got that error because you have no field named line_ids in job.card model. Maybe you need to change it to x_product_ids.
The TyprError (int object is not iterable) is caused by the first tuple passed to the write method :
(6, 0, 0)
Odoo expects the third parameter to be iterable. If you need to clear the list, use (5,0)
Avoid calling the write method inside an onchange function. You can read the following danger notice in the official onchange documentation:
DangerSince #onchange returns a recordset of pseudo-records, calling any one of the CRUD methods (create(), read(), write(), unlink()) on the aforementioned recordset is undefined behaviour, as they potentially do not exist in the database yet.Instead, simply set the record’s field like shown in the example above or call the update() method.
Edit:
You have two errors in get_invoice_line_vals function
1/ ValueError:
Invalid field ' price_unit' on model 'account.move.line'
You need to remove the space at the beginning.
2/ AttributeError:
'job.card.line' object has no attribute 'price_unit'.
Use the price field instead.

odoo-How to save related fields for one2many relation

my problem is that when i saved the fields in model's from B , i did't saw the result in model's view A execept the name of empoyee and department becuse declared in the same model, some freinds suggeste me to use onchnage function but how !!
class FeuilleTemps(models.Model): # A
_name = 'tbrh.feuilletemps'
_rec_name = 'name_emp'
name_emp = fields.Many2one('hr.employee', string="Employé")
name_dep = fields.Many2one('hr.department', string="Département")
abscence_ids = fields.One2many('tbrh.abscences', 'feuille_id', string="ma liste ")
relation_id = fields.Many2one('tbrh.abscences')
date2 = fields.Date(related='relation_id.date', store=True, use_parent_address=False)
statut = fields.Selection(related='relation_id.statut', store=True)
class Abscences(models.Model): # B
_name = 'tbrh.abscences'
statut = fields.Selection([('abscent', 'Abscent'), ('present', 'Présent')], string="Statut")
date = fields.Date()
feuille_id = fields.Many2one('tbrh.feuilletemps',
ondelete='cascade', string="feuille ", required=True)

POS order product total

my goal is to add a field in product form view that will show how many time this product was bought with POS. really stuck with this one and would be nice to have an idea how to do this.
class Product(models.Model):
_inherit = 'product.product'
pos_product_order_total = fields.Char(
string='Product POS Orders', compute='_product_pos_orders')
def _product_pos_orders(self):
Order = self.env['pos.order']
for product in self:
domain = [('product_id', '=', product.id)]
for o in Order.search(domain):
pass
Try below solution:
class Product(models.Model):
_inherit = 'product.product'
pos_product_order_total = fields.Char(
string='Product POS Orders', compute='_product_pos_orders')
def _product_pos_orders(self):
OrderLine = self.env['pos.order.line']
for product in self:
domain = [('product_id', '=', product.id)]
product.pos_product_order_total = sum(OrderLine.search(domain).mapped('qty'))