forked from NaN-tic/trytond-stock_valued
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshipment.py
246 lines (207 loc) · 8.64 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
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
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from decimal import Decimal
from trytond import backend
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.modules.account.tax import TaxableMixin
from trytond.modules.currency.fields import Monetary
__all__ = ['ShipmentIn', 'ShipmentOut', 'ShipmentOutReturn']
MOVES = {
'stock.shipment.in': 'incoming_moves',
'stock.shipment.in.return': 'moves',
'stock.shipment.out': 'outgoing_moves',
'stock.shipment.out.return': 'incoming_moves',
}
TAX_TYPE = {
'stock.shipment.in': 'invoice',
'stock.shipment.in.return': 'credit_note',
'stock.shipment.out': 'invoice',
'stock.shipment.out.return': 'credit_note',
}
_ZERO = Decimal('0.0')
class ShipmentValuedMixin(TaxableMixin):
currency = fields.Function(fields.Many2One('currency.currency',
'Currency'), 'on_change_with_currency')
untaxed_amount_cache = Monetary('Untaxed Cache',
digits='currency', currency='currency', readonly=True)
tax_amount_cache = Monetary('Tax Cache',
digits='currency', currency='currency', readonly=True)
total_amount_cache = Monetary('Total Cache',
digits='currency', currency='currency', readonly=True)
untaxed_amount = fields.Function(Monetary('Untaxed',
digits='currency', currency='currency'), 'get_amounts')
tax_amount = fields.Function(Monetary('Tax',
digits='currency', currency='currency'), 'get_amounts')
total_amount = fields.Function(Monetary('Total',
digits='currency', currency='currency'), 'get_amounts')
@fields.depends('company')
def on_change_with_currency(self, name=None):
currency_id = None
if self.valued_moves:
for move in self.valued_moves:
if move.currency:
currency_id = move.currency.id
break
if currency_id is None and self.company:
currency_id = self.company.currency.id
return currency_id
@property
def valued_moves(self):
Move = Pool().get('stock.move')
origins = Move._get_origin()
keep_origin = True if 'stock.move' in origins else False
move_field = MOVES.get(self.__name__)
if (keep_origin and self.__name__ == 'stock.shipment.out'):
moves = getattr(self, 'inventory_moves', [])
if moves:
return moves
return getattr(self, move_field, [])
@property
def tax_type(self):
return TAX_TYPE.get(self.__name__)
@property
def taxable_lines(self):
pool = Pool()
Config = pool.get('stock.configuration')
Move = pool.get('stock.move')
config = Config(1)
valued_origin = config.valued_origin
taxable_lines = []
for move in self.valued_moves:
if move.state == 'cancelled':
continue
origin = move.origin
if isinstance(origin, Move):
origin = origin.origin
if valued_origin and hasattr(origin, 'unit_price'):
if origin.unit_price is not None:
unit_price = origin.unit_price
elif move.unit_price is not None:
unit_price = move.unit_price
else:
unit_price = origin.product.list_price or _ZERO
else:
unit_price = move.unit_price or move.unit_price or _ZERO
taxable_lines.append((
getattr(move, 'taxes', None) or [],
unit_price,
getattr(move, 'quantity', None) or 0,
None,
))
return taxable_lines
def calc_amounts(self):
untaxed_amount = sum((m.amount for m in self.valued_moves if m.amount),
Decimal(0))
taxes = self._get_taxes()
untaxed_amount = self.company.currency.round(untaxed_amount)
tax_amount = sum((self.company.currency.round(tax['amount'])
for tax in taxes.values()), Decimal(0))
return {
'untaxed_amount': untaxed_amount,
'tax_amount': tax_amount if untaxed_amount else Decimal(0),
'total_amount': (untaxed_amount + tax_amount
if untaxed_amount else Decimal(0)),
}
@classmethod
def get_amounts(cls, shipments, names):
untaxed_amount = dict((i.id, Decimal(0)) for i in shipments)
tax_amount = dict((i.id, Decimal(0)) for i in shipments)
total_amount = dict((i.id, Decimal(0)) for i in shipments)
for shipment in shipments:
if (shipment.state in cls._states_valued_cached
and shipment.untaxed_amount_cache is not None
and shipment.tax_amount_cache is not None
and shipment.total_amount_cache is not None):
untaxed_amount[shipment.id] = shipment.untaxed_amount_cache
tax_amount[shipment.id] = shipment.tax_amount_cache
total_amount[shipment.id] = shipment.total_amount_cache
else:
res = shipment.calc_amounts()
untaxed_amount[shipment.id] = res['untaxed_amount']
tax_amount[shipment.id] = res['tax_amount']
total_amount[shipment.id] = res['total_amount']
result = {
'untaxed_amount': untaxed_amount,
'tax_amount': tax_amount,
'total_amount': total_amount,
}
for key in list(result.keys()):
if key not in names:
del result[key]
return result
@classmethod
def store_cache(cls, shipments):
for shipment in shipments:
shipment.untaxed_amount_cache = shipment.untaxed_amount
shipment.tax_amount_cache = shipment.tax_amount
shipment.total_amount_cache = shipment.total_amount
cls.save(shipments)
@classmethod
def reset_cache(cls, shipments):
for shipment in shipments:
shipment.untaxed_amount_cache = None
shipment.tax_amount_cache = None
shipment.total_amount_cache = None
cls.save(shipments)
class ShipmentIn(ShipmentValuedMixin, metaclass=PoolMeta):
__name__ = 'stock.shipment.in'
@classmethod
def __setup__(cls):
super(ShipmentIn, cls).__setup__()
# The states where amounts are cached
cls._states_valued_cached = ['done', 'cancelled']
@classmethod
def __register__(cls, module_name):
table = backend.TableHandler(cls, module_name)
if table.column_exist('untaxed_amount'):
table.column_rename('untaxed_amount', 'untaxed_amount_cache')
table.column_rename('tax_amount', 'tax_amount_cache')
table.column_rename('total_amount', 'total_amount_cache')
super(ShipmentIn, cls).__register__(module_name)
@classmethod
def cancel(cls, shipments):
super(ShipmentIn, cls).cancel(shipments)
cls.store_cache(shipments)
@classmethod
def done(cls, shipments):
super(ShipmentIn, cls).done(shipments)
cls.store_cache(shipments)
class ShipmentOut(ShipmentValuedMixin, metaclass=PoolMeta):
__name__ = 'stock.shipment.out'
@classmethod
def __setup__(cls):
super(ShipmentOut, cls).__setup__()
# The states where amounts are cached
cls._states_valued_cached = ['done', 'cancelled']
@classmethod
def __register__(cls, module_name):
table = backend.TableHandler(cls, module_name)
if table.column_exist('untaxed_amount'):
table.column_rename('untaxed_amount', 'untaxed_amount_cache')
table.column_rename('tax_amount', 'tax_amount_cache')
table.column_rename('total_amount', 'total_amount_cache')
super(ShipmentOut, cls).__register__(module_name)
@classmethod
def cancel(cls, shipments):
super(ShipmentOut, cls).cancel(shipments)
cls.store_cache(shipments)
@classmethod
def done(cls, shipments):
super(ShipmentOut, cls).done(shipments)
cls.store_cache(shipments)
class ShipmentOutReturn(ShipmentValuedMixin, metaclass=PoolMeta):
__name__ = 'stock.shipment.out.return'
@classmethod
def __setup__(cls):
super(ShipmentOutReturn, cls).__setup__()
# The states where amounts are cached
cls._states_valued_cached = ['done', 'cancelled']
@classmethod
def cancel(cls, shipments):
super(ShipmentOutReturn, cls).cancel(shipments)
cls.store_cache(shipments)
@classmethod
def done(cls, shipments):
super(ShipmentOutReturn, cls).done(shipments)
cls.store_cache(shipments)