search_barcode_purchase.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning as UserError
  4. class Search_Barcode_Purchase(models.TransientModel):
  5. _name = 'search.barcode.purchase'
  6. _description = 'Buscar Producto por Codigo de Barra en Compras'
  7. quantity = fields.Float('Quantity', default='1.0')
  8. products_ids = fields.Many2many( 'product.product',
  9. string='Products',
  10. domain=[('sale_ok', '=', True)], )
  11. product_code = fields.Char( string='Codigo de Barra EAN13 ',
  12. help="Este campo está diseñado para ser llenado con un lector de código de barras")
  13. @api.onchange('product_code')
  14. def product_code_change(self):
  15. if self.product_code:
  16. products = self.env['product.product'].search([('factory_barcode', '=ilike', self.product_code)])
  17. if len(products) == 1:
  18. self.products_ids += products[0]
  19. self.product_code =None
  20. elif len(products) > 1:
  21. descrpcion="\n\nProductos con el codigo repetido : "+str(self.product_code)
  22. for xx in products:
  23. descrpcion +="\n"+str(xx.product_tmpl_id.name)
  24. self.product_code =None
  25. return {'warning': {
  26. 'title': _('Error'),
  27. 'message': _(
  28. 'Varios productos se han encontrado con el código ingresado,'
  29. '\nDebe seleccionar el producto manualmente.'+descrpcion)}}
  30. else:
  31. self.product_code =None
  32. return {'warning': {
  33. 'title': _('Error'),
  34. 'message': _(
  35. 'Ningún producto encontrado con el código ingresado, Debe seleccionar el producto manualmente')}}
  36. @api.one
  37. def add_multiple(self):
  38. active_id = self._context['active_id']
  39. purchase = self.env['purchase.order'].browse(active_id)
  40. # x=0
  41. # if purchase.order_line:
  42. # seq =purchase.order_line[len(purchase.order_line) - 1]
  43. # x=seq.sequence
  44. for product_id in self.products_ids:
  45. xxproducts = self.env['product.product'].search([('id', '=', product_id.id)])
  46. orde_line_datos = self.env['purchase.order.line'].search([('order_id', '=', purchase.id), ('product_id', "=", xxproducts.id)])
  47. # x += 1
  48. product = self.env['purchase.order.line'].product_id_change(
  49. purchase.pricelist_id.id,
  50. product_id.id,
  51. qty=self.quantity,
  52. uom_id=product_id.uom_po_id.id,
  53. partner_id=purchase.partner_id.id,
  54. )
  55. if orde_line_datos:
  56. orde_line_datos.write({ 'product_qty' : (orde_line_datos.product_qty + 1)})
  57. else:
  58. val = {
  59. 'name': product['value'].get('name'),
  60. 'product_uom_qty': self.quantity,
  61. 'order_id': active_id,
  62. 'product_id': product_id.id or False,
  63. 'product_uom': product_id.uom_po_id.id,
  64. 'date_planned': product['value'].get('date_planned'),
  65. 'price_unit': product['value'].get('price_unit'),
  66. 'taxes_id': [(6, 0, product['value'].get('taxes_id'))],
  67. # 'sequence': x,
  68. }
  69. self.env['purchase.order.line'].create(val)