2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 01:44:04 +00:00

feat: add loyalty point earning and redemption in POS

This commit is contained in:
AbleKSaju 2024-09-27 17:26:06 +05:30
parent 8d8c85ea88
commit 57d3208437
2 changed files with 70 additions and 1 deletions

View File

@ -27,6 +27,7 @@
:border="true" :border="true"
:value="sinvDoc.loyaltyPoints" :value="sinvDoc.loyaltyPoints"
:df="sinvDoc.fieldMap.loyaltyPoints" :df="sinvDoc.fieldMap.loyaltyPoints"
@change="updateLoyaltyPoints"
/> />
<div class="row-start-6 grid grid-cols-2 gap-4 mt-auto mb-2 px-10"> <div class="row-start-6 grid grid-cols-2 gap-4 mt-auto mb-2 px-10">
@ -34,6 +35,7 @@
<Button <Button
class="w-full bg-green-500" class="w-full bg-green-500"
style="padding: 1.35rem" style="padding: 1.35rem"
@click="setLoyaltyPoints()"
:disabled="validationError" :disabled="validationError"
> >
<slot> <slot>
@ -110,7 +112,56 @@ export default defineComponent({
}; };
}, },
methods: { methods: {
async updateLoyaltyPoints(newValue: number) {
try {
if (this.loyaltyPoints >= newValue) {
this.sinvDoc.loyaltyPoints = newValue;
} else {
throw new Error(
`${this.sinvDoc.party as string} only has ${
this.loyaltyPoints as number
} points`
);
}
const loyaltyProgramDoc = await this.fyo.db.getAll(
ModelNameEnum.LoyaltyProgram,
{
fields: ['conversionFactor'],
filters: { name: this.loyaltyProgram as string },
}
);
const loyaltyPoint =
newValue * ((loyaltyProgramDoc[0]?.conversionFactor as number) || 0);
if (this.sinvDoc.baseGrandTotal?.lt(loyaltyPoint)) {
throw new Error(
t`no need ${newValue as number} points to purchase this item`
);
}
this.validationError = false;
} catch (error) {
this.validationError = true;
showToast({
type: 'error',
message: t`${error as string}`,
});
return;
}
},
setLoyaltyPoints() {
if (
!this.sinvDoc.loyaltyPoints ||
this.sinvDoc.loyaltyPoints > this.loyaltyPoints
) {
return;
}
this.$emit('setLoyaltyPoints', this.sinvDoc.loyaltyPoints);
this.$emit('toggleModal', 'LoyaltyProgram');
},
}, },
}); });
</script> </script>

View File

@ -27,6 +27,7 @@
:sinvDoc="sinvDoc" :sinvDoc="sinvDoc"
:loyalty-points="loyaltyPoints" :loyalty-points="loyaltyPoints"
:loyalty-program="loyaltyProgram" :loyalty-program="loyaltyProgram"
@set-loyalty-points="setLoyaltyPoints"
@toggle-modal="toggleModal" @toggle-modal="toggleModal"
/> />
@ -553,6 +554,23 @@ export default defineComponent({
setTotalTaxedAmount() { setTotalTaxedAmount() {
this.totalTaxedAmount = getTotalTaxedAmount(this.sinvDoc as SalesInvoice); this.totalTaxedAmount = getTotalTaxedAmount(this.sinvDoc as SalesInvoice);
}, },
async setLoyaltyPoints(value: number) {
this.appliedLoyaltyPoints = value;
this.sinvDoc.redeemLoyaltyPoints = true;
const totalLotaltyAmount = await getAddedLPWithGrandTotal(
this.fyo,
this.loyaltyProgram as string,
value as number
);
const total = totalLotaltyAmount
.sub(this.sinvDoc.baseGrandTotal as Money)
.abs();
this.sinvDoc.grandTotal = total;
},
setTransferAmount(amount: Money = fyo.pesa(0)) { setTransferAmount(amount: Money = fyo.pesa(0)) {
this.transferAmount = amount; this.transferAmount = amount;
}, },