Skip to content

Commit

Permalink
[IMP] pos_fixed_discount: Migration to 14.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
hkapatel-initos authored and fshah-initos committed Aug 5, 2021
1 parent 6d8e563 commit 45d980a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pos_fixed_discount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Point of Sale Fixed Discounts",
"summary": "Allow to apply discounts with fixed amount",
"version": "13.0.1.0.0",
"version": "14.0.1.0.0",
"development_status": "Beta",
"category": "Point of Sale",
"website": "https://github.com/OCA/pos",
Expand Down
1 change: 1 addition & 0 deletions pos_fixed_discount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Lorenzo Battistini - https://takobi.online
* Foram Shah <foram.shah@initos.com>
* Helly kapatel <helly.kapatel@initos.com>
78 changes: 45 additions & 33 deletions pos_fixed_discount/static/src/js/discount.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
odoo.define("pos_fixed_discount.pos_fixed_discount", function (require) {
odoo.define("pos_fixed_discount.FixedDiscountButton", function (require) {
"use strict";

var core = require("web.core");
var screens = require("point_of_sale.screens");
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");

var _t = core._t;

var FixedDiscountButton = screens.ActionButtonWidget.extend({
template: "FixedDiscountButton",
button_click: function () {
class FixedDiscountButton extends PosComponent {
constructor() {
super(...arguments);
useListener("click", this.onClick);
}
async onClick() {
var self = this;
this.gui.show_popup("number", {
title: _t("Discount Amount"),
value: 0,
confirm: function (val) {
self.apply_discount(val);
},
const {confirmed, payload} = await this.showPopup("NumberPopup", {
title: this.env._t("Discount Amount"),
startingValue: 0,
});
},
apply_discount: function (amount) {
var order = this.pos.get_order();
if (confirmed) {
var val = Math.round(Math.max(0, Math.min(100, parseFloat(payload))));
self.apply_discount(val);
}
}

async apply_discount(amount) {
var order = this.env.pos.get_order();
var lines = order.get_orderlines();
var product = this.pos.db.get_product_by_id(
this.pos.config.discount_product_id[0]
var product = this.env.pos.db.get_product_by_id(
this.env.pos.config.discount_product_id[0]
);
if (product === undefined) {
this.gui.show_popup("error", {
title: _t("No discount product found"),
body: _t(
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'."
),
});
Expand All @@ -46,31 +51,38 @@ odoo.define("pos_fixed_discount.pos_fixed_discount", function (require) {

// Add discount
// We add the price as manually set to avoid recomputation when changing customer.
var discount = -amount.replace(",", ".");
if (product.taxes_id.length) {
var first_tax = this.env.pos.taxes_by_id[product.taxes_id[0]];
if (first_tax.price_include) {
order.get_total_with_tax();
}
}
var discount = -amount;

if (discount < 0) {
order.add_product(product, {
price: discount,
lst_price: discount,
extras: {
price_manually_set: true,
},
});
}
},
});
}
}
FixedDiscountButton.template = "FixedDiscountButton";

screens.define_action_button({
name: "fixed_discount",
widget: FixedDiscountButton,
ProductScreen.addControlButton({
component: FixedDiscountButton,
condition: function () {
return (
this.pos.config.module_pos_discount &&
this.pos.config.discount_product_id
this.env.pos.config.module_pos_discount &&
this.env.pos.config.discount_product_id
);
},
});

return {
FixedDiscountButton: FixedDiscountButton,
};
Registries.Component.add(FixedDiscountButton);

return FixedDiscountButton;
});
24 changes: 15 additions & 9 deletions pos_fixed_discount/static/src/xml/discount_templates.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates id="template" xml:space="preserve">
<t t-extend="DiscountButton">
<t t-jquery="div[class='control-button js_discount']" t-operation="replace">
<div class='control-button js_discount'>
<i class='fa fa-tag' /> Discount (%)
</div>
<t t-extend="pos_discount.DiscountButton">
<t t-jquery="span[class='control-button js_discount']" t-operation="replace">
<span class="control-button js_discount">
<i class="fa fa-tag" />
<span> </span>
<span>Discount (%)</span>
</span>
</t>
</t>
<t t-name="FixedDiscountButton">
<div class='control-button js_fixed_discount'>
<i class='fa fa-tag' /> Discount (Amount)
</div>

<t t-name="FixedDiscountButton" owl="1">
<span class="control-button js_fixed_discount">
<i class="fa fa-tag" />
<span> </span>
<span>Discount (Amount)</span>
</span>
</t>

</templates>

0 comments on commit 45d980a

Please sign in to comment.