This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstock.py
213 lines (185 loc) · 7.28 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
# 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 Decimal
from functools import wraps
from trytond.i18n import gettext
from trytond.model import ModelView, Workflow, fields
from trytond.model.exceptions import AccessError
from trytond.modules.product import round_price
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
def process_sale(moves_field):
def _process_sale(func):
@wraps(func)
def wrapper(cls, shipments):
pool = Pool()
Sale = pool.get('sale.sale')
transaction = Transaction()
context = transaction.context
with transaction.set_context(_check_access=False):
sales = set(m.sale for s in cls.browse(shipments)
for m in getattr(s, moves_field) if m.sale)
func(cls, shipments)
if sales:
with transaction.set_context(
queue_batch=context.get('queue_batch', True)):
Sale.__queue__.process(sales)
return wrapper
return _process_sale
class ShipmentOut(metaclass=PoolMeta):
__name__ = 'stock.shipment.out'
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, shipments):
SaleLine = Pool().get('sale.line')
for shipment in shipments:
for move in shipment.outgoing_moves:
if (move.state == 'cancelled'
and isinstance(move.origin, SaleLine)):
raise AccessError(
gettext('sale.msg_sale_move_reset_draft',
move=move.rec_name))
return super(ShipmentOut, cls).draft(shipments)
@classmethod
@ModelView.button
@Workflow.transition('done')
@process_sale('outgoing_moves')
def done(cls, shipments):
super(ShipmentOut, cls).done(shipments)
@classmethod
@ModelView.button
@Workflow.transition('cancelled')
@process_sale('outgoing_moves')
def cancel(cls, shipments):
super(ShipmentOut, cls).cancel(shipments)
class ShipmentOutReturn(metaclass=PoolMeta):
__name__ = 'stock.shipment.out.return'
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, shipments):
SaleLine = Pool().get('sale.line')
for shipment in shipments:
for move in shipment.incoming_moves:
if (move.state == 'cancelled'
and isinstance(move.origin, SaleLine)):
raise AccessError(
gettext('sale.msg_sale_move_reset_draft',
move=move.rec_name))
return super(ShipmentOutReturn, cls).draft(shipments)
@classmethod
@ModelView.button
@Workflow.transition('received')
@process_sale('incoming_moves')
def receive(cls, shipments):
super(ShipmentOutReturn, cls).receive(shipments)
def process_sale_move(func):
@wraps(func)
def wrapper(cls, moves):
pool = Pool()
Sale = pool.get('sale.sale')
transaction = Transaction()
context = transaction.context
with transaction.set_context(_check_access=False):
sales = set(m.sale for m in cls.browse(moves) if m.sale)
func(cls, moves)
if sales:
with transaction.set_context(
queue_batch=context.get('queue_batch', True)):
Sale.__queue__.process(sales)
return wrapper
class Move(metaclass=PoolMeta):
__name__ = 'stock.move'
sale = fields.Function(
fields.Many2One('sale.sale', "Sale"),
'get_sale', searcher='search_sale')
sale_exception_state = fields.Function(fields.Selection([
('', ''),
('ignored', 'Ignored'),
('recreated', 'Recreated'),
], 'Exception State'), 'get_sale_exception_state')
@classmethod
def _get_origin(cls):
models = super(Move, cls)._get_origin()
models.append('sale.line')
return models
@classmethod
def check_origin_types(cls):
types = super(Move, cls).check_origin_types()
types.add('customer')
return types
def get_sale(self, name):
SaleLine = Pool().get('sale.line')
if isinstance(self.origin, SaleLine):
return self.origin.sale.id
@classmethod
def search_sale(cls, name, clause):
return [('origin.' + clause[0],) + tuple(clause[1:3])
+ ('sale.line',) + tuple(clause[3:])]
def get_sale_exception_state(self, name):
SaleLine = Pool().get('sale.line')
if not isinstance(self.origin, SaleLine):
return ''
if self in self.origin.moves_recreated:
return 'recreated'
if self in self.origin.moves_ignored:
return 'ignored'
return ''
@fields.depends('origin')
def on_change_with_product_uom_category(self, name=None):
pool = Pool()
SaleLine = pool.get('sale.line')
category = super(Move, self).on_change_with_product_uom_category(
name=name)
# Enforce the same unit category as they are used to compute the
# remaining quantity to ship and the quantity to invoice.
# Use getattr as reference field can have negative id
if (isinstance(self.origin, SaleLine)
and getattr(self.origin, 'unit', None)):
category = self.origin.unit.category.id
return category
def get_cost_price(self, product_cost_price=None):
pool = Pool()
SaleLine = pool.get('sale.line')
Sale = pool.get('sale.sale')
# For return sale's move use the cost price of the original sale
if (isinstance(self.origin, SaleLine)
and self.origin.quantity < 0
and self.from_location.type != 'storage'
and self.to_location.type == 'storage'
and isinstance(self.origin.sale.origin, Sale)):
sale = self.origin.sale.origin
cost = Decimal(0)
qty = Decimal(0)
for move in sale.moves:
if (move.state == 'done'
and move.from_location.type == 'storage'
and move.to_location.type == 'customer'
and move.product == self.product):
move_quantity = Decimal(str(move.internal_quantity))
cost_price = move.get_cost_price(
product_cost_price=move.cost_price)
qty += move_quantity
cost += cost_price * move_quantity
if qty:
product_cost_price = round_price(cost / qty)
return super().get_cost_price(product_cost_price=product_cost_price)
@property
def origin_name(self):
pool = Pool()
SaleLine = pool.get('sale.line')
name = super(Move, self).origin_name
if isinstance(self.origin, SaleLine):
name = self.origin.sale.rec_name
return name
@classmethod
@ModelView.button
@Workflow.transition('cancelled')
@process_sale_move
def cancel(cls, moves):
super(Move, cls).cancel(moves)
@classmethod
@process_sale_move
def delete(cls, moves):
super(Move, cls).delete(moves)