Permalink
Browse files

Expense report editing: Warn when the days total has changed

  • Loading branch information...
1 parent eab809d commit 0bc9a59092f27ffd4ab9a5002622439d2d15ada9 @matthiask committed Apr 12, 2012
Oops, something went wrong.
View
@@ -533,6 +533,7 @@ def generate_expensereports(self):
sick_days=0,
holi_days=0,
forced_leave_days=data['forced'],
+ calculated_total_days=data['working'] + data['free'] + data['forced'],
clothing_expenses=clothing_expenses,
)
@@ -588,6 +589,10 @@ class ExpenseReport(models.Model):
forced_leave_days = models.PositiveIntegerField(_('forced leave days'))
forced_leave_days_notes = models.CharField(_('notes'), max_length=100, blank=True)
+ calculated_total_days = models.PositiveIntegerField(_('calculated total days'),
+ help_text=_('This field is filled in automatically by the system and should not be changed.'),
+ default=0)
+
clothing_expenses = models.DecimalField(_('clothing expenses'),
max_digits=10, decimal_places=2, default=Decimal('0.00'))
clothing_expenses_notes = models.CharField(_('notes'), max_length=100, blank=True)
@@ -176,3 +176,6 @@ strong.warning {
form p {
margin-bottom: 5px;
}
+
+.box.error { background: #ea9e8f; }
+.box.warning { background: #ecd488; }
@@ -14,6 +14,18 @@
<form method="post" action=".">{% csrf_token %}
{% form_errors form formsets %}
+{% if form.warnings %}
+<div class="box warning cf">
+ {% for warning in form.warnings %}
+ {{ warning }}
+ {% endfor %}
+ <label for="{{ form.ignore_warnings.auto_id }}">
+ {{ form.ignore_warnings }}
+ {{ form.ignore_warnings.label }}
+ </label>
+</div>
+{% endif %}
+
<table>
{% if object %}
<tr>
View
@@ -386,6 +386,30 @@ def post_save(self, request, instance, form, formset, change):
)
+class EditExpenseReportForm(forms.ModelForm, towel_forms.WarningsForm):
+ class Meta:
+ model = ExpenseReport
+ exclude = ('assignment', 'total', 'calculated_total_days')
+
+ def clean(self):
+ data = super(EditExpenseReportForm, self).clean()
+
+ try:
+ total_days = (data['working_days'] + data['free_days'] + data['sick_days']
+ + data['holi_days'] + data['forced_leave_days'])
+ except (ValueError, TypeError):
+ return data
+
+ if total_days != self.instance.calculated_total_days:
+ self.add_warning(
+ _('The number of days in this form (%(total)s) differs from the calculated number of days for this period (%(calculated)s).') % {
+ 'total': total_days,
+ 'calculated': self.instance.calculated_total_days,
+ })
+
+ return data
+
+
class ExpenseReportModelView(ZivinetzModelView):
paginate_by = 50
@@ -436,8 +460,7 @@ def deletion_allowed(self, request, instance):
def get_form(self, request, instance=None, change=None, **kwargs):
if instance and instance.pk:
- return super(ExpenseReportModelView, self).get_form(request, instance=instance,
- exclude=('assignment', 'total'))
+ return EditExpenseReportForm
else:
class ModelForm(forms.ModelForm):
assignment = forms.ModelChoiceField(
@@ -447,7 +470,7 @@ class ModelForm(forms.ModelForm):
class Meta:
model = self.model
- exclude = ('total',)
+ exclude = ('total', 'calculated_total_days')
formfield_callback = towel_forms.stripped_formfield_callback

0 comments on commit 0bc9a59

Please sign in to comment.