This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
account.py
596 lines (511 loc) · 20.1 KB
/
account.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
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import ROUND_DOWN, ROUND_HALF_EVEN, Decimal
from itertools import groupby
from operator import itemgetter
from sql.functions import CharLength
from trytond import backend
from trytond.i18n import gettext
from trytond.model import (
Index, MatchMixin, ModelSQL, ModelView, Workflow, fields)
from trytond.modules.company.model import CompanyValueMixin
from trytond.modules.product import price_digits, round_price
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, Id
from trytond.tools.multivalue import migrate_property
from trytond.transaction import Transaction
from trytond.wizard import Button, StateTransition, StateView, Wizard
from .exceptions import FilterUnusedWarning, NoMoveWarning
def _parents(records):
for record in records:
while record:
yield record
record = record.parent
class Configuration(metaclass=PoolMeta):
__name__ = 'account.configuration'
landed_cost_sequence = fields.MultiValue(fields.Many2One(
'ir.sequence', "Landed Cost Sequence", required=True,
domain=[
('company', 'in',
[Eval('context', {}).get('company', -1), None]),
('sequence_type', '=',
Id('account_stock_landed_cost',
'sequence_type_landed_cost')),
]))
@classmethod
def default_landed_cost_sequence(cls, **pattern):
return cls.multivalue_model(
'landed_cost_sequence').default_landed_cost_sequence()
class ConfigurationLandedCostSequence(ModelSQL, CompanyValueMixin):
"Account Configuration Landed Cost Sequence"
__name__ = 'account.configuration.landed_cost_sequence'
landed_cost_sequence = fields.Many2One(
'ir.sequence', "Landed Cost Sequence", required=True,
domain=[
('company', 'in', [Eval('company', -1), None]),
('sequence_type', '=',
Id('account_stock_landed_cost', 'sequence_type_landed_cost')),
])
@classmethod
def __register__(cls, module_name):
exist = backend.TableHandler.table_exist(cls._table)
super(ConfigurationLandedCostSequence, cls).__register__(module_name)
if not exist:
cls._migrate_property([], [], [])
@classmethod
def _migrate_property(cls, field_names, value_names, fields):
field_names.append('landed_cost_sequence')
value_names.append('landed_cost_sequence')
fields.append('company')
migrate_property(
'account.configuration', field_names, cls, value_names,
fields=fields)
@classmethod
def default_landed_cost_sequence(cls, **pattern):
pool = Pool()
ModelData = pool.get('ir.model.data')
try:
return ModelData.get_id(
'account_stock_landed_cost', 'sequence_landed_cost')
except KeyError:
return None
class LandedCost(Workflow, ModelSQL, ModelView, MatchMixin):
'Landed Cost'
__name__ = 'account.landed_cost'
_rec_name = 'number'
number = fields.Char("Number", readonly=True)
company = fields.Many2One('company.company', 'Company', required=True,
states={
'readonly': Eval('state') != 'draft',
})
shipments = fields.Many2Many('account.landed_cost-stock.shipment.in',
'landed_cost', 'shipment', 'Shipments',
states={
'readonly': Eval('state') != 'draft',
},
domain=[
('company', '=', Eval('company')),
('state', 'in', ['received', 'done']),
])
invoice_lines = fields.One2Many('account.invoice.line', 'landed_cost',
'Invoice Lines',
states={
'readonly': Eval('state') != 'draft',
},
add_remove=[
('landed_cost', '=', None),
],
domain=[
('company', '=', Eval('company', -1)),
('invoice.state', 'in', ['posted', 'paid']),
('invoice.type', '=', 'in'),
('product.landed_cost', '=', True),
('type', '=', 'line'),
])
allocation_method = fields.Selection([
('value', 'By Value'),
], 'Allocation Method', required=True,
states={
'readonly': Eval('state') != 'draft',
})
categories = fields.Many2Many(
'account.landed_cost-product.category', 'landed_cost', 'category',
"Categories",
states={
'readonly': Eval('state') != 'draft',
},
help="Apply only to products of these categories.")
products = fields.Many2Many(
'account.landed_cost-product.product', 'landed_cost', 'product',
"Products",
states={
'readonly': Eval('state') != 'draft',
},
help="Apply only to these products.")
posted_date = fields.Date('Posted Date', readonly=True)
state = fields.Selection([
('draft', 'Draft'),
('posted', 'Posted'),
('cancelled', 'Cancelled'),
], "State", readonly=True, sort=False)
factors = fields.Dict(None, "Factors", readonly=True)
@classmethod
def __setup__(cls):
cls.number.search_unaccented = False
super(LandedCost, cls).__setup__()
t = cls.__table__()
cls._sql_indexes.add(
Index(t, (t.state, Index.Equality()), where=t.state == 'draft'))
cls._order = [
('number', 'DESC'),
('id', 'DESC'),
]
cls._transitions |= set((
('draft', 'posted'),
('draft', 'cancelled'),
('posted', 'cancelled'),
('cancelled', 'draft'),
))
cls._buttons.update({
'cancel': {
'invisible': Eval('state') == 'cancelled',
'depends': ['state'],
},
'draft': {
'invisible': Eval('state') != 'cancelled',
'depends': ['state'],
},
'post_wizard': {
'invisible': Eval('state') != 'draft',
'depends': ['state'],
},
'show': {
'invisible': Eval('state').in_(['draft', 'cancelled']),
'depends': ['state']
},
})
@classmethod
def __register__(cls, module_name):
cursor = Transaction().connection.cursor()
table_h = cls.__table_handler__(module_name)
sql_table = cls.__table__()
# Migration from 3.8: rename code into number
if table_h.column_exist('code'):
table_h.column_rename('code', 'number')
super(LandedCost, cls).__register__(module_name)
# Migration from 5.6: rename state cancel to cancelled
cursor.execute(*sql_table.update(
[sql_table.state], ['cancelled'],
where=sql_table.state == 'cancel'))
@classmethod
def order_number(cls, tables):
table, _ = tables[None]
return [CharLength(table.number), table.number]
@staticmethod
def default_company():
return Transaction().context.get('company')
@staticmethod
def default_allocation_method():
return 'value'
@staticmethod
def default_state():
return 'draft'
@classmethod
@ModelView.button
@Workflow.transition('cancelled')
def cancel(cls, landed_costs):
for landed_cost in landed_costs:
if landed_cost.state == 'posted':
getattr(landed_cost, 'unallocate_cost_by_%s' %
landed_cost.allocation_method)()
cls.write(landed_costs, {
'posted_date': None,
'factors': None,
'state': 'cancelled',
})
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, landed_costs):
pass
@property
def cost(self):
pool = Pool()
Currency = pool.get('currency.currency')
currency = self.company.currency
cost = Decimal(0)
for line in self.invoice_lines:
with Transaction().set_context(date=line.invoice.currency_date):
cost += Currency.compute(
line.invoice.currency, line.amount, currency, round=False)
return cost
def stock_moves(self):
moves = []
for shipment in self.shipments:
for move in shipment.incoming_moves:
if move.state == 'cancelled':
continue
if self._stock_move_filter(move):
moves.append(move)
return moves
def _stock_move_filter(self, move):
if not self.categories and not self.products:
return True
result = False
if self.categories:
result |= bool(
set(self.categories)
& set(_parents(move.product.categories_all)))
if self.products:
result |= bool(move.product in self.products)
return result
def _stock_move_filter_unused(self, moves):
pool = Pool()
Warning = pool.get('res.user.warning')
categories = {
c for m in moves for c in _parents(m.product.categories_all)}
for category in self.categories:
if category not in categories:
key = '%s - %s' % (self, category)
if Warning.check(key):
raise FilterUnusedWarning(
key,
gettext('account_stock_landed_cost'
'.msg_landed_cost_unused_category',
landed_cost=self.rec_name,
category=category.rec_name))
products = {m.product for m in moves}
for product in self.products:
if product not in products:
key = '%s - %s' % (self, product)
if Warning.check(key):
raise FilterUnusedWarning(
key,
gettext('account_stock_landed_cost'
'.msg_landed_cost_unused_product',
landed_cost=self.rec_name,
product=product.rec_name))
def allocate_cost_by_value(self):
self.factors = self._get_factors('value')
self._allocate_cost(self.factors)
def unallocate_cost_by_value(self):
factors = self.factors or self._get_factors('value')
self._allocate_cost(factors, sign=-1)
def _get_factors(self, method=None):
if method is None:
method = self.allocation_method
return getattr(self, '_get_%s_factors' % method)()
def _get_value_factors(self):
"Return the factor for each move based on value"
pool = Pool()
Currency = pool.get('currency.currency')
currency = self.company.currency
moves = self.stock_moves()
sum_value = 0
unit_prices = {}
for move in moves:
with Transaction().set_context(date=move.effective_date):
unit_price = Currency.compute(
move.currency, move.unit_price, currency, round=False)
unit_prices[move.id] = unit_price
sum_value += unit_price * Decimal(str(move.quantity))
factors = {}
length = Decimal(len(moves))
for move in moves:
quantity = Decimal(str(move.quantity))
if not sum_value:
factors[str(move.id)] = 1 / length
else:
factors[str(move.id)] = (
quantity * unit_prices[move.id] / sum_value)
return factors
def _costs_to_allocate(self, moves, factors):
pool = Pool()
Move = pool.get('stock.move')
cost = self.cost
costs = []
digit = Move.unit_price.digits[1]
exp = Decimal(str(10.0 ** -digit))
difference = cost
for move in moves:
quantity = Decimal(str(move.quantity))
move_cost = cost * factors[str(move.id)]
unit_landed_cost = round_price(
move_cost / quantity, rounding=ROUND_DOWN)
costs.append({
'unit_landed_cost': unit_landed_cost,
'difference': move_cost - (unit_landed_cost * quantity),
'move': move,
})
difference -= unit_landed_cost * quantity
costs.sort(key=itemgetter('difference'), reverse=True)
for cost in costs:
move = cost['move']
quantity = Decimal(str(move.quantity))
if exp * quantity <= difference:
cost['unit_landed_cost'] += exp
difference -= exp * quantity
if difference < exp:
break
return costs
def _allocate_cost(self, factors, sign=1):
"Allocate cost on moves using factors"
pool = Pool()
Move = pool.get('stock.move')
Currency = pool.get('currency.currency')
assert sign in {1, -1}
currency = self.company.currency
moves = [m for m in self.stock_moves() if m.quantity]
costs = self._costs_to_allocate(moves, factors)
for cost in costs:
move = cost['move']
with Transaction().set_context(date=move.effective_date):
unit_landed_cost = Currency.compute(
currency, cost['unit_landed_cost'],
move.currency, round=False)
unit_landed_cost = round_price(
unit_landed_cost, rounding=ROUND_HALF_EVEN)
if move.unit_landed_cost is None:
move.unit_landed_cost = 0
move.unit_price += unit_landed_cost * sign
move.unit_landed_cost += unit_landed_cost * sign
Move.save(moves)
@classmethod
@ModelView.button_action(
'account_stock_landed_cost.wizard_landed_cost_post')
def post_wizard(cls, landed_costs):
pass
@classmethod
@ModelView.button_action(
'account_stock_landed_cost.wizard_landed_cost_show')
def show(cls, landed_costs):
pass
@classmethod
@Workflow.transition('posted')
def post(cls, landed_costs):
pool = Pool()
Date = pool.get('ir.date')
Warning = pool.get('res.user.warning')
today = Date.today()
for landed_cost in landed_costs:
stock_moves = landed_cost.stock_moves()
if not stock_moves:
key = '%s post no move' % landed_cost
if Warning.check(key):
raise NoMoveWarning(
key,
gettext('account_stock_landed_cost'
'.msg_landed_cost_post_no_stock_move',
landed_cost=landed_cost.rec_name))
landed_cost._stock_move_filter_unused(stock_moves)
getattr(landed_cost, 'allocate_cost_by_%s' %
landed_cost.allocation_method)()
for company, c_landed_costs in groupby(
landed_costs, key=lambda l: l.company):
with Transaction().set_context(company=company.id):
today = Date.today()
for landed_cost in c_landed_costs:
landed_cost.posted_date = today
landed_cost.posted_date = today
# Use save as allocate methods may modify the records
cls.save(landed_costs)
@classmethod
def create(cls, vlist):
pool = Pool()
Config = pool.get('account.configuration')
vlist = [v.copy() for v in vlist]
config = Config(1)
default_company = cls.default_company()
for values in vlist:
if values.get('number') is None:
values['number'] = config.get_multivalue(
'landed_cost_sequence',
company=values.get('company', default_company)).get()
return super(LandedCost, cls).create(vlist)
@classmethod
def copy(cls, landed_costs, default=None):
default = default.copy() if default is not None else {}
default.setdefault('invoice_lines', None)
return super().copy(landed_costs, default=default)
class LandedCost_Shipment(ModelSQL):
'Landed Cost - Shipment'
__name__ = 'account.landed_cost-stock.shipment.in'
landed_cost = fields.Many2One(
'account.landed_cost', 'Landed Cost',
required=True, ondelete='CASCADE')
shipment = fields.Many2One(
'stock.shipment.in', 'Shipment', required=True, ondelete='CASCADE')
class LandedCost_ProductCategory(ModelSQL):
"Landed Cost - Product Category"
__name__ = 'account.landed_cost-product.category'
landed_cost = fields.Many2One(
'account.landed_cost', 'Landed Cost',
required=True, ondelete='CASCADE')
category = fields.Many2One(
'product.category', "Category", required=True, ondelete='CASCADE')
class LandedCost_Product(ModelSQL):
"Landed Cost - Product"
__name__ = 'account.landed_cost-product.product'
landed_cost = fields.Many2One(
'account.landed_cost', "Landed Cost",
required=True, ondelete='CASCADE')
product = fields.Many2One(
'product.product', "Product", required=True, ondelete='CASCADE')
class ShowLandedCostMixin(Wizard):
start_state = 'show'
show = StateView('account.landed_cost.show',
'account_stock_landed_cost.landed_cost_show_view_form', [])
@property
def factors(self):
return self.record._get_factors()
def default_show(self, fields):
moves = []
default = {
'cost': round_price(self.record.cost),
'moves': moves,
}
stock_moves = [m for m in self.record.stock_moves() if m.quantity]
costs = self.record._costs_to_allocate(stock_moves, self.factors)
for cost in costs:
moves.append({
'move': cost['move'].id,
'cost': round_price(
cost['unit_landed_cost'], rounding=ROUND_HALF_EVEN),
})
return default
class PostLandedCost(ShowLandedCostMixin):
"Post Landed Cost"
__name__ = 'account.landed_cost.post'
post = StateTransition()
@classmethod
def __setup__(cls):
super().__setup__()
cls.show.buttons.extend([
Button("Cancel", 'end', 'tryton-cancel'),
Button("Post", 'post', 'tryton-ok', default=True),
])
def transition_post(self):
self.model.post([self.record])
return 'end'
class ShowLandedCost(ShowLandedCostMixin):
"Show Landed Cost"
__name__ = 'account.landed_cost.show'
@classmethod
def __setup__(cls):
super().__setup__()
cls.show.buttons.extend([
Button("Close", 'end', 'tryton-close', default=True),
])
@property
def factors(self):
return self.record.factors or super().factors
class LandedCostShow(ModelView):
"Landed Cost Show"
__name__ = 'account.landed_cost.show'
cost = fields.Numeric("Cost", digits=price_digits, readonly=True)
moves = fields.One2Many(
'account.landed_cost.show.move', None, "Moves", readonly=True)
class LandedCostShowMove(ModelView):
"Landed Cost Show"
__name__ = 'account.landed_cost.show.move'
move = fields.Many2One('stock.move', "Move", readonly=True)
cost = fields.Numeric("Cost", digits=price_digits, readonly=True)
class InvoiceLine(metaclass=PoolMeta):
__name__ = 'account.invoice.line'
landed_cost = fields.Many2One(
'account.landed_cost', "Landed Cost", readonly=True,
states={
'invisible': ~Eval('landed_cost'),
})
@classmethod
def __setup__(cls):
super(InvoiceLine, cls).__setup__()
cls._check_modify_exclude.add('landed_cost')
@classmethod
def copy(cls, lines, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('landed_cost', None)
return super(InvoiceLine, cls).copy(lines, default=default)