You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2015-TODAY Akretion (<http://www.akretion.com>).
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import sys
  5. from odoo import models, fields, api, tools
  6. class ProductTemplate(models.Model):
  7. _inherit = 'product.template'
  8. pos_categ_id = fields.Many2one('product.category',
  9. store=True, related='categ_id')
  10. @api.model
  11. def create(self, vals):
  12. if 'categ_id' in vals:
  13. vals['pos_categ_id'] = vals['categ_id']
  14. return super(ProductTemplate, self).create(vals)
  15. @api.multi
  16. def write(self, vals):
  17. if 'pos_categ_id' in vals and not vals['pos_categ_id']:
  18. del vals['pos_categ_id']
  19. return super(ProductTemplate, self).write(vals)
  20. class ProductCategory(models.Model):
  21. _inherit = 'product.category'
  22. image = fields.Binary(help='Show Image Category in Form View')
  23. image_medium = fields.Binary(help='Show image category button in POS',
  24. compute="_compute_image",
  25. inverse="_set_image",
  26. store=True)
  27. available_in_pos = fields.Boolean(
  28. string="Available in the Point of Sale",
  29. default=True,
  30. help="Check if you want this category to appear in Point Of Sale.\n"
  31. "If you uncheck, children categories will becomes invisible too, "
  32. "whatever their checkbox state.")
  33. @api.multi
  34. def _compute_image(self):
  35. return dict(
  36. (rec.id, tools.image_get_resized_images(rec.image)) for rec in
  37. self)
  38. @api.one
  39. def _set_image(self):
  40. return self.write(
  41. {'image': tools.image_resize_image_big(self.image_medium)})
  42. _auto_end_original = models.BaseModel._auto_end
  43. @api.model
  44. def _auto_end(self):
  45. """ Create the foreign keys recorded by _auto_init.
  46. (pos_remove_pos_category monkey patching)
  47. """
  48. module = self._context['module']
  49. foreign_keys = []
  50. patched = 'odoo.addons.pos_remove_pos_category' in sys.modules
  51. for fk in self._foreign_keys:
  52. t = fk[0]
  53. k = fk[1]
  54. if patched and (t, k) == ('product_template', 'pos_categ_id'):
  55. if module == 'pos_remove_pos_category':
  56. self._cr.execute('''
  57. ALTER TABLE product_template
  58. DROP CONSTRAINT IF EXISTS
  59. product_template_pos_categ_id_fkey
  60. ''')
  61. self._cr.execute('''
  62. UPDATE product_template
  63. SET pos_categ_id = categ_id;
  64. ''')
  65. self._cr.execute('''
  66. ALTER TABLE product_template ADD CONSTRAINT
  67. "product_template_pos_categ_id_fkey"
  68. FOREIGN KEY (pos_categ_id)
  69. REFERENCES product_category(id) ON DELETE SET NULL;
  70. ''')
  71. continue
  72. foreign_keys.append(fk)
  73. self._foreign_keys = foreign_keys
  74. return _auto_end_original
  75. models.BaseModel._auto_end = _auto_end