diff --git a/src/pages/05_quotation/QuotationFormInfo.vue b/src/pages/05_quotation/QuotationFormInfo.vue index bcb529b3..420dff3b 100644 --- a/src/pages/05_quotation/QuotationFormInfo.vue +++ b/src/pages/05_quotation/QuotationFormInfo.vue @@ -14,6 +14,7 @@ import SelectInput from 'src/components/shared/SelectInput.vue'; import useOptionStore from 'src/stores/options'; import { storeToRefs } from 'pinia'; +import { precisionRound } from 'src/utils/arithmetic'; defineProps<{ readonly?: boolean; @@ -93,32 +94,97 @@ const payTypeOpion = ref([ }, ]); -const bankBookOptions = ref[]>([]); -let bankBoookFilter: ( - value: string, - update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void, -) => void; +const amount4Show = ref([]); -onMounted(() => { - if (optionStore.globalOption) { - bankBoookFilter = selectFilterOptionRefMod( - ref(optionStore.globalOption.bankBook), - bankBookOptions, - 'label', - ); +// const bankBookOptions = ref[]>([]); +// let bankBoookFilter: ( +// value: string, +// update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void, +// ) => void; + +// onMounted(() => { +// if (optionStore.globalOption) { +// bankBoookFilter = selectFilterOptionRefMod( +// ref(optionStore.globalOption.bankBook), +// bankBookOptions, +// 'label', +// ); +// } +// }); + +// watch( +// () => optionStore.globalOption, +// () => { +// bankBoookFilter = selectFilterOptionRefMod( +// ref(optionStore.globalOption.bankBook), +// bankBookOptions, +// 'label', +// ); +// }, +// ); +function calculateInstallments(param: { + customIndex?: number; + customAmount?: number; + newCount: number; + oldCount: number; +}) { + if (param.newCount !== null && param.oldCount !== null) { + const totalAmount = summaryPrice.value.finalPrice; + + if (param.newCount > param.oldCount) { + const installmentAmount = +(totalAmount / param.newCount).toFixed(2); + + // Update existing + paySplit.value.forEach((payment, i) => { + if (i !== param.customIndex) { + payment.amount = installmentAmount; + amount4Show.value[i] = installmentAmount.toString(); + } + }); + + // Add + for (let i = param.oldCount; i < param.newCount; i++) { + paySplit.value.push({ + no: i + 1, + date: null, + amount: installmentAmount, + }); + amount4Show.value.push(installmentAmount.toString()); + } + } else if (param.newCount < param.oldCount) { + // Remove extra + paySplit.value.splice(param.newCount, param.oldCount - param.newCount); + amount4Show.value.splice(param.newCount, param.oldCount - param.newCount); + + // Recalculate + const installmentAmount = +(totalAmount / param.newCount).toFixed(2); + paySplit.value.forEach((payment, i) => { + if (i !== param.customIndex) { + payment.amount = installmentAmount; + amount4Show.value[i] = installmentAmount.toString(); + } + }); + } else if (param.newCount === param.oldCount) { + if (param.customIndex !== undefined && param.customAmount !== undefined) { + paySplit.value[param.customIndex].amount = param.customAmount; + amount4Show.value[param.customIndex] = param.customAmount.toString(); + } + } + + // Calculate last + if (paySplit.value.length > 0) { + const totalPreviousPayments = paySplit.value + .slice(0, -1) + .reduce((sum, payment) => sum + payment.amount, 0); + + const remainingAmount = totalAmount - totalPreviousPayments; + paySplit.value[paySplit.value.length - 1].amount = + +remainingAmount.toFixed(2); + amount4Show.value[amount4Show.value.length - 1] = + (+remainingAmount.toFixed(2)).toString(); + } } -}); - -watch( - () => optionStore.globalOption, - () => { - bankBoookFilter = selectFilterOptionRefMod( - ref(optionStore.globalOption.bankBook), - bankBookOptions, - 'label', - ); - }, -); +} watch( () => payType.value, @@ -134,49 +200,7 @@ watch( watch( () => [paySplitCount.value, summaryPrice.value.finalPrice], ([newCount, _newF], [oldCount, _oldF]) => { - if (newCount !== null && oldCount !== null) { - const totalAmount = summaryPrice.value.finalPrice; - - if (newCount > oldCount) { - // Calculate the base installment amount for all except the last installment - const installmentAmount = +(totalAmount / newCount).toFixed(2); - - // Update existing installments - paySplit.value.forEach((payment) => { - payment.amount = installmentAmount; - }); - - // Add new installments - for (let i = oldCount; i < newCount; i++) { - paySplit.value.push({ - no: i + 1, - date: null, - amount: installmentAmount, - }); - } - } else if (newCount < oldCount) { - // Remove extra installments - paySplit.value.splice(newCount, oldCount - newCount); - - // Recalculate the base installment amount for remaining installments - const installmentAmount = +(totalAmount / newCount).toFixed(2); - paySplit.value.forEach((payment) => { - payment.amount = installmentAmount; - }); - } - - // Calculate the amount for the last installment - if (paySplit.value.length > 0) { - const totalPreviousPayments = paySplit.value - .slice(0, -1) - .reduce((sum, payment) => sum + payment.amount, 0); - - // Set the last installment to cover the remaining balance, rounded to 2 decimal places - const remainingAmount = totalAmount - totalPreviousPayments; - paySplit.value[paySplit.value.length - 1].amount = - +remainingAmount.toFixed(2); - } - } + calculateInstallments({ newCount: newCount || 0, oldCount: oldCount || 0 }); }, { deep: true }, ); @@ -326,11 +350,16 @@ watch( /> +
+ {{ amount4Show }} +
+ {{ paySplit.map((i) => i.amount) }} +