gpt4 book ai didi

python - 如何从自定义模块在 Odoo 中创建库存变动?

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:10 27 4
gpt4 key购买 nike

我正在为 Odoo 创建一个自定义模块。

我有不同阶段的制造订单:订单、准备、制造、运输和最终。

我有我想要制造的产品的product_id。

当“最终”阶段到来时,该产品的库存需要增加我从“数量”字段中选择的数量。

代码如下:

class fabrication_orders(models.Model):
_name = 'proyecto.fabrication_orders'
order_id = fields.Many2one('proyecto.products',required="true",string="Product Id")
name = fields.Char(related='order_id.name',required="true",string="Product Name")
order_number = fields.Char(compute='_get_order_number',string="Order Nº",store="true")
image = fields.Binary(related='order_id.image_medium',string="Image")
quantity = fields.Float(required="true")
date = fields.Datetime(required="true",string="Order Date") ### Order Date
end_date = fields.Datetime(compute='_get_end_date',string="Finish Date",store="true") ### Finish Date
initial_cost = fields.Float(related='order_id.standard_price',string="Initial Cost")
final_cost = fields.Float(compute='_get_order_cost',string="Fabrication Cost")
#venue = fields.Many2one('proyecto.venues',required="true",string="Ship to")
order_state = fields.Selection([
('orden', "Order"),
('preparacion', "Preparation"),
('fabricacion', "Fabrication"),
('envio', "Shipping"),
('final', "Final"),
], default='orden')


#Este metodo pone la fecha final segun la cantidad
@api.depends('date')
def _get_end_date(self):
for d in self:
if d.date:
d.end_date = datetime.now() + timedelta(hours=d.quantity)


@api.depends('order_id')
def _get_order_number(self):
for r in self:
if r.order_id:
r.order_number=str(random.randint(1, 1e6))

@api.multi
def change_order_state(self):
for r in self:
if r.order_state == 'orden':
r.write({'order_state':'preparacion'})
elif r.order_state == 'preparacion':
r.write({'order_state':'fabricacion'})
elif r.order_state == 'fabricacion':
r.write({'order_state':'envio'})
elif r.order_state == 'envio':
r.write({'order_state':'final'})
r.end_date = datetime.now()

elif r.order_state == 'final':
raise ValidationError("La fabricación ha finalizado !")

您能帮忙增加产品的库存数量吗?

非常感谢。

最佳答案

我正在使用 Odoo 11。在我的用例中,当我的自定义模型的阶段设置为“完成”时,我希望减少产品的数量。库存数量由 stock.move 和 stock.move.line 更改。所以我所做的是创建一个 stock.move 并在状态更改为“完成”时将 stock.move.line 链接到它。

stock.move 创建的示例可以在 addons/stock/tests/test_move.py 中找到

这是一个食谱:

(1.) 如果您还没有,请创建一个位置

<record id="location_mylocation" model="stock.location">
<field name="name">MyLocation</field>
<field name="location_id" ref="stock.stock_location_locations_virtual"/>
<field name="usage">inventory</field>
<field name="company_id"></field>
</record>

使用量设置为“库存”以减少产品数量。报废订单使用相同的机制。

(2.) 创建 stock.move

stock_location = self.env.ref('stock.stock_location_stock')
move = self.env['stock.move'].create({
'name': 'Use on MyLocation',
'location_id': stock_location.id,
'location_dest_id': mylocation.id,
'product_id': product.id,
'product_uom': product.uom_id.id,
'product_uom_qty': qty,
})
move._action_confirm()
move._action_assign()
# This creates a stock.move.line record.
# You could also do it manually using self.env['stock.move.line'].create({...})
move.move_line_ids.write({'qty_done': qty})
move._action_done()

关于python - 如何从自定义模块在 Odoo 中创建库存变动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47739027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com