-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduction.py
122 lines (104 loc) · 4.53 KB
/
production.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
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from functools import wraps
from trytond.model import ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction, without_check_access
from trytond.wizard import Button, StateTransition, StateView, Wizard
from trytond.i18n import gettext
from trytond.exceptions import UserError
def process_sale():
def _process_sale(func):
@wraps(func)
def wrapper(cls, productions):
pool = Pool()
Sale = pool.get('sale.sale')
SaleLine = Pool().get('sale.line')
transaction = Transaction()
context = transaction.context
with without_check_access():
sales = list(set([p.origin.sale for p in productions
if p.origin and isinstance(p.origin, SaleLine)]))
func(cls, productions)
if sales:
with transaction.set_context(
queue_batch=context.get('queue_batch', True)):
Sale.__queue__.process(sales)
return wrapper
return _process_sale
class Production(metaclass=PoolMeta):
__name__ = 'production'
@classmethod
def _get_origin(cls):
return super()._get_origin() | {'sale.line'}
@classmethod
@process_sale()
def delete(cls, productions):
super().delete(productions)
class ChangeQuantityStart(ModelView):
'Change Production Quantity - Start'
__name__ = 'production.change_quantity.start'
production = fields.Many2One('production', 'Production', readonly=True)
sale_line = fields.Many2One('sale.line', 'Sale Line', readonly=True)
current_quantity = fields.Float('Current Quantity',
digits='uom', readonly=True)
new_quantity = fields.Float('New Quantity',
digits='uom', required=True,
domain=[
('new_quantity', '!=', Eval('current_quantity')),
('new_quantity', '>', 0),
])
uom = fields.Many2One('product.uom', 'Uom', readonly=True)
class ChangeQuantity(Wizard):
'Change Production Quantity'
__name__ = 'production.change_quantity'
start = StateView('production.change_quantity.start',
'sale_supply_production.production_change_quantity_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Modify', 'modify', 'tryton-ok', default=True),
])
modify = StateTransition()
def default_start(self, fields):
pool = Pool()
Production = pool.get('production')
SaleLine = Pool().get('sale.line')
production = Production(Transaction().context['active_id'])
if production.state not in ('draft', 'waiting'):
raise UserError(gettext(
'sale_supply_production.invalid_production_state',
production=production.rec_name))
if not isinstance(production.origin, SaleLine):
raise UserError(gettext(
'sale_supply_production.production_no_related_to_sale'))
productions = Production.search(['origin','=', str(production.origin)])
if len(productions) != 1:
raise UserError(gettext(
'sale_supply_production.production_with_same_origin',
productions=",".join([x.rec_name for x in productions]),
sale_line=production.origin.rec_name))
return {
'production': production.id,
'sale_line': production.origin.id,
'current_quantity': production.quantity,
'uom': production.unit.id,
}
def transition_modify(self):
pool = Pool()
Uom = pool.get('product.uom')
SaleChangeLineQuantity = pool.get('sale.change_line_quantity',
type='wizard')
sale_line = self.start.sale_line
sale_line_new_quantity = (sale_line.quantity
+ Uom.compute_qty(
self.start.uom,
self.start.new_quantity - self.start.current_quantity,
sale_line.unit))
sale_change_quantity = SaleChangeLineQuantity(self._session_id)
sale_change_quantity.start.sale = sale_line.sale
sale_change_quantity.start.line = sale_line
sale_change_quantity.start.current_quantity = sale_line.quantity
sale_change_quantity.start.new_quantity = sale_line_new_quantity
sale_change_quantity.start.unit = sale_line.unit
sale_change_quantity.transition_modify()
return 'end'