2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import inventory
|
2016-03-03 21:40:44 +00:00
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
|
|
|
|
2016-04-11 03:11:31 +00:00
|
|
|
class ApplyCreditNoteForm(forms.Form):
|
|
|
|
|
|
|
|
def __init__(self, user, *a, **k):
|
|
|
|
''' User: The user whose invoices should be made available as
|
|
|
|
choices. '''
|
|
|
|
self.user = user
|
|
|
|
super(ApplyCreditNoteForm, self).__init__(*a, **k)
|
|
|
|
|
|
|
|
self.fields["invoice"].choices = self._unpaid_invoices_for_user
|
|
|
|
|
|
|
|
def _unpaid_invoices_for_user(self):
|
2016-04-22 05:06:24 +00:00
|
|
|
invoices = commerce.Invoice.objects.filter(
|
|
|
|
status=commerce.Invoice.STATUS_UNPAID,
|
2016-04-11 03:11:31 +00:00
|
|
|
user=self.user,
|
|
|
|
)
|
|
|
|
|
|
|
|
return [
|
|
|
|
(invoice.id, "Invoice %(id)d - $%(value)d" % invoice.__dict__)
|
|
|
|
for invoice in invoices
|
|
|
|
]
|
|
|
|
|
|
|
|
invoice = forms.ChoiceField(
|
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ManualCreditNoteRefundForm(forms.ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
2016-04-22 05:06:24 +00:00
|
|
|
model = commerce.ManualCreditNoteRefund
|
2016-04-11 03:11:31 +00:00
|
|
|
fields = ["reference"]
|
|
|
|
|
|
|
|
|
2016-04-07 09:19:19 +00:00
|
|
|
class ManualPaymentForm(forms.ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
2016-04-22 05:06:24 +00:00
|
|
|
model = commerce.ManualPayment
|
2016-04-07 09:19:19 +00:00
|
|
|
fields = ["reference", "amount"]
|
|
|
|
|
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
# Products forms -- none of these have any fields: they are to be subclassed
|
2016-08-22 00:15:01 +00:00
|
|
|
# and the fields added as needs be. ProductsForm (the function) is responsible
|
|
|
|
# for the subclassing.
|
|
|
|
|
|
|
|
def ProductsForm(category, products):
|
|
|
|
''' Produces an appropriate _ProductsForm subclass for the given render
|
|
|
|
type. '''
|
|
|
|
|
|
|
|
# Each Category.RENDER_TYPE value has a subclass here.
|
2016-09-02 01:43:27 +00:00
|
|
|
cat = inventory.Category
|
2016-08-22 00:15:01 +00:00
|
|
|
RENDER_TYPES = {
|
2016-09-02 01:43:27 +00:00
|
|
|
cat.RENDER_TYPE_QUANTITY: _QuantityBoxProductsForm,
|
|
|
|
cat.RENDER_TYPE_RADIO: _RadioButtonProductsForm,
|
|
|
|
cat.RENDER_TYPE_ITEM_QUANTITY: _ItemQuantityProductsForm,
|
2016-08-22 00:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Produce a subclass of _ProductsForm which we can alter the base_fields on
|
|
|
|
class ProductsForm(RENDER_TYPES[category.render_type]):
|
|
|
|
pass
|
|
|
|
|
|
|
|
ProductsForm.set_fields(category, products)
|
2016-08-22 03:17:24 +00:00
|
|
|
|
|
|
|
if category.render_type == inventory.Category.RENDER_TYPE_ITEM_QUANTITY:
|
|
|
|
ProductsForm = forms.formset_factory(
|
|
|
|
ProductsForm,
|
|
|
|
formset=_ItemQuantityProductsFormSet,
|
|
|
|
)
|
|
|
|
|
2016-08-22 00:15:01 +00:00
|
|
|
return ProductsForm
|
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
|
2016-08-22 00:13:02 +00:00
|
|
|
class _HasProductsFields(object):
|
2016-03-26 23:23:59 +00:00
|
|
|
|
|
|
|
PRODUCT_PREFIX = "product_"
|
|
|
|
|
|
|
|
''' Base class for product entry forms. '''
|
|
|
|
def __init__(self, *a, **k):
|
|
|
|
if "product_quantities" in k:
|
|
|
|
initial = self.initial_data(k["product_quantities"])
|
|
|
|
k["initial"] = initial
|
|
|
|
del k["product_quantities"]
|
2016-08-22 00:15:01 +00:00
|
|
|
super(_HasProductsFields, self).__init__(*a, **k)
|
2016-03-26 23:23:59 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def field_name(cls, product):
|
|
|
|
return cls.PRODUCT_PREFIX + ("%d" % product.id)
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
@classmethod
|
2016-03-27 00:18:26 +00:00
|
|
|
def set_fields(cls, category, products):
|
2016-03-26 23:23:59 +00:00
|
|
|
''' Sets the base_fields on this _ProductsForm to allow selecting
|
|
|
|
from the provided products. '''
|
|
|
|
pass
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
@classmethod
|
|
|
|
def initial_data(cls, product_quantites):
|
|
|
|
''' Prepares initial data for an instance of this form.
|
|
|
|
product_quantities is a sequence of (product,quantity) tuples '''
|
|
|
|
return {}
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
def product_quantities(self):
|
|
|
|
''' Yields a sequence of (product, quantity) tuples from the
|
|
|
|
cleaned form data. '''
|
|
|
|
return iter([])
|
2016-03-26 02:30:46 +00:00
|
|
|
|
2016-08-22 03:17:24 +00:00
|
|
|
def add_product_error(self, product, error):
|
|
|
|
''' Adds an error to the given product's field '''
|
|
|
|
|
|
|
|
''' if product in field_names:
|
|
|
|
field = field_names[product]
|
|
|
|
elif isinstance(product, inventory.Product):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
field = None '''
|
|
|
|
|
|
|
|
self.add_error(self.field_name(product), error)
|
|
|
|
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-08-22 00:13:02 +00:00
|
|
|
class _ProductsForm(_HasProductsFields, forms.Form):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
class _QuantityBoxProductsForm(_ProductsForm):
|
|
|
|
''' Products entry form that allows users to enter quantities
|
|
|
|
of desired products. '''
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
@classmethod
|
2016-03-27 00:18:26 +00:00
|
|
|
def set_fields(cls, category, products):
|
2016-03-26 23:23:59 +00:00
|
|
|
for product in products:
|
2016-04-06 06:45:54 +00:00
|
|
|
if product.description:
|
2016-04-07 03:26:25 +00:00
|
|
|
help_text = "$%d each -- %s" % (
|
|
|
|
product.price,
|
|
|
|
product.description,
|
|
|
|
)
|
2016-04-06 06:45:54 +00:00
|
|
|
else:
|
|
|
|
help_text = "$%d each" % product.price
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
field = forms.IntegerField(
|
|
|
|
label=product.name,
|
|
|
|
help_text=help_text,
|
2016-04-06 12:57:40 +00:00
|
|
|
min_value=0,
|
2016-04-25 06:16:22 +00:00
|
|
|
max_value=500, # Issue #19. We should figure out real limit.
|
2016-03-26 23:23:59 +00:00
|
|
|
)
|
|
|
|
cls.base_fields[cls.field_name(product)] = field
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
@classmethod
|
|
|
|
def initial_data(cls, product_quantities):
|
|
|
|
initial = {}
|
|
|
|
for product, quantity in product_quantities:
|
|
|
|
initial[cls.field_name(product)] = quantity
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-26 23:23:59 +00:00
|
|
|
return initial
|
|
|
|
|
|
|
|
def product_quantities(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith(self.PRODUCT_PREFIX):
|
|
|
|
product_id = int(name[len(self.PRODUCT_PREFIX):])
|
2016-08-22 03:17:24 +00:00
|
|
|
yield (product_id, value)
|
2016-03-26 23:23:59 +00:00
|
|
|
|
|
|
|
|
2016-03-27 00:18:26 +00:00
|
|
|
class _RadioButtonProductsForm(_ProductsForm):
|
|
|
|
''' Products entry form that allows users to enter quantities
|
|
|
|
of desired products. '''
|
|
|
|
|
|
|
|
FIELD = "chosen_product"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_fields(cls, category, products):
|
|
|
|
choices = []
|
|
|
|
for product in products:
|
|
|
|
choice_text = "%s -- $%d" % (product.name, product.price)
|
|
|
|
choices.append((product.id, choice_text))
|
|
|
|
|
2016-04-25 06:16:41 +00:00
|
|
|
if not category.required:
|
|
|
|
choices.append((0, "No selection"))
|
|
|
|
|
2016-03-27 00:18:26 +00:00
|
|
|
cls.base_fields[cls.FIELD] = forms.TypedChoiceField(
|
|
|
|
label=category.name,
|
|
|
|
widget=forms.RadioSelect,
|
|
|
|
choices=choices,
|
|
|
|
empty_value=0,
|
|
|
|
coerce=int,
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def initial_data(cls, product_quantities):
|
|
|
|
initial = {}
|
|
|
|
|
|
|
|
for product, quantity in product_quantities:
|
|
|
|
if quantity > 0:
|
|
|
|
initial[cls.FIELD] = product.id
|
|
|
|
break
|
|
|
|
|
|
|
|
return initial
|
|
|
|
|
|
|
|
def product_quantities(self):
|
|
|
|
ours = self.cleaned_data[self.FIELD]
|
|
|
|
choices = self.fields[self.FIELD].choices
|
|
|
|
for choice_value, choice_display in choices:
|
2016-04-25 06:16:41 +00:00
|
|
|
if choice_value == 0:
|
|
|
|
continue
|
2016-03-27 00:18:26 +00:00
|
|
|
yield (
|
|
|
|
choice_value,
|
|
|
|
1 if ours == choice_value else 0,
|
|
|
|
self.FIELD,
|
|
|
|
)
|
|
|
|
|
2016-08-22 03:17:24 +00:00
|
|
|
def add_product_error(self, product, error):
|
2016-09-02 01:43:27 +00:00
|
|
|
self.add_error(self.FIELD, error)
|
|
|
|
|
2016-08-22 03:17:24 +00:00
|
|
|
|
2016-08-22 00:41:39 +00:00
|
|
|
class _ItemQuantityProductsForm(_ProductsForm):
|
|
|
|
''' Products entry form that allows users to select a product type, and
|
2016-08-22 03:17:24 +00:00
|
|
|
enter a quantity of that product. This version _only_ allows a single
|
2016-09-02 01:43:27 +00:00
|
|
|
product type to be purchased. This form is usually used in concert with
|
|
|
|
the _ItemQuantityProductsFormSet to allow selection of multiple
|
|
|
|
products.'''
|
2016-08-22 00:41:39 +00:00
|
|
|
|
|
|
|
CHOICE_FIELD = "choice"
|
|
|
|
QUANTITY_FIELD = "quantity"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_fields(cls, category, products):
|
|
|
|
choices = []
|
2016-08-22 03:17:24 +00:00
|
|
|
|
|
|
|
if not category.required:
|
|
|
|
choices.append((0, "---"))
|
|
|
|
|
2016-08-22 00:41:39 +00:00
|
|
|
for product in products:
|
|
|
|
choice_text = "%s -- $%d each" % (product.name, product.price)
|
|
|
|
choices.append((product.id, choice_text))
|
|
|
|
|
|
|
|
cls.base_fields[cls.CHOICE_FIELD] = forms.TypedChoiceField(
|
|
|
|
label=category.name,
|
|
|
|
widget=forms.Select,
|
|
|
|
choices=choices,
|
2016-08-22 03:17:24 +00:00
|
|
|
initial=0,
|
2016-08-22 00:41:39 +00:00
|
|
|
empty_value=0,
|
|
|
|
coerce=int,
|
|
|
|
)
|
|
|
|
|
|
|
|
cls.base_fields[cls.QUANTITY_FIELD] = forms.IntegerField(
|
|
|
|
label="Quantity", # TODO: internationalise
|
|
|
|
min_value=0,
|
|
|
|
max_value=500, # Issue #19. We should figure out real limit.
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def initial_data(cls, product_quantities):
|
|
|
|
initial = {}
|
|
|
|
|
|
|
|
for product, quantity in product_quantities:
|
|
|
|
if quantity > 0:
|
|
|
|
initial[cls.CHOICE_FIELD] = product.id
|
|
|
|
initial[cls.QUANTITY_FIELD] = quantity
|
|
|
|
break
|
|
|
|
|
|
|
|
return initial
|
|
|
|
|
|
|
|
def product_quantities(self):
|
|
|
|
our_choice = self.cleaned_data[self.CHOICE_FIELD]
|
|
|
|
our_quantity = self.cleaned_data[self.QUANTITY_FIELD]
|
|
|
|
choices = self.fields[self.CHOICE_FIELD].choices
|
|
|
|
for choice_value, choice_display in choices:
|
|
|
|
if choice_value == 0:
|
|
|
|
continue
|
|
|
|
yield (
|
|
|
|
choice_value,
|
|
|
|
our_quantity if our_choice == choice_value else 0,
|
|
|
|
)
|
|
|
|
|
2016-08-22 03:17:24 +00:00
|
|
|
def add_product_error(self, product, error):
|
|
|
|
if self.CHOICE_FIELD not in self.cleaned_data:
|
|
|
|
return
|
|
|
|
|
|
|
|
if product.id == self.cleaned_data[self.CHOICE_FIELD]:
|
2016-08-22 05:03:08 +00:00
|
|
|
self.add_error(self.CHOICE_FIELD, error)
|
2016-08-22 03:17:24 +00:00
|
|
|
self.add_error(self.QUANTITY_FIELD, error)
|
|
|
|
|
|
|
|
|
|
|
|
class _ItemQuantityProductsFormSet(_HasProductsFields, forms.BaseFormSet):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_fields(cls, category, products):
|
|
|
|
raise ValueError("set_fields must be called on the underlying Form")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def initial_data(cls, product_quantities):
|
|
|
|
''' Prepares initial data for an instance of this form.
|
|
|
|
product_quantities is a sequence of (product,quantity) tuples '''
|
|
|
|
|
|
|
|
f = [
|
|
|
|
{
|
|
|
|
_ItemQuantityProductsForm.CHOICE_FIELD: product.id,
|
|
|
|
_ItemQuantityProductsForm.QUANTITY_FIELD: quantity,
|
|
|
|
}
|
|
|
|
for product, quantity in product_quantities
|
|
|
|
if quantity > 0
|
|
|
|
]
|
|
|
|
return f
|
|
|
|
|
|
|
|
def product_quantities(self):
|
|
|
|
''' Yields a sequence of (product, quantity) tuples from the
|
|
|
|
cleaned form data. '''
|
|
|
|
|
|
|
|
products = set()
|
|
|
|
# Track everything so that we can yield some zeroes
|
|
|
|
all_products = set()
|
|
|
|
|
|
|
|
for form in self:
|
|
|
|
if form.empty_permitted and not form.cleaned_data:
|
|
|
|
# This is the magical empty form at the end of the list.
|
|
|
|
continue
|
|
|
|
|
|
|
|
for product, quantity in form.product_quantities():
|
|
|
|
all_products.add(product)
|
|
|
|
if quantity == 0:
|
|
|
|
continue
|
|
|
|
if product in products:
|
|
|
|
form.add_error(
|
|
|
|
_ItemQuantityProductsForm.CHOICE_FIELD,
|
|
|
|
"You may only choose each product type once.",
|
|
|
|
)
|
|
|
|
form.add_error(
|
|
|
|
_ItemQuantityProductsForm.QUANTITY_FIELD,
|
|
|
|
"You may only choose each product type once.",
|
|
|
|
)
|
|
|
|
products.add(product)
|
|
|
|
yield product, quantity
|
|
|
|
|
|
|
|
for product in (all_products - products):
|
|
|
|
yield product, 0
|
|
|
|
|
|
|
|
def add_product_error(self, product, error):
|
|
|
|
for form in self.forms:
|
|
|
|
form.add_product_error(product, error)
|
|
|
|
|
2016-03-27 00:18:26 +00:00
|
|
|
|
2016-03-23 02:33:33 +00:00
|
|
|
class VoucherForm(forms.Form):
|
|
|
|
voucher = forms.CharField(
|
|
|
|
label="Voucher code",
|
|
|
|
help_text="If you have a voucher code, enter it here",
|
2016-03-23 08:36:54 +00:00
|
|
|
required=False,
|
2016-03-23 02:33:33 +00:00
|
|
|
)
|
2016-09-03 04:22:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StaffProductsForm(forms.Form):
|
|
|
|
''' Form for allowing staff to add an item to a user's cart. '''
|
|
|
|
|
|
|
|
product = forms.ModelChoiceField(
|
|
|
|
widget=forms.Select,
|
|
|
|
queryset=inventory.Product.objects.all(),
|
|
|
|
)
|
|
|
|
|
|
|
|
quantity = forms.IntegerField(
|
|
|
|
min_value=0,
|
|
|
|
)
|
|
|
|
|
|
|
|
StaffProductsFormSet = forms.formset_factory(StaffProductsForm)
|