Please check
from odoo import models
from odoo.fields import Date
from datetime import date, datetime
import dateutil.relativedelta
fs = Date.from_string
ts = Date.to_string
class CalibExcelReport(models.AbstractModel):
_name = "report.calibration_tracking.calib_xlsx_document"
_inherit = "report.report_xlsx.abstract"
_description = "Calibration XLSX Report"
def generate_xlsx_report(self, workbook, data, vals):
cell_text_format = workbook.add_format({'align': 'center', 'bold': True, 'font_size': 12, 'bg_color': '#FF9933'})
head = workbook.add_format({'align': 'center', 'bold': True,'size': 14, 'bg_color': '#6B8E23'})
head1 = workbook.add_format({'align': 'left', 'bold': True, 'size': 12, 'bg_color': '#D3D3D3'})
head2 = workbook.add_format({'align': 'left', 'size': 12})
head3 = workbook.add_format({'align': 'left', 'bold': True, 'size': 12})
format1 = workbook.add_format({'align': 'center', 'font_size': 10})
format2 = workbook.add_format({'align': 'center', 'bold': True, 'font_size': 10, 'border': 1, 'bg_color': '#D3D3D3'})
format3 = workbook.add_format({'align': 'center', 'font_size': 10, 'border': 1})
format6 = workbook.add_format({'align': 'center', 'font_size': 10})
worksheet = workbook.add_worksheet('Report')
worksheet.set_column('A:A', 6,)
worksheet.set_column('B:B', 20)
worksheet.set_column('C:C', 20)
worksheet.set_column('D:D', 20)
worksheet.set_column('E:E', 20)
worksheet.set_column('F:F', 20)
worksheet.set_column('G:G', 20)
worksheet.set_column('H:H', 20)
worksheet.set_column('I:I', 20)
worksheet.set_column('J:J', 25)
worksheet.set_column('K:K', 20)
worksheet.set_column('L:L', 20)
row = 2
column = 0
worksheet.merge_range(row, column, row + 1, column + 12, 'Calibration Instrument Master List', head)
row = 5
column = 0
worksheet.write(row, column, 'SL. No', cell_text_format)
worksheet.write(row, column + 1, 'DESCRIPTION', cell_text_format)
worksheet.write(row, column + 2, 'Range', cell_text_format)
worksheet.write(row, column + 3, 'Make', cell_text_format)
worksheet.write(row, column + 4, 'Sl.No', cell_text_format)
worksheet.write(row, column + 5, 'ID NUMBER', cell_text_format)
worksheet.write(row, column + 6, 'CALIBRATED ON', cell_text_format)
worksheet.write(row, column + 7, 'CALIBRATION DUE', cell_text_format)
worksheet.write(row, column + 8, 'LOCATION', cell_text_format)
worksheet.write(row, column + 9, 'TEST CERTIFICATE NO', cell_text_format)
worksheet.write(row, column + 10, 'STATUS/REMARKS', cell_text_format)
worksheet.write(row, column + 11, 'VERIFIED ', cell_text_format)
calibrations = []
if data['date_range_based']:
calibrations = self.env['instrument.calibration'].search([('last_due_date', '>=', data['from_date'])]).filtered(lambda s:s.last_due_date != False and s.last_calib_date != False)
if data['month_based']:
due_date = (date.today() + dateutil.relativedelta.relativedelta(months=int(data['notify_date'])))
calibrations = self.env['instrument.calibration'].search([('last_due_date', '<=', due_date)]).filtered(lambda s:s.last_due_date != False and s.last_calib_date != False)
calib_list = []
if calibrations:
i = 1
for calib in calibrations:
vals = {'no': i,
'desc': calib.product_desc,
'range': calib.range,
'make': calib.instrument_make.name if calib.instrument_make else False,
'sl': calib.sl_no,
'id_no': calib.id_number,
'calib_date': calib.last_calib_date.strftime('%d/%m/%Y') if calib.last_calib_date else False,
'calib_due': calib.last_due_date.strftime('%d/%m/%Y') if calib.last_due_date else False,
'location': calib.location_id.name if calib.location_id else False,
'cert_no': calib.last_certi_no,
'status': calib.status,
'verified': calib.calib_verify}
calib_list.append(vals)
i = i+1
print(calib_list)
row = 6
for ca_list in calib_list:
worksheet.write(row + 1, column + 0, ca_list['no'], format1)
worksheet.write(row + 1, column + 1, ca_list['desc'], format1)
worksheet.write(row + 1, column + 2, ca_list['range'], format1)
worksheet.write(row + 1, column + 3, ca_list['make'], format1)
worksheet.write(row + 1, column + 4, ca_list['sl'], format1)
worksheet.write(row + 1, column + 5, ca_list['id_no'], format1)
worksheet.write(row + 1, column + 6, ca_list['calib_date'], format1)
worksheet.write(row + 1, column + 7, ca_list['calib_due'], format1)
worksheet.write(row + 1, column + 8, ca_list['location'], format1)
worksheet.write(row + 1, column + 9, ca_list['cert_no'], format1)
worksheet.write(row + 1, column + 10, ca_list['status'], format1)
worksheet.write(row + 1, column + 11, ca_list['verified'], format1)
row +=1