Files
bodyshop/server/accounting/qbxml/qbxml-receivables.js
2020-05-28 15:42:02 -07:00

143 lines
3.9 KiB
JavaScript

const GraphQLClient = require("graphql-request").GraphQLClient;
const path = require("path");
const DineroQbFormat = require("../accounting-constants").DineroQbFormat;
const queries = require("../../graphql-client/queries");
const Dinero = require("dinero.js");
var builder = require("xmlbuilder");
require("dotenv").config({
path: path.resolve(
process.cwd(),
`.env.${process.env.NODE_ENV || "development"}`
),
});
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 XML file.
const InvoiceLineAdd = [];
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) => {
InvoiceLineAdd.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) => {
InvoiceLineAdd.push(
generateInvoiceLine(
jobs_by_pk,
alloc,
bodyshop.md_responsibility_centers
)
);
}
);
}
);
const foo = {
QBXML: {
QBXMLMsgsRq: {
"@onError": "stopOnError",
InvoiceAddRq: {
InvoiceAdd: {
CustomerRef: {
ListID: jobs_by_pk.owner.accountingid,
FullName: `${jobs_by_pk.ownr_ln}, ${jobs_by_pk.ownr_fn}`,
},
TxnDate: new Date(),
RefNumber: jobs_by_pk.ro_number,
BillAddress: {
Addr1: jobs_by_pk.ownr_addr1,
Addr2: jobs_by_pk.ownr_addr2,
City: jobs_by_pk.ownr_city,
State: jobs_by_pk.ownr_st,
PostalCode: jobs_by_pk.ownrzip,
},
// PONumber: "Ponumber",
InvoiceLineAdd: InvoiceLineAdd,
},
},
},
},
};
var requestXML = builder
.create(foo, { version: "1.30", encoding: "UTF-8", headless: true })
.end({ pretty: true });
const ret = `<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="13.0"?>
${requestXML}
`;
console.log(ret);
res.status(200).send(ret);
} catch (error) {
console.log("error", error);
res.status(400).send(JSON.stringify(error));
}
};
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 {
ItemRef: { FullName: account.accountitem },
Desc: account.accountdesc,
Quantity: 1,
//Rate: 100,
Amount: DineroAmount.toFormat(DineroQbFormat),
SalesTaxCodeRef: {
FullName: "Z",
},
};
};