Skip to Content
Menu
This question has been flagged
2 Replies
1808 Views

Hello,

I am working on Odoo 17e and I want to change the global discount to a fixed amount instead of percentage. How can I achieve that via custom module? Which functions should I target?

Any help is appreciated.

Thanks,

Avatar
Discard
Best Answer

Thank to @Waleen for pointing me in the right direction. The line to edit in that file is #27. This is only if you want to change the discount to apply as a fixed amount.

Here's a modification of the same file that first gives you a selection popup to choose whether you want a fixed or % discount, then you enter the discount amount

(sorry, code formatting is not applying properly so you'll have to copy-paste in your IDE and format)


/** @odoo-module **/

import { _t } from "@web/core/l10n/translation";import { ProductScreen } from "@point_of_sale/app/screens/product_screen/product_screen";import { useService } from "@web/core/utils/hooks";import { NumberPopup } from "@point_of_sale/app/utils/input_popups/number_popup";import { ErrorPopup } from "@point_of_sale/app/errors/popups/error_popup";import { SelectionPopup } from "@point_of_sale/app/utils/input_popups/selection_popup"; // Assuming SelectionPopup exists or create a similar componentimport { Component } from "@odoo/owl";import { usePos } from "@point_of_sale/app/store/pos_hook";import { parseFloat } from "@web/views/fields/parsers";

export class DiscountButton extends Component {    static template = "pos_discount.DiscountButton";

    setup() {        this.pos = usePos();        this.popup = useService("popup");    }

    async click() {        // First, ask the user to choose discount type        const { confirmed: typeConfirmed, payload: discountType } = await this.popup.add(SelectionPopup, {            title: _t("Discount Type"),            list: [                { id: 1, label: _t("Percentage"), item: "percentage", isSelected: true },                { id: 2, label: _t("Fixed Amount"), item: "fixed" },            ],            isInputSelected: true,        });        if (!typeConfirmed) return;

        // Ask for discount value based on the chosen type        const { confirmed, payload } = await this.popup.add(NumberPopup, {            title: discountType === "percentage" ? _t("Discount Percentage") : _t("Discount Amount"),            startingValue: this.pos.config.discount_pc || 0,            isInputSelected: true,        });        if (confirmed) {            const val = parseFloat(payload);            await this.apply_discount(val, discountType);        }    }

    async apply_discount(value, discountType) {        const order = this.pos.get_order();        const lines = order.get_orderlines();        const product = this.pos.db.get_product_by_id(this.pos.config.discount_product_id[0]);

        if (product === undefined) {            await this.popup.add(ErrorPopup, {                title: _t("No discount product found"),                body: _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        lines            .filter((line) => line.get_product() === product)            .forEach((line) => order._unlinkOrderline(line));

        // Add one discount line per tax group        const linesByTax = order.get_orderlines_grouped_by_tax_ids();        for (const [tax_ids, lines] of Object.entries(linesByTax)) {            const tax_ids_array = tax_ids                .split(",")                .filter((id) => id !== "")                .map((id) => Number(id));

            const baseToDiscount = order.calculate_base_amount(                tax_ids_array,                lines.filter((ll) => ll.isGlobalDiscountApplicable())            );

            // Calculate discount based on type            let discount = 0;            if (discountType === "percentage") {                discount = (-value / 100.0) * baseToDiscount;            } else if (discountType === "fixed") {                discount = -value;            }

            if (discount < 0) {                order.add_product(product, {                    price: discount,                    lst_price: discount,                    tax_ids: tax_ids_array,                    merge: false,                    description:                        `${discountType === "percentage" ? value + "%" : this.pos.currency.symbol + value}, ` +                        (tax_ids_array.length                            ? _t(                                  "Tax: %s",                                  tax_ids_array                                      .map((taxId) => this.pos.taxes_by_id[taxId].amount + "%")                                      .join(", ")                              )                            : _t("No tax")),                    extras: {                        price_type: "automatic",                    },                });            }        }    }}

ProductScreen.addControlButton({    component: DiscountButton,    condition: function () {        const { module_pos_discount, discount_product_id } = this.pos.config;        return module_pos_discount && discount_product_id;    },});


Avatar
Discard
Related Posts Replies Views Activity
1
May 25
500
2
Oct 16
2925
1
Mar 15
4538
1
Jun 25
416
1
Mar 25
692