forked from sharoonthomas/trytond-ups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sale.py
429 lines (354 loc) · 13.3 KB
/
sale.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# -*- coding: utf-8 -*-
"""
sale.py
:copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from decimal import Decimal
import math
from lxml.builder import E
from ups.rating_package import RatingService
from ups.base import PyUPSException
from trytond.model import ModelView, fields
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
from trytond.pyson import Eval
__all__ = ['Configuration', 'Sale', 'SaleLine']
__metaclass__ = PoolMeta
UPS_PACKAGE_TYPES = [
('01', 'UPS Letter'),
('02', 'Customer Supplied Package'),
('03', 'Tube'),
('04', 'PAK'),
('21', 'UPS Express Box'),
('24', 'UPS 25KG Box'),
('25', 'UPS 10KG Box'),
('30', 'Pallet'),
('2a', 'Small Express Box'),
('2b', 'Medium Express Box'),
('2c', 'Large Express Box'),
]
class Configuration:
'Sale Configuration'
__name__ = 'sale.configuration'
ups_service_type = fields.Many2One(
'ups.service', 'Default UPS Service Type',
)
ups_package_type = fields.Selection(
UPS_PACKAGE_TYPES, 'Package Content Type'
)
@staticmethod
def default_ups_package_type():
# This is the default value as specified in UPS doc
return '02'
class Sale:
"Sale"
__name__ = 'sale.sale'
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',
)
ups_package_type = fields.Selection(
UPS_PACKAGE_TYPES, 'Package Content Type'
)
ups_saturday_delivery = fields.Boolean("Is Saturday Delivery")
@classmethod
def __setup__(cls):
super(Sale, cls).__setup__()
cls._buttons.update({
'update_ups_shipment_cost': {
'invisible': Eval('state') != 'quotation'
}
})
@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 on_change_lines(self):
"""Pass a flag in context which indicates the get_sale_price method
of ups carrier not to calculate cost on each line change
"""
with Transaction().set_context({'ignore_carrier_computation': True}):
return super(Sale, self).on_change_lines()
def get_is_ups_shipping(self, name):
"""
Check if shipping is from UPS
"""
return self.carrier and self.carrier.carrier_cost_method == 'ups'
def _get_carrier_context(self):
"Pass sale in the context"
context = super(Sale, self)._get_carrier_context()
if not self.carrier.carrier_cost_method == 'ups':
return context
context = context.copy()
context['sale'] = self.id
return context
def apply_ups_shipping(self):
"Add a shipping line to sale for ups"
Sale = Pool().get('sale.sale')
Currency = Pool().get('currency.currency')
if self.is_ups_shipping:
with Transaction().set_context(self._get_carrier_context()):
shipment_cost, currency_id = self.carrier.get_sale_price()
if not shipment_cost:
return
# Convert the shipping cost to sale currency from USD
shipment_cost = Currency.compute(
Currency(currency_id), shipment_cost, self.currency
)
Sale.write([self], {
'lines': [
('create', [{
'type': 'line',
'product': self.carrier.carrier_product.id,
'description': self.ups_service_type.name,
'quantity': 1, # XXX
'unit': self.carrier.carrier_product.sale_uom.id,
'unit_price': shipment_cost,
'shipment_cost': shipment_cost,
'amount': shipment_cost,
'taxes': [],
'sequence': 9999, # XXX
}]),
('delete', [
line for line in self.lines if line.shipment_cost
]),
]
})
@classmethod
def quote(cls, sales):
res = super(Sale, cls).quote(sales)
cls.update_ups_shipment_cost(sales)
return res
@classmethod
@ModelView.button
def update_ups_shipment_cost(cls, sales):
"Updates the shipping line with new value if any"
for sale in sales:
sale.apply_ups_shipping()
def _update_ups_shipments(self):
"""
Update shipments with ups data
"""
Shipment = Pool().get('stock.shipment.out')
assert self.is_ups_shipping
shipments = list(self.shipments)
Shipment.write(shipments, {
'ups_service_type': self.ups_service_type.id,
'ups_package_type': self.ups_package_type,
'ups_saturday_delivery': self.ups_saturday_delivery,
})
def create_shipment(self, shipment_type):
"""
Create shipments for sale
"""
shipments = super(Sale, self).create_shipment(shipment_type)
if shipment_type == 'out' and shipments and self.is_ups_shipping:
self._update_ups_shipments()
return shipments
def _get_ups_packages(self):
"""
Return UPS Packages XML
"""
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
package_type = RatingService.packaging_type(
Code=self.ups_package_type
)
package_weight = RatingService.package_weight_type(
Weight=str(sum(map(
lambda line: line.get_weight_for_ups(), self.lines
))),
Code=ups_config.weight_uom_code,
)
package_service_options = RatingService.package_service_options_type(
RatingService.insured_value_type(MonetaryValue='0')
)
package_container = RatingService.package_type(
package_type,
package_weight,
package_service_options
)
return [package_container]
def _get_ship_from_address(self):
"""
Usually the warehouse from which you ship
"""
return self.warehouse.address
def _get_rate_request_xml(self, mode='rate'):
"""
Return the E builder object with the rate fetching request
:param mode: 'rate' - to fetch rate of current shipment and selected
package type
'shop' - to get a rates list
"""
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
assert mode in ('rate', 'shop'), "Mode should be 'rate' or 'shop'"
if mode == 'rate' and not self.ups_service_type:
self.raise_user_error('ups_service_type_missing')
shipment_args = self._get_ups_packages()
shipment_args.extend([
self.warehouse.address.to_ups_shipper(), # Shipper
self.shipment_address.to_ups_to_address(), # Ship to
self._get_ship_from_address().to_ups_from_address(), # Ship from
])
if ups_config.negotiated_rates:
shipment_args.append(
RatingService.rate_information_type(negotiated=True)
)
if mode == 'rate':
# TODO: handle ups_saturday_delivery
shipment_args.append(
RatingService.service_type(Code=self.ups_service_type.code)
)
request_option = E.RequestOption('Rate')
else:
request_option = E.RequestOption('Shop')
return RatingService.rating_request_type(
E.Shipment(*shipment_args), RequestOption=request_option
)
def _get_rate_from_rated_shipment(cls, rated_shipment):
"""
The rated_shipment is an xml container in the response which has the
standard rates and negotiated rates. This method should extract the
value and return it with the currency
"""
Currency = Pool().get('currency.currency')
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
currency, = Currency.search([
('code', '=', str(rated_shipment.TotalCharges.CurrencyCode))
])
if ups_config.negotiated_rates and \
hasattr(rated_shipment, 'NegotiatedRates'):
# If there are negotiated rates return that instead
charges = rated_shipment.NegotiatedRates.NetSummaryCharges
charges = currency.round(Decimal(
str(charges.GrandTotal.MonetaryValue)
))
else:
charges = currency.round(
Decimal(str(rated_shipment.TotalCharges.MonetaryValue))
)
return charges, currency
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')
ups_config = UPSConfiguration(1)
rate_request = self._get_rate_request_xml()
rate_api = ups_config.api_instance(call="rate")
# Instead of shopping for rates, just get a price for the given
# service and package type to the destination we know.
rate_api.RequestOption = E.RequestOption('Rate')
try:
response = rate_api.request(rate_request)
except PyUPSException, e:
self.raise_user_error(unicode(e[0]))
shipment_cost, currency = self._get_rate_from_rated_shipment(
response.RatedShipment
)
return shipment_cost, currency.id
def _make_rate_line(self, rated_shipment):
"""
Build a rate line from the rated shipment
"""
UPSService = Pool().get('ups.service')
# First identify the service
service = UPSService.search([
('code', '=', str(rated_shipment.Service.Code))
])
if not service:
return
cost, currency = self._get_rate_from_rated_shipment(rated_shipment)
# Extract metadata
metadata = {}
if hasattr(rated_shipment, 'ScheduledDeliveryTime'):
metadata['ScheduledDeliveryTime'] = \
rated_shipment.ScheduledDeliveryTime.pyval
if hasattr(rated_shipment, 'GuaranteedDaysToDelivery'):
metadata['GuaranteedDaysToDelivery'] = \
rated_shipment.GuaranteedDaysToDelivery.pyval
# values that need to be written back to sale order
write_vals = {
'ups_service_type': service[0].id,
}
return (
service[0].name, # Display name
cost,
currency.id,
metadata,
write_vals,
)
def get_ups_shipping_rates(self):
"""
Call the rates service and get possible quotes for shipping the product
"""
UPSConfiguration = Pool().get('ups.configuration')
ups_config = UPSConfiguration(1)
rate_request = self._get_rate_request_xml(mode='shop')
rate_api = ups_config.api_instance(call="rate")
try:
response = rate_api.request(rate_request)
except PyUPSException, e:
self.raise_user_error(unicode(e[0]))
return filter(None, [
self._make_rate_line(rated_shipment)
for rated_shipment in response.iterchildren(tag='RatedShipment')
])
class SaleLine:
'Sale Line'
__name__ = 'sale.line'
@classmethod
def __setup__(cls):
super(SaleLine, cls).__setup__()
cls._error_messages.update({
'weight_required': 'Weight is missing on the product %s',
})
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' or self.quantity <= 0:
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.unit != self.product.default_uom:
quantity = ProductUom.compute_qty(
self.unit,
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)