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))
class AccountOptions(admin.ModelAdmin): search_fields = ['name'] list_filter = ['type', 'closed_on'] list_display = ("name", "type", "budgeted_limit", "balance", "closed") ordering = ("name", "closed_on") def get_urls(self): urls = super(AccountOptions, self).get_urls() my_urls = patterns('', url(r'^(\d+)/close/$', self.admin_site.admin_view(self.close), name="accounts-close"), url(r'^(\d+)/delete/$', "accounts.views.delete_account", name="accounts-delete") ) return my_urls + urls @permission_required("accounts.delete_account") def close(self, request, id, template_name="accounts/close_account.html"): account = Account.objects.get(id=id) try: default = list(Account.objects.filter(type__iexact="Equity"))[0] except Exception, ex: default = None class CloseAccountForm(forms.Form): close_to = forms.ModelChoiceField( initial=default, queryset=Account.objects.filter(type__iexact="Equity") ) renew = forms.BooleanField( required=False, initial=True, help_text="check to zero and renew the account<br />uncheck to zero and close account" ) if request.method == "POST": form = CloseAccountForm(request.POST) if form.is_valid(): entry = Entry(site=Site.objects.get_current()) entry.description = "closing entry for " + account.name entry.save() balance = account.balance() action1 = EntryAction(entry=entry) action1.amount = -balance action1.account = account action1.save() action2 = EntryAction(entry=entry) action2.amount = balance action2.account = form.cleaned_data['close_to'] action2.save() if form.cleaned_data['renew']: name = account.name[:account.name.rfind(" ")] + datetime.datetime.now().strftime(" %y") account.name = name else: account.closed_on = datetime.datetime.now() account.save() return HttpResponseRedirect(reverse("accounts-summary")) else: form = CloseAccountForm() return render_to_response(template_name, locals(), context_instance=RequestContext(request))