Skip to content

Commit

Permalink
[IMP] connector_oxigesti: logic for deprecated field of pricelist item
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC013 committed Jan 5, 2024
1 parent d77e8af commit 0965df8
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import export_mapper
from . import binder
from . import binding
from . import listener
32 changes: 31 additions & 1 deletion connector_oxigesti/models/product_pricelist_item/binding.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright NuoBiT Solutions - Eric Antones <[email protected]>
# Copyright NuoBiT Solutions - Kilian Niubo <[email protected]>
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ProductPricelistItem(models.Model):
Expand All @@ -14,6 +16,32 @@ class ProductPricelistItem(models.Model):
string="Oxigesti Bindings",
)

@api.constrains("compute_price", "applied_on", "product_tmpl_id")
def _check_binding(self):
if self.oxigesti_bind_ids:
partners = (
self.env["res.partner"]
.search(
[
("company_id", "=?", self.company_id.id),
]
)
.filtered(
lambda x: x.property_product_pricelist.id == self.pricelist_id.id
)
)
if partners:
raise ValidationError(

Check warning on line 34 in connector_oxigesti/models/product_pricelist_item/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/binding.py#L34

Added line #L34 was not covered by tests
_(
"You can't change the product, the price calculation "
"method or the applied on field of a pricelist item "
"because they have been exported the product prices by "
"customer to Oxigesti.\nIf you need to change any of "
"these fields, you can delete the pricelist item and "
"create a new one."
)
)


class ProductPricelistItemBinding(models.Model):
_name = "oxigesti.product.pricelist.item"
Expand Down Expand Up @@ -44,6 +72,8 @@ class ProductPricelistItemBinding(models.Model):
),
]

deprecated = fields.Boolean()

@api.model
def export_data(self, backend, since_date):
domain = [("company_id", "in", (backend.company_id.id, False))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ def Codigo_Mutua(self, record):

@mapping
def Deprecated(self, record):
if record.deprecated or not record.active or not record.product_tmpl_id.active:
return {"Deprecated": 1}

Check warning on line 67 in connector_oxigesti/models/product_pricelist_item/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/export_mapper.py#L67

Added line #L67 was not covered by tests
return {"Deprecated": 0}
95 changes: 77 additions & 18 deletions connector_oxigesti/models/product_pricelist_item/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)


from odoo.addons.component.core import Component
from odoo.addons.component.core import AbstractComponent, Component


class ProductPricelistItemBatchExporter(Component):
class ProductPricelistItemBatchExporter(AbstractComponent):
"""Export the Oxigesti Product prices.
For every product in the list, a delayed job is created.
"""

_name = "oxigesti.product.pricelist.item.delayed.batch.exporter"
_inherit = "oxigesti.delayed.batch.exporter"
_apply_on = "oxigesti.product.pricelist.item"
_name = "oxigesti.product.pricelist.item.batch.exporter"

