upd
This commit is contained in:
@ -1081,7 +1081,7 @@ class PoAdapter extends Adapter {
|
|||||||
qry = "select v.*, po.currency_id, po.rate_snapshot, c.currency AS currency_code, c.symbol AS currency_symbol "
|
qry = "select v.*, po.currency_id, po.rate_snapshot, c.currency AS currency_code, c.symbol AS currency_symbol "
|
||||||
+ "from vw_po v left join tbl_po po on po._idx = v._idx "
|
+ "from vw_po v left join tbl_po po on po._idx = v._idx "
|
||||||
+ "left join tbl_currency c on c._idx = po.currency_id "
|
+ "left join tbl_currency c on c._idx = po.currency_id "
|
||||||
+ "where v._idx='" + idxpo + "'";
|
+ "where v._idx='" + idxheader + "'";
|
||||||
db.query(qry, [], function (err, result1, fields) {
|
db.query(qry, [], function (err, result1, fields) {
|
||||||
if (err) {
|
if (err) {
|
||||||
apires.meta["message"] = err.toString();
|
apires.meta["message"] = err.toString();
|
||||||
@ -1416,11 +1416,92 @@ class PoAdapter extends Adapter {
|
|||||||
|
|
||||||
parseBoqIds(idxpoboq) {
|
parseBoqIds(idxpoboq) {
|
||||||
return String(idxpoboq || "")
|
return String(idxpoboq || "")
|
||||||
.split(",")
|
.split(/[,|]/)
|
||||||
.map((id) => id.trim())
|
.map((id) => id.trim())
|
||||||
.filter((id) => id && id !== "0");
|
.filter((id) => id && id !== "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolvePoBoqIds(idxpo, idxpoboq, callback) {
|
||||||
|
const self = this;
|
||||||
|
const requested = self.parseBoqIds(idxpoboq);
|
||||||
|
const headerId = String(idxpo || "").trim();
|
||||||
|
|
||||||
|
const queryBoq = (sql, next) => {
|
||||||
|
db.query(sql, [], function (err, rows) {
|
||||||
|
if (err) {
|
||||||
|
callback(err, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
next(rows || []);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const finish = (resolvedPo, resolvedIds) => {
|
||||||
|
callback(null, {
|
||||||
|
idxpo: String(resolvedPo),
|
||||||
|
boqIds: resolvedIds.map((id) => String(id)),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const tryAllUnderHeader = () => {
|
||||||
|
if (!headerId) {
|
||||||
|
finish(headerId, requested);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
queryBoq(
|
||||||
|
"select _idx from tbl_poboq where idxheader='" +
|
||||||
|
headerId +
|
||||||
|
"' and isdeleted=0",
|
||||||
|
(rows) => {
|
||||||
|
if (rows.length > 0) {
|
||||||
|
finish(headerId, rows.map((row) => row._idx));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finish(headerId, requested);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const tryByIdsOnly = () => {
|
||||||
|
if (requested.length === 0) {
|
||||||
|
tryAllUnderHeader();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
queryBoq(
|
||||||
|
"select _idx, idxheader from tbl_poboq where _idx in (" +
|
||||||
|
requested.join(",") +
|
||||||
|
") and isdeleted=0",
|
||||||
|
(rows) => {
|
||||||
|
if (rows.length > 0) {
|
||||||
|
finish(rows[0].idxheader, rows.map((row) => row._idx));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tryAllUnderHeader();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!headerId) {
|
||||||
|
tryByIdsOnly();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sql =
|
||||||
|
"select _idx from tbl_poboq where idxheader='" +
|
||||||
|
headerId +
|
||||||
|
"' and isdeleted=0";
|
||||||
|
if (requested.length > 0) {
|
||||||
|
sql += " and _idx in (" + requested.join(",") + ")";
|
||||||
|
}
|
||||||
|
queryBoq(sql, (rows) => {
|
||||||
|
if (rows.length > 0) {
|
||||||
|
finish(headerId, rows.map((row) => row._idx));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tryByIdsOnly();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async queryUpdatePoNumberold(req, posapnumber, sapmessage, callback) {
|
async queryUpdatePoNumberold(req, posapnumber, sapmessage, callback) {
|
||||||
try {
|
try {
|
||||||
var apires = this.getApiResultDefined();
|
var apires = this.getApiResultDefined();
|
||||||
@ -1428,24 +1509,39 @@ class PoAdapter extends Adapter {
|
|||||||
var idxpoboq = req.body.idxpoboq;
|
var idxpoboq = req.body.idxpoboq;
|
||||||
var nik = req.nik || req.body.nik || "";
|
var nik = req.nik || req.body.nik || "";
|
||||||
const poNumber = String(posapnumber || "").trim();
|
const poNumber = String(posapnumber || "").trim();
|
||||||
// status 1 = Create PO SAP — required for approval-po list (filters status 1-6)
|
|
||||||
const finalStatus = req.body.status ?? 1;
|
const finalStatus = req.body.status ?? 1;
|
||||||
const finalStatusDescription =
|
const finalStatusDescription =
|
||||||
req.body.statusdescription || "Create PO SAP";
|
req.body.statusdescription || "Create PO SAP";
|
||||||
const safeSapMessage = String(sapmessage || "").replace(/'/g, "''");
|
const safeSapMessage = String(sapmessage || "").replace(/'/g, "''");
|
||||||
const safePoNumber = poNumber.replace(/'/g, "''");
|
const safePoNumber = poNumber.replace(/'/g, "''");
|
||||||
|
|
||||||
if (!idxpo || !poNumber || poNumber === "000") {
|
if (!poNumber || poNumber === "000") {
|
||||||
apires.meta.code = 500;
|
apires.meta.code = 500;
|
||||||
apires.meta.message = "Invalid PO header or SAP PO number";
|
apires.meta.message = "Invalid SAP PO number";
|
||||||
callback("err", apires);
|
callback("err", apires);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
let boqIds = self.parseBoqIds(idxpoboq);
|
|
||||||
|
|
||||||
const persistPoNumber = (resolvedBoqIds) => {
|
self.resolvePoBoqIds(idxpo, idxpoboq, function (errResolve, resolved) {
|
||||||
|
if (errResolve) {
|
||||||
|
apires.meta.code = 500;
|
||||||
|
apires.meta.message = errResolve.toString();
|
||||||
|
callback("err", apires);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedPo = resolved.idxpo;
|
||||||
|
const resolvedBoqIds = resolved.boqIds;
|
||||||
|
|
||||||
|
if (!resolvedPo) {
|
||||||
|
apires.meta.code = 500;
|
||||||
|
apires.meta.message = "PO header not found for update";
|
||||||
|
callback("err", apires);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const qryPo =
|
const qryPo =
|
||||||
"update tbl_po set ponumber='" +
|
"update tbl_po set ponumber='" +
|
||||||
safePoNumber +
|
safePoNumber +
|
||||||
@ -1458,7 +1554,7 @@ class PoAdapter extends Adapter {
|
|||||||
"',uby='" +
|
"',uby='" +
|
||||||
nik +
|
nik +
|
||||||
"',udt=now() where _idx='" +
|
"',udt=now() where _idx='" +
|
||||||
idxpo +
|
resolvedPo +
|
||||||
"' and isdeleted=0";
|
"' and isdeleted=0";
|
||||||
|
|
||||||
db.query(qryPo, [], function (err, result) {
|
db.query(qryPo, [], function (err, result) {
|
||||||
@ -1486,7 +1582,7 @@ class PoAdapter extends Adapter {
|
|||||||
"',uby='" +
|
"',uby='" +
|
||||||
nik +
|
nik +
|
||||||
"',udt=now() where idxheader='" +
|
"',udt=now() where idxheader='" +
|
||||||
idxpo +
|
resolvedPo +
|
||||||
"' and isdeleted=0";
|
"' and isdeleted=0";
|
||||||
if (resolvedBoqIds.length > 0) {
|
if (resolvedBoqIds.length > 0) {
|
||||||
qryBoq += " and _idx in (" + resolvedBoqIds.join(",") + ")";
|
qryBoq += " and _idx in (" + resolvedBoqIds.join(",") + ")";
|
||||||
@ -1510,11 +1606,11 @@ class PoAdapter extends Adapter {
|
|||||||
|
|
||||||
let qrySelect =
|
let qrySelect =
|
||||||
resolvedBoqIds.length > 0
|
resolvedBoqIds.length > 0
|
||||||
? "select idxpoboq,ponumber from vw_poboq where idxpoboq in(" +
|
? "select _idx as idxpoboq, ponumber from tbl_poboq where _idx in(" +
|
||||||
resolvedBoqIds.join(",") +
|
resolvedBoqIds.join(",") +
|
||||||
")"
|
") and isdeleted=0"
|
||||||
: "select _idx as idxpoboq, ponumber from tbl_poboq where idxheader='" +
|
: "select _idx as idxpoboq, ponumber from tbl_poboq where idxheader='" +
|
||||||
idxpo +
|
resolvedPo +
|
||||||
"' and isdeleted=0";
|
"' and isdeleted=0";
|
||||||
|
|
||||||
db.query(qrySelect, [], function (errSelect, result1) {
|
db.query(qrySelect, [], function (errSelect, result1) {
|
||||||
@ -1530,7 +1626,7 @@ class PoAdapter extends Adapter {
|
|||||||
if (apires.data.length === 0) {
|
if (apires.data.length === 0) {
|
||||||
apires.data = [
|
apires.data = [
|
||||||
{
|
{
|
||||||
idxpoboq: resolvedBoqIds[0] || idxpo,
|
idxpoboq: resolvedBoqIds[0] || resolvedPo,
|
||||||
ponumber: poNumber,
|
ponumber: poNumber,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
@ -1543,32 +1639,7 @@ class PoAdapter extends Adapter {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
|
||||||
if (boqIds.length > 0) {
|
|
||||||
persistPoNumber(boqIds);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.query(
|
|
||||||
"select idxpoboq from tbl_po where _idx='" +
|
|
||||||
idxpo +
|
|
||||||
"' and isdeleted=0 limit 1",
|
|
||||||
[],
|
|
||||||
function (errLookup, rows) {
|
|
||||||
if (errLookup) {
|
|
||||||
apires.meta.message = errLookup.toString();
|
|
||||||
apires.meta.code = 500;
|
|
||||||
callback("err", apires);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rows && rows.length > 0 && rows[0].idxpoboq) {
|
|
||||||
boqIds = self.parseBoqIds(rows[0].idxpoboq);
|
|
||||||
}
|
|
||||||
persistPoNumber(boqIds);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
apires.meta.code = 500;
|
apires.meta.code = 500;
|
||||||
apires.meta.message = err.toString();
|
apires.meta.message = err.toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user