diff --git a/adapter/procjustificationadapter.js b/adapter/procjustificationadapter.js index 8fa62e3..d843292 100644 --- a/adapter/procjustificationadapter.js +++ b/adapter/procjustificationadapter.js @@ -1071,34 +1071,55 @@ class ProcJustificationAdapter extends Adapter { var apires = this.getApiResultDefined(); try { 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 = []; - vendors.forEach(function (vendor) { - if (vendor.boq && vendor.boq.length > 0) { - vendor.boq.forEach(function (boq) { - boqList.push(boq); - }); - } - }); - - if (boqList.length === 0) { + if (allVendors.length === 0) { apires.success = true; - apires.meta.message = "No BOQ to update"; + apires.meta.message = "No vendors"; callback(null, apires); return; } - let pending = boqList.length; - boqList.forEach(function (boq) { - let qry = "update tbl_procjustificationvendorboq "; - qry += "set items='" + boq.items + "',qty='" + boq.qty + "',unitprice='" + boq.unitprice + "',units='" + boq.units + "',total='" + boq.total + "',uby='" + nik + "',udt=now() "; - qry += "where _idx='" + boq._idx + "'"; - db.query(qry, [], function (err) { - if (--pending === 0) { - apires.success = true; - apires.meta.message = "BOQ Updated"; - callback(null, apires); + let total = allVendors.length; + let done = 0; + function checkDone() { + if (++done === total) { + apires.success = true; + apires.meta.message = "Saved"; + 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(); } }); });