This question has been flagged
1239 Views

I have customized a new report in excel. In a wizard, after selecting the required fields, one button is given to print. That time one new file will be created and shows in a field. From there we can download it.

But after downloading the extension seems like .doc instead of .xls. In the linux system it's opening in the Excel but in the windows system it's showing error. Following is my code.

import xlwt
import base64
from io import BytesIO
from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError, Warning
from datetime import date

class ZambiaSalePurchaseReport(models.TransientModel):
_name = "zambia.sale.purchase.report"
_description = "Zambia Sale Purchase Report"

start_date = fields.Date(required=True)
end_date = fields.Date(required=True)
report_type = fields.Selection([('sale', 'Sale'), ('purchase', 'Purchase')], required=True)
tax_id = fields.Many2one('account.tax')
company_id = fields.Many2one('res.company', string='Company', required=True,
default=lambda self: self.env.company)
state = fields.Selection([('choose', 'choose'), ('get', 'get')],
default='choose')
file_name = fields.Binary('Tax Report', readonly=True)
summary_data = fields.Char('Name', size=256)

def print_xls_report(self):
report_type = {'sale':'Sale Report','purchase':'Purchase Report'}
if self.start_date > self.end_date:
raise UserError(_("Can not select start date greater than end date"))
file_name = report_type.get(self.report_type) +str(self.start_date)+'TO'+str(self.end_date)+'.xls'

workbook = xlwt.Workbook(encoding="UTF-8")
format0 = xlwt.easyxf(
'font:height 500,bold True;pattern: pattern solid, fore_colour gray25;align: horiz center; borders: top_color black, bottom_color black, right_color black, left_color black,\
left thin, right thin, top thin, bottom thin;')
formathead2 = xlwt.easyxf(
'font:height 250,bold True;pattern: pattern solid, fore_colour gray25;align: horiz center; borders: top_color black, bottom_color black, right_color black, left_color black,\
left thin, right thin, top thin, bottom thin;')
format1 = xlwt.easyxf('font:bold True;pattern: pattern solid, fore_colour gray25;align: horiz left; borders: top_color black, bottom_color black, right_color black, left_color black,\
left thin, right thin, top thin, bottom thin;')
format2 = xlwt.easyxf('font:bold True;align: horiz left')
format3 = xlwt.easyxf('align: horiz left; borders: top_color black, bottom_color black, right_color black, left_color black,\
left thin, right thin, top thin, bottom thin;')

row, col = 8, 0
report_type = {'sale': 'Sale Report', 'purchase': 'Purchase Report'}
sheet = workbook.add_sheet(report_type.get(self.report_type))

sheet.col(0).width = int(30 * 260)
sheet.col(1).width = int(30 * 260)
sheet.col(2).width = int(30 * 260)
sheet.col(3).width = int(30 * 260)
sheet.col(4).width = int(50 * 260)
sheet.col(5).width = int(30 * 260)
sheet.col(6).width = int(30 * 260)
sheet.col(7).width = int(30 * 260)
sheet.col(8).width = int(30 * 260)

sheet.row(0).height_mismatch = True
sheet.row(0).height = 150 * 4

sheet.row(1).height_mismatch = True
sheet.row(1).height = 150 * 4

sheet.write_merge(0, 0, 0, 4, report_type.get(self.report_type), format0)
sheet.write_merge(1, 1, 0, 4, 'Start Date:' + str(self.start_date) + 'End Date:' + str(self.end_date), formathead2)

sheet.row(2).height_mismatch = True
sheet.row(2).height = 150 * 3

report_type = {'sale': 'Sale Report', 'purchase': 'Purchase Report'}

if self.report_type == 'sale':
order_ids = self.env['account.move'].search([
('invoice_date', '>=', self.start_date),
('invoice_date', '<=', self.end_date),
('company_id', '=', self.company_id.id),
('move_type', 'in', ['out_invoice', 'out_refund']),
('state', '=', 'posted')], order="id asc")

