File size: 15,228 Bytes
22836fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""

Data handler for CarbonFootprint by GXS application.

Manages data import, export, and processing.

"""

import pandas as pd
import json
import os
from datetime import datetime
import csv
from io import StringIO
from fpdf import FPDF
import matplotlib.pyplot as plt
import seaborn as sns
from emission_factors import get_emission_factor, get_categories, get_activities

# Constants
DATA_DIR = "data"
EMISSIONS_FILE = os.path.join(DATA_DIR, "emissions.json")
COMPANY_INFO_FILE = os.path.join(DATA_DIR, "company_info.json")

# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)

class DataHandler:
    def __init__(self):
        """Initialize the DataHandler class."""
        self.load_emissions_data()
        self.load_company_info()
    
    def load_emissions_data(self):
        """Load emissions data from file."""
        if os.path.exists(EMISSIONS_FILE):
            with open(EMISSIONS_FILE, 'r') as f:
                try:
                    self.emissions_data = pd.DataFrame(json.load(f))
                    # Convert date strings to datetime objects
                    if 'date' in self.emissions_data.columns:
                        self.emissions_data['date'] = pd.to_datetime(self.emissions_data['date'])
                except json.JSONDecodeError:
                    self.create_empty_emissions_data()
        else:
            self.create_empty_emissions_data()
    
    def create_empty_emissions_data(self):
        """Create empty emissions dataframe."""
        self.emissions_data = pd.DataFrame(columns=[
            'date', 'scope', 'category', 'activity', 'quantity', 
            'unit', 'emission_factor', 'emissions_kgCO2e', 'notes'
        ])
    
    def load_company_info(self):
        """Load company information from file."""
        if os.path.exists(COMPANY_INFO_FILE):
            with open(COMPANY_INFO_FILE, 'r') as f:
                try:
                    self.company_info = json.load(f)
                except json.JSONDecodeError:
                    self.create_empty_company_info()
        else:
            self.create_empty_company_info()
    
    def create_empty_company_info(self):
        """Create empty company information."""
        self.company_info = {
            "name": "",
            "industry": "",
            "location": "",
            "export_markets": [],
            "contact_person": "",
            "email": "",
            "phone": "",
            "address": "",
            "registration_number": "",
            "reporting_year": datetime.now().year
        }
    
    def save_emissions_data(self):
        """Save emissions data to file."""
        # Convert datetime objects to strings
        data_to_save = self.emissions_data.copy()
        if 'date' in data_to_save.columns:
            data_to_save['date'] = data_to_save['date'].dt.strftime('%Y-%m-%d')
        
        with open(EMISSIONS_FILE, 'w') as f:
            json.dump(data_to_save.to_dict('records'), f, indent=2)
    
    def save_company_info(self):
        """Save company information to file."""
        with open(COMPANY_INFO_FILE, 'w') as f:
            json.dump(self.company_info, f, indent=2)
    
    def add_emission_entry(self, date, scope, category, activity, quantity, unit, emission_factor, notes=""):
        """

        Add a new emission entry.

        

        Args:

            date (datetime): Date of the emission

            scope (str): Emission scope (Scope 1, Scope 2, or Scope 3)

            category (str): Emission category

            activity (str): Specific activity

            quantity (float): Quantity of activity

            unit (str): Unit of measurement

            emission_factor (float): Emission factor

            notes (str, optional): Additional notes

            

        Returns:

            bool: True if successful, False otherwise

        """
        try:
            # Calculate emissions
            emissions_kgCO2e = float(quantity) * float(emission_factor)
            
            # Create new entry
            new_entry = pd.DataFrame([{
                'date': pd.Timestamp(date),
                'scope': scope,
                'category': category,
                'activity': activity,
                'quantity': float(quantity),
                'unit': unit,
                'emission_factor': float(emission_factor),
                'emissions_kgCO2e': emissions_kgCO2e,
                'notes': notes
            }])
            
            # Append to existing data
            self.emissions_data = pd.concat([self.emissions_data, new_entry], ignore_index=True)
            
            # Save data
            self.save_emissions_data()
            
            return True
        except Exception as e:
            print(f"Error adding emission entry: {str(e)}")
            return False
    
    def import_csv(self, file_path_or_buffer):
        """

        Import emissions data from CSV.

        

        Args:

            file_path_or_buffer: Path to CSV file or file-like object

            

        Returns:

            tuple: (success, message)

        """
        try:
            # Read CSV
            df = pd.read_csv(file_path_or_buffer)
            
            # Check required columns
            required_columns = ['date', 'scope', 'category', 'activity', 'quantity', 'unit', 'emission_factor']
            missing_columns = [col for col in required_columns if col not in df.columns]
            
            if missing_columns:
                return False, f"Missing required columns: {', '.join(missing_columns)}"
            
            # Convert date strings to datetime objects
            df['date'] = pd.to_datetime(df['date'])
            
            # Calculate emissions if not provided
            if 'emissions_kgCO2e' not in df.columns:
                df['emissions_kgCO2e'] = df['quantity'].astype(float) * df['emission_factor'].astype(float)
            
            # Add notes column if not present
            if 'notes' not in df.columns:
                df['notes'] = ""
            
            # Append to existing data
            self.emissions_data = pd.concat([self.emissions_data, df], ignore_index=True)
            
            # Save data
            self.save_emissions_data()
            
            return True, f"Successfully imported {len(df)} entries"
        except Exception as e:
            return False, f"Error importing CSV: {str(e)}"
    
    def export_csv(self, file_path=None, start_date=None, end_date=None):
        """

        Export emissions data to CSV.

        

        Args:

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

            start_date (datetime, optional): Start date for filtering

            end_date (datetime, optional): End date for filtering

            

        Returns:

            str or bool: CSV string if file_path is None, otherwise True if successful

        """
        try:
            # Filter data by date range if specified
            data = self.emissions_data.copy()
            if start_date and end_date:
                mask = (data['date'] >= pd.Timestamp(start_date)) & (data['date'] <= pd.Timestamp(end_date))
                data = data.loc[mask]
            
            # Convert datetime objects to strings
            if 'date' in data.columns:
                data['date'] = data['date'].dt.strftime('%Y-%m-%d')
            
            if file_path:
                # Save to file
                data.to_csv(file_path, index=False)
                return True
            else:
                # Return CSV string
                csv_buffer = StringIO()
                data.to_csv(csv_buffer, index=False)
                return csv_buffer.getvalue()
        except Exception as e:
            print(f"Error exporting CSV: {str(e)}")
            return False
    
    def generate_pdf_report(self, file_path=None, start_date=None, end_date=None):
        """

        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

            

        Returns:

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

        """
        try:
            # Filter data by date range if specified
            data = self.emissions_data.copy()
            if start_date and end_date:
                mask = (data['date'] >= pd.Timestamp(start_date)) & (data['date'] <= pd.Timestamp(end_date))
                data = data.loc[mask]
            
            # Create PDF
            pdf = FPDF()
            pdf.add_page()
            
            # Set font
            pdf.set_font("Arial", "B", 16)
            
            # Title
            pdf.cell(0, 10, "Carbon Emissions Report", 0, 1, "C")
            pdf.set_font("Arial", "", 12)
            
            # Company info
            pdf.cell(0, 10, f"Company: {self.company_info['name']}", 0, 1)
            pdf.cell(0, 10, f"Reporting Period: {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"Generated on: {datetime.now().strftime('%Y-%m-%d')}", 0, 1)
            
            # Summary
            pdf.ln(10)
            pdf.set_font("Arial", "B", 14)
            pdf.cell(0, 10, "Summary", 0, 1)
            pdf.set_font("Arial", "", 12)
            
            total_emissions = data['emissions_kgCO2e'].sum()
            pdf.cell(0, 10, f"Total Emissions: {total_emissions:.2f} kgCO2e", 0, 1)
            
            # Emissions by scope
            scope_data = data.groupby('scope')['emissions_kgCO2e'].sum().reset_index()
            pdf.ln(5)
            pdf.cell(0, 10, "Emissions by Scope:", 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)
            
            # Emissions by category
            category_data = data.groupby('category')['emissions_kgCO2e'].sum().reset_index()
            pdf.ln(5)
            pdf.cell(0, 10, "Top Categories:", 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)
            
            # Data table
            pdf.ln(10)
            pdf.set_font("Arial", "B", 14)
            pdf.cell(0, 10, "Emissions Data", 0, 1)
            pdf.set_font("Arial", "B", 10)
            
            # Table header
            col_widths = [25, 25, 30, 30, 20, 15, 25, 30]
            headers = ['Date', 'Scope', 'Category', 'Activity', 'Quantity', 'Unit', 'Factor', 'Emissions (kgCO2e)']
            
            for i, header in enumerate(headers):
                pdf.cell(col_widths[i], 10, header, 1)
            pdf.ln()
            
            # Table data
            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()
            
            if file_path:
                # Save to file
                pdf.output(file_path)
                return True
            else:
                # Return PDF bytes
                return pdf.output(dest='S').encode('latin1')
        except Exception as e:
            print(f"Error generating PDF report: {str(e)}")
            return False
    
    def get_emissions_summary(self):
        """

        Get emissions summary statistics.

        

        Returns:

            dict: Summary statistics

        """
        if len(self.emissions_data) == 0:
            return {
                "total_emissions": 0,
                "scope_breakdown": {},
                "category_breakdown": {},
                "time_series": {}
            }
        
        # Total emissions
        total_emissions = self.emissions_data['emissions_kgCO2e'].sum()
        
        # Emissions by scope
        scope_data = self.emissions_data.groupby('scope')['emissions_kgCO2e'].sum().to_dict()
        
        # Emissions by category
        category_data = self.emissions_data.groupby('category')['emissions_kgCO2e'].sum().to_dict()
        
        # Time series data (monthly)
        time_data = self.emissions_data.copy()
        if 'date' in time_data.columns and len(time_data) > 0:
            time_data['month'] = time_data['date'].dt.strftime('%Y-%m')
            time_series = time_data.groupby(['month', 'scope'])['emissions_kgCO2e'].sum().reset_index()
            time_series_dict = {}
            for _, row in time_series.iterrows():
                if row['month'] not in time_series_dict:
                    time_series_dict[row['month']] = {}
                time_series_dict[row['month']][row['scope']] = row['emissions_kgCO2e']
        else:
            time_series_dict = {}
        
        return {
            "total_emissions": total_emissions,
            "scope_breakdown": scope_data,
            "category_breakdown": category_data,
            "time_series": time_series_dict
        }
    
    def get_filtered_data(self, start_date=None, end_date=None, scope=None, category=None):
        """

        Get filtered emissions data.

        

        Args:

            start_date (datetime, optional): Start date for filtering

            end_date (datetime, optional): End date for filtering

            scope (str, optional): Scope for filtering

            category (str, optional): Category for filtering

            

        Returns:

            pandas.DataFrame: Filtered data

        """
        data = self.emissions_data.copy()
        
        # Apply filters
        if start_date and end_date:
            mask = (data['date'] >= pd.Timestamp(start_date)) & (data['date'] <= pd.Timestamp(end_date))
            data = data.loc[mask]
        
        if scope:
            data = data[data['scope'] == scope]
        
        if category:
            data = data[data['category'] == category]
        
        return data