def mutate(self, info, resident_cpf, apartment_number): """Method to execute the mutation""" resident = Resident.objects.filter(cpf=resident_cpf).first() apartment = Apartment.objects.filter(number=apartment_number).first() entry = Entry(resident=resident, apartment=apartment) entry.save() return CreateEntry(resident=entry.resident, apartment=entry.apartment)
def expense(self, request, template_name="accounts/add_expense.html"): ActionFormSet = inlineformset_factory(Entry, EntryAction, form=AssetActionForm) entry = Entry(site=Site.objects.get_current()) if request.method == "POST": form = AddExpenseForm(request.POST, instance=entry) action_forms = ActionFormSet(request.POST, instance=entry) if form.is_valid() and action_forms.is_valid(): entry = form.save() actions = action_forms.save(commit=False) amount = form.cleaned_data['amount'] # get all extra actions and subtract them from the base for action in actions: amount -= action.amount action.amount = -action.amount if amount < 0: raise Exception("other assets are more that expected") entry.save() # add the default action expense = EntryAction() expense.entry = entry expense.amount = form.cleaned_data['amount'] expense.account = form.cleaned_data['expense'] asset = EntryAction() asset.entry = entry asset.amount = -amount asset.account = form.cleaned_data['asset'] actions += [expense, asset] # save entry and actions for action in actions: action.entry = entry action.save() return HttpResponseRedirect(reverse("accounts-summary")) else: form = AddExpenseForm(instance=entry) action_forms = ActionFormSet(instance=entry) return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def revenue(self, request, template_name="accounts/add_revenue.html"): WithholdingFormSet = inlineformset_factory(Entry, EntryAction, form=WithholdingForm, can_delete=False, extra=5) entry = Entry(site=Site.objects.get_current()) if request.method == "POST": form = AddRevenueForm(request.POST, instance=entry) action_forms = WithholdingFormSet(request.POST, instance=entry) if form.is_valid() and action_forms.is_valid(): entry = form.save(commit=False) amount = total = form.cleaned_data['amount'] entry.save() actions = action_forms.save() # get all extra actions and subtract them from the base for action in actions: amount -= action.amount # process the budget amount, budgeted = form.cleaned_data['budget'].process(entry, total, amount) actions += budgeted # add the default action actions += [EntryAction(entry=entry, amount=-total, account=form.cleaned_data['revenue'])] if amount > 0: actions += [EntryAction(entry=entry, amount=amount, account=form.cleaned_data['default'])] # save entry and actions for action in actions: action.save() return HttpResponseRedirect(reverse("accounts-summary")) else: form = AddRevenueForm(instance=entry) action_forms = WithholdingFormSet(instance=entry) cash = get_cash() return render_to_response(template_name, locals(), context_instance=RequestContext(request))