.
This commit is contained in:
parent
986322e3e3
commit
873c8f582e
@ -6,11 +6,11 @@ import { useFormContext } from "react-hook-form";
|
|||||||
|
|
||||||
export const QuotePricesResume = () => {
|
export const QuotePricesResume = () => {
|
||||||
const { watch, register, formState } = useFormContext();
|
const { watch, register, formState } = useFormContext();
|
||||||
const { formatNumber, formatPercentage } = useLocalization();
|
const { formatNumber } = useLocalization();
|
||||||
|
|
||||||
const subtotal_price = formatNumber(watch("subtotal_price"));
|
const subtotal_price = formatNumber(watch("subtotal_price"));
|
||||||
const discount_price = formatPercentage(watch("discount_price"));
|
const discount_price = formatNumber(watch("discount_price"));
|
||||||
const tax_price = formatPercentage(watch("tax_price"));
|
const tax_price = formatNumber(watch("tax_price"));
|
||||||
const total_price = formatNumber(watch("total_price"));
|
const total_price = formatNumber(watch("total_price"));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -6,12 +6,6 @@ import { useFormContext } from "react-hook-form";
|
|||||||
export const QuoteGeneralCardEditor = () => {
|
export const QuoteGeneralCardEditor = () => {
|
||||||
const { register, formState } = useFormContext();
|
const { register, formState } = useFormContext();
|
||||||
|
|
||||||
console.log({
|
|
||||||
...register("customer_information", {
|
|
||||||
required: true,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='grid gap-6 md:grid-cols-6'>
|
<div className='grid gap-6 md:grid-cols-6'>
|
||||||
<FormGroup
|
<FormGroup
|
||||||
|
|||||||
@ -68,6 +68,25 @@ export const QuoteEdit = () => {
|
|||||||
amount: undefined,
|
amount: undefined,
|
||||||
scale: 0,
|
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: {
|
total_price: {
|
||||||
amount: undefined,
|
amount: undefined,
|
||||||
scale: 2,
|
scale: 2,
|
||||||
@ -268,6 +287,16 @@ export const QuoteEdit = () => {
|
|||||||
);
|
);
|
||||||
break;
|
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": {
|
case name === "items": {
|
||||||
quote.items &&
|
quote.items &&
|
||||||
quote.items.map((item, index) => {
|
quote.items.map((item, index) => {
|
||||||
@ -276,8 +305,11 @@ export const QuoteEdit = () => {
|
|||||||
setValue(`items.${index}.total_price`, quoteItemTotals.totalPrice.toObject());
|
setValue(`items.${index}.total_price`, quoteItemTotals.totalPrice.toObject());
|
||||||
});
|
});
|
||||||
|
|
||||||
const quoteTotals = calculateQuoteTotals(quote);
|
const quoteTotals = calculateQuoteTotals(quote, true);
|
||||||
setValue("subtotal_price", quoteTotals.subtotalPrice.toObject());
|
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());
|
setValue("total_price", quoteTotals.totalPrice.toObject());
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@ -294,8 +326,11 @@ export const QuoteEdit = () => {
|
|||||||
setValue(`items.${index}.total_price`, quoteItemTotals.totalPrice.toObject());
|
setValue(`items.${index}.total_price`, quoteItemTotals.totalPrice.toObject());
|
||||||
|
|
||||||
// Cabecera
|
// Cabecera
|
||||||
const quoteTotals = calculateQuoteTotals(quote);
|
const quoteTotals = calculateQuoteTotals(quote, true);
|
||||||
setValue("subtotal_price", quoteTotals.subtotalPrice.toObject());
|
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());
|
setValue("total_price", quoteTotals.totalPrice.toObject());
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -1,21 +1,45 @@
|
|||||||
import { MoneyValue, Percentage, Quantity } from "@shared/contexts";
|
import { MoneyValue, Percentage, Quantity } from "@shared/contexts";
|
||||||
|
|
||||||
export const calculateQuoteTotals = (quote: any) => {
|
export const calculateQuoteTotals = (quote: any, force: boolean = false) => {
|
||||||
const { discount: discount_dto, tax: tax_dto } = quote || {};
|
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) {
|
if (discountOrError.isFailure) {
|
||||||
throw discountOrError.error;
|
throw discountOrError.error;
|
||||||
}
|
}
|
||||||
const discount = discountOrError.object;
|
const discount = discountOrError.object;
|
||||||
|
|
||||||
const taxOrError = Percentage.create(tax_dto);
|
const taxOrError = Percentage.create(
|
||||||
|
tax_dto || {
|
||||||
|
amount: null,
|
||||||
|
scale: 2,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
if (taxOrError.isFailure) {
|
if (taxOrError.isFailure) {
|
||||||
throw taxOrError.error;
|
throw taxOrError.error;
|
||||||
}
|
}
|
||||||
const tax = taxOrError.object;
|
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);
|
const discountPrice = subtotalPrice.percentage(discount.toNumber()).convertScale(2);
|
||||||
|
|
||||||
|
|||||||
@ -57,7 +57,7 @@ export const useCustomLocalization = (props: UseLocalizationProps) => {
|
|||||||
useGrouping: true,*/
|
useGrouping: true,*/
|
||||||
}).format(amount === null ? 0 : adjustPrecision({ amount, scale }));
|
}).format(amount === null ? 0 : adjustPrecision({ amount, scale }));
|
||||||
|
|
||||||
console.log(value, result);
|
//console.log(value, result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user