def run(self, domain=None):
def run_common(self, domain=None):
if not domain:
domain = []
# Run the batch synchronization
Expand All @@ -39,20 +37,29 @@ def run(self, domain=None):
binder = self.binder_for(self.model._name)
for p in self.env["res.partner"].search(domain):
partner_external_id = partner_binder.to_external(p, wrap=True)
for pl in p.property_product_pricelist.item_ids.filtered(
lambda x: (
not since_date
or x.write_date > since_date
or p.write_date > since_date
pli_deprecated_id = self.env.context.get("pricelist_item_unlink", False)

Check warning on line 40 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L40

Added line #L40 was not covered by tests
if pli_deprecated_id:
pricelist_items = (

Check warning on line 42 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L42

Added line #L42 was not covered by tests
self.env["product.pricelist.item"]
.browse(pli_deprecated_id)
.exists()
)
and p.customer_rank >= p.supplier_rank
and x.applied_on == "1_product"
and x.compute_price == "fixed"
and (
not x.sudo().product_tmpl_id.company_id
or x.sudo().product_tmpl_id.company_id == p.company_id
else:
pricelist_items = p.property_product_pricelist.item_ids.filtered(
lambda x: (
not since_date
or x.write_date > since_date
or p.write_date > since_date
)
and p.customer_rank >= p.supplier_rank
and x.applied_on == "1_product"
and x.compute_price == "fixed"
and (
not x.sudo().product_tmpl_id.company_id
or x.sudo().product_tmpl_id.company_id == p.company_id
)
)
):
for pl in pricelist_items:
if pl.product_tmpl_id.default_code and partner_external_id:
external_id = [
pl.product_tmpl_id.default_code,
Expand All @@ -72,7 +79,59 @@ def run(self, domain=None):
"odoo_partner_id": p.id,
},
)
binding.deprecated = bool(pli_deprecated_id)

Check warning on line 82 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L82

Added line #L82 was not covered by tests
self._export_record(binding)
if pricelist_items and not pli_deprecated_id:
oxigesti_pricelist_item_deprecated = self.env[

Check warning on line 85 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L85

Added line #L85 was not covered by tests
"oxigesti.product.pricelist.item"
].search(
[
("odoo_partner_id", "=", p.id),
("odoo_id", "not in", pricelist_items.ids),
("company_id", "=", p.company_id.id),
]
)
for pl in oxigesti_pricelist_item_deprecated:
binding = binder.wrap_binding(

Check warning on line 95 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L95

Added line #L95 was not covered by tests
pl.odoo_id,
binding_extra_vals={
"odoo_partner_id": p.id,
},
)
binding.deprecated = True
self._export_record(binding)

Check warning on line 102 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L101-L102

Added lines #L101 - L102 were not covered by tests


class ProductPricelistItemDelayedBatchExporter(Component):
"""Export the Oxigesti Product prices.
For every product in the list, a delayed job is created.
"""

_name = "oxigesti.product.pricelist.item.delayed.batch.exporter"
_inherit = [
"oxigesti.delayed.batch.exporter",
"oxigesti.product.pricelist.item.batch.exporter",
]

def run(self, domain=None):
super().run_common(domain=domain)

Check warning on line 118 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L118

Added line #L118 was not covered by tests


class ProductPricelistItemDirectBatchExporter(Component):
"""Export the Oxigesti Product prices.
For every product in the list, a direct job is created.
"""

_name = "oxigesti.product.pricelist.item.direct.batch.exporter"
_inherit = [
"oxigesti.direct.batch.exporter",
"oxigesti.product.pricelist.item.batch.exporter",
]

def run(self, domain=None):
super().run_common(domain=domain)

Check warning on line 134 in connector_oxigesti/models/product_pricelist_item/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/exporter.py#L134

Added line #L134 was not covered by tests


class ProductPricelistItemExporter(Component):
Expand Down
23 changes: 23 additions & 0 deletions connector_oxigesti/models/product_pricelist_item/listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.component.core import Component


class ProductPricelistItemListener(Component):
_name = "oxigesti.product.pricelist.item.listener"
_inherit = "oxigesti.event.listener"

_apply_on = "product.pricelist.item"

def on_record_unlink(self, relation):
bindings = relation.oxigesti_bind_ids

Check warning on line 14 in connector_oxigesti/models/product_pricelist_item/listener.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/listener.py#L14

Added line #L14 was not covered by tests
for backend in bindings.with_context(
pricelist_item_unlink=relation.id
).backend_id:
partners = bindings.filtered(
lambda x: x.backend_id == backend
).odoo_partner_id
with backend.work_on(bindings._name) as work:
exporter = work.component(usage="direct.batch.exporter")
exporter.run([("id", "=", partners.ids)])

Check warning on line 23 in connector_oxigesti/models/product_pricelist_item/listener.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/product_pricelist_item/listener.py#L21-L23

Added lines #L21 - L23 were not covered by tests

0 comments on commit 0965df8

Please sign in to comment.