Ejemplo n.º 1
0
	def create_company_file_with_two_entries(self, dir):
		company_file_path = join(dir, "company.txt")
		strategy = CompanyStrategy()
		f = open(company_file_path,"w")
		f.write(strategy.get_header_in_file() + linesep)
		f.write("1, Uddevalla Energi, 00543277320043, 5567-2356, Sopavgift" + linesep)
		f.write("2, Telenor,, 885674-3," + linesep)
		f.close()
Ejemplo n.º 2
0
	def test_sort_by_company_id(self):
		# initialize
		strategy = CompanyStrategy()
		strategy.company_list = self.create_company_list_2()
		# test
		strategy.sort_by_company_id()
		# verify
		self.assertEqual(len(strategy.company_list), 3)
		self.assertEqual(strategy.company_list[0].name, "Telenor")
		self.assertEqual(strategy.company_list[1].name, "Uddevalla Energi")
		self.assertEqual(strategy.company_list[2].name, "Bredbbandsbolaget")
		return
Ejemplo n.º 3
0
 def __init__(self):
     self.periodic_invoice_list = []
     self.company_strategy = CompanyStrategy()
     self.dot_invoice_folder_path = None
     self.months = [
         u"januari", u"feruari", u"mars", u"april", u"maj", u"juni",
         u"juli", u"augusti", u"september", u"oktober", u"november",
         u"december"
     ]
     self.frequencies = [
         u"varje månad", u"var annan månad", u"kvartal", u"var 4:e månad",
         u"halvår", u"varje år"
     ]
     return
Ejemplo n.º 4
0
	def test_read_file_where_file_contains_no_entries(self):
		# initialize
		dir = tempfile.mkdtemp()
		self.create_company_file_with_with_no_entries(dir)
		strategy = CompanyStrategy()
		strategy.dot_invoice_folder_path = dir
		# test
		rows = strategy.read_file()
		# verify
		self.assertEqual(len(strategy.company_list), 0)
		self.assertEqual(rows, 1)
		# cleanup
		shutil.rmtree(dir)
		return
Ejemplo n.º 5
0
	def test_get_company_info_where_line_has_four_columns(self):
		# initialize
		expected_id = "1"
		expected_name = "Uddevalla Energi"
		expected_ocr = ""
		expected_giro = "5567-2356"
		line = expected_id + ", " + expected_name + "," + expected_ocr + ", " + expected_giro
		strategy = CompanyStrategy()
		# test
		try:
			result = strategy.get_company_info(1, line)
			# verify
			self.fail("Exception expected.")
		except LineParseError:
			pass
		return
Ejemplo n.º 6
0
	def test_get_company_info_where_ocr_and_comment_are_empty(self):
		# initialize
		expected_id = "1"
		expected_name = "Uddevalla Energi"
		expected_ocr = ""
		expected_giro = "5567-2356"
		expected_comment = ""
		line = expected_id + ", " + expected_name + "," + expected_ocr + ", " + expected_giro + "," + expected_comment
		strategy = CompanyStrategy()
		# test
		result = strategy.get_company_info(1, line)
		# verify
		self.assertEqual(result.id, int(expected_id))
		self.assertEqual(result.name, expected_name)
		self.assertEqual(result.ocr, expected_ocr)
		self.assertEqual(result.giro, expected_giro)
		self.assertEqual(result.comment, expected_comment)
		return
Ejemplo n.º 7
0
	def test_get_company_info_where_all_columns_have_values(self):
		# initialize
		expected_id = "1"
		expected_name = "Uddevalla Energi"
		expected_ocr = "00543277320043"
		expected_giro = "5567-2356"
		expected_comment = "Sopavgift"
		line = expected_id + ", " + expected_name + ", " + expected_ocr + ", " + expected_giro + ", " + expected_comment
		strategy = CompanyStrategy()
		# test
		result = strategy.get_company_info(1, line)
		# verify
		self.assertEqual(result.id, int(expected_id))
		self.assertEqual(result.name, expected_name)
		self.assertEqual(result.ocr, expected_ocr)
		self.assertEqual(result.giro, expected_giro)
		self.assertEqual(result.comment, expected_comment)
		return
