diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/pos_discount/static/src | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/pos_discount/static/src')
| -rw-r--r-- | addons/pos_discount/static/src/js/DiscountButton.js | 82 | ||||
| -rw-r--r-- | addons/pos_discount/static/src/js/models.js | 21 | ||||
| -rw-r--r-- | addons/pos_discount/static/src/xml/DiscountButton.xml | 12 |
3 files changed, 115 insertions, 0 deletions
diff --git a/addons/pos_discount/static/src/js/DiscountButton.js b/addons/pos_discount/static/src/js/DiscountButton.js new file mode 100644 index 00000000..e906d354 --- /dev/null +++ b/addons/pos_discount/static/src/js/DiscountButton.js @@ -0,0 +1,82 @@ +odoo.define('pos_discount.DiscountButton', function(require) { + 'use strict'; + + const PosComponent = require('point_of_sale.PosComponent'); + const ProductScreen = require('point_of_sale.ProductScreen'); + const { useListener } = require('web.custom_hooks'); + const Registries = require('point_of_sale.Registries'); + + class DiscountButton extends PosComponent { + constructor() { + super(...arguments); + useListener('click', this.onClick); + } + async onClick() { + var self = this; + const { confirmed, payload } = await this.showPopup('NumberPopup',{ + title: this.env._t('Discount Percentage'), + startingValue: this.env.pos.config.discount_pc, + }); + if (confirmed) { + const val = Math.round(Math.max(0,Math.min(100,parseFloat(payload)))); + await self.apply_discount(val); + } + } + + async apply_discount(pc) { + var order = this.env.pos.get_order(); + var lines = order.get_orderlines(); + var product = this.env.pos.db.get_product_by_id(this.env.pos.config.discount_product_id[0]); + if (product === undefined) { + await this.showPopup('ErrorPopup', { + title : this.env._t("No discount product found"), + body : this.env._t("The discount product seems misconfigured. Make sure it is flagged as 'Can be Sold' and 'Available in Point of Sale'."), + }); + return; + } + + // Remove existing discounts + var i = 0; + while ( i < lines.length ) { + if (lines[i].get_product() === product) { + order.remove_orderline(lines[i]); + } else { + i++; + } + } + + // Add discount + // We add the price as manually set to avoid recomputation when changing customer. + var base_to_discount = order.get_total_without_tax(); + if (product.taxes_id.length){ + var first_tax = this.env.pos.taxes_by_id[product.taxes_id[0]]; + if (first_tax.price_include) { + base_to_discount = order.get_total_with_tax(); + } + } + var discount = - pc / 100.0 * base_to_discount; + + if( discount < 0 ){ + order.add_product(product, { + price: discount, + lst_price: discount, + extras: { + price_manually_set: true, + }, + }); + } + } + } + DiscountButton.template = 'DiscountButton'; + + ProductScreen.addControlButton({ + component: DiscountButton, + condition: function() { + return this.env.pos.config.module_pos_discount && this.env.pos.config.discount_product_id; + }, + }); + + Registries.Component.add(DiscountButton); + + return DiscountButton; +}); diff --git a/addons/pos_discount/static/src/js/models.js b/addons/pos_discount/static/src/js/models.js new file mode 100644 index 00000000..c54c7c93 --- /dev/null +++ b/addons/pos_discount/static/src/js/models.js @@ -0,0 +1,21 @@ +odoo.define('pos_discount.models', function (require) { + "use strict"; + + var models = require('point_of_sale.models'); + + var existing_models = models.PosModel.prototype.models; + var product_index = _.findIndex(existing_models, function (model) { + return model.model === "product.product"; + }); + var product_model = existing_models[product_index]; + + models.load_models([{ + model: product_model.model, + fields: product_model.fields, + order: product_model.order, + domain: function(self) {return [['id', '=', self.config.discount_product_id[0]]];}, + context: product_model.context, + loaded: product_model.loaded, + }]); + +}); diff --git a/addons/pos_discount/static/src/xml/DiscountButton.xml b/addons/pos_discount/static/src/xml/DiscountButton.xml new file mode 100644 index 00000000..c8cd5bc5 --- /dev/null +++ b/addons/pos_discount/static/src/xml/DiscountButton.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<templates id="template" xml:space="preserve"> + + <t t-name="DiscountButton" owl="1"> + <span class="control-button js_discount"> + <i class="fa fa-tag"></i> + <span> </span> + <span>Discount</span> + </span> + </t> + +</templates> |
