2016-06-04 03:44:41 +02:00
|
|
|
from bootstrap3_datetime.widgets import DateTimePicker
|
2016-06-04 01:24:40 +02:00
|
|
|
from django import forms
|
2016-06-04 03:44:41 +02:00
|
|
|
from django.db.models import Q
|
2016-06-04 01:24:40 +02:00
|
|
|
from refunding.models import Refund, Payment
|
|
|
|
|
|
|
|
|
|
|
|
class RefundForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Refund
|
|
|
|
fields = '__all__'
|
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
payments = forms.ModelMultipleChoiceField(queryset=Payment.objects.none(), label='Paiements')
|
2016-06-04 01:24:40 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(RefundForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.instance:
|
|
|
|
self.fields['payments'].initial = self.instance.payment_set.all()
|
2016-06-12 18:06:07 +02:00
|
|
|
self.fields['payments'].queryset = Payment\
|
|
|
|
.objects\
|
|
|
|
.filter(Q(refund=None) | Q(refund=self.instance))\
|
|
|
|
.order_by('-date')
|
2016-06-04 01:24:40 +02:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# Save the refund
|
2016-06-04 03:44:41 +02:00
|
|
|
instance = super(RefundForm, self).save()
|
2016-06-04 01:24:40 +02:00
|
|
|
# Remove the refund from payments it was previously assigned to
|
|
|
|
self.fields['payments'].initial.update(refund=None)
|
|
|
|
# Add the refund to the selected payments
|
|
|
|
self.cleaned_data['payments'].update(refund=instance)
|
|
|
|
return instance
|
2016-06-04 03:44:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RefundFormAdmin(RefundForm):
|
|
|
|
class Meta:
|
|
|
|
model = Refund
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
class RefundFormPublic(RefundForm):
|
|
|
|
class Meta:
|
|
|
|
model = Refund
|
|
|
|
exclude = ('user',)
|
|
|
|
|
|
|
|
date = forms.DateField(
|
|
|
|
widget=DateTimePicker(
|
|
|
|
options={
|
|
|
|
'format': 'YYYY-MM-DD'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2016-06-04 04:37:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PaymentForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Payment
|
|
|
|
fields = ('title', 'value', 'date')
|
|
|
|
widgets = {
|
|
|
|
'date': DateTimePicker(
|
|
|
|
options={
|
|
|
|
"format": "YYYY-MM-DD"
|
|
|
|
}
|
|
|
|
),
|
|
|
|
}
|