Permalink
Browse files

Added ability to void line items.

  • Loading branch information...
maxolasersquad committed Mar 8, 2014
1 parent 0e03eb5 commit bb628e41dc3edaa5e13ccf1790335bb3a7fbc293
View
@@ -121,9 +121,10 @@ def get_totals(self):
total = Decimal(0.0)
tax = Decimal(0.0)
for line_item in self.lineitem_set.all():
- total = total + line_item.price
- if line_item.item.taxable:
- tax = Decimal(tax) + line_item.price * Decimal('.07')
+ if line_item.status == 'ACTIVE':
+ total = total + line_item.price
+ if line_item.item.taxable:
+ tax = Decimal(tax) + line_item.price * Decimal('.07')
tax = tax.quantize(Decimal(10) ** -2).normalize()
paid_total = 0
for tender in self.tender_set.all():
@@ -142,13 +143,17 @@ class LineItem(models.Model):
description = models.CharField(max_length=100)
price = models.DecimalField(max_digits=17, decimal_places=2)
item = models.ForeignKey('inventory.Item')
+ status = models.CharField(max_length=8, default='ACTIVE')
def __unicode__(self):
return str(self.scale) + ' x ' + self.description + ' ' + self.description
def total(self):
return self.price * self.quantity
+ def cancel(self):
+ self.status = 'INACTIVE'
+
class Tender(models.Model):
transaction = models.ForeignKey(Transaction)
amount = models.DecimalField(max_digits=17, decimal_places=2)
View
@@ -94,6 +94,26 @@ def test_paid_tender_ends_transaction(self):
self.transaction.create_line_item(self.item, 1)
self.transaction.create_tender(25.09, 'CASH')
self.assertIsNotNone(self.transaction.finish_date)
+ def test_transaction_totals_with_cancled_item(self):
+ self.transaction.create_line_item(self.item, 1)
+ self.transaction.create_line_item(self.item, 1).cancel()
+ self.assertEqual(transaction_total.total, Decimal('50.18'))
+ self.assertEqual(transaction_total.tax_total, Decimal('3.28'))
+ self.assertEqual(transaction_total.total, Decimal('50.18'))
+ def test_paid_tender_ends_transaction(self):
+ self.transaction.create_line_item(self.item, 1)
+ self.transaction.create_tender(25.09, 'CASH')
+ self.assertIsNotNone(self.transaction.finish_date)
+ def test_transaction_totals_with_cancled_item(self):
+ line_item = self.transaction.create_line_item(self.item, 1)
+ line_item.save()
+ line_item = self.transaction.create_line_item(self.item, 1)
+ line_item.cancel()
+ line_item.save()
+ transaction_total = self.transaction.get_totals()
+ self.assertEqual(transaction_total.sub_total, Decimal('23.45'))
+ self.assertEqual(transaction_total.tax_total, Decimal('1.64'))
+ self.assertEqual(transaction_total.total, Decimal('25.09'))
class LineItemTest(TestCase):
def setUp(self):
View
@@ -7,4 +7,6 @@
url(r'tender_transaction/', views.tender_transaction, name='tender_transaction'),
url(r'end_shift/', views.end_shift, name='end_shift'),
url(r'product_search/', views.product_search, name='product_search'),
+ url(r'cancel_line/', views.cancel_line, name='cancel_line'),
+ url(r'transaction_total/', views.transaction_total, name='transaction_total'),
)
View
@@ -2,6 +2,7 @@
from django.core import serializers
from register.models import *
from inventory.models import *
+from django.views.decorators.csrf import csrf_exempt
def index(request):
current_transaction = Transaction.get_current()
@@ -18,12 +19,12 @@ def process_upc(request):
check = 'true'
item = Item.objects.get(upc=upc.upc[:-1])
transaction = Transaction.get_current()
- transaction.create_line_item(item, 1)
+ line_item = transaction.create_line_item(item, 1)
else:
check = 'false'
item = None
- context_instance = { 'item': item, 'quantity': quantity, 'check_passed': check, 'transaction': transaction.get_totals() }
+ context_instance = { 'item': item, 'quantity': quantity, 'check_passed': check, 'transaction': transaction.get_totals(), 'line_item': line_item }
return render(request, 'register/process_upc.json', context_instance)
@@ -46,3 +47,16 @@ def product_search(request):
results = Item.objects.filter(name__contains=search) | Item.objects.filter(price__contains=search) | Item.objects.filter(vendor__name__contains=search).order_by('name')
context_instance = { 'search': search, 'results': results}
return render(request, 'register/product_search.html', context_instance)
+
+def cancel_line(request):
+ line_item = LineItem.objects.get(id=request.POST['id'])
+ line_item.cancel()
+ line_item.save()
+ context_instance = { 'line_item': line_item }
+ return render(request, 'register/cancel_line.json', context_instance, content_type="application/json")
+
+@csrf_exempt
+def transaction_total(request):
+ current_transaction = Transaction.get_current()
+ context = { 'transaction_total': current_transaction.get_totals() }
+ return render(request, 'register/transaction_total.json', context)
View
@@ -102,3 +102,9 @@
#ringtype-upc {
display: none;
}
+
+.void-line {
+ user-select: none;
+ -webkit-user-select: none;
+ cursor: pointer;
+}
View
@@ -49,7 +49,7 @@ Orthosie.input = {
$('#transactions>table>tbody').html('');
$('#transactions').data('status', 'ring');
}
- $('#transactions>table').append('<tr><td>' + data.vendor + ' ' + data.name + '</td><td>' + data.quantity + ' @ $' + data.price + '</td></tr>');
+ $('#transactions>table').append('<tr id="line-' + data.id + '"><td>' + data.vendor + ' ' + data.name + '</td><td>' + data.quantity + ' @ $' + data.price + '</td><td><i class="fa fa-times text-danger void-line" onclick="Orthosie.input.void_line(' + data.id + ')"></i></td></tr>');
$('#sub_total_value').html('$' + data.subtotal);
$('#tax_total_value').html('$' + data.taxtotal);
$('#paid_total_value').html('$' + data.paidtotal);
@@ -149,5 +149,34 @@ Orthosie.input = {
}
check_digit = (10 - check_digit % 10) % 10;
return check_digit;
+ },
+ void_line: function(id) {
+ post_args = {
+ id: id
+ };
+ post_args[$('#csrf_token>input').attr('name')] = $('#csrf_token>input').attr('value');
+ $.ajax({
+ url: '/register/cancel_line/',
+ data: post_args,
+ type: 'POST',
+ dataType: 'json',
+ success: function(data, status) {
+ $('#line-' + id).addClass('danger');
+ $('#line-' + id + ' td:nth-child(3)').html('')
+ Orthosie.input.update_totals();
+ }
+ });
+ },
+ update_totals: function() {
+ $.ajax({
+ url: '/register/transaction_total',
+ dataType: 'json',
+ success: function(data) {
+ $('#sub_total_value').html('$' + data.subtotal);
+ $('#tax_total_value').html('$' + data.taxtotal);
+ $('#paid_total_value').html('$' + data.paidtotal);
+ $('#total_value').html('$' + data.total);
+ }
+ })
}
}
@@ -0,0 +1,10 @@
+{
+ "id": "{{ line_item.id }}",
+ "upc": "{{ line_item.upc }}",
+ "quantity": {{ line_item.quantity }},
+ "scale": {% if line_item.scale != None %} {{ line_item.scale }} {% else %} null {% endif %},
+ "description": "{{ line_item.description }}",
+ "price": {{ line_item.price }},
+ "item": {{ line_item.item.id }},
+ "status": "{{ line_item.status }}"
+}
@@ -1,6 +1,7 @@
{
"success": {{ check_passed }},
{% if item %}
+ "id": {{ line_item.id }},
"name": "{{ item.name }}",
"vendor": "{{ item.vendor.name }}",
"price": {{ item.price }},
@@ -0,0 +1,6 @@
+{
+ "subtotal": {{ transaction_total.sub_total }},
+ "taxtotal": {{ transaction_total.tax_total }},
+ "paidtotal": {{ transaction_total.paid_total }},
+ "total": {{ transaction_total.total }}
+}
@@ -19,9 +19,14 @@
<table class="table table-striped table-hover table-condensed table-responsive">
<tbody>
{% for line_item in line_items %}
- <tr>
+ <tr id="line-{{ line_item.id }}" {% if line_item.status == 'INACTIVE' %}class="danger"{% endif %}>
<td data-code="{{ line_item.upc }}">{{ line_item.description }}</td>
<td>{{ line_item.quantity }} @ ${{ line_item.price }}</td>
+ <td>
+ {% if line_item.status == 'ACTIVE' %}
+ <i class="fa fa-times text-danger void-line" onclick="Orthosie.input.void_line({{ line_item.id }})"></i>
+ {% endif %}
+ </td>
</tr>
{% endfor %}
</tbody>

0 comments on commit bb628e4

Please sign in to comment.