stock_change_product_qty.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import UserError
  5. class ProductChangeQuantity(models.TransientModel):
  6. _name = "stock.change.product.qty"
  7. _description = "Change Product Quantity"
  8. product_id = fields.Many2one('product.product', 'Product', required=True)
  9. product_tmpl_id = fields.Many2one('product.template', 'Template', required=True)
  10. product_variant_count = fields.Integer('Variant Count', related='product_tmpl_id.product_variant_count')
  11. new_quantity = fields.Float(
  12. 'New Quantity on Hand', default=1,
  13. digits='Product Unit of Measure', required=True,
  14. help='This quantity is expressed in the Default Unit of Measure of the product.')
  15. @api.onchange('product_id')
  16. def _onchange_product_id(self):
  17. self.new_quantity = self.product_id.qty_available
  18. @api.constrains('new_quantity')
  19. def check_new_quantity(self):
  20. if any(wizard.new_quantity < 0 for wizard in self):
  21. raise UserError(_('Quantity cannot be negative.'))
  22. def change_product_qty(self):
  23. """ Changes the Product Quantity by creating/editing corresponding quant.
  24. """
  25. warehouse = self.env['stock.warehouse'].search(
  26. [('company_id', '=', self.env.company.id)], limit=1
  27. )
  28. # Before creating a new quant, the quand `create` method will check if
  29. # it exists already. If it does, it'll edit its `inventory_quantity`
  30. # instead of create a new one.
  31. self.env['stock.quant'].with_context(inventory_mode=True).create({
  32. 'product_id': self.product_id.id,
  33. 'location_id': warehouse.lot_stock_id.id,
  34. 'inventory_quantity': self.new_quantity,
  35. })._apply_inventory()
  36. return {'type': 'ir.actions.act_window_close'}