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.

121 lines
5.8 KiB

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. import uuid
  6. class BeesdooProduct(models.Model):
  7. _inherit = "product.template"
  8. eco_label = fields.Many2one('beesdoo.product.label', domain=[('type', '=', 'eco')])
  9. local_label = fields.Many2one('beesdoo.product.label', domain=[('type', '=', 'local')])
  10. fair_label = fields.Many2one('beesdoo.product.label', domain=[('type', '=', 'fair')])
  11. origin_label = fields.Many2one('beesdoo.product.label', domain=[('type', '=', 'delivery')])
  12. main_seller_id = fields.Many2one('res.partner', compute='_compute_main_seller_id', store=True)
  13. display_unit = fields.Many2one('product.uom')
  14. default_reference_unit = fields.Many2one('product.uom')
  15. display_weight = fields.Float(compute='_get_display_weight', store=True)
  16. total_with_vat = fields.Float(compute='_get_total', store=True, string="Total Sales Price with VAT")
  17. total_with_vat_by_unit = fields.Float(compute='_get_total', store=True, string="Total Sales Price with VAT by Reference Unit")
  18. total_deposit = fields.Float(compute='_get_total', store=True, string="Deposit Price")
  19. label_to_be_printed = fields.Boolean('Print label?')
  20. label_last_printed = fields.Datetime('Label last printed on')
  21. note = fields.Text('Comments')
  22. # S0023 : List_price = Price HTVA, so add a suggested price
  23. list_price = fields.Float(string='exVAT Price')
  24. suggested_price = fields.Float(string='Suggested exVAT Price', compute='_compute_cost', readOnly=True)
  25. def _get_main_supplier_info(self):
  26. return self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  27. @api.one
  28. def generate_barcode(self):
  29. if self.to_weight:
  30. seq_internal_code = self.env.ref('beesdoo_product.seq_ean_product_internal_ref')
  31. bc = ''
  32. if not self.default_code:
  33. rule = self.env['barcode.rule'].search([('name', '=', 'Price Barcodes (Computed Weight) 2 Decimals')])[0]
  34. default_code = seq_internal_code.next_by_id()
  35. while(self.search_count([('default_code', '=', default_code)]) > 1):
  36. default_code = seq_internal_code.next_by_id()
  37. self.default_code = default_code
  38. ean = '02' + self.default_code[0:5] + '000000'
  39. bc = ean[0:12] + str(self.env['barcode.nomenclature'].ean_checksum(ean))
  40. else:
  41. rule = self.env['barcode.rule'].search([('name', '=', 'Beescoop Product Barcodes')])[0]
  42. size = 13 - len(rule.pattern)
  43. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  44. bc = ean[0:12] + str(self.env['barcode.nomenclature'].ean_checksum(ean))
  45. # Make sure there is no other active member with the same barcode
  46. while(self.search_count([('barcode', '=', bc)]) > 1):
  47. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  48. bc = ean[0:12] + str(self.env['barcode.nomenclature'].ean_checksum(ean))
  49. print 'barcode :', bc
  50. self.barcode = bc
  51. @api.one
  52. @api.depends('seller_ids', 'seller_ids.date_start')
  53. def _compute_main_seller_id(self):
  54. # Calcule le vendeur associé qui a la date de début la plus récente et plus petite qu’aujourd’hui
  55. sellers_ids = self._get_main_supplier_info() # self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  56. self.main_seller_id = sellers_ids and sellers_ids[0].name or False
  57. @api.one
  58. @api.depends('taxes_id', 'list_price', 'taxes_id.amount', 'taxes_id.tax_group_id', 'total_with_vat', 'display_weight', 'weight')
  59. def _get_total(self):
  60. consignes_group = self.env.ref('beesdoo_product.consignes_group_tax', raise_if_not_found=False)
  61. 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])
  62. 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])
  63. self.total_with_vat = self.list_price + tax_amount_sum
  64. if self.display_weight > 0:
  65. self.total_with_vat_by_unit = self.total_with_vat / self.weight
  66. @api.one
  67. @api.depends('weight', 'display_unit')
  68. def _get_display_weight(self):
  69. self.display_weight = self.weight * self.display_unit.factor
  70. @api.one
  71. @api.constrains('display_unit', 'default_reference_unit')
  72. def _unit_same_category(self):
  73. if self.display_unit.category_id != self.default_reference_unit.category_id:
  74. raise UserError(_('Reference Unit and Display Unit should belong to the same category'))
  75. @api.one
  76. @api.depends('seller_ids')
  77. def _compute_cost(self):
  78. suppliers = self._get_main_supplier_info()
  79. if(len(suppliers) > 0):
  80. self.suggested_price = (suppliers[0].price * self.uom_po_id.factor)* (1 + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100)
  81. class BeesdooProductLabel(models.Model):
  82. _name = "beesdoo.product.label"
  83. name = fields.Char()
  84. type = fields.Selection([('eco', 'Écologique'), ('local', 'Local'), ('fair', 'Équitable'), ('delivery', 'Distribution')])
  85. color_code = fields.Char()
  86. active = fields.Boolean(default=True)
  87. class BeesdooProductCategory(models.Model):
  88. _inherit = "product.category"
  89. profit_margin = fields.Float(default = '10.0', string = "Product Margin [%]")
  90. @api.one
  91. @api.constrains('profit_margin')
  92. def _check_margin(self):
  93. if (self.profit_margin < 0.0):
  94. raise UserError(_('Percentages for Profit Margin must > 0.'))
  95. class BeesdooProductSupplierInfo(models.Model):
  96. _inherit = "product.supplierinfo"
  97. price = fields.Float('exVAT Price')