如何在自定义模块中创建Odoo中的库存移动?

时间:2017-12-10 12:52:20

标签: python module openerp product stock

我正在为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 !")

请帮助增加产品的库存量?

非常感谢。

1 个答案:

答案 0 :(得分:0)

我正在使用Odoo11。在我的用例中,当我的自定义模型的阶段设置为“完成”时,我想减少产品的数量。库存量由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_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()