1616from bot .common import EntryType
1717from bot .common import ConversationState as CS
1818import bot .google_sheet as gs
19- import bot .firebase as db
19+ import bot .firestore as db
2020
2121timezone = pytz .timezone ("Asia/Singapore" )
2222
@@ -37,6 +37,7 @@ def check_date_format(date_string):
3737 pattern = r"\b\d{1,2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b"
3838 return bool (re .fullmatch (pattern , date_string , re .IGNORECASE ))
3939
40+
4041def check_month_format (month_string ):
4142 pattern = r"^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$"
4243 return bool (re .fullmatch (pattern , month_string , re .IGNORECASE ))
@@ -623,6 +624,7 @@ def get_day_transaction(update, context):
623624 update .message .reply_text (ERROR_TEXT )
624625 return ConversationHandler .END
625626
627+
626628def get_overall (update , context ):
627629 context .user_data .clear ()
628630 telegram_id = update .effective_user .id
@@ -634,6 +636,7 @@ def get_overall(update, context):
634636 update .message .reply_text (ERROR_TEXT )
635637 return ConversationHandler .END
636638
639+
637640def handle_get_transaction (update , context ):
638641 sheet_id = context .user_data ["sheet_id" ]
639642 reply = update .message .text
@@ -662,28 +665,42 @@ def handle_get_transaction(update, context):
662665 except Exception as e :
663666 update .message .reply_text (ERROR_TEXT )
664667
668+
665669def handle_get_overall (update , context ):
666670 sheet_id = context .user_data ["sheet_id" ]
667671 month = update .message .text
668- msg = ""
669672 try :
670673 if check_month_format (month ):
671674 values = gs .get_overall (sheet_id , month )
672- msg = "```\n "
673- for row in values :
674- if len (row ) == 3 :
675- msg += "{:<20} {:<10} {:<10}\n " .format (* row )
676- elif len (row ) == 2 :
677- msg += "{:<20} {:<10}\n " .format (* row )
678- msg += "```"
679- update .message .reply_text (msg , parse_mode = 'Markdown' )
675+
676+ final_income = values [0 ]
677+ msg = f"--- Final Income ---\n `{ final_income [2 ]} `\n \n "
678+
679+ msg += "--- Spending ---\n "
680+ max_len = (
681+ max (len (row [0 ]) for row in values [1 :- 2 ]) + 1
682+ ) # +1 for some extra space
683+ for row in values [1 :- 2 ]:
684+ if row [1 ].startswith ("-" ): # if value is negative
685+ msg += f"`{ row [0 ].ljust (max_len )} { row [1 ]} `\n "
686+ else :
687+ msg += f"`{ row [0 ].ljust (max_len )} { row [1 ]} `\n " # else keep the original space
688+
689+ total_spent = values [- 2 ]
690+ msg += f"\n --- Total Spent ---\n `{ total_spent [1 ]} `\n "
691+
692+ overall = values [- 1 ]
693+ msg += f"\n --- Overall ---\n `{ overall [2 ]} `\n "
694+
695+ update .message .reply_text (msg , parse_mode = "Markdown" )
680696 return ConversationHandler .END
681697 else :
682698 update .message .reply_text (GET_OVERALL_TEXT )
683699 return CS .HANDLE_GET_OVERALL
684700 except Exception as e :
685701 update .message .reply_text (ERROR_TEXT )
686702
703+
687704def add_income (update , context ):
688705 context .user_data .clear ()
689706 telegram_id = update .effective_user .id
@@ -807,7 +824,7 @@ def setup_handlers(dispatcher):
807824 CommandHandler ("addothers" , add_others ),
808825 CommandHandler ("addincome" , add_income ),
809826 CommandHandler ("getdaytransaction" , get_day_transaction ),
810- CommandHandler ("getoverall" , get_overall )
827+ CommandHandler ("getoverall" , get_overall ),
811828 ],
812829 states = {
813830 CS .SET_UP : [MessageHandler (Filters .text & ~ Filters .command , set_up )],
0 commit comments