Linking The id of the Automaticlly Genereated Records in Odoo - odoo

I have created custom module that automatically create a journal items when user click on a button. In this custom module I have many2one field called x_move_id
x_move_id = fields.Many2one('account.move', string="Journal", readonly=True)
that should show the reference of the created journal items automatically, similar to account.invoice module when user validate a bill, journal items get created and its id appear.
code:
class BillOfEntry(models.Model):
_name = 'entry.bill'
_description = 'Bill of Entry'
name = fields.Char()
state = fields.Selection([
('draft', 'Draft'),
('sent', 'Validate'),
('done', 'Paid'),
('cancel', 'Cancelled'),
], string='BOE Status', readonly=True, copy=False, store=True, default='draft')
date = fields.Date(string="Date")
custom_agent = fields.Many2one('res.partner', string="Custom Agent")
reference = fields.Char(string="Reference")
total_customs = fields.Float(string='Total Customs', store=True, readonly=True, track_visibility='always', digits=(14, 3))
total_tax = fields.Float(string='Total Tax', store=True, readonly=True, track_visibility='always', digits=(14, 3))
inelig_tax = fields.Float(string="Ineligible Tax", store=True, readonly=True, track_visibility='always', digits=(14, 3))
total_amount = fields.Float(string='Total Amount', store=True, readonly=True, track_visibility='always', digits=(14, 3))
entry_line = fields.One2many('entry.bill.line', 'entry_ref', string="Bill of Entry Line")
input_vat = fields.Many2one('account.account', string="Input VAT")
output_vat = fields.Many2one('account.account', string="Output VAT")
customs_account = fields.Many2one('account.account', string="Customs Account")
x_move_id = fields.Many2one('account.move', string="Journal", readonly=True)
def entry_move_line(self):
data_line = []
line = {}
for record in self:
for move in record.entry_line:
tax = move.tax_id.id
data = (0,0, {
'account_id': record.input_vat.id,
'partner_id': record.custom_agent.id,
'name': move.product_ids.name,
'debit': 0,
'credit': 0,
'x_vat_code': move.vat_code_id.id,
'tax_ids': [(6, 0, [tax])],
})
data_line.append(data)
line = {
'name': record.name,
'date': record.date,
'ref': record.reference,
'line_ids': data_line,
'journal_id': 3,
'state': 'posted'
}
record.move_id.create(line)
record.update({
'state': 'sent'
})
class BillOfEntry(models.Model):
_name = 'entry.bill.line'
_description = 'Bill of Entry Line'
assessable_amount = fields.Float('Assessable Amount', digits=(14, 3))
customs_amount = fields.Float('Customs + Additional Cost', digits=(14, 3))
tax_amount = fields.Float('Tax Amount', digits=(14, 3))
taxable_amount = fields.Float('Taxable Amount', digits=(14, 3))
elig_perc = fields.Float(string="ITC Eligibility %", help="Input Tax Credit Eligibility", digits=(14, 3))
vat_code_id = fields.Many2one('vat.configuration', string="VAT Code")
tax_id = fields.Many2many('account.tax', string='Taxes', domain=['|', ('active', '=', False), ('active', '=', True)])
product_ids = fields.Many2one('product.product', string="product")
inelegible = fields.Float(string="Ineleigible", digits=(14, 3))
entry_ref = fields.Many2one('entry.bill', string='Bill of Entry')
So my question how to get the (id) of the created journal items in custom module?

You can write as following :
def entry_move_line(self):
data_line = []
line = {}
for record in self:
for move in record.entry_line:
tax = move.tax_id.id
data = (0,0, {
'account_id': record.input_vat.id,
'partner_id': record.custom_agent.id,
'name': move.product_ids.name,
'debit': 0,
'credit': 0,
'x_vat_code': move.vat_code_id.id,
'tax_ids': [(6, 0, [tax])],
})
data_line.append(data)
line = {
'name': record.name,
'date': record.date,
'ref': record.reference,
'line_ids': data_line,
'journal_id': 3,
'state': 'posted'
}
account_move = self.env['account.move'].create(line)
record.write({'x_move_id':account_move.id})
record.update({
'state': 'sent'
})

Related

I am working on the hr leave odoo 12, xlsx report by department , I have overwrite problem when print xlsx report for more than one department

