0
votes

Suppose I have a field called isbn on stock_move, this field is of Boolean type, I can create a method, with domain filter, which will bring all the stock_move objects, with ('isbn', '=', 'True'),, and reflect that calculation on another field, like this:

@api.multi
@api.depends('stock_move')
def _compute_amount_total(self):
    draft_orders = self.env['stock.move'].search([('isbn', '=', True)])
    product_uom_qty = sum(draft_orders.mapped('product_uom_qty'))
    for record in self:
        record.total_isbn = product_uom_qty

So, by far, this is a nice approach, but now, suppose I want to do this, but not for ALL the stock_moves in which the isbn field is checked, but just for the stock_moves I'm calling from let's say 4 Many2one fields in a model, ie:

stock_move1 = fields.Many2one('stock.move', string="My Stock Move 1")
stock_move2 = fields.Many2one('stock.move', string="My Stock Move 2")
stock_move3 = fields.Many2one('stock.move', string="My Stock Move 3")
stock_move4 = fields.Many2one('stock.move', string="My Stock Move 4")

I can call the isbn field on my model like this though:

stock_move1 = fields.Many2one('stock.move', string="My Stock Move 1")
isbn1 = fields.Boolean(string="ISBN 1", related="stock_move1.isbn")
stock_move2 = fields.Many2one('stock.move', string="My Stock Move 2")
isbn2 = fields.Boolean(string="ISBN 2", related="stock_move2.isbn")
stock_move3 = fields.Many2one('stock.move', string="My Stock Move 3")
isbn3 = fields.Boolean(string="ISBN 3", related="stock_move3.isbn")
stock_move4 = fields.Many2one('stock.move', string="My Stock Move 4")
isbn4 = fields.Boolean(string="ISBN 4", related="stock_move4.isbn")

This should bring them, but actually I don't want to show them or anything on my view, just read those 4 stock_moves look for isbn = True and sum product_uom_qty on them.

I hope I've explained myself.

Any ideas on how this could be accomplished?

2
Can you explain what do you need exactly in a short sentenceCharif DZ
Hi, I need to sum up all the product_uom_qty fields in the stock_moves I bring by these many2one fields in my form/view, and store the result in a field, if there is one, show that result, if I choose all the 4, the sum up all the four product_uom_qty in those four stock_moves.NeoVe
So user select select a stock.move and you compute the total of quantityCharif DZ
Exactly, if 1, compute that one, if 2, or 4 compute all 4...NeoVe
So why all this related fields just add a compute field that sum the selcted quantityCharif DZ

2 Answers

2
votes

Maybe something like this:

@api.multi
@api.depends(
    'stock_move1.isbn', 'stock_move1.product_uom_qty',
    'stock_move2.isbn', 'stock_move2.product_uom_qty',
    'stock_move3.isbn', 'stock_move3.product_uom_qty',
    'stock_move4.isbn', 'stock_move4.product_uom_qty')
def _compute_amount_total(self):
    for record in self:
        moves = (record.stock_move1 +
            record.stock_move2 +
            record.stock_move3 +
            record.stock_move4).filtered('isbn')
        record.total_isbn = sum(moves.mapped('product_uom_qty'))

From what I understood from your description you don't really the isbn1..4 related fields.

1
votes

I think @naglis jonailis has answered your question. You may get some errors because he didn't check the values are empty or not he concatenated the field directly.

                total_quantity = 0
                # check if the field is selected and add the quantity 
                if rec.many2one_field :
                      total_quantity =+ rec.many2one_field.product_uom_qty


                # affect the sum to your computed field
                 rec.compute_sum_field = total_quantity

And one thind why using four many2one use many2many user can select many stock moved .