diff options
author | Filipp Lepalaan <filipp@mac.com> | 2015-11-26 23:20:10 +0200 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2015-11-26 23:20:10 +0200 |
commit | 3f38c74a145e587a257fab0e0f27a03ca3c2c9eb (patch) | |
tree | de699e89769cbda7040a414004b3af13bca8c03a /servo | |
parent | b299a35443a70c8262fd0ec49d8fddb4fe73f7eb (diff) | |
download | Servo-3f38c74a145e587a257fab0e0f27a03ca3c2c9eb.tar.gz Servo-3f38c74a145e587a257fab0e0f27a03ca3c2c9eb.tar.bz2 Servo-3f38c74a145e587a257fab0e0f27a03ca3c2c9eb.zip |
WIP adding location-based stocking report
Diffstat (limited to 'servo')
-rwxr-xr-x | servo/templates/products/index.html | 8 | ||||
-rwxr-xr-x | servo/templates/products/view.html | 8 | ||||
-rw-r--r-- | servo/urls/products.py | 2 | ||||
-rw-r--r-- | servo/views/product.py | 36 |
4 files changed, 34 insertions, 20 deletions
diff --git a/servo/templates/products/index.html b/servo/templates/products/index.html index 1bb75db..be5f8f2 100755 --- a/servo/templates/products/index.html +++ b/servo/templates/products/index.html @@ -26,15 +26,15 @@ <i class="icon-cog"></i> <span class="caret"></span> </a> <ul class="dropdown-menu"> - {% if group.pk and perms.servo.add_product %} + {% if group.pk and perms.servo.add_product %} <li><a href="{% url 'products-edit_category' group.slug %}" data-modal="#modal">{% trans "Edit Category" %}</a></li> <li><a href="{% url 'products-delete_category' group.slug %}" data-modal="#modal">{% trans "Delete Category" %}</a></li> - {% else %} + {% else %} <li class="disabled"><a href="#">{% trans "Edit Category" %}</a></li> <li class="disabled"><a href="#">{% trans "Delete Category" %}</a></li> - {% endif %} + {% endif %} <li class="divider"></li> - <li><a href="{% url 'products-download' %}">{% trans "Download Products" %}</a></li> + <li><a href="{% url 'products-download' group.slug %}">{% trans "Download Products" %}</a></li> <li class="disabled"><a href="{% url 'products-get_inventory_report' %}">{% trans "Download Inventory Report" %}</a></li> <li><a href="{% url 'products-upload_products' %}" data-modal="#modal">{% trans "Upload Products" %}</a></li> <li><a href="{% url 'products-upload_gsx_parts' %}" data-modal="#modal">{% trans "Upload Parts Database" %}</a></li> diff --git a/servo/templates/products/view.html b/servo/templates/products/view.html index 589e7f1..740621d 100755 --- a/servo/templates/products/view.html +++ b/servo/templates/products/view.html @@ -135,13 +135,13 @@ </thead> {% for i in sales %} <tr> - {% with i.order as order %} + {% with i.order as order %} <td><a href="{{ order.get_absolute_url }}">{{ order.code }}</a></td> <td>{{ order.customer_name|default:"-" }}</td> <td>{{ i.price|currency }}</td> <td>{{ i.created_at|date:"SHORT_DATE_FORMAT" }}</td> <td>{{ i.dispatched_at|date:"SHORT_DATE_FORMAT" }}</td> - {% endwith %} + {% endwith %} </tr> {% empty %} <tr><td colspan="5" class="empty muted">{% trans "No Sales Orders" %}</td></tr> @@ -188,9 +188,9 @@ {% for i in invoices %} <tr> <td><a href="{{ i.invoice.get_absolute_url }}">{{ i.invoice.pk|safe }}</a></td> - {% with i.invoice.order as order %} + {% with i.invoice.order as order %} <td><a href="{{ order.get_absolute_url }}">{{ order.code }}</a></td> - {% endwith %} + {% endwith %} <td>{{ i.created_at|date:"SHORT_DATE_FORMAT" }}</td> <td>{{ i.price|currency }}</td> </tr> diff --git a/servo/urls/products.py b/servo/urls/products.py index c17b082..6d54ff1 100644 --- a/servo/urls/products.py +++ b/servo/urls/products.py @@ -8,7 +8,7 @@ urlpatterns = patterns( url(r'^tags/$', "tags", name="products-tags"), url(r'^all/$', "list_products", {'group': 'all'}, name="products-list_products"), - url(r'^download/$', "download_products", + url(r'^(?P<group>[\w\-/]*)/download/$', "download_products", name="products-download"), url(r'^inventory_report/$', "get_inventory_report", name="products-get_inventory_report"), diff --git a/servo/views/product.py b/servo/views/product.py index 60467d4..1ff344e 100644 --- a/servo/views/product.py +++ b/servo/views/product.py @@ -126,20 +126,21 @@ def get_inventory_report(request): """ Returns stocked amount of products at each location """ + import re from django.db import connection cursor = connection.cursor() location_map = {} for l in Location.objects.filter(enabled=True): - location_map[l.pk] = l.title + location_map[str(l.pk)] = l.title # @TODO this should be rewritten as a pivot query # but this will have to do for now. This is still much # faster than using the ORM. query = """SELECT p.id, p.code, l.id, i.amount_stocked FROM servo_product p, servo_inventory i, servo_location l - WHERE p.id = i.product_id AND l.id = i.location_id - ORDER BY p.id ASC""" + WHERE p.id = i.product_id AND l.id = i.location_id + ORDER BY p.id ASC""" cursor.execute(query) response = HttpResponse(content_type="text/plain; charset=utf-8") @@ -147,16 +148,28 @@ def get_inventory_report(request): #response['Content-Disposition'] = 'attachment; filename="%s"' % filename header = ['ID', 'CODE'] + location_map.values() response.write("\t".join(header) + "\n") - results = [] - inventory = {} + inventory, codemap = {}, {} for k in cursor.fetchall(): - response.write(k) - #inventory[k[0]] = - row = {'id': k[0], 'code': k[1]} + product_id = unicode(k[0]) + codemap[product_id] = k[1] # map product IDs to product codes + inv_slot = {k[2]: k[3]} - for r in results: - pass #response.write("\t".join(r) + "\n") + try: + inventory[product_id].append(inv_slot) + except KeyError: + inventory[product_id] = [inv_slot] + + for k, v in inventory.iteritems(): + inventory_cols = [] + for i, x in location_map.iteritems(): + for p in v: + amount = p.get(i, '0') # fill empty inventory slots with zeros + inventory_cols.append(amount) + + code = unicode(codemap[k]) + row = [k, code] + inventory_cols + response.write("\t".join(row) + "\n") return response @@ -230,7 +243,8 @@ def upload_products(request, group=None): product, created = Product.objects.get_or_create(code=cols[1]) - product.title = cols[2].strip(' "').replace('""', '"') # Remove Excel escapes + # Remove Excel escapes + product.title = cols[2].strip(' "').replace('""', '"') product.price_purchase_stock = cols[3].replace(',', '.') product.price_sales_stock = cols[4].replace(',', '.') product.save() |