We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Bug: Draft order uses incorrect tax rate for lines on changing order's country
### What are you trying to achieve?

Create a draft order with taxes for one country, and changing the country to another one, and have the taxes correctly recalculated with a new rate.

### Steps to reproduce the problem

The easiest way to recreate the issue is be using Saleor Dashboard:
1. Create channel A with the default country e.g. Germany.
2. Enable flat rates in channel A; create a tax class; configure tax rates for Germany by setting some value for the tax class, and leaving the "Country default rate" empty.
3. Add tax configuration for some other country, e.g. Poland, and provide tax rates for both the tax class as well as the "Country default rate".
4. Create a draft order without an address in channel A.
5. Add an order line with a product without a tax class (also product type cannot have a tax class). Taxes should be 0, since there is no default country rate or tax class.
6. Set order addresses to some valid addresses from Poland. Default rate for Poland is set and Saleor should recalculate taxes with the Polish rate. Currently, it doesn't happen and taxes stay 0.

### What did you expect to happen?

Taxes should be correctly recalculated when changing addresses in draft order.

### Logs

_No response_

### Environment

Saleor version: 3.12.22


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `saleor/tax/calculations/order.py`
Content:
```
1 from decimal import Decimal
2 from typing import TYPE_CHECKING, Iterable, Tuple
3 
4 from prices import Money, TaxedMoney
5 
6 from ...core.prices import quantize_price
7 from ...core.taxes import zero_money, zero_taxed_money
8 from ...discount import OrderDiscountType
9 from ...order import base_calculations
10 from ...order.utils import (
11     get_order_country,
12     get_total_order_discount_excluding_shipping,
13 )
14 from ..models import TaxClassCountryRate
15 from ..utils import (
16     denormalize_tax_rate_from_db,
17     get_tax_rate_for_tax_class,
18     normalize_tax_rate_for_db,
19 )
20 from . import calculate_flat_rate_tax
21 
22 if TYPE_CHECKING:
23     from ...order.models import Order, OrderLine
24 
25 
26 def update_order_prices_with_flat_rates(
27     order: "Order",
28     lines: Iterable["OrderLine"],
29     prices_entered_with_tax: bool,
30 ):
31     country_code = get_order_country(order)
32     default_country_rate_obj = TaxClassCountryRate.objects.filter(
33         country=country_code, tax_class=None
34     ).first()
35     default_tax_rate = (
36         default_country_rate_obj.rate if default_country_rate_obj else Decimal(0)
37     )
38 
39     # Calculate order line totals.
40     _, undiscounted_subtotal = update_taxes_for_order_lines(
41         order, lines, country_code, default_tax_rate, prices_entered_with_tax
42     )
43 
44     # Calculate order shipping.
45     shipping_method = order.shipping_method
46     shipping_tax_class = getattr(shipping_method, "tax_class", None)
47     if shipping_tax_class:
48         shipping_tax_rate = get_tax_rate_for_tax_class(
49             shipping_tax_class,
50             shipping_tax_class.country_rates.all(),
51             default_tax_rate,
52             country_code,
53         )
54     elif order.shipping_tax_rate is not None:
55         # Use order.shipping_tax_rate if it was ever set before (it's non-null now).
56         # This is a valid case when recalculating shipping price and the tax class is
57         # null, because it was removed from the system.
58         shipping_tax_rate = denormalize_tax_rate_from_db(order.shipping_tax_rate)
59     else:
60         shipping_tax_rate = default_tax_rate
61 
62     order.shipping_price = _calculate_order_shipping(
63         order, shipping_tax_rate, prices_entered_with_tax
64     )
65     order.shipping_tax_rate = normalize_tax_rate_for_db(shipping_tax_rate)
66 
67     # Calculate order total.
68     order.undiscounted_total = undiscounted_subtotal + order.base_shipping_price
69     order.total = _calculate_order_total(order, lines)
70 
71 
72 def _calculate_order_total(
73     order: "Order",
74     lines: Iterable["OrderLine"],
75 ) -> TaxedMoney:
76     currency = order.currency
77 
78     default_value = base_calculations.base_order_total(order, lines)
79     default_value = TaxedMoney(default_value, default_value)
80     if default_value <= zero_taxed_money(currency):
81         return quantize_price(default_value, currency)
82 
83     total = zero_taxed_money(currency)
84     undiscounted_subtotal = zero_taxed_money(currency)
85     for line in lines:
86         total += line.total_price
87         undiscounted_subtotal += line.undiscounted_total_price
88     total += order.shipping_price
89 
90     order_discount = order.discounts.filter(type=OrderDiscountType.MANUAL).first()
91     if order_discount and order_discount.amount > undiscounted_subtotal.gross:
92         remaining_amount = order_discount.amount - undiscounted_subtotal.gross
93         total -= remaining_amount
94     return quantize_price(max(total, zero_taxed_money(currency)), currency)
95 
96 
97 def _calculate_order_shipping(
98     order: "Order", tax_rate: Decimal, prices_entered_with_tax: bool
99 ) -> TaxedMoney:
100     shipping_price = order.base_shipping_price
101     taxed_shipping_price = calculate_flat_rate_tax(
102         shipping_price, tax_rate, prices_entered_with_tax
103     )
104     return quantize_price(taxed_shipping_price, taxed_shipping_price.currency)
105 
106 
107 def update_taxes_for_order_lines(
108     order: "Order",
109     lines: Iterable["OrderLine"],
110     country_code: str,
111     default_tax_rate: Decimal,
112     prices_entered_with_tax: bool,
113 ) -> Tuple[Iterable["OrderLine"], TaxedMoney]:
114     currency = order.currency
115     lines = list(lines)
116 
117     total_discount_amount = get_total_order_discount_excluding_shipping(order).amount
118     order_total_price = sum(
119         [line.base_unit_price.amount * line.quantity for line in lines]
120     )
121     total_line_discounts = 0
122 
123     undiscounted_subtotal = zero_taxed_money(order.currency)
124 
125     for line in lines:
126         variant = line.variant
127         if not variant:
128             continue
129 
130         tax_class = line.tax_class
131         if tax_class:
132             tax_rate = get_tax_rate_for_tax_class(
133                 tax_class,
134                 tax_class.country_rates.all() if tax_class else [],
135                 default_tax_rate,
136                 country_code,
137             )
138         elif line.tax_rate is not None:
139             # line.tax_class can be None when the tax class was removed from DB. In
140             # this case try to use line.tax_rate which stores the denormalized tax rate
141             # value that was originally used.
142             tax_rate = denormalize_tax_rate_from_db(line.tax_rate)
143         else:
144             tax_rate = default_tax_rate
145 
146         line_total_price = line.base_unit_price * line.quantity
147         undiscounted_subtotal += line_total_price
148 
149         price_with_discounts = line.base_unit_price
150         if total_discount_amount:
151             if line is lines[-1]:
152                 # for the last line applied remaining discount
153                 discount_amount = total_discount_amount - total_line_discounts
154             else:
155                 # calculate discount proportionally to the rate of total line price
156                 # to order total price.
157                 discount_amount = quantize_price(
158                     line_total_price.amount / order_total_price * total_discount_amount,
159                     currency,
160                 )
161             price_with_discounts = max(
162                 quantize_price(
163                     (line_total_price - Money(discount_amount, currency))
164                     / line.quantity,
165                     currency,
166                 ),
167                 zero_money(currency),
168             )
169             # sum already applied discounts
170             total_line_discounts += discount_amount
171 
172         unit_price = calculate_flat_rate_tax(
173             price_with_discounts, tax_rate, prices_entered_with_tax
174         )
175         undiscounted_unit_price = calculate_flat_rate_tax(
176             line.undiscounted_base_unit_price, tax_rate, prices_entered_with_tax
177         )
178 
179         line.unit_price = quantize_price(unit_price, currency)
180         line.undiscounted_unit_price = quantize_price(undiscounted_unit_price, currency)
181 
182         line.total_price = quantize_price(unit_price * line.quantity, currency)
183         line.undiscounted_total_price = quantize_price(
184             undiscounted_unit_price * line.quantity, currency
185         )
186         line.tax_rate = normalize_tax_rate_for_db(tax_rate)
187 
188     return lines, undiscounted_subtotal
189
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/saleor/tax/calculations/order.py b/saleor/tax/calculations/order.py
--- a/saleor/tax/calculations/order.py
+++ b/saleor/tax/calculations/order.py
@@ -51,10 +51,13 @@
             default_tax_rate,
             country_code,
         )
-    elif order.shipping_tax_rate is not None:
-        # Use order.shipping_tax_rate if it was ever set before (it's non-null now).
-        # This is a valid case when recalculating shipping price and the tax class is
-        # null, because it was removed from the system.
+    elif (
+        order.shipping_tax_class_name is not None
+        and order.shipping_tax_rate is not None
+    ):
+        # Use order.shipping_tax_rate if it was ever set before (it's non-null now and
+        # the name is non-null). This is a valid case when recalculating shipping price
+        # and the tax class is null, because it was removed from the system.
         shipping_tax_rate = denormalize_tax_rate_from_db(order.shipping_tax_rate)
     else:
         shipping_tax_rate = default_tax_rate
@@ -135,10 +138,11 @@
                 default_tax_rate,
                 country_code,
             )
-        elif line.tax_rate is not None:
-            # line.tax_class can be None when the tax class was removed from DB. In
-            # this case try to use line.tax_rate which stores the denormalized tax rate
-            # value that was originally used.
+        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

{"golden_diff": "diff --git a/saleor/tax/calculations/order.py b/saleor/tax/calculations/order.py\n--- a/saleor/tax/calculations/order.py\n+++ b/saleor/tax/calculations/order.py\n@@ -51,10 +51,13 @@\n             default_tax_rate,\n             country_code,\n         )\n-    elif order.shipping_tax_rate is not None:\n-        # Use order.shipping_tax_rate if it was ever set before (it's non-null now).\n-        # This is a valid case when recalculating shipping price and the tax class is\n-        # null, because it was removed from the system.\n+    elif (\n+        order.shipping_tax_class_name is not None\n+        and order.shipping_tax_rate is not None\n+    ):\n+        # Use order.shipping_tax_rate if it was ever set before (it's non-null now and\n+        # the name is non-null). This is a valid case when recalculating shipping price\n+        # and the tax class is null, because it was removed from the system.\n         shipping_tax_rate = denormalize_tax_rate_from_db(order.shipping_tax_rate)\n     else:\n         shipping_tax_rate = default_tax_rate\n@@ -135,10 +138,11 @@\n                 default_tax_rate,\n                 country_code,\n             )\n-        elif line.tax_rate is not None:\n-            # line.tax_class can be None when the tax class was removed from DB. In\n-            # this case try to use line.tax_rate which stores the denormalized tax rate\n-            # value that was originally used.\n+        elif line.tax_class_name is not None and line.tax_rate is not None:\n+            # If tax_class is None but tax_class_name is set, the tax class was set\n+            # for this line before, but is now removed from the system. In this case\n+            # try to use line.tax_rate which stores the denormalized tax rate value\n+            # that was originally provided by the tax class.\n             tax_rate = denormalize_tax_rate_from_db(line.tax_rate)\n         else:\n             tax_rate = default_tax_rate\n", "issue": "Bug: Draft order uses incorrect tax rate for lines on changing order's country\n### What are you trying to achieve?\n\nCreate a draft order with taxes for one country, and changing the country to another one, and have the taxes correctly recalculated with a new rate.\n\n### Steps to reproduce the problem\n\nThe easiest way to recreate the issue is be using Saleor Dashboard:\r\n1. Create channel A with the default country e.g. Germany.\r\n2. Enable flat rates in channel A; create a tax class; configure tax rates for Germany by setting some value for the tax class, and leaving the \"Country default rate\" empty.\r\n3. Add tax configuration for some other country, e.g. Poland, and provide tax rates for both the tax class as well as the \"Country default rate\".\r\n4. Create a draft order without an address in channel A.\r\n5. Add an order line with a product without a tax class (also product type cannot have a tax class). Taxes should be 0, since there is no default country rate or tax class.\r\n6. Set order addresses to some valid addresses from Poland. Default rate for Poland is set and Saleor should recalculate taxes with the Polish rate. Currently, it doesn't happen and taxes stay 0.\n\n### What did you expect to happen?\n\nTaxes should be correctly recalculated when changing addresses in draft order.\n\n### Logs\n\n_No response_\n\n### Environment\n\nSaleor version: 3.12.22\r\n\n", "before_files": [{"content": "from decimal import Decimal\nfrom typing import TYPE_CHECKING, Iterable, Tuple\n\nfrom prices import Money, TaxedMoney\n\nfrom ...core.prices import quantize_price\nfrom ...core.taxes import zero_money, zero_taxed_money\nfrom ...discount import OrderDiscountType\nfrom ...order import base_calculations\nfrom ...order.utils import (\n    get_order_country,\n    get_total_order_discount_excluding_shipping,\n)\nfrom ..models import TaxClassCountryRate\nfrom ..utils import (\n    denormalize_tax_rate_from_db,\n    get_tax_rate_for_tax_class,\n    normalize_tax_rate_for_db,\n)\nfrom . import calculate_flat_rate_tax\n\nif TYPE_CHECKING:\n    from ...order.models import Order, OrderLine\n\n\ndef update_order_prices_with_flat_rates(\n    order: \"Order\",\n    lines: Iterable[\"OrderLine\"],\n    prices_entered_with_tax: bool,\n):\n    country_code = get_order_country(order)\n    default_country_rate_obj = TaxClassCountryRate.objects.filter(\n        country=country_code, tax_class=None\n    ).first()\n    default_tax_rate = (\n        default_country_rate_obj.rate if default_country_rate_obj else Decimal(0)\n    )\n\n    # Calculate order line totals.\n    _, undiscounted_subtotal = update_taxes_for_order_lines(\n        order, lines, country_code, default_tax_rate, prices_entered_with_tax\n    )\n\n    # Calculate order shipping.\n    shipping_method = order.shipping_method\n    shipping_tax_class = getattr(shipping_method, \"tax_class\", None)\n    if shipping_tax_class:\n        shipping_tax_rate = get_tax_rate_for_tax_class(\n            shipping_tax_class,\n            shipping_tax_class.country_rates.all(),\n            default_tax_rate,\n            country_code,\n        )\n    elif order.shipping_tax_rate is not None:\n        # Use order.shipping_tax_rate if it was ever set before (it's non-null now).\n        # This is a valid case when recalculating shipping price and the tax class is\n        # null, because it was removed from the system.\n        shipping_tax_rate = denormalize_tax_rate_from_db(order.shipping_tax_rate)\n    else:\n        shipping_tax_rate = default_tax_rate\n\n    order.shipping_price = _calculate_order_shipping(\n        order, shipping_tax_rate, prices_entered_with_tax\n    )\n    order.shipping_tax_rate = normalize_tax_rate_for_db(shipping_tax_rate)\n\n    # Calculate order total.\n    order.undiscounted_total = undiscounted_subtotal + order.base_shipping_price\n    order.total = _calculate_order_total(order, lines)\n\n\ndef _calculate_order_total(\n    order: \"Order\",\n    lines: Iterable[\"OrderLine\"],\n) -> TaxedMoney:\n    currency = order.currency\n\n    default_value = base_calculations.base_order_total(order, lines)\n    default_value = TaxedMoney(default_value, default_value)\n    if default_value <= zero_taxed_money(currency):\n        return quantize_price(default_value, currency)\n\n    total = zero_taxed_money(currency)\n    undiscounted_subtotal = zero_taxed_money(currency)\n    for line in lines:\n        total += line.total_price\n        undiscounted_subtotal += line.undiscounted_total_price\n    total += order.shipping_price\n\n    order_discount = order.discounts.filter(type=OrderDiscountType.MANUAL).first()\n    if order_discount and order_discount.amount > undiscounted_subtotal.gross:\n        remaining_amount = order_discount.amount - undiscounted_subtotal.gross\n        total -= remaining_amount\n    return quantize_price(max(total, zero_taxed_money(currency)), currency)\n\n\ndef _calculate_order_shipping(\n    order: \"Order\", tax_rate: Decimal, prices_entered_with_tax: bool\n) -> TaxedMoney:\n    shipping_price = order.base_shipping_price\n    taxed_shipping_price = calculate_flat_rate_tax(\n        shipping_price, tax_rate, prices_entered_with_tax\n    )\n    return quantize_price(taxed_shipping_price, taxed_shipping_price.currency)\n\n\ndef update_taxes_for_order_lines(\n    order: \"Order\",\n    lines: Iterable[\"OrderLine\"],\n    country_code: str,\n    default_tax_rate: Decimal,\n    prices_entered_with_tax: bool,\n) -> Tuple[Iterable[\"OrderLine\"], TaxedMoney]:\n    currency = order.currency\n    lines = list(lines)\n\n    total_discount_amount = get_total_order_discount_excluding_shipping(order).amount\n    order_total_price = sum(\n        [line.base_unit_price.amount * line.quantity for line in lines]\n    )\n    total_line_discounts = 0\n\n    undiscounted_subtotal = zero_taxed_money(order.currency)\n\n    for line in lines:\n        variant = line.variant\n        if not variant:\n            continue\n\n        tax_class = line.tax_class\n        if tax_class:\n            tax_rate = get_tax_rate_for_tax_class(\n                tax_class,\n                tax_class.country_rates.all() if tax_class else [],\n                default_tax_rate,\n                country_code,\n            )\n        elif line.tax_rate is not None:\n            # line.tax_class can be None when the tax class was removed from DB. In\n            # this case try to use line.tax_rate which stores the denormalized tax rate\n            # value that was originally used.\n            tax_rate = denormalize_tax_rate_from_db(line.tax_rate)\n        else:\n            tax_rate = default_tax_rate\n\n        line_total_price = line.base_unit_price * line.quantity\n        undiscounted_subtotal += line_total_price\n\n        price_with_discounts = line.base_unit_price\n        if total_discount_amount:\n            if line is lines[-1]:\n                # for the last line applied remaining discount\n                discount_amount = total_discount_amount - total_line_discounts\n            else:\n                # calculate discount proportionally to the rate of total line price\n                # to order total price.\n                discount_amount = quantize_price(\n                    line_total_price.amount / order_total_price * total_discount_amount,\n                    currency,\n                )\n            price_with_discounts = max(\n                quantize_price(\n                    (line_total_price - Money(discount_amount, currency))\n                    / line.quantity,\n                    currency,\n                ),\n                zero_money(currency),\n            )\n            # sum already applied discounts\n            total_line_discounts += discount_amount\n\n        unit_price = calculate_flat_rate_tax(\n            price_with_discounts, tax_rate, prices_entered_with_tax\n        )\n        undiscounted_unit_price = calculate_flat_rate_tax(\n            line.undiscounted_base_unit_price, tax_rate, prices_entered_with_tax\n        )\n\n        line.unit_price = quantize_price(unit_price, currency)\n        line.undiscounted_unit_price = quantize_price(undiscounted_unit_price, currency)\n\n        line.total_price = quantize_price(unit_price * line.quantity, currency)\n        line.undiscounted_total_price = quantize_price(\n            undiscounted_unit_price * line.quantity, currency\n        )\n        line.tax_rate = normalize_tax_rate_for_db(tax_rate)\n\n    return lines, undiscounted_subtotal\n", "path": "saleor/tax/calculations/order.py"}], "after_files": [{"content": "from decimal import Decimal\nfrom typing import TYPE_CHECKING, Iterable, Tuple\n\nfrom prices import Money, TaxedMoney\n\nfrom ...core.prices import quantize_price\nfrom ...core.taxes import zero_money, zero_taxed_money\nfrom ...discount import OrderDiscountType\nfrom ...order import base_calculations\nfrom ...order.utils import (\n    get_order_country,\n    get_total_order_discount_excluding_shipping,\n)\nfrom ..models import TaxClassCountryRate\nfrom ..utils import (\n    denormalize_tax_rate_from_db,\n    get_tax_rate_for_tax_class,\n    normalize_tax_rate_for_db,\n)\nfrom . import calculate_flat_rate_tax\n\nif TYPE_CHECKING:\n    from ...order.models import Order, OrderLine\n\n\ndef update_order_prices_with_flat_rates(\n    order: \"Order\",\n    lines: Iterable[\"OrderLine\"],\n    prices_entered_with_tax: bool,\n):\n    country_code = get_order_country(order)\n    default_country_rate_obj = TaxClassCountryRate.objects.filter(\n        country=country_code, tax_class=None\n    ).first()\n    default_tax_rate = (\n        default_country_rate_obj.rate if default_country_rate_obj else Decimal(0)\n    )\n\n    # Calculate order line totals.\n    _, undiscounted_subtotal = update_taxes_for_order_lines(\n        order, lines, country_code, default_tax_rate, prices_entered_with_tax\n    )\n\n    # Calculate order shipping.\n    shipping_method = order.shipping_method\n    shipping_tax_class = getattr(shipping_method, \"tax_class\", None)\n    if shipping_tax_class:\n        shipping_tax_rate = get_tax_rate_for_tax_class(\n            shipping_tax_class,\n            shipping_tax_class.country_rates.all(),\n            default_tax_rate,\n            country_code,\n        )\n    elif (\n        order.shipping_tax_class_name is not None\n        and order.shipping_tax_rate is not None\n    ):\n        # Use order.shipping_tax_rate if it was ever set before (it's non-null now and\n        # the name is non-null). This is a valid case when recalculating shipping price\n        # and the tax class is null, because it was removed from the system.\n        shipping_tax_rate = denormalize_tax_rate_from_db(order.shipping_tax_rate)\n    else:\n        shipping_tax_rate = default_tax_rate\n\n    order.shipping_price = _calculate_order_shipping(\n        order, shipping_tax_rate, prices_entered_with_tax\n    )\n    order.shipping_tax_rate = normalize_tax_rate_for_db(shipping_tax_rate)\n\n    # Calculate order total.\n    order.undiscounted_total = undiscounted_subtotal + order.base_shipping_price\n    order.total = _calculate_order_total(order, lines)\n\n\ndef _calculate_order_total(\n    order: \"Order\",\n    lines: Iterable[\"OrderLine\"],\n) -> TaxedMoney:\n    currency = order.currency\n\n    default_value = base_calculations.base_order_total(order, lines)\n    default_value = TaxedMoney(default_value, default_value)\n    if default_value <= zero_taxed_money(currency):\n        return quantize_price(default_value, currency)\n\n    total = zero_taxed_money(currency)\n    undiscounted_subtotal = zero_taxed_money(currency)\n    for line in lines:\n        total += line.total_price\n        undiscounted_subtotal += line.undiscounted_total_price\n    total += order.shipping_price\n\n    order_discount = order.discounts.filter(type=OrderDiscountType.MANUAL).first()\n    if order_discount and order_discount.amount > undiscounted_subtotal.gross:\n        remaining_amount = order_discount.amount - undiscounted_subtotal.gross\n        total -= remaining_amount\n    return quantize_price(max(total, zero_taxed_money(currency)), currency)\n\n\ndef _calculate_order_shipping(\n    order: \"Order\", tax_rate: Decimal, prices_entered_with_tax: bool\n) -> TaxedMoney:\n    shipping_price = order.base_shipping_price\n    taxed_shipping_price = calculate_flat_rate_tax(\n        shipping_price, tax_rate, prices_entered_with_tax\n    )\n    return quantize_price(taxed_shipping_price, taxed_shipping_price.currency)\n\n\ndef update_taxes_for_order_lines(\n    order: \"Order\",\n    lines: Iterable[\"OrderLine\"],\n    country_code: str,\n    default_tax_rate: Decimal,\n    prices_entered_with_tax: bool,\n) -> Tuple[Iterable[\"OrderLine\"], TaxedMoney]:\n    currency = order.currency\n    lines = list(lines)\n\n    total_discount_amount = get_total_order_discount_excluding_shipping(order).amount\n    order_total_price = sum(\n        [line.base_unit_price.amount * line.quantity for line in lines]\n    )\n    total_line_discounts = 0\n\n    undiscounted_subtotal = zero_taxed_money(order.currency)\n\n    for line in lines:\n        variant = line.variant\n        if not variant:\n            continue\n\n        tax_class = line.tax_class\n        if tax_class:\n            tax_rate = get_tax_rate_for_tax_class(\n                tax_class,\n                tax_class.country_rates.all() if tax_class else [],\n                default_tax_rate,\n                country_code,\n            )\n        elif line.tax_class_name is not None and line.tax_rate is not None:\n            # If tax_class is None but tax_class_name is set, the tax class was set\n            # for this line before, but is now removed from the system. In this case\n            # try to use line.tax_rate which stores the denormalized tax rate value\n            # that was originally provided by the tax class.\n            tax_rate = denormalize_tax_rate_from_db(line.tax_rate)\n        else:\n            tax_rate = default_tax_rate\n\n        line_total_price = line.base_unit_price * line.quantity\n        undiscounted_subtotal += line_total_price\n\n        price_with_discounts = line.base_unit_price\n        if total_discount_amount:\n            if line is lines[-1]:\n                # for the last line applied remaining discount\n                discount_amount = total_discount_amount - total_line_discounts\n            else:\n                # calculate discount proportionally to the rate of total line price\n                # to order total price.\n                discount_amount = quantize_price(\n                    line_total_price.amount / order_total_price * total_discount_amount,\n                    currency,\n                )\n            price_with_discounts = max(\n                quantize_price(\n                    (line_total_price - Money(discount_amount, currency))\n                    / line.quantity,\n                    currency,\n                ),\n                zero_money(currency),\n            )\n            # sum already applied discounts\n            total_line_discounts += discount_amount\n\n        unit_price = calculate_flat_rate_tax(\n            price_with_discounts, tax_rate, prices_entered_with_tax\n        )\n        undiscounted_unit_price = calculate_flat_rate_tax(\n            line.undiscounted_base_unit_price, tax_rate, prices_entered_with_tax\n        )\n\n        line.unit_price = quantize_price(unit_price, currency)\n        line.undiscounted_unit_price = quantize_price(undiscounted_unit_price, currency)\n\n        line.total_price = quantize_price(unit_price * line.quantity, currency)\n        line.undiscounted_total_price = quantize_price(\n            undiscounted_unit_price * line.quantity, currency\n        )\n        line.tax_rate = normalize_tax_rate_for_db(tax_rate)\n\n    return lines, undiscounted_subtotal\n", "path": "saleor/tax/calculations/order.py"}]}