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" }; return { message: "Hello World 1" };
} }
// @Post() @Post()
// public async Post(@Query() profileId: string) { public async Post(@Query() profileId: string) {
// const result = calculateGovAge(profileId); const result = calculateGovAge(profileId,"OFFICER");
// return new HttpSuccess(result); return new HttpSuccess(result);
// } }
} }

View file

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

View file

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

View file

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