class EntryField(FloatLayout): now = datetime.now() which_day = now.strftime('%d') which_month = now.strftime('%m') which_year = now.strftime("%Y") year_path = os.path.join('storage', str(which_year)) month_path = os.path.join(year_path, str(which_month)) day_path = os.path.join(month_path, str(which_day)) Path('storage').mkdir(exist_ok=True) Path(year_path).mkdir(exist_ok=True) Path(month_path).mkdir(exist_ok=True) wb_complete_path = f"{day_path}.xlsx" cols_list = [ 'Sale', 'Sale Time', 'Amount', 'Selling Price', 'Profit', 'Buying Price', 'Description' ] if not Path(wb_complete_path).is_file(): new_wb = opx.Workbook() for i, v in enumerate(cols_list, 1): new_wb.active.cell(row=1, column=i, value=v) new_wb.save(wb_complete_path) new_wb.close() load_wb = opx.load_workbook(wb_complete_path) ws = load_wb.active def __init__(self, title, grp, categories=['Alfa', 'Touch'], **kwargs): super().__init__(**kwargs) self.grp = grp self.categories = categories self.title = title self.label_color = '#231123' # as same as background color self.title_label = Label(text=f'[b]{self.title.upper()}[/b]', pos_hint={ 'x': 0.5, 'y': 0.94 }, font_size=20, markup=True, color=get_color_from_hex(self.label_color), size_hint=(0, 0)) self.add_widget(self.title_label) self.tglbox1 = BoxLayout(orientation='horizontal', pos_hint={ 'x': 0.005, 'y': 0.83 }, size_hint=(0.6, 0.06)) self.tglbtn1 = CheckBox(group=self.grp, size_hint=(0.2, 1)) self.tglbox1.add_widget(self.tglbtn1) self.tglbtn1_lbl = Label(text=self.categories[0], size_hint=(0.2, 1), font_size=17) self.tglbox1.add_widget(self.tglbtn1_lbl) self.add_widget(self.tglbox1) self.tglbox2 = BoxLayout(orientation='horizontal', pos_hint={ 'x': 0.005, 'y': 0.74 }, size_hint=(0.6, 0.06)) self.tglbtn2 = CheckBox(group=self.grp, size_hint=(0.2, 1)) self.tglbox2.add_widget(self.tglbtn2) self.tglbtn2_lbl = Label(text=self.categories[1], size_hint=(0.2, 1), font_size=17) self.tglbox2.add_widget(self.tglbtn2_lbl) self.add_widget(self.tglbox2) self.amt_box = BoxLayout( orientation='vertical', size_hint=(1, 0.1), pos_hint={ 'x': 0.02, 'y': 0.51 }, ) self.amt_label = Label(text='Amount:', size_hint=(0.34, 0.25)) self.amt_box.add_widget(self.amt_label) self.amt_input = ModifiedTextInput(size_hint=(0.34, 0.35), input_filter='int', text='1') self.amt_box.add_widget(self.amt_input) self.add_widget(self.amt_box) self.price_box = BoxLayout(orientation='vertical', size_hint=(0.9, 0.1), pos_hint={ 'x': 0.02, 'y': 0.31 }) self.price_label = Label(text='Price:', size_hint=(0.2, 0.25)) self.price_box.add_widget(self.price_label) self.price = ModifiedTextInput(hint_text='10000', size_hint=(0.55, 0.35), input_filter='int') self.price_box.add_widget(self.price) self.add_widget(self.price_box) self.cardtypes = ['Small', 'Big', 'Emergency', 'Start'] self.card_choose = Spinner( text=self.cardtypes[0], values=self.cardtypes, pos_hint={ 'x': 0.02, 'y': 0.58 }, size_hint=(0.95, 0.08), background_normal='', background_color=get_color_from_hex('#231123'), sync_height=True, text_autoupdate=True) self.bp_box = BoxLayout(orientation='vertical', size_hint=(0.9, 0.1), pos_hint={ 'x': 0.02, 'y': 0.18 }) self.bp_label = Label(text='Buying Price:', size_hint=(0.45, 0.25)) self.bp_box.add_widget(self.bp_label) self.bp = ModifiedTextInput(hint_text='10000', size_hint=(0.55, 0.35), input_filter='int') self.bp_box.add_widget(self.bp) self.product_box = BoxLayout(orientation='vertical', size_hint=(0.9, 0.13), pos_hint={ 'x': 0.02, 'y': 0.59 }) self.product_label = Label(text='Product Name:', size_hint=(0.55, 0.25)) self.product_box.add_widget(self.product_label) self.product_name = ModifiedTextInput(hint_text='Samsung S9+', size_hint=(1, 0.7)) self.product_name.font_size = 15 self.product_name.multiline = True self.product_box.add_widget(self.product_name) self.addbtn = AddButton(pos_hint={ 'x': 0.3, 'y': 0 }, size_hint=(0.35, 0.14)) self.add_widget(self.addbtn) self.addbtn.bind(on_press=self.callback) def callback(self, ins): print(ins) self.add_to_sheet() def add_to_sheet(self): self.background_color = get_color_from_hex('#9e2020') self.checked_box = '' if self.tglbtn1._get_active(): self.checked_box = self.categories[0] elif self.tglbtn2._get_active(): self.checked_box = self.categories[1] else: return self.error_popup( f'Please Choose {self.categories[0]} or {self.categories[1]}') if not self.price.text or not self.amt_input.text: return self.error_popup('Fields cannot be empty') if self.title.lower() == 'dollars': alltext = f'{self.checked_box.lower()} {self.title.lower()}' existing = AmountsField.existing_amounts_dict[alltext] company_take = AmountsField.existing_amounts_dict[ f'{self.checked_box.lower()} dollars taken'][1] if (existing[0] - (int(self.amt_input.text) + company_take)) < 0: return self.error_popup() if (int(self.price.text) / int(self.amt_input.text)) < existing[1]: return self.error_popup( 'Cannot sell for less than buying price') existing[0] -= (float(self.amt_input.text) + company_take) AmountsField.labels_dict[ alltext].text = f'{alltext.capitalize()}: {format(existing[0], ".2f")}' self.add_row(alltext.capitalize(), existing[1]) elif self.title.lower() == 'days': ind = f'{self.checked_box.lower()} big cards' existing_bc = AmountsField.existing_amounts_dict[ind] if int(self.amt_input.text) > existing_bc[0]: return self.error_popup() days_price = AmountsField.existing_amounts_dict[ f'{self.checked_box.lower()} {self.title.lower()}'][1] if (int(self.price.text) / int(self.amt_input.text)) < days_price: return self.error_popup( 'Cannot sell for less than buying price') existing_bc[0] -= int(self.amt_input.text) AmountsField.labels_dict[ ind].text = f'{ind.capitalize()}: {int(existing_bc[0])}' self.add_row(f'{self.checked_box.lower()} {self.title.lower()}', days_price) elif self.title.lower() == 'cards': type_of_card = f'{self.checked_box.lower()} {self.card_choose.text.lower()} {self.title.lower()}' existing_card = AmountsField.existing_amounts_dict[type_of_card] if int(self.amt_input.text) > existing_card[0]: return self.error_popup() if (int(self.price.text) / int(self.amt_input.text)) < existing_card[1]: return self.error_popup( 'Cannot sell for less than buying price') existing_card[0] -= int(self.amt_input.text) AmountsField.labels_dict[ type_of_card].text = f'{type_of_card.capitalize()}: {int(existing_card[0])}' self.add_row(type_of_card, existing_card[1]) elif self.title.lower() == 'other': self.add_row(self.checked_box, int(self.bp.text), True) self.load_wb.save(self.wb_complete_path) with open('.storage.json', 'w') as f: json.dump(AmountsField.existing_amounts_dict, f) def error_popup(self, error_text='You can\'t sell more than what you have!'): popup_label = Label(text=error_text, font_size=22) return Popup(title='Error', content=popup_label, size_hint=(0.45, 0.4)).open() def add_row(self, name, buying_price, is_phone_acc=False): max_row = self.ws.max_row + 1 self.ws['A' + str(max_row)] = name self.ws['B' + str(max_row)] = f"{datetime.now().strftime('%H: %M: %S')}" self.ws['C' + str(max_row)] = self.amt_input.text self.ws['D' + str(max_row)] = self.price.text total_buying_price = (buying_price * int(self.amt_input.text)) self.ws['E' + str(max_row)] = int(self.price.text) - total_buying_price self.ws['F' + str(max_row)] = total_buying_price TotalSelledField.totals[f"{datetime.now().strftime('%d')}"][1] += int( self.price.text) with open(TotalSelledField.totals_path, 'w') as f: json.dump(TotalSelledField.totals, f) if is_phone_acc: self.ws['G' + str(max_row)] = self.product_name.text self.product_name.text = '' self.bp.text = '' self.price.text = '' self.amt_input.text = '1' self.tglbtn1._set_active(False) self.tglbtn2._set_active(False)