diff --git a/src/app/utils/formatUtil.ts b/src/app/utils/formatUtil.ts index 15c7d82..bc72e4d 100644 --- a/src/app/utils/formatUtil.ts +++ b/src/app/utils/formatUtil.ts @@ -32,7 +32,7 @@ export const truncateMoney = (amount: number | undefined) => { const fractionDigits = maximumFractionDigits ?? minimumFractionDigits ?? 0; const factor = Math.pow(10, fractionDigits); - const truncatedAmount = Math.floor(amount * factor) / factor; + const truncatedAmount = Math.round(amount * factor) / factor; return truncatedAmount; }; diff --git a/src/components/CreateProject/CreateProject.tsx b/src/components/CreateProject/CreateProject.tsx index c0f1db9..22767b6 100644 --- a/src/components/CreateProject/CreateProject.tsx +++ b/src/components/CreateProject/CreateProject.tsx @@ -59,7 +59,7 @@ import { } from "../Swal/CustomAlerts"; import dayjs from "dayjs"; import { DELETE_PROJECT } from "@/middleware"; -import { OUTPUT_DATE_FORMAT } from "@/app/utils/formatUtil"; +import { OUTPUT_DATE_FORMAT, sumMoney } from "@/app/utils/formatUtil"; import { deleteDraft, loadDraft, saveToLocalStorage } from "@/app/utils/draftUtils"; export interface Props { @@ -272,7 +272,7 @@ const CreateProject: React.FC = ({ const onSubmit = useCallback>( async (data, event) => { try { - console.log(data); + // console.log(data); // detect errors let hasErrors = false; @@ -373,26 +373,38 @@ const CreateProject: React.FC = ({ data.milestones[parseFloat(key)].startDate = null data.milestones[parseFloat(key)].endDate = null } - // if ( - // !Boolean(startDate) || - // startDate === "Invalid Date" || - // !Boolean(endDate) || - // endDate === "Invalid Date" || - // new Date(startDate) > new Date(endDate) - // ) { - // formProps.setError("milestones", { - // message: "milestones is not valid", - // type: "invalid", - // }); - // setTabIndex(3); - // hasErrors = true; - // } - + /* + unused code for checking the stage start date and end date + if ( + !Boolean(startDate) || + startDate === "Invalid Date" || + !Boolean(endDate) || + endDate === "Invalid Date" || + new Date(startDate) > new Date(endDate) + ) { + formProps.setError("milestones", { + message: "milestones is not valid", + type: "invalid", + }); + setTabIndex(3); + hasErrors = true; + } + + unused code for bulk add milestone payment projectTotal += payments.reduce( (acc, payment) => acc + payment.amount, 0, ); + **/ + + projectTotal += payments.reduce( + (acc, p) => sumMoney(acc, p.amount), + 0, + ); + + }); + // console.log(projectTotal) console.log(milestonesKeys) if ( diff --git a/src/components/CreateProject/Milestone.tsx b/src/components/CreateProject/Milestone.tsx index d7c9de7..69f41bc 100644 --- a/src/components/CreateProject/Milestone.tsx +++ b/src/components/CreateProject/Milestone.tsx @@ -23,6 +23,7 @@ import { useFormContext } from "react-hook-form"; import { CreateProjectInputs } from "@/app/api/projects/actions"; import MilestoneSection from "./MilestoneSection"; import ProjectTotalFee from "./ProjectTotalFee"; +import { sumMoney } from "@/app/utils/formatUtil"; export interface Props { allTasks: Task[]; @@ -89,7 +90,12 @@ const Milestone: React.FC = ({ allTasks, isActive }) => { // hasError = true // } - projectTotal += payments.reduce((acc, payment) => acc + payment.amount, 0) + // projectTotal += payments.reduce((acc, payment) => acc + payment.amount, 0) + projectTotal += payments.reduce( + (acc, p) => sumMoney(acc, p.amount), + 0, + ); + }) console.log(milestonesKeys)