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.

187 lines
8.4 KiB

  1. import uuid
  2. from odoo import models, fields, api
  3. from odoo.tools.translate import _
  4. from odoo.exceptions import UserError, ValidationError
  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', string='Main Seller', compute='_compute_main_seller_id', store=True)
  12. display_unit = fields.Many2one('uom.uom')
  13. default_reference_unit = fields.Many2one('uom.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. note = fields.Text('Comments')
  21. # S0023 : List_price = Price HTVA, so add a suggested price
  22. list_price = fields.Float(string='exVAT Price')
  23. suggested_price = fields.Float(string='Suggested exVAT Price', compute='_compute_cost', readOnly=True)
  24. deadline_for_sale = fields.Integer(string="Deadline for sale(days)")
  25. deadline_for_consumption = fields.Integer(string="Deadline for consumption(days)")
  26. ingredients = fields.Char(string="Ingredient")
  27. scale_label_info_1 = fields.Char(string="Scale lable info 1")
  28. scale_label_info_2 = fields.Char(string="Scale lable info 2")
  29. scale_sale_unit = fields.Char(compute="_get_scale_sale_uom", string="Scale sale unit", store=True)
  30. scale_category = fields.Many2one('beesdoo.scale.category', string="Scale Category")
  31. scale_category_code = fields.Integer(related='scale_category.code', string="Scale category code", readonly=True, store=True)
  32. @api.depends('uom_id','uom_id.category_id','uom_id.category_id.type')
  33. @api.multi
  34. def _get_scale_sale_uom(self):
  35. for product in self:
  36. if product.uom_id.category_id.type == 'unit':
  37. product.scale_sale_unit = 'F'
  38. elif product.uom_id.category_id.type == 'weight':
  39. product.scale_sale_unit = 'P'
  40. def _get_main_supplier_info(self):
  41. suppliers = self.seller_ids.sorted(
  42. key=lambda seller: seller.date_start,
  43. reverse=True)
  44. if suppliers:
  45. return suppliers[0]
  46. else:
  47. return suppliers
  48. @api.one
  49. def generate_barcode(self):
  50. if self.to_weight:
  51. seq_internal_code = self.env.ref('beesdoo_product.seq_ean_product_internal_ref')
  52. bc = ''
  53. if not self.default_code:
  54. rule = self.env['barcode.rule'].search([('name', '=', 'Price Barcodes (Computed Weight) 2 Decimals')])[0]
  55. default_code = seq_internal_code.next_by_id()
  56. while(self.search_count([('default_code', '=', default_code)]) > 1):
  57. default_code = seq_internal_code.next_by_id()
  58. self.default_code = default_code
  59. ean = '02' + self.default_code[0:5] + '000000'
  60. bc = ean[0:12] + str(self.env['barcode.nomenclature'].ean_checksum(ean))
  61. else:
  62. rule = self.env['barcode.rule'].search([('name', '=', 'Beescoop Product Barcodes')])[0]
  63. size = 13 - len(rule.pattern)
  64. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  65. bc = ean[0:12] + str(self.env['barcode.nomenclature'].ean_checksum(ean))
  66. # Make sure there is no other active member with the same barcode
  67. while(self.search_count([('barcode', '=', bc)]) > 1):
  68. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  69. bc = ean[0:12] + str(self.env['barcode.nomenclature'].ean_checksum(ean))
  70. print('barcode :', bc)
  71. self.barcode = bc
  72. @api.one
  73. @api.depends('seller_ids', 'seller_ids.date_start')
  74. def _compute_main_seller_id(self):
  75. # Calcule le vendeur associé qui a la date de début la plus récente et plus petite qu’aujourd’hui
  76. sellers_ids = self._get_main_supplier_info() # self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  77. self.main_seller_id = sellers_ids and sellers_ids[0].name or False
  78. @api.one
  79. @api.depends('taxes_id', 'list_price', 'taxes_id.amount',
  80. 'taxes_id.tax_group_id',
  81. 'display_weight', 'weight')
  82. def _get_total(self):
  83. consignes_group = self.env.ref('beesdoo_product.consignes_group_tax',
  84. raise_if_not_found=False)
  85. taxes_included = set(self.taxes_id.mapped('price_include'))
  86. if len(taxes_included) == 0:
  87. self.total_with_vat = self.list_price
  88. return True
  89. elif len(taxes_included) > 1:
  90. raise ValidationError('Several tax strategies (price_include) defined for %s' % self.name)
  91. elif taxes_included.pop():
  92. self.total_with_vat = self.list_price
  93. 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])
  94. else:
  95. tax_amount_sum = sum([tax._compute_amount(self.list_price, self.list_price)
  96. for tax in self.taxes_id
  97. if tax.tax_group_id != consignes_group])
  98. self.total_with_vat = self.list_price + tax_amount_sum
  99. self.total_deposit = sum([tax._compute_amount(self.list_price, self.list_price)
  100. for tax in self.taxes_id
  101. if tax.tax_group_id == consignes_group])
  102. if self.display_weight > 0:
  103. self.total_with_vat_by_unit = self.total_with_vat / self.weight
  104. @api.one
  105. @api.depends('weight', 'display_unit')
  106. def _get_display_weight(self):
  107. self.display_weight = self.weight * self.display_unit.factor
  108. @api.one
  109. @api.constrains('display_unit', 'default_reference_unit')
  110. def _unit_same_category(self):
  111. if self.display_unit.category_id != self.default_reference_unit.category_id:
  112. raise UserError(_('Reference Unit and Display Unit should belong to the same category'))
  113. @api.one
  114. @api.depends('seller_ids')
  115. def _compute_cost(self):
  116. suppliers = self._get_main_supplier_info()
  117. if(len(suppliers) > 0):
  118. self.suggested_price = (suppliers[0].price * self.uom_po_id.factor)* (1 + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100)
  119. class BeesdooScaleCategory(models.Model):
  120. _name = "beesdoo.scale.category"
  121. name = fields.Char(string="Scale name category")
  122. code = fields.Integer(string="Category code")
  123. _sql_constraints = [
  124. ('code_scale_categ_uniq', 'unique (code)', 'The code of the scale category must be unique !')
  125. ]
  126. class BeesdooProductLabel(models.Model):
  127. _name = "beesdoo.product.label"
  128. name = fields.Char()
  129. type = fields.Selection([('eco', 'Écologique'), ('local', 'Local'), ('fair', 'Équitable'), ('delivery', 'Distribution')])
  130. color_code = fields.Char()
  131. active = fields.Boolean(default=True)
  132. class BeesdooProductCategory(models.Model):
  133. _inherit = "product.category"
  134. profit_margin = fields.Float(default = '10.0', string = "Product Margin [%]")
  135. @api.one
  136. @api.constrains('profit_margin')
  137. def _check_margin(self):
  138. if (self.profit_margin < 0.0):
  139. raise UserError(_('Percentages for Profit Margin must > 0.'))
  140. class BeesdooProductSupplierInfo(models.Model):
  141. _inherit = "product.supplierinfo"
  142. price = fields.Float('exVAT Price')
  143. class BeesdooUOMCateg(models.Model):
  144. _inherit = 'uom.category'
  145. type = fields.Selection([('unit','Unit'),
  146. ('weight','Weight'),
  147. ('time','Time'),
  148. ('distance','Distance'),
  149. ('surface','Surface'),
  150. ('volume','Volume'),
  151. ('other','Other')],string='Category type',default='unit')