This commit is contained in:
David Arranz 2024-07-19 17:59:50 +02:00
parent 986322e3e3
commit 873c8f582e
5 changed files with 70 additions and 17 deletions

View File

@ -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 (

View File

@ -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 (
<div className='grid gap-6 md:grid-cols-6'>
<FormGroup

View File

@ -68,6 +68,25 @@ export const QuoteEdit = () => {
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;

View File

@ -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);

View File

@ -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;
},