Skip to content

Commit

Permalink
fix: credit note
Browse files Browse the repository at this point in the history
  • Loading branch information
keellyp committed Feb 11, 2025
1 parent c104201 commit 48c7351
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 47 deletions.
73 changes: 28 additions & 45 deletions src/components/creditNote/CreditNoteFormCalculation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { gql } from '@apollo/client'
import Decimal from 'decimal.js'
import { FormikProps, getIn } from 'formik'
import { debounce } from 'lodash'
import { useCallback, useEffect, useMemo } from 'react'
Expand Down Expand Up @@ -182,23 +181,22 @@ export const CreditNoteFormCalculation = ({
useEffect(() => {
// Set the default values for credit payback fields
formikProps.setFieldValue('payBack.0.type', CreditTypeEnum.credit)
formikProps.setFieldValue(
'payBack.0.value',
!totalTaxIncluded ? undefined : Number(totalTaxIncluded || 0)?.toFixed(currencyPrecision),
)
formikProps.setFieldValue('payBack.0.value', undefined)

// Initialize the refund field if possible
if (canRefund) {
formikProps.setFieldValue('payBack.1.type', CreditTypeEnum.refund)
formikProps.setFieldValue('payBack.1.value', undefined)
}

formikProps.setTouched({ payBack: true })

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [totalTaxIncluded])
}, [])

useEffect(() => {
formikProps.setFieldValue(
'payBack.0.value',
!totalTaxIncluded ? undefined : Number(totalTaxIncluded || 0)?.toFixed(currencyPrecision),
)

setPayBackValidation(
array()
.of(
Expand All @@ -208,45 +206,38 @@ export const CreditNoteFormCalculation = ({
}),
)
.test({
test: function (payback) {
test: (payback, { createError }) => {
if (canRefund) {
const credit = payback?.[0].value
const creditSet = credit && credit > 0
const refund = payback?.[1].value
const refundSet = refund && refund > 0
const credit = payback?.[0].value ?? 0
const refund = payback?.[1].value ?? 0
const errors: ValidationError[] = []

if (!totalTaxIncluded || !maxCreditableAmount || !maxRefundableAmount) {
return true
}

// Check if the sum of credit and refund is different than the total tax included
if (creditSet && refundSet && credit + refund !== totalTaxIncluded) {
if (credit + refund !== totalTaxIncluded) {
errors.push(
this.createError({
createError({
message: PayBackErrorEnum.maxTotalInvoice,
path: 'payBackErrors',
}),
)
}
// If refund only set, check if it is different than the total tax included
if (!creditSet && refundSet && refund !== totalTaxIncluded) {
errors.push(
this.createError({
message: PayBackErrorEnum.maxSumItems,
path: 'payBackErrors',
}),
)
}
// Check if refund is greater than the max refundable amount
if (refundSet && refund > maxRefundableAmount) {
if (refund > maxRefundableAmount) {
errors.push(
this.createError({
createError({
message: PayBackErrorEnum.maxRefund,
path: 'payBack.1.value',
}),
)
}
// Check if credit is greater than the max creditable amount
if (creditSet && credit > maxCreditableAmount) {
if (credit > maxCreditableAmount) {
errors.push(
this.createError({
createError({
message: PayBackErrorEnum.maxCredit,
path: 'payBack.0.value',
}),
Expand All @@ -260,7 +251,11 @@ export const CreditNoteFormCalculation = ({
},
}),
)
}, [formikProps.values.payBack])

formikProps.setTouched({
payBack: true,
})
}, [totalTaxIncluded])

if (!invoice) return null

Expand Down Expand Up @@ -375,17 +370,12 @@ export const CreditNoteFormCalculation = ({
label={translate('text_637d0e720ace4ea09aaf0630')}
hasError={
!!getIn(formikProps.errors, 'payBack.0.value') ||
getIn(formikProps.errors, 'payBackErrors') === PayBackErrorEnum.maxTotalInvoice
!!getIn(formikProps.errors, 'payBackErrors')
}
error={
getIn(formikProps.errors, 'payBack.0.value') === PayBackErrorEnum.maxCredit
? translate('text_1738751394771xq525lyxj9k', {
max: intlFormatNumber(
new Decimal(maxCreditableAmount)
.minus(formikProps.values.payBack?.[1].value ?? 0)
.toNumber(),
{ currency },
),
max: intlFormatNumber(maxCreditableAmount, { currency }),
})
: undefined
}
Expand All @@ -399,9 +389,7 @@ export const CreditNoteFormCalculation = ({
})}
hasError={
!!getIn(formikProps.errors, 'payBack.1.value') ||
[PayBackErrorEnum.maxSumItems, PayBackErrorEnum.maxTotalInvoice].includes(
getIn(formikProps.errors, 'payBackErrors'),
)
!!getIn(formikProps.errors, 'payBackErrors')
}
error={
getIn(formikProps.errors, 'payBack.1.value') === PayBackErrorEnum.maxRefund
Expand All @@ -418,11 +406,6 @@ export const CreditNoteFormCalculation = ({
<Alert type="danger">
<Typography
color="textSecondary"
{...(getIn(formikProps.errors, 'payBackErrors') === PayBackErrorEnum.maxSumItems && {
html: translate('text_1738943751446py5pdviwixn', {
total: intlFormatNumber(totalTaxIncluded || 0, { currency }),
}),
})}
{...(getIn(formikProps.errors, 'payBackErrors') ===
PayBackErrorEnum.maxTotalInvoice && {
html: translate('text_637e334680481f653e8caa9d', {
Expand Down
1 change: 0 additions & 1 deletion src/components/creditNote/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ export enum CreditNoteFeeErrorEnum {
export enum PayBackErrorEnum {
maxRefund = 'maxRefund',
maxCredit = 'maxCredit',
maxSumItems = 'maxSumItems',
maxTotalInvoice = 'maxTotalInvoice',
}
2 changes: 1 addition & 1 deletion translations/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@
"text_637d10c83077eff6e8c79cd0": "Refund customer (maximum {{max}})",
"text_637d2e7e5af40c52246b1a12": "Remove method",
"text_637e23e47a15bf0bd71e0d03": "Refund amount cannot exceed {{max}}",
"text_637e334680481f653e8caa9d": "The sum of <b>credit on customer wallet</b> and <b>refund</b> amounts must equal the <b>total amount to credit</b> of {{total}}.",
"text_637e334680481f653e8caa9d": "The <b>total amount to credit</b>, calculated by summing the item amounts, must equal the sum of the credit on the customer’s wallet and the total refund amount of {{total}}.",
"text_63725b30957fd5b26b308dd3": "Credit notes",
"text_63725b30957fd5b26b308dd7": "Credits available",
"text_63725b30957fd5b26b308dd9": "Total amount available",
Expand Down

0 comments on commit 48c7351

Please sign in to comment.