Added payment export BOD-147
This commit is contained in:
106
server/accounting/qbxml/qbxml-payments.js
Normal file
106
server/accounting/qbxml/qbxml-payments.js
Normal file
@@ -0,0 +1,106 @@
|
||||
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 moment = require("moment");
|
||||
const QbXmlUtils = require("./qbxml-utils");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
|
||||
const { generateJobTier, generateOwnerTier, generateSourceTier } = QbXmlUtils;
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { payments: paymentsToQuery } = req.body;
|
||||
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
Authorization: BearerToken,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_PAYMENTS_FOR_EXPORT, {
|
||||
payments: paymentsToQuery,
|
||||
});
|
||||
const { payments } = result;
|
||||
|
||||
const QbXmlToExecute = [];
|
||||
payments.map((i) => {
|
||||
QbXmlToExecute.push({
|
||||
id: i.id,
|
||||
okStatusCodes: ["0"],
|
||||
qbxml: generatePayment(i),
|
||||
});
|
||||
});
|
||||
|
||||
res.status(200).json(QbXmlToExecute);
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
|
||||
const generatePayment = (payment) => {
|
||||
console.log("generatePayment -> payment", payment);
|
||||
const paymentQbxmlObj = {
|
||||
QBXML: {
|
||||
QBXMLMsgsRq: {
|
||||
"@onError": "continueOnError",
|
||||
ReceivePaymentAddRq: {
|
||||
ReceivePaymentAdd: {
|
||||
CustomerRef: {
|
||||
FullName:
|
||||
payment.job.bodyshop.accountingconfig.tiers === 3
|
||||
? `${generateSourceTier(payment.job)}:${generateOwnerTier(
|
||||
payment.job
|
||||
)}:${generateJobTier(payment.job)}`
|
||||
: `${generateOwnerTier(payment.job)}:${generateJobTier(
|
||||
payment.job
|
||||
)}`,
|
||||
},
|
||||
ARAccountRef: {
|
||||
FullName:
|
||||
payment.job.bodyshop.md_responsibility_centers.ar.accountname,
|
||||
},
|
||||
TxnDate: moment(payment.created_at).format("YYYY-MM-DD"), //Trim String
|
||||
RefNumber: payment.stripeid || payment.transactionid,
|
||||
TotalAmount: Dinero({
|
||||
amount: Math.round(payment.amount * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
Memo: `RO ${payment.job.ro_number || ""} OWNER ${
|
||||
payment.job.ownr_fn || ""
|
||||
} ${payment.job.ownr_ln || ""} ${payment.job.ownr_co_nm || ""} ${
|
||||
payment.stripeid
|
||||
}`,
|
||||
IsAutoApply: true,
|
||||
// AppliedToTxnAdd:{
|
||||
// T
|
||||
// }
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var paymentQbxmlPartial = builder
|
||||
.create(paymentQbxmlObj, {
|
||||
version: "1.30",
|
||||
encoding: "UTF-8",
|
||||
headless: true,
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const paymentQbxmlFull = QbXmlUtils.addQbxmlHeader(paymentQbxmlPartial);
|
||||
console.log("generateBill -> paymentQbxmlFull", paymentQbxmlFull);
|
||||
|
||||
return paymentQbxmlFull;
|
||||
};
|
||||
@@ -12,6 +12,8 @@ require("dotenv").config({
|
||||
),
|
||||
});
|
||||
|
||||
const { generateJobTier, generateOwnerTier, generateSourceTier } = QbXmlUtils;
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { jobId } = req.body;
|
||||
@@ -72,224 +74,13 @@ exports.default = async (req, res) => {
|
||||
qbxml: generateInvoiceQbxml(jobs_by_pk, bodyshop),
|
||||
});
|
||||
|
||||
res.status(200).json([{ id: jobId, okStatusCodes: ["0"], qbxml: t }]);
|
||||
res.status(200).json(QbXmlToExecute);
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
|
||||
const t = `<?xml version="1.0" encoding="utf-8"?>
|
||||
<?qbxml version="13.0"?>
|
||||
<QBXML>
|
||||
<QBXMLMsgsRq onError="continueOnError">
|
||||
<CustomerAddRq>
|
||||
<CustomerAdd>
|
||||
<Name>Insurance Corporation of British Co</Name>
|
||||
<BillAddress>
|
||||
<Addr1>21291 122B AVE</Addr1>
|
||||
<City>MAPLE RIDGE</City>
|
||||
<State>BC</State>
|
||||
</BillAddress>
|
||||
</CustomerAdd>
|
||||
</CustomerAddRq>
|
||||
|
||||
<CustomerAddRq>
|
||||
<CustomerAdd>
|
||||
<Name>CLOVER LANDON #4</Name>
|
||||
<ParentRef>
|
||||
<FullName>Insurance Corporation of British Co</FullName>
|
||||
</ParentRef>
|
||||
</CustomerAdd>
|
||||
</CustomerAddRq>
|
||||
|
||||
<CustomerAddRq>
|
||||
<CustomerAdd>
|
||||
<Name>RO29</Name>
|
||||
<ParentRef>
|
||||
<FullName>Insurance Corporation of British Co:CLOVER LANDON #4</FullName>
|
||||
</ParentRef>
|
||||
</CustomerAdd>
|
||||
</CustomerAddRq>
|
||||
|
||||
<InvoiceAddRq>
|
||||
<InvoiceAdd>
|
||||
<CustomerRef>
|
||||
<FullName>Insurance Corporation of British Co:CLOVER LANDON #4:RO29</FullName>
|
||||
</CustomerRef>
|
||||
<TxnDate/>
|
||||
<RefNumber>RO29</RefNumber>
|
||||
<BillAddress>
|
||||
<Addr1>21291 122B AVE</Addr1>
|
||||
<City>MAPLE RIDGE</City>
|
||||
<State>BC</State>
|
||||
</BillAddress>
|
||||
<PONumber>BM27914-1-A</PONumber>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_PAA</FullName>
|
||||
</ItemRef>
|
||||
<Desc>Aftermarketd</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>1351.03</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_PAN</FullName>
|
||||
</ItemRef>
|
||||
<Desc>BODY SHOP SALES:PARTS:OEM</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>292.45</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_ATP</FullName>
|
||||
</ItemRef>
|
||||
<Desc>ATPd</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>144.09</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_LAB</FullName>
|
||||
</ItemRef>
|
||||
<Desc>BODY SHOP SALESLABOR:BODY</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>653.35</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_LAR</FullName>
|
||||
</ItemRef>
|
||||
<Desc>BODY SHOP SALES:LABOR:REFINISH</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>565.26</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_MAPA</FullName>
|
||||
</ItemRef>
|
||||
<Desc>paintd</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>347.66</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>BODY SHOP_MASH</FullName>
|
||||
</ItemRef>
|
||||
<Desc>shopd</Desc>
|
||||
<Quantity>1</Quantity>
|
||||
<Amount>54.38</Amount>
|
||||
<SalesTaxCodeRef>
|
||||
<FullName>E</FullName>
|
||||
</SalesTaxCodeRef>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>GST On Sales</FullName>
|
||||
</ItemRef>
|
||||
<Desc>Receiver General - GST</Desc>
|
||||
<Amount>170.41</Amount>
|
||||
</InvoiceLineAdd>
|
||||
<InvoiceLineAdd>
|
||||
<ItemRef>
|
||||
<FullName>PST On Sales</FullName>
|
||||
</ItemRef>
|
||||
<Desc>Ministry of Finance (BC)</Desc>
|
||||
<Amount>238.58</Amount>
|
||||
</InvoiceLineAdd>
|
||||
</InvoiceAdd>
|
||||
</InvoiceAddRq>
|
||||
</QBXMLMsgsRq>
|
||||
|
||||
</QBXML>
|
||||
`;
|
||||
|
||||
// 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;
|
||||
// const QbXmlToExecute = [];
|
||||
|
||||
// //Is this a two tier, or 3 tier setup?
|
||||
// const isThreeTier = bodyshop.accountingconfig.tiers === 3;
|
||||
// const twoTierPref = bodyshop.accountingconfig.twotierpref;
|
||||
|
||||
// if (isThreeTier) {
|
||||
// QbXmlToExecute.push({
|
||||
// id: jobId,
|
||||
// okStatusCodes: ["0", "3100"],
|
||||
// qbxml: generateSourceCustomerQbxml(jobs_by_pk, bodyshop), // Create the source customer.
|
||||
// });
|
||||
// }
|
||||
|
||||
// QbXmlToExecute.push({
|
||||
// id: jobId,
|
||||
// okStatusCodes: ["0", "3100"],
|
||||
// qbxml: generateJobQbxml(
|
||||
// jobs_by_pk,
|
||||
// bodyshop,
|
||||
// isThreeTier,
|
||||
// 2,
|
||||
// twoTierPref
|
||||
// ),
|
||||
// });
|
||||
|
||||
// QbXmlToExecute.push({
|
||||
// id: jobId,
|
||||
// okStatusCodes: ["0", "3100"],
|
||||
// qbxml: generateJobQbxml(
|
||||
// jobs_by_pk,
|
||||
// bodyshop,
|
||||
// isThreeTier,
|
||||
// 3,
|
||||
// twoTierPref
|
||||
// ),
|
||||
// });
|
||||
// //Generate the actual invoice.
|
||||
// QbXmlToExecute.push({
|
||||
// id: jobId,
|
||||
// okStatusCodes: ["0"],
|
||||
// qbxml: generateInvoiceQbxml(jobs_by_pk, bodyshop),
|
||||
// });
|
||||
|
||||
// res.status(200).json(QbXmlToExecute);
|
||||
// } catch (error) {
|
||||
// console.log("error", error);
|
||||
// res.status(400).send(JSON.stringify(error));
|
||||
// }
|
||||
// };
|
||||
|
||||
const generateSourceCustomerQbxml = (jobs_by_pk, bodyshop) => {
|
||||
const customerQbxmlObj = {
|
||||
QBXML: {
|
||||
@@ -324,22 +115,6 @@ const generateSourceCustomerQbxml = (jobs_by_pk, bodyshop) => {
|
||||
return customerQbxml_Full;
|
||||
};
|
||||
|
||||
const generateSourceTier = (jobs_by_pk) => {
|
||||
return jobs_by_pk.ins_co_nm;
|
||||
};
|
||||
const generateJobTier = (jobs_by_pk) => {
|
||||
return jobs_by_pk.ro_number;
|
||||
};
|
||||
const generateOwnerTier = (jobs_by_pk) => {
|
||||
return jobs_by_pk.ownr_co_nm
|
||||
? `${jobs_by_pk.ownr_co_nm} - ${jobs_by_pk.ownr_ln || ""} ${
|
||||
jobs_by_pk.ownr_fn || ""
|
||||
} #${jobs_by_pk.owner.accountingid || ""}`
|
||||
: `${jobs_by_pk.ownr_ln || ""} ${jobs_by_pk.ownr_fn || ""} #${
|
||||
jobs_by_pk.owner.accountingid || ""
|
||||
}`;
|
||||
};
|
||||
|
||||
const generateJobQbxml = (
|
||||
jobs_by_pk,
|
||||
bodyshop,
|
||||
|
||||
@@ -4,3 +4,21 @@ exports.addQbxmlHeader = addQbxmlHeader = (xml) => {
|
||||
${xml}
|
||||
`;
|
||||
};
|
||||
|
||||
exports.generateSourceTier = (jobs_by_pk) => {
|
||||
return jobs_by_pk.ins_co_nm;
|
||||
};
|
||||
|
||||
exports.generateJobTier = (jobs_by_pk) => {
|
||||
return jobs_by_pk.ro_number;
|
||||
};
|
||||
|
||||
exports.generateOwnerTier = (jobs_by_pk) => {
|
||||
return jobs_by_pk.ownr_co_nm
|
||||
? `${jobs_by_pk.ownr_co_nm} - ${jobs_by_pk.ownr_ln || ""} ${
|
||||
jobs_by_pk.ownr_fn || ""
|
||||
} #${jobs_by_pk.owner.accountingid || ""}`
|
||||
: `${jobs_by_pk.ownr_ln || ""} ${jobs_by_pk.ownr_fn || ""} #${
|
||||
jobs_by_pk.owner.accountingid || ""
|
||||
}`;
|
||||
};
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
exports.receivables = require("./qbxml-receivables").default;
|
||||
exports.payables = require("./qbxml-payables").default;
|
||||
exports.payments = require("./qbxml-payments").default;
|
||||
|
||||
@@ -106,9 +106,40 @@ query QUERY_INVOICES_FOR_PAYABLES_EXPORT($invoices: [uuid!]!) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
`;
|
||||
|
||||
exports.QUERY_PAYMENTS_FOR_EXPORT = `
|
||||
query QUERY_PAYMENTS_FOR_EXPORT($payments: [uuid!]!) {
|
||||
payments(where: {id: {_in: $payments}}) {
|
||||
id
|
||||
created_at
|
||||
jobid
|
||||
job {
|
||||
id
|
||||
ro_number
|
||||
est_number
|
||||
ins_co_nm
|
||||
owner{
|
||||
accountingid
|
||||
}
|
||||
ownr_fn
|
||||
ownr_ln
|
||||
ownr_co_nm
|
||||
bodyshop{
|
||||
accountingconfig
|
||||
md_responsibility_centers
|
||||
}
|
||||
}
|
||||
transactionid
|
||||
memo
|
||||
amount
|
||||
stripeid
|
||||
exportedat
|
||||
stripeid
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.QUERY_UPCOMING_APPOINTMENTS = `
|
||||
query QUERY_UPCOMING_APPOINTMENTS($now: timestamptz!, $jobId: uuid!) {
|
||||
jobs_by_pk(id: $jobId) {
|
||||
|
||||
Reference in New Issue
Block a user