flectra/addons/stock/models/stock_production_lot.py
2018-01-16 02:34:37 -08:00

59 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
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)
@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'}