Repository: saleor/saleor

Issue:
Voucher Min Amount Spent Blank After Creation
### What I'm trying to achieve
Add vouchers with min amount spent

### Steps to reproduce the problem
Create new voucher, fill in the fields, hit create.

### What I expected to happen
Find new voucher created in the Voucher list page

### What actually happened
The voucher was created but the min amount spent field is blank

### Screenshots
<img width="752" alt="Screenshot 2019-05-01 18 38 21" src="https://user-images.githubusercontent.com/22291306/57019562-5b7aec00-6c40-11e9-83e1-42ed0cf5362c.png">
<img width="977" alt="Screenshot 2019-05-01 18 38 29" src="https://user-images.githubusercontent.com/22291306/57019564-5cac1900-6c40-11e9-8fbb-f5ccaddfc292.png">

**System information**
Operating system: macOS Mojave
Browser: Chrome



Discussion:
Correction: THIS ONLY HAPPENS WHEN I SELECT

Specific collection of products

It works perfectly when it's all products or a specific product
Steps to reproduce:
1. Create a voucher;
1. Select a voucher type applicable for other than all products or shipping;
1. Give it a minimum amount.

The minimum amount field is not the same one on the other types as the 'all products' or 'shipping' type's minimum amount field. It seems like the value is not taken and no populated.

Resolving patch:
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -169,7 +169,7 @@ class ProductVoucherForm(CommonVoucherForm):
 
     class Meta:
         model = Voucher
-        fields = ["products", "apply_once_per_order"]
+        fields = ["products", "apply_once_per_order", "min_amount_spent"]
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -180,7 +180,7 @@ def __init__(self, *args, **kwargs):
 class CollectionVoucherForm(CommonVoucherForm):
     class Meta:
         model = Voucher
-        fields = ["collections", "apply_once_per_order"]
+        fields = ["collections", "apply_once_per_order", "min_amount_spent"]
         labels = {"collections": pgettext_lazy("Collections", "Collections")}
 
     def __init__(self, *args, **kwargs):
@@ -197,4 +197,4 @@ class CategoryVoucherForm(CommonVoucherForm):
 
     class Meta:
         model = Voucher
-        fields = ["categories", "apply_once_per_order"]
+        fields = ["categories", "apply_once_per_order", "min_amount_spent"]


Test patch:
diff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.py
--- a/tests/dashboard/test_discounts.py
+++ b/tests/dashboard/test_discounts.py
@@ -3,23 +3,25 @@
 from decimal import Decimal
 from unittest.mock import Mock
 
-import pytest
 from django.urls import reverse
+
 from prices import Money, TaxedMoney
 
+import pytest
+
 from saleor.dashboard.order.utils import get_voucher_discount_for_order
 from saleor.discount import DiscountValueType, VoucherType
 from saleor.discount.models import NotApplicable, Sale, Voucher
 from saleor.product.models import Collection
 
 
-def test_sales_list(admin_client, sale):
+def test_sales_list(admin_client):
     url = reverse("dashboard:sale-list")
     response = admin_client.get(url)
     assert response.status_code == 200
 
 
-def test_vouchers_list(admin_client, voucher):
+def test_vouchers_list(admin_client):
     url = reverse("dashboard:voucher-list")
     response = admin_client.get(url)
     assert response.status_code == 200
@@ -110,7 +112,7 @@ def test_view_sale_add_requires_product_category_or_collection(
     ],
 )
 def test_value_voucher_order_discount(
-    settings, total, discount_value, discount_type, min_amount_spent, expected_value
+    total, discount_value, discount_type, min_amount_spent, expected_value
 ):
     voucher = Voucher(
         code="unique",
@@ -217,3 +219,47 @@ def test_ajax_voucher_list(admin_client, voucher):
 
     assert response.status_code == 200
     assert resp_decoded == {"results": vouchers_list}
+
+
+@pytest.mark.parametrize(
+    "voucher_type", ["collection", "category", "product", "value", "shipping"]
+)
+def test_voucher_form_min_amount_spent_is_changed_on_edit(
+    admin_client, product, collection, voucher_type
+):
+    assert Voucher.objects.count() == 0
+    url = reverse("dashboard:voucher-add")
+    data = {
+        "code": "TESTVOUCHER",
+        "name": "Test Voucher",
+        "start_date": "2019-01-01",
+        "end_date": "2019-06-01",
+        "type": voucher_type,
+        "discount_value": "15.99",
+        "discount_value_type": DiscountValueType.FIXED,
+        "product-products": [product.pk],
+        "category-categories": [product.category.pk],
+        "collection-collections": [collection.pk],
+        "shipping-min_amount_spent": "400",
+        "product-min_amount_spent": "400",
+        "category-min_amount_spent": "400",
+        "collection-min_amount_spent": "400",
+        "value-min_amount_spent": "400",
+    }
+
+    data["{}-min_amount_spent".format(voucher_type)] = "800"
+
+    response = admin_client.post(url, data, follow=True)
+
+    assert response.status_code == 200
+    assert Voucher.objects.count() == 1
+
+    voucher = Voucher.objects.all()[0]
+    assert voucher.type == voucher_type
+    assert voucher.code == data["code"]
+    assert voucher.name == data["name"]
+    assert voucher.start_date == date(2019, 1, 1)
+    assert voucher.end_date == date(2019, 6, 1)
+    assert voucher.discount_value_type == DiscountValueType.FIXED
+    assert voucher.discount_value == Decimal("15.99")
+    assert voucher.min_amount_spent == Money("800", "USD")
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -1,8 +1,9 @@
 from datetime import date, timedelta
 
-import pytest
 from prices import Money, TaxedMoney
 
+import pytest
+
 from saleor.checkout.utils import get_voucher_discount_for_checkout
 from saleor.discount import DiscountValueType, VoucherType
 from saleor.discount.models import NotApplicable, Sale, Voucher