Skip to content

Commit

Permalink
Added paid button quick link and paid date to invoice, closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-bridge committed Jan 28, 2019
1 parent 8202a1d commit 49c54fd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions PyInvoice/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
url(r'^api/get_company_for_form/$', api.get_company_for_form, name='get_company_for_form'),
url(r'^api/invoice_logo_upload/$', api.invoice_photo_upload, name='invoice_photo_upload'),
url(r'^api/mark_sent', api.mark_invoice_sent, name='mark_invoice_sent'),
url(r'^api/mark_paid', api.mark_invoice_paid, name='mark_invoice_paid'),
url(r'^api/get_expense_items_for_modal/$', api.get_expense_items_for_modal, name='get_expense_items_for_modal'),
url(r'^api/get_items_for_delete_modal/$', api.get_items_for_delete_modal, name='get_items_for_delete_modal'),
url(r'^api/delete_item', api.delete_item, name='delete_item'),
Expand Down
16 changes: 16 additions & 0 deletions invoice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ def mark_invoice_sent(request):
return HttpResponse(std_json.dumps(context, cls=DjangoJSONEncoder), content_type='application/json')


@login_required()
def mark_invoice_paid(request):
context = dict()
if request.method == 'POST' and request.is_ajax():
invoice_pk = request.POST.get('invoice_pk', '0')

invoice = models.Invoice.objects.update_or_create(pk=invoice_pk, defaults={
'paid_date': timezone.now(),
'paid': True,
})
# context['paid_date'] = invoice[0].paid_date
context['paid_date'] = datetime.strftime(invoice[0].paid_date.date(), '%d %b %Y')

return HttpResponse(std_json.dumps(context, cls=DjangoJSONEncoder), content_type='application/json')


@login_required()
def get_items_for_delete_modal(request):
context = dict()
Expand Down
17 changes: 17 additions & 0 deletions invoice/static/js/ajax_form_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ function markInvoiceSent(invoice_pk) {
});
}

function markInvoicePaid(invoice_pk) {
$.ajax({
url : "/api/mark_paid", // the endpoint
type : "POST", // http method
data : {
invoice_pk: invoice_pk
},
traditional: true,

success : function(data) {
// console.log(data);
$('#invoice-paid-date').text(data['paid_date']);
$('#mark_paid_button').addClass('scale-out-button')
}
});
}

function updateItem() {
$.ajax({
url : "/invoice/item/update/", // the endpoint
Expand Down
9 changes: 6 additions & 3 deletions invoice/templates/invoice_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <h4>

<br>
<dt>Paid</dt>
<dd>{{ invoice.paid }}</dd>
<dd id="invoice-paid-date">{{ invoice.paid_date.date|default:'-' }}</dd>

{% if invoice.utr or invoice.utr != invoice.company.require_utr %}
<dt>UTR</dt>
Expand Down Expand Up @@ -220,10 +220,13 @@ <h4>
<!-- Reverse Order -->
{% if not edit %}
{% if not invoice.sent_date %}
<li><a onclick="markInvoiceSent({{ invoice.id }})" id="mark_sent_button" class="btn-floating green lighten-1 tooltipped" data-position="top" data-tooltip="Mark Sent"><i class="material-icons">done</i></a></li>
<li><a onclick="markInvoiceSent({{ invoice.id }})" id="mark_sent_button" class="btn-floating green lighten-1 tooltipped" data-position="top" data-tooltip="Mark Sent"><i class="material-icons">reply</i></a></li>
{% endif %}
{% if not invoice.paid_date %}
<li><a onclick="markInvoicePaid({{ invoice.id }})" id="mark_paid_button" class="btn-floating orange lighten-1 tooltipped" data-position="top" data-tooltip="Mark Paid"><i class="material-icons">attach_money</i></a></li>
{% endif %}
<li><a href="/invoice/{{ invoice.id }}/print" target="_blank" class="btn-floating purple lighten-1 tooltipped" data-position="top" data-tooltip="Print"><i class="material-icons">print</i></a></li>
<li><a href="edit/" class="btn-floating"><i class="material-icons tooltipped" data-position="top" data-tooltip="Edit">edit</i></a></li>
<li><a href="edit/" class="btn-floating orange lighten-1"><i class="material-icons tooltipped" data-position="top" data-tooltip="Edit">edit</i></a></li>
{% else %}
<li><a class="btn-floating pink lighten-1" onclick="showDeleteModal('{{ invoice }}', {{ invoice.pk }}, 'Invoice')"><i class="material-icons tooltipped" data-position="top" data-tooltip="Delete">delete</i></a></li>
<li><button name="action" type="submit" class="btn-floating light-blue lighten-1"><i class="material-icons tooltipped" data-position="top" data-tooltip="Save">save</i></button></li>
Expand Down
9 changes: 9 additions & 0 deletions invoice/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.utils import timezone
from dateutil import parser
from django.urls import reverse
from django.db.models import Q
Expand Down Expand Up @@ -112,11 +113,19 @@ def invoice_update(request):
defaults['paid'] = bool(defaults.pop('paid', None))
defaults['is_quote'] = bool(defaults.pop('is_quote', None))

if defaults['paid']:
defaults['paid_date'] = timezone.now()
else:
defaults['paid_date'] = None

if not defaults['sent_date'] == '':
defaults['sent_date'] = parser.parse(defaults.pop('sent_date', None))
else:
defaults['sent_date'] = None

if defaults['paid']:
defaults['sent_date'] = timezone.now()

if invoice_pk == 0:
invoice = models.Invoice.objects.create(**defaults)
context['url'] = reverse('invoice_edit', args=[invoice.pk])
Expand Down

0 comments on commit 49c54fd

Please sign in to comment.