hrms-api-org/src/interfaces/utils.ts

135 lines
3.5 KiB
TypeScript
Raw Normal View History

import { OrgRevision } from "../entities/OrgRevision";
import { AppDataSource } from "../database/data-source";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
export function calculateAge(start: Date, end = new Date()) {
if (start.getTime() > end.getTime()) return null;
let year = end.getFullYear() - start.getFullYear();
let month = end.getMonth() - start.getMonth();
let day = end.getDate() - start.getDate();
2024-07-01 17:27:01 +07:00
// if (month < 0) {
// month += 12;
// year -= 1;
// }
// if (day < 0) {
// month -= 1;
// day =
// new Date(start.getFullYear(), start.getMonth(), 0).getDate() -
// start.getDate() +
// end.getDate();
// }
2024-07-03 00:36:23 +07:00
if (day < 0) {
month -= 1;
day += new Date(end.getFullYear(), end.getMonth(), 0).getDate();
}
2024-07-01 17:27:01 +07:00
2024-07-03 00:36:23 +07:00
if (month < 0) {
month += 12;
year -= 1;
}
return { year, month, day };
}
2024-03-08 17:20:39 +07:00
export function calculateRetireDate(birthDate: Date) {
2024-07-03 00:36:23 +07:00
// let _birthDate = birthDate;
// _birthDate.setFullYear(_birthDate.getFullYear() + 60); var year = d.getFullYear();
var d = birthDate;
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
var c = new Date(year + 60, month, day);
return c;
}
export function calculateRetireLaw(birthDate: Date) {
2024-03-08 17:20:39 +07:00
let dd = birthDate.getDate();
let mm = birthDate.getMonth();
let yy = birthDate.getFullYear();
let flag = true;
switch (mm) {
2024-07-03 00:36:23 +07:00
case 9:
2024-03-08 17:20:39 +07:00
if (dd >= 2) flag = false;
break;
2024-07-03 00:36:23 +07:00
case 10:
flag = false;
2024-03-26 09:47:17 +07:00
break;
2024-07-03 00:36:23 +07:00
case 11:
flag = false;
2024-03-08 17:20:39 +07:00
break;
}
2024-07-03 00:36:23 +07:00
if (flag == true) return new Date(`${yy + 60}-09-30T00:00:00.000+07:00`);
2024-03-08 17:20:39 +07:00
2024-03-11 10:45:40 +07:00
return new Date(`${yy + 61}-09-30T00:00:00.000+07:00`);
2024-03-08 17:20:39 +07:00
}
2024-03-26 09:47:17 +07:00
export function calculateRetireYear(birthDate: Date) {
let dd = birthDate.getDate();
let mm = birthDate.getMonth();
let yy = birthDate.getFullYear();
let flag = true;
switch (mm) {
case 10:
if (dd >= 2) flag = false;
break;
case 11:
break;
case 12:
break;
}
if (flag) return yy + 60;
return yy + 61;
}
export async function removeProfileInOrganize(profileId: string) {
const currentRevision = await AppDataSource.getRepository(OrgRevision)
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
if (!currentRevision) {
throw new Error("Not Found");
}
const findProfileInposMaster = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id })
.andWhere("posMaster.current_holderId = :profileId", { profileId })
.getOne();
await AppDataSource.getRepository(PosMaster)
.createQueryBuilder()
.update(PosMaster)
.set({ current_holderId: null })
.where("id = :id", { id: findProfileInposMaster?.id })
.execute();
if (!findProfileInposMaster) {
throw new Error("Not Found");
}
const findPosition = await AppDataSource.getRepository(Position)
.createQueryBuilder("position")
.where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id })
.getOne();
if (!findPosition) {
throw new Error("Not Found");
}
await AppDataSource.getRepository(Position)
.createQueryBuilder()
.update(Position)
.set({ positionIsSelected: false })
.where("id = :id", { id: findPosition?.id })
.execute();
}