Skip to content
Browse files

Moved logic from the view and into the model where it belongs.

  • Loading branch information...
1 parent 52d085c commit 78fbc3a1cfb40e2a2a2235368c6effba93705603 @maxolasersquad committed Jan 24, 2014
Showing with 20 additions and 12 deletions.
  1. +18 −1 register/models.py
  2. +2 −11 register/views.py
View
19 register/models.py
@@ -1,4 +1,3 @@
-
#
# This file is part of Orthosie.
#
@@ -20,6 +19,7 @@
from django.conf import settings
from decimal import Decimal
import time
+from django.core.exceptions import ObjectDoesNotExist
class Shift(models.Model):
begin_date = models.DateTimeField(auto_now=True)
@@ -39,6 +39,15 @@ def create_transaction(self):
if self.finish_date == None:
return self.transaction_set.create(begin_date=timezone.now())
+ @staticmethod
+ def get_current():
+ try:
+ current_shift = Shift.objects.get(finish_date = None)
+ except ObjectDoesNotExist:
+ current_shift = Shift()
+ current_shift.save()
+ return current_shift
+
def get_totals(self):
sub_total = Decimal(0.0)
tax = Decimal(0.0)
@@ -75,6 +84,14 @@ def end_transaction(self):
self.finish_date = timezone.now()
self.save()
+ @staticmethod
+ def get_current():
+ try:
+ current_transaction = Transaction.objects.get(finish_date = None)
+ except ObjectDoesNotExist:
+ current_transaction = Shift.get_current().create_transaction()
+ return current_transaction
+
def print_receipt(self):
r = Receipt(self)
r.print()
View
13 register/views.py
@@ -1,18 +1,9 @@
from django.shortcuts import render
from register.models import *
from inventory.models import *
-from django.core.exceptions import ObjectDoesNotExist
def index(request):
- try:
- current_transaction = Transaction.objects.get(finish_date = None)
- except ObjectDoesNotExist:
- try:
- current_shift = Shift.objects.get(finish_date = None)
- except ObjectDoesNotExist:
- current_shift = Shift()
- current_shift.save()
- current_transaction = current_shift.create_transaction()
+ current_transaction = Transaction.get_current()
line_items = current_transaction.lineitem_set.all()
transaction_total = current_transaction.get_totals()
context = { 'line_items': line_items, 'transaction_total': transaction_total }
@@ -25,7 +16,7 @@ def process_upc(request):
if upc.verify_check_digit():
check = 'true'
item = Item.objects.get(upc=upc.upc[:-1])
- transaction = Transaction.objects.get(finish_date = None)
+ transaction = Transaction.get_current()
transaction.create_line_item(item, 1)
else:
check = 'false'

0 comments on commit 78fbc3a

Please sign in to comment.
Something went wrong with that request. Please try again.