def test_service(self):
     service = Service("json_test.json", "json_test.json", "json_test.json",
                       "json_test.json")
     # Customer Options
     company = Company()
     address = Address()
     address.set_address("Strada Academiei nr. 7")
     address.set_county("Bucuresti")
     address.set_country("Romania")
     address.set_postal_code("010011")
     company.set_id(1)
     company.set_address(address)
     company.set_company_name("La Geani")
     company.set_registration_number("RO0123")
     company.set_fiscal_no("0000231523647")
     company.set_email_address("*****@*****.**")
     company.set_first_name("Ion")
     company.set_last_name("Bogdan")
     company.set_phone_number("0752314567")
     company.set_fiscal_no("0000012345678")
     company.set_registration_number("RO01923")
     service.create_customer(company)
     self.assertEqual(service.get_company_customer(1), company)
     company.set_company_name("Marketplex")
     service.modify_customer(1, company)
     self.assertEqual(service.get_company_customer(1), company)
     service.delete_customer(company)
     self.assertEqual(service.view_all_company_customer(), [])
     individual = Individual()
     individual.set_cnp("1971209075425")
     individual.set_id(1)
     individual.set_first_name("Petre")
     individual.set_last_name("Vasile")
     individual.set_phone_number("0745321784")
     individual.set_email_address("*****@*****.**")
     service.create_customer(individual)
     self.assertEqual(service.get_individual_customer(1), individual)
     individual.set_last_name("Gigel")
     service.modify_customer(1, individual)
     self.assertEqual(service.get_individual_customer(1), individual)
     service.delete_customer(individual)
     self.assertEqual(service.view_all_individual_customer(), [])
     individual.set_address(address)
     # Currency Options
     currency = Currency("$", "Dollar", "USD")
     currency.set_id(1)
     currency.set_exchange_rate(4.5)
     service.create_currency(currency)
     self.assertEqual(currency, service.get_currency(1))
     currency.set_exchange_rate(4.2)
     service.modify_currency(1, currency)
     self.assertEqual(currency, service.get_currency(1))
     service.delete_currency(1)
     self.assertEqual(service.view_currency(), [])
     # Item Options
     item = Item()
     item.set_id(1)
     item.set_currency(currency)
     item.set_name("McChicken")
     item.set_description("Chicken hamburger")
     item.set_price(2)
     item.set_discount(0)
     service.create_item(item)
     self.assertEqual(item, service.choose_item(1))
     item.set_price(3)
     service.modify_item(1, item)
     self.assertEqual(item, service.choose_item(1))
     service.delete_item(1)
     self.assertEqual(service.view_items(), [])
     # Bill Options
     bill_item = BillItem()
     bill_item.set_id(1)
     bill_item.set_currency(currency)
     bill_item.set_name("McChicken")
     bill_item.set_description("Chicken hamburger")
     bill_item.set_price(3)
     bill_item.set_discount(0)
     bill = Invoice()
     bill.set_customer(individual)
     bill.set_currency(currency)
     bill.set_items([bill_item])
     bill.set_id(1)
     bill.set_issuer(company)
     bill.set_issue_date("24.01.2021")
     bill.set_due_date("10.02.2021")
     bill.set_notes("Platiti la Banca Transilvania")
     service.create_item(item)
     service.create_bill(bill)
     self.assertEqual(service.get_invoice(1), bill)
     bill.set_notes("asdfge")
     service.modify_bill(1, bill)
     self.assertEqual(service.get_invoice(1), bill)
     service.add_item_to_bill(1, bill)
     bill.add_items(bill_item)
     self.assertEqual(service.get_invoice(1), bill)
     service.invoice_to_fiscal(1)
     fiscal_bill = FiscalBill()
     fiscal_bill.set_customer(individual)
     fiscal_bill.set_currency(currency)
     bill_item.set_quantity(2)
     fiscal_bill.set_total(3.0)
     fiscal_bill.set_items([bill_item])
     fiscal_bill.set_id(1)
     fiscal_bill.set_issuer(company)
     fiscal_bill.set_issue_date("24.01.2021")
     fiscal_bill.set_due_date("10.02.2021")
     fiscal_bill.set_notes("asdfge")
     self.assertEqual(fiscal_bill, service.get_fiscal(1))
     service.delete_bill(bill)
     file = open("json_test.json", "w")
     file.truncate(0)
     file.write("{}")
     file.close()
