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.

306 lines
9.7 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
4 years ago
  1. import uuid
  2. from odoo import api, fields, models
  3. from odoo.exceptions import UserError, ValidationError
  4. from odoo.tools.translate import _
  5. import logging
  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.one
  89. def generate_barcode(self):
  90. if self.to_weight:
  91. seq_internal_code = self.env.ref(
  92. "beesdoo_product.seq_ean_product_internal_ref"
  93. )
  94. bc = ""
  95. if not self.default_code:
  96. rule = self.env["barcode.rule"].search(
  97. [
  98. (
  99. "name",
  100. "=",
  101. "Price Barcodes (Computed Weight) 2 Decimals",
  102. )
  103. ]
  104. )[0]
  105. default_code = seq_internal_code.next_by_id()
  106. while (
  107. self.search_count([("default_code", "=", default_code)])
  108. > 1
  109. ):
  110. default_code = seq_internal_code.next_by_id()
  111. self.default_code = default_code
  112. ean = "02" + self.default_code[0:5] + "000000"
  113. bc = ean[0:12] + str(
  114. self.env["barcode.nomenclature"].ean_checksum(ean)
  115. )
  116. else:
  117. rule = self.env["barcode.rule"].search(
  118. [("name", "=", "Beescoop Product Barcodes")]
  119. )[0]
  120. size = 13 - len(rule.pattern)
  121. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  122. bc = ean[0:12] + str(
  123. self.env["barcode.nomenclature"].ean_checksum(ean)
  124. )
  125. # Make sure there is no other active member with the same barcode
  126. while self.search_count([("barcode", "=", bc)]) > 1:
  127. ean = rule.pattern + str(uuid.uuid4().fields[-1])[:size]
  128. bc = ean[0:12] + str(
  129. self.env["barcode.nomenclature"].ean_checksum(ean)
  130. )
  131. _logger.info("barcode :", bc)
  132. self.barcode = bc
  133. @api.one
  134. @api.depends("seller_ids", "seller_ids.date_start")
  135. def _compute_main_seller_id(self):
  136. # Calcule le vendeur associé qui a la date de début la plus récente et plus petite qu’aujourd’hui
  137. sellers_ids = (
  138. self._get_main_supplier_info()
  139. ) # self.seller_ids.sorted(key=lambda seller: seller.date_start, reverse=True)
  140. self.main_seller_id = sellers_ids and sellers_ids[0].name or False
  141. @api.one
  142. @api.depends(
  143. "taxes_id",
  144. "list_price",
  145. "taxes_id.amount",
  146. "taxes_id.tax_group_id",
  147. "display_weight",
  148. "weight",
  149. )
  150. def _compute_total(self):
  151. consignes_group = self.env.ref(
  152. "beesdoo_product.consignes_group_tax", raise_if_not_found=False
  153. )
  154. taxes_included = set(self.taxes_id.mapped("price_include"))
  155. if len(taxes_included) == 0:
  156. self.total_with_vat = self.list_price
  157. return True
  158. elif len(taxes_included) > 1:
  159. raise ValidationError(
  160. _("Several tax strategies (price_include) defined for %s")
  161. % self.name
  162. )
  163. elif taxes_included.pop():
  164. self.total_with_vat = self.list_price
  165. self.total_deposit = sum(
  166. [
  167. tax._compute_amount(self.list_price, self.list_price)
  168. for tax in self.taxes_id
  169. if tax.tax_group_id == consignes_group
  170. ]
  171. )
  172. else:
  173. tax_amount_sum = sum(
  174. [
  175. tax._compute_amount(self.list_price, self.list_price)
  176. for tax in self.taxes_id
  177. if tax.tax_group_id != consignes_group
  178. ]
  179. )
  180. self.total_with_vat = self.list_price + tax_amount_sum
  181. self.total_deposit = sum(
  182. [
  183. tax._compute_amount(self.list_price, self.list_price)
  184. for tax in self.taxes_id
  185. if tax.tax_group_id == consignes_group
  186. ]
  187. )
  188. if self.display_weight > 0:
  189. self.total_with_vat_by_unit = self.total_with_vat / self.weight
  190. @api.one
  191. @api.depends("weight", "display_unit")
  192. def _compute_display_weight(self):
  193. self.display_weight = self.weight * self.display_unit.factor
  194. @api.one
  195. @api.constrains("display_unit", "default_reference_unit")
  196. def _unit_same_category(self):
  197. if (
  198. self.display_unit.category_id
  199. != self.default_reference_unit.category_id
  200. ):
  201. raise UserError(
  202. _(
  203. "Reference Unit and Display Unit should belong to the same category"
  204. )
  205. )
  206. @api.one
  207. @api.depends("seller_ids")
  208. def _compute_cost(self):
  209. suppliers = self._get_main_supplier_info()
  210. if len(suppliers) > 0:
  211. self.suggested_price = (
  212. suppliers[0].price * self.uom_po_id.factor
  213. ) * (1 + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100)
  214. class BeesdooScaleCategory(models.Model):
  215. _name = "beesdoo.scale.category"
  216. _description = "beesdoo.scale.category"
  217. name = fields.Char(string="Scale name category")
  218. code = fields.Integer(string="Category code")
  219. _sql_constraints = [
  220. (
  221. "code_scale_categ_uniq",
  222. "unique (code)",
  223. "The code of the scale category must be unique !",
  224. )
  225. ]
  226. class BeesdooProductLabel(models.Model):
  227. _name = "beesdoo.product.label"
  228. _description = "beesdoo.product.label"
  229. name = fields.Char()
  230. type = fields.Selection(
  231. [
  232. ("eco", "Écologique"),
  233. ("local", "Local"),
  234. ("fair", "Équitable"),
  235. ("delivery", "Distribution"),
  236. ]
  237. )
  238. color_code = fields.Char()
  239. active = fields.Boolean(default=True)
  240. class BeesdooProductCategory(models.Model):
  241. _inherit = "product.category"
  242. profit_margin = fields.Float(default="10.0", string="Product Margin [%]")
  243. @api.one
  244. @api.constrains("profit_margin")
  245. def _check_margin(self):
  246. if self.profit_margin < 0.0:
  247. raise UserError(_("Percentages for Profit Margin must > 0."))
  248. class BeesdooProductSupplierInfo(models.Model):
  249. _inherit = "product.supplierinfo"
  250. price = fields.Float("exVAT Price")
  251. class BeesdooUOMCateg(models.Model):
  252. _inherit = "uom.category"
  253. type = fields.Selection(
  254. [
  255. ("unit", "Unit"),
  256. ("weight", "Weight"),
  257. ("time", "Time"),
  258. ("distance", "Distance"),
  259. ("surface", "Surface"),
  260. ("volume", "Volume"),
  261. ("other", "Other"),
  262. ],
  263. string="Category type",
  264. default="unit",
  265. )