Search code examples
pythonodoo

Subtract products through two tables


It turns out that I am achieving a stock system, however, I managed to make it increase every time a product enters my warehouse but I can't make it subtract every time I leave.

The tables I work are product and document_ detail. In the document detail_table I have an attribute called quantity_to_have which has the function of adding the quantity of product that will leave the inventory for later being discounted and also the "quantity_of ", which has the function of putting the quantity of product entering the inventory The final stock is in the product table.

My code:

class Product (models.Model):

quantity_available = fields.Float (string = "Quantity available", compute = "_ stock")

detail_document_ids = fields.One2many (...)

@ api.one

@ api.depends ("detail_document_ids")

def _stock (self):

      sum = 0

      for detail_document in self.detail_document_ids:

          sum + = detail_document.cantity_deb

      self.quantity_available = sum

@ api.multi

@ api.depends ("detail_document_ids", "product_ids")

      def _stock (self):

          for detail_document in self.detail_document_ids:

              self.quantity_available = self.quantity_available - self. quantity_to_have

class Document_Detail (models.Model):

quantity_to_have = fields.Float (string = "Amount to have")

Solution

  • I don't know if I have understood you OK.

    I think that each one of your products has a list of document details. Each one of these details has a cantity_deb and a quantity_to_have. And the quantity available of each product is the sum of the cantity_deb minus the sum of the quantity_to have of all of its details.

    If that is the case:

    class Product (models.Model):
        ...
    
        quantity_available = fields.Float(
            string='Quantity available',
            compute='_stock',
        )
    
        detail_document_ids = fields.One2many(
            ...
        )
    
        @api.multi
        @api.depends('detail_document_ids', 'detail_document_ids.cantity_deb',
                     'detail_document_ids.quantity_to_have')
        def _stock(self):
            for product in self:
                sum = 0
                for detail_document in product.detail_document_ids:
                    sum += (detail_document.cantity_deb -
                        detail_document.quantity_to_have)
                product.quantity_available = sum
    
    
    class Document_Detail (models.Model):
        ...
    
        quantity_to_have = fields.Float(
            string = 'Amount to have',
        )