Ejemplo n.º 8
0
 def select_strategy(self):
     if self.args.item == "company" or self.args.item == "c":
         self.strategy = CompanyStrategy()
     elif self.args.item == "invoice" or self.args.item == "i":
         self.strategy = InvoiceStrategy()
     elif self.args.item == "periodic-invoice" or self.args.item == "p":
         self.strategy = PeriodicInvoiceStrategy()
     elif self.args.item == "automatic-invoice" or self.args.item == "a":
         self.strategy = AutomaticInvoiceStrategy()
     return
Ejemplo n.º 9
0
	def test_read_file_where_file_contains_two_entries(self):
		# initialize
		dir = tempfile.mkdtemp()
		self.create_company_file_with_two_entries(dir)
		strategy = CompanyStrategy()
		strategy.dot_invoice_folder_path = dir
		# test
		rows = strategy.read_file()
		# verify
		self.assertEqual(len(strategy.company_list), 2)
		self.assertEqual(rows, 3)
		self.assertEqual(strategy.company_list[0].id, 1)
		self.assertEqual(strategy.company_list[0].name, "Uddevalla Energi")
		self.assertEqual(strategy.company_list[0].ocr, "00543277320043")
		self.assertEqual(strategy.company_list[0].giro, "5567-2356")
		self.assertEqual(strategy.company_list[0].comment, "Sopavgift")
		self.assertEqual(strategy.company_list[1].id, 2)
		self.assertEqual(strategy.company_list[1].name, "Telenor")
		self.assertEqual(strategy.company_list[1].ocr, "")
		self.assertEqual(strategy.company_list[1].giro, "885674-3")
		self.assertEqual(strategy.company_list[1].comment, "")
		# cleanup
		shutil.rmtree(dir)
		return
Ejemplo n.º 10
0
 def initialize_default_folders_and_files(self, home_path):
     return_message = []
     if not exists(home_path):
         return_message.append("HOME_PATH_DOES_NOT_EXIST")
         return return_message
     return_message.append("HOME_PATH_DOES_EXIST")
     self.dot_invoice_folder_path = join(home_path, ".invoice")
     if not exists(self.dot_invoice_folder_path):
         mkdir(self.dot_invoice_folder_path)
         return_message.append("DOT_INVOICE_FOLDER_CREATED")
     invoice_file_path = join(self.dot_invoice_folder_path, "invoice.txt")
     if not exists(invoice_file_path):
         strategy = InvoiceStrategy()
         f = codecs.open(invoice_file_path, encoding='utf-8', mode='w')
         f.write(strategy.get_header_in_file() + linesep)
         f.close()
         return_message.append("INVOICE_FILE_CREATED")
     copmany_file_path = join(self.dot_invoice_folder_path, "company.txt")
     if not exists(copmany_file_path):
         strategy = CompanyStrategy()
         f = codecs.open(copmany_file_path, encoding='utf-8', mode='w')
         f.write(strategy.get_header_in_file() + linesep)
         f.close()
         return_message.append("COMPANY_FILE_CREATED")
     automatic_invoice_file_path = join(self.dot_invoice_folder_path,
                                        "automatic_invoice.txt")
     if not exists(automatic_invoice_file_path):
         strategy = AutomaticInvoiceStrategy()
         f = codecs.open(automatic_invoice_file_path,
                         encoding='utf-8',
                         mode='w')
         f.write(
             strategy.get_header_in_file() + linesep
         )  # Mind that 'pay day' is on a day in the month. Not date!
         f.close()
         return_message.append("AUTOMATIC_INVOICE_FILE_CREATED")
     periodic_invoice_file_path = join(self.dot_invoice_folder_path,
                                       "periodic_invoice.txt")
     if not exists(periodic_invoice_file_path):
         strategy = PeriodicInvoiceStrategy()
         f = codecs.open(periodic_invoice_file_path,
                         encoding='utf-8',
                         mode='w')
         f.write(strategy.get_header_in_file() + linesep)
         f.close()
         return_message.append("PERIODIC_INVOICE_FILE_CREATED")
     return return_message
