16
16
from bot .common import EntryType
17
17
from bot .common import ConversationState as CS
18
18
import bot .google_sheet as gs
19
- import bot .firebase as db
19
+ import bot .firestore as db
20
20
21
21
timezone = pytz .timezone ("Asia/Singapore" )
22
22
@@ -37,6 +37,7 @@ def check_date_format(date_string):
37
37
pattern = r"\b\d{1,2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b"
38
38
return bool (re .fullmatch (pattern , date_string , re .IGNORECASE ))
39
39
40
+
40
41
def check_month_format (month_string ):
41
42
pattern = r"^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$"
42
43
return bool (re .fullmatch (pattern , month_string , re .IGNORECASE ))
@@ -623,6 +624,7 @@ def get_day_transaction(update, context):
623
624
update .message .reply_text (ERROR_TEXT )
624
625
return ConversationHandler .END
625
626
627
+
626
628
def get_overall (update , context ):
627
629
context .user_data .clear ()
628
630
telegram_id = update .effective_user .id
@@ -634,6 +636,7 @@ def get_overall(update, context):
634
636
update .message .reply_text (ERROR_TEXT )
635
637
return ConversationHandler .END
636
638
639
+
637
640
def handle_get_transaction (update , context ):
638
641
sheet_id = context .user_data ["sheet_id" ]
639
642
reply = update .message .text
@@ -662,28 +665,42 @@ def handle_get_transaction(update, context):
662
665
except Exception as e :
663
666
update .message .reply_text (ERROR_TEXT )
664
667
668
+
665
669
def handle_get_overall (update , context ):
666
670
sheet_id = context .user_data ["sheet_id" ]
667
671
month = update .message .text
668
- msg = ""
669
672
try :
670
673
if check_month_format (month ):
671
674
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" )
680
696
return ConversationHandler .END
681
697
else :
682
698
update .message .reply_text (GET_OVERALL_TEXT )
683
699
return CS .HANDLE_GET_OVERALL
684
700
except Exception as e :
685
701
update .message .reply_text (ERROR_TEXT )
686
702
703
+
687
704
def add_income (update , context ):
688
705
context .user_data .clear ()
689
706
telegram_id = update .effective_user .id
@@ -807,7 +824,7 @@ def setup_handlers(dispatcher):
807
824
CommandHandler ("addothers" , add_others ),
808
825
CommandHandler ("addincome" , add_income ),
809
826
CommandHandler ("getdaytransaction" , get_day_transaction ),
810
- CommandHandler ("getoverall" , get_overall )
827
+ CommandHandler ("getoverall" , get_overall ),
811
828
],
812
829
states = {
813
830
CS .SET_UP : [MessageHandler (Filters .text & ~ Filters .command , set_up )],
0 commit comments