-
Notifications
You must be signed in to change notification settings - Fork 5
/
report_html_stock.py
373 lines (311 loc) · 11.6 KB
/
report_html_stock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: utf-8 -*-
from itertools import groupby, imap, chain
from dateutil.relativedelta import relativedelta
from trytond.pool import Pool, PoolMeta
from trytond.model import fields, ModelView
from trytond.wizard import Wizard, Button, StateAction, StateView
from trytond.transaction import Transaction
from openlabs_report_webkit import ReportWebkit
__all__ = [
'PickingList', 'SupplierRestockingList', 'CustomerReturnRestockingList',
'ConsolidatedPickingList', 'ProductLedgerStartView', 'ProductLedgerReport',
'ProductLedger', 'InternalShipmentReport'
]
__metaclass__ = PoolMeta
class ReportMixin(ReportWebkit):
"""
Mixin Class to inherit from, for all HTML reports.
"""
@classmethod
def wkhtml_to_pdf(cls, data, options=None):
"""
Call wkhtmltopdf to convert the html to pdf
"""
Company = Pool().get('company.company')
company = ''
if Transaction().context.get('company'):
company = Company(Transaction().context.get('company')).party.name
opts = {
'margin-bottom': '0.50in',
'margin-left': '0.50in',
'margin-right': '0.50in',
'margin-top': '0.50in',
'footer-font-size': '8',
'footer-left': company,
'footer-line': '',
'footer-right': '[page]/[toPage]',
'footer-spacing': '5',
"page-size": "Letter"
}
if options:
opts.update(options)
return super(ReportMixin, cls).wkhtml_to_pdf(
data, options=opts
)
@classmethod
def get_sorted_moves(cls, records):
"""
Sorting the moves for each shipment
"""
sorted_moves = {}
for shipment in records:
sorted_moves[shipment.id] = sorted(
shipment.inventory_moves,
key=lambda m: (m.from_location, m.to_location)
)
return sorted_moves
class PickingList(ReportMixin):
"""
Picking List Report
"""
__name__ = 'report.picking_list'
@staticmethod
def sort_inventory_moves(shipment, sort_key=None):
"""
A sorter that can be overwritten by downstream modules
"""
return sorted(shipment.inventory_moves, key=sort_key)
@classmethod
def get_context(cls, records, data):
report_context = super(PickingList, cls).get_context(records, data)
report_context['sort_inventory_moves'] = cls.sort_inventory_moves
report_context['sort_key'] = lambda move: (
move.from_location.rec_name, move.product.name
)
return report_context
class ConsolidatedPickingList(ReportMixin):
"""
Consolidated Picking List.
"""
__name__ = 'report.consolidated_picking_list'
@classmethod
def group_key(cls, move):
"""
Key function for grouping and sorting of
moves
"""
return (move.from_location, move.product)
@classmethod
def get_moves(cls, shipment):
return shipment.inventory_moves
@classmethod
def get_product_repr_from(cls, key):
"""
Returns the product representation from the key
"""
return key[1].rec_name
@classmethod
def get_location_repr_from(cls, key):
"""
Returns the location representation from the key
"""
return key[0].rec_name
@classmethod
def get_context(cls, records, data):
"""
The default implementation groups by product
and sorts by from_location.
"""
report_context = super(ConsolidatedPickingList, cls).get_context(
records, data
)
report_context['grouped_moves'] = []
for key, grouper in groupby(
# Sort all the moves from all shipments
# and then group it
list(sorted(
# Chain all inventory moves from all shipments
chain(*imap(lambda s: cls.get_moves(s), records)),
key=cls.group_key
)), cls.group_key):
moves = list(grouper)
# TODO: Sum totals everything, the UOM to base UOM conversion
# is not done
report_context['grouped_moves'].append(
(key, moves, sum(map(lambda m: m.quantity, moves)))
)
report_context['get_product_repr_from'] = cls.get_product_repr_from
report_context['get_location_repr_from'] = cls.get_location_repr_from
return report_context
class SupplierRestockingList(ReportMixin):
'Supplier Restocking List'
__name__ = 'report.supplier_restocking_list'
@classmethod
def get_context(cls, records, data):
report_context = super(SupplierRestockingList, cls).get_context(
records, data
)
sorted_moves = cls.get_sorted_moves(records)
report_context['moves'] = sorted_moves
return report_context
class CustomerReturnRestockingList(ReportMixin):
'Customer Return Restocking List'
__name__ = 'report.customer_return_restocking_list'
@classmethod
def get_context(cls, records, data):
report_context = super(CustomerReturnRestockingList, cls).get_context(
records, data
)
sorted_moves = cls.get_sorted_moves(records)
report_context['moves'] = sorted_moves
return report_context
class DeliveryNote(ReportMixin):
"Delivery Note"
__name__ = 'report.delivery_note'
class InternalShipmentReport(ReportMixin):
__name__ = 'report.internal_shipment'
class ProductLedgerStartView(ModelView):
'Product Ledger Start'
__name__ = 'product.product.ledger.start'
products = fields.One2Many(
'product.product', None, 'Products', required=True,
domain=[
('type', '=', 'goods')
], add_remove=[('type', '=', 'goods')]
)
warehouses = fields.One2Many(
'stock.location', None, 'Warehouses',
domain=[
('type', '=', 'warehouse')
], add_remove=[('type', '=', 'warehouse')]
)
start_date = fields.Date('Start Date', required=True)
end_date = fields.Date('End Date', required=True)
@staticmethod
def default_start_date():
Date = Pool().get('ir.date')
return Date.today() - relativedelta(months=1)
@staticmethod
def default_end_date():
Date = Pool().get('ir.date')
return Date.today()
class ProductLedgerReport(ReportMixin):
'Product Ledger Report'
__name__ = 'report.product_ledger'
@classmethod
def get_purchases(cls, product_id, data):
Move = Pool().get('stock.move')
return Move.search([
('effective_date', '>=', data['start_date']),
('effective_date', '<=', data['end_date']),
('product', '=', product_id),
('state', '=', 'done'),
('from_location.type', '=', 'supplier'),
], order=[('effective_date', 'asc')])
@classmethod
def get_productions(cls, product_id, data):
Move = Pool().get('stock.move')
return Move.search([
('effective_date', '>=', data['start_date']),
('effective_date', '<=', data['end_date']),
('product', '=', product_id),
('state', '=', 'done'),
('from_location.type', '=', 'production'),
], order=[('effective_date', 'asc')])
@classmethod
def get_customers(cls, product_id, data):
Move = Pool().get('stock.move')
return Move.search([
('effective_date', '>=', data['start_date']),
('effective_date', '<=', data['end_date']),
('product', '=', product_id),
('state', '=', 'done'),
('to_location.type', '=', 'customer'),
], order=[('effective_date', 'asc')])
@classmethod
def get_lost_and_founds(cls, product_id, data):
Move = Pool().get('stock.move')
return Move.search([
('effective_date', '>=', data['start_date']),
('effective_date', '<=', data['end_date']),
('product', '=', product_id),
('state', '=', 'done'),
('from_location.type', '=', 'lost_found'),
], order=[('effective_date', 'asc')])
@classmethod
def get_consumed(cls, product_id, data):
Move = Pool().get('stock.move')
return Move.search([
('effective_date', '>=', data['start_date']),
('effective_date', '<=', data['end_date']),
('product', '=', product_id),
('state', '=', 'done'),
('to_location.type', '=', 'production'),
], order=[('effective_date', 'asc')])
@classmethod
def _get_total_quantity(cls, moves):
"""
Returns sum of quantity for list of stock moves
"""
sum = 0.0
for move in moves:
sum += move.internal_quantity
return sum
@classmethod
def get_summary(cls, record, data):
Product = Pool().get('product.product')
rv = {}
product = record['product']
with Transaction().set_context(
locations=data['warehouses'],
stock_date_end=data['start_date'] - relativedelta(days=1)
):
rv['opening_stock'] = Product(product.id).quantity
with Transaction().set_context(
locations=data['warehouses'], stock_date_end=data['end_date']
):
rv['closing_stock'] = Product(product.id).quantity
rv['purchased'] = cls._get_total_quantity(record['purchases'])
rv['produced'] = cls._get_total_quantity(record['productions'])
rv['customer'] = cls._get_total_quantity(record['customers'])
rv['lost'] = cls._get_total_quantity(record['lost_and_founds'])
rv['consumed'] = cls._get_total_quantity(record['consumed'])
return rv
@classmethod
def get_context(cls, objects, data):
Product = Pool().get('product.product')
Locations = Pool().get('stock.location')
report_context = super(ProductLedgerReport, cls).get_context(
objects, data
)
records = []
summary = {}
for product_id in data['products']:
product = Product(product_id)
record = {
'product': product,
'purchases': cls.get_purchases(product.id, data),
'productions': cls.get_productions(product.id, data),
'customers': cls.get_customers(product.id, data),
'lost_and_founds': cls.get_lost_and_founds(product.id, data),
'consumed': cls.get_consumed(product.id, data)
}
records.append(record)
summary[product] = cls.get_summary(record, data)
report_context['summary'] = summary
report_context['warehouses'] = Locations.browse(data['warehouses'])
return report_context
class ProductLedger(Wizard):
'Wizard for generating product ledger'
__name__ = 'product.product.ledger.wizard'
start = StateView(
'product.product.ledger.start',
'report_html_stock.wizard_product_ledger_start_form',
[
Button('Cancel', 'end', 'tryton-cancel'),
Button('View', 'view', 'tryton-go-next', default=True),
]
)
view = StateAction('report_html_stock.report_product_ledger')
def default_start(self, fields):
return {
'products': Transaction().context.get('active_ids'),
}
def do_view(self, action):
data = {
'products': map(int, self.start.products),
'warehouses': map(int, self.start.warehouses),
'start_date': self.start.start_date,
'end_date': self.start.end_date,
}
return action, data