Skip to content

Commit

Permalink
The Big Stock Update (#45)
Browse files Browse the repository at this point in the history
* Fixed #3, can show all stock

* Fixed #44
  • Loading branch information
Epse authored Feb 28, 2018
1 parent feef211 commit b77d8c8
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 13 deletions.
8 changes: 7 additions & 1 deletion pos/templates/pos/addition.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Payment failed. Cash register now contains: {{ currency }}{{ cash.amount }}
</div>
{% endif %}
{% if stock_error %}
<div class="alert alert-danger alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Not enough in stock of requested product.
</div>
{% endif %}
<table class="table">
<thead>
Expand All @@ -32,7 +38,7 @@
{% endfor %}
{% endif %}
<tr>
<td>Total</td>
<th scope="row">Total</th>
<td>{{ currency }}{{ total_price }}</td>
<td></td>
</tr>
Expand Down
4 changes: 4 additions & 0 deletions pos/templates/pos/order.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
Reset Cash
</a>
</li>
<li class="nav-item">
<a href="{% url 'view_stock' %}" class="nav-link active" title="View Stock" style="cursor:pointer;">View Stock</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link active" href="{% url 'admin:index' %}">Admin</a>
Expand Down Expand Up @@ -123,6 +126,7 @@
}

function removeProduct(product) {
console.log("remove " + product);
$('#additiondiv').load("{%url 'order_remove_product' '' %}" + encodeURIComponent(product));
}

Expand Down
81 changes: 81 additions & 0 deletions pos/templates/pos/stock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Order id {{ order.id }} from {{ order.last_change }}</title>

{% load static %}
<script src="{% static 'pos/js/jquery-3.2.1.min.js' %}">
</script>

<link rel="stylesheet" href="{% static 'pos/css/bootstrap.min.css' %}">

<script src="{% static 'pos/js/bootstrap.bundle.min.js' %}">
</script>

<link rel="apple-touch-icon" sizes="57x57" href="{% static 'pos/img/apple-touch-icon-57x57.png' %}">
<link rel="apple-touch-icon" sizes="60x60" href="{% static 'pos/img/apple-touch-icon-60x60.png' %}">
<link rel="apple-touch-icon" sizes="72x72" href="{% static 'pos/img/apple-touch-icon-72x72.png' %}">
<link rel="apple-touch-icon" sizes="76x76" href="{% static 'pos/img/apple-touch-icon-76x76.png' %}">
<link rel="apple-touch-icon" sizes="114x114" href="{% static 'pos/img/apple-touch-icon-114x114.png' %}">
<link rel="apple-touch-icon" sizes="120x120" href="{% static 'pos/img/apple-touch-icon-120x120.png' %}">
<link rel="apple-touch-icon" sizes="144x144" href="{% static 'pos/img/apple-touch-icon-144x144.png' %}">
<link rel="apple-touch-icon" sizes="152x152" href="{% static 'pos/img/apple-touch-icon-152x152.png' %}">
<link rel="apple-touch-icon" sizes="180x180" href="{% static 'pos/img/apple-touch-icon-180x180.png' %}">
<link rel="icon" type="image/png" sizes="32x32" href="{% static "pos/img/favicon-32x32.png" %}" >
<link rel="icon" type="image/png" sizes="192x192" href="{% static "pos/img/android-chrome-192x192.png" %}" >
<link rel="icon" type="image/png" sizes="96x96" href="{% static "pos/img/favicon-96x96.png" %}">
<link rel="icon" type="image/png" sizes="16x16" href="{% static "pos/img/favicon-16x16.png" %}">
<link rel="manifest" href="{% static "pos/manifest.json" %}">
<link rel="mask-icon" href="{% static "pos/img/safari-pinned-tab.svg" color="#5bbad5" %}">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-TileImage" content="{% static "pos/img/mstile-144x144.png" %}">
<meta name="theme-color" content="#ffffff">

