| 1 |  |  | import random | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from functools import partial | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import arrow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import telegram | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from . import BotCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | class TotalCommand(BotCommand): | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |     command = '/total' | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |     def __init__(self, *args, **kwargs): | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         self.commands = [ | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |             {'text': "Today's Total", | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |              'function': self.get_todays_total}, | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |             {'text': 'This Month', | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |              'function': self.get_month_total}, | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |             {'text': 'Last Month', | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |              'function': self.get_last_month_total}, | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |             {'text': 'Grand Total', | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |              'function': self.get_grand_total}, | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |             ] | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         super(TotalCommand, self).__init__(*args, **kwargs) | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |     def default(self, message): | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         keyboard = [["Today's Total"], | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |                     ["This Month", "Last Month"], | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |                     ['Grand Total']] | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         reply_markup = telegram.ReplyKeyboardMarkup( | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |             keyboard, resize_keyboard=True, | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |             one_time_keyboard=True, selective=True) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         msg = self.bot.say(message, 'Choose', reply_markup=reply_markup) | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         self.queue(msg, partial(self.process_which_total)) | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |     def process_which_total(self, message): | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         if not message.text: | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |             self.bot.say(message, 'Nope') | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |             return | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         for command in self.commands: | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |             if message.text == command['text']: | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |                 command['function'](message) | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     def get_month_total(self, message): | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         tab = self.get_tab(message.chat.id) | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         from_date = arrow.now(tab.tz).floor('month') | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         self.bot.say(message, "Month's Total: {:.2f}".format(tab.get_total(from_date=from_date)), | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                      reply_markup=telegram.ReplyKeyboardHide()) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |     def get_last_month_total(self, message): | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         tab = self.get_tab(message.chat.id) | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         from_date, to_date = arrow.now(tab.tz).replace(months=-1).span('month') | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         total = tab.get_total(from_date=from_date, to_date=to_date) | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         self.bot.say(message, "Last Month's Total: {:.2f}".format(total), | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                      reply_markup=telegram.ReplyKeyboardHide()) | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     def get_todays_total(self, message): | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         tab = self.get_tab(message.chat.id) | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         today = arrow.now(tab.tz).floor('day') | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         self.bot.say(message, "Today's Total: {:.2f}".format(tab.get_total(from_date=today)), | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |                      reply_markup=telegram.ReplyKeyboardHide()) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     def get_grand_total(self, message): | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         tab = self.get_tab(message.chat.id) | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         emoji = getattr(telegram.Emoji, | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |                         random.choice(['ASTONISHED_FACE', 'FACE_SCREAMING_IN_FEAR'])) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         self.bot.say(message, 'Grand Total: {:.2f} {}'.format(tab.grandtotal, emoji), | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |                      reply_markup=telegram.ReplyKeyboardHide()) | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  |