def get_item_title_sold_last_from_table(table):
    """
    Returns the _title_ of the item that was sold most recently.

    Args:
        table (list of lists): the sales table

    Returns:
        str: the _title_ of the item that was sold most recently.
    """

    filecontent = data_manager.get_table_from_file('sales/sales.csv')
    month = 3
    day = 4
    year = 5
    all_date_list = []

    for line in filecontent:
        date = line[month] + ";" + line[day] + ";" + line[year]
        all_date_list.append(date)
    dates_sorted_ = common.my_sort_(all_date_list)
    latest_date = dates_sorted_[0]
    latest_dates_list = latest_date.split(';')

    item_title_last_sold = 1
    day = 1
    month = 0
    year = 2
    for data in filecontent:
        if latest_dates_list[month] in data and latest_dates_list[
                day] in data and latest_dates_list[year] in data:
            output = data[item_title_last_sold]

    return output
def get_item_id_sold_last_from_table(table):
    """
    Returns the _id_ of the item that was sold most recently.

    Args:
        table (list of lists): the sales table

    Returns:
        str: the _id_ of the item that was sold most recently.
    """
    filecontent = table
    month = 3
    day = 4
    year = 5
    all_date_list = []
    for line in filecontent:
        date = line[month] + ";" + line[day] + ";" + line[year]
        all_date_list.append(date)
    dates_sorted_ = common.my_sort_(all_date_list)
    latest_date = dates_sorted_[0]
    latest_dates_list = latest_date.split(';')

    item_id_sold_last = 0
    day = 1
    month = 0
    year = 2
    for data in filecontent:
        if latest_dates_list[month] in data and latest_dates_list[
                day] in data and latest_dates_list[year] in data:
            output = data[item_id_sold_last]
    ui.print_result(output, 'Last sold item ID is: ')

    return output
def get_item_id_sold_last():
    """
    Reads the table with the help of the data_manager module.
    Returns the _id_ of the item that was sold most recently.

    Returns:
        str: the _id_ of the item that was sold most recently.
    """

    month_column = 3
    day_column = 4
    year_column = 5
    all_date_list = []
    filecontent = data_manager.get_table_from_file('sales/sales.csv')
    for line in filecontent:
        date = line[month_column] + ";" + \
            line[day_column] + ";" + line[year_column]
        all_date_list.append(date)
    dates_sorted_ = common.my_sort_(all_date_list)
    latest_date = dates_sorted_[0]
    latest_dates_list = latest_date.split(';')

    item_id_index = 0
    month = 0
    day = 1
    year = 2
    for data in filecontent:
        if latest_dates_list[month] in data and latest_dates_list[
                day] in data and latest_dates_list[year] in data:
            output = data[item_id_index]
    ui.print_result(output, "Last item's ID is: ")

    return output
def get_lowest_price_item_id(table):
    """
    Question: What is the id of the item that was sold for the lowest price?
    if there are more than one item at the lowest price, return the last item by alphabetical order of the title

    Args:
        table (list): data table to work on

    Returns:
         string: id
    """

    id_column = 0
    name_column = 1
    price_column = 2
    sorted_names = []
    sorted_lowest_table = []
    lowest_price = int(table[0][price_column])
    for line in table:
        sorted_names.append(line[name_column])
        if int(line[price_column]) < lowest_price:
            lowest_price = int(line[price_column])
    sorted_names = common.my_sort_(sorted_names)
    for name in sorted_names:
        for line in table:
            if name == line[name_column] and int(
                    line[price_column]) == lowest_price:
                sorted_lowest_table.append(line)
                break
    if len(sorted_lowest_table) == 1:
        return sorted_lowest_table[0][id_column]
    else:
        return sorted_lowest_table[-1][id_column]
def get_the_last_buyer_id():
    """
    Returns the customer _id_ of the customer made sale last.

    Returns:
        str: Customer id of the last buyer
    """

    filecontent = data_manager.get_table_from_file('sales/sales.csv')
    month = 3
    day = 4
    year = 5
    all_date_list = []

    for line in filecontent:
        date = line[month] + ";" + line[day] + ";" + line[year]
        all_date_list.append(date)
    dates_sorted_ = common.my_sort_(all_date_list)
    latest_date = dates_sorted_[0]
    latest_dates_list = latest_date.split(';')

    costumer_id_index = 6
    day = 1
    month = 0
    year = 2
    for data in filecontent:
        if latest_dates_list[month] in data and latest_dates_list[
                day] in data and latest_dates_list[year] in data:
            output = data[costumer_id_index]

    return output
def get_longest_name_id(table):
    """
        Question: What is the id of the customer with the longest name?

        Args:
            table (list): data table to work on

        Returns:
            string: id of the longest name (if there are more than one, return
                the last by alphabetical order of the names)
        """

    names = common.get_column(table, 1)
    ordered_names = common.my_sort_(names)
    longest_name_len = 0
    longest_name = ""
    for name in ordered_names:
        if longest_name_len <= len(name):
            longest_name_len = len(name)
            longest_name = name
    id_index = 0
    for user in table:
        if longest_name in user:
            return user[id_index]