<style type="text/css">
@media print {
.no-print {
display:none;
}
}
</style>
</head>
<body>
<div class="container">
<div class="row text-center">
<div class="col-12">
<h1>{{ company }}</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<p>
<button type="button" class="btn btn-primary no-print" onclick="window.print();">Print</button>
</p>
</div>
</div>
<div class="row">
<div class="col-12">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Stock</th>
</tr>
</thead>
<tbody>
{% if list %}
{% for product in list %}
<tr>
<td>{{ product.name }}</td>
<td>{{ product.stock }}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
3 changes: 2 additions & 1 deletion pos/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
url(r'^print-order/(?P<order_id>[0-9]+)/?$', views.print_order,
name='print_order'),
url(r'^print-current-order/?$', views.print_current_order,
name='print_current_order')
name='print_current_order'),
url(r'^stock/?$', views.view_stock, name='view_stock')
]
60 changes: 49 additions & 11 deletions pos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ def order(request):
return render(request, 'pos/order.html', context=context)


def _addition_no_stock(request):
cash, current_order, currency = helper.setup_handling(request)

total_price = current_order.total_price
list = Order_Item.objects.filter(order=current_order)
context = {
'list': list,
'total_price': total_price,
'cash': cash,
'succesfully_payed': False,
'payment_error': False,
'amount_added': 0,
'currency': currency,
'stock_error': True,
}
return render(request, 'pos/addition.html', context=context)


@login_required
def addition(request):
cash, current_order, currency = helper.setup_handling(request)
Expand All @@ -59,6 +77,7 @@ def addition(request):
'payment_error': False,
'amount_added': 0,
'currency': currency,
'stock-error': False,
}
return render(request, 'pos/addition.html', context=context)

Expand Down Expand Up @@ -108,6 +127,15 @@ def order_add_product(request, product_id):
cash, current_order, _ = helper.setup_handling(request)

to_add = get_object_or_404(Product, id=product_id)

# Make sure we can't go under 0 stock
if to_add.stock_applies:
if to_add.stock < 1:
return _addition_no_stock(request)
else:
to_add.stock -= 1
to_add.save()

Order_Item.objects.create(order=current_order, product=to_add,
price=to_add.price, name=to_add.name)
current_order.total_price = (
Expand All @@ -123,11 +151,16 @@ def order_add_product(request, product_id):
@login_required
def order_remove_product(request, product_id):
cash, current_order, _ = helper.setup_handling(request)
order_product = get_object_or_404(Order_Item, id=product_id)
order_item = get_object_or_404(Order_Item, id=product_id)
order_product = order_item.product

if order_product.stock_applies:
order_product.stock += 1
order_product.save()

current_order.total_price = (
current_order.total_price -
order_product.price).quantize(
order_item.price).quantize(
decimal.Decimal('0.01'))

if current_order.total_price < 0:
Expand All @@ -137,7 +170,7 @@ def order_remove_product(request, product_id):
current_order.total_price = 0

current_order.save()
order_product.delete()
order_item.delete()

# I only need default values.
return addition(request)
Expand All @@ -162,10 +195,6 @@ def payment_cash(request):
cash, current_order, currency = helper.setup_handling(request)

for product in helper.product_list_from_order(current_order):
if product.stock_applies:
product.stock -= 1
product.save()

cash.amount += product.price
amount_added += product.price
cash.save()
Expand Down Expand Up @@ -196,10 +225,6 @@ def payment_card(request):
cash, current_order, currency = helper.setup_handling(request)

for product in helper.product_list_from_order(current_order):
if product.stock_applies:
product.stock -= 1
product.save()

current_order.done = True
current_order.save()
current_order = Order.objects.create(user=request.user)
Expand Down Expand Up @@ -229,3 +254,16 @@ def cash(request, amount):
return HttpResponse('')
else:
return HttpResponseForbidden('403 Forbidden')


@login_required
def view_stock(request):
stock_products = Product.objects.filter(stock_applies=True)
company = helper.get_company()

context = {
'list': stock_products,
'company': company
}

return render(request, 'pos/stock.html', context=context)

0 comments on commit b77d8c8

Please sign in to comment.