Refactoring for 3tier setup BOD-83
This commit is contained in:
@@ -25,97 +25,195 @@ exports.default = async (req, res) => {
|
||||
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 QbXmlToExecute = [];
|
||||
|
||||
const InvoiceLineAdd = [];
|
||||
const invoice_allocation = jobs_by_pk.invoice_allocation;
|
||||
Object.keys(invoice_allocation.partsAllocations).forEach(
|
||||
(partsAllocationKey) => {
|
||||
if (
|
||||
!!!invoice_allocation.partsAllocations[partsAllocationKey].allocations
|
||||
)
|
||||
return;
|
||||
//Is this a two tier, or 3 tier setup?
|
||||
const isThreeTier = bodyshop.accountingconfig.tiers === 3;
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
QbXmlToExecute.push(
|
||||
generateCustomerQbxml(jobs_by_pk, bodyshop, isThreeTier)
|
||||
);
|
||||
|
||||
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",
|
||||
if (isThreeTier) {
|
||||
QbXmlToExecute.push(generateJobQbxml(jobs_by_pk, bodyshop, 2));
|
||||
QbXmlToExecute.push(generateJobQbxml(jobs_by_pk, bodyshop, 3));
|
||||
}
|
||||
|
||||
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);
|
||||
console.log(QbXmlToExecute);
|
||||
res.status(200).json(QbXmlToExecute);
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
|
||||
const generateCustomerQbxml = (jobs_by_pk, bodyshop, isThreeTier) => {
|
||||
const customerQbxmlObj = {
|
||||
QBXML: {
|
||||
QBXMLMsgsRq: {
|
||||
"@onError": "continueOnError",
|
||||
CustomerAddRq: {
|
||||
CustomerAdd: {
|
||||
Name: isThreeTier
|
||||
? jobs_by_pk.ins_co_nm
|
||||
: 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 || ""
|
||||
}`,
|
||||
BillAddress: isThreeTier
|
||||
? null
|
||||
: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var customerQbxml_partial = builder
|
||||
.create(customerQbxmlObj, {
|
||||
version: "1.30",
|
||||
encoding: "UTF-8",
|
||||
headless: true,
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const customerQbxml_Full = addQbxmlHeader(customerQbxml_partial);
|
||||
|
||||
return customerQbxml_Full;
|
||||
};
|
||||
|
||||
const generateJobQbxml = (jobs_by_pk, bodyshop, isThreeTier, tierLevel) => {
|
||||
const tier1Name = jobs_by_pk.ownr_co_nm;
|
||||
const tier2Name = 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 jobQbxmlObj = {
|
||||
QBXML: {
|
||||
QBXMLMsgsRq: {
|
||||
"@onError": "continueOnError",
|
||||
CustomerAddRq: {
|
||||
CustomerAdd: {
|
||||
Name: tierLevel === 2 ? null : null,
|
||||
ParentRef: {
|
||||
FullName: tierLevel === 2 ? null : null,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var jobQbxml_partial = builder
|
||||
.create(jobQbxmlObj, {
|
||||
version: "1.30",
|
||||
encoding: "UTF-8",
|
||||
headless: true,
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const jobQbxml_Full = addQbxmlHeader(jobQbxml_partial);
|
||||
return jobQbxml_Full;
|
||||
};
|
||||
|
||||
const generateInvoiceQbxml = (jobs_by_pk, bodyshop) => {
|
||||
//Build the Invoice 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 invoiceQbxmlObj = {
|
||||
QBXML: {
|
||||
QBXMLMsgsRq: {
|
||||
"@onError": "stopOnError",
|
||||
InvoiceAddRq: {
|
||||
InvoiceAdd: {
|
||||
CustomerRef: {
|
||||
//This can equal the Customer or the Customer Job.
|
||||
FullName:
|
||||
bodyshop.accountingconfig.tiers === 3
|
||||
? "3tier"
|
||||
: bodyshop.accountingconfig.twotierpref === "name"
|
||||
? "2tiername"
|
||||
: "2tiersource",
|
||||
},
|
||||
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: jobs_by_pk.clm_no,
|
||||
InvoiceLineAdd: InvoiceLineAdd,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
var invoiceQbxml_partial = builder
|
||||
.create(invoiceQbxmlObj, {
|
||||
version: "1.30",
|
||||
encoding: "UTF-8",
|
||||
headless: true,
|
||||
})
|
||||
.end({ pretty: true });
|
||||
|
||||
const invoiceQbxml_Full = addQbxmlHeader(invoiceQbxml_partial);
|
||||
|
||||
return invoiceQbxml_Full;
|
||||
};
|
||||
|
||||
const generateInvoiceLine = (job, allocation, responsibilityCenters) => {
|
||||
const { amount, center } = allocation;
|
||||
const DineroAmount = Dinero(amount);
|
||||
@@ -140,3 +238,10 @@ const generateInvoiceLine = (job, allocation, responsibilityCenters) => {
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const addQbxmlHeader = (xml) => {
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<?qbxml version="13.0"?>
|
||||
${xml}
|
||||
`;
|
||||
};
|
||||
|
||||
@@ -57,12 +57,14 @@ query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($id: uuid!) {
|
||||
ownr_zip
|
||||
ownr_city
|
||||
ownr_st
|
||||
ins_co_nm
|
||||
owner{
|
||||
accountingid
|
||||
}
|
||||
bodyshop {
|
||||
id
|
||||
md_responsibility_centers
|
||||
accountingconfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user