2
votes

I'm creating a custom module for Odoo.

I have Fabrication Orders with different phases : Order , Preparation , Fabrication , Shipping and Final.

And I have the product_id for the product that I want to Fabricate.

When the "Final" phase comes , the stock for that product need to be increased with the quantity that I choose from a field called "quantity".

Here's the code :

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 !")

Can you , please , help to increase the stock quantity of the product ?

Thanks a lot.

1

1 Answers

2
votes

I am using Odoo 11. In my use case I wanted to reduce the amount of a product when the stage of my custom model is set to 'done'. Inventory amount is changed by stock.move and stock.move.line. So what I did is creating a stock.move and linked a stock.move.line to it when the state changes to 'done'.

Examples of stock.move creation can be found in addons/stock/tests/test_move.py

Here is a recipe:

(1.) If you do not have one yet, create a location

<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>

The usage is set to 'inventory' to reduce the amount of the product. Scrap Orders use the same mechanism.

(2.) Create a 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()