File size: 15,661 Bytes
f6ad4dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""

Report generator for YourCarbonFootprint application.

Generates PDF reports and visualizations.

"""

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from fpdf import FPDF
import os
from datetime import datetime
import base64
from io import BytesIO

class ReportGenerator:
    def __init__(self, data_handler, translations=None):
        """Initialize the ReportGenerator class."""
        self.data_handler = data_handler
        # Use provided translations or default
        self.translations = translations or {
            'English': {
                'title': 'Carbon Emissions Report',
                'company': 'Company',
                'industry': 'Industry',
                'location': 'Location',
                'reporting_period': 'Reporting Period',
                'generated_on': 'Generated on',
                'summary': 'Summary',
                'total_emissions': 'Total Emissions',
                'emissions_by_scope': 'Emissions by Scope:',
                'top_categories': 'Top Categories:',
                'emissions_data': 'Emissions Data',
                'date': 'Date',
                'scope': 'Scope',
                'category': 'Category',
                'activity': 'Activity',
                'quantity': 'Quantity',
                'unit': 'Unit',
                'factor': 'Factor',
                'emissions_kgco2e': 'Emissions (kgCO2e)',
                'reg_compliance': 'Regulatory Compliance',
                'cbam': 'EU CBAM: This report can be used as supporting documentation for EU CBAM compliance.',
                'gx_league': 'Japan GX League: This report follows the GX League reporting format.',
                'ets': 'Indonesia ETS/ETP: This report can be used for Indonesia ETS/ETP compliance.',
                'recommendations': 'Recommendations',
                'rec1': '1. Focus on reducing emissions from the top categories identified in this report.',
                'rec2': '2. Consider implementing energy efficiency measures for Scope 2 emissions.',
                'rec3': '3. Explore renewable energy options to reduce your carbon footprint.',
                'rec4': '4. Engage with suppliers to address Scope 3 emissions in your value chain.',
            },
            'Vietnamese': {
                'title': 'Báo cáo Phát thải Carbon',
                'company': 'Công ty',
                'industry': 'Ngành nghề',
                'location': 'Địa điểm',
                'reporting_period': 'Kỳ báo cáo',
                'generated_on': 'Ngày tạo',
                'summary': 'Tóm tắt',
                'total_emissions': 'Tổng phát thải',
                'emissions_by_scope': 'Phát thải theo phạm vi:',
                'top_categories': 'Danh mục hàng đầu:',
                'emissions_data': 'Dữ liệu phát thải',
                'date': 'Ngày',
                'scope': 'Phạm vi',
                'category': 'Danh mục',
                'activity': 'Hoạt động',
                'quantity': 'Số lượng',
                'unit': 'Đơn vị',
                'factor': 'Hệ số',
                'emissions_kgco2e': 'Phát thải (kgCO2e)',
                'reg_compliance': 'Tuân thủ quy định',
                'cbam': 'EU CBAM: Báo cáo này có thể dùng làm tài liệu hỗ trợ tuân thủ EU CBAM.',
                'gx_league': 'Japan GX League: Báo cáo này tuân theo định dạng báo cáo GX League.',
                'ets': 'Indonesia ETS/ETP: Báo cáo này có thể dùng cho tuân thủ Indonesia ETS/ETP.',
                'recommendations': 'Khuyến nghị',
                'rec1': '1. Tập trung giảm phát thải từ các danh mục hàng đầu trong báo cáo này.',
                'rec2': '2. Xem xét thực hiện các biện pháp tiết kiệm năng lượng cho phát thải phạm vi 2.',
                'rec3': '3. Khám phá các lựa chọn năng lượng tái tạo để giảm dấu chân carbon.',
                'rec4': '4. Hợp tác với nhà cung cấp để giải quyết phát thải phạm vi 3 trong chuỗi giá trị.',
            }
        }

    def t(self, key, language):
        return self.translations.get(language, self.translations['English']).get(key, key)

    def generate_pdf_report(self, file_path=None, start_date=None, end_date=None, company_info=None, language='English'):
        """

        Generate PDF report.

        

        Args:

            file_path (str, optional): Path to save PDF file

            start_date (datetime, optional): Start date for filtering

            end_date (datetime, optional): End date for filtering

            company_info (dict, optional): Company information

            language (str, optional): Language for the report text



        Returns:

            bytes or bool: PDF bytes if file_path is None, otherwise True if successful

        """
        try:
            # Get filtered data
            data = self.data_handler.get_filtered_data(start_date, end_date)
            
            if len(data) == 0:
                return False, "No data available for the selected period."
            
            # Create PDF
            pdf = FPDF()
            pdf.add_page()
            pdf.set_font("Arial", "B", 16)
            pdf.cell(0, 10, self.t('title', language), 0, 1, "C")
            pdf.set_font("Arial", "", 12)
            if company_info:
                pdf.cell(0, 10, f"{self.t('company', language)}: {company_info.get('name', 'N/A')}", 0, 1)
                pdf.cell(0, 10, f"{self.t('industry', language)}: {company_info.get('industry', 'N/A')}", 0, 1)
                pdf.cell(0, 10, f"{self.t('location', language)}: {company_info.get('location', 'N/A')}", 0, 1)
            pdf.cell(0, 10, f"{self.t('reporting_period', language)}: {start_date.strftime('%Y-%m-%d') if start_date else 'All'} to {end_date.strftime('%Y-%m-%d') if end_date else 'All'}", 0, 1)
            pdf.cell(0, 10, f"{self.t('generated_on', language)}: {datetime.now().strftime('%Y-%m-%d')}", 0, 1)
            pdf.ln(10)
            pdf.set_font("Arial", "B", 14)
            pdf.cell(0, 10, self.t('summary', language), 0, 1)
            pdf.set_font("Arial", "", 12)
            total_emissions = data['emissions_kgCO2e'].sum()
            pdf.cell(0, 10, f"{self.t('total_emissions', language)}: {total_emissions:.2f} kgCO2e", 0, 1)
            scope_data = data.groupby('scope')['emissions_kgCO2e'].sum().reset_index()
            pdf.ln(5)
            pdf.cell(0, 10, self.t('emissions_by_scope', language), 0, 1)
            for _, row in scope_data.iterrows():
                pdf.cell(0, 10, f"{row['scope']}: {row['emissions_kgCO2e']:.2f} kgCO2e ({row['emissions_kgCO2e'] / total_emissions * 100:.1f}%)", 0, 1)
            category_data = data.groupby('category')['emissions_kgCO2e'].sum().reset_index()
            pdf.ln(5)
            pdf.cell(0, 10, self.t('top_categories', language), 0, 1)
            for _, row in category_data.nlargest(5, 'emissions_kgCO2e').iterrows():
                pdf.cell(0, 10, f"{row['category']}: {row['emissions_kgCO2e']:.2f} kgCO2e ({row['emissions_kgCO2e'] / total_emissions * 100:.1f}%)", 0, 1)
            pdf.ln(10)
            pdf.set_font("Arial", "B", 14)
            pdf.cell(0, 10, self.t('emissions_data', language), 0, 1)
            pdf.set_font("Arial", "B", 10)
            col_widths = [25, 25, 30, 30, 20, 15, 25, 30]
            headers = [self.t('date', language), self.t('scope', language), self.t('category', language), self.t('activity', language), self.t('quantity', language), self.t('unit', language), self.t('factor', language), self.t('emissions_kgco2e', language)]
            for i, header in enumerate(headers):
                pdf.cell(col_widths[i], 10, header, 1)
            pdf.ln()
            pdf.set_font("Arial", "", 8)
            for _, row in data.iterrows():
                pdf.cell(col_widths[0], 10, row['date'].strftime('%Y-%m-%d') if isinstance(row['date'], pd.Timestamp) else str(row['date']), 1)
                pdf.cell(col_widths[1], 10, str(row['scope']), 1)
                pdf.cell(col_widths[2], 10, str(row['category']), 1)
                pdf.cell(col_widths[3], 10, str(row['activity']), 1)
                pdf.cell(col_widths[4], 10, f"{row['quantity']:.2f}", 1)
                pdf.cell(col_widths[5], 10, str(row['unit']), 1)
                pdf.cell(col_widths[6], 10, f"{row['emission_factor']:.4f}", 1)
                pdf.cell(col_widths[7], 10, f"{row['emissions_kgCO2e']:.2f}", 1)
                pdf.ln()
            pdf.ln(10)
            pdf.set_font("Arial", "B", 14)
            pdf.cell(0, 10, self.t('reg_compliance', language), 0, 1)
            pdf.set_font("Arial", "", 12)
            pdf.cell(0, 10, self.t('cbam', language), 0, 1)
            pdf.cell(0, 10, self.t('gx_league', language), 0, 1)
            pdf.cell(0, 10, self.t('ets', language), 0, 1)
            pdf.ln(10)
            pdf.set_font("Arial", "B", 14)
            pdf.cell(0, 10, self.t('recommendations', language), 0, 1)
            pdf.set_font("Arial", "", 12)
            pdf.cell(0, 10, self.t('rec1', language), 0, 1)
            pdf.cell(0, 10, self.t('rec2', language), 0, 1)
            pdf.cell(0, 10, self.t('rec3', language), 0, 1)
            pdf.cell(0, 10, self.t('rec4', language), 0, 1)
            if file_path:
                pdf.output(file_path)
                return True, self.t('title', language) + ' generated successfully.'
            else:
                return pdf.output(dest='S').encode('latin1'), self.t('title', language) + ' generated successfully.'
        except Exception as e:
            return False, f"Error generating PDF report: {str(e)}"
    
    def create_scope_pie_chart(self, data):
        """

        Create pie chart of emissions by scope.

        

        Args:

            data (pandas.DataFrame): Emissions data

            

        Returns:

            plotly.graph_objects.Figure: Pie chart figure

        """
        scope_data = data.groupby('scope')['emissions_kgCO2e'].sum().reset_index()
        fig = px.pie(
            scope_data, 
            values='emissions_kgCO2e', 
            names='scope',
            color='scope',
            color_discrete_map={
                'Scope 1': '#4CAF50', 
                'Scope 2': '#2196F3', 
                'Scope 3': '#FFC107'
            },
            title='Emissions by Scope'
        )
        fig.update_layout(
            legend_title="Scope",
            font=dict(size=12),
            margin=dict(t=50, b=20, l=20, r=20)
        )
        return fig
    
    def create_category_bar_chart(self, data):
        """

        Create bar chart of emissions by category.

        

        Args:

            data (pandas.DataFrame): Emissions data

            

        Returns:

            plotly.graph_objects.Figure: Bar chart figure

        """
        category_data = data.groupby('category')['emissions_kgCO2e'].sum().reset_index()
        category_data = category_data.sort_values('emissions_kgCO2e', ascending=False)
        fig = px.bar(
            category_data, 
            x='category', 
            y='emissions_kgCO2e',
            color='category',
            title='Emissions by Category'
        )
        fig.update_layout(
            xaxis_title="Category",
            yaxis_title="Emissions (kgCO2e)",
            legend_title="Category",
            font=dict(size=12),
            margin=dict(t=50, b=100, l=50, r=20),
            xaxis_tickangle=-45
        )
        return fig
    
    def create_time_series_chart(self, data):
        """

        Create time series chart of emissions over time.

        

        Args:

            data (pandas.DataFrame): Emissions data

            

        Returns:

            plotly.graph_objects.Figure: Line chart figure

        """
        if 'date' not in data.columns or len(data) == 0:
            # Create empty figure if no data
            fig = go.Figure()
            fig.update_layout(
                title='Emissions Over Time',
                xaxis_title="Month",
                yaxis_title="Emissions (kgCO2e)",
                font=dict(size=12),
                margin=dict(t=50, b=50, l=50, r=20)
            )
            return fig
        
        # Group by month and scope
        time_data = data.copy()
        time_data['month'] = pd.to_datetime(time_data['date']).dt.strftime('%Y-%m')
        time_data = time_data.groupby(['month', 'scope'])['emissions_kgCO2e'].sum().reset_index()
        
        fig = px.line(
            time_data, 
            x='month', 
            y='emissions_kgCO2e',
            color='scope',
            markers=True,
            title='Emissions Over Time'
        )
        fig.update_layout(
            xaxis_title="Month",
            yaxis_title="Emissions (kgCO2e)",
            legend_title="Scope",
            font=dict(size=12),
            margin=dict(t=50, b=50, l=50, r=20)
        )
        return fig
    
    def create_activity_treemap(self, data):
        """

        Create treemap of emissions by scope, category, and activity.

        

        Args:

            data (pandas.DataFrame): Emissions data

            

        Returns:

            plotly.graph_objects.Figure: Treemap figure

        """
        fig = px.treemap(
            data,
            path=['scope', 'category', 'activity'],
            values='emissions_kgCO2e',
            color='scope',
            color_discrete_map={
                'Scope 1': '#4CAF50', 
                'Scope 2': '#2196F3', 
                'Scope 3': '#FFC107'
            },
            title='Emissions Breakdown'
        )
        fig.update_layout(
            margin=dict(t=50, b=20, l=20, r=20),
            font=dict(size=12)
        )
        return fig
    
    def create_monthly_comparison_chart(self, data):
        """

        Create bar chart comparing emissions by month.

        

        Args:

            data (pandas.DataFrame): Emissions data

            

        Returns:

            plotly.graph_objects.Figure: Bar chart figure

        """
        if 'date' not in data.columns or len(data) == 0:
            # Create empty figure if no data
            fig = go.Figure()
            fig.update_layout(
                title='Monthly Emissions Comparison',
                xaxis_title="Month",
                yaxis_title="Emissions (kgCO2e)",
                font=dict(size=12),
                margin=dict(t=50, b=50, l=50, r=20)
            )
            return fig
        
        # Group by month
        monthly_data = data.copy()
        monthly_data['month'] = pd.to_datetime(monthly_data['date']).dt.strftime('%Y-%m')
        monthly_data = monthly_data.groupby('month')['emissions_kgCO2e'].sum().reset_index()
        
        fig = px.bar(
            monthly_data,
            x='month',
            y='emissions_kgCO2e',
            title='Monthly Emissions Comparison'
        )
        fig.update_layout(
            xaxis_title="Month",
            yaxis_title="Emissions (kgCO2e)",
            font=dict(size=12),
            margin=dict(t=50, b=50, l=50, r=20)
        )
        return fig