-
Notifications
You must be signed in to change notification settings - Fork 3
/
sale.py
87 lines (75 loc) · 2.98 KB
/
sale.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
# -*- coding: utf-8 -*-
"""
sale.py
:copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from trytond.pool import Pool, PoolMeta
from trytond.model import fields
from trytond.transaction import Transaction
from trytond.pyson import Eval, Or
__metaclass__ = PoolMeta
__all__ = ['SaleLine']
class SaleLine:
__name__ = 'sale.line'
available_stock_qty = fields.Function(
fields.Float(
'Available Quantity',
digits=(16, Eval('unit_digits', 2)),
states={
'invisible': Or(
Eval('type') != 'line',
Eval('sale_state') == 'done'
),
},
depends=['type', 'unit_digits', 'sale_state']
),
'on_change_with_available_stock_qty'
)
sale_state = fields.Function(
fields.Char('Sale State'), 'get_sale_state'
)
@fields.depends(
'_parent_sale.warehouse', '_parent_sale.sale_date',
'product', 'type')
def on_change_with_available_stock_qty(self, name=None):
"""
Returns the available stock to process a Sale
"""
Location = Pool().get('stock.location')
Date = Pool().get('ir.date')
Product = Pool().get('product.product')
try:
self.warehouse
except AttributeError:
# On change will not have the warehouse set since its constructed
# from a dictionary of changes rather than the database active
# record. The warehouse on line is never displayed on view and
# won't be there.
self.warehouse = Location(self.get_warehouse(None))
# If a date is specified on sale, use that. If not, use the
# current date.
date = self.sale.sale_date or Date.today()
# If the sales person is taking an order for a date in the past
# (which tryton allows), the stock cannot be of the past, but of
# the current date.
date = max(date, Date.today())
if self.type == 'line' and self.product and self.warehouse:
with Transaction().set_context(
locations=[self.warehouse.id], # warehouse of the line
stock_skip_warehouse=True, # quantity of storage only
stock_date_end=date, # Stock as of sale date
stock_assign=True): # Exclude Assigned
product = Product(self.product.id)
if date <= Date.today():
return product.quantity
else:
# For a sale in the future, it is more interesting to
# see the forecasted quantity rather than what is
# currently in the warehouse.
return product.forecast_quantity
def get_sale_state(self, name):
"""
Returns the state of the Sale
"""
return self.sale and self.sale.state