Ejemplo n.º 11
0
class PeriodicInvoiceStrategy:
    def __init__(self):
        self.periodic_invoice_list = []
        self.company_strategy = CompanyStrategy()
        self.dot_invoice_folder_path = None
        self.months = [
            u"januari", u"feruari", u"mars", u"april", u"maj", u"juni",
            u"juli", u"augusti", u"september", u"oktober", u"november",
            u"december"
        ]
        self.frequencies = [
            u"varje månad", u"var annan månad", u"kvartal", u"var 4:e månad",
            u"halvår", u"varje år"
        ]
        return

    #
    # periodic_invoice.txt
    #   Contains all invoices that occurs in periodic frequency.
    #
    #   id - unique integer number, starting from 1 and increasing by row. Mandatory. Set automaticaly by the command 'add'. Not by user input.
    #   company name ref - Company name reference. Mandatory
    #   amount - the amount in SEK. Mandatory
    #   start date - the start date when this invoice will be valid. If omitted, periodic invoice is valid until end date. Optional
    #   end date - the end date when this invoice will be invalid. If omitted, periodic invoice is valid from start date until forever. Optional
    #   frequency - montly, every second month, quartly, every 4:th month, half year, yearly. Mandatory
    #   start month - which month period starts each year. Integer. January starts at 1. Mandatory
    #   comment - Optional
    #
    def get_header_in_file(self):
        return u"id, company name ref, amount, start date, end date, frequency, start month, comment"

    def do_strategy(self, action, dot_invoice_folder_path):
        self.dot_invoice_folder_path = dot_invoice_folder_path
        self.read_file()
        # Read all companies and put them in self.company_list.
        self.company_strategy.dot_invoice_folder_path = dot_invoice_folder_path
        self.company_strategy.read_file()

        if action == "list" or action == "l":
            self.list_periodic_invoices()
        elif action == "add" or action == "a":
            self.add_periodic_invoice()
        elif action == "change" or action == "c":
            print "Not yet implemented"
            pass
        elif action == "delete" or action == "d":
            print "It is not possible to delete a company information."
            pass
        return

    def list_periodic_invoices(self):
        column_widths = self.calculate_column_widths()
        title = "{:<5}   {:<" + str(column_widths[0]) + "}   {:<" + str(
            column_widths[1]) + "}   {:<" + str(
                column_widths[2]) + "}   {:<" + str(
                    column_widths[3]) + "}   {:<" + str(
                        column_widths[4]) + "}   {:<" + str(
                            column_widths[5]) + "}   {:<" + str(
                                column_widths[6]) + "}"
        print title.format("Index", "Company name", "Amount", "Start date",
                           "End date", "Frequency", "Start month", "Comment")
        index = 1
        row = u"{:>4}    {:<" + str(column_widths[0]) + u"}   {:<" + str(
            column_widths[1]) + u"}   {:<" + str(
                column_widths[2]) + u"}   {:<" + str(
                    column_widths[3]) + u"}   {:<" + str(
                        column_widths[4]) + u"}   {:<" + str(
                            column_widths[5]) + u"}   {:<" + str(
                                column_widths[6]) + u"}"
        for periodic_invoice_info in self.periodic_invoice_list:
            print row.format(
                index,
                self.company_strategy.get_company_info_given_id_ref(
                    periodic_invoice_info.name_ref).name[0:30],
                periodic_invoice_info.amount[0:30],
                periodic_invoice_info.start_date[0:30],
                periodic_invoice_info.end_date[0:30],
                self.frequencies[periodic_invoice_info.frequency_index][0:30],
                self.months[periodic_invoice_info.start_month_index][0:30],
                periodic_invoice_info.comment[0:30])
            index = index + 1
        return

    def add_periodic_invoice(self):
        next_id = 1
        if len(self.periodic_invoice_list) > 0:
            next_id = self.periodic_invoice_list[-1].id + 1
        print "Add periodic invoice information"
        print "--------------------------------"
        print "You will be asked questions about: Company name, amount, start date, end date, frequency, start month and comment."
        print "Company name, frequency and start month are mandatory. The others are optional."
        answer = raw_input(
            "Do you want to list current stored peridoic invoice information? [y/n/q = quit] "
        )
        if answer == "y" or answer == "Y":
            self.list_periodic_invoices()
            print ""
        elif answer == "q" or answer == "Q":
            sys.exit(0)
        print "Stored companies:"
        self.company_strategy.list_companies()
        answer = raw_input("Index for company name ? Example: '5' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        index = int(answer)
        company_info = self.company_strategy.company_list[index - 1]
        company_name_ref = company_info.id
        print "'" + company_info.name + "' was selected."
        answer = raw_input(
            "Amount in SEK (Optional)? Example: '359' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        amount = unicode(answer.decode('utf-8'))
        answer = raw_input(
            "Start date (Optional)? Example: '2017-06-01' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        start_date = unicode(answer.decode('utf-8'))
        answer = raw_input(
            "End date (Optional)? Example: '2019-12-31' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        end_date = unicode(answer.decode('utf-8'))
        print "Index Frequency"
        index = 1
        for frequency in self.frequencies:
            print "  " + str(index) + "   " + frequency
            index = index + 1
        answer = raw_input(
            "Frequency index: (Mandatory)? Example: '3' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        frequency_index = int(answer) - 1
        print "'" + self.frequencies[frequency_index] + "' was selected."
        print "Index Months"
        index = 1
        for month in self.months:
            print "  " + str(index) + "   " + month
            index = index + 1
        answer = raw_input(
            "Start month index (Mandatory)? Example: '5' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        start_month_index = int(answer) - 1
        print "'" + self.months[start_month_index] + "' was selected."
        answer = raw_input(
            "Comment (Optinal)? Example: 'Important' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        comment = unicode(answer.decode('utf-8'))
        answer = raw_input(
            "Do you want to save this company information? [y/n/] ")
        if answer == "n" or answer == "N":
            sys.exit(0)
        periodic_invoice_info = PeriodicInvoiceInfo()
        periodic_invoice_info.id = next_id
        periodic_invoice_info.name_ref = company_name_ref
        periodic_invoice_info.amount = amount
        periodic_invoice_info.start_date = start_date
        periodic_invoice_info.end_date = end_date
        periodic_invoice_info.frequency_index = frequency_index
        periodic_invoice_info.start_month_index = start_month_index
        periodic_invoice_info.comment = comment
        self.periodic_invoice_list.append(periodic_invoice_info)
        self.save_periodic_invoice_list_to_file()
        return

    #
    # The period_invoice_strategy.txt file contains
    # Header: id, company name ref, amount, start date, end date, frequency, start month, comment
    #
    # id. An integer. May not be empty.
    # company name ref contains an integer. May not be empty
    # amount contains stirng. May be empty
    # start date contains string. May not be empty
    # end date contains string. May not be empty
    # frquency contains an integer. May not be empty.
    # start month contains an integer. May not be empty.
    # comment contains string. May be empty
    #
    def read_file(self):
        periodic_invoice_file_path = join(self.dot_invoice_folder_path,
                                          "periodic_invoice.txt")
        file = codecs.open(periodic_invoice_file_path,
                           encoding='utf-8',
                           mode='r')
        index = 0
        for line in file:
            if (len(line.strip())) == 0:
                continue
            if index > 0:
                periodic_invoice_info = self.get_periodic_invoice_info(
                    index, line)
                self.periodic_invoice_list.append(periodic_invoice_info)
            index = index + 1
        file.close()
        return index

    def save_periodic_invoice_list_to_file(self):
        periodic_invoice_file_path = join(self.dot_invoice_folder_path,
                                          "periodic_invoice.txt")
        f = codecs.open(periodic_invoice_file_path, encoding='utf-8', mode='w')
        f.write(self.get_header_in_file() + linesep)
        for periodic_invoice_info in self.periodic_invoice_list:
            f.write(
                str(periodic_invoice_info.id) + u", " +
                str(periodic_invoice_info.name_ref) + u", " +
                periodic_invoice_info.amount + u", " +
                periodic_invoice_info.start_date + u", " +
                periodic_invoice_info.end_date + u", " +
                str(periodic_invoice_info.frequency_index) + u", " +
                str(periodic_invoice_info.start_month_index) + u", " +
                periodic_invoice_info.comment + linesep)
        f.close()
        return

    def get_periodic_invoice_info(self, index, line):
        words = line.split(",")
        if len(words) != 8:
            raise LineParseError(
                "Error at line: " + str(index) +
                ", in periodic_invoice.txt file. Line must have 8 columns. Line has: "
                + str(len(words)) + ", columns.")
        periodic_invoice_info = PeriodicInvoiceInfo()
        periodic_invoice_info.id = int(words[0].strip())
        periodic_invoice_info.name_ref = int(words[1].strip())
        periodic_invoice_info.amount = words[2].strip()
        periodic_invoice_info.start_date = words[3].strip()
        periodic_invoice_info.end_date = words[4].strip()
        periodic_invoice_info.frequency_index = int(words[5].strip())
        periodic_invoice_info.start_month_index = int(words[6].strip())
        periodic_invoice_info.comment = words[7].strip()
        return periodic_invoice_info

    # company name, amount, start date, end date, frequency, start month, comment
    def calculate_column_widths(self):
        column_widths = [12, 6, 10, 8, 9, 11, 7]
        for periodic_invoice_info in self.periodic_invoice_list:
            if len(
                    self.company_strategy.get_company_info_given_id_ref(
                        periodic_invoice_info.name_ref).name
            ) > column_widths[0]:
                column_widths[0] = len(
                    self.company_strategy.get_company_info_given_id_ref(
                        periodic_invoice_info.name_ref).name)
            if len(periodic_invoice_info.start_date) > column_widths[1]:
                column_widths[1] = len(periodic_invoice_info.start_date)
            if len(periodic_invoice_info.start_date) > column_widths[2]:
                column_widths[2] = len(periodic_invoice_info.start_date)
            if len(periodic_invoice_info.end_date) > column_widths[3]:
                column_widths[3] = len(periodic_invoice_info.end_date)
            if len(self.frequencies[
                    periodic_invoice_info.frequency_index]) > column_widths[4]:
                column_widths[4] = len(
                    self.frequencies[periodic_invoice_info.frequency_index])
            if len(self.months[periodic_invoice_info.start_month_index]
                   ) > column_widths[5]:
                column_widths[5] = len(
                    self.months[periodic_invoice_info.start_month_index])
            if len(periodic_invoice_info.comment) > column_widths[6]:
                column_widths[6] = len(periodic_invoice_info.comment)
        index = 0
        for length in column_widths:
            if length > 30:
                column_widths[index] = 30
            index = index + 1
        return column_widths

    def get_company_info_given_id_ref(self, id):
        for periodic_invoice_info in self.periodic_invoice_list:
            if periodic_invoice_info.id == id:
                company_info = self.company_strategy.get_company_info_given_id_ref(
                    periodic_invoice_info.name_ref)
                return company_info
        return 0
Ejemplo n.º 12
0
class InvoiceStrategy:
    def __init__(self):
        self.invoice_list = []
        self.company_strategy = CompanyStrategy()
        self.periodic_invoice_strategy = PeriodicInvoiceStrategy()
        self.dot_invoice_folder_path = None
        self.months = [
            u"januari", u"feruari", u"mars", u"april", u"maj", u"juni",
            u"juli", u"augusti", u"september", u"oktober", u"november",
            u"december"
        ]
        self.frequencies = [
            u"varje månad", u"var annan månad", u"kvartal", u"var 4:e månad",
            u"halvår", u"varje år"
        ]
        return

    def get_header_in_file(self):
        return u"company name ref, periodic invoice ref, pay date, amount, ocr, giro, comment"

    def do_strategy(self, action, dot_invoice_folder_path):
        self.dot_invoice_folder_path = dot_invoice_folder_path
        self.read_file()
        # Read all companies.
        self.company_strategy.dot_invoice_folder_path = dot_invoice_folder_path
        self.company_strategy.read_file()
        # Read all periodic invoices.
        self.periodic_invoice_strategy.dot_invoice_folder_path = dot_invoice_folder_path
        self.periodic_invoice_strategy.read_file()
        self.periodic_invoice_strategy.company_strategy.dot_invoice_folder_path = dot_invoice_folder_path
        self.periodic_invoice_strategy.company_strategy.read_file()

        if action == "list" or action == "l":
            self.list_invoices()
        elif action == "add" or action == "a":
            self.add_invoice()
        elif action == "change" or action == "c":
            print "Not yet implemented"
            pass
        elif action == "delete" or action == "d":
            print "It is not possible to delete a company information."
            pass
        return

    def list_invoices(self):
        column_widths = self.calculate_column_widths()
        title = "  {:<" + str(column_widths[0]) + "}   {:<" + str(
            column_widths[1]) + "}   {:<" + str(
                column_widths[2]) + "}   {:<" + str(
                    column_widths[3]) + "}   {:<" + str(
                        column_widths[4]) + "}   {:<" + str(
                            column_widths[5]) + "}"
        print title.format("Company name", "Pay date", "Amount", "OCR", "Giro",
                           "Comment")
        row = u"  {:<" + str(column_widths[0]) + u"}   {:<" + str(
            column_widths[1]) + u"}   {:<" + str(
                column_widths[2]) + u"}   {:<" + str(
                    column_widths[3]) + u"}   {:<" + str(
                        column_widths[4]) + u"}   {:<" + str(
                            column_widths[5]) + u"}"
        for invoice_info in self.invoice_list:
            company_name = ""
            if (invoice_info.name_ref >= 0):
                company_name = self.company_strategy.get_company_info_given_id_ref(
                    invoice_info.name_ref).name
            else:
                company_name = self.periodic_invoice_strategy.get_company_info_given_id_ref(
                    invoice_info.periodic_invoice_ref).name
            print row.format(company_name[0:30], invoice_info.pay_date[0:30],
                             invoice_info.amount[0:30], invoice_info.ocr[0:30],
                             invoice_info.giro[0:30],
                             invoice_info.comment[0:30])
        return

    def add_invoice(self):
        next_id = 1
        if len(self.invoice_list) > 0:
            next_id = self.invoice_list[
                -1].id + 1  # TODO: Search the whole list and find the highest. Add one to that.
        print "Add invoice information"
        print "-----------------------"
        print "You will be asked questions about: Company name, pay date, amount, OCR number, GIRO number and a comment."
        answer = raw_input(
            "Is this invoice a periodic invoice or an ordinary invoice? [p/o/q = quit] "
        )
        name_ref = -1
        periodic_invoice_ref = -1
        ocr = ""
        giro = ""
        pay_date = date.today().isoformat()
        if answer == "q" or answer == "Q":
            sys.exit(0)
        elif answer == "p" or answer == "P":
            print "These are the pariodic invoices:"
            self.periodic_invoice_strategy.list_periodic_invoices()

            answer = raw_input(
                "Index for pariodic invoice? Example: '5' [q = quit] ")
            if answer == "q" or answer == "Q":
                sys.exit(0)
            index = int(answer)
            periodic_invoice_info = self.periodic_invoice_strategy.periodic_invoice_list[
                index - 1]
            periodic_invoice_ref = periodic_invoice_info.id
            print "'" + self.company_strategy.get_company_info_given_id_ref(
                periodic_invoice_info.name_ref).name + "' was selected."
            ocr = self.company_strategy.get_company_info_given_id_ref(
                periodic_invoice_info.name_ref).ocr
            giro = self.company_strategy.get_company_info_given_id_ref(
                periodic_invoice_info.name_ref).giro
        else:
            # answer is equal 'o' or 'O'
            print "These are the companies stored:"
            self.company_strategy.list_companies()

            answer = raw_input("Index for companies? Example: '2' [q = quit] ")
            if answer == "q" or answer == "Q":
                sys.exit(0)
            index = int(answer)
            company_info = self.company_strategy.company_list[index - 1]
            name_ref = company_info.id
            print "'" + self.company_strategy.get_company_info_given_id_ref(
                name_ref).name + "' was selected."
        answer = raw_input("Do you want to use todays date? [y/n/q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        if answer == "n" or answer == "N":
            answer = raw_input(
                "Enter a date in format YYYY-mm-dd. Example: '2019-06-25' (25:th of June 2019). No check is made that the date is valid. [q = quit] "
            )
            if answer == "q" or answer == "Q":
                sys.exit(0)
            pay_date = unicode(answer.decode('utf-8'))
        answer = raw_input("Enter amount in SEK? Example: '349' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        amount = unicode(answer.decode('utf-8'))
        if len(ocr) > 0:
            answer = raw_input(
                "OCR number (Mandatory)? Example: '0045321223' [q = quit]. Default is "
                + ocr + ". Just pressing enter selects default ")
            if answer == "q" or answer == "Q":
                sys.exit(0)
            if len(answer) > 0:
                ocr = unicode(answer.decode('utf-8'))
        else:
            answer = raw_input(
                "OCR number (Mandatory)? Example: '0045321223' [q = quit] ")
            if answer == "q" or answer == "Q":
                sys.exit(0)
            ocr = unicode(answer.decode('utf-8'))
        if len(giro) > 0:
            answer = raw_input(
                "Giro number (Mandatory)? Example: '5564-2344' [q = quit]. Default is "
                + giro + ". Just pressing enter selects default ")
            if answer == "q" or answer == "Q":
                sys.exit(0)
            if len(answer) > 0:
                giro = unicode(answer.decode('utf-8'))
        else:
            answer = raw_input(
                "Giro number (Mandatory)? Example: '5564-2344' [q = quit] ")
            if answer == "q" or answer == "Q":
                sys.exit(0)
            giro = unicode(answer.decode('utf-8'))
        answer = raw_input(
            "Comment (Optional)? Example: 'Bredbandsfaktura' [q = quit] ")
        if answer == "q" or answer == "Q":
            sys.exit(0)
        comment = unicode(answer.decode('utf-8'))
        answer = raw_input(
            "Do you want to save this company information? [y/n/] ")
        if answer == "n" or answer == "N":
            sys.exit(0)
        invoice_info = InvoiceInfo()
        invoice_info.id = next_id
        invoice_info.name_ref = name_ref
        invoice_info.periodic_invoice_ref = periodic_invoice_ref
        invoice_info.pay_date = pay_date
        invoice_info.amount = amount
        invoice_info.ocr = ocr
        invoice_info.giro = giro
        invoice_info.comment = comment
        self.invoice_list.append(invoice_info)
        self.save_invoice_list_to_file()
        return

    #
    # invoice.txt
    #   Contains all payed invoices, but the automatic invoices.
    #
    #   id. An integer. May not be empty.
    #   company name ref - Referece number to company. Integer. Optional.
    #   periodic invoice ref - Reference to periodic_invoice. Integer. Optional.
    #   pay date - the date when the invoice is payed. Mandatory
    #   amount - the amount in SEK. If periodic invoice contains amount, this value is optional. If present, it overrides periodic invoice amount. If perioduc invoice is not present, this is mandatory.
    #   ocr - OCR number. If company contains ocr number, this is optional. If present, it overrides compant ocr. If company does not have ocr, then a value in this ocr is mandatory.
    #   comment - Optional
    #
    #   Note: When listing invoices, use the giro from company. Same for ocr. If ocr is empty, use ocr from company.
    #         One of Compant name ref and Periodic Invoice Ref is mandatory. Not both.
    #         When listing invoices, add automatic invoices to the list.
    #
    def read_file(self):
        invoice_file_path = join(self.dot_invoice_folder_path, "invoice.txt")
        file = codecs.open(invoice_file_path, encoding='utf-8', mode='r')
        index = 0
        for line in file:
            if (len(line.strip())) == 0:
                continue
            if index > 0:
                invoice_info = self.get_invoice_info(index, line)
                self.invoice_list.append(invoice_info)
            index = index + 1
        file.close()
        return index

    def save_invoice_list_to_file(self):
        invoice_file_path = join(self.dot_invoice_folder_path, "invoice.txt")
        f = codecs.open(invoice_file_path, encoding='utf-8', mode='w')
        f.write(self.get_header_in_file() + linesep)
        for invoice_info in self.invoice_list:
            f.write(
                str(invoice_info.id) + u", " + str(invoice_info.name_ref) +
                u", " + str(invoice_info.periodic_invoice_ref) + u", " +
                invoice_info.pay_date + u", " + invoice_info.amount + u", " +
                invoice_info.ocr + u", " + invoice_info.giro + u", " +
                invoice_info.comment + linesep)
        f.close()
        return

    def get_invoice_info(self, index, line):
        words = line.split(",")
        if len(words) != 8:
            raise LineParseError(
                "Error at line: " + str(index) +
                ", in invoice.txt file. Line must have 8 columns. Line has: " +
                str(len(words)) + ", columns.")
        invoice_info = InvoiceInfo()
        invoice_info.id = int(words[0].strip())
        invoice_info.name_ref = int(words[1].strip())
        invoice_info.periodic_invoice_ref = int(words[2].strip())
        invoice_info.pay_date = words[3].strip()
        invoice_info.amount = words[4].strip()
        invoice_info.ocr = words[5].strip()
        invoice_info.giro = words[6].strip()
        invoice_info.comment = words[7].strip()
        return invoice_info

    # company name ref, periodic invoice ref, pay date, amount, ocr, comment
    def calculate_column_widths(self):
        column_widths = [12, 8, 6, 3, 4, 7]
        for invoice_info in self.invoice_list:
            if invoice_info.name_ref >= 0:
                if len(
                        self.company_strategy.get_company_info_given_id_ref(
                            invoice_info.name_ref).name) > column_widths[0]:
                    column_widths[0] = len(
                        self.company_strategy.get_company_info_given_id_ref(
                            invoice_info.name_ref).name)
            if invoice_info.periodic_invoice_ref >= 0:
                if len(
                        self.periodic_invoice_strategy.
                        get_company_info_given_id_ref(
                            invoice_info.periodic_invoice_ref).name
                ) > column_widths[0]:
                    column_widths[0] = len(
                        self.periodic_invoice_strategy.
                        get_company_info_given_id_ref(
                            invoice_info.periodic_invoice_ref).name)
            if len(invoice_info.pay_date) > column_widths[1]:
                column_widths[1] = len(invoice_info.pay_date)
            if len(invoice_info.amount) > column_widths[2]:
                column_widths[2] = len(invoice_info.amount)
            if len(invoice_info.ocr) > column_widths[3]:
                column_widths[3] = len(invoice_info.ocr)
            if len(invoice_info.giro) > column_widths[4]:
                column_widths[4] = len(invoice_info.giro)
            if len(invoice_info.comment) > column_widths[5]:
                column_widths[5] = len(invoice_info.comment)
        index = 0
        for length in column_widths:
            if length > 30:
                column_widths[index] = 30
            index = index + 1
        return column_widths
Ejemplo n.º 13
0
	def create_company_file_with_with_no_entries(self, dir):
		company_file_path = join(dir, "company.txt")
		strategy = CompanyStrategy()
		f = open(company_file_path,"w")
		f.write(strategy.get_header_in_file() + linesep)
		f.close()