import base64 from io import BytesIO from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from odoo import models, fields, api from datetime import datetime import pytz from odoo.exceptions import UserError from odoo.tools import pdf import os import weasyprint class ReportGenerator(models.Model): _name = 'sos_report_generator' _description = 'Report Generator' name = fields.Char('Name', required=True) report_type = fields.Selection([ ('daily', 'Daily'), ('weekly', 'Weekly'), ('monthly', 'Monthly'), ], string='Report Type', required=True , default='daily') @api.model def generate_report(self): """ Generate the report based on the report type. """ # Execute the query for the material data self.env.cr.execute(""" SELECT part_no,inhand_stock_qty,CASE WHEN inhand_stock_val IS NULL THEN '0.00' ELSE TO_CHAR(inhand_stock_val, 'FM999999999.00') END as formatted_inhand_stock_val FROM sos_material; """) materialdatas = self.env.cr.fetchall() # Execute the query for the fg data self.env.cr.execute(""" SELECT name,inhand_stock_qty,CASE WHEN inhand_stock_val IS NULL THEN '0.00' ELSE TO_CHAR(inhand_stock_val, 'FM999999999.00') END as formatted_inhand_stock_val FROM sos_fg ; """) fgdatas = self.env.cr.fetchall() # Execute the query for the sfg data self.env.cr.execute(""" SELECT name,inhand_stock_qty,CASE WHEN inhand_stock_val IS NULL THEN '0.00' ELSE TO_CHAR(inhand_stock_val, 'FM999999999.00') END as formatted_inhand_stock_val FROM sos_sfg; """) sfgdatas = self.env.cr.fetchall() # Prepare the HTML content report_html = self.generate_html_report(materialdatas, fgdatas, sfgdatas) # Generate PDF and send email attachment = self.create_pdf_from_html(report_html) self.send_email(attachment) def generate_html_report(self, materialdatas, fgdatas, sfgdatas): """ Generate the HTML content for the report with tables. """ report_data = [] report_data.append("") report_data.append("
| ID | Name | Inhand Stock Qty | Inhand Stock Val |
|---|---|---|---|
| {sno} | {row[0]} | {row[1]} | {row[2]} |
Dear User,
Find the attached report.
', 'email_from': "slink