2014-09-05 14:27:30 +00:00
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2014-09-06 10:56:30 +00:00
|
|
|
from orchestra.admin.utils import admin_link
|
2015-04-27 12:24:17 +00:00
|
|
|
from orchestra.forms import SpanWidget
|
2014-09-05 14:27:30 +00:00
|
|
|
|
2014-09-06 10:56:30 +00:00
|
|
|
|
|
|
|
class SelectSourceForm(forms.ModelForm):
|
2015-04-27 12:24:17 +00:00
|
|
|
bill_link = forms.CharField(label=_("Number"), required=False, widget=SpanWidget)
|
2014-09-06 10:56:30 +00:00
|
|
|
account_link = forms.CharField(label=_("Account"), required=False)
|
2015-04-27 12:24:17 +00:00
|
|
|
show_total = forms.CharField(label=_("Total"), required=False, widget=SpanWidget)
|
|
|
|
display_type = forms.CharField(label=_("Type"), required=False, widget=SpanWidget)
|
2014-09-05 14:27:30 +00:00
|
|
|
source = forms.ChoiceField(label=_("Source"), required=False)
|
|
|
|
|
|
|
|
class Meta:
|
2014-09-06 10:56:30 +00:00
|
|
|
fields = (
|
2015-04-27 12:24:17 +00:00
|
|
|
'bill_link', 'display_type', 'account_link', 'show_total', 'source'
|
2014-09-06 10:56:30 +00:00
|
|
|
)
|
2015-04-27 12:24:17 +00:00
|
|
|
readonly_fields = ('account_link',)
|
2014-09-05 14:27:30 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-09-06 10:56:30 +00:00
|
|
|
super(SelectSourceForm, self).__init__(*args, **kwargs)
|
2014-09-05 14:27:30 +00:00
|
|
|
bill = kwargs.get('instance')
|
|
|
|
if bill:
|
2015-04-27 12:24:17 +00:00
|
|
|
total = bill.get_total()
|
2014-09-05 14:27:30 +00:00
|
|
|
sources = bill.account.paymentsources.filter(is_active=True)
|
2015-04-27 12:24:17 +00:00
|
|
|
recharge = bool(total < 0)
|
2014-09-05 14:27:30 +00:00
|
|
|
choices = [(None, '-----------')]
|
|
|
|
for source in sources:
|
|
|
|
if not recharge or source.method_class().allow_recharge:
|
|
|
|
choices.append((source.pk, str(source)))
|
|
|
|
self.fields['source'].choices = choices
|
2014-09-06 10:56:30 +00:00
|
|
|
self.fields['source'].initial = choices[-1][0]
|
2015-04-27 12:24:17 +00:00
|
|
|
self.fields['show_total'].widget.display = total
|
|
|
|
self.fields['bill_link'].widget.display = admin_link('__str__')(bill)
|
|
|
|
self.fields['display_type'].widget.display = bill.get_type_display()
|
2014-09-05 14:27:30 +00:00
|
|
|
|
|
|
|
def clean_source(self):
|
|
|
|
source_id = self.cleaned_data['source']
|
|
|
|
if not source_id:
|
|
|
|
return None
|
|
|
|
source_model = self.instance.account.paymentsources.model
|
|
|
|
return source_model.objects.get(id=source_id)
|
|
|
|
|
2014-09-06 10:56:30 +00:00
|
|
|
def has_changed(self):
|
|
|
|
return False
|
|
|
|
|
2014-09-05 14:27:30 +00:00
|
|
|
def save(self, commit=True):
|
2014-09-06 10:56:30 +00:00
|
|
|
pass
|