采购入库单

This commit is contained in:
李鹏宇
2026-03-06 11:57:59 +08:00
parent 38315a1463
commit 901fc4cfeb
2 changed files with 42 additions and 40 deletions
+40 -38
View File
@@ -257,26 +257,20 @@ class StockPicking(models.Model):
return True
def action_update_name_by_products(self):
"""批量更新入库单名称(根据产品生成)"""
"""批量更新入库单编号(SH+年月+序号格式)"""
updated_count = 0
for picking in self:
# 只处理入库单
if picking.picking_type_code == 'incoming' and picking.move_ids_without_package:
product_names = picking.move_ids_without_package.mapped('product_id.name')
if product_names:
products_str = '/'.join(product_names)
# 添加入库单ID确保唯一性
new_name = f"{products_str}的入库单-{picking.id}"
picking.write({'name': new_name})
updated_count += 1
if picking.picking_type_code == 'incoming':
new_name = picking._generate_sh_sequence(picking.scheduled_date)
picking.write({'name': new_name})
updated_count += 1
# 返回提示消息
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': '更新完成',
'message': f'已成功更新 {updated_count} 条入库单的名称',
'message': f'已成功更新 {updated_count} 条入库单的编号',
'type': 'success',
'sticky': False,
}
@@ -293,40 +287,48 @@ class StockPicking(models.Model):
self.receipt_status = 'certification' # 状态变为证明
return True
def _generate_sh_sequence(self, date=None):
"""生成 SH+年月+三位序号 格式的编号,按月重置"""
from datetime import datetime
if not date:
date = datetime.now()
elif isinstance(date, str):
date = fields.Datetime.from_string(date)
year_month = date.strftime('%Y%m') # 例: 202601
prefix = f'SH{year_month}'
# 查询当月已有的最大序号
existing = self.env['stock.picking'].search([
('name', 'like', f'{prefix}%'),
('picking_type_code', '=', 'incoming')
], order='name desc', limit=1)
if existing:
try:
last_seq = int(existing.name[-3:])
next_seq = last_seq + 1
except (ValueError, IndexError):
next_seq = 1
else:
next_seq = 1
return f'{prefix}{next_seq:03d}'
@api.model_create_multi
def create(self, vals_list):
"""重写create方法,根据产品生成入库单名称"""
"""重写create方法,生成 SH+年月+序号 格式的入库单编号"""
pickings = super(StockPicking, self).create(vals_list)
for picking in pickings:
# 只处理入库单(picking_type_code为incoming)
if picking.picking_type_code == 'incoming' and picking.move_ids_without_package:
# 获取所有产品名称
product_names = picking.move_ids_without_package.mapped('product_id.name')
if product_names:
# 用/连接产品名称
products_str = '/'.join(product_names)
# 生成新的name格式:产品/产品/产品的入库单-ID(确保唯一性)
new_name = f"{products_str}的入库单-{picking.id}"
# 更新name字段
picking.write({'name': new_name})
# 只处理入库单
if picking.picking_type_code == 'incoming':
new_name = picking._generate_sh_sequence(picking.scheduled_date)
picking.write({'name': new_name})
return pickings
def write(self, vals):
"""重写write方法,当产品变化时更新入库单名称"""
"""重写write方法"""
result = super(StockPicking, self).write(vals)
# 如果修改了move_ids相关字段,更新name
if 'move_ids_without_package' in vals or 'move_ids' in vals:
for picking in self:
if picking.picking_type_code == 'incoming' and picking.move_ids_without_package:
product_names = picking.move_ids_without_package.mapped('product_id.name')
if product_names:
products_str = '/'.join(product_names)
new_name = f"{products_str}的入库单-{picking.id}"
# 避免递归调用,使用super().write
if picking.name != new_name:
super(StockPicking, picking).write({'name': new_name})
return result
+2 -2
View File
@@ -90,9 +90,9 @@
<field name="inherit_id" ref="stock.vpicktree"/>
<field name="arch" type="xml">
<xpath expr="//tree/header" position="inside">
<button name="action_update_name_by_products" type="object" string="根据产品更新名称"
<button name="action_update_name_by_products" type="object" string="重新生成编号"
class="btn-secondary"
help="将选中的入库单名称更新为:产品1/产品2/产品3的入库单"/>
help="将选中的入库单重新生成 SH+年月+序号 格式的编号"/>
</xpath>
<xpath expr="//field[@name='name']" position="attributes">
<attribute name="string">编号</attribute>