Files
tryton/inventory.py
T

293 lines
11 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.
2008-05-06 11:24:28 +02:00
'Inventory'
2008-07-17 19:37:38 +02:00
from trytond.osv import fields, OSV
2008-08-26 09:16:05 +02:00
from trytond.wizard import Wizard
2008-05-06 11:24:28 +02:00
2008-05-07 02:18:29 +02:00
STATES = {
2008-05-08 13:48:55 +02:00
'readonly': "state != 'open'",
2008-05-07 02:18:29 +02:00
}
2008-05-06 11:24:28 +02:00
2008-05-08 17:33:44 +02:00
2008-05-06 11:24:28 +02:00
class Inventory(OSV):
'Stock Inventory'
_name = 'stock.inventory'
_description = __doc__
_rec_name = 'location'
location = fields.Many2One(
'stock.location', 'Location', required=True,
domain="[('type', '=', 'storage')]", states={
'readonly': "state != 'open' or bool(lines)",
})
date = fields.Date('Date', states=STATES)
2008-05-07 14:56:54 +02:00
lost_found = fields.Many2One(
'stock.location', 'Lost and Found', required=True,
2008-06-11 20:18:37 +02:00
domain="[('type', '=', 'lost_found')]", states=STATES)
2008-05-06 11:24:28 +02:00
lines = fields.One2Many(
2008-10-24 13:55:56 +02:00
'stock.inventory.line', 'inventory', 'Lines', states=STATES)
2008-05-08 13:48:55 +02:00
moves = fields.Many2Many(
'stock.move', 'inventory_move_rel', 'inventory', 'move',
2008-10-24 13:55:56 +02:00
'Moves')
2008-05-22 01:58:47 +02:00
company = fields.Many2One(
'company.company', 'Company', required=True, states={
'readonly': "state != 'open' or bool(lines)",
})
2008-10-24 13:55:56 +02:00
state = fields.Selection([
('open', 'Open'),
('done', 'Done'),
('cancel', 'Cancel'),
], '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
self._rpc_allowed += [
2008-05-14 12:40:27 +02:00
'set_state_cancel',
'set_state_open',
'set_state_done',
]
2008-05-07 14:56:54 +02:00
2008-05-06 11:24:28 +02:00
def default_state(self, cursor, user, context=None):
2008-05-07 02:18:29 +02:00
return 'open'
2008-05-06 11:24:28 +02:00
2008-05-07 14:56:54 +02:00
def default_date(self, cursor, user, context=None):
2008-10-08 12:19:58 +02:00
date_obj = self.pool.get('ir.date')
return date_obj.today(cursor, user, context=context)
2008-05-07 14:56:54 +02:00
2008-05-22 01:58:47 +02:00
def default_company(self, cursor, user, context=None):
2008-10-24 13:56:29 +02:00
company_obj = self.pool.get('company.company')
if context is None:
context = {}
2008-05-22 01:58:47 +02:00
if context.get('company'):
2008-10-24 13:56:29 +02:00
return company_obj.name_get(cursor, user, context['company'],
context=context)[0]
return False
2008-05-22 01:58:47 +02:00
2008-05-08 13:48:55 +02:00
def set_state_cancel(self, cursor, user, ids, context=None):
2008-05-14 12:40:27 +02:00
self.write(cursor, user, ids, {
'state': 'cancel',
}, context=context)
def set_state_open(self, cursor, user, ids, context=None):
self.write(cursor, user, ids, {
'state': 'open',
}, context=context)
def set_state_done(self, cursor, user, ids, context=None):
self.write(cursor, user, ids, {
'state': 'done',
}, context=context)
def _cancel(self, cursor, user, ids, context=None):
2008-05-08 13:48:55 +02:00
move_obj = self.pool.get("stock.move")
inventories = self.browse(cursor, user, ids, context=context)
move_ids = \
[move.id for inventory in inventories for move in inventory.moves]
2008-10-24 23:58:23 +02:00
return move_obj.write(cursor, user, move_ids, {
'state': 'cancel',
}, context=context)
2008-05-07 14:56:54 +02:00
2008-05-14 12:40:27 +02:00
def _done(self, cursor, user, ids, context=None):
2008-05-07 14:56:54 +02:00
move_obj = self.pool.get('stock.move')
2008-10-08 12:19:58 +02:00
date_obj = self.pool.get('ir.date')
2008-05-07 14:56:54 +02:00
inventories = self.browse(cursor, user, ids, context=context)
2008-05-08 14:16:28 +02:00
for inventory in inventories:
2008-05-14 12:40:27 +02:00
if inventory.state != 'open':
continue
2008-05-08 14:16:28 +02:00
moves = []
2008-05-07 14:56:54 +02:00
for line in inventory.lines:
delta_qty = line.expected_quantity - line.quantity
if delta_qty == 0.0:
continue
from_location = inventory.location.id
to_location = inventory.lost_found.id
2008-05-07 14:56:54 +02:00
if delta_qty < 0:
(from_location, to_location, delta_qty) = \
(to_location, from_location, -delta_qty)
2008-05-14 12:40:27 +02:00
move_id = move_obj.create(cursor, user, {
'from_location': from_location,
'to_location': to_location,
'quantity': delta_qty,
'product': line.product.id,
'uom': line.uom.id,
'company': inventory.company.id,
2008-05-14 12:40:27 +02:00
'state': 'done',
}, context=context)
2008-05-08 14:16:28 +02:00
moves.append(move_id)
self.write(cursor, user, ids, {
2008-10-08 12:19:58 +02:00
'date': date_obj.today(cursor, user, context=context),
'moves': [('add', x) for x in moves],
2008-05-14 12:40:27 +02:00
}, context=context)
2008-10-24 23:59:19 +02:00
def create(self, cursor, user, vals, context=None):
new_id = super(Inventory, self).create(cursor, user, vals,
context=context)
if 'state' in vals:
if vals['state'] == 'done':
self._done(cursor, user, [new_id], context=context)
elif vals['state'] == 'cancel':
self._cancel(cursor, user, [new_id], context=context)
return new_id
2008-05-14 12:40:27 +02:00
def write(self, cursor, user, ids, vals, context=None):
if 'state' in vals:
if vals['state'] == 'done':
self._done(cursor, user, ids, context=context)
elif vals['state'] == 'cancel':
self._cancel(cursor, user, ids, context=context)
return super(Inventory, self).write(cursor, user, ids, vals,
context=context)
2008-05-07 14:56:54 +02:00
2008-05-06 11:24:28 +02:00
Inventory()
2008-05-08 17:33:44 +02:00
2008-05-06 11:24:28 +02:00
class InventoryLine(OSV):
'Stock Inventory Line'
_name = 'stock.inventory.line'
_description = __doc__
_rec_name = 'product'
2008-07-12 01:27:59 +02:00
product = fields.Many2One('product.product', 'Product', required=True,
domain=[('type', '=', 'stockable')], on_change=['product'])
uom = fields.Function('get_uom', type='many2one', relation='product.uom',
string='UOM')
unit_digits = fields.Function('get_unit_digits', type='integer',
string='Unit Digits')
expected_quantity = fields.Float('Expected Quantity',
digits="(16, unit_digits)", readonly=True)
quantity = fields.Float('Quantity', digits="(16, unit_digits)")
2008-05-06 11:24:28 +02:00
inventory = fields.Many2One('stock.inventory', 'Inventory')
2008-05-07 02:18:29 +02:00
def __init__(self):
super(InventoryLine, self).__init__()
self._sql_constraints += [
('check_line_qty_pos',
2008-10-24 23:59:44 +02:00
'CHECK(quantity >= 0.0)', 'Line quantity must be positive!'),
2008-05-14 12:40:27 +02:00
('inventory_product_uniq', 'UNIQUE(inventory, product)',
'Product must be unique by inventory!'),
2008-05-07 02:18:29 +02:00
]
2008-10-24 23:59:59 +02:00
def default_unit_digits(self, cursor, user, context=None):
return 2
def on_change_product(self, cursor, user, ids, vals, context=None):
product_obj = self.pool.get('product.product')
uom_obj = self.pool.get('product.uom')
res = {}
2008-10-24 23:59:59 +02:00
res['unit_digits'] = 2
if vals.get('product'):
product = product_obj.browse(cursor, user, vals['product'],
context=context)
res['uom'] = uom_obj.name_get(cursor, user, product.default_uom.id,
context=context)[0]
res['unit_digits'] = product.default_uom.digits
return res
def get_uom(self, cursor, user, ids, name, arg, context=None):
uom_obj = self.pool.get('product.uom')
res = {}
for line in self.browse(cursor, user, ids, context=context):
res[line.id] = line.product.default_uom.id
uom2name = {}
for uom_id, name in uom_obj.name_get(cursor, user, res.values(),
context=context):
uom2name[uom_id] = (uom_id, name)
for line_id in res:
res[line_id] = uom2name[res[line_id]]
return res
2008-05-06 11:24:28 +02:00
def get_unit_digits(self, cursor, user, ids, name, arg, context=None):
res = {}
for line in self.browse(cursor, user, ids, context=context):
res[line.id] = line.product.default_uom.digits
return res
2008-05-06 11:24:28 +02:00
InventoryLine()
class CompleteInventory(Wizard):
'Complete Inventory '
_name = 'stock.inventory.complete'
states = {
'init': {
'result': {
'type': 'action',
'action': '_complete',
'state': 'end',
},
},
}
2008-05-08 14:16:28 +02:00
2008-05-06 11:24:28 +02:00
def _complete(self, cursor, user, data, context=None):
line_obj = self.pool.get('stock.inventory.line')
2008-05-07 02:18:29 +02:00
inventory_obj = self.pool.get('stock.inventory')
2008-05-06 11:24:28 +02:00
product_obj = self.pool.get('product.product')
uom_obj = self.pool.get('product.uom')
2008-05-06 11:24:28 +02:00
inventories = inventory_obj.browse(cursor, user, data['ids'],
2008-05-14 12:40:27 +02:00
context=context)
context = context and context.copy() or {}
2008-05-08 14:16:28 +02:00
for inventory in inventories:
# Compute product quantities
if inventory.date:
context['stock_date_end'] = inventory.date
pbl = product_obj.products_by_location(
2008-07-11 23:20:17 +02:00
cursor, user, [inventory.location.id],
2008-05-08 14:16:28 +02:00
context=context)
else:
pbl = product_obj.products_by_location(
cursor, user, [inventory.location.id], context=context)
# Index some data
product2uom = {}
2008-08-22 11:16:10 +02:00
for product in product_obj.browse(
cursor, user, [line[1] for line in pbl], context=context):
product2uom[product.id] = product.default_uom.id
product_qty = {}
for (location, product), quantity in pbl.iteritems():
product_qty[product] = (quantity, product2uom[product])
# Update existing lines
for line in inventory.lines:
if line.product.id in product_qty:
quantity, uom_id = product_qty[line.product.id]
del product_qty[line.product.id]
# if nothing as changed, continue
if line.quantity == line.expected_quantity == quantity \
and line.uom.id == uom_id:
continue
values = {'expected_quantity': quantity,
'uom': uom_id}
# update also quantity field if not edited
if line.quantity == line.expected_quantity:
values['quantity'] = max(quantity, 0.0)
else:
values = {'expected_quantity': 0.0,}
if line.quantity == line.expected_quantity:
values['quantity'] = 0
2008-05-06 11:24:28 +02:00
line_obj.write(
cursor, user, line.id, values, context=context)
# Create lines if needed
for product in product_qty:
quantity, uom_id = product_qty[product]
values = {
'product': product,
'expected_quantity': quantity,
'quantity': max(quantity, 0.0),
'uom': uom_id,
'inventory': inventory.id,
}
line_obj.create(
cursor, user, values, context=context)
2008-05-07 02:18:29 +02:00
return {}
2008-05-06 11:24:28 +02:00
CompleteInventory()