diff --git a/joint_buying_product/__manifest__.py b/joint_buying_product/__manifest__.py index d4bc7aac..b1c3322b 100644 --- a/joint_buying_product/__manifest__.py +++ b/joint_buying_product/__manifest__.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { "name": "Joint Buying - Products", - "version": "12.0.5.0.1", + "version": "12.0.6.0.0", "category": "GRAP - Logistics", "author": "GRAP", "website": "https://github.com/grap/odoo-addons-logistics", diff --git a/joint_buying_product/migrations/12.0.6.0.0/post-migration.py b/joint_buying_product/migrations/12.0.6.0.0/post-migration.py new file mode 100644 index 00000000..c7e94ae6 --- /dev/null +++ b/joint_buying_product/migrations/12.0.6.0.0/post-migration.py @@ -0,0 +1,58 @@ +# Copyright (C) 2024-Today: GRAP (http://www.grap.coop) +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import logging + +from openupgradelib import openupgrade + +_logger = logging.getLogger(__name__) + + +@openupgrade.migrate() +def migrate(env, version): + JointBuyingOrder = env["joint.buying.purchase.order"] + + # Get null orders deposited or closed + # that still have a transport request related + openupgrade.logged_query( + env.cr, + """ + SELECT po.id + FROM joint_buying_transport_request tr + INNER JOIN joint_buying_purchase_order po + ON po.id = tr.order_id + WHERE po.state IN ('deposited', 'closed') + AND po.amount_untaxed = 0.0; + """, + ) + + order_ids = [x[0] for x in env.cr.fetchall()] + + _logger.info(f"Unlink Transport Requests for orders {order_ids} ...") + JointBuyingOrder.browse(order_ids)._hook_state_changed() + + # Get NOT null recent orders + # that don't have a transport request related + openupgrade.logged_query( + env.cr, + """ + SELECT + po.id, + po.create_date, + po.state, + po.amount_untaxed + FROM joint_buying_purchase_order po + WHERE po.id not in ( + SELECT order_id from joint_buying_transport_request + WHERE order_id is not null + ) + AND po.deposit_partner_id != po.delivery_partner_id + AND po.amount_untaxed > 0.0 AND po.deposit_date > '2024-01-01'; + """, + ) + + order_ids = [x[0] for x in env.cr.fetchall()] + + _logger.info(f"Create Transport Requests for orders {order_ids} ...") + JointBuyingOrder.browse(order_ids)._hook_state_changed() diff --git a/joint_buying_product/models/joint_buying_purchase_order.py b/joint_buying_product/models/joint_buying_purchase_order.py index 7ac62091..737acb72 100644 --- a/joint_buying_product/models/joint_buying_purchase_order.py +++ b/joint_buying_product/models/joint_buying_purchase_order.py @@ -175,6 +175,7 @@ class JointBuyingPurchaseOrder(models.Model): amount_untaxed = fields.Float( string="Total Untaxed Amount", compute="_compute_amount", + track_visibility=True, store=True, digits=dp.get_precision("Product Price"), ) diff --git a/joint_buying_product/models/joint_buying_purchase_order_line.py b/joint_buying_product/models/joint_buying_purchase_order_line.py index f6a7f918..4dc6415e 100644 --- a/joint_buying_product/models/joint_buying_purchase_order_line.py +++ b/joint_buying_product/models/joint_buying_purchase_order_line.py @@ -343,3 +343,13 @@ def _get_report_tour_data(self): "" ), } + + def write(self, vals): + res = super().write(vals) + # if the related orders are closed (or deposited), and if user changed the quantity + # lately. (for exemple, view joint.buying.wizard.update.order.grouped), we should + # ensure that transport request are correctly created. (or deleted) + self.mapped("order_id").filtered( + lambda x: x.state in ["closed", "deposited"] + )._hook_state_changed() + return res diff --git a/joint_buying_product/tests/__init__.py b/joint_buying_product/tests/__init__.py index 4924ff7c..3aa9227c 100644 --- a/joint_buying_product/tests/__init__.py +++ b/joint_buying_product/tests/__init__.py @@ -8,3 +8,4 @@ from . import test_tour_report from . import test_request_invalidate from . import test_joint_buying_transport_request_compute +from . import test_joint_buying_wizard_update_order_grouped diff --git a/joint_buying_product/tests/test_abstract.py b/joint_buying_product/tests/test_abstract.py index 6ff8e2af..5affded5 100644 --- a/joint_buying_product/tests/test_abstract.py +++ b/joint_buying_product/tests/test_abstract.py @@ -30,12 +30,16 @@ def setUp(self): self.Order = self.env["joint.buying.purchase.order"] self.OrderLine = self.env["joint.buying.purchase.order.line"] self.JointBuyingWizardCreateOrder = self.env["joint.buying.wizard.create.order"] + self.JointBuyingWizardUpdateOrderGrouped = self.env[ + "joint.buying.wizard.update.order.grouped" + ] self.company_ELD = self.env.ref("joint_buying_base.company_ELD") self.company_CDA = self.env.ref("joint_buying_base.company_CDA") self.company_CHE = self.env.ref("joint_buying_base.company_CHE") self.company_3PP = self.env.ref("joint_buying_base.company_3PP") self.company_LSE = self.env.ref("joint_buying_base.company_LSE") + self.company_1GG = self.env.ref("joint_buying_base.company_1GG") self.salaison_devidal = self.JointBuyingResPartner.browse( self.env.ref("joint_buying_base.supplier_salaison_devidal").id @@ -56,6 +60,9 @@ def setUp(self): self.product_LSE_patatoe_agila = self.env.ref( "joint_buying_product.product_LSE_patatoe_agila" ) + self.grouped_order_ronzon_past = self.env.ref( + "joint_buying_product.grouped_order_ronzon_past" + ) def _create_order_grouped_salaison_devidal_by_wizard(self, user=False): # Use Wizard to create grouped order diff --git a/joint_buying_product/tests/test_joint_buying_transport_request_compute.py b/joint_buying_product/tests/test_joint_buying_transport_request_compute.py index 73a7eb58..63d597f9 100644 --- a/joint_buying_product/tests/test_joint_buying_transport_request_compute.py +++ b/joint_buying_product/tests/test_joint_buying_transport_request_compute.py @@ -6,15 +6,11 @@ from odoo.tests import tagged -from odoo.addons.joint_buying_base.tests import ( - test_joint_buying_transport_request_compute, -) +from .test_abstract import TestAbstract @tagged("post_install", "-at_install") -class TestJointBuyingTransportRequest( - test_joint_buying_transport_request_compute.TestJointBuyingTransportRequest -): +class TestJointBuyingTransportRequest(TestAbstract): def setUp(self): super().setUp() diff --git a/joint_buying_product/tests/test_joint_buying_wizard_update_order_grouped.py b/joint_buying_product/tests/test_joint_buying_wizard_update_order_grouped.py new file mode 100644 index 00000000..2daa65a6 --- /dev/null +++ b/joint_buying_product/tests/test_joint_buying_wizard_update_order_grouped.py @@ -0,0 +1,45 @@ +# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop) +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + + +from odoo.tests import tagged + +from .test_abstract import TestAbstract + + +@tagged("post_install", "-at_install") +class TestJointBuyingWizardUpdateOrderGrouped(TestAbstract): + def setUp(self): + super().setUp() + + def test_01_wizard_update_order_grouped(self): + + wizard = self.JointBuyingWizardUpdateOrderGrouped.with_context( + active_id=self.grouped_order_ronzon_past.id + ).create({}) + wizard.show_all_orders = True + wizard.show_all_products = True + wizard.onchange_show_settings() + + wizard.line_ids.write({"qty": 0}) + + self.assertEqual( + len( + self.grouped_order_ronzon_past.mapped("order_ids.transport_request_id") + ), + 0, + ) + + wizard.line_ids.write({"qty": 100}) + + self.assertEqual( + len( + self.grouped_order_ronzon_past.mapped("order_ids.transport_request_id") + ), + len( + self.grouped_order_ronzon_past.mapped("order_ids").filtered( + lambda x: x.deposit_partner_id != x.delivery_partner_id + ) + ), + )