-
Notifications
You must be signed in to change notification settings - Fork 18
/
manifest.py
137 lines (117 loc) · 3.84 KB
/
manifest.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 -*-
"""
manifest.py
:copyright: (c) 2016 by Fulfil.IO Inc.
:license: see LICENSE for more details.
"""
from datetime import datetime
from trytond.model import fields, ModelView, ModelSQL, Workflow
from trytond.pool import PoolMeta
from trytond.pyson import Eval, Or
__metaclass__ = PoolMeta
__all__ = ["ShippingManifest"]
class ShippingManifest(Workflow, ModelSQL, ModelView):
"Manifest Model for shipping."
__name__ = "shipping.manifest"
carrier = fields.Many2One(
"carrier", "Carrier", required=True, select=True,
states={
"readonly": Eval("state") == "closed",
},
)
carrier_cost_method = fields.Function(
fields.Char("Carrier Cost Method", depends=['carrier']),
getter='get_carrier_cost_method'
)
warehouse = fields.Many2One(
"stock.location", "Warehouse", required=True, select=True,
domain=[("type", "=", "warehouse")],
states={
"readonly": Eval("state") == "closed",
},
)
shipments = fields.One2Many(
'stock.shipment.out', 'shipping_manifest', 'Shipments',
states={
"readonly": Or(
Eval("state") == "closed",
Eval('carrier_cost_method') == 'canada_post',
),
},
domain=[
('carrier', '=', Eval('carrier')),
('state', 'in', ('done', 'packed')),
('tracking_number', '!=', None)
],
add_remove=[
('carrier', '=', Eval('carrier')),
('warehouse', '=', Eval('warehouse')),
('shipping_manifest', '=', None),
('state', 'in', ('done', 'packed')),
('tracking_number', '!=', None)
], depends=['state', 'carrier', 'warehouse', 'carrier_cost_method']
)
state = fields.Selection([
("open", "Open"),
("closed", "Closed")
], "State", required=True, select=True, readonly=True
)
close_date = fields.DateTime("Close Date", readonly=True)
def get_carrier_cost_method(self, name):
return self.carrier and self.carrier.carrier_cost_method
@classmethod
@ModelView.button
@Workflow.transition("closed")
def close(cls, manifests):
for manifest in manifests:
manifest.close_date = datetime.utcnow()
manifest.save()
@staticmethod
def default_state():
return "open"
@classmethod
def __setup__(cls):
super(ShippingManifest, cls).__setup__()
cls._transitions |= set((
('open', 'closed'),
))
cls._buttons.update({
"close": {
"invisible": Eval("state").in_(["closed"])
},
})
def check_single_open_manifest(self):
"""
Check if carrier has not more than 1 manifest
"""
if self.search([
("state", "=", "open"),
("carrier", "=", self.carrier),
("id", "!=", self.id),
("warehouse", "=", self.warehouse)
]):
self.raise_user_error(
"""One carrier cannot have more than 1
open manifest in a warehouse at same time!"""
)
@classmethod
def validate(cls, manifests):
super(ShippingManifest, cls).validate(manifests)
for manifest in manifests:
manifest.check_single_open_manifest()
@classmethod
def get_manifest(cls, carrier, warehouse):
"""
Returns currently opened manifest for carrier account. if not open new.
"""
manifests = cls.search([
('state', '=', 'open'),
('carrier', '=', carrier),
('warehouse', '=', warehouse),
])
if manifests:
return manifests[0]
return cls.create([{
'carrier': carrier,
'warehouse': warehouse
}])[0]