Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] so line qty set to 0 on confirmed so #26

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions sale_global_discount_amount/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ def write(self, vals):
or "order_line" in vals
or not self.global_discount_ok
) and not self.env.context.get("discount_lines", False):
self.order_line.filtered(lambda x: x.is_discount_line).with_context(
discount_lines=True
).unlink()
if self.state == "draft":
self.order_line.filtered(lambda x: x.is_discount_line).with_context(
discount_lines=True
).unlink()
else:
self.order_line.filtered(lambda x: x.is_discount_line).with_context(
discount_lines=True
).write({"product_uom_qty": 0, "price_unit": 0})
if self.global_discount_amount != 0.0:
self.env["sale.order.line"]._create_discount_lines(order=self)
return res
Expand Down Expand Up @@ -87,7 +92,7 @@ def _create_discount_lines(self, order):
)
# create one discount line for the all order lines without price tax
lines_without_price_tax = order.order_line.filtered(
lambda x: x.price_tax == 0.0
lambda x: x.price_tax == 0.0 and x.product_uom_qty
)
without_tax_base_amount = 0.0
for line in lines_without_price_tax:
Expand All @@ -108,13 +113,20 @@ def _create_one_discount_line(
discount_product = self.env.ref(
"account_global_discount_amount.discount_product"
)
discount_line = self.create(
{
"product_id": discount_product.id,
"order_id": order.id,
"product_uom_qty": 1,
}
)
discount_line = self.search([("product_id", "=", discount_product.id),
("order_id", "=", order.id),
("tax_id", "=", tax_ids[0][2][0])
], limit=1)
if not len(discount_line):
discount_line = self.create(
{
"product_id": discount_product.id,
"order_id": order.id,
"product_uom_qty": 1,
}
)
else:
discount_line.product_uom_qty=1
discount_line.product_id_change()
price_unit = discount_line._prepare_discount_line_vals(
amount_untaxed=amount_untaxed,
Expand All @@ -126,6 +138,7 @@ def _create_one_discount_line(
"price_unit": -1 * price_unit,
"is_discount_line": True,
"tax_id": tax_ids,

}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,20 @@ def test_create_invoice_from_sale_3_lines_multi_tax_with_global_discount(self):
lambda x: x.tax_ids == self.vat20
)
self.assertEqual(discount_line_20.price_unit, -27.78)

def test_create_updt_qty_to_zero_on_confirmed_so(self):
discount_order_lines = self.env["sale.order.line"].search(
[
("product_id", "=", self.discount_product.id),
("order_id", "=", self.sale_single_tax.id),
]
)
self.sale_single_tax.action_confirm()
line_1_id = self.sale_single_tax.order_line.filtered(lambda r: not r.discount)[0].id
self.assertTrue(self.sale_single_tax.write({"order_line": [(1, line_1_id, {"product_uom_qty": 0})]}))
# self.assertEqual(len(discount_order_lines), 1)
# self.assertEqual(discount_order_lines[0].is_discount_line, True)
# self.assertEqual(discount_order_lines[0].price_unit, -30.00)
# self.assertEqual(discount_order_lines[0].product_uom_qty, 1.0)
# self.assertEqual(len(discount_order_lines[0].tax_id), 1)
# self.assertEqual(discount_order_lines[0].tax_id[0].name, "TEST 10%")