-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshipment.py
137 lines (114 loc) · 4.34 KB
/
shipment.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
# -*- coding: utf-8 -*-
"""
shipment.py
:copyright: (c) 2015 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from collections import defaultdict
from openlabs_report_webkit import ReportWebkit
from trytond.pool import Pool
from trytond.transaction import Transaction
from trytond.model import ModelView
from trytond.wizard import Wizard, StateAction, StateView, Button
__all__ = [
'ItemsWaitingShipmentReport', 'ItemsWaitingShipmentStart',
'ItemsWaitingShipmentReportWizard'
]
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
options = {
'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',
}
return super(ReportMixin, cls).wkhtml_to_pdf(
data, options=options
)
class ItemsWaitingShipmentReport(ReportMixin):
"""
Items Waiting Shipment Report
"""
__name__ = 'report.items_waiting_shipment'
@classmethod
def parse(cls, report, records, data, localcontext):
ShipmentOut = Pool().get('stock.shipment.out')
StockLocation = Pool().get('stock.location')
Date = Pool().get('ir.date')
Product = Pool().get('product.product')
domain = [('state', 'in', ['assigned', 'waiting'])]
shipments = ShipmentOut.search(domain)
moves_by_products = {}
product_quantities = defaultdict(int)
for shipment in shipments:
for move in shipment.inventory_moves:
moves_by_products.setdefault(
move.product, []).append(move)
warehouses = StockLocation.search([
('type', '=', 'warehouse'),
])
products = moves_by_products.keys()
with Transaction().set_context(
stock_skip_warehouse=True,
stock_date_end=Date.today(),
stock_assign=True,
):
pbl = Product.products_by_location(
location_ids=map(int, warehouses),
product_ids=map(int, products),
)
for key, qty in pbl.iteritems():
_, product_id = key
product_quantities[product_id] += qty
localcontext.update({
'moves_by_products': moves_by_products,
# TODO: Report is already available on context
# Use that and remove this context variable
'report_ext': report.extension,
'product_quantities': product_quantities,
})
return super(ItemsWaitingShipmentReport, cls).parse(
report, records, data, localcontext
)
@classmethod
def get_jinja_filters(cls):
rv = super(ItemsWaitingShipmentReport, cls).get_jinja_filters()
rv['oldest_date'] = lambda moves: sorted(
moves, key=lambda m: m.planned_date)[0].planned_date
rv['quantity_in_state'] = lambda moves, state: sum(
[m.quantity for m in moves if m.state == state]
)
return rv
class ItemsWaitingShipmentStart(ModelView):
'Generate Items Waiting Shipments Report'
__name__ = 'report.items_waiting_shipment_wizard.start'
class ItemsWaitingShipmentReportWizard(Wizard):
'Generate Items Waiting Shipments Report Wizard'
__name__ = 'report.items_waiting_shipment_wizard'
start = StateView(
'report.items_waiting_shipment_wizard.start',
'waiting_customer_shipment_report.items_waiting_shipments_start_view_form', [ # noqa
Button('Cancel', 'end', 'tryton-cancel'),
Button('Generate Report', 'generate', 'tryton-ok', default=True),
]
)
generate = StateAction(
'waiting_customer_shipment_report.report_items_waiting_shipment')
def transition_generate(self):
return 'end'