def payment_history(request, invoice_id): args = {} invoice = get_object_or_None(Invoice, pk=invoice_id) payments, total_paid = invoice.get_total_paid() args['payments'] = payments args['total_paid'] = total_paid return render(request, 'invoice/payment_history.html', args)
def check_pay_levels(request): period_id = request.GET.get('period') pay_levels = request.GET.getlist('pay_levels[]') invoice = request.GET.get('invoice') period = None warning_msg = '' if not period_id: logger.error( 'period_id query parameter is null, unable to get period. ' 'Received request with data {}'.format(request.GET)) else: period = get_object_or_None(PeriodCode, id=period_id) if period and pay_levels: invoices = Invoice.objects.filter(period=period, pay_levels__in=pay_levels, is_active=True).distinct('id') if invoice: invoices = invoices.exclude(id=invoice) if invoices.exists(): msg = "Warning! The following pay levels already exist " + \ "in another invoice for period {}:<br/>{}" invoices_list = [ "- {} {}".format(lev.detachment.name, lev.pay_level) for i in invoices for lev in i.pay_levels.all() if str(lev.id) in pay_levels ] invoices_str = '<br/>'.join(invoices_list) warning_msg = msg.format(period, invoices_str) return HttpResponse(json.dumps(warning_msg), content_type='application/json; charset=UTF-8')
def list_invoices_by_costcenter(request, costcenter): args = {} page_num = request.GET.get('page') status = request.GET.get('status') cost_center = get_object_or_None(CostCenter, id=costcenter) query = Invoice.objects.active().filter(cost_center__id=cost_center.id) if status == 'paid': query = query.paid() elif status == 'sent': query = query.sent() elif status == 'overdue': query = query.overdue() invoices = query.prefetch_related( Prefetch('invoicepayment_set', to_attr='amount', queryset=InvoicePayment.objects.filter(is_active=True))) for inv in invoices: total_paid = sum([pay.amount for pay in inv.amount if inv.amount]) inv.balance = inv.total_amount - total_paid invoice_page = page_objects(page_num, invoices) args['page'] = invoice_page args['status'] = status args['statuses'] = INVOICE_STATUS args['costcenter'] = cost_center if cost_center.detachment: args['detachment'] = cost_center.detachment else: args['company'] = cost_center.company return render(request, 'invoice/list_invoices.html', args)
def cancel_invoice(request, invoice_id): invoice = get_object_or_None(Invoice, pk=invoice_id) if not invoice: messages.error(request, "Invoice record not found.") return reverse_redirect('invoice:list_invoices') invoice.cancelled = True invoice.modified_by = request.user invoice.save() return reverse_redirect('invoice:view_invoice', [invoice.id])
def add_game_level(request, game_id=None): game = get_object_or_None(Game, pk=game_id) form = GameLevelForm() if request.method == 'POST': form = GameLevelForm(request.POST) if form.is_valid(): level = form.save(commit=False) level.game = game import datetime level.date_created = datetime.datetime.today() level.created_by = request.user level.save() return reverse_redirect('view-game', args=[game.id]) return { 'form':form, 'game':game }
def add_apt(request, game_id=None, game_level_id=None): level = get_object_or_None(GameLevel, pk=game_level_id) form = APTForm() if request.method == 'POST': form = APTForm(request.POST) if form.is_valid(): apt = form.save(commit=False) apt.user = request.user apt.game_level = level import datetime apt.date_added = datetime.datetime.today() apt.added_by = request.user apt.save() return reverse_redirect('view-game', args=[level.game.id]) return { 'form':form, 'level':level }
def delete_invoice(request, invoice_id): invoice = get_object_or_None(Invoice, pk=invoice_id) if not invoice: messages.error(request, "Invoice record not found.") return reverse_redirect('invoice:list_invoices') invoice.is_active = False invoice.detachment = timezone.now() invoice.modified_by = request.user invoice.save() msg = "Invoice {0} deleted.".format(invoice) messages.add_message(request, settings.DELETE_MESSAGE, msg) action.send(request.user, verb='deleted invoice record', action_object=invoice) return reverse_redirect('invoice:list_invoices')
def update_invoice(request, invoice): args = {} company_id = request.POST.get('company') company = get_object_or_None(Company, pk=company_id) pay_level_ids = request.POST.getlist('pay_levels') total_amount = request.POST.get('total_amount') args['company'] = company pay_levels = PayrollRate.objects.filter(id__in=pay_level_ids) args['pay_levels'] = pay_levels payments, total_paid = invoice.get_total_paid() args['balance'] = invoice.total_amount - total_paid args['add_types'] = ADD_INVOICE_ITEM_TYPE args['deduct_types'] = DEDUCT_INVOICE_ITEM_TYPE args['invoice'] = invoice invoiceform = InvoiceForm(request.POST, instance=invoice) formset = InvoiceItemFormset(request.POST, prefix='invoiceitem', instance=invoice) args['formset'] = formset args['invoiceform'] = invoiceform if not invoiceform.is_valid(): messages.error(request, "Error updating invoice") return render(request, 'invoice/invoice_detail.html', args) for form in formset.forms: if not form.is_valid(): messages.error(request, "Error updating invoice") return render(request, 'invoice/invoice_detail.html', args) invoice = invoiceform.save(commit=False) invoice.total_amount = total_amount invoice.company = company invoice.modified_by = request.user invoice.update_fully_paid() invoiceform.save_m2m() for form in formset.forms: if form in formset.deleted_forms: form.instance.delete() else: item = form.save(commit=False) item.created_by = request.user item.modified_by = request.user item.invoice = invoice item.save() messages.success(request, "Invoice updated.") action.send(request.user, verb='updated invoice', action_object=invoice) return reverse_redirect('invoice:view_invoice', [invoice.id])
def onchange_company(request, company): args = {} company = get_object_or_None(Company, id=company) if not company: messages.warning(request, 'Company not found.') else: detachments = company.get_detachments() args['detachments'] = detachments args['selected_company_id'] = company.id costcenters = company.get_costcenters() args['costcenters_regular'] = costcenters.filter(augmentation=False) args['costcenters_augmentation'] = costcenters.filter( augmentation=True) args['companies'] = Company.objects.filter( is_active=True).order_by('full_name') return render(request, 'invoice/select_client.html', args)
def save_invoice(request, costcenter): args = {} cost_center = get_object_or_None(CostCenter, id=costcenter) args['costcenter'] = cost_center if cost_center.detachment: company = cost_center.detachment.company elif cost_center.company: company = cost_center.company args['company'] = company total_amount = request.POST.get('total_amount') args['invoiceform'] = InvoiceForm(request.POST) args['formset'] = InvoiceItemFormset(request.POST, prefix='invoiceitem') args['title'] = 'Create Invoice' args['add_types'] = ADD_INVOICE_ITEM_TYPE args['deduct_types'] = DEDUCT_INVOICE_ITEM_TYPE invoiceform = InvoiceForm(request.POST) if not invoiceform.is_valid(): messages.error(request, 'Error saving invoice') return render(request, 'invoice/create_invoice.html', args) formset = InvoiceItemFormset(request.POST, prefix='invoiceitem') for form in formset.forms: if not form.is_valid(): messages.error(request, 'Error saving invoice') return render(request, 'invoice/create_invoice.html', args) invoice = invoiceform.save(commit=False) invoice.total_amount = total_amount invoice.created_by = request.user invoice.modified_by = request.user invoice.save() invoiceform.save_m2m() formset = InvoiceItemFormset(request.POST, prefix='invoiceitem') for form in formset.forms: if not form.has_changed(): continue else: item = form.save(commit=False) item.created_by = request.user item.modified_by = request.user item.invoice = invoice item.save() messages.success(request, "Invoice saved.") action.send(request.user, verb='saved invoice', action_object=invoice) return reverse_redirect('invoice:view_invoice', [invoice.id])
def onchange_detachment(request, detachment): args = {} detachment = get_object_or_None(Detachment, id=detachment) company = detachment.company if not detachment: messages.warning(request, 'Detachment not found.') else: detachments = company.get_detachments() args['detachments'] = detachments args['selected_company_id'] = company.id args['selected_detachment_id'] = detachment.id costcenters = CostCenter.objects.filter(Q(detachment=detachment) | Q(company=company), is_active=True) args['costcenters_regular'] = costcenters.filter(augmentation=False) args['costcenters_augmentation'] = costcenters.filter( augmentation=True) args['companies'] = Company.objects.filter( is_active=True).order_by('full_name') return render(request, 'invoice/select_client.html', args)
def create_invoice(request, costcenter): args = {} cost_center = get_object_or_None(CostCenter, id=costcenter) values = { 'cost_center': cost_center, } if cost_center.detachment: values['company'] = cost_center.detachment.company args['company'] = cost_center.detachment.company else: values['company'] = cost_center.company args['company'] = cost_center.company args['invoiceform'] = InvoiceForm(initial=values) InvoiceItemFormset.extra = 2 #args['company'] = company args['formset'] = InvoiceItemFormset(prefix='invoiceitem') args['add_types'] = ADD_INVOICE_ITEM_TYPE args['deduct_types'] = DEDUCT_INVOICE_ITEM_TYPE #args['period'] = period args['title'] = 'Create Invoice' args['costcenter'] = cost_center return render(request, 'invoice/create_invoice.html', args)
def view_invoice(request, invoice_id): args = {} invoice = get_object_or_None(Invoice, pk=invoice_id) if not invoice: messages.error(request, "Invoice record not found.") return reverse_redirect('invoice:list_invoices') payments, total_paid = invoice.get_total_paid() items = invoice.invoiceitem_set.order_by('pk') InvoiceItemFormset.extra = 0 args['formset'] = InvoiceItemFormset(instance=invoice, prefix='invoiceitem', queryset=items) args['invoice'] = invoice args['period'] = invoice.period args['invoiceform'] = InvoiceForm(instance=invoice) args['periods'] = ACTIVE_PERIODS args['payment_form'] = InvoicePaymentForm() args['balance'] = invoice.total_amount - total_paid args['add_types'] = ADD_INVOICE_ITEM_TYPE args['deduct_types'] = DEDUCT_INVOICE_ITEM_TYPE args['pay_levels'] = invoice.pay_levels.all() if request.method == 'POST': return update_invoice(request, invoice) return render(request, 'invoice/invoice_detail.html', args)
def view_apts(request, game_id=None, game_level_id=None): level = get_object_or_None(GameLevel, pk=game_level_id) apts = APT.objects.filter(game_level=level) return { 'level':level,'apts':apts }
def view_game(request, game_id=None): game = get_object_or_None(Game, pk=game_id) levels = GameLevel.objects.filter(game=game) return { 'game':game,'levels':levels }
def duplicate_invoice(request, invoice_id): args = {} invoice = get_object_or_None(Invoice, pk=invoice_id) if not invoice: messages.error(request, "Invoice record not found.") return reverse_redirect('invoice:list_invoices') args['invoiceform'] = InvoiceForm(instance=invoice) initial = [] items = InvoiceItem.objects.filter(invoice=invoice, is_active=True).order_by('pk') for i in items: initial.append({ 'type': i.type, 'description': i.description, 'equivalent_guard_shift': i.equivalent_guard_shift, 'hours': i.hours, 'rate': i.rate, 'amount': i.amount }) DuplicateInvoiceItemFormset.extra = len(items) invoiceitem_formset = DuplicateInvoiceItemFormset( queryset=InvoiceItem.objects.none(), prefix='invoiceitem', initial=initial) args['formset'] = invoiceitem_formset args['add_types'] = ADD_INVOICE_ITEM_TYPE args['deduct_types'] = DEDUCT_INVOICE_ITEM_TYPE args['invoice'] = invoice args['title'] = 'Duplicate Invoice' if request.method == 'POST': total_amount = request.POST.get('total_amount') invoiceform = InvoiceForm(request.POST or None) formset = DuplicateInvoiceItemFormset(request.POST, prefix='invoiceitem') args['invoiceform'] = invoiceform args['formset'] = formset if not invoiceform.is_valid(): messages.error(request, "Error duplicating invoice.") return render(request, 'invoice/create_invoice.html', args) for form in formset.forms: if not form.is_valid(): messages.error(request, "Error duplicating invoice.") return render(request, 'invoice/create_invoice.html', args) invoice = invoiceform.save(commit=False) invoice.total_amount = total_amount invoice.created_by = request.user invoice.modified_by = request.user invoice.pk = None invoice.save() invoiceform.save_m2m() for form in formset.forms: if form.is_valid(): item = form.save(commit=False) item.created_by = request.user item.modified_by = request.user item.invoice = invoice item.save() messages.success(request, "Invoice duplicated.") action.send(request.user, verb='duplicated', action_object=invoice) return reverse_redirect('invoice:view_invoice', [invoice.id]) return render(request, 'invoice/create_invoice.html', args)