From 1a7af568368908ee1edfb1ab4db9135a4e023615 Mon Sep 17 00:00:00 2001 From: Ilyas Date: Fri, 15 Dec 2023 14:22:02 +0500 Subject: [PATCH] [IMP] pos_disable_pricelist_selection: selectable pricelists --- pos_disable_pricelist_selection/README.rst | 26 +++++-- .../__manifest__.py | 1 + .../models/pos_config.py | 69 ++++++++++++++++++- .../readme/CONTEXT.rst | 1 + .../readme/DESCRIPTION.rst | 2 +- .../readme/USAGE.rst | 9 ++- .../static/description/index.html | 52 +++++++------- .../static/src/js/PosModel.js | 31 +++++++++ .../static/src/js/SetPricelistButton.js | 21 ++++++ .../views/assets.xml | 8 +++ .../views/pos_config_view.xml | 28 ++++++++ 11 files changed, 215 insertions(+), 33 deletions(-) create mode 100644 pos_disable_pricelist_selection/readme/CONTEXT.rst create mode 100644 pos_disable_pricelist_selection/static/src/js/PosModel.js create mode 100644 pos_disable_pricelist_selection/static/src/js/SetPricelistButton.js diff --git a/pos_disable_pricelist_selection/README.rst b/pos_disable_pricelist_selection/README.rst index 7ce2e07dbc..b842f2b189 100644 --- a/pos_disable_pricelist_selection/README.rst +++ b/pos_disable_pricelist_selection/README.rst @@ -7,7 +7,7 @@ Disable Pricelist selection button in POS !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:8f42c1529377cdb9437e7dcee3a0ac4dc559530e373e03662886e03e535f8251 + !! source digest: sha256:b6d65b350b4dcc110d420a6ea344d211056ce9420fe3cda7b40f59db9c373ebc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -28,19 +28,27 @@ Disable Pricelist selection button in POS |badge1| |badge2| |badge3| |badge4| |badge5| -Disable Pricelist selection button in POS +This module allows to restrict POS interface user from selecting any or specific pricelists. **Table of contents** .. contents:: :local: +Use Cases / Context +=================== + +In case there is a structure of pricelists with lines using "Formula: Based on" (eg: Pricelist 3 is based on Pricelist 2 which is based on Pricelist 1) they all need to be added in "Available pricelists" for POS to load correctly. + Usage ===== -* In POS configuration enable multiple pricelists -* There will be a checkbox "Hide Pricelist Button" -* If checked pricelist selection button will be hidden in POS Screen +In POS configuration, enable advanced pricelists and add all pricelists with reference to pricelists to be used in POS in "Available Pricelists". + +To restrict POS user from selecting any pricelist (therefore only use prices from "Default Pricelist"), enable boolean "Hide Pricelist Button" + +To restrict POS user's pricelist selection to a set of pricelists, add them to "Selectable Pricelists". +Note: make sure "Default Pricelist" is among "Selectable Pricelists". Changelog ========= @@ -88,6 +96,14 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. +.. |maintainer-ilyasprogrammer| image:: https://github.com/ilyasprogrammer.png?size=40px + :target: https://github.com/ilyasprogrammer + :alt: ilyasprogrammer + +Current `maintainer `__: + +|maintainer-ilyasprogrammer| + This module is part of the `OCA/pos `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/pos_disable_pricelist_selection/__manifest__.py b/pos_disable_pricelist_selection/__manifest__.py index 7d7db5645c..a980c1a5ad 100644 --- a/pos_disable_pricelist_selection/__manifest__.py +++ b/pos_disable_pricelist_selection/__manifest__.py @@ -7,6 +7,7 @@ "license": "LGPL-3", "category": "Purchase", "website": "https://github.com/OCA/pos", + "maintainers": ["ilyasprogrammer"], "depends": ["point_of_sale"], "external_dependencies": {}, "demo": [], diff --git a/pos_disable_pricelist_selection/models/pos_config.py b/pos_disable_pricelist_selection/models/pos_config.py index 50931cfba1..d9bbfc55a0 100644 --- a/pos_disable_pricelist_selection/models/pos_config.py +++ b/pos_disable_pricelist_selection/models/pos_config.py @@ -1,9 +1,76 @@ -from odoo import fields, models +from odoo import api, fields, models class PosConfig(models.Model): _inherit = "pos.config" + def _default_pricelist(self): + return self.env["product.pricelist"].search( + [ + ("company_id", "in", (False, self.env.company.id)), + ("currency_id", "=", self.env.company.currency_id.id), + ], + limit=1, + ) + hide_pricelist_button = fields.Boolean( default=False, ) + selectable_pricelist_ids = fields.Many2many( + "product.pricelist", + string="Selectable Pricelists", + domain="[('id', 'in', available_pricelist_ids)]", + relation="pos_conf_selectable_pricelist_rel", + default=_default_pricelist, + ) + pricelist_id_domain = fields.Binary( + compute="_compute_pricelist_id_domain", + readonly=True, + store=False, + ) + + @api.depends( + "hide_pricelist_button", "allowed_pricelist_ids", "selectable_pricelist_ids" + ) + def _compute_pricelist_id_domain(self): + for rec in self: + if rec.hide_pricelist_button: + rec.pricelist_id_domain = [("id", "in", rec.allowed_pricelist_ids.ids)] + else: + rec.pricelist_id_domain = [ + ("id", "in", rec.selectable_pricelist_ids.ids) + ] + + @api.onchange("selectable_pricelist_ids") + def onchange_selectable_pricelist_ids(self): + if self.pricelist_id.id not in self.selectable_pricelist_ids.ids: + self.update({"pricelist_id": self.selectable_pricelist_ids[0].id}) + + @api.onchange("available_pricelist_ids") + def onchange_available_pricelist_ids(self): + self.update( + {"selectable_pricelist_ids": [(6, 0, self.allowed_pricelist_ids.ids)]} + ) + + @api.onchange("hide_pricelist_button") + def onchange_hide_pricelist_button(self): + self.update( + {"selectable_pricelist_ids": [(6, 0, self.allowed_pricelist_ids.ids)]} + ) + + def write(self, vals): + for rec in self: + if vals.get("available_pricelist_ids"): + if rec and not vals.get("selectable_pricelist_ids"): + selectable = set(rec.selectable_pricelist_ids.ids) + else: + selectable = set(vals["selectable_pricelist_ids"][0][2]) + # leave only ids from available_pricelist_ids + intersection = list( + selectable.intersection(set(vals["available_pricelist_ids"][0][2])) + ) + if intersection: + vals["selectable_pricelist_ids"] = [(6, 0, intersection)] + else: + vals["selectable_pricelist_ids"] = vals["available_pricelist_ids"] + return super(PosConfig, self).write(vals) diff --git a/pos_disable_pricelist_selection/readme/CONTEXT.rst b/pos_disable_pricelist_selection/readme/CONTEXT.rst new file mode 100644 index 0000000000..b9b54049db --- /dev/null +++ b/pos_disable_pricelist_selection/readme/CONTEXT.rst @@ -0,0 +1 @@ +In case there is a structure of pricelists with lines using "Formula: Based on" (eg: Pricelist 3 is based on Pricelist 2 which is based on Pricelist 1) they all need to be added in "Available pricelists" for POS to load correctly. diff --git a/pos_disable_pricelist_selection/readme/DESCRIPTION.rst b/pos_disable_pricelist_selection/readme/DESCRIPTION.rst index 7261337ab1..32ec7c81fb 100644 --- a/pos_disable_pricelist_selection/readme/DESCRIPTION.rst +++ b/pos_disable_pricelist_selection/readme/DESCRIPTION.rst @@ -1 +1 @@ -Disable Pricelist selection button in POS +This module allows to restrict POS interface user from selecting any or specific pricelists. diff --git a/pos_disable_pricelist_selection/readme/USAGE.rst b/pos_disable_pricelist_selection/readme/USAGE.rst index e4ee197bca..96d094d178 100644 --- a/pos_disable_pricelist_selection/readme/USAGE.rst +++ b/pos_disable_pricelist_selection/readme/USAGE.rst @@ -1,3 +1,6 @@ -* In POS configuration enable multiple pricelists -* There will be a checkbox "Hide Pricelist Button" -* If checked pricelist selection button will be hidden in POS Screen +In POS configuration, enable advanced pricelists and add all pricelists with reference to pricelists to be used in POS in "Available Pricelists". + +To restrict POS user from selecting any pricelist (therefore only use prices from "Default Pricelist"), enable boolean "Hide Pricelist Button" + +To restrict POS user's pricelist selection to a set of pricelists, add them to "Selectable Pricelists". +Note: make sure "Default Pricelist" is among "Selectable Pricelists". diff --git a/pos_disable_pricelist_selection/static/description/index.html b/pos_disable_pricelist_selection/static/description/index.html index fdeb408cff..a0ef707a21 100644 --- a/pos_disable_pricelist_selection/static/description/index.html +++ b/pos_disable_pricelist_selection/static/description/index.html @@ -367,46 +367,50 @@

