forked from trytonus/trytond-shipping-endicia
/
stock.py
667 lines (564 loc) · 22.8 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# -*- encoding: utf-8 -*-
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
'''
Inherit stock for endicia API
'''
from decimal import Decimal, ROUND_UP
import base64
import math
import logging
from endicia import ShippingLabelAPI, LabelRequest, RefundRequestAPI, \
BuyingPostageAPI, Element, CalculatingPostageAPI
from endicia.tools import objectify_response, get_images
from endicia.exceptions import RequestError
from trytond.model import Workflow, 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, Bool
from trytond.rpc import RPC
from .sale import ENDICIA_PACKAGE_TYPES, MAILPIECE_SHAPES
__metaclass__ = PoolMeta
__all__ = [
'ShipmentOut', 'ShippingEndicia', 'GenerateShippingLabel',
'EndiciaRefundRequestWizardView', 'EndiciaRefundRequestWizard',
'BuyPostageWizardView', 'BuyPostageWizard',
]
STATES = {
'readonly': Eval('state') == 'done',
}
logger = logging.getLogger(__name__)
quantize_2_decimal = lambda v: Decimal(v).quantize(
Decimal('.01'), rounding=ROUND_UP
)
class ShipmentOut:
"Shipment Out"
__name__ = 'stock.shipment.out'
endicia_mailclass = fields.Many2One(
'endicia.mailclass', 'MailClass', states=STATES, depends=['state']
)
endicia_mailpiece_shape = fields.Selection(
MAILPIECE_SHAPES, 'Endicia MailPiece Shape', states=STATES,
depends=['state']
)
endicia_shipment_bag = fields.Many2One(
'endicia.shipment.bag', 'Endicia Shipment Bag')
endicia_label_subtype = fields.Selection([
('None', 'None'),
('Integrated', 'Integrated')
], 'Label Subtype', states=STATES, depends=['state'])
endicia_integrated_form_type = fields.Selection([
(None, ''),
('Form2976', 'Form2976(Same as CN22)'),
('Form2976A', 'Form2976(Same as CP72)'),
], 'Integrated Form Type', states=STATES, depends=['state'])
endicia_include_postage = fields.Boolean(
'Include Postage ?', states=STATES, depends=['state']
)
endicia_package_type = fields.Selection(
ENDICIA_PACKAGE_TYPES, 'Package Content Type',
states=STATES, depends=['state']
)
is_endicia_shipping = fields.Function(
fields.Boolean('Is Endicia Shipping?', readonly=True),
'get_is_endicia_shipping'
)
endicia_refunded = fields.Boolean('Refunded ?', readonly=True)
@classmethod
def view_attributes(cls):
return super(ShipmentOut, cls).view_attributes() + [
('//page[@id="endicia"]', 'states', {
'invisible': ~Bool(Eval('is_endicia_shipping'))
})]
def _get_weight_uom(self):
"""
Returns uom for endicia
"""
UOM = Pool().get('product.uom')
if self.is_endicia_shipping:
# Endicia by default uses this uom
return UOM.search([('symbol', '=', 'oz')])[0]
return super(ShipmentOut, self)._get_weight_uom()
@staticmethod
def default_endicia_mailclass():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.endicia_mailclass and config.endicia_mailclass.id or None
@staticmethod
def default_endicia_label_subtype():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.endicia_label_subtype
@staticmethod
def default_endicia_integrated_form_type():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.endicia_integrated_form_type
@staticmethod
def default_endicia_include_postage():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.endicia_include_postage
@staticmethod
def default_endicia_package_type():
Config = Pool().get('sale.configuration')
config = Config(1)
return config.endicia_package_type
@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({
'mailclass_missing':
'Select a mailclass to ship using Endicia [USPS].',
'error_label': 'Error in generating label "%s"',
'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',
'wrong_carrier': 'Carrier for selected shipment is not Endicia',
})
cls.__rpc__.update({
'make_endicia_labels': RPC(readonly=False, instantiate=0),
'get_endicia_shipping_cost': RPC(readonly=False, instantiate=0),
})
@fields.depends('is_endicia_shipping', 'carrier')
def on_change_carrier(self):
super(ShipmentOut, self).on_change_carrier()
self.is_endicia_shipping = self.carrier and \
self.carrier.carrier_cost_method == 'endicia' or None
@classmethod
@ModelView.button
@Workflow.transition('done')
def done(cls, shipments):
"""
Add endicia shipments to a open bag
"""
EndiciaShipmentBag = Pool().get('endicia.shipment.bag')
super(ShipmentOut, cls).done(shipments)
endicia_shipments = filter(
lambda s: s.carrier and s.carrier.carrier_cost_method == 'endicia',
shipments
)
if not endicia_shipments:
return
with Transaction().set_user(0):
bag = EndiciaShipmentBag.get_bag()
cls.write(endicia_shipments, {
'endicia_shipment_bag': bag
})
def _get_carrier_context(self):
"Pass shipment in the context"
context = super(ShipmentOut, self)._get_carrier_context()
if not self.carrier.carrier_cost_method == 'endicia':
return context
context = context.copy()
context['shipment'] = self.id
return context
def _update_endicia_item_details(self, request):
'''
Adding customs items/info and form descriptions to the request
:param request: Shipping Label API request instance
'''
User = Pool().get('res.user')
UOM = Pool().get('product.uom')
user = User(Transaction().user)
uom_oz, = UOM.search([('symbol', '=', 'oz')])
customsitems = []
value = 0
for move in self.outgoing_moves:
if move.quantity <= 0:
continue
weight_oz = quantize_2_decimal(move.get_weight(uom_oz))
new_item = [
Element('Description', move.product.name[0:50]),
Element('Quantity', int(math.ceil(move.quantity))),
Element('Weight', weight_oz),
Element('Value', quantize_2_decimal(
move.product.customs_value_used
)),
]
customsitems.append(Element('CustomsItem', new_item))
value += float(move.product.customs_value_used) * move.quantity
description = ','.join([
move.product.name for move in self.outgoing_moves
])
request.add_data({
'customsinfo': [
Element('ContentsExplanation', description[:25]),
Element('CustomsItems', customsitems),
Element('ContentsType', self.endicia_package_type)
]
})
total_value = sum(map(
lambda move: float(move.product.cost_price) * move.quantity,
self.outgoing_moves
))
request.add_data({
'ContentsType': self.endicia_package_type,
'Value': quantize_2_decimal(total_value),
'Description': description[:50],
'CustomsCertify': 'TRUE', # TODO: Should this be part of config ?
'CustomsSigner': user.name,
})
def make_endicia_labels(self):
"""
Make labels for the given shipment
:return: Tracking number as string
"""
Attachment = Pool().get('ir.attachment')
if self.state not in ('packed', 'done'):
self.raise_user_error('invalid_state')
if not (
self.carrier and
self.carrier.carrier_cost_method == 'endicia'
):
self.raise_user_error('wrong_carrier')
if self.tracking_number:
self.raise_user_error('tracking_number_already_present')
if not self.endicia_mailclass:
self.raise_user_error('mailclass_missing')
mailclass = self.endicia_mailclass.value
label_request = LabelRequest(
Test=self.carrier.endicia_is_test and 'YES' or 'NO',
LabelType=(
'International' in mailclass
) and 'International' or 'Default',
# TODO: Probably the following have to be configurable
ImageFormat="PNG",
LabelSize="6x4",
ImageResolution="203",
ImageRotation="Rotate270",
)
# Endicia only support 1 decimal place in weight
weight_oz = "%.1f" % self.weight
shipping_label_request = ShippingLabelAPI(
label_request=label_request,
weight_oz=weight_oz,
partner_customer_id=self.delivery_address.id,
partner_transaction_id=self.id,
mail_class=mailclass,
accountid=self.carrier.endicia_account_id,
requesterid=self.carrier.endicia_requester_id,
passphrase=self.carrier.endicia_passphrase,
test=self.carrier.endicia_is_test,
)
shipping_label_request.mailpieceshape = self.endicia_mailpiece_shape
from_address = self._get_ship_from_address()
shipping_label_request.add_data(
from_address.address_to_endicia_from_address().data
)
shipping_label_request.add_data(
self.delivery_address.address_to_endicia_to_address().data
)
shipping_label_request.add_data({
'LabelSubtype': self.endicia_label_subtype,
'IncludePostage':
self.endicia_include_postage and 'TRUE' or 'FALSE',
})
if self.endicia_label_subtype != 'None':
# Integrated form type needs to be sent for international shipments
shipping_label_request.add_data({
'IntegratedFormType': self.endicia_integrated_form_type,
})
if self.delivery_address.country.code != 'US':
self._update_endicia_item_details(shipping_label_request)
# Logging.
logger.debug(
'Making Shipping Label Request for'
'Shipment ID: {0} and Carrier ID: {1}'
.format(self.id, self.carrier.id)
)
logger.debug('--------SHIPPING LABEL REQUEST--------')
logger.debug(str(shipping_label_request.to_xml()))
logger.debug('--------END REQUEST--------')
try:
response = shipping_label_request.send_request()
except RequestError, error:
self.raise_user_error('error_label', error_args=(error,))
else:
result = objectify_response(response)
# Logging.
logger.debug('--------SHIPPING LABEL RESPONSE--------')
logger.debug(str(response))
logger.debug('--------END RESPONSE--------')
tracking_number = result.TrackingNumber.pyval
self.__class__.write([self], {
'tracking_number': unicode(result.TrackingNumber.pyval),
'cost': Decimal(str(result.FinalPostage.pyval)),
})
# Save images as attachments
images = get_images(result)
for (id, label) in images:
Attachment.create([{
'name': "%s_%s_USPS-Endicia.png" % (tracking_number, id),
'data': buffer(base64.decodestring(label)),
'resource': '%s,%s' % (self.__name__, self.id)
}])
return str(tracking_number)
def get_endicia_shipping_cost(self):
"""Returns the calculated shipping cost as sent by endicia
:returns: The shipping cost in USD
"""
Carrier = Pool().get('carrier')
carrier, = Carrier.search(['carrier_cost_method', '=', 'endicia'])
if not self.endicia_mailclass:
self.raise_user_error('mailclass_missing')
from_address = self._get_ship_from_address()
to_address = self.delivery_address
to_zip = to_address.zip
if to_address.country and to_address.country.code == 'US':
# Domestic
to_zip = to_zip and to_zip[:5]
else:
# International
to_zip = to_zip and to_zip[:15]
# Endicia only support 1 decimal place in weight
weight_oz = "%.1f" % self.weight
calculate_postage_request = CalculatingPostageAPI(
mailclass=self.endicia_mailclass.value,
weightoz=weight_oz,
from_postal_code=from_address.zip and from_address.zip[:5],
to_postal_code=to_zip,
to_country_code=to_address.country and to_address.country.code,
accountid=carrier.endicia_account_id,
requesterid=carrier.endicia_requester_id,
passphrase=carrier.endicia_passphrase,
test=carrier.endicia_is_test,
)
calculate_postage_request.mailpieceshape = self.endicia_mailpiece_shape
# Logging.
logger.debug(
'Making Postage Request for'
'Shipment ID: {0} and Carrier ID: {1}'
.format(self.id, carrier.id)
)
logger.debug('--------POSTAGE REQUEST--------')
logger.debug(str(calculate_postage_request.to_xml()))
logger.debug('--------END REQUEST--------')
try:
response = calculate_postage_request.send_request()
except RequestError, error:
self.raise_user_error('error_label', error_args=(error,))
# Logging.
logger.debug('--------POSTAGE RESPONSE--------')
logger.debug(str(response))
logger.debug('--------END RESPONSE--------')
return Decimal(
objectify_response(response).PostagePrice.get('TotalAmount')
)
def get_is_endicia_shipping(self, name):
"""
Check if shipping is from USPS
"""
return self.carrier and self.carrier.carrier_cost_method == 'endicia'
class EndiciaRefundRequestWizardView(ModelView):
"""Endicia Refund Wizard View
"""
__name__ = 'endicia.refund.wizard.view'
refund_status = fields.Text('Refund Status', readonly=True,)
refund_approved = fields.Boolean('Refund Approved ?', readonly=True,)
class EndiciaRefundRequestWizard(Wizard):
"""A wizard to cancel the current shipment and refund the cost
"""
__name__ = 'endicia.refund.wizard'
start = StateView(
'endicia.refund.wizard.view',
'shipping_endicia.endicia_refund_wizard_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Request Refund', 'request_refund', 'tryton-ok',
default=True),
]
)
request_refund = StateView(
'endicia.refund.wizard.view',
'shipping_endicia.endicia_refund_wizard_view_form', [
Button('OK', 'end', 'tryton-ok', default=True),
]
)
@classmethod
def __setup__(self):
super(EndiciaRefundRequestWizard, self).__setup__()
self._error_messages.update({
'wrong_carrier': 'Carrier for selected shipment is not Endicia'
})
def default_request_refund(self, data):
"""Requests the refund for the current shipment record
and returns the response.
"""
Shipment = Pool().get('stock.shipment.out')
shipments = Shipment.browse(Transaction().context['active_ids'])
# PICNumber is the argument name expected by endicia in API,
# so its better to use the same name here for better understanding
pic_numbers = []
for shipment in shipments:
if not (
shipment.carrier and
shipment.carrier.carrier_cost_method == 'endicia'
):
self.raise_user_error('wrong_carrier')
pic_numbers.append(shipment.tracking_number)
test = shipment.carrier.endicia_is_test and 'Y' or 'N'
refund_request = RefundRequestAPI(
pic_numbers=pic_numbers,
accountid=shipment.carrier.endicia_account_id,
requesterid=shipment.carrier.endicia_requester_id,
passphrase=shipment.carrier.endicia_passphrase,
test=test,
)
try:
response = refund_request.send_request()
except RequestError, error:
self.raise_user_error('error_label', error_args=(error,))
result = objectify_response(response)
if str(result.RefundList.PICNumber.IsApproved) == 'YES':
refund_approved = True
# If refund is approved, then set the state of record
# as cancel/refund
shipment.__class__.write(
[shipment], {'endicia_refunded': True}
)
else:
refund_approved = False
default = {
'refund_status': unicode(result.RefundList.PICNumber.ErrorMsg),
'refund_approved': refund_approved
}
return default
class BuyPostageWizardView(ModelView):
"""Buy Postage Wizard View
"""
__name__ = 'buy.postage.wizard.view'
amount = fields.Numeric('Amount in USD', required=True)
response = fields.Text('Response', readonly=True)
carrier = fields.Many2One(
"carrier", "Carrier", required=True,
domain=[('carrier_cost_method', '=', 'endicia')]
)
class BuyPostageWizard(Wizard):
"""Buy Postage Wizard
"""
__name__ = 'buy.postage.wizard'
start = StateView(
'buy.postage.wizard.view',
'shipping_endicia.endicia_buy_postage_wizard_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Buy Postage', 'buy_postage', 'tryton-ok',
default=True),
]
)
buy_postage = StateView(
'buy.postage.wizard.view',
'shipping_endicia.endicia_buy_postage_wizard_view_form', [
Button('OK', 'end', 'tryton-ok', default=True),
]
)
def default_buy_postage(self, data):
"""
Generate the SCAN Form for the current shipment record
"""
default = {}
buy_postage_api = BuyingPostageAPI(
request_id=Transaction().user,
recredit_amount=self.start.amount,
requesterid=self.start.carrier.endicia_requester_id,
accountid=self.start.carrier.endicia_account_id,
passphrase=self.start.carrier.endicia_passphrase,
test=self.start.carrier.endicia_is_test,
)
try:
response = buy_postage_api.send_request()
except RequestError, error:
self.raise_user_error('error_label', error_args=(error,))
result = objectify_response(response)
default['amount'] = self.start.amount
default['carrier'] = self.start.carrier
default['response'] = str(result.ErrorMessage) \
if hasattr(result, 'ErrorMessage') else 'Success'
return default
class ShippingEndicia(ModelView):
'Endicia Configuration'
__name__ = 'shipping.label.endicia'
endicia_mailclass = fields.Many2One(
'endicia.mailclass', 'MailClass', required=True
)
endicia_mailpiece_shape = fields.Selection(
MAILPIECE_SHAPES, 'Endicia MailPiece Shape'
)
endicia_label_subtype = fields.Selection([
('None', 'None'),
('Integrated', 'Integrated')
], 'Label Subtype')
endicia_integrated_form_type = fields.Selection([
(None, ''),
('Form2976', 'Form2976(Same as CN22)'),
('Form2976A', 'Form2976(Same as CP72)'),
], 'Integrated Form Type')
endicia_include_postage = fields.Boolean('Include Postage ?')
endicia_package_type = fields.Selection(
ENDICIA_PACKAGE_TYPES, 'Package Content Type'
)
endicia_refunded = fields.Boolean('Refunded ?', readonly=True)
class GenerateShippingLabel(Wizard):
'Generate Labels'
__name__ = 'shipping.label'
endicia_config = StateView(
'shipping.label.endicia',
'shipping_endicia.shipping_endicia_configuration_view_form',
[
Button('Back', 'start', 'tryton-go-previous'),
Button('Continue', 'generate', 'tryton-go-next', default=True),
]
)
def default_endicia_config(self, data):
Config = Pool().get('sale.configuration')
config = Config(1)
shipment = self.start.shipment
return {
'endicia_mailclass': (
shipment.endicia_mailclass and shipment.endicia_mailclass.id
) or (
config.endicia_mailclass and config.endicia_mailclass.id
) or None,
'endicia_mailpiece_shape': (
shipment.endicia_mailpiece_shape or
config.endicia_mailpiece_shape
),
'endicia_label_subtype': (
shipment.endicia_label_subtype or config.endicia_label_subtype
),
'endicia_integrated_form_type': (
shipment.endicia_integrated_form_type or
config.endicia_integrated_form_type
),
'endicia_include_postage': (
shipment.endicia_include_postage or
config.endicia_include_postage
),
'endicia_package_type': (
shipment.endicia_package_type or config.endicia_package_type
)
}
def transition_next(self):
state = super(GenerateShippingLabel, self).transition_next()
if self.start.carrier.carrier_cost_method == 'endicia':
return 'endicia_config'
return state
def update_shipment(self):
shipment = self.start.shipment
if self.start.carrier.carrier_cost_method == 'endicia':
shipment.endicia_mailclass = self.endicia_config.endicia_mailclass
shipment.endicia_mailpiece_shape = \
self.endicia_config.endicia_mailpiece_shape
shipment.endicia_label_subtype = \
self.endicia_config.endicia_label_subtype
shipment.endicia_integrated_form_type = \
self.endicia_config.endicia_integrated_form_type
shipment.endicia_package_type = \
self.endicia_config.endicia_package_type
shipment.endicia_include_postage = \
self.endicia_config.endicia_include_postage
return super(GenerateShippingLabel, self).update_shipment()