# -*- coding: utf-8 -*- from openerp import models, fields, api, _ from openerp.exceptions import Warning as UserError class Search_Barcode_Purchase(models.TransientModel): _name = 'search.barcode.purchase' _description = 'Buscar Producto por Codigo de Barra en Compras' quantity = fields.Float('Quantity', default='1.0') products_ids = fields.Many2many( 'product.product', string='Products', domain=[('sale_ok', '=', True)], ) product_code = fields.Char( string='Codigo de Barra EAN13 ', help="Este campo está diseñado para ser llenado con un lector de código de barras") @api.onchange('product_code') def product_code_change(self): if self.product_code: products = self.env['product.product'].search([('factory_barcode', '=ilike', self.product_code)]) if len(products) == 1: self.products_ids += products[0] self.product_code =None elif len(products) > 1: descrpcion="\n\nProductos con el codigo repetido : "+str(self.product_code) for xx in products: descrpcion +="\n"+str(xx.product_tmpl_id.name) self.product_code =None return {'warning': { 'title': _('Error'), 'message': _( 'Varios productos se han encontrado con el código ingresado,' '\nDebe seleccionar el producto manualmente.'+descrpcion)}} else: self.product_code =None return {'warning': { 'title': _('Error'), 'message': _( 'Ningún producto encontrado con el código ingresado, Debe seleccionar el producto manualmente')}} @api.one def add_multiple(self): active_id = self._context['active_id'] purchase = self.env['purchase.order'].browse(active_id) # x=0 # if purchase.order_line: # seq =purchase.order_line[len(purchase.order_line) - 1] # x=seq.sequence for product_id in self.products_ids: xxproducts = self.env['product.product'].search([('id', '=', product_id.id)]) orde_line_datos = self.env['purchase.order.line'].search([('order_id', '=', purchase.id), ('product_id', "=", xxproducts.id)]) # x += 1 product = self.env['purchase.order.line'].product_id_change( purchase.pricelist_id.id, product_id.id, qty=self.quantity, uom_id=product_id.uom_po_id.id, partner_id=purchase.partner_id.id, ) if orde_line_datos: orde_line_datos.write({ 'product_qty' : (orde_line_datos.product_qty + 1)}) else: val = { 'name': product['value'].get('name'), 'product_uom_qty': self.quantity, 'order_id': active_id, 'product_id': product_id.id or False, 'product_uom': product_id.uom_po_id.id, 'date_planned': product['value'].get('date_planned'), 'price_unit': product['value'].get('price_unit'), 'taxes_id': [(6, 0, product['value'].get('taxes_id'))], # 'sequence': x, } self.env['purchase.order.line'].create(val)