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.

204 lines
9.1 KiB

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