1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.CORE |
4
|
|
|
# |
5
|
|
|
# Copyright 2018 by it's authors. |
6
|
|
|
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst. |
7
|
|
|
|
8
|
|
|
from operator import itemgetter |
9
|
|
|
from operator import methodcaller |
10
|
|
|
|
11
|
|
|
from bika.lims import bikaMessageFactory as _ |
12
|
|
|
from bika.lims.browser import BrowserView |
13
|
|
|
from bika.lims.utils import createPdf |
14
|
|
|
from Products.CMFCore.utils import getToolByName |
15
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class View(BrowserView): |
19
|
|
|
"""Supply Order View |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
content = ViewPageTemplateFile("templates/supplyorder_content.pt") |
23
|
|
|
template = ViewPageTemplateFile("templates/supplyorder_view.pt") |
24
|
|
|
title = _("Supply Order") |
25
|
|
|
|
26
|
|
|
def __init__(self, context, request): |
27
|
|
|
super(View, self).__init__(context, request) |
28
|
|
|
self.icon = "{}/{}".format( |
29
|
|
|
self.portal_url, |
30
|
|
|
"/++resource++bika.lims.images/supplyorder_big.png") |
31
|
|
|
|
32
|
|
|
def __call__(self): |
33
|
|
|
context = self.context |
34
|
|
|
portal = self.portal |
35
|
|
|
setup = portal.bika_setup |
36
|
|
|
|
37
|
|
|
# Collect general data |
38
|
|
|
self.orderDate = self.ulocalized_time(context.getOrderDate()) |
39
|
|
|
self.contact = context.getContact() |
40
|
|
|
self.contact = self.contact.getFullname() if self.contact else "" |
41
|
|
|
self.subtotal = "%.2f" % context.getSubtotal() |
42
|
|
|
self.vat = "%.2f" % context.getVATAmount() |
43
|
|
|
self.total = "%.2f" % context.getTotal() |
44
|
|
|
# Set the title |
45
|
|
|
self.title = context.Title() |
46
|
|
|
# Collect order item data |
47
|
|
|
items = context.supplyorder_lineitems |
48
|
|
|
|
49
|
|
|
self.items = [] |
50
|
|
|
for item in items: |
51
|
|
|
prodid = item["Product"] |
52
|
|
|
product = setup.bika_labproducts[prodid] |
53
|
|
|
price = float(item["Price"]) |
54
|
|
|
vat = float(item["VAT"]) |
55
|
|
|
qty = float(item["Quantity"]) |
56
|
|
|
self.items.append({ |
57
|
|
|
"title": product.Title(), |
58
|
|
|
"description": product.Description(), |
59
|
|
|
"volume": product.getVolume(), |
60
|
|
|
"unit": product.getUnit(), |
61
|
|
|
"price": price, |
62
|
|
|
"vat": "%s%%" % vat, |
63
|
|
|
"quantity": qty, |
64
|
|
|
"totalprice": "%.2f" % (price * qty) |
65
|
|
|
}) |
66
|
|
|
self.items = sorted(self.items, key=itemgetter("title")) |
67
|
|
|
# Render the template |
68
|
|
|
return self.template() |
69
|
|
|
|
70
|
|
|
def getPreferredCurrencyAbreviation(self): |
71
|
|
|
return self.context.bika_setup.getCurrency() |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
class EditView(BrowserView): |
75
|
|
|
"""Supply Order Edit View |
76
|
|
|
""" |
77
|
|
|
|
78
|
|
|
template = ViewPageTemplateFile("templates/supplyorder_edit.pt") |
79
|
|
|
field = ViewPageTemplateFile("templates/row_field.pt") |
80
|
|
|
|
81
|
|
|
def __call__(self): |
82
|
|
|
portal = self.portal |
83
|
|
|
request = self.request |
84
|
|
|
context = self.context |
85
|
|
|
setup = portal.bika_setup |
86
|
|
|
# Allow adding items to this context |
87
|
|
|
context.setConstrainTypesMode(0) |
88
|
|
|
# Collect the products |
89
|
|
|
products = setup.bika_labproducts.objectValues("LabProduct") |
90
|
|
|
|
91
|
|
|
# Handle for submission and regular request |
92
|
|
|
if "submit" in request: |
93
|
|
|
portal_factory = getToolByName(context, "portal_factory") |
94
|
|
|
context = portal_factory.doCreate(context, context.id) |
95
|
|
|
context.processForm() |
96
|
|
|
# Clear the old line items |
97
|
|
|
context.supplyorder_lineitems = [] |
98
|
|
|
# Process the order item data |
99
|
|
|
for prodid, qty in request.form.items(): |
100
|
|
|
if prodid.startswith("product_") and qty and float(qty) > 0: |
101
|
|
|
prodid = prodid.replace("product_", "") |
102
|
|
|
product = setup.bika_labproducts[prodid] |
103
|
|
|
context.supplyorder_lineitems.append( |
104
|
|
|
{"Product": prodid, |
105
|
|
|
"Quantity": qty, |
106
|
|
|
"Price": product.getPrice(), |
107
|
|
|
"VAT": product.getVAT()}) |
108
|
|
|
|
109
|
|
|
# Redirect to the list of orders |
110
|
|
|
obj_url = context.absolute_url_path() |
111
|
|
|
request.response.redirect(obj_url) |
112
|
|
|
return |
113
|
|
|
else: |
114
|
|
|
self.orderDate = context.Schema()["OrderDate"] |
115
|
|
|
self.contact = context.Schema()["Contact"] |
116
|
|
|
self.subtotal = "%.2f" % context.getSubtotal() |
117
|
|
|
self.vat = "%.2f" % context.getVATAmount() |
118
|
|
|
self.total = "%.2f" % context.getTotal() |
119
|
|
|
# Prepare the products |
120
|
|
|
items = context.supplyorder_lineitems |
121
|
|
|
self.products = [] |
122
|
|
|
products = sorted(products, key=methodcaller("Title")) |
123
|
|
|
for product in products: |
124
|
|
|
item = [o for o in items if o["Product"] == product.getId()] |
125
|
|
|
quantity = item[0]["Quantity"] if len(item) > 0 else 0 |
126
|
|
|
self.products.append({ |
127
|
|
|
"id": product.getId(), |
128
|
|
|
"title": product.Title(), |
129
|
|
|
"description": product.Description(), |
130
|
|
|
"volume": product.getVolume(), |
131
|
|
|
"unit": product.getUnit(), |
132
|
|
|
"price": product.getPrice(), |
133
|
|
|
"vat": "%s%%" % product.getVAT(), |
134
|
|
|
"quantity": quantity, |
135
|
|
|
"total": (float(product.getPrice()) * float(quantity)), |
136
|
|
|
}) |
137
|
|
|
# Render the template |
138
|
|
|
return self.template() |
139
|
|
|
|
140
|
|
|
def getPreferredCurrencyAbreviation(self): |
141
|
|
|
return self.context.bika_setup.getCurrency() |
142
|
|
|
|
143
|
|
|
|
144
|
|
View Code Duplication |
class PrintView(View): |
|
|
|
|
145
|
|
|
"""Supply Order Print View |
146
|
|
|
""" |
147
|
|
|
template = ViewPageTemplateFile('templates/supplyorder_print.pt') |
148
|
|
|
|
149
|
|
|
def __call__(self): |
150
|
|
|
html = super(PrintView, self).__call__() |
151
|
|
|
pdf = createPdf(html) |
152
|
|
|
filename = "{}.pdf".format(self.context.getId()) |
153
|
|
|
return self.download(pdf, filename) |
154
|
|
|
|
155
|
|
|
def download(self, data, filename, content_type="application/pdf"): |
156
|
|
|
"""Download the PDF |
157
|
|
|
""" |
158
|
|
|
self.request.response.setHeader( |
159
|
|
|
"Content-Disposition", "inline; filename=%s" % filename) |
160
|
|
|
self.request.response.setHeader("Content-Type", content_type) |
161
|
|
|
self.request.response.setHeader("Content-Length", len(data)) |
162
|
|
|
self.request.response.setHeader("Cache-Control", "no-store") |
163
|
|
|
self.request.response.setHeader("Pragma", "no-cache") |
164
|
|
|
self.request.response.write(data) |
165
|
|
|
|