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.

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