def calculate_total_sale(self, index, is_detail=False): payment = self.payments_tree.item(index) sale_type = payment['values'][3] amount = payment['values'][4] currency = payment['values'][5] rate = string_to_float(payment['values'][7]) if currency == 'Dólares': if sale_type == 'Pago': self.total_payments += string_to_float(amount) self.dollars_payments += string_to_float(amount) else: self.total_payments -= string_to_float(amount) self.dollars_payments -= string_to_float(amount) self.total_payments_dollars_label['text'] = number_to_str( self.dollars_payments) + "$" else: if sale_type == 'Pago': self.total_payments += (string_to_float(amount) / rate) self.bs_payments += string_to_float(amount) else: self.total_payments -= (string_to_float(amount) / rate) self.bs_payments -= string_to_float(amount) if is_detail: self.total_payments_dollars_label['text'] = number_to_str( self.total_payments) + "$" self.total_payments_bs_label['text'] = number_to_str( self.bs_payments) + "bs"
def add_payment_to_tree(): try: if not (amount_entry.get()) or (amount_entry.get() == '0'): raise Exception("Debes agregar el monto.") if currency.get() == 'Bolívares': amount_currency = number_to_str(amount_entry.get()) + 'bs' if not (rate_entry.get()) or (string_to_float( rate_entry.get()) == 0): raise Exception("Debes especificar la tasa.") else: amount_currency = number_to_str(amount_entry.get()) + '$' type = 'Pago' if is_return: type = 'Vuelto' index = self.payments_tree.insert( "", index='end', value=(None, None, new_payment_date_entry.get(), type, amount_currency, currency.get(), method.get(), rate_entry.get(), account.get())) self.calculate_total_sale(index, is_detail) self.row_indexes.append(index) new_payment_window.destroy() if callbacks: for callback in callbacks: callback() except Exception as err: messagebox.showerror("Error", err, parent=new_payment_window)
def calculate_remaining(self): dollars_order = float(self.order_tree.total_orders_usd) total_dollar_payments = self.payment_handler.total_payments remaining_dollars = dollars_order - total_dollar_payments rate = string_to_float(self.rate_entry.get()) remaining_bs = number_to_str(remaining_dollars * rate) self.remaining_sale_dollars_label['text'] = number_to_str(remaining_dollars) + "$" self.remaining_sale_bs_label['text'] = number_to_str(remaining_bs) + "bs"
def _display_window(self): self.product_window = tk.Toplevel(width=700, height=700, padx=30, pady=30) self.product_window.title("Nueva Orden") # Rate rate_label = tk.Label(self.product_window, text="Tasa", font=('calibri', 16, 'bold')) rate_label.grid(row=0, columnspan=2, pady=(10, 20), sticky=tk.W) self.rate_entry = ttk.Entry(self.product_window, width=12, font=('calibri', 14)) self.rate_entry.insert(0, number_to_str(self.initial_rate)) self.rate_entry.grid(row=0, columnspan=2, pady=(10, 20), sticky=tk.W, padx=(50)) # Title. filters_title = tk.Label(self.product_window, text="Filtrar Productos", font=('calibri', 18, 'bold')) filters_title.grid(row=0, columnspan=2, pady=(10, 20))
def _insert_into_product_tree(self): self.product_tree.delete(*self.product_tree.get_children()) products = Product.select() if self.name_entry.get() != '': for word in self.name_entry.get().split(" "): products = products.select().where(Product.name.contains(word)) if self.reference_entry.get() != '': products = products.select().where( Product.reference.contains(self.reference_entry.get())) if self.code_entry.get() != '': products = products.select().where( Product.code.contains(self.code_entry.get())) if self.brand_entry.get() != '': products = products.select().where( Product.brand.contains(self.brand_entry.get())) rate = string_to_float(self.rate_entry.get()) for product in products: self.product_tree.insert( "", index='end', values=(product.id, product.brand, product.name, product.reference, product.code, str(product.price) + "$" + " ({}bs)".format( number_to_str(float(product.price) * rate))))
def insert_into_payment_tree(self, params): self.payment_tree.delete(*self.payment_tree.get_children()) payments = (Payment.select().where( Payment.date.between(params['from_date'], params['to_date']))) if params['type'] != 'Todo': type = Payment.TYPES[params['type']] payments = payments.select().where(Payment.type == type) if params['currency'] != 'Todo': currency = Payment.CURRENCIES[params['currency']] payments = payments.select().where(Payment.currency == currency) if params['method'] != 'Todo': method = Payment.METHODS[params['method']] payments = payments.select().where(Payment.method == method) if params['account'] != 'Todo': account = Payment.ACCOUNTS[params['account']] payments = payments.select().where(Payment.account == account) if params['client_id'] and params['pre_id']: client_identity_card = params['pre_id'] + "-" + params['client_id'] client = Client.get(Client.identity_card == client_identity_card) payments = payments.select().join(Sale).where( Sale.client == client) for payment in payments: currency_sign = 'bs' if payment.currency == 1: currency_sign = '$' self.payment_tree.insert( "", index=tk.END, values=(payment.sale, payment.date.strftime(DATE_FORMAT), [t for t in Payment.TYPES.keys()][payment.type], number_to_str(payment.amount) + currency_sign, number_to_str(payment.rate), [m for m in Payment.METHODS.keys()][payment.method], [acc for acc in Payment.ACCOUNTS.keys()][payment.account])) bs, usd, total = get_summary_payments(payments) self.bs_label['text'] = number_to_str(bs) + "bs" self.usd_label['text'] = number_to_str(usd) + "$" self.total_label['text'] = "Total " + number_to_str(total) + "$"
def insert_into_daily_tree(self, query_date=None): # Update title if not query_date: day = self.query_date.get() else: day = query_date self.day_tree_label['text'] = "Ventas del {} {} {} - {}".format( get_weekday(datetime.strptime(day, DATE_FORMAT)), day.split('-')[2], get_month_name(day), day.split('-')[0]) # Delete Previus Rows. self.day_tree.delete(*self.day_tree.get_children()) # Date. day_date = datetime.strptime(self.query_date.get(), DATE_FORMAT) day_sales = Sale.select().where(Sale.date==day_date).order_by(-Sale.is_finished) for sale in day_sales: orders = (Order .select() .join(Sale) .where(Sale.id == sale)) sale_total_orders = 0 for order in orders: sale_total_orders += order.price payments = (Payment .select() .join(Sale) .where(Sale.id == sale)) sale_total_payments = get_summary_payments(payments)[2] total = abs(sale_total_orders - sale_total_payments) if sale.is_finished: state = "Finalizado" total = sale_total_orders elif (sale_total_orders > sale_total_payments): state = "Crédito" else: state = "Vale" self.day_tree.insert( "", index='end', value=( sale.id, state, sale.description, number_to_str(total))) self.insert_into_summary_day()
def delete_payment_row(callbacks): if self.payments_tree.focus(): index = self.payments_tree.focus() payment = self.payments_tree.item(index) pay_type = payment['values'][3] amount = payment['values'][4] currency = payment['values'][5] rate = string_to_float(str(payment['values'][7])) if currency == 'Dólares': if pay_type == 'Pago': self.total_payments -= string_to_float(amount) self.dollars_payments -= string_to_float(amount) else: self.total_payments += string_to_float(amount) self.dollars_payments += string_to_float(amount) self.total_payments_dollars_label['text'] = number_to_str( self.dollars_payments) + '$' else: if pay_type == 'Pago': self.total_payments -= (string_to_float(amount) / rate) self.bs_payments -= string_to_float(amount) else: self.total_payments += (string_to_float(amount) / rate) self.bs_payments += string_to_float(amount) self.total_payments_bs_label['text'] = number_to_str( self.bs_payments) + 'bs' if self.payments_tree.item(index)['values'][0] != 'None': self.payments_to_delete.append( self.payments_tree.item(index)['values'][0]) self.payments_tree.delete(index) if sale_id: self.total_payments_dollars_label['text'] = number_to_str( self.total_payments) + '$' if callbacks: for callback in callbacks: callback()
def insert_into_payments_sale_tree(self, sale_id): payments = Payment.select().join(Sale).where(Sale.id == sale_id) for payment in payments: if payment.currency == 0: sign = "bs" else: sign = "$" self.payments_tree.insert( "", index='end', value=(payment.id, payment.sale.id, payment.date, [t for t in Payment.TYPES.keys()][payment.type], number_to_str(payment.amount) + sign, [c for c in Payment.CURRENCIES.keys()][payment.currency], [m for m in Payment.METHODS.keys() ][payment.method], payment.rate, payment.account)) total_payments = get_summary_payments(payments)[2] self.total_payments_dollars_label['text'] = number_to_str( total_payments) for index in self.payments_tree.get_children(): self.calculate_total_sale(index, True)
def add_payment_window(self, date, rate, is_return=False, is_detail=False, total=0, currency_option=None, callbacks=[]): # New Window. new_payment_window = tk.Toplevel(padx=30, pady=50) # Title. title = 'Agregar Pago' if is_return: title = 'Agregar Vuelto' new_payment_window.title(title) title_label = tk.Label(new_payment_window, text=title, font=('calibri', 18, 'bold')) title_label.grid(row=0, column=0, pady=(0, 30)) # Date. date_label = tk.Label(new_payment_window, text="Fecha", font=('calibri', 15)) date_label.grid(row=1, column=0, pady=(0, 20), sticky=tk.W) new_payment_date_entry = ttk.Entry(new_payment_window, width=10, font=('calibri', 15)) new_payment_date_entry.insert(0, date) new_payment_date_entry.grid( row=1, sticky=tk.E, pady=(0, 20), ) # Currency. curr_label = tk.Label(new_payment_window, text="Moneda", font=('calibri', 15)) curr_label.grid(row=2, column=0, pady=(0, 20), padx=(0, 200), sticky=tk.W) currency = tk.StringVar() currency_choices = ('', 'Bolívares', 'Dólares') currency.set(currency_choices[1]) curr_option = ttk.OptionMenu(new_payment_window, currency, *currency_choices) curr_option.grid(row=2, pady=(0, 20), sticky=tk.E) if currency_option == "usd": currency.set(currency_choices[2]) # Method method_label = tk.Label(new_payment_window, text="Método Pago", font=('calibri', 15)) method_label.grid(row=3, sticky=tk.W, pady=(0, 20)) method = tk.StringVar() methods = [m for m in Payment.METHODS.keys()] method_choices = ('', *methods) method.set(method_choices[1]) if is_return: method.set(method_choices[3]) method_option = ttk.OptionMenu(new_payment_window, method, *method_choices) method_option.grid(row=3, sticky=tk.E, pady=(0, 20)) if currency_option == "usd": method.set(method_choices[4]) # Account account_label = tk.Label(new_payment_window, text="Cuenta", font=('calibri', 15)) account_label.grid(row=4, sticky=tk.W, pady=(0, 20)) account = tk.StringVar() account_choices = ['', *[acc for acc in Payment.ACCOUNTS.keys()]] account.set(account_choices[1]) account_option = ttk.OptionMenu(new_payment_window, account, *account_choices) account_option.grid(row=4, sticky=tk.E, pady=(0, 20)) # Rate rate_label = tk.Label(new_payment_window, text="Tasa del día", font=('calibri', 15)) rate_label.grid(row=5, column=0, sticky=tk.W, pady=(0, 20)) rate_entry = ttk.Entry(new_payment_window, width=13, font=('calibri', 15)) rate_entry.insert(0, rate) rate_entry.grid(row=5, column=0, sticky=tk.E, pady=(0, 20)) # Amount amount_label = tk.Label(new_payment_window, text="Monto", font=('calibri', 15)) amount_label.grid(row=6, pady=(0, 20), sticky=tk.W) amount_entry = ttk.Entry(new_payment_window, width=13, font=('calibri', 15)) amount_entry.focus() amount_entry.grid(row=6, pady=(0, 20), sticky=tk.E) if total: amount_entry.insert(0, number_to_str(total)) # Saving def add_payment_to_tree(): try: if not (amount_entry.get()) or (amount_entry.get() == '0'): raise Exception("Debes agregar el monto.") if currency.get() == 'Bolívares': amount_currency = number_to_str(amount_entry.get()) + 'bs' if not (rate_entry.get()) or (string_to_float( rate_entry.get()) == 0): raise Exception("Debes especificar la tasa.") else: amount_currency = number_to_str(amount_entry.get()) + '$' type = 'Pago' if is_return: type = 'Vuelto' index = self.payments_tree.insert( "", index='end', value=(None, None, new_payment_date_entry.get(), type, amount_currency, currency.get(), method.get(), rate_entry.get(), account.get())) self.calculate_total_sale(index, is_detail) self.row_indexes.append(index) new_payment_window.destroy() if callbacks: for callback in callbacks: callback() except Exception as err: messagebox.showerror("Error", err, parent=new_payment_window) save_button = tk.Button(new_payment_window, text="Agregar", font=('calibri', 18, 'bold'), bd=1, relief=tk.RIDGE, bg='#54bf54', command=add_payment_to_tree) save_button.grid(row=7, pady=(20, 0), sticky=tk.W + tk.E)
def insert_into_credits_tree(self, params): self.credits_tree.delete(*self.credits_tree.get_children()) credits = [] vales = [] unfinished_sales = (Sale.select().where(Sale.is_finished == False)) client_identity_card = params['pre_id'] + "-" + params['identity'] if params['identity']: unfinished_sales = (unfinished_sales .select() .join(Client) .where(Client.identity_card .contains(client_identity_card))) elif params['name']: unfinished_sales = (unfinished_sales .select() .join(Client) .where(Client.name .contains(params['name']))) for sale in unfinished_sales: sale_total_orders = 0 sale_total_payments = 0 orders = (Order .select() .join(Sale) .where(Sale.id == sale) ) for order in orders: sale_total_orders += order.price payments = (Payment .select() .join(Sale) .where(Sale.id == sale)) sale_total_payments = get_summary_payments(payments)[2] total = abs(sale_total_orders - sale_total_payments) if (sale_total_orders > sale_total_payments): credits.append([sale,total]) else: vales.append([sale,total]) total_credits = 0 total_vales = 0 if not self.vale: for credit in credits: sale = credit[0] if not sale.client: print(sale.id) total = credit[1] total_credits += total self.total_label['text'] = number_to_str(total_credits) + "$" self.credits_tree.insert( "", index='end', value=( sale.id, (date.today() - sale.date).days, sale.client.name, sale.client.identity_card, sale.description, number_to_str(total) ) ) else: for vale in vales: sale = vale[0] total = vale[1] total_vales += total self.total_label['text'] = number_to_str(total_vales) + "$" self.credits_tree.insert( "", index='end', value=( sale.id, (date.today() - sale.date).days, sale.client.name, sale.client.identity_card, sale.description, number_to_str(total) ) )
def _update_total_labels(self): self.total_orders_label_usd['text'] = number_to_str(self.total_orders_usd) + "$" if not self.sale: self.total_orders_label_bs['text'] = number_to_str(self.total_orders_bs) + "bs"
def insert_into_summary_day(self, query_date=None): if not query_date: query_date = self.query_date.get() # Getting sales and payments def get_month_payments(): year = int(query_date.split("-")[0]) month = int(query_date.split("-")[1]) day = int(query_date.split("-")[2]) day_date = datetime.strptime(query_date, DATE_FORMAT) if day == 1: return (Payment .select() .where(Payment.date==day_date)) else: first_day_of_month = datetime(year, month, 1) return (Payment .select() .where(Payment.date.between(first_day_of_month, day_date))) def get_week_payments(): day_date = datetime.strptime(self.query_date.get(), DATE_FORMAT) for i in range(7): new_date = day_date + timedelta(days=-i) if (get_weekday(new_date) == 'Lunes') and (i == 0): return (Payment .select() .where(Payment.date==new_date)) elif (get_weekday(new_date) == 'Lunes'): return (Payment .select() .where(Payment.date.between(new_date, day_date))) month_payments = get_month_payments() week_payments = get_week_payments() day_payments = Payment.select().where(Payment.date==datetime.strptime(self.query_date.get(), DATE_FORMAT)) self.summary_sales_tree.delete(*self.summary_sales_tree.get_children()) # Summary day bs_day, usd_day, total_day = get_summary_payments(day_payments) self.summary_sales_tree.insert( "", index='end', value=( 'Día', number_to_str(bs_day), number_to_str(usd_day), number_to_str(total_day))) # Sumary week bs_week, usd_week, total_week = get_summary_payments(week_payments) self.summary_sales_tree.insert( "", index='end', value=( 'Semana', number_to_str(bs_week), number_to_str(usd_week), number_to_str(total_week))) # Sumary month bs_month, usd_month, total_month = get_summary_payments(month_payments) self.summary_sales_tree.insert( "", index='end', value=( 'Mes', number_to_str(bs_month), number_to_str(usd_month), number_to_str(total_month)))