Hi, how are you? I made a custom module that allows me to visualize the available warehouses for each product that is chosen on the sales website. I did it using controllers and XML inheritance, now I need that when choosing a warehouse for a product, whose field (warehouse_id (many2one) ) shows me the same warehouse or value in the sales order line. I would appreciate my help, I am new to Odoo programming. I share my code to help of this one.
Controller (main.py)
classMultiWarehouses(WebsiteSale):
def _prepare_product_values(self, product, category, search, **kwargs):
values=super()._prepare_product_values(product, category, search, **kwargs)
warehouse_id=request.env['stock.warehouse']
wh_obj=request.env['stock.quant'].search([('product_tmpl_id','=', product[0].id),('location_id.usage','=','internal'),('product_tmpl_id.product_variant_id.product_template_variant_value_ids', 'in', product[0].id)])
for wh in warehouse_id:
if product.with_context(warehouse_id=[wh.id]).qty_available >0:
wh_obj+=wh
values['warehouse_id'] =wh_obj
return values
Website (website.py)
warehouse_ids= fields.Many2many(comodel_name='stock.warehouse', string='Secondary Warehouses')
website_id= fields.Many2one(comodel_name='website', string='Website')
def _prepare_sale_order_values(self, partner_sudo):
values=super()._prepare_sale_order_values(partner_sudo)
warehouse_id=self._get_warehouse_available()
product=request.env['product.template']warehouses=request.env['stock.warehouse']
wh_obj=request.env['stock.quant'].search([('product_tmpl_id','=', product.id),('location_id.usage','=','internal'),('product_tmpl_id.product_variant_id.product_template_variant_value_ids', 'in', product.id)])
for wh in warehouses:
if product.with_context(warehouse=[wh.id]).qty_available >0:
wh_obj+=wh
if warehouse_id:
values['warehouse_id'] =wh_obj
return values
def _get_warehouse_available(self):
return (self.warehouse_id.id or self.env['ir.default'].get('sale.order', 'warehouse_id', company_id=self.company_id.id) or self.env['ir.default'].get('sale.order', 'warehouse_id') or self.env['stock.warehouse'].sudo().search([('company_id', '=', self.company_id.id)], limit=1).id )
def sale_get_order(self, *args, **kwargs):
so=super().sale_get_order(*args, **kwargs)
return so.with_context(warehouse_id=so.warehouse_id.id) if so else so