date = str(self.start_date) + ' - ' + str(self.end_date)
sheet.write(2, 0, 'TPIN of Purchaser', formathead2)
sheet.write(2, 1, 'Name Of Purchaser', formathead2)
sheet.write(2, 2, 'Invoice Number', formathead2)
sheet.write(2, 3, 'Invoice Date', formathead2)
sheet.write(2, 4, "Description Of Goods & Services", formathead2)
sheet.write(2, 5, "Amount Before Tax", formathead2)
sheet.write(2, 6, "Local Excise Duty", formathead2)
sheet.write(2, 7, "Total", formathead2)
sheet.write(2, 8, "Vat Charged", formathead2)

row = 3
for order_id in order_ids:
line_ids = order_id.invoice_line_ids
line_name = line_ids[0].name if line_ids else "--"
amount_without_tax, vat_charge, total_amount = 0, 0, 0
for line_id in line_ids:
if self.tax_id.id in line_id.tax_ids.ids:
if order_id.move_type == 'out_refund':
amount_without_tax += -(line_id.price_subtotal)
total_amount += -(line_id.price_total)
vat_charge += -(line_id.price_total - line_id.price_subtotal)
else:
amount_without_tax += line_id.price_subtotal
total_amount += line_id.price_total
vat_charge += line_id.price_total - line_id.price_subtotal

sheet.write(row, 0, order_id.partner_id.tpin if order_id.partner_id.tpin else '--')
sheet.write(row, 1, order_id.partner_id.name)
sheet.write(row, 2, order_id.name)
sheet.write(row, 3, str(order_id.invoice_date))
sheet.write(row, 4, line_name)
sheet.write(row, 5, amount_without_tax)
sheet.write(row, 6, 0)
sheet.write(row, 7, total_amount)
sheet.write(row, 8, vat_charge)
row += 1


elif self.report_type == 'purchase':
order_ids = self.env['account.move'].search([
('invoice_date', '>=', self.start_date),
('invoice_date', '<=', self.end_date),
('company_id', '=', self.company_id.id),
('move_type', 'in', ['in_invoice', 'in_refund']),
('state', '=', 'posted')], order="id asc")

date = str(self.start_date) + ' - ' + str(self.end_date)
sheet.write(2, 0, 'TPIN of Supplier', formathead2)
sheet.write(2, 1, 'Name Of Supplier', formathead2)
sheet.write(2, 2, 'Invoice Number', formathead2)
sheet.write(2, 3, 'Invoice Date', formathead2)
sheet.write(2, 4, "Description Of Goods & Services", formathead2)
sheet.write(2, 5, "Amount Before Tax", formathead2)
sheet.write(2, 6, "Vat Charged", formathead2)

row = 3
for order_id in order_ids:
line_ids = order_id.invoice_line_ids
line_name = line_ids[0].name if line_ids else "--"
amount_without_tax, vat_charge, total_amount = 0, 0, 0
for line_id in line_ids:
if self.tax_id.id in line_id.tax_ids.ids:
if order_id.move_type == 'in_refund':
amount_without_tax += -(line_id.price_subtotal)
total_amount += -(line_id.price_total)
vat_charge += -(line_id.price_total - line_id.price_subtotal)
else:
amount_without_tax += line_id.price_subtotal
total_amount += line_id.price_total
vat_charge += line_id.price_total - line_id.price_subtotal
sheet.write(row, 0, order_id.partner_id.tpin if order_id.partner_id.tpin else '--')
sheet.write(row, 1, order_id.partner_id.name)
sheet.write(row, 2, order_id.name)
sheet.write(row, 3, str(order_id.invoice_date))
sheet.write(row, 4, line_name)
sheet.write(row, 5, amount_without_tax)
sheet.write(row, 6, vat_charge)
row += 1
fp = BytesIO()
workbook.save(fp)
self.write(
{'state': 'get', 'file_name': base64.encodestring(fp.getvalue()), 'summary_data': file_name})
fp.close()
return {
'type': 'ir.actions.act_window',
'res_model': 'zambia.sale.purchase.report',
'view_mode': 'form',
'view_type': 'form',
'res_id': self.id,
'target': 'new',
}
# return res

@api.onchange('report_type')
def onchange_basket(self):
for rec in self:
rec.tax_id = False
res = {
'domain' : {
'tax_id' : [('type_tax_use', '=', self.report_type)],
},
}
return res

Avatar
Discard

Consider using xlsxwriter module of python