# Path: web/pos/models.py
# class Product(models.Model):
#     # The different colours, to use as Product.<colour>
#     BLUE = "blue"
#     GREEN = "green"
#     YELLOW = "yellow"
#     ORANGE = "orange"
#     PURPLE = "purple"
#     BLACK = "black"
#     PINK = "pink"
#     CYAN = "cyan"
# 
#     # Colour choices
#     COLOUR_CHOICES = (
#             (BLUE, "Blue"),
#             (GREEN, "Green"),
#             (YELLOW, "Yellow"),
#             (ORANGE, "Orange"),
#             (PURPLE, "Purple"),
#             (BLACK, "Black"),
#             (PINK, "Pink"),
#             (CYAN, "Cyan"),
#     )
# 
#     name = models.CharField(max_length=100,
#                             validators=[validate_product_name])
#     price = models.DecimalField(max_digits=7, decimal_places=2)
#     stock_applies = models.BooleanField()
#     minimum_stock = models.PositiveSmallIntegerField(default=0)
#     stock = models.IntegerField(default=0)
#     code = models.CharField(max_length=50, unique=True, null=True, blank=True)
#     colour = models.CharField(max_length=10,
#                               choices=COLOUR_CHOICES, default="blue")
# 
#     def __str__(self):
#         return self.name
# 
#     def clean(self):
#         validate_product_name(self.name)
#         if self.code == "":
#             self.code = None
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             self.full_clean()
#         return super(Product, self).save(*args, **kwargs)
# 
# class Order(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL,
#                              on_delete=models.CASCADE)
#     total_price = models.DecimalField(max_digits=10,
#                                       decimal_places=2,
#                                       default=0)
#     done = models.BooleanField(default=False)
#     last_change = models.DateTimeField(auto_now=True)
# 
#     def get_absolute_url(self):
#         return reverse('view_order', args=[self.id])
# 
# class Cash(models.Model):
#     amount = models.DecimalField(max_digits=7,
#                                  decimal_places=2,
#                                  default=0)
# 
# class Order_Item(models.Model):
#     product = models.ForeignKey(Product,
#                                 on_delete=models.CASCADE)
#     order = models.ForeignKey(Order, on_delete=models.CASCADE)
#     price = models.DecimalField(max_digits=7, decimal_places=2)
#     name = models.CharField(max_length=100)
#     timestamp = models.DateTimeField(auto_now=True)


import logging
import decimal
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render, get_object_or_404
from django.http import (HttpResponse,
                         HttpResponseForbidden,
                         HttpResponseBadRequest)
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth import login as auth_login
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from .models import Product, Order, Cash, Order_Item
from . import helper
@login_required
def print_current_order(request):
    # Get the user. This is quite a roundabout, sorry
    usr = User.objects.get_by_natural_key(request.user.username)
    q = Order.objects.filter(user=usr)\
                     .order_by('-last_change')

    # This is an edge case that would pretty much never occur
    if q.count() < 1:
        return HttpResponseBadRequest
    return _show_order(request, q[0].id, True)


@login_required
def order_amount(request):
    _, current_order, _ = helper.setup_handling(request)
    return HttpResponse(current_order.total_price)


@login_required
def order_add_product(request, product_id):
    _, current_order, _ = helper.setup_handling(request)

    if product_id.isdigit():
        try:
            to_add = Product.objects.get(id=product_id)
        except ObjectDoesNotExist:
            to_add = get_object_or_404(Product, code=product_id)

    else:

# Path: web/pos/models.py
# class Product(models.Model):
#     # The different colours, to use as Product.<colour>
#     BLUE = "blue"
#     GREEN = "green"
#     YELLOW = "yellow"
#     ORANGE = "orange"
#     PURPLE = "purple"
#     BLACK = "black"
#     PINK = "pink"
#     CYAN = "cyan"
# 
#     # Colour choices
#     COLOUR_CHOICES = (
#             (BLUE, "Blue"),
#             (GREEN, "Green"),
#             (YELLOW, "Yellow"),
#             (ORANGE, "Orange"),
#             (PURPLE, "Purple"),
#             (BLACK, "Black"),
#             (PINK, "Pink"),
#             (CYAN, "Cyan"),
#     )
# 
#     name = models.CharField(max_length=100,
#                             validators=[validate_product_name])
#     price = models.DecimalField(max_digits=7, decimal_places=2)
#     stock_applies = models.BooleanField()
#     minimum_stock = models.PositiveSmallIntegerField(default=0)
#     stock = models.IntegerField(default=0)
#     code = models.CharField(max_length=50, unique=True, null=True, blank=True)
#     colour = models.CharField(max_length=10,
#                               choices=COLOUR_CHOICES, default="blue")
# 
#     def __str__(self):
#         return self.name
# 
#     def clean(self):
#         validate_product_name(self.name)
#         if self.code == "":
#             self.code = None
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             self.full_clean()
#         return super(Product, self).save(*args, **kwargs)
# 
# class Order(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL,
#                              on_delete=models.CASCADE)
#     total_price = models.DecimalField(max_digits=10,
#                                       decimal_places=2,
#                                       default=0)
#     done = models.BooleanField(default=False)
#     last_change = models.DateTimeField(auto_now=True)
# 
#     def get_absolute_url(self):
#         return reverse('view_order', args=[self.id])
# 
# class Cash(models.Model):
#     amount = models.DecimalField(max_digits=7,
#                                  decimal_places=2,
#                                  default=0)
# 
# class Order_Item(models.Model):
#     product = models.ForeignKey(Product,
#                                 on_delete=models.CASCADE)
#     order = models.ForeignKey(Order, on_delete=models.CASCADE)
#     price = models.DecimalField(max_digits=7, decimal_places=2)
#     name = models.CharField(max_length=100)
#     timestamp = models.DateTimeField(auto_now=True)


# Path: web/pos/views.py
import logging
import decimal
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render, get_object_or_404
from django.http import (HttpResponse,
                         HttpResponseForbidden,
                         HttpResponseBadRequest)
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth import login as auth_login
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from .models import Product, Order, Cash, Order_Item
from . import helper

@login_required
def print_current_order(request):
    # Get the user. This is quite a roundabout, sorry
    usr = User.objects.get_by_natural_key(request.user.username)
    q = Order.objects.filter(user=usr)\
                     .order_by('-last_change')

    # This is an edge case that would pretty much never occur
    if q.count() < 1:
        return HttpResponseBadRequest
    return _show_order(request, q[0].id, True)


@login_required
def order_amount(request):
    _, current_order, _ = helper.setup_handling(request)
    return HttpResponse(current_order.total_price)


@login_required
def order_add_product(request, product_id):
    _, current_order, _ = helper.setup_handling(request)

    if product_id.isdigit():
        try:
            to_add = Product.objects.get(id=product_id)
        except ObjectDoesNotExist:
            to_add = get_object_or_404(Product, code=product_id)

    else:

        to_add = get_object_or_404(Product, code=product_id)