Merge branch 'adiDev' into develop

This commit is contained in:
AdisakKanthawilang 2024-10-29 17:01:19 +07:00
commit b0dc1917c4
4 changed files with 85 additions and 118 deletions

View file

@ -12,9 +12,9 @@ export class AppController extends Controller {
return { message: "Hello World 1" };
}
// @Post()
// public async Post(@Query() profileId: string) {
// const result = calculateGovAge(profileId);
// return new HttpSuccess(result);
// }
@Post()
public async Post(@Query() profileId: string) {
const result = calculateGovAge(profileId,"OFFICER");
return new HttpSuccess(result);
}
}

View file

@ -8,7 +8,7 @@ import { Profile } from "../entities/Profile";
import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment";
import { Position } from "../entities/Position";
import { PosMaster } from "../entities/PosMaster";
import { calculateAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils";
import { calculateAge, calculateGovAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils";
import permission from "../interfaces/permission";
import { OrgRevision } from "../entities/OrgRevision";
@Route("api/v1/org/profile/government")
@ -119,7 +119,8 @@ export class ProfileGovernmentHistoryController extends Controller {
positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
dateRetireLaw: record.dateRetireLaw ?? null,
govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
govAge: await calculateGovAge(profile.id,"OFFICER"),
dateAppoint: record.dateAppoint,
dateStart: record.dateStart,
govAgeAbsent: record.govAgeAbsent,
@ -229,7 +230,8 @@ export class ProfileGovernmentHistoryController extends Controller {
positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
dateRetireLaw: record.dateRetireLaw ?? null,
govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
govAge: await calculateGovAge(profileId,"OFFICER"),
dateAppoint: record.dateAppoint,
dateStart: record.dateStart,
govAgeAbsent: record.govAgeAbsent,
@ -322,7 +324,8 @@ export class ProfileGovernmentHistoryController extends Controller {
positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
dateRetireLaw: record.dateRetireLaw ?? null,
govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
govAge: await calculateGovAge(profileId,"OFFICER"),
dateAppoint: record.dateAppoint,
dateStart: record.dateStart,
govAgeAbsent: record.govAgeAbsent,

View file

@ -8,7 +8,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee";
import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment";
import { EmployeePosition } from "../entities/EmployeePosition";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { calculateAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils";
import { calculateAge, calculateGovAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils";
import permission from "../interfaces/permission";
import { OrgRevision } from "../entities/OrgRevision";
@Route("api/v1/org/profile-employee/government")
@ -108,7 +108,8 @@ export class ProfileGovernmentEmployeeController extends Controller {
posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
dateRetireLaw: record.dateRetireLaw ?? null,
govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
govAge: await calculateGovAge(profile.id,"EMPLOYEE"),
dateAppoint: record.dateAppoint,
dateStart: record.dateStart,
govAgeAbsent: record.govAgeAbsent,
@ -211,7 +212,8 @@ export class ProfileGovernmentEmployeeController extends Controller {
dateStart: record.dateStart, //วันที่เริ่มปฎิบัติงานราชการ
reasonSameDate: record.reasonSameDate, //เหตุผลที่วันที่ไม่ตรงกัน
dateRetire: record.dateRetire ?? null, //วันครบเกษียณอายุ
govAge: record.dateStart == null ? null : calculateAge(record.dateStart), //อายุราชการ
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart), //อายุราชการ
govAge: await calculateGovAge(profileEmployeeId,"EMPLOYEE"),
govAgeAbsent: record.govAgeAbsent ?? null, // ขาดราชการ
govAgePlus: record.govAgePlus, // อายุราชการเกื้อกูล
dateRetireLaw: record.dateRetireLaw ?? null, // วันที่เกษียฯอายุราชการตามกฎหมาย
@ -294,7 +296,8 @@ export class ProfileGovernmentEmployeeController extends Controller {
dateStart: record.dateStart, //วันที่เริ่มปฎิบัติงานราชการ
reasonSameDate: record.reasonSameDate, //เหตุผลที่วันที่ไม่ตรงกัน
dateRetire: record.dateRetire ?? null, //วันครบเกษียณอายุ
govAge: record.dateStart == null ? null : calculateAge(record.dateStart), //อายุราชการ
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart), //อายุราชการ
govAge: await calculateGovAge(profileEmployeeId,"EMPLOYEE"),
govAgeAbsent: record.govAgeAbsent ?? null, // ขาดราชการ
govAgePlus: record.govAgePlus, // อายุราชการเกื้อกูล
dateRetireLaw: record.dateRetireLaw ?? null, // วันที่เกษียฯอายุราชการตามกฎหมาย

View file

@ -40,158 +40,119 @@ export function calculateAge(start: Date, end = new Date()) {
return { year, month, day };
}
export async function calculateGovAge(profileId: string) {
export async function calculateGovAge(profileId: string , type: string) { // type = OFFICER , EMPLOYEE
const isEmployee = type === 'EMPLOYEE';
const records = await AppDataSource.getRepository(ProfileSalary).find({
where: {
profileId: profileId,
},
where: {
[isEmployee ? 'profileEmployeeId' : 'profileId']: profileId,
},
select: ["date", "dateGovernment", "isGovernment"],
order: { order: "ASC" },
}); // หา record ทั้งหมด
});
if (!records || records.length === 0) {
return null;
}
let sumYears = 0;
let sumMonths = 0;
let sumDays = 0;
let endDateFristRec: any;
endDateFristRec = await AppDataSource.getRepository(ProfileSalary).findOne({
let endDateFristRec: any = null;
endDateFristRec = await AppDataSource.getRepository(ProfileSalary).findOne({
where: {
profileId: profileId,
[isEmployee ? 'profileEmployeeId' : 'profileId']: profileId,
dateGovernment: Not(IsNull()),
isGovernment: false,
},
select: ["date", "dateGovernment", "isGovernment", "order"],
select: ["dateGovernment", "order"],
order: { order: "ASC" },
}); // หา isGovernment: false record แรก
});
let totalYears1 = 0;
let totalMonths1 = 0;
let totalDays1 = 0;
const calculateDuration = (startDate:any, endDate:any) => {
let years = endDate.getFullYear() - startDate.getFullYear();
let months = endDate.getMonth() - startDate.getMonth();
let days = endDate.getDate() - startDate.getDate();
if (days < 0) {
months--;
const lastMonthDays = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
days += lastMonthDays;
}
if (months < 0) {
months += 12;
years--;
}
return { years, months, days };
};
const firstStartDate = new Date(records[0].date);
const firstEndDate = endDateFristRec?new Date(endDateFristRec.dateGovernment):new Date();
console.log("[firstStartDate]",firstStartDate);
console.log("[firstEndDate]",firstEndDate);
totalYears1 = firstEndDate.getFullYear() - firstStartDate.getFullYear();
totalMonths1 = firstEndDate.getMonth() - firstStartDate.getMonth();
totalDays1 = firstEndDate.getDate() - firstStartDate.getDate();
const firstEndDate = endDateFristRec ? new Date(endDateFristRec.dateGovernment) : new Date();
if (totalDays1 < 0) {
totalMonths1 -= 1; // หาวันในเดือนก่อนหน้า
const lastMonth = new Date(firstEndDate.getFullYear(), firstEndDate.getMonth(), 0).getDate();
totalDays1 += lastMonth;
}
if (totalMonths1 < 0) {
totalMonths1 += 12;
totalYears1 -= 1;
}
const { years: totalYears1, months: totalMonths1, days: totalDays1 } = calculateDuration(firstStartDate, firstEndDate);
const records_middle = await AppDataSource.getRepository(ProfileSalary).find({
where: {
profileId: profileId,
[isEmployee ? 'profileEmployeeId' : 'profileId']: profileId,
order: MoreThan(endDateFristRec?.order),
dateGovernment: Not(IsNull()),
},
select: ["date", "dateGovernment", "isGovernment", "order"],
select: ["dateGovernment", "isGovernment"],
order: { order: "ASC" },
}); // หา record order ที่ทำหลังจาก endDateFristRec
console.log("[Gov1]", totalYears1, totalMonths1, totalDays1);
if (!records_middle || records_middle.length === 0) { // ถ้าไม่เจอแปลว่ากำลังพักราชการอยู่หรือยังไม่เคยพักราชการ
sumYears = totalYears1,
sumMonths = totalMonths1,
sumDays = totalDays1
console.log("[SumGov1]", sumYears, sumMonths, sumDays);
return {
sumYears,
sumMonths,
sumDays,
};
});
if (!records_middle || records_middle.length === 0) {
return { year: totalYears1, month: totalMonths1, day: totalDays1 };
}
let totalYears2 = 0;
let totalMonths2 = 0;
let totalDays2 = 0;
let totalYears2 = 0, totalMonths2 = 0, totalDays2 = 0;
for (let i = 0; i < records_middle.length; i++) {
const startDate = new Date(records_middle[i].dateGovernment);
const endDate = (i < records_middle.length - 1)
? new Date(records_middle[i + 1].dateGovernment)
: new Date(); // วันที่ปัจจุบันถ้าเป็นช่วงสุดท้าย
if (records_middle[i].isGovernment === false && records_middle[i + 1].isGovernment === false) {
? new Date(records_middle[i + 1].dateGovernment)
: new Date();
if (records_middle[i].isGovernment === false &&
(i === records_middle.length - 1 || records_middle[i + 1].isGovernment === false)) {
break;
}
if (records_middle[i].isGovernment === false && i === records_middle.length - 1) {
break;
}
console.log("[SDate]", startDate);
console.log("[EDate]", endDate);
// คำนวณระยะเวลา
let years_mid = endDate.getFullYear() - startDate.getFullYear();
let months_mid = endDate.getMonth() - startDate.getMonth();
let days_mid = endDate.getDate() - startDate.getDate();
const { years: years_mid, months: months_mid, days: days_mid } = calculateDuration(startDate, endDate);
if (days_mid < 0) {
months_mid--;
const lastMonthDays = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
days_mid += lastMonthDays; // ปรับวันให้ถูกต้อง
}
// การปรับเดือน
if (months_mid < 0) {
months_mid += 12;
years_mid--;
}
// รวมเข้ากับ total
totalYears2 += years_mid;
totalMonths2 += months_mid;
totalDays2 += days_mid;
}
// การปรับค่าหาก totalMonths2 เกิน 12
if (totalMonths2 >= 12) {
totalYears2 += Math.floor(totalMonths2 / 12);
totalMonths2 = totalMonths2 % 12;
}
const adjustTotal = (years: any, months: any, days: any) => {
if (days >= new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()) {
months += Math.floor(days / new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate());
days %= new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
}
// การปรับค่าหาก totalDays2 เกินจำนวนวันในเดือน
const daysInCurrentMonth = new Date(
new Date().getFullYear(),
new Date().getMonth() + 1,
0,
).getDate();
if (totalDays2 >= daysInCurrentMonth) {
totalMonths2 += Math.floor(totalDays2 / daysInCurrentMonth);
totalDays2 = totalDays2 % daysInCurrentMonth;
}
if (months >= 12) {
years += Math.floor(months / 12);
months %= 12;
}
sumYears = totalYears1 + totalYears2;
sumMonths = totalMonths1 + totalMonths2;
sumDays = totalDays1 + totalDays2;
return { years, months, days };
};
const adjustedTotal = adjustTotal(totalYears2, totalMonths2, totalDays2);
let sumYears = totalYears1 + adjustedTotal.years;
let sumMonths = totalMonths1 + adjustedTotal.months;
let sumDays = totalDays1 + adjustedTotal.days;
if (sumMonths >= 12) {
sumYears += Math.floor(sumMonths / 12);
sumMonths = sumMonths % 12;
sumMonths %= 12;
}
console.log("[data1]",totalYears1+"/"+totalMonths1+"/"+totalDays1);
console.log("[data2]",totalYears2+"/"+totalMonths2+"/"+totalDays2);
console.log("[SumGovAge]", sumYears, sumMonths, sumDays);
return {
sumYears,
sumMonths,
sumDays,
};
return {year: sumYears, month: sumMonths, day: sumDays};
}
export function calculateRetireDate(birthDate: Date) {
// let _birthDate = birthDate;