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.

351 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
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. suggested_price = fields.Float(
  60. string="Suggested Price", compute="_compute_cost", readOnly=True,
  61. help="""
  62. This field computes a suggested price based on the 'Product Margin'
  63. field on Partners (Vendors), if it's set, or otherwise on the 'Product
  64. Margin' field in Product Categories (which has a default value).
  65. """
  66. )
  67. deadline_for_sale = fields.Integer(string="Deadline for sale(days)")
  68. deadline_for_consumption = fields.Integer(
  69. string="Deadline for consumption(days)"
  70. )
  71. ingredients = fields.Char(string="Ingredient")
  72. scale_label_info_1 = fields.Char(string="Scale lable info 1")
  73. scale_label_info_2 = fields.Char(string="Scale lable info 2")
  74. scale_sale_unit = fields.Char(
  75. compute="_compute_scale_sale_uom", string="Scale sale unit", store=True
  76. )
  77. scale_category = fields.Many2one(
  78. "beesdoo.scale.category", string="Scale Category"
  79. )
  80. scale_category_code = fields.Integer(
  81. related="scale_category.code",
  82. string="Scale category code",
  83. readonly=True,
  84. store=True,
  85. )
  86. @api.depends("uom_id", "uom_id.category_id", "uom_id.category_id.type")
  87. @api.multi
  88. def _compute_scale_sale_uom(self):
  89. for product in self:
  90. if product.uom_id.category_id.type == "unit":
  91. product.scale_sale_unit = "F"
  92. elif product.uom_id.category_id.type == "weight":
  93. product.scale_sale_unit = "P"
  94. def _get_main_supplier_info(self):
  95. suppliers = self.seller_ids.sorted(
  96. key=lambda seller: seller.date_start, reverse=True
  97. )
  98. if suppliers:
  99. return suppliers[0]
  100. else:
  101. return suppliers
  102. @api.multi
  103. def generate_barcode(self):
  104. self.ensure_one()
  105. if self.to_weight:
  106. seq_internal_code = self.env.ref(
  107. "beesdoo_product.seq_ean_product_internal_ref"
  108. )
  109. bc = ""
  110. if not self.default_code:
  111. rule = self.env["barcode.rule"].search(
  112. [
  113. (
  114. "name",
  115. "=",
  116. "Price Barcodes (Computed Weight) 2 Decimals",
  117. )
  118. ]
  119. )[0]
  120. default_code = seq_internal_code.next_by_id()
  121. while (
  122. self.search_count([("default_code", "=", default_code)])
  123. > 1
  124. ):
  125. default_code = seq_internal_code.next_by_id()
  126. self.default_code = default_code
  127. ean = "02" + self.default_code[0:5] + "000000"
  128. bc = ean[0:12] + str(
  129. self.env["barcode.nomenclature"].ean_checksum(ean)
  130. )
  131. else:
  132. rule = self.env["barcode.rule"].search(
  133. [("name", "=", "Beescoop Product Barcodes")]
  134. )[0]
  135. size = 13 - len(rule.pattern)
  136. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  137. bc = ean[0:12] + str(
  138. self.env["barcode.nomenclature"].ean_checksum(ean)
  139. )
  140. # Make sure there is no other active member with the same barcode
  141. while self.search_count([("barcode", "=", bc)]) > 1:
  142. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  143. bc = ean[0:12] + str(
  144. self.env["barcode.nomenclature"].ean_checksum(ean)
  145. )
  146. _logger.info("barcode :", bc)
  147. self.barcode = bc
  148. @api.multi
  149. @api.depends("seller_ids", "seller_ids.date_start")
  150. def _compute_main_seller_id(self):
  151. for product in self:
  152. # Calcule le vendeur associé qui a la date de début la plus récente
  153. # et plus petite qu’aujourd’hui
  154. sellers_ids = product._get_main_supplier_info()
  155. product.main_seller_id = (
  156. sellers_ids and sellers_ids[0].name or False
  157. )
  158. @api.multi
  159. @api.depends(
  160. "taxes_id",
  161. "list_price",
  162. "taxes_id.amount",
  163. "taxes_id.tax_group_id",
  164. "display_weight",
  165. "weight",
  166. )
  167. def _compute_total(self):
  168. for product in self:
  169. consignes_group = self.env.ref(
  170. "beesdoo_product.consignes_group_tax", raise_if_not_found=False
  171. )
  172. taxes_included = set(product.taxes_id.mapped("price_include"))
  173. if len(taxes_included) == 0:
  174. product.total_with_vat = product.list_price
  175. return True
  176. elif len(taxes_included) > 1:
  177. raise ValidationError(
  178. _("Several tax strategies (price_include) defined for %s")
  179. % product.name
  180. )
  181. elif taxes_included.pop():
  182. product.total_with_vat = product.list_price
  183. product.total_deposit = sum(
  184. [
  185. tax._compute_amount(
  186. product.list_price, product.list_price
  187. )
  188. for tax in product.taxes_id
  189. if tax.tax_group_id == consignes_group
  190. ]
  191. )
  192. else:
  193. tax_amount_sum = sum(
  194. [
  195. tax._compute_amount(
  196. product.list_price, product.list_price
  197. )
  198. for tax in product.taxes_id
  199. if tax.tax_group_id != consignes_group
  200. ]
  201. )
  202. product.total_with_vat = product.list_price + tax_amount_sum
  203. product.total_deposit = sum(
  204. [
  205. tax._compute_amount(product.list_price, product.list_price)
  206. for tax in product.taxes_id
  207. if tax.tax_group_id == consignes_group
  208. ]
  209. )
  210. if product.display_weight > 0:
  211. product.total_with_vat_by_unit = (
  212. product.total_with_vat / product.weight
  213. )
  214. @api.multi
  215. @api.depends("weight", "display_unit")
  216. def _compute_display_weight(self):
  217. for product in self:
  218. product.display_weight = (
  219. product.weight * product.display_unit.factor
  220. )
  221. @api.multi
  222. @api.constrains("display_unit", "default_reference_unit")
  223. def _unit_same_category(self):
  224. for product in self:
  225. if (
  226. product.display_unit.category_id
  227. != product.default_reference_unit.category_id
  228. ):
  229. raise UserError(
  230. _(
  231. "Reference Unit and Display Unit should belong to the "
  232. "same category "
  233. )
  234. )
  235. @api.multi
  236. @api.depends("seller_ids")
  237. def _compute_cost(self):
  238. for product in self:
  239. suppliers = product._get_main_supplier_info()
  240. if len(suppliers) > 0:
  241. price = suppliers[0].price
  242. profit_margin_supplier = suppliers[0].name.profit_margin
  243. profit_margin_product_category = suppliers[
  244. 0
  245. ].product_tmpl_id.categ_id.profit_margin
  246. profit_margin = (
  247. profit_margin_supplier or profit_margin_product_category
  248. )
  249. product.suggested_price = (
  250. price * product.uom_po_id.factor
  251. ) * (1 + profit_margin / 100)
  252. class BeesdooScaleCategory(models.Model):
  253. _name = "beesdoo.scale.category"
  254. _description = "beesdoo.scale.category"
  255. name = fields.Char(string="Scale name category")
  256. code = fields.Integer(string="Category code")
  257. _sql_constraints = [
  258. (
  259. "code_scale_categ_uniq",
  260. "unique (code)",
  261. "The code of the scale category must be unique !",
  262. )
  263. ]
  264. class BeesdooProductLabel(models.Model):
  265. _name = "beesdoo.product.label"
  266. _description = "beesdoo.product.label"
  267. name = fields.Char()
  268. type = fields.Selection(
  269. [
  270. ("eco", "Écologique"),
  271. ("local", "Local"),
  272. ("fair", "Équitable"),
  273. ("delivery", "Distribution"),
  274. ]
  275. )
  276. color_code = fields.Char()
  277. active = fields.Boolean(default=True)
  278. class BeesdooProductCategory(models.Model):
  279. _inherit = "product.category"
  280. profit_margin = fields.Float(default="10.0", string="Product Margin [%]")
  281. @api.multi
  282. @api.constrains("profit_margin")
  283. def _check_margin(self):
  284. for product in self:
  285. if product.profit_margin < 0.0:
  286. raise UserError(_("Percentages for Profit Margin must >= 0."))
  287. class BeesdooProductSupplierInfo(models.Model):
  288. _inherit = "product.supplierinfo"
  289. price = fields.Float("Price")
  290. class BeesdooUOMCateg(models.Model):
  291. _inherit = "uom.category"
  292. type = fields.Selection(
  293. [
  294. ("unit", "Unit"),
  295. ("weight", "Weight"),
  296. ("time", "Time"),
  297. ("distance", "Distance"),
  298. ("surface", "Surface"),
  299. ("volume", "Volume"),
  300. ("other", "Other"),
  301. ],
  302. string="Category type",
  303. default="unit",
  304. )