Merge branch 'develop' into dev
All checks were successful
Build & Deploy on Dev / build (push) Successful in 48s

This commit is contained in:
harid 2025-12-12 09:57:44 +07:00
commit 7bc7645443
2 changed files with 380 additions and 100 deletions

View file

@ -489,7 +489,7 @@ export class SalaryPeriodController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง");
}
let type = salaryProfile.type;
//SalaryRank
//SalaryRanks
let salaryRanks: any = null;
if (salaryProfile.amount != null) {
salaryRanks = await this.salaryRankRepository.findOne({
@ -957,7 +957,7 @@ export class SalaryPeriodController extends Controller {
let _null: any = null;
salaryProfile.remark = body.remark == null ? _null : body.remark;
let type = salaryProfile.type;
//SalaryRank
//SalaryRanks
let salaryRanks: any = null;
if (salaryProfile.amount != null) {
salaryRanks = await this.salaryRankRepository.findOne({
@ -1200,6 +1200,7 @@ export class SalaryPeriodController extends Controller {
return new HttpSuccess();
}
//OLD CHANGE TYPE-MULTI
/**
* API
*
@ -1293,7 +1294,7 @@ export class SalaryPeriodController extends Controller {
let _null: any = null;
salaryProfile.remark = body.remark == null ? _null : body.remark;
let type = salaryProfile.type;
//SalaryRank
//SalaryRanks
let salaryRanks: any = null;
if (salaryProfile.amount != null) {
salaryRanks = await this.salaryRankRepository.findOne({
@ -1543,6 +1544,239 @@ export class SalaryPeriodController extends Controller {
return new HttpSuccess();
}
//NEW CHANGE TYPE-MULTI
@Post("newchange/type-multi")
async newchangeTypeMulti(
@Body() body: { profileId: string[]; type: string; isReserve: boolean; remark?: string | null },
@Request() req: RequestWithUser,
) {
await new permission().PermissionCreate(req, "SYS_SALARY_OFFICER");
// -----------------------------
// 1) ดึง salaryProfiles ทีเดียว
// -----------------------------
const salaryProfiles = await this.salaryProfileRepository.find({
where: { id: In(body.profileId) },
relations: ["salaryOrg", "salaryOrg.salaryPeriod"],
});
if (!salaryProfiles.length) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล salaryProfile");
}
// -----------------------------
// 2) โหลด PosType, PosLevel, Salary, SalaryRanks ทั้งหมดทีเดียว
// -----------------------------
const posTypes = await this.posTypeRepository.find();
const posLevels = await this.posLevelRepository.find();
const salaries = await this.salaryRepository.find({ where: { isActive: true } });
const salaryRanks = await this.salaryRankRepository.find();
// Map lookup
const posTypeMap = new Map(posTypes.map((x) => [x.posTypeName, x]));
const posLevelMap = new Map(posLevels.map((x) => [`${x.posTypeId}|${x.posLevelName}`, x]));
const salaryMap = new Map(
salaries.map((x) => [`${x.posTypeId}|${x.posLevelId}|${x.isSpecial ? 1 : 0}`, x]),
);
const ranksBySalaryId = salaryRanks.reduce(
(acc, r: any) => {
if (!acc[r.salaryId]) acc[r.salaryId] = [];
acc[r.salaryId].push(r);
return acc;
},
{} as Record<number, SalaryRanks[]>,
);
const profilesToSave: SalaryProfile[] = [];
const orgNeedRecalc = new Set<string>();
for (const profile of salaryProfiles) {
const bodyType = body.type?.toUpperCase() ?? profile.type;
// --- ตรวจ FULLHAFT → หา APR snapshot2 ---
if (bodyType === "FULLHAFT" && profile.salaryOrg.salaryPeriod.period === "OCT") {
const checkPrev = await this.salaryProfileRepository.findOne({
relations: ["salaryOrg", "salaryOrg.salaryPeriod"],
where: {
citizenId: profile.citizenId,
salaryOrg: {
salaryPeriod: { period: "APR", year: profile.salaryOrg.salaryPeriod.year },
snapshot: "SNAP2",
},
type: "FULL",
},
});
if (checkPrev) {
throw new HttpError(404, "ไม่สามารถเลื่อนขั้นเกิน 2 ครั้งในปีเดียวกันได้");
}
}
// --- apply posType/posLevel/salary ---
const posType = posTypeMap.get(profile.posType);
if (!posType) throw new HttpError(404, "ไม่พบประเภทตำแหน่ง");
const posLevel = posLevelMap.get(`${posType.id}|${profile.posLevel}`);
if (!posLevel) throw new HttpError(404, "ไม่พบระดับตำแหน่ง");
const salaryBase = salaryMap.get(
`${posLevel.posTypeId}|${posLevel.id}|${profile.isSpecial ? 1 : 0}`,
);
if (!salaryBase) throw new HttpError(404, "ไม่พบระดับเงินเดือน");
const salaryId = Number(salaryBase.id);
const ranks = ranksBySalaryId[salaryId] ?? [];
// --- หา rank ตาม amount ก่อน dynamic type adjustment ---
let rank: SalaryRanks | null = null;
if (profile.amount != null) {
const amount = profile.amount;
const possible = ranks
.filter((r: any) => r.salary >= amount && !r.isNext)
.sort((a: any, b: any) => a.salary - b.salary);
rank = possible[0] ?? null;
if (!rank) {
const next = ranks
.filter((r: any) => r.salary > amount && r.isNext)
.sort((a: any, b: any) => a.salary - b.salary);
rank = next[0] ?? null;
}
}
// --- คำนวณเงินเดือนตาม rank เดิมก่อน dynamic type adjustment ---
const calc = (sp: keyof SalaryRanks, next: keyof SalaryRanks) => ({
amountSpecial: rank?.[sp] ?? 0,
amountUse:
profile.amount != null && rank?.[next] != null
? Number(rank[next]) - Number(profile.amount)
: 0,
positionSalaryAmount: rank?.[next] ?? 0,
isNext: rank?.isNext ?? 0,
});
// --- FULLHAFT dynamic type adjustment หลังคำนวณเงินเดือน ---
let finalType = bodyType;
if (bodyType === "FULLHAFT" && rank) {
const halfSpecial = rank.salaryHalfSpecial ?? 0;
const fullSpecial = rank.salaryFullSpecial ?? 0;
const fullHalfSpecial = rank.salaryFullHalfSpecial ?? 0;
if (fullHalfSpecial > 0) {
if (fullSpecial === 0) finalType = "HAFT";
else if (halfSpecial === 0) finalType = "FULL";
else finalType = "FULLHAFT";
}
}
profile.type = finalType;
profile.isReserve = finalType === "FULL" ? body.isReserve : false;
profile.remark = body.remark ?? "";
if (finalType === "NONE") {
profile.amountSpecial = 0;
profile.amountUse = 0;
profile.positionSalaryAmount = profile.amount ?? 0;
} else if (finalType === "PENDING") {
profile.amountSpecial = 0;
profile.amountUse = 0;
profile.positionSalaryAmount = 0;
} else if (finalType === "HAFT") {
Object.assign(profile, calc("salaryHalfSpecial", "salaryHalf"));
} else if (finalType === "FULL") {
Object.assign(profile, calc("salaryFullSpecial", "salaryFull"));
} else if (finalType === "FULLHAFT") {
Object.assign(profile, calc("salaryFullHalfSpecial", "salaryFullHalf"));
}
profile.lastUpdateUserId = req.user.sub;
profile.lastUpdateFullName = req.user.name;
profile.lastUpdatedAt = new Date();
// --- log diff ---
const before = structuredClone(profile);
profilesToSave.push(profile);
setLogDataDiff(req, { before, after: profile });
orgNeedRecalc.add(profile.salaryOrg.id);
}
// -----------------------------
// 4) Save batch
// -----------------------------
await this.salaryProfileRepository.save(profilesToSave);
// -----------------------------
// 5) Recalculate SalaryOrg
// -----------------------------
for (const orgId of orgNeedRecalc) {
const org = await this.salaryOrgRepository.findOne({
where: { id: orgId },
relations: ["salaryProfiles", "salaryPeriod"],
});
if (!org) continue;
const beforeOrg = structuredClone(org);
// SNAP1 / APR
if (org.snapshot === "SNAP1" && org.salaryPeriod.period === "APR") {
const countFull = org.salaryProfiles.filter((p) => p.type === "FULL").length;
org.total = org.salaryProfiles.length;
org.fifteenPercent = Math.floor(org.total * 0.15);
org.quantityUsed = countFull;
org.remainQuota = org.fifteenPercent - countFull;
}
// SNAP1 / OCT
if (org.snapshot === "SNAP1" && org.salaryPeriod.period === "OCT") {
const total = org.salaryProfiles.reduce((sum, p) => sum + (p.amount ?? 0), 0);
org.currentAmount = total;
org.sixPercentAmount = total * 0.06;
const useAmount = org.salaryProfiles
.filter((p) => ["HAFT", "FULL", "FULLHAFT"].includes(p.type))
.reduce((s, p) => s + (p.amountUse ?? 0), 0);
org.useAmount = useAmount;
org.remainingAmount = org.sixPercentAmount - useAmount;
// --- SNAP2 APR recalc ---
const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({
where: { period: "APR", year: org.salaryPeriod.year },
});
if (salaryPeriodAPROld) {
const orgSnap2Old: any = await this.salaryOrgRepository.findOne({
where: {
salaryPeriodId: salaryPeriodAPROld.id,
rootId: org.rootId,
group: org.group,
snapshot: "SNAP2",
},
relations: ["salaryProfiles"],
});
if (orgSnap2Old) {
const spent = orgSnap2Old.salaryProfiles.reduce(
(sum: number, p: any) => sum + (p.amountUse ?? 0),
0,
);
org.spentAmount = spent;
org.remainingAmount = org.sixPercentAmount - useAmount - spent;
}
}
}
org.lastUpdateUserId = req.user.sub;
org.lastUpdateFullName = req.user.name;
org.lastUpdatedAt = new Date();
await this.salaryOrgRepository.save(org);
setLogDataDiff(req, { before: beforeOrg, after: org });
}
return new HttpSuccess();
}
/**
* API
*
@ -1652,7 +1886,7 @@ export class SalaryPeriodController extends Controller {
_data.child1 != undefined && _data.child1 != null
? _data.child1[0] != null
? `child1Id IN (:...child1)`
: `child1Id is null`
: `child1Id is ${_data.privilege == "PARENT" ? "not null" : "null"}`
: "1=1",
{
child1: _data.child1,
@ -1687,27 +1921,25 @@ export class SalaryPeriodController extends Controller {
{
child4: _data.child4,
},
)
);
if (body.sortBy) {
if(body.sortBy === "posExecutive"){
query = query
.orderBy( `profile.posExecutive`,body.descending ? "DESC" : "ASC")
.addOrderBy( `profile.positionExecutiveField`,body.descending ? "DESC" : "ASC");
}else{
query = query.orderBy(
`profile.${body.sortBy}`,
body.descending ? "DESC" : "ASC"
);
}
}else{
query = query.orderBy("profile.rootOrder", "ASC")
if (body.sortBy) {
if (body.sortBy === "posExecutive") {
query = query
.orderBy(`profile.posExecutive`, body.descending ? "DESC" : "ASC")
.addOrderBy(`profile.positionExecutiveField`, body.descending ? "DESC" : "ASC");
} else {
query = query.orderBy(`profile.${body.sortBy}`, body.descending ? "DESC" : "ASC");
}
} else {
query = query
.orderBy("profile.rootOrder", "ASC")
.addOrderBy("profile.child1Order", "ASC")
.addOrderBy("profile.child2Order", "ASC")
.addOrderBy("profile.child3Order", "ASC")
.addOrderBy("profile.child4Order", "ASC")
.addOrderBy("profile.posMasterNo", "ASC")
}
.addOrderBy("profile.posMasterNo", "ASC");
}
const [salaryProfile, total] = await query
.skip((body.page - 1) * body.pageSize)
@ -1846,7 +2078,7 @@ export class SalaryPeriodController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง");
}
let type = salaryProfile.type;
//SalaryRank
//SalaryRanks
let salaryRanks: any = null;
if (salaryProfile.amount != null) {
salaryRanks = await this.salaryRankRepository.findOne({
@ -2535,19 +2767,17 @@ export class SalaryPeriodController extends Controller {
"salaryPeriod.status",
"salaryPeriod.year",
"salaryPeriod.revisionId",
])
]);
if (sortBy) {
query = query.orderBy(
`salaryPeriod.${sortBy}`,
descending ? "DESC" : "ASC"
);
}else{
query = query.orderBy("salaryPeriod.year", "DESC")
.addOrderBy("salaryPeriod.effectiveDate", "DESC")
}
if (sortBy) {
query = query.orderBy(`salaryPeriod.${sortBy}`, descending ? "DESC" : "ASC");
} else {
query = query
.orderBy("salaryPeriod.year", "DESC")
.addOrderBy("salaryPeriod.effectiveDate", "DESC");
}
const [salaryPeriod, total] = await query
const [salaryPeriod, total] = await query
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
@ -3928,22 +4158,40 @@ export class SalaryPeriodController extends Controller {
// Parallel loading of initial data and API calls
console.time("⏱ API: Load initial data");
const [orgs, revisionId, _orgProfiles, _orgProfileEmployees] = await Promise.all([
new CallAPI().GetData({ headers: { authorization: request } }, "/org/unauthorize/active/root/id",false),
new CallAPI().GetData({ headers: { authorization: request } }, "/org/unauthorize/revision/latest",false),
new CallAPI().PostData({ headers: { authorization: request } }, "/org/unauthorize/new-salary/gen", {
page: 1,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},false),
new CallAPI().PostData({ headers: { authorization: request } }, "/org/unauthorize/new-salary/employee/gen", {
page: 1,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},false),
new CallAPI().GetData(
{ headers: { authorization: request } },
"/org/unauthorize/active/root/id",
false,
),
new CallAPI().GetData(
{ headers: { authorization: request } },
"/org/unauthorize/revision/latest",
false,
),
new CallAPI().PostData(
{ headers: { authorization: request } },
"/org/unauthorize/new-salary/gen",
{
page: 1,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},
false,
),
new CallAPI().PostData(
{ headers: { authorization: request } },
"/org/unauthorize/new-salary/employee/gen",
{
page: 1,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},
false,
),
]);
console.timeEnd("⏱ API: Load initial data");
@ -3958,13 +4206,18 @@ export class SalaryPeriodController extends Controller {
const promises = [];
for (let index = 2; index <= page; index++) {
promises.push(
new CallAPI().PostData({ headers: { authorization: request } }, "/org/unauthorize/new-salary/gen", {
page: index,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},false),
new CallAPI().PostData(
{ headers: { authorization: request } },
"/org/unauthorize/new-salary/gen",
{
page: index,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},
false,
),
);
}
const results = await Promise.all(promises);
@ -3983,13 +4236,18 @@ export class SalaryPeriodController extends Controller {
const promises = [];
for (let index = 2; index <= page; index++) {
promises.push(
new CallAPI().PostData({ headers: { authorization: request } }, "/org/unauthorize/new-salary/employee/gen", {
page: index,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},false),
new CallAPI().PostData(
{ headers: { authorization: request } },
"/org/unauthorize/new-salary/employee/gen",
{
page: index,
pageSize: 1000,
keyword: "",
year: salaryPeriod.year,
period: salaryPeriod.period,
},
false,
),
);
}
const results = await Promise.all(promises);
@ -4575,9 +4833,9 @@ export class SalaryPeriodController extends Controller {
_salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
}
_salaryOrg.createdUserId = ""
_salaryOrg.createdUserId = "";
_salaryOrg.createdFullName = "System Administrator";
_salaryOrg.lastUpdateUserId = ""
_salaryOrg.lastUpdateUserId = "";
_salaryOrg.lastUpdateFullName = "System Administrator";
_salaryOrg.createdAt = new Date();
_salaryOrg.lastUpdatedAt = new Date();
@ -4713,9 +4971,10 @@ export class SalaryPeriodController extends Controller {
},
},
);
let request:any = response.data.access_token;
if (current.getDate() == 1 && current.getMonth() == 2) { //snap1 วันที่ 1 มีนา
let request: any = response.data.access_token;
if (current.getDate() == 1 && current.getMonth() == 2) {
//snap1 วันที่ 1 มีนา
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
year: current.getFullYear(),
@ -4726,7 +4985,8 @@ export class SalaryPeriodController extends Controller {
if (salaryPeriod) {
this.performSnapshotOperationForCronjob("SNAP1", salaryPeriod.id, request);
}
} else if (current.getDate() == 1 && current.getMonth() == 3) { //snap2 วันที่ 1 เมษา
} else if (current.getDate() == 1 && current.getMonth() == 3) {
//snap2 วันที่ 1 เมษา
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
year: current.getFullYear(),
@ -4737,7 +4997,8 @@ export class SalaryPeriodController extends Controller {
if (salaryPeriod) {
this.performSnapshotOperationForCronjob("SNAP2", salaryPeriod.id, request);
}
} else if (current.getDate() == 1 && current.getMonth() == 8) { //snap1 วันที่ 1 กันยา
} else if (current.getDate() == 1 && current.getMonth() == 8) {
//snap1 วันที่ 1 กันยา
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
year: current.getFullYear(),
@ -4748,7 +5009,8 @@ export class SalaryPeriodController extends Controller {
if (salaryPeriod) {
this.performSnapshotOperationForCronjob("SNAP1", salaryPeriod.id, request);
}
} else if (current.getDate() == 1 && current.getMonth() == 9) { //snap2 วันที่ 1 ตุลา
} else if (current.getDate() == 1 && current.getMonth() == 9) {
//snap2 วันที่ 1 ตุลา
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
year: current.getFullYear(),

View file

@ -94,6 +94,15 @@ class CheckAuth {
child4: null,
privilege: "ROOT",
};
} else if (privilege == "PARENT") {
data = {
root: [x.orgRootId],
child1: [null],
child2: null,
child3: null,
child4: null,
privilege: "PARENT",
};
} else if (privilege == "CHILD") {
data = {
root: node >= 0 ? [x.orgRootId] : null,
@ -103,6 +112,15 @@ class CheckAuth {
child4: node >= 4 ? [x.orgChild4Id] : null,
privilege: "CHILD",
};
} else if (privilege == "BROTHER") {
data = {
// root: node >= 0 ? [x.orgRootId] : null,
root: node >= 0 ? [x.orgRootId] : null,
child1: node >= 2 ? [x.orgChild1Id] : null,
child2: node >= 3 ? [x.orgChild2Id] : null,
child3: node >= 4 ? [x.orgChild3Id] : null,
privilege: "BROTHER",
};
} else if (privilege == "NORMAL") {
data = {
root: [x.orgRootId],
@ -185,38 +203,38 @@ class CheckAuth {
}
public async checkOrg(token: any, keycloakId: string) {
const redisClient = await this.redis.createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
})
const getAsync = promisify(redisClient.get).bind(redisClient)
try {
let reply = await getAsync("org_" + keycloakId)
if (reply != null) {
reply = JSON.parse(reply)
} else {
if (!keycloakId) throw new Error("No KeycloakId provided")
const x = await new CallAPI().GetData(
{
headers: { authorization: token },
},
`/org/permission/checkOrg/${keycloakId}`,
false
)
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
});
const getAsync = promisify(redisClient.get).bind(redisClient);
try {
let reply = await getAsync("org_" + keycloakId);
if (reply != null) {
reply = JSON.parse(reply);
} else {
if (!keycloakId) throw new Error("No KeycloakId provided");
const x = await new CallAPI().GetData(
{
headers: { authorization: token },
},
`/org/permission/checkOrg/${keycloakId}`,
false,
);
const data = {
orgRootId: x.orgRootId,
orgChild1Id: x.orgChild1Id,
orgChild2Id: x.orgChild2Id,
orgChild3Id: x.orgChild3Id,
orgChild4Id: x.orgChild4Id,
}
const data = {
orgRootId: x.orgRootId,
orgChild1Id: x.orgChild1Id,
orgChild2Id: x.orgChild2Id,
orgChild3Id: x.orgChild3Id,
orgChild4Id: x.orgChild4Id,
};
return data
}
} catch (error) {
console.error("Error calling API:", error)
throw error
}
return data;
}
} catch (error) {
console.error("Error calling API:", error);
throw error;
}
}
public async PermissionCreate(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "CREATE");