[FIX] pos_loyalty: Check gift card prefix from nomenclature

The barcode nomenclature allows to define custom prefix for gift cards.
e.g. the default nomenclature in demo data allows both 043 or 044 as
prefix for rule of type coupon.

Therefore the hardcoded string with 044 doesn't allow to sell a gift
card whose barcode does not start with this.

Instead of hardcoding the value, fetch it from the configuration and
the nomenclature that is defined in the settings.

OPW-3499787

closes odoo/odoo#159349

Signed-off-by: David Monnom (moda) <moda@odoo.com>
This commit is contained in:
Pedram (pebr)
2024-03-27 14:09:50 +00:00
parent 8846ad9545
commit 91b31d105d
3 changed files with 21 additions and 5 deletions
@@ -95,7 +95,18 @@ patch(PosStore.prototype, {
return false;
}
const trimmedCode = code.trim();
if (trimmedCode && trimmedCode.startsWith("044")) {
let nomenclatureRules = this.barcodeReader.parser.nomenclature.rules;
if (this.barcodeReader.fallbackParser) {
nomenclatureRules = nomenclatureRules.concat(
this.barcodeReader.fallbackParser.nomenclature.rules
);
}
const couponRules = nomenclatureRules.filter((rule) => rule.type === "coupon");
const isValidCoupon = couponRules.some((rule) => {
let patterns = rule.pattern.split("|");
return patterns.some((pattern) => trimmedCode.startsWith(pattern));
});
if (isValidCoupon) {
// check if the code exist in the database
// if so, use its balance, otherwise, use the unit price of the gift card product
const fetchedGiftCard = await this.orm.searchRead(
@@ -48,18 +48,18 @@ registry.category("web_tour.tours").add("GiftCardProgramScanUseTour", {
// Pay the 5$ gift card.
ProductScreen.clickDisplayedProduct("Gift Card"),
TextInputPopup.isShown(),
TextInputPopup.inputText("044123456"),
TextInputPopup.inputText("043123456"),
TextInputPopup.clickConfirm(),
PosLoyalty.orderTotalIs("5.00"),
PosLoyalty.finalizeOrder("Cash", "5"),
// Partially use the gift card. (4$)
ProductScreen.addOrderline("Desk Pad", "2", "2", "4.0"),
PosLoyalty.enterCode("044123456"),
PosLoyalty.enterCode("043123456"),
PosLoyalty.orderTotalIs("0.00"),
PosLoyalty.finalizeOrder("Cash", "0"),
// Use the remaining of the gift card. (5$ - 4$ = 1$)
ProductScreen.addOrderline("Whiteboard Pen", "6", "6", "36.0"),
PosLoyalty.enterCode("044123456"),
PosLoyalty.enterCode("043123456"),
PosLoyalty.orderTotalIs("35.00"),
PosLoyalty.finalizeOrder("Cash", "35"),
].flat(),
+6 -1
View File
@@ -543,6 +543,11 @@ class TestUi(TestPointOfSaleHttpCommon):
(4, self.env.ref('stock.group_stock_user').id),
]
})
# set the nomenclature to GS1
barcodes_gs1_nomenclature = self.env.ref("barcodes_gs1_nomenclature.default_gs1_nomenclature")
self.main_pos_config.company_id.write({
'nomenclature_id': barcodes_gs1_nomenclature.id
})
LoyaltyProgram = self.env['loyalty.program']
# Deactivate all other programs to avoid interference
@@ -558,7 +563,7 @@ class TestUi(TestPointOfSaleHttpCommon):
{"active_id": gift_card_program.id}
).create({"coupon_qty": 1, 'points_granted': 5}).generate_coupons()
# Change the code of the gift card.
gift_card_program.coupon_ids.code = '044123456'
gift_card_program.coupon_ids.code = '043123456'
# Run the tour. It will pay the gift card and use it.
self.start_tour(
"/pos/web?config_id=%d" % self.main_pos_config.id,