From 873c8f582efd2dcc2edd33713ed0bedd14c68b48 Mon Sep 17 00:00:00 2001 From: David Arranz Date: Fri, 19 Jul 2024 17:59:50 +0200 Subject: [PATCH] . --- .../quotes/components/QuotePricesResume.tsx | 6 +-- .../editors/QuoteGeneralCardEditor.tsx | 6 --- client/src/app/quotes/edit.tsx | 39 ++++++++++++++++++- client/src/lib/calc.ts | 34 +++++++++++++--- .../useLocalization/useLocatlization.tsx | 2 +- 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/client/src/app/quotes/components/QuotePricesResume.tsx b/client/src/app/quotes/components/QuotePricesResume.tsx index 2544f36..9dc36a5 100644 --- a/client/src/app/quotes/components/QuotePricesResume.tsx +++ b/client/src/app/quotes/components/QuotePricesResume.tsx @@ -6,11 +6,11 @@ import { useFormContext } from "react-hook-form"; export const QuotePricesResume = () => { const { watch, register, formState } = useFormContext(); - const { formatNumber, formatPercentage } = useLocalization(); + const { formatNumber } = useLocalization(); const subtotal_price = formatNumber(watch("subtotal_price")); - const discount_price = formatPercentage(watch("discount_price")); - const tax_price = formatPercentage(watch("tax_price")); + const discount_price = formatNumber(watch("discount_price")); + const tax_price = formatNumber(watch("tax_price")); const total_price = formatNumber(watch("total_price")); return ( diff --git a/client/src/app/quotes/components/editors/QuoteGeneralCardEditor.tsx b/client/src/app/quotes/components/editors/QuoteGeneralCardEditor.tsx index 5f7534d..67d0dd6 100644 --- a/client/src/app/quotes/components/editors/QuoteGeneralCardEditor.tsx +++ b/client/src/app/quotes/components/editors/QuoteGeneralCardEditor.tsx @@ -6,12 +6,6 @@ import { useFormContext } from "react-hook-form"; export const QuoteGeneralCardEditor = () => { const { register, formState } = useFormContext(); - console.log({ - ...register("customer_information", { - required: true, - }), - }); - return (
{ amount: undefined, scale: 0, }, + discount_price: { + amount: undefined, + scale: 2, + currency_code: data?.currency_code ?? quoteCurrency.code, + }, + before_tax_price: { + amount: undefined, + scale: 2, + currency_code: data?.currency_code ?? quoteCurrency.code, + }, + tax: { + amount: undefined, + scale: 0, + }, + tax_price: { + amount: undefined, + scale: 2, + currency_code: data?.currency_code ?? quoteCurrency.code, + }, total_price: { amount: undefined, scale: 2, @@ -268,6 +287,16 @@ export const QuoteEdit = () => { ); break; + case name === "discount" || name === "tax": { + const quoteTotals = calculateQuoteTotals(quote); + setValue("subtotal_price", quoteTotals.subtotalPrice.toObject()); + setValue("discount_price", quoteTotals.discountPrice.toObject()); + setValue("before_tax_price", quoteTotals.priceBeforeTaxes.toObject()); + setValue("tax_price", quoteTotals.taxesPrice.toObject()); + setValue("total_price", quoteTotals.totalPrice.toObject()); + break; + } + case name === "items": { quote.items && quote.items.map((item, index) => { @@ -276,8 +305,11 @@ export const QuoteEdit = () => { setValue(`items.${index}.total_price`, quoteItemTotals.totalPrice.toObject()); }); - const quoteTotals = calculateQuoteTotals(quote); + const quoteTotals = calculateQuoteTotals(quote, true); setValue("subtotal_price", quoteTotals.subtotalPrice.toObject()); + setValue("discount_price", quoteTotals.discountPrice.toObject()); + setValue("before_tax_price", quoteTotals.priceBeforeTaxes.toObject()); + setValue("tax_price", quoteTotals.taxesPrice.toObject()); setValue("total_price", quoteTotals.totalPrice.toObject()); break; @@ -294,8 +326,11 @@ export const QuoteEdit = () => { setValue(`items.${index}.total_price`, quoteItemTotals.totalPrice.toObject()); // Cabecera - const quoteTotals = calculateQuoteTotals(quote); + const quoteTotals = calculateQuoteTotals(quote, true); setValue("subtotal_price", quoteTotals.subtotalPrice.toObject()); + setValue("discount_price", quoteTotals.discountPrice.toObject()); + setValue("before_tax_price", quoteTotals.priceBeforeTaxes.toObject()); + setValue("tax_price", quoteTotals.taxesPrice.toObject()); setValue("total_price", quoteTotals.totalPrice.toObject()); break; diff --git a/client/src/lib/calc.ts b/client/src/lib/calc.ts index df85c83..76b37dc 100644 --- a/client/src/lib/calc.ts +++ b/client/src/lib/calc.ts @@ -1,21 +1,45 @@ import { MoneyValue, Percentage, Quantity } from "@shared/contexts"; -export const calculateQuoteTotals = (quote: any) => { - const { discount: discount_dto, tax: tax_dto } = quote || {}; +export const calculateQuoteTotals = (quote: any, force: boolean = false) => { + const { discount: discount_dto, tax: tax_dto, subtotal_price: subtotal_price_dto } = quote || {}; + + const discountOrError = Percentage.create( + discount_dto || { + amount: null, + scale: 2, + } + ); - const discountOrError = Percentage.create(discount_dto); if (discountOrError.isFailure) { throw discountOrError.error; } const discount = discountOrError.object; - const taxOrError = Percentage.create(tax_dto); + const taxOrError = Percentage.create( + tax_dto || { + amount: null, + scale: 2, + } + ); + if (taxOrError.isFailure) { throw taxOrError.error; } const tax = taxOrError.object; - const subtotalPrice = calculateQuoteItemsTotals(quote.items).convertScale(2); + const subtotalOrError = MoneyValue.create( + subtotal_price_dto || { + amount: null, + scale: 2, + } + ); + + if (subtotalOrError.isFailure) { + throw subtotalOrError.error; + } + const subtotalPrice = force + ? calculateQuoteItemsTotals(quote.items).convertScale(2) + : subtotalOrError.object; const discountPrice = subtotalPrice.percentage(discount.toNumber()).convertScale(2); diff --git a/client/src/lib/hooks/useLocalization/useLocatlization.tsx b/client/src/lib/hooks/useLocalization/useLocatlization.tsx index c815c0e..f2a0ff8 100644 --- a/client/src/lib/hooks/useLocalization/useLocatlization.tsx +++ b/client/src/lib/hooks/useLocalization/useLocatlization.tsx @@ -57,7 +57,7 @@ export const useCustomLocalization = (props: UseLocalizationProps) => { useGrouping: true,*/ }).format(amount === null ? 0 : adjustPrecision({ amount, scale })); - console.log(value, result); + //console.log(value, result); return result; },