# -*- coding: utf-8 -*- from odoo import fields, models, api from odoo.exceptions import ValidationError class SfLocation(models.Model): _inherit = 'stock.location' # 重写字段定义 name = fields.Char('Location Name', required=True, size=20) barcode = fields.Char('Barcode', copy=False, required=True, size=15) # 仓库类别(selection:仓库、库区、库位、货位) location_type = fields.Selection([ ('仓库', '仓库'), ('库区', '库区'), ('库位', '库位'), ('货位', '货位') ], string='仓库类别') # 仓库类型(分类:成品库、坯料库、原材料库、刀具库、线边料库、线边刀库) location_category = fields.Selection([ ('成品库', '成品库'), ('坯料库', '坯料库'), ('原材料库', '原材料库'), ('刀具库', '刀具库'), ('线边料库', '线边料库'), ('线边刀库', '线边刀库') ], string='仓库类型') # 库区类型(selection:拣货区、存货区、收货区、退货区、次品区) area_type = fields.Selection([ ('拣货区', '拣货区'), ('存货区', '存货区'), ('收货区', '收货区'), ('退货区', '退货区'), ('次品区', '次品区') ], string='库区类型') # 货架独有字段:通道、方向、货架高度(m)、货架层数、层数容量 channel = fields.Char(string='通道') direction = fields.Selection([ ('R', 'R'), ('L', 'L') ], string='方向') shelf_height = fields.Float(string='货架高度(m)') shelf_layer = fields.Integer(string='货架层数') layer_capacity = fields.Integer(string='层数容量') # 货位独有字段:货位状态、产品(关联产品对象)、产品序列号(关联产品序列号对象) location_status = fields.Selection([ ('空闲', '空闲'), ('占用', '占用'), ('禁用', '禁用') ], string='货位状态', default='空闲') product_id = fields.Many2one('product.template', string='产品') # product_sn_id = fields.Many2one('product.serial.number', string='产品序列号') # 添加SQL约束 _sql_constraints = [ ('name_uniq', 'unique(name)', '位置名称必须唯一!'), ] hide_location_type = fields.Boolean(compute='_compute_hide_what', string='隐藏仓库') hide_area = fields.Boolean(compute='_compute_hide_what', string='隐藏库区') hide_shelf = fields.Boolean(compute='_compute_hide_what', string='隐藏货架') hide_location = fields.Boolean(compute='_compute_hide_what', string='隐藏货位') @api.depends('location_type') def _compute_hide_what(self): """ 根据仓库类别,隐藏不需要的字段 :return: """ for record in self: record.hide_location_type = False record.hide_area = False record.hide_shelf = False record.hide_location = False if record.location_type and record.location_type == '仓库': record.hide_location_type = True elif record.location_type and record.location_type == '库区': record.hide_area = True elif record.location_type and record.location_type == '库位': record.hide_shelf = True elif record.location_type and record.location_type == '货位': record.hide_location = True else: pass # # 添加Python约束 # @api.constrains('name', 'barcode') # def _check_len(self): # for rec in self: # if len(rec.name) > 20: # raise ValidationError("Location Name length must be less equal than 20!") # if len(rec.barcode) > 15: # raise ValidationError("Barcode length must be less equal than 15!") @api.model def default_get(self, fields): res = super(SfLocation, self).default_get(fields) if 'barcode' in fields and 'barcode' not in res: # 这里是你生成barcode的代码 res['barcode'] = self.generate_barcode() # 假设你有一个方法generate_barcode来生成barcode return res def generate_barcode(self): # 这里是你生成barcode的代码 # 这只是一个示例,你需要根据你的实际需求来编写这个方法 last_location = self.search([], order='id desc', limit=1) if last_location: last_barcode = int(last_location.barcode or 0) return str(last_barcode + 1).zfill(8) else: return '00000001'