Disable Pricelist selection button in POS

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:8f42c1529377cdb9437e7dcee3a0ac4dc559530e373e03662886e03e535f8251 +!! source digest: sha256:b6d65b350b4dcc110d420a6ea344d211056ce9420fe3cda7b40f59db9c373ebc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: LGPL-3 OCA/pos Translate me on Weblate Try me on Runboat

-

Disable Pricelist selection button in POS

+

This module allows to restrict POS interface user from selecting any or specific pricelists.

Table of contents

+
+

Use Cases / Context

+

In case there is a structure of pricelists with lines using “Formula: Based on” (eg: Pricelist 3 is based on Pricelist 2 which is based on Pricelist 1) they all need to be added in “Available pricelists” for POS to load correctly.

+
-

Usage

-
    -
  • In POS configuration enable multiple pricelists
  • -
  • There will be a checkbox “Hide Pricelist Button”
  • -
  • If checked pricelist selection button will be hidden in POS Screen
  • -
+

Usage

+

In POS configuration, enable advanced pricelists and add all pricelists with reference to pricelists to be used in POS in “Available Pricelists”.

+

To restrict POS user from selecting any pricelist (therefore only use prices from “Default Pricelist”), enable boolean “Hide Pricelist Button”

+

To restrict POS user’s pricelist selection to a set of pricelists, add them to “Selectable Pricelists”. +Note: make sure “Default Pricelist” is among “Selectable Pricelists”.

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -414,28 +418,30 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • Ooops
  • Cetmix
