This commit is contained in:
Rizki
2026-04-23 16:03:39 +07:00
parent 7882040e56
commit c4aad30461

View File

@ -1071,34 +1071,55 @@ class ProcJustificationAdapter extends Adapter {
var apires = this.getApiResultDefined(); var apires = this.getApiResultDefined();
try { try {
let nik = req.body.nik; let nik = req.body.nik;
let vendors = JSON.parse(req.body.vendors); let idxprocjustification = req.body.idxprocjustification;
let allVendors = JSON.parse(req.body.vendors);
let existingVendors = allVendors.filter(function (v) { return v.vendor_idx; });
let newVendors = allVendors.filter(function (v) { return !v.vendor_idx; });
let boqList = []; if (allVendors.length === 0) {
vendors.forEach(function (vendor) {
if (vendor.boq && vendor.boq.length > 0) {
vendor.boq.forEach(function (boq) {
boqList.push(boq);
});
}
});
if (boqList.length === 0) {
apires.success = true; apires.success = true;
apires.meta.message = "No BOQ to update"; apires.meta.message = "No vendors";
callback(null, apires); callback(null, apires);
return; return;
} }
let pending = boqList.length; let total = allVendors.length;
boqList.forEach(function (boq) { let done = 0;
let qry = "update tbl_procjustificationvendorboq "; function checkDone() {
qry += "set items='" + boq.items + "',qty='" + boq.qty + "',unitprice='" + boq.unitprice + "',units='" + boq.units + "',total='" + boq.total + "',uby='" + nik + "',udt=now() "; if (++done === total) {
qry += "where _idx='" + boq._idx + "'"; apires.success = true;
db.query(qry, [], function (err) { apires.meta.message = "Saved";
if (--pending === 0) { callback(null, apires);
apires.success = true; }
apires.meta.message = "BOQ Updated"; }
callback(null, apires);
function insertBoq(idxvendor, boqList, onComplete) {
if (!boqList || boqList.length === 0) { onComplete(); return; }
let pending = boqList.length;
boqList.forEach(function (boq) {
let q = "insert into tbl_procjustificationvendorboq set idxprocjustificationvendor='" + idxvendor + "',items='" + (boq.items || '') + "',qty='" + (boq.qty || 0) + "',unitprice='" + (boq.unitprice || 0) + "',units='" + (boq.units || '') + "',total='" + (boq.total || 0) + "',iby='" + nik + "',idt=now()";
db.query(q, [], function () { if (--pending === 0) onComplete(); });
});
}
existingVendors.forEach(function (vendor) {
let qUpd = "update tbl_procjustificationvendor set name='" + (vendor.vendorname || '') + "',phone='" + (vendor.phone || '') + "',address='" + (vendor.address || '') + "'," + (vendor.currency_id ? "currency_id='" + vendor.currency_id + "'," : "") + "uby='" + nik + "',udt=now() where _idx='" + vendor.vendor_idx + "'";
db.query(qUpd, [], function () {
let qDel = "update tbl_procjustificationvendorboq set isdeleted=1,dby='" + nik + "',ddt=now() where idxprocjustificationvendor='" + vendor.vendor_idx + "' and isdeleted=0";
db.query(qDel, [], function () {
insertBoq(vendor.vendor_idx, vendor.boq, checkDone);
});
});
});
newVendors.forEach(function (vendor, i) {
let numberoffile = existingVendors.length + i + 1;
let qIns = "insert into tbl_procjustificationvendor set idxprocjustification='" + idxprocjustification + "',numberoffile='" + numberoffile + "',name='" + (vendor.vendorname || '') + "',address='" + (vendor.address || '') + "',phone='" + (vendor.phone || '') + "'," + (vendor.currency_id ? "currency_id='" + vendor.currency_id + "'," : "") + "iby='" + nik + "',idt=now()";
db.query(qIns, [], function (err, result) {
if (!err && result && result.insertId) {
insertBoq(result.insertId, vendor.boq, checkDone);
} else {
checkDone();
} }
}); });
}); });