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.

314 lines
9.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import logging
  2. import uuid
  3. from odoo import api, fields, models
  4. from odoo.exceptions import UserError, ValidationError
  5. from odoo.tools.translate import _
  6. _logger = logging.getLogger(__name__)
  7. class BeesdooProduct(models.Model):
  8. _inherit = "product.template"
  9. eco_label = fields.Many2one(
  10. "beesdoo.product.label", domain=[("type", "=", "eco")]
  11. )
  12. local_label = fields.Many2one(
  13. "beesdoo.product.label", domain=[("type", "=", "local")]
  14. )
  15. fair_label = fields.Many2one(
  16. "beesdoo.product.label", domain=[("type", "=", "fair")]
  17. )
  18. origin_label = fields.Many2one(
  19. "beesdoo.product.label", domain=[("type", "=", "delivery")]
  20. )
  21. main_seller_id = fields.Many2one(
  22. "res.partner",
  23. string="Main Seller",
  24. compute="_compute_main_seller_id",
  25. store=True,
  26. )
  27. display_unit = fields.Many2one("uom.uom")
  28. default_reference_unit = fields.Many2one("uom.uom")
  29. display_weight = fields.Float(
  30. compute="_compute_display_weight", store=True
  31. )
  32. total_with_vat = fields.Float(
  33. compute="_compute_total",
  34. store=True,
  35. string="Total Sales Price with VAT",
  36. )
  37. total_with_vat_by_unit = fields.Float(
  38. compute="_compute_total",
  39. store=True,
  40. string="Total Sales Price with VAT by Reference Unit",
  41. )
  42. total_deposit = fields.Float(
  43. compute="_compute_total", store=True, string="Deposit Price"
  44. )
  45. label_to_be_printed = fields.Boolean("Print label?")
  46. label_last_printed = fields.Datetime("Label last printed on")
  47. note = fields.Text("Comments")
  48. # S0023 : List_price = Price HTVA, so add a suggested price
  49. list_price = fields.Float(string="exVAT Price")
  50. suggested_price = fields.Float(
  51. string="Suggested exVAT Price", compute="_compute_cost", readOnly=True
  52. )
  53. deadline_for_sale = fields.Integer(string="Deadline for sale(days)")
  54. deadline_for_consumption = fields.Integer(
  55. string="Deadline for consumption(days)"
  56. )
  57. ingredients = fields.Char(string="Ingredient")
  58. scale_label_info_1 = fields.Char(string="Scale lable info 1")
  59. scale_label_info_2 = fields.Char(string="Scale lable info 2")
  60. scale_sale_unit = fields.Char(
  61. compute="_compute_scale_sale_uom", string="Scale sale unit", store=True
  62. )
  63. scale_category = fields.Many2one(
  64. "beesdoo.scale.category", string="Scale Category"
  65. )
  66. scale_category_code = fields.Integer(
  67. related="scale_category.code",
  68. string="Scale category code",
  69. readonly=True,
  70. store=True,
  71. )
  72. @api.depends("uom_id", "uom_id.category_id", "uom_id.category_id.type")
  73. @api.multi
  74. def _compute_scale_sale_uom(self):
  75. for product in self:
  76. if product.uom_id.category_id.type == "unit":
  77. product.scale_sale_unit = "F"
  78. elif product.uom_id.category_id.type == "weight":
  79. product.scale_sale_unit = "P"
  80. def _get_main_supplier_info(self):
  81. suppliers = self.seller_ids.sorted(
  82. key=lambda seller: seller.date_start, reverse=True
  83. )
  84. if suppliers:
  85. return suppliers[0]
  86. else:
  87. return suppliers
  88. @api.multi
  89. def generate_barcode(self):
  90. self.ensure_one()
  91. if self.to_weight:
  92. seq_internal_code = self.env.ref(
  93. "beesdoo_product.seq_ean_product_internal_ref"
  94. )
  95. bc = ""
  96. if not self.default_code:
  97. rule = self.env["barcode.rule"].search(
  98. [
  99. (
  100. "name",
  101. "=",
  102. "Price Barcodes (Computed Weight) 2 Decimals",
  103. )
  104. ]
  105. )[0]
  106. default_code = seq_internal_code.next_by_id()
  107. while (
  108. self.search_count([("default_code", "=", default_code)])
  109. > 1
  110. ):
  111. default_code = seq_internal_code.next_by_id()
  112. self.default_code = default_code
  113. ean = "02" + self.default_code[0:5] + "000000"
  114. bc = ean[0:12] + str(
  115. self.env["barcode.nomenclature"].ean_checksum(ean)
  116. )
  117. else:
  118. rule = self.env["barcode.rule"].search(
  119. [("name", "=", "Beescoop Product Barcodes")]
  120. )[0]
  121. size = 13 - len(rule.pattern)
  122. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  123. bc = ean[0:12] + str(
  124. self.env["barcode.nomenclature"].ean_checksum(ean)
  125. )
  126. # Make sure there is no other active member with the same barcode
  127. while self.search_count([("barcode", "=", bc)]) > 1:
  128. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  129. bc = ean[0:12] + str(
  130. self.env["barcode.nomenclature"].ean_checksum(ean)
  131. )
  132. _logger.info("barcode :", bc)
  133. self.barcode = bc
  134. @api.multi
  135. @api.depends("seller_ids", "seller_ids.date_start")
  136. def _compute_main_seller_id(self):
  137. self.ensure_one()
  138. # Calcule le vendeur associé qui a la date de début la plus récente
  139. # et plus petite qu’aujourd’hui
  140. sellers_ids = self._get_main_supplier_info()
  141. self.main_seller_id = sellers_ids and sellers_ids[0].name or False
  142. @api.multi
  143. @api.depends(
  144. "taxes_id",
  145. "list_price",
  146. "taxes_id.amount",
  147. "taxes_id.tax_group_id",
  148. "display_weight",
  149. "weight",
  150. )
  151. def _compute_total(self):
  152. self.ensure_one()
  153. consignes_group = self.env.ref(
  154. "beesdoo_product.consignes_group_tax", raise_if_not_found=False
  155. )
  156. taxes_included = set(self.taxes_id.mapped("price_include"))
  157. if len(taxes_included) == 0:
  158. self.total_with_vat = self.list_price
  159. return True
  160. elif len(taxes_included) > 1:
  161. raise ValidationError(
  162. _("Several tax strategies (price_include) defined for %s")
  163. % self.name
  164. )
  165. elif taxes_included.pop():
  166. self.total_with_vat = self.list_price
  167. self.total_deposit = sum(
  168. [
  169. tax._compute_amount(self.list_price, self.list_price)
  170. for tax in self.taxes_id
  171. if tax.tax_group_id == consignes_group
  172. ]
  173. )
  174. else:
  175. tax_amount_sum = sum(
  176. [
  177. tax._compute_amount(self.list_price, self.list_price)
  178. for tax in self.taxes_id
  179. if tax.tax_group_id != consignes_group
  180. ]
  181. )
  182. self.total_with_vat = self.list_price + tax_amount_sum
  183. self.total_deposit = sum(
  184. [
  185. tax._compute_amount(self.list_price, self.list_price)
  186. for tax in self.taxes_id
  187. if tax.tax_group_id == consignes_group
  188. ]
  189. )
  190. if self.display_weight > 0:
  191. self.total_with_vat_by_unit = self.total_with_vat / self.weight
  192. @api.multi
  193. @api.depends("weight", "display_unit")
  194. def _compute_display_weight(self):
  195. self.ensure_one()
  196. self.display_weight = self.weight * self.display_unit.factor
  197. @api.multi
  198. @api.constrains("display_unit", "default_reference_unit")
  199. def _unit_same_category(self):
  200. for product in self:
  201. if (
  202. product.display_unit.category_id
  203. != product.default_reference_unit.category_id
  204. ):
  205. raise UserError(
  206. _(
  207. "Reference Unit and Display Unit should belong to the "
  208. "same category "
  209. )
  210. )
  211. @api.multi
  212. @api.depends("seller_ids")
  213. def _compute_cost(self):
  214. self.ensure_one()
  215. suppliers = self._get_main_supplier_info()
  216. if len(suppliers) > 0:
  217. self.suggested_price = (
  218. suppliers[0].price * self.uom_po_id.factor
  219. ) * (1 + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100)
  220. class BeesdooScaleCategory(models.Model):
  221. _name = "beesdoo.scale.category"
  222. _description = "beesdoo.scale.category"
  223. name = fields.Char(string="Scale name category")
  224. code = fields.Integer(string="Category code")
  225. _sql_constraints = [
  226. (
  227. "code_scale_categ_uniq",
  228. "unique (code)",
  229. "The code of the scale category must be unique !",
  230. )
  231. ]
  232. class BeesdooProductLabel(models.Model):
  233. _name = "beesdoo.product.label"
  234. _description = "beesdoo.product.label"
  235. name = fields.Char()
  236. type = fields.Selection(
  237. [
  238. ("eco", "Écologique"),
  239. ("local", "Local"),
  240. ("fair", "Équitable"),
  241. ("delivery", "Distribution"),
  242. ]
  243. )
  244. color_code = fields.Char()
  245. active = fields.Boolean(default=True)
  246. class BeesdooProductCategory(models.Model):
  247. _inherit = "product.category"
  248. profit_margin = fields.Float(default="10.0", string="Product Margin [%]")
  249. @api.multi
  250. @api.constrains("profit_margin")
  251. def _check_margin(self):
  252. for product in self:
  253. if product.profit_margin < 0.0:
  254. raise UserError(_("Percentages for Profit Margin must > 0."))
  255. class BeesdooProductSupplierInfo(models.Model):
  256. _inherit = "product.supplierinfo"
  257. price = fields.Float("exVAT Price")
  258. class BeesdooUOMCateg(models.Model):
  259. _inherit = "uom.category"
  260. type = fields.Selection(
  261. [
  262. ("unit", "Unit"),
  263. ("weight", "Weight"),
  264. ("time", "Time"),
  265. ("distance", "Distance"),
  266. ("surface", "Surface"),
  267. ("volume", "Volume"),
  268. ("other", "Other"),
  269. ],
  270. string="Category type",
  271. default="unit",
  272. )