Compare commits

..

No commits in common. "8ec457bba1974240a451b7adfc9783b1c423ba7d" and "6de869c86ab5f72d361468c9ec4bedc0118a9f9c" have entirely different histories.

2 changed files with 20 additions and 67 deletions

View File

@ -1 +1 @@
1.3.126 1.3.125

View File

@ -140,39 +140,13 @@ class SubscriptionMatrixService:
months: Number of months to include months: Number of months to include
Returns: Returns:
List of product records with aggregated monthly data (including empty months) List of product records with aggregated monthly data
""" """
# Generate list of all months to display (last N months from today)
all_months = self._generate_month_range(months)
# Structure: {product_number: {year_month: {amount, status, invoice_number, period_from, period_to}}} # Structure: {product_number: {year_month: {amount, status, invoice_number, period_from, period_to}}}
product_matrix = defaultdict(dict) product_matrix = defaultdict(dict)
product_names = {} # Cache product names product_names = {} # Cache product names
# Initialize all products with empty cells for all months
try: try:
for invoice in invoices:
lines = invoice.get('lines', [])
for line in lines:
product_number = line.get('product', {}).get('productNumber')
product_name = line.get('product', {}).get('name') or line.get('description')
if product_number and product_number not in product_names:
product_names[product_number] = product_name or f"Product {product_number}"
# Initialize all products with all months (empty)
for product_number in product_names.keys():
for year_month in all_months:
product_matrix[product_number][year_month] = {
"amount": 0.0,
"status": "missing",
"invoice_number": None,
"period_from": None,
"period_to": None,
"period_label": None
}
# Now fill in data from invoices
for invoice in invoices: for invoice in invoices:
invoice_number = invoice.get('bookedInvoiceNumber') or invoice.get('draftInvoiceNumber') invoice_number = invoice.get('bookedInvoiceNumber') or invoice.get('draftInvoiceNumber')
# Determine status based on invoice type/endpoint it came from # Determine status based on invoice type/endpoint it came from
@ -240,15 +214,24 @@ class SubscriptionMatrixService:
# Calculate period label # Calculate period label
period_label = self._format_period_label(period_from, period_to) period_label = self._format_period_label(period_from, period_to)
# Update cell (it should already exist from initialization) # Initialize cell if doesn't exist
if year_month in product_matrix.get(product_number, {}): if year_month not in product_matrix[product_number]:
cell = product_matrix[product_number][year_month] product_matrix[product_number][year_month] = {
cell["amount"] = amount # Take last amount (or sum if multiple?) "amount": 0.0,
cell["status"] = self._determine_status(invoice_status) "status": "missing",
cell["invoice_number"] = invoice_number "invoice_number": None,
cell["period_from"] = period_from.isoformat().split('T')[0] "period_from": None,
cell["period_to"] = period_to.isoformat().split('T')[0] "period_to": None
cell["period_label"] = period_label }
# Update cell
cell = product_matrix[product_number][year_month]
cell["amount"] = amount # Take last amount (or sum if multiple?)
cell["status"] = self._determine_status(invoice_status)
cell["invoice_number"] = invoice_number
cell["period_from"] = period_from.isoformat().split('T')[0]
cell["period_to"] = period_to.isoformat().split('T')[0]
cell["period_label"] = period_label
except Exception as e: except Exception as e:
logger.error(f"❌ Error aggregating data: {e}") logger.error(f"❌ Error aggregating data: {e}")
@ -285,36 +268,6 @@ class SubscriptionMatrixService:
return products return products
@staticmethod
def _generate_month_range(num_months: int) -> List[str]:
"""
Generate list of month keys for the last N months
Args:
num_months: Number of months to generate (e.g., 12)
Returns:
Sorted list of 'YYYY-MM' strings going back from today
"""
months = []
today = datetime.now()
for i in range(num_months):
# Go back i months from today using calendar arithmetic
year = today.year
month = today.month - i
# Adjust year and month if month goes negative
while month <= 0:
month += 12
year -= 1
month_key = f"{year:04d}-{month:02d}"
months.append(month_key)
# Return sorted chronologically (oldest first)
return sorted(months)
@staticmethod @staticmethod
def _format_period_label(period_from: datetime, period_to: datetime) -> str: def _format_period_label(period_from: datetime, period_to: datetime) -> str:
"""Format period as readable label (e.g., 'Jan', 'Jan-Mar', etc)""" """Format period as readable label (e.g., 'Jan', 'Jan-Mar', etc)"""