-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

+

Current maintainer:

+

ilyasprogrammer

This module is part of the OCA/pos project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

diff --git a/pos_disable_pricelist_selection/static/src/js/PosModel.js b/pos_disable_pricelist_selection/static/src/js/PosModel.js new file mode 100644 index 0000000000..c43ea44652 --- /dev/null +++ b/pos_disable_pricelist_selection/static/src/js/PosModel.js @@ -0,0 +1,31 @@ +odoo.define("pos_disable_pricelist_selection.PosModel", function (require) { + "use strict"; + + var models = require("point_of_sale.models"); + var posmodel_super = models.PosModel.prototype; + + models.PosModel = models.PosModel.extend({ + load_server_data: function () { + this.models.push({ + model: "product.pricelist", + fields: ["name", "display_name", "discount_policy"], + domain: function (self) { + if (self.config.use_pricelist) { + if (self.config.hide_pricelist_button) { + return [["id", "in", self.config.available_pricelist_ids]]; + } + return [["id", "in", self.config.selectable_pricelist_ids]]; + } + return [["id", "=", self.config.pricelist_id[0]]]; + }, + loaded: function (self, pricelists) { + _.map(pricelists, function (pricelist) { + pricelist.items = []; + }); + self.selectable_pricelists = pricelists; + }, + }); + return posmodel_super.load_server_data.apply(this, arguments); + }, + }); +}); diff --git a/pos_disable_pricelist_selection/static/src/js/SetPricelistButton.js b/pos_disable_pricelist_selection/static/src/js/SetPricelistButton.js new file mode 100644 index 0000000000..89f6ea2a99 --- /dev/null +++ b/pos_disable_pricelist_selection/static/src/js/SetPricelistButton.js @@ -0,0 +1,21 @@ +odoo.define("pos_disable_pricelist_selection.SetPricelistButton", function (require) { + "use strict"; + + const SetPricelistButton = require("point_of_sale.SetPricelistButton"); + const Registries = require("point_of_sale.Registries"); + + const SetPricelistButtonSelect = (SetPricelistButton) => + class extends SetPricelistButton { + async onClick() { + // Lets use selectable_pricelists instead of pricelists + const original_pricelists = this.env.pos.pricelists; + this.env.pos.pricelists = this.env.pos.selectable_pricelists; + super.onClick(); + this.env.pos.pricelists = original_pricelists; + } + }; + + Registries.Component.extend(SetPricelistButton, SetPricelistButtonSelect); + + return SetPricelistButtonSelect; +}); diff --git a/pos_disable_pricelist_selection/views/assets.xml b/pos_disable_pricelist_selection/views/assets.xml index e4cde6aab1..71c8a04a1b 100644 --- a/pos_disable_pricelist_selection/views/assets.xml +++ b/pos_disable_pricelist_selection/views/assets.xml @@ -6,6 +6,14 @@ type="text/javascript" src="/pos_disable_pricelist_selection/static/src/js/ProductScreen.js" /> +