diff --git a/hotel_channel_connector/components/backend_adapter.py b/hotel_channel_connector/components/backend_adapter.py index 7db9f48bc..4cf04d669 100644 --- a/hotel_channel_connector/components/backend_adapter.py +++ b/hotel_channel_connector/components/backend_adapter.py @@ -56,6 +56,9 @@ class HotelChannelInterfaceAdapter(AbstractComponent): def create_vplan(self, name, pid, dtype, value): raise NotImplementedError + def modify_vplan(self, pid, dtype, value): + raise NotImplementedError + def delete_plan(self, channel_plan_id): raise NotImplementedError diff --git a/hotel_channel_connector/models/product_pricelist/common.py b/hotel_channel_connector/models/product_pricelist/common.py index 41f01ca13..3a2dfed17 100644 --- a/hotel_channel_connector/models/product_pricelist/common.py +++ b/hotel_channel_connector/models/product_pricelist/common.py @@ -6,6 +6,8 @@ from odoo.exceptions import UserError from odoo.addons.queue_job.job import job from odoo.addons.component.core import Component from odoo.addons.component_event import skip_if +import logging +_logger = logging.getLogger(__name__) class ChannelProductPricelist(models.Model): @@ -37,6 +39,15 @@ class ChannelProductPricelist(models.Model): exporter = work.component(usage='product.pricelist.exporter') exporter.create_vplan(self) + @job(default_channel='root.channel') + @api.multi + def modify_vplan(self): + self.ensure_one() + if self.external_id: + with self.backend_id.work_on(self._name) as work: + exporter = work.component(usage='product.pricelist.exporter') + exporter.modify_vplan(self) + @job(default_channel='root.channel') @api.multi def update_plan_name(self): @@ -149,6 +160,9 @@ class BindingProductPricelistListener(Component): if 'name' in fields: for binding in record.channel_bind_ids: binding.update_plan_name() + if 'item_ids' in fields and record.is_virtual_plan: + for binding in record.channel_bind_ids: + binding.modify_vplan() class ChannelBindingProductPricelistListener(Component): diff --git a/hotel_channel_connector/models/product_pricelist/exporter.py b/hotel_channel_connector/models/product_pricelist/exporter.py index 7c1a10c79..f46795a8d 100644 --- a/hotel_channel_connector/models/product_pricelist/exporter.py +++ b/hotel_channel_connector/models/product_pricelist/exporter.py @@ -22,3 +22,7 @@ class ProductPricelistExporter(Component): @api.model def create_vplan(self, binding): raise NotImplementedError + + @api.model + def modify_vplan(self, binding): + raise NotImplementedError \ No newline at end of file diff --git a/hotel_channel_connector_wubook/components/backend_adapter.py b/hotel_channel_connector_wubook/components/backend_adapter.py index 4a61abc6f..72bf18799 100644 --- a/hotel_channel_connector_wubook/components/backend_adapter.py +++ b/hotel_channel_connector_wubook/components/backend_adapter.py @@ -337,6 +337,21 @@ class WuBookAdapter(AbstractComponent): }) return results + def modify_vplan(self, pid, dtype, value): + rcode, results = self._server.mod_vplans( + self._session_info[0], + self._session_info[1], + [{'pid': pid, + 'variation': value, + 'variation_type': dtype + }] + ) + if rcode != 0: + raise ChannelConnectorError(_("Can't modify virtual pricing plan in wubook"), { + 'message': results, + }) + return results + def delete_plan(self, channel_plan_id): rcode, results = self._server.del_plan( self._session_info[0], diff --git a/hotel_channel_connector_wubook/models/product_pricelist/common.py b/hotel_channel_connector_wubook/models/product_pricelist/common.py index fdb23e1b4..688c3dff9 100644 --- a/hotel_channel_connector_wubook/models/product_pricelist/common.py +++ b/hotel_channel_connector_wubook/models/product_pricelist/common.py @@ -18,6 +18,9 @@ class ProductPricelistAdapter(Component): def create_vplan(self, name, pid, dtype, value): return super(ProductPricelistAdapter, self).create_vplan(name, pid, dtype, value) + def modify_vplan(self, pid, dtype, value): + return super(ProductPricelistAdapter, self).modify_vplan(pid, dtype, value) + def delete_plan(self, external_id): return super(ProductPricelistAdapter, self).delete_plan(external_id) diff --git a/hotel_channel_connector_wubook/models/product_pricelist/exporter.py b/hotel_channel_connector_wubook/models/product_pricelist/exporter.py index bf30ad252..eb12a10c9 100644 --- a/hotel_channel_connector_wubook/models/product_pricelist/exporter.py +++ b/hotel_channel_connector_wubook/models/product_pricelist/exporter.py @@ -40,12 +40,27 @@ class ProductPricelistExporter(Component): @api.model def create_vplan(self, binding): try: - import wdb; wdb.set_trace() + item_ids = binding.odoo_id.item_ids + base_pricelist = item_ids.base_pricelist_id + value = item_ids.price_discount or item_ids.price_surcharge + dtype = 0 + # NOTE: price_discount is greater than zero for a discount + # and lesser than zero for increasing the price a percentage + if item_ids.price_discount > 0: + dtype = -1 + elif item_ids.price_discount < 0: + dtype = 1 + # NOTE: price_surcharge is greater than zero for increasing the price + # and lesser than zero for a fixed discount + if item_ids.price_surcharge > 0: + dtype = 2 + elif item_ids.price_discount < 0: + dtype = -2 external_id = self.backend_adapter.create_vplan( binding.name, - binding.pid, - binding.dtype, - binding.value, + base_pricelist.channel_bind_ids.external_id, + dtype, + value, ) except ChannelConnectorError as err: self.create_issue( @@ -54,4 +69,36 @@ class ProductPricelistExporter(Component): channel_message=err.data['message']) else: binding.external_id = external_id - self.binder.bind(external_id, binding) \ No newline at end of file + self.binder.bind(external_id, binding) + + @api.model + def modify_vplan(self, binding): + try: + item_ids = binding.odoo_id.item_ids + base_pricelist = item_ids.base_pricelist_id + value = item_ids.price_discount or item_ids.price_surcharge + dtype = 0 + # NOTE: price_discount is greater than zero for a discount + # and lesser than zero for increasing the price a percentage + if item_ids.price_discount > 0: + dtype = -1 + elif item_ids.price_discount < 0: + dtype = 1 + # NOTE: price_surcharge is greater than zero for increasing the price + # and lesser than zero for a fixed discount + if item_ids.price_surcharge > 0: + dtype = 2 + elif item_ids.price_discount < 0: + dtype = -2 + binding.with_context({ + 'connector_no_export': True, + }).write({'sync_date': fields.Datetime.now()}) + return self.backend_adapter.modify_vplan( + base_pricelist.channel_bind_ids.external_id, + dtype, + value) + except ChannelConnectorError as err: + self.create_issue( + section='pricelist', + internal_message=str(err), + channel_message=err.data['message'])