WIP PBS AP.
This commit is contained in:
138
server/accounting/pbs/pbs-ap-allocations.js
Normal file
138
server/accounting/pbs/pbs-ap-allocations.js
Normal file
@@ -0,0 +1,138 @@
|
||||
const path = require("path");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
|
||||
const queries = require("../../graphql-client/queries");
|
||||
const CdkBase = require("../../web-sockets/web-socket");
|
||||
const moment = require("moment");
|
||||
const Dinero = require("dinero.js");
|
||||
|
||||
exports.default = async function (socket, billids) {
|
||||
try {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`Received request to calculate allocations for ${billids}`
|
||||
);
|
||||
const { bills, bodyshops } = await QueryBillData(socket, billids);
|
||||
const bodyshop = bodyshops[0];
|
||||
const transactionLines = [];
|
||||
|
||||
bills.forEach((bill) => {
|
||||
//Keep the allocations at the bill level.
|
||||
const billHash = {
|
||||
[bodyshop.md_responsibility_centers.taxes.federal_itc.name]: {
|
||||
Account:
|
||||
bodyshop.md_responsibility_centers.taxes.federal_itc.dms_acctnumber,
|
||||
//ControlNumber: "String", //need to figure this out still?
|
||||
Amount: Dinero(),
|
||||
// Comment: "String",
|
||||
//AdditionalInfo: "String",
|
||||
InvoiceNumber: bill.invoice_number,
|
||||
InvoiceDate: moment(bill.date).tz(bodyshop.timezone).toISOString(),
|
||||
},
|
||||
[bodyshop.md_responsibility_centers.taxes.state.name]: {
|
||||
Account:
|
||||
bodyshop.md_responsibility_centers.taxes.state.dms_acctnumber,
|
||||
//ControlNumber: "String", //need to figure this out still?
|
||||
Amount: Dinero(),
|
||||
// Comment: "String",
|
||||
//AdditionalInfo: "String",
|
||||
InvoiceNumber: bill.invoice_number,
|
||||
InvoiceDate: moment(bill.date).tz(bodyshop.timezone).toISOString(),
|
||||
},
|
||||
};
|
||||
|
||||
bill.billlines.forEach((bl) => {
|
||||
let lineDinero = Dinero({
|
||||
amount: Math.round((bl.actual_cost || 0) * 100),
|
||||
})
|
||||
.multiply(bl.quantity)
|
||||
.multiply(bill.is_credit_memo ? -1 : 1);
|
||||
const cc = getCostAccount(bl, bodyshop.md_responsibility_centers);
|
||||
|
||||
if (!billHash[cc.name]) {
|
||||
billHash[cc.name] = {
|
||||
Account: cc.dms_acctnumber,
|
||||
//ControlNumber: "String", //need to figure this out still?
|
||||
Amount: Dinero(),
|
||||
// Comment: "String",
|
||||
//AdditionalInfo: "String",
|
||||
InvoiceNumber: bill.invoice_number,
|
||||
InvoiceDate: moment(bill.date).tz(bodyshop.timezone).toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
//Add the line amount.
|
||||
|
||||
billHash[cc.name] = {
|
||||
...billHash[cc.name],
|
||||
Amount: billHash[cc.name].Amount.add(lineDinero),
|
||||
};
|
||||
|
||||
//Does the line have taxes?
|
||||
if (bl.applicable_taxes.federal) {
|
||||
billHash[bodyshop.md_responsibility_centers.taxes.federal_itc.name] =
|
||||
{
|
||||
...bodyshop.md_responsibility_centers.taxes.federal_itc.name,
|
||||
Amount: billHash[
|
||||
bodyshop.md_responsibility_centers.taxes.federal_itc.name
|
||||
].Amount.add(lineDinero.percentage(bl.federal_tax_rate || 0)),
|
||||
};
|
||||
}
|
||||
if (bl.applicable_taxes.state) {
|
||||
billHash[bodyshop.md_responsibility_centers.taxes.state.name] = {
|
||||
...bodyshop.md_responsibility_centers.taxes.state.name,
|
||||
Amount: billHash[
|
||||
bodyshop.md_responsibility_centers.taxes.state.name
|
||||
].Amount.add(lineDinero.percentage(bl.state_tax_rate || 0)),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
Object.keys(billHash).map((key) => {
|
||||
transactionLines.push(billHash[key]);
|
||||
});
|
||||
});
|
||||
|
||||
return transactionLines;
|
||||
} catch (error) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"ERROR",
|
||||
`Error encountered in CdkCalculateAllocations. ${error}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
async function QueryBillData(socket, billids) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`Querying bill data for id(s) ${billids}`
|
||||
);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
|
||||
.request(queries.GET_PBS_AP_ALLOCATIONS, { billids: billids });
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"TRACE",
|
||||
`Bill data query result ${JSON.stringify(result, null, 2)}`
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
//@returns the account object.
|
||||
function getCostAccount(billline, respcenters) {
|
||||
if (!billline.cost_center) return null;
|
||||
|
||||
const acctName = respcenters.defaults.costs[billline.cost_center];
|
||||
|
||||
return respcenters.costs.find((c) => c.name === acctName);
|
||||
}
|
||||
@@ -1633,3 +1633,45 @@ mutation ($sesid: String!, $status: String, $context: jsonb) {
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
exports.GET_PBS_AP_ALLOCATIONS = `
|
||||
query GET_PBS_AP_ALLOCATIONS($billids: [uuid!]) {
|
||||
bodyshops(where: {associations: {active: {_eq: true}}}) {
|
||||
md_responsibility_centers
|
||||
timezone
|
||||
}
|
||||
bills(where: {id: {_in: $billids}}) {
|
||||
id
|
||||
date
|
||||
isinhouse
|
||||
invoice_number
|
||||
federal_tax_rate
|
||||
is_credit_memo
|
||||
jobid
|
||||
job {
|
||||
id
|
||||
ro_number
|
||||
}
|
||||
local_tax_rate
|
||||
state_tax_rate
|
||||
total
|
||||
vendorid
|
||||
vendor {
|
||||
id
|
||||
name
|
||||
}
|
||||
billlines {
|
||||
id
|
||||
actual_cost
|
||||
actual_price
|
||||
applicable_taxes
|
||||
cost_center
|
||||
deductedfromlbr
|
||||
lbr_adjustment
|
||||
quantity
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
`;
|
||||
|
||||
@@ -22,6 +22,9 @@ const {
|
||||
PbsSelectedCustomer,
|
||||
} = require("../accounting/pbs/pbs-job-export");
|
||||
|
||||
const PbsCalculateAllocationsAp =
|
||||
require("../accounting/pbs/pbs-ap-allocations").default;
|
||||
|
||||
io.use(function (socket, next) {
|
||||
try {
|
||||
if (socket.handshake.auth.token) {
|
||||
@@ -101,7 +104,7 @@ io.on("connection", (socket) => {
|
||||
});
|
||||
//END CDK
|
||||
|
||||
//PBS
|
||||
//PBS AR
|
||||
socket.on("pbs-calculate-allocations", async (jobid, callback) => {
|
||||
const allocations = await CdkCalculateAllocations(socket, jobid);
|
||||
createLogEvent(socket, "DEBUG", `Allocations calculated.`);
|
||||
@@ -125,7 +128,21 @@ io.on("connection", (socket) => {
|
||||
socket.selectedCustomerId = selectedCustomerId;
|
||||
PbsSelectedCustomer(socket, selectedCustomerId);
|
||||
});
|
||||
//End PBS
|
||||
//End PBS AR
|
||||
|
||||
//PBS AP
|
||||
socket.on("pbs-calculate-allocations-ap", async (billids, callback) => {
|
||||
const allocations = await PbsCalculateAllocationsAp(socket, billids);
|
||||
createLogEvent(socket, "DEBUG", `AP Allocations calculated.`);
|
||||
createLogEvent(
|
||||
socket,
|
||||
"TRACE",
|
||||
`Allocations calculated. ${JSON.stringify(allocations, null, 2)}`
|
||||
);
|
||||
|
||||
callback(allocations);
|
||||
});
|
||||
//END PBS AP
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
createLogEvent(socket, "DEBUG", `User disconnected.`);
|
||||
|
||||
Reference in New Issue
Block a user