BOD-62 creation of totals utility class + base calculations.

This commit is contained in:
Patrick Fic
2020-04-06 21:09:48 -07:00
parent 4c679b6d83
commit 6bf9ba5be0
7 changed files with 560 additions and 122 deletions

View File

@@ -0,0 +1,9 @@
import React from "react";
export default function JobsTotalsTableComponent({ totals }) {
return (
<div>
<div>{JSON.stringify(totals, null, 2)}</div>
</div>
);
}

View File

@@ -0,0 +1,56 @@
import { useQuery } from "@apollo/react-hooks";
import React, { useEffect, useState } from "react";
import { QUERY_JOB_FINANCIALS } from "../../graphql/jobs.queries";
import AlertComponent from "../alert/alert.component";
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
import JobTotalsTableComponent from "./job-totals-table.component";
import {
CalculateRatesTotals,
CalculatePartsTotals,
CalculateCustPayable
} from "./job-totals.utility";
export default function JobTotalsTableContainer({ jobId }) {
const { loading, error, data } = useQuery(QUERY_JOB_FINANCIALS, {
variables: { jobId: jobId }
});
const [totals, setTotals] = useState({});
useEffect(() => {
if (!!data) {
setTotals({
parts: CalculatePartsTotals(data.jobs_by_pk.joblines),
rates: CalculateRatesTotals(data.jobs_by_pk),
custPayable: CalculateCustPayable(data.jobs_by_pk)
});
}
}, [data]);
console.log("totals", totals);
if (loading) return <LoadingSpinner />;
if (error) return <AlertComponent message={error.message} type='error' />;
return (
<div>
<JobTotalsTableComponent totals={totals} />
<div>
<div>STL Data</div>
<div>
{data
? JSON.stringify(data.jobs_by_pk.cieca_stl.data, null, 2)
: null}
</div>
</div>
<div>
<div>ttl Data</div>
<div>
{data
? JSON.stringify(data.jobs_by_pk.cieca_ttl.data, null, 2)
: null}
</div>
</div>
</div>
);
}

View File

@@ -0,0 +1,179 @@
export function CalculateRatesTotals(ratesList) {
const jobLines = ratesList.joblines;
let ret = {
rate_la1: {
hours: jobLines
.filter(item => item.mod_lbr_ty === "LA1")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2),
rate: ratesList.rate_la1
},
rate_la2: {
hours: jobLines
.filter(item => item.mod_lbr_ty === "LA2")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2),
rate: ratesList.rate_la2
},
rate_la3: {
rate: ratesList.rate_la3,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LA3")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_la4: {
rate: ratesList.rate_la4,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LA4")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_laa: {
rate: ratesList.rate_laa,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAA")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lab: {
rate: ratesList.rate_lab,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAB")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lad: {
rate: ratesList.rate_lad,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAD")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lae: {
rate: ratesList.rate_lae,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAE")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_laf: {
rate: ratesList.rate_laf,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAF")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lag: {
rate: ratesList.rate_lag,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAG")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lam: {
rate: ratesList.rate_lam,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAM")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lar: {
rate: ratesList.rate_lar,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAR")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_las: {
rate: ratesList.rate_las,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAS")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
},
rate_lau: {
rate: ratesList.rate_lau,
hours: jobLines
.filter(item => item.mod_lbr_ty === "LAU")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.toFixed(2)
}
};
let subtotal = 0;
for (const property in ret) {
ret[property].total = (ret[property].hours * ret[property].rate).toFixed(2);
subtotal = subtotal + ret[property].hours * ret[property].rate;
}
ret.subtotal = subtotal.toFixed(2);
return ret;
}
export function CalculatePartsTotals(jobLines) {
const ret = jobLines.reduce(
(acc, value) => {
switch (value.part_type) {
case "PAA":
case "PAC":
case "PAG":
case "PAL":
case "PAM":
case "PAN":
case "PAND":
case "PANF":
case "PAO":
case "PAP":
case "PAR":
return {
...acc,
parts: {
...acc.parts,
subtotal: acc.parts.subtotal + value.act_price
//TODO Add Adjustments in
}
};
case "PAS":
case "PASL":
return {
...acc,
sublets: {
...acc.sublets,
subtotal: acc.sublets.subtotal + value.act_price
//TODO Add Adjustments in
}
};
default:
return acc;
}
},
{
parts: { subtotal: 0, adjustments: 0, total: 0 },
sublets: { subtotal: 0, adjustments: 0, total: 0 }
}
);
return {
parts: { ...ret.parts, total: ret.parts.subtotal + ret.parts.adjustments },
sublets: {
...ret.sublets,
total: ret.sublets.subtotal + ret.sublets.adjustments
}
};
}
export function CalculateCustPayable(job) {
return {
deductible: job.ded_amt || 0,
federal_tax: job.federal_tax_payable || 0,
other_customer_amount: job.other_amount_payable || 0,
dep_taxes: job.depreciation_taxes || 0,
total:
job.ded_amt ||
0 + job.federal_tax_payable ||
0 + job.other_amount_payable ||
0 + job.depreciation_taxes ||
0
};
}