Progress on qbxml import for BOD-83.

This commit is contained in:
Patrick Fic
2020-05-28 15:42:02 -07:00
parent 39bd89b232
commit a98e007141
8 changed files with 119 additions and 429 deletions

View File

@@ -0,0 +1 @@
exports.DineroQbFormat = "0,0.00";

View File

@@ -1,4 +1,5 @@
const GraphQLClient = require("graphql-request").GraphQLClient;
const DineroQbFormat = require("../accounting-constants").DineroQbFormat;
const path = require("path");
require("dotenv").config({
path: path.resolve(
@@ -159,8 +160,6 @@ const generateTaxLine = (job, amount, type, taxMapping) => {
const END_TRNS = `ENDTRNS`;
const DineroQbFormat = "0,0.0";
const generateJobInvoiceDate = (job) => {
return `${new Date(job.date_invoiced).getMonth() + 1}/${new Date(
job.date_invoiced

View File

@@ -1,16 +1,15 @@
const GraphQLClient = require("graphql-request").GraphQLClient;
const path = require("path");
var QuickBooks = require("node-quickbooks");
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"}`
),
});
const queries = require("../../graphql-client/queries");
const Dinero = require("dinero.js");
exports.default = async (req, res) => {
const BearerToken = req.headers.authorization;
@@ -31,86 +30,113 @@ exports.default = async (req, res) => {
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="CA13.0"?>
<?qbxml version="13.0"?>
${requestXML}
`;
console.log(ret);
//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.xml`);
//res.send(response.join("\n"));
res.status(200).send(ret);
} catch (error) {
console.log("error", error);
res.status(400).send(JSON.stringify(error));
}
};
const foo = {
QBXML: {
QBXMLMsgsRq: {
"@onError": "stopOnError",
InvoiceAddRq: {
InvoiceAdd: {
CustomerRef: {
FullName: "Full Name",
},
TxnDate: new Date(),
RefNumber: "RefNumber",
BillAddress: {
Addr1: "Address1",
Addr2: "Address2",
Addr3: "Address3",
Addr4: "Address4",
Addr5: "Address5",
City: "City",
State: "State",
PostalCode: "PostalCode",
Country: "Country",
Note: "Note",
},
PONumber: "Ponumber",
IsTaxIncluded: true,
// InvoiceLineAddList: {
InvoiceLineAdd: [
{
ItemRef: { FullName: "ItemRef FullName" },
Desc: "Description",
Quantity: 1,
//Rate: 100,
Amount: 100,
},
{
ItemRef: { FullName: "ItemRef FullName" },
Desc: "Description",
Quantity: 1,
//Rate: 100,
Amount: 100,
},
{
ItemRef: { FullName: "ItemRef FullName" },
Desc: "Description",
Quantity: 1,
//Rate: 100,
Amount: 100,
},
{
ItemRef: { FullName: "ItemRef FullName" },
Desc: "Description",
Quantity: 1,
//Rate: 100,
Amount: 100,
},
],
// },
},
},
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",
},
},
};
};

View File

@@ -50,11 +50,16 @@ query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($id: uuid!) {
clm_total
invoice_allocation
ownerid
ownr_ln
ownr_fn
ownr_addr1
ownr_addr2
ownr_zip
ownr_city
ownr_st
owner{
accountingid
}
bodyshop {
id
md_responsibility_centers