Files
tryton/inventory.py
T

359 lines
12 KiB
Python
Raw Normal View History

2008-10-08 12:19:58 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level
#of this repository contains the full copyright notices and license terms.
2009-02-28 17:04:50 +01:00
from trytond.model import ModelWorkflow, ModelView, ModelSQL, fields
from trytond.wizard import Wizard
2010-01-20 11:04:28 +01:00
from trytond.pyson import Not, Equal, Eval, Or, Bool
from trytond.backend import TableHandler
2010-08-19 22:22:44 +02:00
from trytond.transaction import Transaction
2011-07-03 13:19:14 +02:00
from trytond.pool import Pool
2008-05-06 11:24:28 +02:00
2008-05-07 02:18:29 +02:00
STATES = {
2010-01-20 11:04:28 +01:00
'readonly': Not(Equal(Eval('state'), 'draft')),
2008-05-07 02:18:29 +02:00
}
2011-08-04 16:57:42 +02:00
DEPENDS = ['state']
2008-05-06 11:24:28 +02:00
2008-05-08 17:33:44 +02:00
2009-02-28 17:04:50 +01:00
class Inventory(ModelWorkflow, ModelSQL, ModelView):
2008-05-06 11:24:28 +02:00
'Stock Inventory'
_name = 'stock.inventory'
_description = __doc__
_rec_name = 'location'
location = fields.Many2One(
'stock.location', 'Location', required=True,
2008-11-11 23:33:38 +01:00
domain=[('type', '=', 'storage')], states={
2010-01-20 11:04:28 +01:00
'readonly': Or(Not(Equal(Eval('state'), 'draft')),
Bool(Eval('lines'))),
2011-08-04 16:57:42 +02:00
},
depends=['state', 'lines'])
date = fields.Date('Date', required=True, states={
2010-01-20 11:04:28 +01:00
'readonly': Or(Not(Equal(Eval('state'), 'draft')),
Bool(Eval('lines'))),
2011-08-04 16:57:42 +02:00
},
depends=['state', 'lines'])
2008-05-07 14:56:54 +02:00
lost_found = fields.Many2One(
'stock.location', 'Lost and Found', required=True,
2011-08-04 16:57:42 +02:00
domain=[('type', '=', 'lost_found')], states=STATES, depends=DEPENDS)
2008-05-06 11:24:28 +02:00
lines = fields.One2Many(
2011-08-04 16:57:42 +02:00
'stock.inventory.line', 'inventory', 'Lines', states=STATES,
depends=DEPENDS)
2010-01-20 11:04:28 +01:00
company = fields.Many2One('company.company', 'Company', required=True,
states={
'readonly': Or(Not(Equal(Eval('state'), 'draft')),
Bool(Eval('lines'))),
2011-08-04 16:57:42 +02:00
},
depends=['state', 'lines'])
2008-10-24 13:55:56 +02:00
state = fields.Selection([
2008-11-04 23:38:48 +01:00
('draft', 'Draft'),
2008-10-24 13:55:56 +02:00
('done', 'Done'),
2009-03-20 13:09:19 +01:00
('cancel', 'Canceled'),
2008-10-24 13:55:56 +02:00
], 'State', readonly=True, select=1)
2008-05-06 11:24:28 +02:00
def __init__(self):
2008-05-07 02:18:29 +02:00
super(Inventory, self).__init__()
2008-05-08 13:48:55 +02:00
self._order.insert(0, ('date', 'DESC'))
2008-05-07 14:56:54 +02:00
2010-08-19 22:22:44 +02:00
def init(self, module_name):
super(Inventory, self).init(module_name)
cursor = Transaction().cursor
# Add index on create_date
table = TableHandler(cursor, self, module_name)
table.index_action('create_date', action='add')
2010-08-19 22:22:44 +02:00
def default_state(self):
2008-11-04 23:38:48 +01:00
return 'draft'
2008-05-06 11:24:28 +02:00
2010-08-19 22:22:44 +02:00
def default_date(self):
2011-07-03 13:19:14 +02:00
date_obj = Pool().get('ir.date')
2010-08-19 22:22:44 +02:00
return date_obj.today()
2008-05-07 14:56:54 +02:00
2010-08-19 22:22:44 +02:00
def default_company(self):
return Transaction().context.get('company') or False
2008-05-22 01:58:47 +02:00
2010-08-19 22:22:44 +02:00
def default_lost_found(self):
2011-07-03 13:19:14 +02:00
location_obj = Pool().get('stock.location')
2010-08-19 22:22:44 +02:00
location_ids = location_obj.search(self.lost_found.domain)
2008-11-11 23:33:38 +01:00
if len(location_ids) == 1:
return location_ids[0]
2008-11-11 23:33:38 +01:00
return False
def wkf_draft(self, inventory):
self.write(inventory.id, {
2008-11-04 23:38:48 +01:00
'state': 'draft',
2010-08-19 22:22:44 +02:00
})
2008-05-14 12:40:27 +02:00
def wkf_cancel(self, inventory):
2011-07-03 13:19:14 +02:00
line_obj = Pool().get("stock.inventory.line")
2010-08-19 22:22:44 +02:00
line_obj.cancel_move(inventory.lines)
self.write(inventory.id, {
2008-10-24 23:58:23 +02:00
'state': 'cancel',
2010-08-19 22:22:44 +02:00
})
2008-05-07 14:56:54 +02:00
def wkf_done(self, inventory):
2011-07-03 13:19:14 +02:00
date_obj = Pool().get('ir.date')
line_obj = Pool().get('stock.inventory.line')
2008-10-08 12:19:58 +02:00
2008-11-04 23:38:48 +01:00
for line in inventory.lines:
2010-08-19 22:22:44 +02:00
line_obj.create_move(line)
self.write(inventory.id, {
'state': 'done',
2010-08-19 22:22:44 +02:00
})
2008-05-07 14:56:54 +02:00
2010-08-19 22:22:44 +02:00
def copy(self, ids, default=None):
2011-07-03 13:19:14 +02:00
date_obj = Pool().get('ir.date')
line_obj = Pool().get('stock.inventory.line')
2008-12-05 13:54:13 +01:00
int_id = False
if isinstance(ids, (int, long)):
int_id = True
ids = [ids]
2008-11-04 23:38:48 +01:00
if default is None:
default = {}
default = default.copy()
2010-08-19 22:22:44 +02:00
default['date'] = date_obj.today()
default['lines'] = False
2008-12-05 13:54:13 +01:00
new_ids = []
2010-08-19 22:22:44 +02:00
for inventory in self.browse(ids):
new_id = super(Inventory, self).copy(inventory.id, default=default)
line_obj.copy([x.id for x in inventory.lines],
2008-12-05 13:54:13 +01:00
default={
'inventory': new_id,
'move': False,
2010-08-19 22:22:44 +02:00
})
self.complete_lines(new_id)
2008-12-05 13:54:13 +01:00
new_ids.append(new_id)
if int_id:
return new_ids[0]
return new_ids
2008-10-24 23:59:19 +02:00
2010-08-19 22:22:44 +02:00
def complete_lines(self, ids):
'''
Complete or update the inventories
:param ids: the ids of stock.inventory
:param context: the context
'''
2011-07-03 13:19:14 +02:00
pool = Pool()
line_obj = pool.get('stock.inventory.line')
product_obj = pool.get('product.product')
uom_obj = pool.get('product.uom')
if isinstance(ids, (int, long)):
ids = [ids]
2010-08-19 22:22:44 +02:00
inventories = self.browse(ids)
2008-05-08 14:16:28 +02:00
for inventory in inventories:
# Compute product quantities
2010-08-19 22:22:44 +02:00
with Transaction().set_context(stock_date_end=inventory.date):
pbl = product_obj.products_by_location(
[inventory.location.id])
# Index some data
product2uom = {}
product2type = {}
2010-08-19 22:22:44 +02:00
for product in product_obj.browse([line[1] for line in pbl]):
2008-08-22 11:16:10 +02:00
product2uom[product.id] = product.default_uom.id
product2type[product.id] = product.type
product_qty = {}
for (location, product), quantity in pbl.iteritems():
product_qty[product] = (quantity, product2uom[product])
# Update existing lines
for line in inventory.lines:
2009-09-23 16:42:40 +02:00
if not (line.product.active and
line.product.type == 'stockable'):
2010-08-19 22:22:44 +02:00
line_obj.delete(line.id)
2009-09-23 16:42:40 +02:00
continue
if line.product.id in product_qty:
quantity, uom_id = product_qty.pop(line.product.id)
elif line.product.id in product2uom:
quantity, uom_id = 0.0, product2uom[line.product.id]
else:
quantity, uom_id = 0.0, line.product.default_uom.id
2010-08-19 22:22:44 +02:00
values = line_obj.update_values4complete(line, quantity, uom_id)
if values:
2010-08-19 22:22:44 +02:00
line_obj.write(line.id, values)
# Create lines if needed
for product_id in product_qty:
if product2type[product_id] != 'stockable':
continue
quantity, uom_id = product_qty[product_id]
2010-08-19 22:22:44 +02:00
values = line_obj.create_values4complete(product_id, inventory,
quantity, uom_id)
line_obj.create(values)
2008-11-04 23:38:48 +01:00
Inventory()
2009-02-28 17:04:50 +01:00
class InventoryLine(ModelSQL, ModelView):
2008-11-04 23:38:48 +01:00
'Stock Inventory Line'
_name = 'stock.inventory.line'
_description = __doc__
_rec_name = 'product'
product = fields.Many2One('product.product', 'Product', required=True,
domain=[('type', '=', 'stockable')], on_change=['product'])
2010-03-11 16:54:32 +01:00
uom = fields.Function(fields.Many2One('product.uom', 'UOM'), 'get_uom')
unit_digits = fields.Function(fields.Integer('Unit Digits'),
'get_unit_digits')
2008-11-04 23:38:48 +01:00
expected_quantity = fields.Float('Expected Quantity',
2010-03-11 22:35:29 +01:00
digits=(16, Eval('unit_digits', 2)), readonly=True,
depends=['unit_digits'])
quantity = fields.Float('Quantity', digits=(16, Eval('unit_digits', 2)),
depends=['unit_digits'])
2008-11-04 23:38:48 +01:00
move = fields.Many2One('stock.move', 'Move', readonly=True)
2009-01-08 20:04:31 +01:00
inventory = fields.Many2One('stock.inventory', 'Inventory', required=True,
ondelete='CASCADE')
2008-11-04 23:38:48 +01:00
def __init__(self):
super(InventoryLine, self).__init__()
self._sql_constraints += [
('check_line_qty_pos',
'CHECK(quantity >= 0.0)', 'Line quantity must be positive!'),
('inventory_product_uniq', 'UNIQUE(inventory, product)',
'Product must be unique by inventory!'),
]
2009-01-08 20:05:04 +01:00
self._order.insert(0, ('product', 'ASC'))
2008-11-04 23:38:48 +01:00
2010-08-19 22:22:44 +02:00
def default_unit_digits(self):
2008-11-04 23:38:48 +01:00
return 2
2010-08-19 22:22:44 +02:00
def on_change_product(self, vals):
2011-07-03 13:19:14 +02:00
product_obj = Pool().get('product.product')
uom_obj = Pool().get('product.uom')
2008-11-04 23:38:48 +01:00
res = {}
res['unit_digits'] = 2
if vals.get('product'):
2010-08-19 22:22:44 +02:00
product = product_obj.browse(vals['product'])
res['uom'] = product.default_uom.id
res['uom.rec_name'] = product.default_uom.rec_name
2008-11-04 23:38:48 +01:00
res['unit_digits'] = product.default_uom.digits
return res
2010-08-19 22:22:44 +02:00
def get_uom(self, ids, name):
2008-11-04 23:38:48 +01:00
res = {}
2010-08-19 22:22:44 +02:00
for line in self.browse(ids):
2008-11-04 23:38:48 +01:00
res[line.id] = line.product.default_uom.id
return res
2010-08-19 22:22:44 +02:00
def get_unit_digits(self, ids, name):
2008-11-04 23:38:48 +01:00
res = {}
2010-08-19 22:22:44 +02:00
for line in self.browse(ids):
2008-11-04 23:38:48 +01:00
res[line.id] = line.product.default_uom.digits
return res
2010-08-19 22:22:44 +02:00
def cancel_move(self, lines):
2011-07-03 13:19:14 +02:00
move_obj = Pool().get('stock.move')
2010-08-19 22:22:44 +02:00
move_obj.write( [l.move.id for l in lines if l.move], {
'state': 'cancel',
})
move_obj.delete([l.move.id for l in lines if l.move])
self.write([l.id for l in lines if l.move], {
'move': False,
})
2008-11-04 23:38:48 +01:00
2010-08-19 22:22:44 +02:00
def create_move(self, line):
'''
Create move for an inventory line
:param line: a BrowseRecord of inventory.line
:return: the stock.move id or None
'''
2011-07-03 13:19:14 +02:00
move_obj = Pool().get('stock.move')
uom_obj = Pool().get('product.uom')
delta_qty = uom_obj.compute_qty(line.uom,
line.expected_quantity - line.quantity,
line.uom)
2008-11-04 23:38:48 +01:00
if delta_qty == 0.0:
return
from_location = line.inventory.location.id
to_location = line.inventory.lost_found.id
if delta_qty < 0:
(from_location, to_location, delta_qty) = \
(to_location, from_location, -delta_qty)
2010-08-19 22:22:44 +02:00
move_id = move_obj.create({
2008-11-04 23:38:48 +01:00
'from_location': from_location,
'to_location': to_location,
'quantity': delta_qty,
'product': line.product.id,
'uom': line.uom.id,
'company': line.inventory.company.id,
'state': 'done',
'effective_date': line.inventory.date,
2010-08-19 22:22:44 +02:00
})
self.write(line.id, {
'move': move_id,
})
return move_id
2008-11-04 23:38:48 +01:00
2010-08-19 22:22:44 +02:00
def update_values4complete(self, line, quantity, uom_id):
'''
Return update values to complete inventory
:param line: a BrowseRecord of inventory.line
:param quantity: the actual product quantity for the inventory location
:param uom_id: the UoM id of the product line
:return: a dictionary
'''
res = {}
# if nothing changed, no update
if line.quantity == line.expected_quantity == quantity \
and line.uom.id == uom_id:
return {}
res['expected_quantity'] = quantity
res['uom'] = uom_id
# update also quantity field if not edited
if line.quantity == line.expected_quantity:
res['quantity'] = max(quantity, 0.0)
return res
2010-08-19 22:22:44 +02:00
def create_values4complete(self, product_id, inventory, quantity, uom_id):
'''
Return create values to complete inventory
:param product_id: the product.product id
:param inventory: a BrowseRecord of inventory.inventory
:param quantity: the actual product quantity for the inventory location
:param uom_id: the UoM id of the product_id
:return: a dictionary
'''
return {
'inventory': inventory.id,
'product': product_id,
'expected_quantity': quantity,
'quantity': max(quantity, 0.0),
'uom': uom_id,
}
2008-11-04 23:38:48 +01:00
InventoryLine()
class CompleteInventory(Wizard):
2008-11-21 14:55:58 +01:00
'Complete Inventory'
2008-11-04 23:38:48 +01:00
_name = 'stock.inventory.complete'
states = {
'init': {
'result': {
'type': 'action',
'action': '_complete',
'state': 'end',
},
},
}
2010-08-19 22:22:44 +02:00
def _complete(self, data):
2011-07-03 13:19:14 +02:00
inventory_obj = Pool().get('stock.inventory')
2010-08-19 22:22:44 +02:00
inventory_obj.complete_lines(data['ids'])
2008-11-04 23:38:48 +01:00
2008-05-07 02:18:29 +02:00
return {}
2008-05-06 11:24:28 +02:00
CompleteInventory()