From 6a9d67147cd92357dbcb64a32cc1a0fbc54b6174 Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Sat, 16 May 2020 20:51:21 +0200 Subject: [PATCH] [FIX] fix demo data and fix api.one to api.multi --- beesdoo_base/demo/cooperators.xml | 6 -- beesdoo_base/models/partner.py | 70 +++++++++++-- beesdoo_product/models/beesdoo_product.py | 116 ++++++++++++---------- 3 files changed, 126 insertions(+), 66 deletions(-) diff --git a/beesdoo_base/demo/cooperators.xml b/beesdoo_base/demo/cooperators.xml index 1d3bcd6..2795b16 100644 --- a/beesdoo_base/demo/cooperators.xml +++ b/beesdoo_base/demo/cooperators.xml @@ -9,7 +9,6 @@ Fernand Peso - worker_eater fernand_peso@demo.net @@ -22,7 +21,6 @@ Dupont Dupont - worker_eater d_dupont@demo.net @@ -35,7 +33,6 @@ Ronan Le Gall - worker_eater ronan_gall@demo.net @@ -48,7 +45,6 @@ Elouan Bees - worker_eater elouan_bees@demo.net @@ -61,7 +57,6 @@ Anne de Marchalo - worker_eater anne_marchalo@demo.net @@ -74,7 +69,6 @@ Jean Beaumont - worker_eater jean_beaumont@demo.net diff --git a/beesdoo_base/models/partner.py b/beesdoo_base/models/partner.py index 8196b02..e062164 100644 --- a/beesdoo_base/models/partner.py +++ b/beesdoo_base/models/partner.py @@ -1,3 +1,6 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + from odoo import _, api, fields, models from odoo.exceptions import ValidationError @@ -24,10 +27,23 @@ class Partner(models.Model): compute="_compute_bar_code", string="Parent Barcode", store=True ) member_card_ids = fields.One2many("member.card", "partner_id") + country_id = fields.Many2one( + required=True, default=lambda self: self.env.ref("base.be") + ) member_card_to_be_printed = fields.Boolean("Print BEES card?") last_printed = fields.Datetime("Last printed on") + cooperator_type = fields.Selection( + [ + ("share_a", "Share A"), + ("share_b", "Share B"), + ("share_c", "Share C"), + ], + store=True, + compute=None, + ) + @api.multi @api.depends( "parent_eater_id", "parent_eater_id.barcode", @@ -35,28 +51,61 @@ class Partner(models.Model): "member_card_ids", ) def _compute_bar_code(self): - for rec in self: - if rec.eater == "eater": - rec.parent_barcode = rec.parent_eater_id.barcode - elif rec.member_card_ids: - for c in rec.member_card_ids: + for partner in self: + if partner.eater == "eater": + partner.parent_barcode = partner.parent_eater_id.barcode + elif partner.member_card_ids: + for c in partner.member_card_ids: if c.valid: - rec.barcode = c.barcode + partner.barcode = c.barcode + + @api.multi + @api.constrains("child_eater_ids", "parent_eater_id") + def _check_number_of_eaters(self): + """The owner of an A share can have a maximum of two eaters but + the owner of a B share can have a maximum of three eaters. + """ + for partner in self: + # Get the default_code of the share for the current eater and his parent + share_type_code = partner.cooperator_type + parent_share_type_code = partner.parent_eater_id.cooperator_type + # Raise exception + if ( + share_type_code == "share_b" + or parent_share_type_code == "share_b" + ): + if ( + len(partner.child_eater_ids) > 3 + or len(partner.parent_eater_id.child_eater_ids) > 3 + ): + raise ValidationError( + _( + "You can only set three additional eaters per worker" + ) + ) + else: + if ( + len(partner.child_eater_ids) > 2 + or len(partner.parent_eater_id.child_eater_ids) > 2 + ): + raise ValidationError( + _("You can only set two additional eaters per worker") + ) @api.multi def write(self, values): - for rec in self: + for partner in self: if ( values.get("parent_eater_id") - and rec.parent_eater_id - and rec.parent_eater_id.id != values.get("parent_eater_id") + and partner.parent_eater_id + and partner.parent_eater_id.id != values.get("parent_eater_id") ): raise ValidationError( _( "You try to assign a eater to a worker but this eater " "is already assign to %s please remove it before " ) - % rec.parent_eater_id.name + % partner.parent_eater_id.name ) # replace many2many command when writing on child_eater_ids to just # remove the link @@ -66,6 +115,7 @@ class Partner(models.Model): command[0] = 3 return super(Partner, self).write(values) + @api.multi def _deactivate_active_cards(self): self.ensure_one() for card in self.member_card_ids.filtered("valid"): diff --git a/beesdoo_product/models/beesdoo_product.py b/beesdoo_product/models/beesdoo_product.py index e483f05..c460a9a 100644 --- a/beesdoo_product/models/beesdoo_product.py +++ b/beesdoo_product/models/beesdoo_product.py @@ -1,3 +1,6 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + import logging import uuid @@ -150,11 +153,13 @@ class BeesdooProduct(models.Model): @api.multi @api.depends("seller_ids", "seller_ids.date_start") def _compute_main_seller_id(self): - self.ensure_one() - # Calcule le vendeur associé qui a la date de début la plus récente - # et plus petite qu’aujourd’hui - sellers_ids = self._get_main_supplier_info() - self.main_seller_id = sellers_ids and sellers_ids[0].name or False + for product in self: + # Calcule le vendeur associé qui a la date de début la plus récente + # et plus petite qu’aujourd’hui + sellers_ids = product._get_main_supplier_info() + product.main_seller_id = ( + sellers_ids and sellers_ids[0].name or False + ) @api.multi @api.depends( @@ -166,57 +171,65 @@ class BeesdooProduct(models.Model): "weight", ) def _compute_total(self): - self.ensure_one() - consignes_group = self.env.ref( - "beesdoo_product.consignes_group_tax", raise_if_not_found=False - ) + for product in self: + consignes_group = self.env.ref( + "beesdoo_product.consignes_group_tax", raise_if_not_found=False + ) - taxes_included = set(self.taxes_id.mapped("price_include")) - if len(taxes_included) == 0: - self.total_with_vat = self.list_price - return True + taxes_included = set(product.taxes_id.mapped("price_include")) + if len(taxes_included) == 0: + product.total_with_vat = product.list_price + return True - elif len(taxes_included) > 1: - raise ValidationError( - _("Several tax strategies (price_include) defined for %s") - % self.name - ) + elif len(taxes_included) > 1: + raise ValidationError( + _("Several tax strategies (price_include) defined for %s") + % product.name + ) - elif taxes_included.pop(): - self.total_with_vat = self.list_price - self.total_deposit = sum( + elif taxes_included.pop(): + product.total_with_vat = product.list_price + product.total_deposit = sum( + [ + tax._compute_amount( + product.list_price, product.list_price + ) + for tax in product.taxes_id + if tax.tax_group_id == consignes_group + ] + ) + else: + tax_amount_sum = sum( + [ + tax._compute_amount( + product.list_price, product.list_price + ) + for tax in product.taxes_id + if tax.tax_group_id != consignes_group + ] + ) + product.total_with_vat = product.list_price + tax_amount_sum + + product.total_deposit = sum( [ - tax._compute_amount(self.list_price, self.list_price) - for tax in self.taxes_id + tax._compute_amount(product.list_price, product.list_price) + for tax in product.taxes_id if tax.tax_group_id == consignes_group ] ) - else: - tax_amount_sum = sum( - [ - tax._compute_amount(self.list_price, self.list_price) - for tax in self.taxes_id - if tax.tax_group_id != consignes_group - ] - ) - self.total_with_vat = self.list_price + tax_amount_sum - - 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 - ] - ) - if self.display_weight > 0: - self.total_with_vat_by_unit = self.total_with_vat / self.weight + if product.display_weight > 0: + product.total_with_vat_by_unit = ( + product.total_with_vat / product.weight + ) @api.multi @api.depends("weight", "display_unit") def _compute_display_weight(self): - self.ensure_one() - self.display_weight = self.weight * self.display_unit.factor + for product in self: + product.display_weight = ( + product.weight * product.display_unit.factor + ) @api.multi @api.constrains("display_unit", "default_reference_unit") @@ -236,12 +249,15 @@ class BeesdooProduct(models.Model): @api.multi @api.depends("seller_ids") def _compute_cost(self): - self.ensure_one() - suppliers = self._get_main_supplier_info() - if len(suppliers) > 0: - self.suggested_price = ( - suppliers[0].price * self.uom_po_id.factor - ) * (1 + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100) + for product in self: + suppliers = product._get_main_supplier_info() + if len(suppliers) > 0: + product.suggested_price = ( + suppliers[0].price * product.uom_po_id.factor + ) * ( + 1 + + suppliers[0].product_tmpl_id.categ_id.profit_margin / 100 + ) class BeesdooScaleCategory(models.Model):