codigo_barra.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, api
  22. class product_template(models.Model):
  23. _name = 'product.template'
  24. _inherit = 'product.template'
  25. factory_barcode = fields.Char('Código de barra de fábrica',size=64)
  26. factory_reference = fields.Char('Referencia de fábrica',size=64)
  27. _defaults = {
  28. 'type':'product',
  29. }
  30. @api.multi
  31. def write(self, vals):
  32. prod_tmpl_id = super(product_template,self).write(vals)
  33. for p in self.product_variant_ids:
  34. prod = self.env['product.product'].browse(p['id'])
  35. ref = ''
  36. if prod.factory_reference:
  37. if prod['factory_reference'] != None:
  38. #prod.factory_reference = prod['factory_reference']
  39. prod.factory_reference = self.factory_reference
  40. ref = prod['factory_reference']
  41. elif self.factory_reference:
  42. prod.factory_reference = self.factory_reference
  43. ref = self['factory_reference']
  44. if prod.factory_barcode:
  45. if prod['factory_barcode'] != None:
  46. prod.factory_barcode = prod['factory_barcode']
  47. ref = prod['factory_barcode']
  48. elif self.factory_barcode:
  49. prod.factory_barcode = self.factory_barcode
  50. ref = self['factory_barcode']
  51. prod['default_code'] = ref
  52. return prod_tmpl_id
  53. class product_product(models.Model):
  54. _name = 'product.product'
  55. _inherit = 'product.product'
  56. factory_barcode = fields.Char('Código de barra de fábrica',size=64)
  57. factory_reference = fields.Char('Referencia de fábrica',size=64)
  58. @api.model
  59. def create(self, vals):
  60. prod_id = super(product_product,self).create(vals)
  61. self.copiar_referencia(prod_id)
  62. return prod_id
  63. @api.model
  64. def copiar_referencia(self,prod_id):
  65. # print prod_id['id']
  66. prod_tmpl = self.env['product.template'].browse(prod_id['product_tmpl_id']['id'])
  67. #print prod_tmpl
  68. ref = ''
  69. if prod_tmpl.factory_reference:
  70. prod_id['factory_reference'] = prod_tmpl.factory_reference
  71. ref = prod_tmpl.factory_reference
  72. if prod_tmpl.factory_barcode:
  73. prod_id['factory_barcode'] = prod_tmpl.factory_barcode
  74. ref = prod_tmpl.factory_barcode
  75. prod_id['default_code'] = ref
  76. return True
  77. @api.onchange('factory_barcode')
  78. def copiar_referencia_codigo_barra(self):
  79. self.default_code = self.factory_barcode
  80. product_product()