Read the product in rows and in warehouses

I need to check my lines, the products that I have, their number and find out what the availability of such products in warehouses is, stock.moveand stock.pickingdo something similar, but this is an old api, I need a custom method.

This is my method:

class bsi_production_order(models.Model):
_name = 'bsi.production.order'

name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Production Date")
production_type = fields.Selection([
        ('budgeted','Budgeted'),
        ('nonbudgeted','Non Budgeted'),
        ('direct','Direct Order'),
    ], string='Type of Order', index=True,  
    track_visibility='onchange', copy=False,
    help=" ")
notes = fields.Text(string="Notes")
order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")
warehouse_quantity = fields.Char(compute='quantity', string='Quantity per warehouse')

class bsi_production_order_lines(models.Model):
_name = 'bsi.production.order.lines'

production_order = fields.Many2one('bsi.production.order', string="Production Orders")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Integer(string="Quantity")
consumed_qty = fields.Float(string="Consumed quantity")
remaining_qty = fields.Float(string="Remaining quantity")

I need to check bsi.production.orderin the order_linesOne2many, field isbn, which is the product, how much it is available in all locations of the system, and also compare it with qty, so from there I can go to another state of the object.

Think of objects stock.pickingor stock.move. This is basically the same logic.

So far, I have tried this method to check if there is any row in the One2many object.

@api.multi
@api.depends('order_lines', 'order_lines.isbn')
def checkit(self):
    #actual_stock = self.env['product.product'].browse(qty_available)
    for record in self:
        if self.order_lines:
            for line in self.order_lines:
                if line.isbn:
                    return line.isbn
        else:
            raise Warning(('Enter​ ​at least​ ​1​ ​ISBN to produce'))

, , isbn, . , , , , stock.location.

OCA repo, , - .

, , :

@api.multi
@api.depends('order_lines', 'order_lines.isbn')
def quantity(self):
    for record in self:
        warehouse_quantity_text = ''
        isbn = self.env['product.product'].sudo().search([('product_tmpl_id', '=', record.id)])
        if isbn:
            quant_ids = self.env['stock.quant'].sudo().search([('isbn','=',isbn[0].id),('location_id.usage','=','internal')])
            t_warehouses = {}
            for quant in quant_ids:
                if quant.location_id:
                    if quant.location_id not in t_warehouses:
                        t_warehouses.update({quant.location_id:0})
                    t_warehouses[quant.location_id] += quant.qty

            tt_warehouses = {}
            for location in t_warehouses:
                warehouse = False
                location1 = location
                while (not warehouse and location1):
                    warehouse_id = self.env['stock.warehouse'].sudo().search([('lot_stock_id','=',location1.id)])
                    if len(warehouse_id) > 0:
                        warehouse = True
                    else:
                        warehouse = False
                    location1 = location1.location_id
                if warehouse_id:
                    if warehouse_id.name not in tt_warehouses:
                        tt_warehouses.update({warehouse_id.name:0})
                    tt_warehouses[warehouse_id.name] += t_warehouses[location]

            for item in tt_warehouses:
                if tt_warehouses[item] != 0:
                    warehouse_quantity_text = warehouse_quantity_text + ' ** ' + item + ': ' + str(tt_warehouses[item])
            record.warehouse_quantity = warehouse_quantity_text

, , , , , .

: , isbn () , qty, , , , .

+4
1

, , @api.constrains not @api.depends, @api.depends, .

, isbn, many2one product.product, , _ .

@api.constrains('order_lines', 'order_lines.isbn')
def checkit(self):
#actual_stock = self.env['product.product'].browse(qty_available)
for record in self:
    # inside the loop use record not self
    if self.order_lines:continue # if the order_lines contains one record go back and check the second record
         # no need for other instruction because if the field is empty this will full
         # another thing if you return the program will exit the function but you only
         # checked one record what if someone user write with mutliple record   
    else: # here order_line is empty
        raise Warning(('Enter? ?at least? ?1? ?ISBN to produce'))

, , , , .

@api.constrains('order_lines', 'order_lines.isbn')
  def checkit(self):
    for record in self:
        # inside the loop use record not self
        if self.order_lines:
             found_isbn = False
             for line in self.order_lines:
                if line.isbn:
                     found_isbn = True
                     break # no need to check other lines.

             if not found_isbn: # after the looping the lines check if the isbn is found
                raise Warning(('Enter at least one ISBN to produce'))

        else: # here order_line is empty
            raise Warning(('Enter? ?at least? ?1? ?ISBN to produce'))

, , , .

, - .

onchange

@api.onchange('order_lines.qty')
def check_quantity(self):
    if self.order_lines:
        for line in rec.order_lines:
            if line.qty > line.isbn.qty_available:
                # return warning or validation error if it restricted .
                return {'warning': {
                            'title': _('Warning'),
                            'message': _('Quantity is invalid.')
                        }

:

@api.constrains('order_lines.qty')
def check_quantity(self):
    for rec in self:
        if rec.order_lines:
            for line in rec.order_lines:
                if line.qty > line.isbn.qty_available:
                    # raise validation error to user .
+1

Source: https://habr.com/ru/post/1688113/


All Articles