flectra/addons/stock/models/stock_production_lot.py

71 lines
3.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-16 11:34:37 +01:00
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
2018-01-16 11:34:37 +01:00
from flectra import api, fields, models, _
from flectra.exceptions import UserError
class ProductionLot(models.Model):
_name = 'stock.production.lot'
_inherit = ['mail.thread']
_description = 'Lot/Serial'
name = fields.Char(
'Lot/Serial Number', default=lambda self: self.env['ir.sequence'].next_by_code('stock.lot.serial'),
required=True, help="Unique Lot/Serial Number")
ref = fields.Char('Internal Reference', help="Internal reference number in case it differs from the manufacturer's lot/serial number")
product_id = fields.Many2one(
'product.product', 'Product',
domain=[('type', 'in', ['product', 'consu'])], required=True)
product_uom_id = fields.Many2one(
'product.uom', 'Unit of Measure',
related='product_id.uom_id', store=True)
quant_ids = fields.One2many('stock.quant', 'lot_id', 'Quants', readonly=True)
create_date = fields.Datetime('Creation Date')
product_qty = fields.Float('Quantity', compute='_product_qty')
_sql_constraints = [
('name_ref_uniq', 'unique (name, product_id)', 'The combination of serial number and product must be unique !'),
]
@api.model
def create(self, vals):
active_picking_id = self.env.context.get('active_picking_id', False)
if active_picking_id:
picking_id = self.env['stock.picking'].browse(active_picking_id)
if picking_id and not picking_id.picking_type_id.use_create_lots:
raise UserError(_("You are not allowed to create a lot for this picking type"))
return super(ProductionLot, self).create(vals)
2018-07-06 14:58:06 +02:00
@api.multi
def write(self, vals):
if 'product_id' in vals:
move_lines = self.env['stock.move.line'].search([('lot_id', 'in', self.ids)])
if move_lines:
raise UserError(_(
'You are not allowed to change the product linked to a serial or lot number ' +
'if some stock moves have already been created with that number. ' +
'This would lead to inconsistencies in your stock.'
))
return super(ProductionLot, self).write(vals)
@api.one
def _product_qty(self):
# We only care for the quants in internal or transit locations.
quants = self.quant_ids.filtered(lambda q: q.location_id.usage in ['internal', 'transit'])
self.product_qty = sum(quants.mapped('quantity'))
@api.multi
def action_traceability(self):
move_ids = self.mapped('quant_ids').mapped('history_ids').ids
if not move_ids:
return False
return {
'domain': [('id', 'in', move_ids)],
'name': _('Traceability'),
'view_mode': 'tree,form',
'view_type': 'form',
'context': {'tree_view_ref': 'stock.view_move_tree'},
'res_model': 'stock.move',
'type': 'ir.actions.act_window'}