Update product quantity wizard

Hi,

I was working on a Wizard to update product existence. I use these form[0] with Forecast Quantity and Quantity. To load Forecast Quantity I used the code below [1]. But, sometimes works and the most part no. The quantity is 0. I dont’ know if cache has something related. For example, if I have 10 in existence, but the most part only 0 is loaded.

After it added a transition to create an inventory with the quantity entered. [2] But, unfortunately if the forecast quantity is 0, the quantity does not update correctly.

[0]

[1]

    @staticmethod
    def default_forecast_quantity():
        pool = Pool()
        Product = pool.get('product.product')
        Date_ = pool.get('ir.date')
        Location = pool.get('stock.location')
        context = Transaction().context
        
        warehouse, = Location.search([('code', '=', 'STO')], limit=1)
        today = Date_.today()

        with Transaction().set_context(
                location_ids={warehouse.id},
                stock_date_end=today,
                forecast_date=today,
                locations=[warehouse.id]):
            product = Product(context.get('active_id'))

            return product.quantity

[2]

    def transition_modify(self):
        pool = Pool()
        Product = pool.get('product.product')
        Inventory = pool.get('stock.inventory')
        Date = pool.get('ir.date')
        today = Date.today()

        Inventory = pool.get('stock.inventory')
        InventoryLine = pool.get('stock.inventory.line')
        Location = pool.get('stock.location')
        Product = pool.get('product.product')

        try:
            warehouse, = Location.search([('code', '=', 'STO')], limit=1)
        except:
            raise SaleValidationError(
                gettext('sale_pos.msg_warehouse_not_found'))

        if self.model.__name__ == 'product.product':
            products = records = list(self.records)
            for product in products:
                product_to_update = product
                break
                # TODO
        elif self.model.__name__ == 'product.template':
            templates = records = list(self.records)
            for template in templates:
                for product in template.products:
                    product_to_update = product
                    break
        if not product_to_update:
            raise SaleValidationError(
                gettext('sale_pos.msg_product_not_found')) 

        company=Transaction().context.get('company')
        with Transaction().set_context(company=company,
                location_ids=[warehouse.id],
                stock_date_end=today,
                forecast_date=today):
            
            product_to_update = Product(product_to_update.id)
            products = [product_to_update]
            location_ids = Transaction().context.get('locations')
            product_ids = list(map(int, products))
            name='quantity'

            quantity = product_to_update.quantity

        inventory = Inventory(
            location=warehouse.id,
            date=today,
            empty_quantity="keep",
            )
        inventory.save()

        inventory_line = InventoryLine(
            product=product_to_update,
            expected_quantity=self.start.forecast_quantity,
            quantity=Decimal(self.start.quantity), #quantity
            inventory=inventory,
            )
        inventory_line.save()
        inventory.save()

        inventory.confirm([inventory])

        return 'end'

How to update the product quantity correctly?

Thanks in advance.

You can not update the product quantity because it is computed from the stock moves.

You can make an inventory to manage difference between the computation and the reality.

If you want to make forecast about the stock levels, you can use the stock_forecast module.

I do not want to make a forecast about the stock levels, only update reality of stock quantity.

In the wizard I want to see the quantity computed in the database and after it update to the realitty, but do not know how to.

This is what inventory does so you can just replicate it or use it.