forked from NaN-tic/trytond-stock_valued
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.py
163 lines (143 loc) · 6.23 KB
/
move.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
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from decimal import Decimal
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Equal, Eval, Not
from trytond.modules.product import price_digits
try:
from trytond.modules.account_invoice_discount import discount_digits
except ImportError:
discount_digits = price_digits
from trytond.modules.currency.fields import Monetary
__all__ = ['Move']
_ZERO = Decimal('0.0')
STATES = {
'invisible': Not(Equal(Eval('state', ''), 'done')),
}
DEPENDS = ['state']
PARTIES = {
'stock.shipment.in': 'supplier',
'stock.shipment.in.return': 'supplier',
'stock.shipment.out': 'customer',
'stock.shipment.out.return': 'customer',
'stock.shipment.internal': 'company',
}
class Move(metaclass=PoolMeta):
__name__ = 'stock.move'
gross_unit_price = fields.Function(Monetary('Gross Price',
digits=price_digits, currency='currency', states=STATES, depends=DEPENDS),
'get_origin_fields')
amount = fields.Function(Monetary('Amount',
digits='currency', currency='currency'), 'get_origin_fields')
taxes = fields.Function(fields.Many2Many('account.tax', None, None,
'Taxes'), 'get_origin_fields')
unit_price_w_tax = fields.Function(Monetary('Unit Price with Tax',
digits='currency', currency='currency', states=STATES, depends=DEPENDS),
'get_origin_fields')
discount = fields.Function(Monetary('Discount',
digits=discount_digits, states=STATES, depends=DEPENDS),
'get_origin_fields')
def _get_tax_rule_pattern(self):
'''
Get tax rule pattern
'''
return {}
@classmethod
def get_origin_fields(cls, moves, names):
pool = Pool()
Config = pool.get('stock.configuration')
Tax = pool.get('account.tax')
config = Config(1)
result = {n: {r.id: _ZERO for r in moves} for n in {'gross_unit_price',
'amount', 'taxes', 'unit_price_w_tax', 'discount'}}
def compute_amount_with_tax(move, taxes, amount):
tax_amount = _ZERO
if taxes:
tax_list = Tax.compute(taxes,
move.unit_price or Decimal('0.0'),
move.quantity or 0.0)
tax_amount = sum([t['amount'] for t in tax_list],
Decimal('0.0'))
return amount + tax_amount
for move in moves:
origin = move.origin
if isinstance(origin, cls):
origin = origin.origin
shipment = move.shipment or None
party = (getattr(shipment, PARTIES.get(shipment.__name__))
if shipment and PARTIES.get(shipment.__name__) else None)
if shipment and shipment.__name__ == 'stock.shipment.internal':
# party is from company.party
party = party.party
# amount
unit_price = None
amount = _ZERO
if config.valued_origin and hasattr(origin, 'unit_price'):
unit_price = (origin.unit_price if origin.unit_price != None
else (move.unit_price or _ZERO))
else:
unit_price = (move.unit_price if move.unit_price != None
else (move.unit_price or _ZERO))
if unit_price:
amount = (Decimal(
str(move.get_quantity_for_value() or 0)) * (unit_price))
if move.currency:
amount = move.currency.round(amount)
result['amount'][move.id] = amount
# taxes
taxes = []
if config.valued_origin and hasattr(origin, 'taxes'):
taxes = origin.taxes
else:
pattern = move._get_tax_rule_pattern()
tax_rule = None
taxes_used = []
if shipment:
if shipment.__name__.startswith('stock.shipment.out'):
tax_rule = party and party.customer_tax_rule or None
taxes_used = (move.product.customer_taxes_used
if party else [])
elif shipment.__name__.startswith('stock.shipment.in'):
tax_rule = party and party.supplier_tax_rule or None
taxes_used = (move.product.supplier_taxes_used
if party else [])
for tax in taxes_used:
if tax_rule:
tax_ids = tax_rule.apply(tax, pattern)
if tax_ids:
taxes.extend(Tax.browse(tax_ids))
continue
taxes.append(tax)
if tax_rule:
tax_ids = tax_rule.apply(None, pattern)
if tax_ids:
taxes.extend(Tax.browse(tax_ids))
if taxes:
result['taxes'][move.id] = [t.id for t in taxes]
# unit_price_w_tax
unit_price_w_tax = _ZERO
if move.quantity and move.quantity != 0:
amount_w_tax = compute_amount_with_tax(move, taxes, amount)
unit_price_w_tax = amount_w_tax / Decimal(str(move.quantity))
result['unit_price_w_tax'][move.id] = unit_price_w_tax
if 'gross_unit_price' in names:
gross_unit_price = None
origin = move.origin
if isinstance(origin, cls):
origin = origin.origin
if (config.valued_origin and
hasattr(origin, 'gross_unit_price')):
gross_unit_price = origin.gross_unit_price
else:
gross_unit_price = unit_price_w_tax
if gross_unit_price:
result['gross_unit_price'][move.id] = gross_unit_price
if 'discount' in names:
discount = _ZERO
if (config.valued_origin and hasattr(origin, 'discount')):
discount = origin.discount
result['discount'][move.id] = discount
return result
def get_quantity_for_value(self):
return self.quantity