I am working on the hr leave odoo 12, xlsx report by department , I have overwrite problem when print xlsx report for more than one department.
here is my code:
############################### for department #########################################
def get_all_date(self, data, empid):
domain = [('state', '=', 'validate'), ('employee_id', '=', empid)]
if data.get('date_from'):
domain.append(('date_from', '>=', data.get('date_from')))
if data.get('date_to'):
domain.append(('date_from', '<=', data.get('date_to')))
print(domain)
leave = self.env['hr.leave'].search(domain)
return leave
def generate_xlsx_report(self, workbook, data, record):
##################### for department ########
res = []
Employee = self.env['hr.employee']
if 'depts' in data:
for department in self.env['hr.department'].browse(data['depts']):
res.append(
{
'dept': department.name,
'data': []
}
)
for emp in Employee.search([('department_id', '=', department.id)]):
res[len(res) - 1]['data'].append(
{
# 'emp': emp.name,
'display': self.get_all_date(data['data'], emp.id)
}
)
sheet = workbook.add_worksheet('Leaves Report')
bold = workbook.add_format({'bold': True, 'align': 'center', 'bg_color': '#fffbed', 'border': True})
format = workbook.add_format({'num_format': 'd-m-yyyy'})
header_row_style = workbook.add_format({'bold': True, 'align': 'center', 'border': True})
format2 = workbook.add_format({'font_size': 10, 'bold': True, 'align': 'center', })
title = workbook.add_format(
{'bold': True, 'align': 'center', 'font_size': 20, 'bg_color': '#f2eee4', 'border': True})
sheet.merge_range('A1:E1', 'Leaves Summary Report', title)
# Header row
# Header row
sheet.set_column(0, 4, 18)
sheet.write(2, 0, 'Department', header_row_style)
sheet.write(3, 1, 'Employee', header_row_style)
sheet.write(3, 2, ' Start date', header_row_style)
sheet.write(3, 3, 'End date', header_row_style)
sheet.write(3, 4, 'Leave Type', header_row_style)
######################### for department #############################
for rows, i in enumerate(res):
print(i)
col=0
sheet.write(rows + 4, col, i['dept'], format2)
#rows+1
for j in i['data']:
print(j)
for rows ,k in enumerate(j['display']):
print(k)
# sheet.write(ro + 3, col, k.department_id.name, format2)
sheet.write(rows + 4, col + 1, k.employee_id.name, format2)
sheet.write(rows + 4, col + 2, k.date_from, format)
sheet.write(rows+ 4, col + 3, k.date_to, format)
sheet.write(rows + 4, col + 4, k.holiday_status_id.name, format2)
rows+ 1

How to generate an invoice from a custom module in Odoo13?

I am developing a custom module.
I tried to add it through an object button with the following code but doesn't seem to work
def create_invoice(self):
rslt = self.env['account.invoice'].create({
'partner_id': self.instructor.id,
'name': 'customer invoice',
'type': 'out_invoice',
'date_invoice': 'create_date'
})
return rslt
How can I add a button that generates an invoice?
desu
From Odoo13 there is a change in invoice object, It is now account.move instead of account.invoice.You can take this reference demo example.
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'journal_id': journal.id,
'partner_id': product_id.id,
'invoice_date': date_invoice,
'date': date_invoice,
'invoice_line_ids': [(0, 0, {
'product_id': product_id.id,
'quantity': 40.0,
'name': 'product test 1',
'discount': 10.00,
'price_unit': 2.27,
})]
})

display one2many values of a model in a wizard

