11"""Handlers for reports workflow."""
22
33import calendar
4+ import json
45from datetime import datetime , timedelta
56from io import BytesIO
67from typing import Any , Optional
@@ -457,13 +458,15 @@ async def per_category_report(
457458 ) -> bool :
458459 """Per category expenses."""
459460 from_user = from_user or message .from_user
461+ monthly_report = False
460462 if year is not None and month is not None and day is not None :
461463 period = f'{ year } -{ month :02} -{ day :02} '
462464 elif year is not None and month is not None :
463465 period = '{month}, {year}' .format (
464466 month = __ (MONTH_LABELS [month ], from_user .language_code ),
465467 year = year
466468 )
469+ monthly_report = True
467470 elif year is not None :
468471 period = f'{ year } '
469472 else :
@@ -482,20 +485,41 @@ async def per_category_report(
482485 )
483486 categories = []
484487 amounts = []
488+ colors = []
485489 for record in records :
486490 if record .amount :
491+ amounts .append (record .amount )
487492 if record .category_title :
488493 categories .append (record .category_title )
489494 else :
490495 categories .append ('Uncategorized' )
491- amounts .append (record .amount )
496+ category = self .db .get_category_by (
497+ book_id = book .id ,
498+ id = record .category_id ,
499+ )
500+ if monthly_report :
501+ try :
502+ options = json .loads (category .options )
503+ except Exception :
504+ options = {}
505+ monthly_limit = options .get ('monthly_limit' )
506+ if monthly_limit :
507+ if record .amount <= monthly_limit :
508+ colors .append ('#A1D99B' )
509+ else :
510+ colors .append ('#FC9272' )
511+ else :
512+ colors .append ('#6BAED6' )
513+ else :
514+ colors .append ('#6BAED6' )
515+
492516 if not categories :
493517 return False
494518 total_amount = sum (amounts )
495519 max_amount = max (amounts )
496520
497521 fig , ax = plt .subplots ()
498- bars = ax .barh (categories , amounts , label = categories )
522+ bars = ax .barh (categories , amounts , label = categories , color = colors )
499523 fig .suptitle (
500524 __ (
501525 text_dict = messages .REPORTS_BOOK_AND_PERIOD ,
@@ -564,7 +588,7 @@ async def per_day_report(
564588 else :
565589 category_type_label = __ (messages .REPORTS_EXPENSE , lang = from_user .language_code )
566590 fig , ax = plt .subplots ()
567- bars = ax .bar (days , amounts , label = days , align = 'center' )
591+ bars = ax .bar (days , amounts , label = days , align = 'center' , color = '#6BAED6' )
568592 fig .suptitle (
569593 __ (
570594 text_dict = messages .REPORTS_BOOK_AND_PERIOD ,
0 commit comments