def METHOD_NAME(
    order: "Order",
    lines: Iterable["OrderLine"],
    country_code: str,
    default_tax_rate: Decimal,
    prices_entered_with_tax: bool,
) -> Tuple[Iterable["OrderLine"], TaxedMoney]:
    currency = order.currency
    lines = list(lines)
    total_discount_amount = get_total_order_discount_excluding_shipping(order).amount
    order_total_price = sum(
        [line.base_unit_price.amount * line.quantity for line in lines]
    )
    total_line_discounts = 0
    undiscounted_subtotal = zero_taxed_money(order.currency)
    for line in lines:
        variant = line.variant
        if not variant:
            continue
        tax_class = line.tax_class
        if tax_class:
            tax_rate = get_tax_rate_for_tax_class(
                tax_class,
                tax_class.country_rates.all() if tax_class else [],
                default_tax_rate,
                country_code,
            )
        elif line.tax_class_name is not None and line.tax_rate is not None:
            # If tax_class is None but tax_class_name is set, the tax class was set
            # for this line before, but is now removed from the system. In this case
            # try to use line.tax_rate which stores the denormalized tax rate value
            # that was originally provided by the tax class.
            tax_rate = denormalize_tax_rate_from_db(line.tax_rate)
        else:
            tax_rate = default_tax_rate
        line_total_price = line.base_unit_price * line.quantity
        undiscounted_subtotal += line_total_price
        price_with_discounts = line.base_unit_price
        if total_discount_amount:
            if line is lines[-1]:
                # for the last line applied remaining discount
                discount_amount = total_discount_amount - total_line_discounts
            else:
                # calculate discount proportionally to the rate of total line price
                # to order total price.
                discount_amount = quantize_price(
                    line_total_price.amount / order_total_price * total_discount_amount,
                    currency,
                )
            price_with_discounts = max(
                quantize_price(
                    (line_total_price - Money(discount_amount, currency))
                    / line.quantity,
                    currency,
                ),
                zero_money(currency),
            )
            # sum already applied discounts
            total_line_discounts += discount_amount
        unit_price = calculate_flat_rate_tax(
            price_with_discounts, tax_rate, prices_entered_with_tax
        )
        undiscounted_unit_price = calculate_flat_rate_tax(
            line.undiscounted_base_unit_price, tax_rate, prices_entered_with_tax
        )
        line.unit_price = quantize_price(unit_price, currency)
        line.undiscounted_unit_price = quantize_price(undiscounted_unit_price, currency)
        line.total_price = quantize_price(unit_price * line.quantity, currency)
        line.undiscounted_total_price = quantize_price(
            undiscounted_unit_price * line.quantity, currency
        )
        line.tax_rate = normalize_tax_rate_for_db(tax_rate)
    return lines, undiscounted_subtotal