IO-1269
This commit is contained in:
@@ -1,167 +0,0 @@
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const DineroQbFormat = require("../accounting-constants").DineroQbFormat;
|
||||
const path = require("path");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
const queries = require("../../graphql-client/queries");
|
||||
const Dinero = require("dinero.js");
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { jobId } = req.body;
|
||||
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
Authorization: BearerToken,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_JOBS_FOR_RECEIVABLES_EXPORT, { id: jobId });
|
||||
|
||||
const { jobs_by_pk } = result;
|
||||
const { bodyshop } = jobs_by_pk;
|
||||
//Build the IIF file.
|
||||
const response = [];
|
||||
response.push(TRNS_HEADER);
|
||||
response.push(
|
||||
generateInvoiceHeader(jobs_by_pk, bodyshop.md_responsibility_centers.ar)
|
||||
);
|
||||
|
||||
//Allocations
|
||||
const invoice_allocation = jobs_by_pk.invoice_allocation;
|
||||
Object.keys(invoice_allocation.partsAllocations).forEach(
|
||||
(partsAllocationKey) => {
|
||||
if (
|
||||
!!!invoice_allocation.partsAllocations[partsAllocationKey].allocations
|
||||
)
|
||||
return;
|
||||
|
||||
invoice_allocation.partsAllocations[
|
||||
partsAllocationKey
|
||||
].allocations.forEach((alloc) => {
|
||||
response.push(
|
||||
generateInvoiceLine(
|
||||
jobs_by_pk,
|
||||
alloc,
|
||||
bodyshop.md_responsibility_centers
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
Object.keys(invoice_allocation.labMatAllocations).forEach(
|
||||
(AllocationKey) => {
|
||||
if (!!!invoice_allocation.labMatAllocations[AllocationKey].allocations)
|
||||
return;
|
||||
|
||||
invoice_allocation.labMatAllocations[AllocationKey].allocations.forEach(
|
||||
(alloc) => {
|
||||
response.push(
|
||||
generateInvoiceLine(
|
||||
jobs_by_pk,
|
||||
alloc,
|
||||
bodyshop.md_responsibility_centers
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
//End Allocations
|
||||
|
||||
//Taxes
|
||||
const taxMapping = bodyshop.md_responsibility_centers.taxes;
|
||||
const { federal_tax, state_tax, local_tax } = JSON.parse(
|
||||
jobs_by_pk.job_totals
|
||||
).totals;
|
||||
const federal_tax_dinero = Dinero(federal_tax);
|
||||
const state_tax_dinero = Dinero(state_tax);
|
||||
const local_tax_dinero = Dinero(local_tax);
|
||||
|
||||
if (federal_tax_dinero.getAmount() > 0) {
|
||||
response.push(
|
||||
generateTaxLine(jobs_by_pk, federal_tax_dinero, "federal", taxMapping)
|
||||
);
|
||||
}
|
||||
if (state_tax_dinero.getAmount() > 0) {
|
||||
response.push(
|
||||
generateTaxLine(jobs_by_pk, state_tax_dinero, "state", taxMapping)
|
||||
);
|
||||
}
|
||||
if (local_tax_dinero.getAmount() > 0) {
|
||||
response.push(
|
||||
generateTaxLine(jobs_by_pk, local_tax_dinero, "local", taxMapping)
|
||||
);
|
||||
}
|
||||
//End Taxes
|
||||
|
||||
response.push(END_TRNS);
|
||||
|
||||
//Prep the response and send it.
|
||||
res.setHeader("Content-type", "application/octet-stream");
|
||||
res.setHeader("Content-disposition", "attachment; filename=file.txt");
|
||||
res.setHeader("filename", `${jobs_by_pk.ro_number}-RECEIVABLES.iif`);
|
||||
res.send(response.join("\n"));
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
|
||||
const TRNS_HEADER = `!TRNS TRNSID TRNSTYPE DATE ACCNT NAME CLASS AMOUNT DOCNUM MEMO CLEAR TOPRINT NAMEISTAXABLE ADDR1 ADDR2 ADDR3 ADDR4 DUEDATE TERMS OTHER1 PONUM
|
||||
!SPL SPLID TRNSTYPE DATE ACCNT NAME CLASS AMOUNT DOCNUM MEMO CLEAR QNTY PRICE INVITEM PAYMETH TAXABLE VALADJ SERVICEDATE OTHER2 EXTRA
|
||||
!ENDTRNS`;
|
||||
|
||||
const generateInvoiceHeader = (job, arMapping) =>
|
||||
`TRNS INVOICE ${generateJobInvoiceDate(job)} ${arMapping.name} GUO DA Acct.# ${
|
||||
job.ownerid
|
||||
}:${job.ro_number} 0100 ${job.clm_total} ${job.ro_number} N N Y GUO DA Acct.# ${
|
||||
job.ownr_id
|
||||
}:${job.ro_number} ${job.ownr_addr1} ${job.ownr_city} ${job.ownr_st} ${
|
||||
job.ownr_zip
|
||||
} `;
|
||||
|
||||
const generateInvoiceLine = (job, allocation, responsibilityCenters) => {
|
||||
const { amount, center } = allocation;
|
||||
const DineroAmount = Dinero(amount);
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => i.name.toLowerCase() === center.toLowerCase()
|
||||
);
|
||||
|
||||
if (!!!account) {
|
||||
throw new Error(
|
||||
`A matching account does not exist for the allocation. Center: ${center}`
|
||||
);
|
||||
}
|
||||
|
||||
return `SPL INVOICE ${generateJobInvoiceDate(job)} ${
|
||||
account.accountname
|
||||
} 0100 ${DineroAmount.multiply(-1).toFormat(DineroQbFormat)} ${job.ro_number} ${
|
||||
account.accountdesc
|
||||
} N ${DineroAmount.toFormat(DineroQbFormat)} ${account.accountitem} Y N `;
|
||||
};
|
||||
|
||||
const generateTaxLine = (job, amount, type, taxMapping) => {
|
||||
return `SPL INVOICE ${generateJobInvoiceDate(job)} ${
|
||||
taxMapping[type].accountname
|
||||
} ${taxMapping[type].accountdesc} 0100 ${amount
|
||||
.multiply(-1)
|
||||
.toFormat(DineroQbFormat)} ${job.ro_number} N ${taxMapping[type].rate.toFixed(
|
||||
2
|
||||
)}% ${taxMapping[type].accountitem} N N AUTOSTAX `;
|
||||
};
|
||||
|
||||
const END_TRNS = `ENDTRNS`;
|
||||
|
||||
const generateJobInvoiceDate = (job) => {
|
||||
return `${new Date(job.date_invoiced).getMonth() + 1}/${new Date(
|
||||
job.date_invoiced
|
||||
).getDate()}/${new Date(job.date_invoiced).getFullYear()}`;
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
exports.receivables = require("./iif-receivables").default
|
||||
@@ -7,7 +7,6 @@ var builder = require("xmlbuilder2");
|
||||
const QbXmlUtils = require("./qbxml-utils");
|
||||
const moment = require("moment");
|
||||
const logger = require("../../utils/logger");
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
@@ -26,6 +25,13 @@ exports.default = async (req, res) => {
|
||||
});
|
||||
|
||||
try {
|
||||
logger.log(
|
||||
"qbxml-payable-create",
|
||||
"DEBUG",
|
||||
req.user.email,
|
||||
req.body.billsToQuery
|
||||
);
|
||||
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_BILLS_FOR_PAYABLES_EXPORT, {
|
||||
@@ -43,15 +49,14 @@ exports.default = async (req, res) => {
|
||||
});
|
||||
|
||||
//For each invoice.
|
||||
|
||||
res.status(200).json(QbXmlToExecute);
|
||||
} catch (error) {
|
||||
logger.log(
|
||||
"qbxml-payable-error",
|
||||
"error",
|
||||
req.body.user,
|
||||
"ERROR",
|
||||
req.user.email,
|
||||
req.body.billsToQuery,
|
||||
error
|
||||
{ error }
|
||||
);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
@@ -119,13 +124,6 @@ const generateBillLine = (billLine, responsibilityCenters, jobClass) => {
|
||||
};
|
||||
};
|
||||
|
||||
// [
|
||||
// {
|
||||
// AccountRef: { FullName: "BODY SHOP COST:SUBLET" },
|
||||
// Amount: invoice.amount,
|
||||
// },
|
||||
// ],
|
||||
|
||||
const findTaxCode = (billLine, taxcode) => {
|
||||
const {
|
||||
applicable_taxes: { local, state, federal },
|
||||
|
||||
@@ -29,6 +29,14 @@ exports.default = async (req, res) => {
|
||||
});
|
||||
|
||||
try {
|
||||
logger.log(
|
||||
"qbxml-payments-create",
|
||||
"DEBUG",
|
||||
req.user.email,
|
||||
req.body.paymentsToQuery,
|
||||
null
|
||||
);
|
||||
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_PAYMENTS_FOR_EXPORT, {
|
||||
@@ -85,7 +93,7 @@ exports.default = async (req, res) => {
|
||||
logger.log(
|
||||
"qbxml-payments-error",
|
||||
"error",
|
||||
req.body.user,
|
||||
req.user.email,
|
||||
req.body.paymentsToQuery,
|
||||
error
|
||||
);
|
||||
|
||||
@@ -29,6 +29,14 @@ exports.default = async (req, res) => {
|
||||
});
|
||||
|
||||
try {
|
||||
logger.log(
|
||||
"qbxml-receivables-create",
|
||||
"DEBUG",
|
||||
req.user.email,
|
||||
req.body.jobIds,
|
||||
null
|
||||
);
|
||||
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_JOBS_FOR_RECEIVABLES_EXPORT, { ids: jobIds });
|
||||
@@ -96,9 +104,9 @@ exports.default = async (req, res) => {
|
||||
res.status(200).json(QbXmlToExecute);
|
||||
} catch (error) {
|
||||
logger.log(
|
||||
"qbxml-payments-error",
|
||||
"qbxml-receivables-error",
|
||||
"error",
|
||||
req.body.user,
|
||||
req.user.email,
|
||||
req.body.jobIds,
|
||||
error
|
||||
);
|
||||
@@ -304,7 +312,7 @@ const generateInvoiceQbxml = (
|
||||
// console.log("Done creating hash", JSON.stringify(invoiceLineHash));
|
||||
|
||||
if (!hasMapaLine && jobs_by_pk.job_totals.rates.mapa.total.amount > 0) {
|
||||
console.log("Adding MAPA Line Manually.");
|
||||
// console.log("Adding MAPA Line Manually.");
|
||||
const mapaAccountName = responsibilityCenters.defaults.profits.MAPA;
|
||||
|
||||
const mapaAccount = responsibilityCenters.profits.find(
|
||||
@@ -329,7 +337,7 @@ const generateInvoiceQbxml = (
|
||||
}
|
||||
|
||||
if (!hasMashLine && jobs_by_pk.job_totals.rates.mash.total.amount > 0) {
|
||||
console.log("Adding MASH Line Manually.");
|
||||
// console.log("Adding MASH Line Manually.");
|
||||
|
||||
const mashAccountName = responsibilityCenters.defaults.profits.MASH;
|
||||
|
||||
@@ -350,7 +358,7 @@ const generateInvoiceQbxml = (
|
||||
},
|
||||
});
|
||||
} else {
|
||||
console.log("NO MASH ACCOUNT FOUND!!");
|
||||
// console.log("NO MASH ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user