Hi,
I am developing an app for testing purpose. In this app I need to copy records saved in one model (includes One2many field aswell) to other model by clicking a button that calls the function which is programmed to the stated task. (Same as Quotation ===> Invoice in sales module)
Here is my model
ORIGIN MODEL (Source, Records will be entered via this model)
from odoo import models, fields, api
from datetime import datetime
class cms_project(models.Model):
_name = 'cms.project'
_description = 'Projects'
_order = 'name asc' # asc or desc
name = fields.Char(string="Project ID", required=True, readonly=True, copy=False, index=True, default='New')
project_short = fields.Char(string="Short Name of Project", required=True)
project_desc = fields.Text(string="Full Name of Work", required=True)
client = fields.Many2one('cms.client', 'Client') # Get Client List from cms.client model
project_ecv = fields.Monetary(compute='_compute_ecv', string="Estimated Cost of Project", readonly=True, store=True)
rebate = fields.Float(string="Percentage (+/-)", required=True)
quoted_total = fields.Monetary(string='Quoted Cost Value of the Work', store=True, readonly=True,
compute='_quoted_total')
work_order_number = fields.Char(string="Client Work Order No.")
work_order_date = fields.Date(string='Work Order Date', required=True, index=True, default=datetime.today())
completion_date = fields.Date(string='Estimated Completition Date', required=True, index=True,
default=datetime.today())
completion_time = fields.Integer(compute='_compute_completion_time', string="Completion time (in Days)",
required=True, index=True, store=False)
remaining_completition_days = fields.Integer(compute='_compute_remaining_completition_days',
string="Remaining Completion Time (in Days)")
currency_id = fields.Many2one(related='boq_line.currency_id', depends=['boq_line.currency_id'], store=True,
string='Currency')
boq_line = fields.One2many('cms.boq.lines', 'boq_id', string='BOQ Lines', copy=True, auto_join=True)
note = fields.Text(string="Notes on BOQ")
@api.model
def create(self, vals):
if vals.get('name', 'New') == 'New':
vals['name'] = self.env['ir.sequence'].next_by_code(
'project.id.seq') or 'New'
return super(cms_project, self).create(vals) # Use return command directly without any "RESULT"
_sql_constraints = [
('pr_short_uniq',
'UNIQUE (project_short)',
'Short name already exists.'),
('pr_id_uniq',
'UNIQUE (name)',
'ID already exists.')]
def _compute_completion_time(self):
for record in self:
record.completion_time = (record.completion_date - record.work_order_date).days
def _compute_remaining_completition_days(self):
for record in self:
record.remaining_completition_days = (record.completion_date - fields.Date.today()).days
@api.depends('boq_line.amount')
def _compute_ecv(self):
global order, ecv_total
for order in self:
ecv_total = 0.0
for line in order.boq_line:
ecv_total += line.amount
order.update({'project_ecv': ecv_total})
@api.depends('project_ecv', 'rebate')
def _quoted_total(self):
for order in self:
calculated = order.project_ecv + (order.project_ecv * (order.rebate / 100))
order.update({
'quoted_total': calculated,
})
@api.model
def create_mb(self,cr):
mb_val_list = []
for order in self:
# prepare MB environment
mb_vals = order._prepare_mb()
# prepare MB lines
for line in order.boq_line:
#self.env['mb.entry.line'].create(line._prepare_mb_line())
mb_vals['mb_line'].append((0, 0, line._prepare_mb_line()))
mb_val_list.append(mb_vals)
moves = self.env['mb.entry'].create(mb_val_list)
return moves
@api.model
def _prepare_mb(self):
self.ensure_one()
mb_vals = {
'mb_no': self.name,
'project_short': self.project_short,
'project_desc': self.project_desc,
'project_ecv': self.project_desc,
'client': self.client,
'rebate': self.rebate,
'quoted_total': self.quoted_total,
'work_order_number': self.work_order_number,
'work_order_date': self.work_order_date,
'completion_date': self.completion_date,
'completion_time': self.completion_time,
'remaining_completition_days': self.remaining_completition_days,
'currency_id': self.currency_id,
'mb_line': [],
}
return mb_vals
class cms_boq_lines(models.Model):
_name = 'cms.boq.lines'
_description = "BOQ Lines"
_rec_name = 'item_id'
# name = fields.Char(string="BOQ Lines")
boq_id = fields.Many2one('cms.project', string='BOQ ID', copy=True, required=True, ondelete='cascade', index=True)
item_id = fields.Char(string="SNo.", store=True)
item_short = fields.Many2one('product.product', string='Item', store=True)
item_des = fields.Text(string='Item Description', store=True)
item_qty = fields.Float(string="Estimated QTY.", required=True, digits=(30, 3), store=True)
item_rate = fields.Monetary(string="SOR Rate", required=True, store=True, )
uom = fields.Many2one('uom.uom', string='Per', store=True)
amount = fields.Monetary(compute='_amount', readonly=True, string="Amount", required=True, strore=True)
currency_id = fields.Many2one('res.currency', string='Currency')
@api.depends('item_qty', 'item_rate')
def _amount(self):
for line in self:
qty = line.item_qty
rate = line.item_rate
line.amount = qty * rate
def _prepare_mb_line(self):
self.ensure_one()
res = {
'boq_id': self.boq_id,
'mb_id':self.boq_id,
'item_id': self.item_id,
'item_short': self.item_short,
'item_qty': self.item_qty,
'item_des': self.item_des,
'item_rate': self.item_rate,
'uom': self.uom,
'amount': self.amount,
}
return res
Destination MODEL ( Records will be replicated in this model)
from odoo import models, fields, api
from datetime import datetime
class mb_entry(models.Model):
_name = 'mb.entry'
_description = "Measurement Book"
_rec_name = 'mb_no'
mb_no = fields.Char(string="MB SNo.")
execution_date = fields.Date(string='Execution Date', required=True, index=True, default=datetime.today())
entry_date = fields.Date(string='Entry Date', required=True, index=True, default=datetime.today(), readonly=True)
project = fields.Char(string='Project')
project_short = fields.Char(string="Short Name of Project", required=True)
project_desc = fields.Text(string="Full Name of Work", required=True)
client = fields.Char(string="Client")
project_ecv = fields.Char(string="Estimated Cost of Project", readonly=True, store=True)
rebate = fields.Char(string="Percentage (+/-)", required=True)
quoted_total = fields.Char(string='Quoted Cost Value of the Work', store=True, readonly=True)
work_order_number = fields.Char(string="Client Work Order No.", copy=False)
work_order_date = fields.Date(string='Work Order Date', required=True, index=True)
completion_date = fields.Date(string='Estimated Completition Date', required=True, index=True)
completion_time = fields.Integer(string="Completion time (in Days)",
required=True, index=True, store=False)
remaining_completition_days = fields.Integer(string="Remaining Completion Time (in Days)")
currency_id = fields.Many2one(related='mb_line.currency_id', depends=['mb_line.currency_id'], store=True,
string='Currency')
mb_line = fields.One2many('mb.entry.line', 'mb_id', string='Measurement Book', copy=True, auto_join=True, readonly=True)
@api.onchange('project')
def project_onchange(self):
for rec in self:
if rec.project:
rec.project_short = rec.project.project_short
rec.project_desc = rec.project.project_desc
# def default_get(self):
class mb_entry_line(models.Model):
_name = 'mb.entry.line'
_description = "Measurement Book Lines"
_rec_name = 'mb_id'
mb_id = fields.Many2one('mb.entry',string='MB Lines ID', index=True,
copy=False)
item_id = fields.Char(string="SNo.", store=True, readonly=True)
item_short = fields.Char(string='Item', store=True, readonly=True)
item_qty = fields.Float(string="Estimated QTY.", required=True, digits=(30, 3), store=True, readonly=True)
boq_id = fields.Char(string='BOQ ID')
item_des = fields.Text(string='Item Description', store=True)
item_rate = fields.Monetary(string="SOR Rate", required=True, store=True,)
uom = fields.Char(string='Per', store=True)
amount = fields.Monetary(readonly=True, string="Amount", required=True, strore=True)
currency_id = fields.Many2one('res.currency', string='Currency')
FOCUS METHOD
@api.model
def create_mb(self,cr):
mb_val_list = []
for order in self:
# prepare MB environment
mb_vals = order._prepare_mb()
# prepare MB lines
for line in order.boq_line:
#self.env['mb.entry.line'].create(line._prepare_mb_line())
mb_vals['mb_line'].append((0, 0, line._prepare_mb_line()))
mb_val_list.append(mb_vals)
moves = self.env['mb.entry'].create(mb_val_list)
return moves
@api.model
def _prepare_mb(self):
self.ensure_one()
mb_vals = {
'mb_no': self.name,
'project_short': self.project_short,
'project_desc': self.project_desc,
'project_ecv': self.project_desc,
'client': self.client,
'rebate': self.rebate,
'quoted_total': self.quoted_total,
'work_order_number': self.work_order_number,
'work_order_date': self.work_order_date,
'completion_date': self.completion_date,
'completion_time': self.completion_time,
'remaining_completition_days': self.remaining_completition_days,
'currency_id': self.currency_id,
'mb_line': [],
}
return mb_vals