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.

60 lines
2.8 KiB

8 years ago
8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api
  3. from openerp.tools.translate import _
  4. from openerp.exceptions import UserError
  5. class BeesdooProduct(models.Model):
  6. _inherit = "product.template"
  7. eco_label = fields.Many2one('beesdoo.product.label', domain = [('type', '=', 'eco')])
  8. local_label = fields.Many2one('beesdoo.product.label', domain = [('type', '=', 'local')])
  9. fair_label = fields.Many2one('beesdoo.product.label', domain = [('type', '=', 'fair')])
  10. origin_label = fields.Many2one('beesdoo.product.label', domain = [('type', '=', 'delivery')])
  11. main_seller_id = fields.Many2one('res.partner', compute='_compute_main_seller_id', store=True)
  12. display_unit = fields.Many2one('product.uom', required=True, default=lambda self: self.env.ref('product.product_uom_kgm'))
  13. default_reference_unit = fields.Many2one('product.uom', required=True, default=lambda self: self.env.ref('product.product_uom_kgm'))
  14. display_weight = fields.Float(compute='_get_display_weight', store=True)
  15. total_with_vat = fields.Float(compute='_get_total_with_vat', store=True)
  16. total_with_vat_by_unit = fields.Float(compute='_get_total_with_vat_by_unit', store=True)
  17. @api.one
  18. @api.depends('seller_ids', 'seller_ids.date_start')
  19. def _compute_main_seller_id(self):
  20. # Calcule le vendeur associé qui a la date de début la plus récente et plus petite qu’aujourd’hui
  21. sellers_ids = self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  22. self.main_seller_id = sellers_ids and sellers_ids[0].name or False
  23. @api.one
  24. @api.depends('taxes_id', 'list_price')
  25. def _get_total_with_vat(self):
  26. tax_amount_sum = sum([tax.amount for tax in self.taxes_id])
  27. self.total_with_vat = self.list_price * (100.0 + tax_amount_sum) / 100
  28. @api.one
  29. @api.depends('total_with_vat', 'display_weight', 'weight')
  30. def _get_total_with_vat_by_unit(self):
  31. print self.display_weight, self.total_with_vat, self.weight
  32. if self.display_weight > 0:
  33. self.total_with_vat_by_unit = self.total_with_vat / self.weight
  34. @api.one
  35. @api.depends('weight', 'display_unit')
  36. def _get_display_weight(self):
  37. self.display_weight = self.weight * self.display_unit.factor
  38. @api.one
  39. @api.constrains('display_unit', 'default_reference_unit')
  40. def _unit_same_category(self):
  41. if self.display_unit.category_id != self.default_reference_unit.category_id:
  42. raise UserError(_('Reference Unit and Display Unit should belong to the same category'))
  43. class BeesdooProductLabel(models.Model):
  44. _name = "beesdoo.product.label"
  45. name = fields.Char()
  46. type = fields.Selection([('eco', 'Écologique'), ('local', 'Local'), ('fair', 'Équitable'), ('delivery', 'Distribution')])
  47. color_code = fields.Char()