Created export payables screen + basic invoice exporting BOD-139
This commit is contained in:
107
server/accounting/qbxml/qbxml-payables.js
Normal file
107
server/accounting/qbxml/qbxml-payables.js
Normal file
@@ -0,0 +1,107 @@
|
||||
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");
|
||||
const QbXmlUtils = require("./qbxml-utils");
|
||||
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 { invoices: invoicesToQuery } = req.body;
|
||||
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
Authorization: BearerToken,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_INVOICES_FOR_PAYABLES_EXPORT, {
|
||||
invoices: invoicesToQuery,
|
||||
});
|
||||
const { invoices } = result;
|
||||
|
||||
const QbXmlToExecute = [];
|
||||
invoices.map((i) => {
|
||||
QbXmlToExecute.push(generateBill(i));
|
||||
});
|
||||
|
||||
//For each invoice.
|
||||
|
||||
res.status(200).json(QbXmlToExecute);
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
|
||||
const generateBill = (invoice) => {
|
||||
const billQbxmlObj = {
|
||||
QBXML: {
|
||||
QBXMLMsgsRq: {
|
||||
"@onError": "continueOnError",
|
||||
BillAddRq: {
|
||||
BillAdd: {
|
||||
VendorRef: {
|
||||
FullName: invoice.vendor.name,
|
||||
},
|
||||
TxnDate: invoice.date,
|
||||
DueDate: invoice.due_date,
|
||||
RefNumber: invoice.invoice_number,
|
||||
Memo: `RO ${invoice.job.ro_number || ""} OWNER ${
|
||||
invoice.job.ownr_fn || ""
|
||||
} ${invoice.job.ownr_ln || ""} ${invoice.job.ownr_co_nm || ""}`,
|
||||
ExpenseLineAdd: invoice.invoicelines.map((il) =>
|
||||
generateBillLine(
|
||||
il,
|
||||
invoice.job.bodyshop.md_responsibility_centers
|
||||
)
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var billQbxml_partial = builder
|
||||
.create(billQbxmlObj, {
|
||||
version: "1.30",
|
||||
encoding: "UTF-8",
|
||||
headless: true,
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const billQbxml_Full = QbXmlUtils.addQbxmlHeader(billQbxml_partial);
|
||||
console.log("generateBill -> billQbxml_Full", billQbxml_Full);
|
||||
|
||||
return billQbxml_Full;
|
||||
};
|
||||
|
||||
const generateBillLine = (invoiceLine, responsibilityCenters) => {
|
||||
return {
|
||||
AccountRef: {
|
||||
FullName: responsibilityCenters.costs.find(
|
||||
(c) => c.name === invoiceLine.cost_center
|
||||
).accountname,
|
||||
},
|
||||
Amount: Dinero({
|
||||
amount: Math.round(invoiceLine.actual_cost * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
};
|
||||
};
|
||||
|
||||
// [
|
||||
// {
|
||||
// AccountRef: { FullName: "BODY SHOP COST:SUBLET" },
|
||||
// Amount: invoice.amount,
|
||||
// },
|
||||
// ],
|
||||
@@ -4,6 +4,7 @@ const DineroQbFormat = require("../accounting-constants").DineroQbFormat;
|
||||
const queries = require("../../graphql-client/queries");
|
||||
const Dinero = require("dinero.js");
|
||||
var builder = require("xmlbuilder");
|
||||
const QbXmlUtils = require("./qbxml-utils");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
@@ -77,7 +78,7 @@ const generateSourceCustomerQbxml = (jobs_by_pk, bodyshop) => {
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const customerQbxml_Full = addQbxmlHeader(customerQbxml_partial);
|
||||
const customerQbxml_Full = QbXmlUtils.addQbxmlHeader(customerQbxml_partial);
|
||||
|
||||
return customerQbxml_Full;
|
||||
};
|
||||
@@ -138,7 +139,7 @@ const generateJobQbxml = (jobs_by_pk, bodyshop, isThreeTier, tierLevel) => {
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const jobQbxml_Full = addQbxmlHeader(jobQbxml_partial);
|
||||
const jobQbxml_Full = QbXmlUtils.addQbxmlHeader(jobQbxml_partial);
|
||||
console.log("jobQbxml_Full", jobQbxml_Full);
|
||||
return jobQbxml_Full;
|
||||
};
|
||||
@@ -262,7 +263,7 @@ const generateInvoiceQbxml = (jobs_by_pk, bodyshop) => {
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const invoiceQbxml_Full = addQbxmlHeader(invoiceQbxml_partial);
|
||||
const invoiceQbxml_Full = QbXmlUtils.addQbxmlHeader(invoiceQbxml_partial);
|
||||
|
||||
return invoiceQbxml_Full;
|
||||
};
|
||||
@@ -291,10 +292,3 @@ const generateInvoiceLine = (job, allocation, responsibilityCenters) => {
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const addQbxmlHeader = (xml) => {
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<?qbxml version="13.0"?>
|
||||
${xml}
|
||||
`;
|
||||
};
|
||||
|
||||
6
server/accounting/qbxml/qbxml-utils.js
Normal file
6
server/accounting/qbxml/qbxml-utils.js
Normal file
@@ -0,0 +1,6 @@
|
||||
exports.addQbxmlHeader = addQbxmlHeader = (xml) => {
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<?qbxml version="13.0"?>
|
||||
${xml}
|
||||
`;
|
||||
};
|
||||
@@ -1 +1,2 @@
|
||||
exports.receivables = require("./qbxml-receivables").default
|
||||
exports.receivables = require("./qbxml-receivables").default;
|
||||
exports.payables = require("./qbxml-payables").default;
|
||||
|
||||
@@ -70,3 +70,41 @@ query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($id: uuid!) {
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.QUERY_INVOICES_FOR_PAYABLES_EXPORT = `
|
||||
query QUERY_INVOICES_FOR_PAYABLES_EXPORT($invoices: [uuid!]!) {
|
||||
invoices(where: {id: {_in: $invoices}}) {
|
||||
id
|
||||
date
|
||||
due_date
|
||||
federal_tax_rate
|
||||
invoice_number
|
||||
is_credit_memo
|
||||
job {
|
||||
id
|
||||
ro_number
|
||||
clm_no
|
||||
ownr_fn
|
||||
ownr_ln
|
||||
ownr_co_nm
|
||||
bodyshop{
|
||||
md_responsibility_centers
|
||||
}
|
||||
}
|
||||
invoicelines{
|
||||
id
|
||||
cost_center
|
||||
actual_cost
|
||||
applicable_taxes
|
||||
}
|
||||
state_tax_rate
|
||||
local_tax_rate
|
||||
total
|
||||
vendor{
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
`;
|
||||
|
||||
Reference in New Issue
Block a user