Beispiel #2
0
class Console:
    def __init__(self, currency_file, item_file, customer_file, bill_file):
        self.__service = Service(currency_file, item_file, customer_file,
                                 bill_file)
        self.__validator = Validator.get_instance()

    # Customer options
    def customer_menu(self):
        print("Choose an option:\n1.Create customer\n2.Delete customer")
        print("3.Modify customer\n4.See customer list")
        try:
            options = {
                "1": self.create_customer,
                "2": self.delete_customer,
                "3": self.modify_customer,
                "4": self.view_customers
            }
            option = input()
            self.__validator.option_check(option, 4)
            options[option]()
        except OptionError as exp:
            print(exp)

    def create_customer(self):
        try:
            number = int(input("How many customers do you want to create"))
        except ValueError as exp:
            print("Value must be numeric")
            return
        option = self.choose_customer_type()
        while number != 0:

            try:
                if isinstance(option, Individual):
                    self.__service.create_customer(self.create_individual())
                if isinstance(option, Company):
                    self.__service.create_customer(self.create_company())
                number -= 1
            except Exception as exp:
                print(exp)

    def create_individual(self):
        individual = Individual()
        individual.set_cnp(input("Cnp: "))
        individual.set_first_name(input("First Name: "))
        individual.set_last_name(input("Last Name: "))
        individual.set_phone_number(input("Phone Number: "))
        individual.set_email_address(input("Email Address: "))
        address = input("Give address: ")
        county = input("Give county: ")
        postal_code = input("Give postal code: ")
        country = input("Give country: ")
        customer_address = Address()
        customer_address.set_address(address)
        customer_address.set_county(county)
        customer_address.set_country(country)
        customer_address.set_postal_code(postal_code)
        individual.set_address(address)
        return individual

    def create_company(self):
        company = Company()
        company.set_first_name(input("First Name: "))
        company.set_last_name(input("Last Name: "))
        company.set_phone_number(input("Phone Number: "))
        company.set_email_address(input("Email Address: "))
        company.set_company_name(input("Company Name: "))
        company.set_fiscal_no(input("Fiscal Number: "))
        company.set_registration_number(input("Registration Number: "))
        address = input("Give address: ")
        county = input("Give county: ")
        postal_code = input("Give postal code: ")
        country = input("Give country: ")
        customer_address = Address()
        customer_address.set_address(address)
        customer_address.set_county(county)
        customer_address.set_country(country)
        customer_address.set_postal_code(postal_code)
        company.set_address(customer_address)
        return company

    def delete_customer(self):
        try:
            index = int(input("What customer do you want to delete?"))
            customer = self.choose_customer_type()
            customer.set_id(index)
            self.__service.delete_customer(customer)
        except Exception as exp:
            print(exp)

    def modify_customer(self):
        new_customer = None
        try:
            option = self.choose_customer_type()
            print("What customer do you want to change?")
            index = int(input("Give id: "))
            option.set_id(index)
            print("Create the customer you want to add")
            if isinstance(option, Individual):
                new_customer = self.create_individual()
            if isinstance(option, Company):
                new_customer = self.create_company()
            self.__service.modify_customer(option.get_id(), new_customer)
        except Exception as exp:
            print(exp)

    def choose_customer_type(self):
        option = input(
            "What type of customer database do you want to access?\n1-Individual or 2-Company customers?\n"
        )
        options = {"1": Individual, "2": Company}
        return options[option]()

    def view_customers(self):
        option = self.choose_customer_type()
        if isinstance(option, Individual):
            for customer in self.__service.view_all_individual_customer():
                print(customer)
        if isinstance(option, Company):
            for customer in self.__service.view_all_company_customer():
                print(customer)

    def choose_customer(self, customer_type):
        if isinstance(customer_type, Individual):
            print("What individual do you want to pick?")
            return self.__service.get_individual_customer(
                int(input("Give id: ")))
        if isinstance(customer_type, Company):
            print("What company do you want to pick?")
            return self.__service.get_company_customer(int(input("Give id: ")))

    # Item Options
    def item_menu(self):
        print("1.Create item\n2.Delete item\n3.Modify item\n4.See all items")
        try:
            options = {
                "1": self.create_item,
                "2": self.delete_item,
                "3": self.modify_item,
                "4": self.view_items
            }
            option = input("Choose an option:")
            self.__validator.option_check(option, 4)
            options[option]()
        except OptionError as exp:
            print(exp)

    def create_item(self):
        try:
            item = self.input_item()
            self.__service.create_item(item)
        except Exception as exp:
            print(exp)

    def input_item(self):
        item = Item()
        item.set_name(input("Give name: "))
        item.set_description(input("Give description: "))
        item.set_price(int(input("Give price: ")))
        item.set_discount(int(input("Give discount: ")))
        print("Is the discount a percent?\nYes of No")
        percent = input()
        if percent == "Yes":
            item.set_percent_discount(True)
        else:
            item.set_percent_discount(False)
        item.set_currency(self.choose_currency())
        return item

    def delete_item(self):
        try:
            print("Choose item to delete:")
            option = int(input("Give id: "))
            self.__service.delete_item(option)
        except Exception as exp:
            print(exp)

    def modify_item(self):
        try:
            old_item = int(input("Choose item to delete"))
            new_item = self.input_item()
            self.__service.modify_item(old_item, new_item)
        except OptionError as exp:
            print(exp)

    def view_items(self):
        for item in self.__service.view_items():
            print(item)

    def choose_item(self):
        self.view_items()
        option = input("Choose desired item")
        return option

    # Currency Options
    def currency_menu(self):
        print(
            "1. Create currency\n2.Delete currency\n3.Modify currency\n4.View all curency"
        )
        option = input("Choose an option: ")
        try:
            self.__validator.option_check(option, 4)
            options = {
                "1": self.create_currency,
                "2": self.delete_currency,
                "3": self.modify_currency,
                "4": self.view_all_currency
            }
            options[option]()
        except OptionError as exp:
            print(exp)

    def create_currency(self):
        try:
            self.__service.create_currency(self.input_currency())
        except Exception as exp:
            print(exp)

    def input_currency(self):
        symbol = input("Give currency symbol: ")
        name = input("Give currency name: ")
        code = input("Give currency code: ")
        exchange_rate = float(
            input("Give exchange rate from created currency to RON"))
        currency = Currency(symbol, name, code)
        currency.set_exchange_rate(exchange_rate)
        return currency

    def delete_currency(self):
        try:
            option = int(
                input("What currency do you wish to delete?\nGive id: "))
            self.__service.delete_currency(option)
        except Exception as exp:
            print(exp)

    def modify_currency(self):
        try:
            old_currency = input("Which currency do you wish to change")
            new_currency = self.input_currency()
            self.__service.modify_currency(old_currency, new_currency)
        except Exception as exp:
            print(exp)

    def view_all_currency(self):
        for currency in self.__service.view_currency():
            print(currency)

    def choose_currency(self):
        print("Choose the desired currency from:")
        self.view_all_currency()
        return self.__service.get_currency(int(input("Give id: ")))

    # Bill Options

    def bill_menu(self):
        print("1.Create bill\n2.Delete bill\n3.Modify bill")
        print(
            "4.Print bill\n5.Add item to bill\n6.Create a invoice as a fiscal bill\n"
            "7.Render bill\n8.Choose template")
        option = input("Choose option:")
        try:
            self.__validator.option_check(option, 8)
            options = {
                "1": self.create_bill,
                "2": self.delete_bill,
                "3": self.modify_bill,
                "4": self.print_bill,
                "5": self.add_items_to_bill,
                "6": self.invoice_to_fiscal,
                "7": self.render_bill,
                "8": self.choose_file()
            }
            options[option]()
        except OptionError as exp:
            print(exp)

    def print_bill(self):
        bill = self.choose_bill_type()
        try:
            if bill == Invoice:
                print(self.__service.get_invoice(input("Give id: ")))
            if bill == FiscalBill:
                print(self.__service.get_fiscal(input("Give id: ")))
        except Exception as exp:
            print(exp)

    def choose_bill_type(self):
        correct = False
        option = None
        options = {"2": FiscalBill, "1": Invoice}
        while not correct:
            try:
                option = input(
                    "What type of bill database do you want to access? \n1-Invoices or 2-Fiscal Bills"
                )
                self.__validator.option_check(option, 2)
                correct = True
            except Exception as exp:
                print(exp)
        return options[option]

    def create_bill(self):
        try:
            bill_type = self.choose_bill_type()
            bill = self.input_bill(bill_type)
            self.__service.create_bill(bill)
        except Exception as exp:
            print(exp)

    def input_bill(self, bill_type):
        bill = None
        if bill_type == FiscalBill:
            bill = FiscalBill()
        if bill_type == Invoice:
            bill = Invoice()

        bill.set_customer(self.choose_customer(self.choose_customer_type()))
        company = Company()
        bill.set_issuer(self.choose_customer(company))
        bill.set_currency(self.choose_currency())
        bill.set_issue_date(input("Give issue date: "))
        bill.set_due_date(input("Give due date: "))
        bill.set_notes(input("Notes:"))
        return bill

    def delete_bill(self):
        try:
            bill = None
            bill_type = self.choose_bill_type()
            if bill_type == FiscalBill:
                bill = FiscalBill()
            if bill_type == Invoice:
                bill = Invoice()
            bill_id = int(input("Which bill do you wish to delete"))
            bill.set_id(bill_id)
            self.__service.delete_bill(bill)
        except Exception as exp:
            print(exp)

    def modify_bill(self):
        try:
            print("Create the new bill:")
            new_bill = self.input_bill(self.choose_bill_type())
            old_bill = int(input("What bill do you want to change?"))
            self.__service.modify_bill(old_bill, new_bill)
        except Exception as exp:
            print(exp)

    def add_items_to_bill(self):
        bill = None
        bill_type = self.choose_bill_type()
        if bill_type == FiscalBill:
            bill = self.__service.get_fiscal(int(input("Give id: ")))
        if bill_type == Invoice:
            bill = self.__service.get_invoice(int(input("Give id: ")))
        number = int(input("How many items do you wish to add?"))
        while number != 0:
            try:
                self.__service.add_item_to_bill(self.choose_item(), bill)
                number -= 1
            except Exception as exp:
                print(exp)

    def invoice_to_fiscal(self):
        invoice_id = int(
            input("What invoice do you want to choose?\nGive id: "))
        self.__service.invoice_to_fiscal(invoice_id)

    def render_bill(self):
        bill_type = self.choose_bill_type()
        bill = None
        bill_name = None
        if bill_type == FiscalBill:
            bill_name = "fiscal_bill_"
            bill = self.__service.get_fiscal(int(input("Give id: ")))
        if bill_type == Invoice:
            bill_name = "invoice_"
            bill = self.__service.get_invoice(int(input("Give id: ")))
        template = self.choose_template()
        self.__service.render_bill(bill_name, bill, template)

    def choose_file(self):
        files = []
        config = Config.get_instance()
        for file in os.listdir(config.get_template_folder()):
            if os.path.isfile(config.get_template_folder() + file):
                if file.find("_item") == -1:
                    files.append(file)
        index = 1
        if files:
            print("Choose template from:")
            for file in files:

                print(str(index) + "-" + file + "\n")
                index += 1
            choice = int(input())
            try:
                self.__validator.option_check(choice, len(files))
                config.set_template(files[choice - 1])
            except Exception as exp:
                print(xp)

    def choose_template(self):
        template = Config.get_instance().get_bill_template()
        return template

    def run(self):
        while True:
            print("1.Customer menu\n2.Items menu\n3.Currency menu\n"
                  "4.Bill menu\n5.Exit\n")
            option = input()
            try:
                self.__validator.option_check(option, 5)
                options = {
                    "1": self.customer_menu,
                    "2": self.item_menu,
                    "3": self.currency_menu,
                    "4": self.bill_menu,
                    "5": exit
                }
                options[option]()
            except OptionError as exp:
                print(exp)