from odoo import models, fields, api import io import xlsxwriter from odoo.tools.misc import xlsxwriter import base64 class ActionPlanWizard(models.TransientModel): _name = 'action_plan_report_wizard' _description = 'Action Plan Report Wizard' sales_person_id = fields.Many2one( 'res.users', string='Sales Executive', default=lambda self: self.env.user, domain=lambda self: [('groups_id', 'in', self.env.ref('sos_inventory.sos_sales_user').ids + self.env.ref('sos_inventory.sos_ce_head').ids)] ) from_date = fields.Date( string="From Date", default=fields.Date.today # Default to today's date ) to_date = fields.Date( string="To Date", default=fields.Date.today # Default to today's date ) def generate_report(self): """Trigger the report generation filtered by selected sales person.""" return self.env['sos_case_diary'].action_report_action_plan_btn(self.sales_person_id.id,self.from_date,self.to_date) def action_export(self): try: domain = [] if self.sales_person_id: domain.append(('sales_executive', '=', self.sales_person_id.id)) if self.from_date: domain.append(('date', '>=', self.from_date)) if self.to_date: domain.append(('date', '<=', self.to_date)) records = self.env['sos_sales_action_plan'].search(domain, order="date asc") if records: output = io.BytesIO() workbook = xlsxwriter.Workbook(output, {'in_memory': True}) headers = { 'status':'Status', 'customer_name':'Customer Name', 'location' : 'Location', 'sales_executive': 'Sales Executive', 'product': 'Product', 'date': 'Action Date', 'action_type':'Action Type', 'action': 'Action', 'action_plan': 'Action Plan', 'result': 'Result', 'next_action_date': 'Next Action On' } sequence_util = self.env['sos_common_scripts'] sequence_util._write_sheet(workbook, 'Action Plans', records, headers) workbook.close() output.seek(0) attachment = self.env['ir.attachment'].create({ 'name': f'{self.from_date}_{self.to_date}_Action_Plan.xlsx', 'type': 'binary', 'datas': base64.b64encode(output.read()), 'res_model': self._name, 'res_id': self.id, 'mimetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }) return { 'type': 'ir.actions.act_url', 'url': f'/web/content/{attachment.id}?download=true', 'target': 'self', } else: return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'message': "No Records Found", 'type': 'danger', 'sticky': False } } except ValueError as e: print(f"Failed to find report action: {e}")