forked from sharoonthomas/trytond-ups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock.py
executable file
·353 lines (288 loc) · 11.1 KB
/
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
# -*- coding: utf-8 -*-
"""
stock.py
:copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from decimal import Decimal
import base64
import math
from ups.shipping_package import ShipmentConfirm, ShipmentAccept
from ups.base import PyUPSException
from trytond.model import ModelView, fields
from trytond.wizard import Wizard, StateView, Button
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.rpc import RPC
from .sale import UPS_PACKAGE_TYPES
__metaclass__ = PoolMeta
__all__ = [
'ShipmentOut', 'StockMove', 'GenerateUPSLabelMessage', 'GenerateUPSLabel'
]
STATES = {
'readonly': Eval('state') == 'done',
}
class ShipmentOut:
"Shipment Out"
__name__ = 'stock.shipment.out'
is_ups_shipping = fields.Function(
fields.Boolean('Is Shipping', readonly=True),
'get_is_ups_shipping'
)
ups_service_type = fields.Many2One(
'ups.service', 'UPS Service Type', states=STATES, depends=['state']
)
ups_package_type = fields.Selection(
UPS_PACKAGE_TYPES, 'Package Content Type', states=STATES,
depends=['state']
)
ups_saturday_delivery = fields.Boolean(
"Is Saturday Delivery", states=STATES, depends=['state']
)
tracking_number = fields.Char('Tracking Number', states=STATES)
@staticmethod
def default_ups_package_type():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.ups_package_type
@staticmethod
def default_ups_service_type():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.ups_service_type and config.ups_service_type.id or None
@staticmethod
def default_ups_saturday_delivery():
return False
def get_is_ups_shipping(self, name):
"""
Check if shipping is from UPS
"""
return self.carrier and self.carrier.carrier_cost_method == 'ups'
@classmethod
def __setup__(cls):
super(ShipmentOut, cls).__setup__()
# There can be cases when people might want to use a different
# shipment carrier at any state except `done`.
cls.carrier.states = STATES
cls._error_messages.update({
'ups_wrong_carrier':
'Carrier for selected shipment is not UPS',
'ups_service_type_missing':
'UPS service type missing.',
'tracking_number_already_present':
'Tracking Number is already present for this shipment.',
'invalid_state': 'Labels can only be generated when the '
'shipment is in Packed or Done states only',
})
cls.__rpc__.update({
'make_ups_labels': RPC(readonly=False, instantiate=0),
'get_ups_shipping_cost': RPC(readonly=False, instantiate=0),
})
def _get_ups_packages(self):
"""
Return UPS Packages XML
"""
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
package_type = ShipmentConfirm.packaging_type(
Code=self.ups_package_type
) # FIXME: Support multiple packaging type
package_weight = ShipmentConfirm.package_weight_type(
Weight=str(sum(map(
lambda move: move.get_weight_for_ups(), self.outgoing_moves
))),
Code=ups_config.weight_uom_code,
)
package_service_options = ShipmentConfirm.package_service_options_type(
ShipmentConfirm.insured_value_type(MonetaryValue='0')
)
package_container = ShipmentConfirm.package_type(
package_type,
package_weight,
package_service_options
)
return [package_container]
def _get_carrier_context(self):
"Pass shipment in the context"
context = super(ShipmentOut, self)._get_carrier_context()
if not self.carrier.carrier_cost_method == 'ups':
return context
context = context.copy()
context['shipment'] = self.id
return context
def _get_shipment_confirm_xml(self):
"""
Return XML of shipment for shipment_confirm
"""
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
if not self.ups_service_type:
self.raise_user_error('ups_service_type_missing')
payment_info_prepaid = \
ShipmentConfirm.payment_information_prepaid_type(
AccountNumber=ups_config.shipper_no
)
payment_info = ShipmentConfirm.payment_information_type(
payment_info_prepaid)
packages = self._get_ups_packages()
shipment_service = ShipmentConfirm.shipment_service_option_type(
SaturdayDelivery='1' if self.ups_saturday_delivery
else 'None'
)
shipment_confirm = \
ShipmentConfirm.shipment_confirm_request_type(
self.warehouse.address.to_ups_shipper(),
self.delivery_address.to_ups_to_address(),
self.warehouse.address.to_ups_from_address(),
ShipmentConfirm.service_type(Code=self.ups_service_type.code),
payment_info, shipment_service,
*packages
)
return shipment_confirm
def get_ups_shipping_cost(self):
"""Returns the calculated shipping cost as sent by ups
:returns: The shipping cost with currency
"""
UPSConfiguration = Pool().get('ups.configuration')
Currency = Pool().get('currency.currency')
ups_config = UPSConfiguration(1)
shipment_confirm = self._get_shipment_confirm_xml()
shipment_confirm_instance = ups_config.api_instance(call="confirm")
try:
response = shipment_confirm_instance.request(shipment_confirm)
except PyUPSException, e:
self.raise_user_error(unicode(e[0]))
currency, = Currency.search([
('code', '=', str(
response.ShipmentCharges.TotalCharges.CurrencyCode
))
])
shipping_cost = currency.round(Decimal(
str(response.ShipmentCharges.TotalCharges.MonetaryValue)
))
return shipping_cost, currency.id
def make_ups_labels(self):
"""
Make labels for the given shipment
:return: Tracking number as string
"""
Attachment = Pool().get('ir.attachment')
UPSConfiguration = Pool().get('ups.configuration')
Currency = Pool().get('currency.currency')
ups_config = UPSConfiguration(1)
if self.state not in ('packed', 'done'):
self.raise_user_error('invalid_state')
if not self.is_ups_shipping:
self.raise_user_error('ups_wrong_carrier')
if self.tracking_number:
self.raise_user_error('tracking_number_already_present')
shipment_confirm = self._get_shipment_confirm_xml()
shipment_confirm_instance = ups_config.api_instance(call="confirm")
try:
response = shipment_confirm_instance.request(shipment_confirm)
except PyUPSException, e:
self.raise_user_error(unicode(e[0]))
digest = ShipmentConfirm.extract_digest(response)
shipment_accept = ShipmentAccept.shipment_accept_request_type(digest)
shipment_accept_instance = ups_config.api_instance(call="accept")
try:
response = shipment_accept_instance.request(shipment_accept)
except PyUPSException, e:
self.raise_user_error(unicode(e[0]))
if len(response.ShipmentResults.PackageResults) > 1:
self.raise_user_error('ups_multiple_packages_not_supported')
shipment_res = response.ShipmentResults
package, = shipment_res.PackageResults
tracking_number = package.TrackingNumber.pyval
currency, = Currency.search([
('code', '=', str(
shipment_res.ShipmentCharges.TotalCharges.CurrencyCode
))
])
shipping_cost = currency.round(Decimal(
str(shipment_res.ShipmentCharges.TotalCharges.MonetaryValue)
))
self.__class__.write([self], {
'tracking_number': unicode(tracking_number),
'cost': shipping_cost,
'cost_currency': currency,
})
Attachment.create([{
'name': "%s_%s_.png" % (
tracking_number,
shipment_res.ShipmentIdentificationNumber.pyval
),
'data': buffer(base64.decodestring(
package.LabelImage.GraphicImage.pyval
)),
'resource': '%s,%s' % (self.__name__, self.id)
}])
return tracking_number
class GenerateUPSLabelMessage(ModelView):
'Generate UPS Labels Message'
__name__ = 'generate.ups.label.message'
tracking_number = fields.Char("Tracking number", readonly=True)
class GenerateUPSLabel(Wizard):
'Generate UPS Labels'
__name__ = 'generate.ups.label'
start = StateView(
'generate.ups.label.message',
'ups.generate_ups_label_message_view_form',
[
Button('Ok', 'end', 'tryton-ok'),
]
)
def default_start(self, data):
Shipment = Pool().get('stock.shipment.out')
try:
shipment, = Shipment.browse(Transaction().context['active_ids'])
except ValueError:
self.raise_user_error(
'This wizard can be called for only one shipment at a time'
)
tracking_number = shipment.make_ups_labels()
return {'tracking_number': str(tracking_number)}
class StockMove:
"Stock move"
__name__ = "stock.move"
@classmethod
def __setup__(cls):
super(StockMove, cls).__setup__()
cls._error_messages.update({
'weight_required':
'Weight for product %s in stock move is missing',
})
def get_weight_for_ups(self):
"""
Returns weight as required for ups
"""
ProductUom = Pool().get('product.uom')
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
if self.product.type == 'service':
return 0
if not self.product.weight:
self.raise_user_error(
'weight_required',
error_args=(self.product.name,)
)
# Find the quantity in the default uom of the product as the weight
# is for per unit in that uom
if self.uom != self.product.default_uom:
quantity = ProductUom.compute_qty(
self.uom,
self.quantity,
self.product.default_uom
)
else:
quantity = self.quantity
weight = float(self.product.weight) * quantity
# Convert weights according to UPS
if self.product.weight_uom != ups_config.weight_uom:
weight = ProductUom.compute_qty(
self.product.weight_uom,
weight,
ups_config.weight_uom
)
return math.ceil(weight)