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.

90 lines
4.2 KiB

8 years ago
8 years ago
8 years ago
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')
  13. default_reference_unit = fields.Many2one('product.uom')
  14. display_weight = fields.Float(compute='_get_display_weight', store=True)
  15. total_with_vat = fields.Float(compute='_get_total', store=True, string="Total Sales Price with VAT")
  16. total_with_vat_by_unit = fields.Float(compute='_get_total', store=True, string="Total Sales Price with VAT by Reference Unit")
  17. total_deposit = fields.Float(compute='_get_total', store=True, string="Deposit Price")
  18. label_to_be_printed = fields.Boolean('Print label?')
  19. label_last_printed = fields.Datetime('Label last printed on')
  20. # S0023 : List_price = Price HTVA, so add a suggested price
  21. list_price = fields.Float(string='exVAT Price')
  22. suggested_price = fields.Float(string='Suggested Price', compute='_compute_cost', readOnly=True)
  23. standard_price = fields.Float(compute='_compute_cost')
  24. def _get_main_supplier_info(self):
  25. return self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  26. @api.one
  27. @api.depends('seller_ids', 'seller_ids.date_start')
  28. def _compute_main_seller_id(self):
  29. # Calcule le vendeur associé qui a la date de début la plus récente et plus petite qu’aujourd’hui
  30. sellers_ids = self._get_main_supplier_info() # self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  31. self.main_seller_id = sellers_ids and sellers_ids[0].name or False
  32. @api.one
  33. @api.depends('taxes_id', 'list_price', 'taxes_id.amount', 'taxes_id.tax_group_id', 'total_with_vat', 'display_weight', 'weight')
  34. def _get_total(self):
  35. consignes_group = self.env.ref('beesdoo_product.consignes_group_tax')
  36. tax_amount_sum = sum([tax._compute_amount(self.list_price, self.list_price) for tax in self.taxes_id if tax.tax_group_id != consignes_group])
  37. self.total_deposit = sum([tax._compute_amount(self.list_price, self.list_price) for tax in self.taxes_id if tax.tax_group_id == consignes_group])
  38. self.total_with_vat = self.list_price + tax_amount_sum
  39. if self.display_weight > 0:
  40. self.total_with_vat_by_unit = self.total_with_vat / self.weight
  41. @api.one
  42. @api.depends('weight', 'display_unit')
  43. def _get_display_weight(self):
  44. self.display_weight = self.weight * self.display_unit.factor
  45. @api.one
  46. @api.constrains('display_unit', 'default_reference_unit')
  47. def _unit_same_category(self):
  48. if self.display_unit.category_id != self.default_reference_unit.category_id:
  49. raise UserError(_('Reference Unit and Display Unit should belong to the same category'))
  50. @api.one
  51. @api.depends('seller_ids')
  52. def _compute_cost(self):
  53. suppliers = self._get_main_supplier_info()
  54. if(len(suppliers) > 0):
  55. self.standard_price = suppliers[0].price
  56. self.suggested_price = (suppliers[0].price)* (1 + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100)
  57. class BeesdooProductLabel(models.Model):
  58. _name = "beesdoo.product.label"
  59. name = fields.Char()
  60. type = fields.Selection([('eco', 'Écologique'), ('local', 'Local'), ('fair', 'Équitable'), ('delivery', 'Distribution')])
  61. color_code = fields.Char()
  62. class BeesdooProductCategory(models.Model):
  63. _inherit = "product.category"
  64. profit_margin = fields.Float(default = '10.0', string = "Product Margin [%]")
  65. @api.one
  66. @api.constrains('profit_margin')
  67. def _check_margin(self):
  68. if (self.profit_margin < 0.0):
  69. raise UserError(_('Percentages for Profit Margin must > 0.'))