Odoo 10指定日期产品数量获取及库存表格创建咨询
嘿,针对你提出的两个Odoo 10库存相关问题,我来给你详细拆解解决方法:
问题1:如何在Odoo 10中获取指定日期的产品数量
在Odoo 10里,库存数量的计算依赖于库存移动记录(stock.move)和库存批次记录(stock.quant)——前者记录所有出入库操作,后者存储实时库存及初始库存信息。要获取指定日期的产品库存,本质是计算该日期前所有入库总和减去出库总和,再加上初始库存的结果。
我给你写一个可复用的ORM方法,直接继承产品模型就能用:
from odoo import models, fields, api from datetime import datetime class ProductProduct(models.Model): _inherit = 'product.product' @api.multi def get_quantity_at_date(self, target_date, location_id=False): self.ensure_one() # 默认使用系统默认的库存位置(stock_location_stock) if not location_id: location_id = self.env.ref('stock.stock_location_stock').id # 计算指定日期前的入库总量(已完成的入库操作) in_moves = self.env['stock.move'].search([ ('product_id', '=', self.id), ('location_dest_id', '=', location_id), ('date', '<=', target_date), ('state', '=', 'done') ]) total_in = sum(in_moves.mapped('product_qty')) # 计算指定日期前的出库总量(已完成的出库操作) out_moves = self.env['stock.move'].search([ ('product_id', '=', self.id), ('location_id', '=', location_id), ('date', '<=', target_date), ('state', '=', 'done') ]) total_out = sum(out_moves.mapped('product_qty')) # 获取指定日期前的初始库存(stock.quant中记录的初始批次) initial_quants = self.env['stock.quant'].search([ ('product_id', '=', self.id), ('location_id', '=', location_id), ('in_date', '<=', target_date) ]) total_initial = sum(initial_quants.mapped('quantity')) # 最终库存 = 初始库存 + 入库总量 - 出库总量 return total_initial + total_in - total_out
使用方法很简单:比如要获取产品ID为1的产品在2024-05-15的库存,直接调用self.env['product.product'].browse(1).get_quantity_at_date('2024-05-15')即可。
注意:如果你的系统有多个仓库,记得传入对应的location_id参数;如果产品使用多单位,需要额外处理单位转换(乘以product_uom.factor)。
问题2:创建包含指定列的月度库存报表表格
有了问题1的方法,生成你需要的四列报表就很容易了。我们可以创建一个专门的报表模型,用来生成月度库存数据:
class MonthlyStockReport(models.Model): _name = 'monthly.stock.report' _description = 'Monthly Stock Inventory Report' product_id = fields.Many2one('product.product', string='Product', required=True) month = fields.Date(string='Month', required=True) quantity_in_the_first_day_of_month = fields.Float(string='Qty on First Day of Month') input_quantity = fields.Float(string='Input Quantity') output_quantity = fields.Float(string='Output Quantity') quantity_in_the_last_day_of_the_month = fields.Float(string='Qty on Last Day of Month') @api.multi def generate_yearly_report(self, target_year): # 清空旧报表数据 self.unlink() # 获取所有需要统计的产品 products = self.env['product.product'].search([]) # 默认库存位置 stock_location = self.env.ref('stock.stock_location_stock') for month_num in range(1, 13): # 计算当月第一天和最后一天的日期 first_day = datetime(target_year, month_num, 1).strftime('%Y-%m-%d') last_day = datetime(target_year, month_num, calendar.monthrange(target_year, month_num)[1]).strftime('%Y-%m-%d') for product in products: # 调用问题1的方法获取月初、月末库存 qty_first_day = product.get_quantity_at_date(first_day, stock_location.id) qty_last_day = product.get_quantity_at_date(last_day, stock_location.id) # 计算当月入库总量 monthly_input = sum(self.env['stock.move'].search([ ('product_id', '=', product.id), ('location_dest_id', '=', stock_location.id), ('date', '>=', first_day), ('date', '<=', last_day), ('state', '=', 'done') ]).mapped('product_qty')) # 计算当月出库总量 monthly_output = sum(self.env['stock.move'].search([ ('product_id', '=', product.id), ('location_id', '=', stock_location.id), ('date', '>=', first_day), ('date', '<=', last_day), ('state', '=', 'done') ]).mapped('product_qty')) # 创建报表记录 self.create({ 'product_id': product.id, 'month': first_day, 'quantity_in_the_first_day_of_month': qty_first_day, 'input_quantity': monthly_input, 'output_quantity': monthly_output, 'quantity_in_the_last_day_of_the_month': qty_last_day, }) return True
你只需要调用self.env['monthly.stock.report'].generate_yearly_report(2024),就能生成2024年所有产品的月度库存报表。之后可以基于这个模型创建QWeb视图或者导出Excel,满足展示需求。
内容的提问来源于stack exchange,提问作者MOHAMED
相关产品推荐
相关产品推荐

