no message

This commit is contained in:
Bright 2024-02-28 18:07:38 +07:00
parent 6ed7265094
commit 42acedebf5

View file

@ -28,7 +28,7 @@ import { PosLevel } from "../entities/PosLevel";
import { Salarys } from "../entities/Salarys";
import { SalaryRanks } from "../entities/SalaryRanks";
import CallAPI from "../interfaces/call-api";
import * as express from "express";
@Route("api/v1/salary/period")
@Tags("Salary")
@Security("bearerAuth")
@ -73,11 +73,11 @@ export class SalaryPeriodController extends Controller {
) == null
? null
: salaryPeriod.salaryOrgs.find(
(x) =>
x.group == "GROUP1" &&
x.rootId == body.rootId &&
x.snapshot == body.snapshot.toLocaleUpperCase(),
)?.id,
(x) =>
x.group == "GROUP1" &&
x.rootId == body.rootId &&
x.snapshot == body.snapshot.toLocaleUpperCase(),
)?.id,
group2id:
salaryPeriod.salaryOrgs.find(
(x) =>
@ -87,11 +87,11 @@ export class SalaryPeriodController extends Controller {
) == null
? null
: salaryPeriod.salaryOrgs.find(
(x) =>
x.group == "GROUP2" &&
x.rootId == body.rootId &&
x.snapshot == body.snapshot.toLocaleUpperCase(),
)?.id,
(x) =>
x.group == "GROUP2" &&
x.rootId == body.rootId &&
x.snapshot == body.snapshot.toLocaleUpperCase(),
)?.id,
effectiveDate: salaryPeriod.effectiveDate,
period: salaryPeriod.period,
};
@ -212,9 +212,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryHalf == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryHalf == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryHalf - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf;
@ -222,9 +222,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryFull == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryFull == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryFull - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull;
@ -232,9 +232,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryFullHalf == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryFullHalf == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryFullHalf - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf;
@ -337,9 +337,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryHalf == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryHalf == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryHalf - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf;
@ -347,9 +347,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryFull == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryFull == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryFull - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull;
@ -357,9 +357,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryFullHalf == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryFullHalf == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryFullHalf - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf;
@ -529,9 +529,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryHalf == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryHalf == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryHalf - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf;
@ -539,9 +539,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryFull == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryFull == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryFull - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull;
@ -549,9 +549,9 @@ export class SalaryPeriodController extends Controller {
salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial;
salaryProfile.amountUse =
salaryRanks == null ||
salaryProfile == null ||
salaryRanks.salaryFullHalf == null ||
salaryProfile.amount == null
salaryProfile == null ||
salaryRanks.salaryFullHalf == null ||
salaryProfile.amount == null
? 0
: salaryRanks.salaryFullHalf - salaryProfile.amount;
salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf;
@ -775,9 +775,7 @@ export class SalaryPeriodController extends Controller {
await this.salaryOrgRepository.remove(salaryOrg);
await this.salaryProfileRepository.remove(salaryProfile);
console.log("11111111111111111111111");
let orgs = await new CallAPI().GetData(request, "org/active/root/id");
console.log("22222222222222222222222");
let orgProfiles: any;
await new CallAPI()
.PostData(request, "org/profile/salary/gen", {
@ -788,7 +786,6 @@ export class SalaryPeriodController extends Controller {
.then((x) => {
orgProfiles = x.data;
});
console.log("333333333333333333333");
if (orgProfiles.total > 100) {
const page = Math.ceil(orgProfiles.total.length / 100);
for (let index = 2; index <= page; index++) {
@ -803,9 +800,7 @@ export class SalaryPeriodController extends Controller {
});
}
}
console.log("44444444444444444444444");
let revisionId = await new CallAPI().GetData(request, "org/revision/latest");
console.log("55555555555555555555555");
salaryPeriod.revisionId = revisionId;
await this.salaryPeriodRepository.save(salaryPeriod);
@ -904,18 +899,23 @@ export class SalaryPeriodController extends Controller {
/**
* Cronjob SalaryPeriod
*/
async CronjobSalaryPeriod() {
async CronjobSalaryPeriod() { //bright
const current = new Date();
let salaryPeriod: any;
// console.log(current.getDate(), current.getMonth() , current.getFullYear())
let request: any;
// request = express.request;
// console.log("request: ", request);
if (current.getDate() == 1 && current.getMonth() == 2) {
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
year: current.getFullYear(),
period: "MAR",
period: "APR",
isActive: true,
},
});
if (salaryPeriod) {
this.SnapshotSalarys("SNAP1", salaryPeriod.id, request)
}
} else if (current.getDate() == 1 && current.getMonth() == 3) {
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
@ -924,14 +924,20 @@ export class SalaryPeriodController extends Controller {
isActive: true,
},
});
if (salaryPeriod) {
this.SnapshotSalarys("SNAP2", salaryPeriod.id, request)
}
} else if (current.getDate() == 1 && current.getMonth() == 8) {
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
year: current.getFullYear(),
period: "SEP",
period: "OCT",
isActive: true,
},
});
if (salaryPeriod) {
this.SnapshotSalarys("SNAP1", salaryPeriod.id, request)
}
} else if (current.getDate() == 1 && current.getMonth() == 9) {
salaryPeriod = await this.salaryPeriodRepository.findOne({
where: {
@ -940,6 +946,150 @@ export class SalaryPeriodController extends Controller {
isActive: true,
},
});
if (salaryPeriod) {
this.SnapshotSalarys("SNAP2", salaryPeriod.id, request)
}
}
}
public async SnapshotSalarys(
snapshot: string,
salaryPeriodId: string,
request: any
) {
snapshot = snapshot.toLocaleUpperCase();
const salaryPeriod = await this.salaryPeriodRepository.findOne({
where: { id: salaryPeriodId },
});
if (!salaryPeriod) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการสร้างเงินเดือน");
}
const salaryOrg = await this.salaryOrgRepository.find({
where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot },
});
const salaryProfile = await this.salaryProfileRepository.find({
where: { salaryOrgId: In(salaryOrg.map((x) => x.id)) },
});
await this.salaryOrgRepository.remove(salaryOrg);
await this.salaryProfileRepository.remove(salaryProfile);
let orgs = await new CallAPI().GetData(request, "org/unauthorize/active/root/id");
let orgProfiles: any;
await new CallAPI()
.PostData(request, "org/unauthorize/profile/salary/gen", {
page: 1,
pageSize: 100,
keyword: "",
})
.then((x) => {
orgProfiles = x.data;
});
if (orgProfiles.total > 100) {
const page = Math.ceil(orgProfiles.total.length / 100);
for (let index = 2; index <= page; index++) {
await new CallAPI()
.PostData(request, "org/unauthorize/profile/salary/gen", {
page: index,
pageSize: 100,
keyword: "",
})
.then((x) => {
Array.prototype.push.apply(orgProfiles, x.data);
});
}
}
let revisionId = await new CallAPI().GetData(request, "org/unauthorize/revision/latest");
salaryPeriod.revisionId = revisionId;
await this.salaryPeriodRepository.save(salaryPeriod);
await Promise.all(
orgs.map(async (rootId: string) => {
let salaryOrgNew = Object.assign(new SalaryOrg());
salaryOrgNew.salaryPeriodId = salaryPeriod.id;
salaryOrgNew.status = "PENDING";
salaryOrgNew.rootId = rootId;
salaryOrgNew.revisionId = salaryPeriod.revisionId;
salaryOrgNew.snapshot = snapshot;
salaryOrgNew.group = "GROUP1";
salaryOrgNew.createdUserId = request.user.sub;
salaryOrgNew.createdFullName = request.user.name;
salaryOrgNew.lastUpdateUserId = request.user.sub;
salaryOrgNew.lastUpdateFullName = request.user.name;
await this.salaryOrgRepository.save(salaryOrgNew);
delete salaryOrgNew.id;
salaryOrgNew.group = "GROUP2";
await this.salaryOrgRepository.save(salaryOrgNew);
}),
);
let salaryProfileOld: SalaryProfile[] = [];
if (snapshot == "SNAP2") {
const salaryOrgOld = await this.salaryOrgRepository.findOne({
where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" },
relations: ["salaryProfiles"],
});
if (salaryOrgOld != null) salaryProfileOld = salaryOrgOld.salaryProfiles;
}
await Promise.all(
orgProfiles.map(async (profile: any) => {
let group = "GROUP1";
if (
(profile.posType == "ทั่วไป" && profile.posLevel == "ทักษะพิเศษ") ||
(profile.posType == "วิชาการ" && profile.posLevel == "เชี่ยวชาญ") ||
(profile.posType == "วิชาการ" && profile.posLevel == "ทรงคุณวุฒิ") ||
(profile.posType == "อำนวยการ" && profile.posLevel == "สูง") ||
(profile.posType == "บริหาร" && profile.posLevel == "ต้น") ||
(profile.posType == "บริหาร" && profile.posLevel == "สูง")
) {
group = "GROUP2";
}
const salaryOrgNew = await this.salaryOrgRepository.findOne({
where: {
salaryPeriodId: salaryPeriod.id,
rootId: profile.rootId,
snapshot: snapshot,
group: group,
},
});
if (salaryOrgNew != null) {
let salaryProfileNew = Object.assign(new SalaryProfile(), profile);
salaryProfileNew.salaryOrgId = salaryOrgNew.id;
salaryProfileNew.revisionId = salaryPeriod.revisionId;
salaryProfileNew.createdUserId = request.user.sub;
salaryProfileNew.createdFullName = request.user.name;
salaryProfileNew.lastUpdateUserId = request.user.sub;
salaryProfileNew.lastUpdateFullName = request.user.name;
if (snapshot == "SNAP2") {
const salaryOld = salaryProfileOld.find(
(x) => x.citizenId == salaryProfileNew.citizenId,
);
salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount;
salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial;
salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse;
salaryProfileNew.positionSalaryAmount =
salaryOld == null ? 0 : salaryOld.positionSalaryAmount;
}
await this.salaryProfileRepository.save(salaryProfileNew);
}
}),
);
const salaryOrgNew = await this.salaryOrgRepository.find({
where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot },
relations: ["salaryProfiles"],
});
await Promise.all(
salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => {
_salaryOrg.total = _salaryOrg.salaryProfiles.length;
_salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100);
_salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100;
await this.salaryOrgRepository.save(_salaryOrg);
}),
);
}
}