I have a class salesin which has a one2many field emidetails . Here i defined abutton that directs to a wizard saleswizard . What i need to acheive is when i click the button , the wizard that opens should contain the emidetails one2many field of salesin class .. How to
'''
class Salesin(models.Model):
_inherit = 'sale.order'
amount = fields.Integer(string="Total Amount")
product = fields.Char(string="Product")
paymenttype = fields.Selection([('Full Payment', 'Full Payment'), ('EMI', 'EMI'), ],
string=" Payment Type : ", default='Full Payment')
emidetails = fields.One2many(comodel_name="emidetails",inverse_name="saleorder",string="Emi Details",onchange="restrict")
#api.multi
def cnfirm(self):
result = {
'view_type': 'form',
'view_mode': 'form',
'res_model': 'confirmbox',
'name': 'Confirm the Quantities Currently Have',
'type': 'ir.actions.act_window',
'target': 'new',
}
return result
class emidetails(models.Model):
_name = "emidetails"
saleorder = fields.Many2one( comodel_name="sale.order' ,string="SO")
installment = fields.Selection([('Level 1', '1st Installment'), ('Level 2', '2nd
Installment'), ('Level 3', '3rd Installment'), ],
string=" Installment : ",)
amount = fields.Char(string="Amount:",onchange="calculate_med")
date = fields.Date(string="Due Date")
cust = fields.Many2one(comodel_name="sale.order")
status = fields.Selection([('Pending', 'Pending'), ('Paid', 'Paid'), ],
string=" Status : ",)
class saleswizard(models.TransientMOdel) :
_name = saleswiz
emidetails = fields.One2many
(comodel_name="emidetails",inverse_name="saleorder",string="Emi
Details",onchange="restrict")
Use a Many2many field:
emidetails = fields.Many2many("emidetails", string="EmiDetails")
And passe default values in context:
#api.multi
def cnfirm(self):
self.ensure_one()
emidetails = [(4, emit.id, 0) for emit in self.emidetails]
result = {
'view_type': 'form',
'view_mode': 'form',
'res_model': 'saleswiz',
'name': 'Confirm the Quantities Currently Have',
'type': 'ir.actions.act_window',
'target': 'new',
'context': {'default_emidetails': emidetails}
}
return result

How to write duplicate error in action_confirm button in openerp

def action_confirm(self, cr, uid, ids, context=None):
""" Confirms procurement and writes exception message if any.
#return: True
"""
move_obj = self.pool.get('stock.move')
for procurement in self.browse(cr, uid, ids, context=context):
data=procurement.product_id.id
pro_list.append(data)
if procurement.product_qty <= 0.00:
raise osv.except_osv(_('Data Insufficient!'),
_('Please check the quantity in procurement order(s) for the '
'product "%s", it should not be 0 or less!' %
procurement.product_id.name))
if procurement.product_id.type in ('product', 'consu'):
if not procurement.move_id:
source = procurement.location_id.id
if procurement.procure_method == 'make_to_order':
source = procurement.product_id.property_stock_procurement.id
id = move_obj.create(cr, uid, {
'name': procurement.name,
'location_id': source,
'location_dest_id': procurement.location_id.id,
'product_id': procurement.product_id.id,
'product_qty': procurement.product_qty,
'product_uom': procurement.product_uom.id,
'date_expected': procurement.date_planned,
'state': 'draft',
'company_id': procurement.company_id.id,
'auto_validate': True,
})
move_obj.action_confirm(cr, uid, [id], context=context)
self.write(cr, uid, [procurement.id], {'move_id': id, 'close_move': 1})
self.write(cr, uid, ids, {'state': 'confirmed', 'message': ''})
return True
def action_confirm(self, cr, uid, ids, context=None):
move_obj = self.pool.get('stock.move')
for procurement in self.browse(cr, uid, ids, context=context):
cr.execute("select id,order_id,foc from sale_order_line where product_id = %s and order_id= %s and foc = %s", (procurement.product_id.id, procurement.move_id.sale_line_id.order_id.id, procurement.foc,))
data = cr.fetchall()
count = len(data)
if count >= 2:
raise osv.except_osv(_('Data Duplicate!'),
_('Please check the sale order line.Duplicate record can not allow.'))
else:
{}
if procurement.product_qty <= 0.00:
raise osv.except_osv(_('Data Insufficient!'),
_('Please check the quantity in procurement order(s) for the product "%s", it should not be 0 or less!' % procurement.product_id.name))
if procurement.product_id.type in ('product', 'consu'):
if not procurement.move_id:
source = procurement.location_id.id
if procurement.procure_method == 'make_to_order':
source = procurement.product_id.property_stock_procurement.id
id = move_obj.create(cr, uid, {
'name': procurement.name,
'location_id': source,
'location_dest_id': procurement.location_id.id,
'product_id': procurement.product_id.id,
'product_qty': procurement.product_qty,
'foc':procurement.foc,
'product_uom': procurement.product_uom.id,
'date_expected': procurement.date_planned,
'state': 'draft',
'company_id': procurement.company_id.id,
'auto_validate': True,
})
move_obj.action_confirm(cr, uid, [id], context=context)
self.write(cr, uid, [procurement.id], {'move_id': id, 'close_move': 1})
self.write(cr, uid, ids, {'state': 'confirmed', 'message': ''})
return True

'deposit' object has no attribute 'get_move_line'

I hope friends can help me.
I have code that I created, the goal is to enter a value into journal entries. That's my code.
class deposit(osv.osv):
_name='deposit.travel'
_description='Deposit2 Travel'
def action_process(self, cr, uid, ids, context=None):
move_lines = []
for deposit in self.browse(cr, uid, ids, context=context):
# Create the move lines first
move_lines.append((0,0, self.get_move_line(cr, uid, deposit, 'src')))
#print deposit
move_lines.append((0, 0, self.get_move_line(cr, uid, deposit, 'dest')))
# Create the move for the deposit
move = {
'journal_id': deposit.journal_id.id,
'date': deposit.date,
'deposit_id': deposit.id,
'name':'/',
'line_id': move_lines
}
move_id = self.pool.get('account.move').create(cr, uid, move, context=context)
self.pool.get('account.move').post(cr, uid, [move_id])
self.write(cr, uid, [deposit.id], {'move_id': move_id,
'state': 'done'},context=context)
return True
def get_move_line(self, cr, uid, deposit, type, context=None):
return {
'type': type,
'name': deposit.name or '/',
'debit': type == 'dest' and deposit.amount or 0.0,
'credit': type == 'src' and deposit.amount or 0.0,
'account_id': type == 'src' and deposit.dept_from.id or deposit.dept_to.id,
'date': deposit.date,
'deposit_id': deposit.id
}
_columns = {
'partner':fields.many2one('res.partner','Partner',required=True),
'date':fields.date('Date of Deposit',required=True),
'period_id':fields.many2one('account.period','Force Period', required=True),
'journal_id':fields.many2one('account.journal','Journal', required=True),
'amount':fields.float('Amount',required=True,digits_compute=dp.get_precision('Account')),
'move_id':fields.many2one('account.move','Journal Entry', readonly=True, select=1),
'name':fields.related('move_id','name',type='char',readonly=True, size=64, relation='account.move',string='Deposit Ticket #'),
'dept_from':fields.many2one('account.account','Deposit From'),
'dept_to':fields.many2one('account.account','Deposit To'),
'state': fields.selection([
('draft','Draft'),
('to_be_reviewed','Ready for Review'),
('done','Done'),
('cancel', 'Cancel')
],'State', select=True, readonly=True),
}
_defaults = {
'date': time.strftime('%Y-%m-%d'),
'period_id':_get_period,
'state': 'draft',
}
deposit()
but when i click the button to which I put the command action_process, appear error like this 'deposit' object has no attribute 'get_move_line'. get_move_line is a function, why error the attribute ?
I need your help. Thank's before
The indentation is incorrect for function get_move_line. De-indent the function one tab.
it Should go like this _default should also be at some indentation level:
class deposit(osv.osv):
_name='deposit.travel'
_description='Deposit2 Travel'
def action_process(self, cr, uid, ids, context=None):
move_lines = []
for deposit in self.browse(cr, uid, ids, context=context):
# Create the move lines first
move_lines.append((0,0, self.get_move_line(cr, uid, deposit, 'src')))
#print deposit
move_lines.append((0, 0, self.get_move_line(cr, uid, deposit, 'dest')))
# Create the move for the deposit
move = {
'journal_id': deposit.journal_id.id,
'date': deposit.date,
'deposit_id': deposit.id,
'name':'/',
'line_id': move_lines
}
move_id = self.pool.get('account.move').create(cr, uid, move, context=context)
self.pool.get('account.move').post(cr, uid, [move_id])
self.write(cr, uid, [deposit.id], {'move_id': move_id,
'state': 'done'},context=context)
return True
def get_move_line(self, cr, uid, deposit, type, context=None):
return {
'type': type,
'name': deposit.name or '/',
'debit': type == 'dest' and deposit.amount or 0.0,
'credit': type == 'src' and deposit.amount or 0.0,
'account_id': type == 'src' and deposit.dept_from.id or deposit.dept_to.id,
'date': deposit.date,
'deposit_id': deposit.id,}
_columns = {
'partner':fields.many2one('res.partner','Partner',required=True),
'date':fields.date('Date of Deposit',required=True),
'period_id':fields.many2one('account.period','Force Period', required=True),
'journal_id':fields.many2one('account.journal','Journal', required=True),
'amount':fields.float('Amount',required=True,digits_compute=dp.get_precision('Account')),
'move_id':fields.many2one('account.move','Journal Entry', readonly=True, select=1),
'name':fields.related('move_id','name',type='char',readonly=True, size=64, relation='account.move',string='Deposit Ticket #'),
'dept_from':fields.many2one('account.account','Deposit From'),
'dept_to':fields.many2one('account.account','Deposit To'),
'state': fields.selection([
('draft','Draft'),
('to_be_reviewed','Ready for Review'),
('done','Done'),
('cancel', 'Cancel')
],'State', select=True, readonly=True),
}
_defaults = {
'date': time.strftime('%Y-%m-%d'),
'period_id':_get_period,
'state': 'draft',
}
deposit()