From 4cd598f33e1d4643a6ce5a7f4997f3e0d48729b9 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 28 Nov 2012 15:47:00 -0800 Subject: [PATCH] add inventory worksheet report --- .../pyramid/reports/inventory_worksheet.mako | 96 +++++++++++++++++++ .../pyramid/templates/reports/inventory.mako | 29 ++++++ rattail/pyramid/views/reports.py | 95 ++++++++++++++---- 3 files changed, 202 insertions(+), 18 deletions(-) create mode 100644 rattail/pyramid/reports/inventory_worksheet.mako create mode 100644 rattail/pyramid/templates/reports/inventory.mako diff --git a/rattail/pyramid/reports/inventory_worksheet.mako b/rattail/pyramid/reports/inventory_worksheet.mako new file mode 100644 index 00000000..b39ff57f --- /dev/null +++ b/rattail/pyramid/reports/inventory_worksheet.mako @@ -0,0 +1,96 @@ + + + + + Inventory Worksheet : ${department.name} + + + + +

Inventory Worksheet

+

Department:  ${department.name} (${department.number})

+

generated on ${date} at ${time}

+
+ + + % for subdepartment in department.subdepartments: + <% products = get_products(subdepartment) %> + % if products: + + + + + + + + + + % for product in products: + + + + + + + % endfor + + + % endif + % endfor +
Subdepartment:  ${subdepartment.name} (${subdepartment.number})
UPCBrandDescriptionCount
${get_upc(product)}${product.brand or ''}${product.description} 
+
+ + diff --git a/rattail/pyramid/templates/reports/inventory.mako b/rattail/pyramid/templates/reports/inventory.mako new file mode 100644 index 00000000..8fa5ac1b --- /dev/null +++ b/rattail/pyramid/templates/reports/inventory.mako @@ -0,0 +1,29 @@ +<%inherit file="/reports/base.mako" /> + +<%def name="title()">Report : Inventory Worksheet + +

Please provide the following criteria to generate your report:

+
+ +${h.form(request.current_route_url())} + +
+ +
+ +
+
+ +
+ ${h.checkbox('weighted-only', label=h.literal("Include items sold by weight only."))} +
+ +
+ ${h.submit('submit', "Generate Report")} +
+ +${h.end_form()} diff --git a/rattail/pyramid/views/reports.py b/rattail/pyramid/views/reports.py index 8556e874..11729922 100644 --- a/rattail/pyramid/views/reports.py +++ b/rattail/pyramid/views/reports.py @@ -14,10 +14,75 @@ from pyramid.response import Response import edbob from edbob.pyramid import Session +from edbob.files import resource_path import rattail +plu_upc_pattern = re.compile(r'^000000000(\d{5})$') +weighted_upc_pattern = re.compile(r'^002(\d{5})00000\d$') + +def get_upc(product): + upc = '%014u' % product.upc + m = plu_upc_pattern.match(upc) + if m: + return str(int(m.group(1))) + m = weighted_upc_pattern.match(upc) + if m: + return str(int(m.group(1))) + return upc + + +def inventory_report(request): + """ + This is the "Inventory Worksheet" report. + """ + + departments = Session.query(rattail.Department) + + if request.params.get('department'): + department = departments.get(request.params['department']) + if department: + body = write_inventory_worksheet(request, department) + response = Response(content_type='text/html') + response.headers['Content-Length'] = len(body) + response.headers['Content-Disposition'] = 'attachment; filename=inventory.html' + response.text = body + return response + + departments = departments.order_by(rattail.Department.name) + departments = departments.all() + return{'departments': departments} + + +def write_inventory_worksheet(request, department): + """ + Generates the Inventory Worksheet report. + """ + + def get_products(subdepartment): + q = Session.query(rattail.Product) + q = q.outerjoin(rattail.Brand) + q = q.filter(rattail.Product.subdepartment == subdepartment) + if request.params.get('weighted-only'): + q = q.filter(rattail.Product.unit_of_measure == rattail.UNIT_OF_MEASURE_POUND) + q = q.order_by(rattail.Brand.name, rattail.Product.description) + return q.all() + + now = edbob.local_time() + data = dict( + date=now.strftime('%a %d %b %Y'), + time=now.strftime('%I:%M %p'), + department=department, + get_products=get_products, + get_upc=get_upc, + ) + + report = resource_path('rattail.pyramid:reports/inventory_worksheet.mako') + template = Template(filename=report) + return template.render(**data) + + def ordering_report(request): """ This is the "Ordering Worksheet" report. @@ -37,7 +102,7 @@ def ordering_report(request): response = Response(content_type='text/html') response.headers['Content-Length'] = len(body) response.headers['Content-Disposition'] = 'attachment; filename=ordering.html' - response.body = body + response.text = body return response return {} @@ -60,19 +125,6 @@ def write_ordering_worksheet(vendor, departments): costs[dept].setdefault(subdept, []) costs[dept][subdept].append(cost) - plu_upc_pattern = re.compile(r'^0000000(\d{5})$') - weighted_upc_pattern = re.compile(r'^02(\d{5})00000$') - - def get_upc(prod): - upc = '%012u' % prod.upc - m = plu_upc_pattern.match(upc) - if m: - return str(int(m.group(1))) - m = weighted_upc_pattern.match(upc) - if m: - return str(int(m.group(1))) - return upc - now = edbob.local_time() data = dict( vendor=vendor, @@ -83,12 +135,19 @@ def write_ordering_worksheet(vendor, departments): rattail=rattail, ) - report = os.path.join(os.path.dirname(__file__), os.pardir, 'reports', 'ordering_worksheet.mako') - report = os.path.abspath(report) - template = Template(filename=report, disable_unicode=True) + report = resource_path('rattail.pyramid:reports/ordering_worksheet.mako') + template = Template(filename=report) return template.render(**data) def includeme(config): + + config.add_route('reports.inventory', '/reports/inventory') + config.add_view(inventory_report, + route_name='reports.inventory', + renderer='/reports/inventory.mako') + config.add_route('reports.ordering', '/reports/ordering') - config.add_view(ordering_report, route_name='reports.ordering', renderer='/reports/ordering.mako') + config.add_view(ordering_report, + route_name='reports.ordering', + renderer='/reports/ordering.mako')