def see_history():
        sql_connection = dbconnection.get_db_connection()
        payments = payment_repository.get_payments_by_order_id(sql_connection,
                                                               txt_order_id.get())
        try:
            order_details = order_repository.get_order_details_by_order_id(
                sql_connection,
                txt_order_id.get())
        except OrderDetailsFoundException as ex:
            tkMessageBox.showinfo("Error", ex.message)
            return
        total_order = order_utils.get_total_order(order_details)
        txt_query.delete(1.0, Tkinter.END)
        count = 0
        for payment in payments:
            count += 1
            readable_date = datetime.datetime.fromtimestamp(int(payment.date)).\
                strftime('%Y-%m-%d')
            payment_str = "payment %d - date:%s - amount:%d" % \
                          (count, readable_date, payment.amount)
            txt_query.insert(Tkinter.INSERT, payment_str)
            txt_query.insert(Tkinter.INSERT, "\n-------------------------------\n")

        total_payments = sum([payment.amount for payment in payments])
        debt = total_order - total_payments
        txt_query.insert(Tkinter.INSERT, "****Total payments up date: %d \n" %
                         total_payments)
        txt_query.insert(Tkinter.INSERT, "****Total order price: %d \n" % total_order)
        txt_query.insert(Tkinter.INSERT, "****Debt up date: %d \n" % debt)
        if debt is 0:
            txt_query.insert(Tkinter.INSERT, "****Congratulations, order Ok! :)\n")

        sql_connection.close()
    def status_business():
        txt_query.delete(1.0, Tkinter.END)
        sql_connection = dbconnection.get_db_connection()
        total_capital_up_date = status_business_service.get_total_capital_up_date(
            sql_connection)
        total_orders = status_business_service.get_total_all_orders(sql_connection)
        total_debt = status_business_service.get_total_debt(sql_connection)
        total_balance = status_business_service.get_real_balance(sql_connection)
        profit = status_business_service.get_profit(sql_connection)
        txt_query.insert(Tkinter.INSERT, "****Total capital up date: %d \n" %
                         total_capital_up_date)
        txt_query.insert(Tkinter.INSERT, "****Total orders up date: %d \n" %
                         total_orders)
        txt_query.insert(Tkinter.INSERT, "****Total debt up date: %d \n" %
                         total_debt)
        if total_balance <= 0:
            txt_query.insert(Tkinter.INSERT, "****Total balance: %d, "
                                             "WARNING negative balance \n" %
                             total_balance)
        else:
            txt_query.insert(Tkinter.INSERT, "****Total balance: %d, "
                                             "CONGRATULATIONS balance is positive :) \n" %
                             total_balance)

        txt_query.insert(Tkinter.INSERT, "****Profit: %d\n" % profit)

        txt_query.insert(Tkinter.INSERT, "\n***********DEBTORS***********\n\n")
        debtors = status_business_service.get_debtors(sql_connection)
        _paint_debtors(debtors)
        sql_connection.close()
 def query_order_by_order_id():
     txt_query.delete(1.0, Tkinter.END)
     with dbconnection.get_db_connection() as sql_connection:
         try:
             order = order_repository.get_order_by_order_id(
                 sql_connection, txt_order_id.get())
             paint_orders([order])
         except OrderIdNotFoundException as ex:
             tkMessageBox.showinfo("Error", ex.message)
    def save_payment():
        date = int(datetime.datetime.strptime(txt_date.get(), '%Y-%m-%d').strftime("%s"))
        try:
            int_amount = int(txt_amount.get())
            payment = Payment(None, txt_order_id.get() or None, date or None,
                              int_amount or None)
        except ValueError as ex:
            tkMessageBox.showinfo("Error", ex.message)
            return

        sql_connection = dbconnection.get_db_connection()
        try:
            payment_service.save_payment(sql_connection, payment)
            tkMessageBox.showinfo("Message", "save data ok!")
        except PaymentExceedsValueDebtException as ex:
            tkMessageBox.showinfo("Error", ex.message)
        except InvalidAmountToSave as ex:
            tkMessageBox.showinfo("Error", ex.message)
        except sqlite3.IntegrityError, ex:
            tkMessageBox.showinfo("Error", ex.message)
 def save_action():
     result = tkMessageBox.askquestion("Validate Details info", order_details_list)
     if result == "yes":
         try:
             order = _create_order_object(
                 txt_customer.get(),
                 int(datetime.datetime.strptime(txt_date.get(),
                                                '%Y-%m-%d').strftime("%s")),
                 txt_customer_address.get(),
                 txt_customer_phone.get(),
                 txt_purchase_price.get(),
                 txt_note.get(1.0, Tkinter.END), order_details_list)
         except ValueError as ex:
             tkMessageBox.showinfo("Error", ex.message)
             return
         sql_connection = dbconnection.get_db_connection()
         try:
             order_service.save_order(sql_connection, order)
             clear_fields()
             tkMessageBox.showinfo("Message", "save data ok!")
         except sqlite3.IntegrityError, ex:
             tkMessageBox.showinfo("Error", ex.message)
         except OrderDetailsEmptyException, ex:
             tkMessageBox.showinfo("Error", ex.message)
 def query_order_by_customer_name():
     txt_query.delete(1.0, Tkinter.END)
     with dbconnection.get_db_connection() as sql_connection:
         orders = order_repository.get_orders_by_customer_name(
             sql_connection, txt_customer.get())
         paint_orders(orders)