|
@@ -0,0 +1,202 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+from openerp import models, fields, api, _
|
|
|
+from openerp.exceptions import Warning as UserError
|
|
|
+
|
|
|
+
|
|
|
+class SearchBarcodePurchase(models.TransientModel):
|
|
|
+ _name = 'search.barcode.purchase'
|
|
|
+ _description = 'Buscar Producto por Codigo de Barra en Compras'
|
|
|
+
|
|
|
+ line_ids = fields.One2many(
|
|
|
+ 'search.barcode.purchase.line', 'wiz_id', string='Escaneos')
|
|
|
+ product_code = fields.Char(
|
|
|
+ string='Codigo de Barra EAN13',
|
|
|
+ help="Escanee el código de barras aquí"
|
|
|
+ )
|
|
|
+
|
|
|
+ @api.onchange('product_code')
|
|
|
+ def _onchange_product_code(self):
|
|
|
+ if not self.product_code:
|
|
|
+ return
|
|
|
+
|
|
|
+ products = self.env['product.product'].search([
|
|
|
+ ('factory_barcode', '=ilike', self.product_code)
|
|
|
+ ])
|
|
|
+
|
|
|
+ if len(products) == 1:
|
|
|
+ prod = products[0]
|
|
|
+ existing = self.line_ids.filtered(lambda l: l.product_id.id == prod.id)
|
|
|
+ if existing:
|
|
|
+ existing.qty += 1
|
|
|
+ else:
|
|
|
+ self.line_ids += self.line_ids.new({
|
|
|
+ 'product_id': prod.id,
|
|
|
+ 'qty': 1.0,
|
|
|
+ })
|
|
|
+ self.product_code = None
|
|
|
+
|
|
|
+ elif len(products) > 1:
|
|
|
+ desc = u"\n\nProductos con el código repetido: %s" % self.product_code
|
|
|
+ for p in products:
|
|
|
+ desc += u"\n%s" % p.product_tmpl_id.name
|
|
|
+ self.product_code = None
|
|
|
+ return {'warning': {
|
|
|
+ 'title': _('Error'),
|
|
|
+ 'message': _(u'Varios productos se han encontrado con el código ingresado.\n'
|
|
|
+ u'Debe seleccionar el producto manualmente.') + desc}}
|
|
|
+
|
|
|
+ else:
|
|
|
+ self.product_code = None
|
|
|
+ return {'warning': {
|
|
|
+ 'title': _('Error'),
|
|
|
+ 'message': _(u'Ningún producto encontrado con el código ingresado.\n'
|
|
|
+ u'Debe seleccionar el producto manualmente.')}}
|
|
|
+
|
|
|
+ @api.multi
|
|
|
+ def add_multiple(self):
|
|
|
+ self.ensure_one()
|
|
|
+ purchase = self.env['purchase.order'].browse(self._context.get('active_id'))
|
|
|
+ if not purchase:
|
|
|
+ raise UserError(_('No se encontró la orden de compra activa.'))
|
|
|
+
|
|
|
+ for line in self.line_ids:
|
|
|
+ product = line.product_id
|
|
|
+ existing = self.env['purchase.order.line'].search([
|
|
|
+ ('order_id', '=', purchase.id),
|
|
|
+ ('product_id', '=', product.id),
|
|
|
+ ], limit=1)
|
|
|
+
|
|
|
+ onchange_vals = self.env['purchase.order.line'].product_id_change(
|
|
|
+ False, # en compras no siempre hay pricelist_id
|
|
|
+ product.id,
|
|
|
+ qty=line.qty,
|
|
|
+ uom_id=product.uom_po_id.id,
|
|
|
+ partner_id=purchase.partner_id.id,
|
|
|
+ )
|
|
|
+
|
|
|
+ if existing:
|
|
|
+ existing.write({
|
|
|
+ 'product_qty': existing.product_qty + line.qty
|
|
|
+ })
|
|
|
+ else:
|
|
|
+ vals = {
|
|
|
+ 'name': onchange_vals['value'].get('name'),
|
|
|
+ 'order_id': purchase.id,
|
|
|
+ 'product_id': product.id,
|
|
|
+ 'product_uom': product.uom_po_id.id,
|
|
|
+ 'product_qty': line.qty,
|
|
|
+ 'date_planned': onchange_vals['value'].get('date_planned'),
|
|
|
+ 'price_unit': onchange_vals['value'].get('price_unit'),
|
|
|
+ 'taxes_id': [(6, 0, onchange_vals['value'].get('taxes_id') or [])],
|
|
|
+ }
|
|
|
+ self.env['purchase.order.line'].create(vals)
|
|
|
+
|
|
|
+ return {'type': 'ir.actions.act_window_close'}
|
|
|
+
|
|
|
+
|
|
|
+class SearchBarcodePurchaseLine(models.TransientModel):
|
|
|
+ _name = 'search.barcode.purchase.line'
|
|
|
+ _description = 'Líneas del wizard de lectura de EAN'
|
|
|
+
|
|
|
+ wiz_id = fields.Many2one(
|
|
|
+ 'search.barcode.purchase', ondelete='cascade')
|
|
|
+ product_id = fields.Many2one(
|
|
|
+ 'product.product', string='Producto',
|
|
|
+ domain=[('purchase_ok', '=', True)], required=True)
|
|
|
+ qty = fields.Float('Cantidad', default=1.0, required=True)
|
|
|
+
|
|
|
+
|
|
|
+# 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)
|
|
|
+
|
|
|
+ # if orde_line_datos:
|
|
|
+ # orde_line_datos.write({
|
|
|
+ # 'product_qty': orde_line_datos.product_qty + (self.quantity or 1.0)
|
|
|
+ # })
|
|
|
+ # else:
|
|
|
+ # val = {
|
|
|
+ # 'name': product['value'].get('name'),
|
|
|
+ # 'product_qty': self.quantity or 1.0, # <-- antes: product_uom_qty
|
|
|
+ # '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'))],
|
|
|
+ # }
|
|
|
+ # self.env['purchase.order.line'].create(val)
|