import { OrgRevision } from "../entities/OrgRevision"; import { AppDataSource } from "../database/data-source"; import { PosMaster } from "../entities/PosMaster"; import { Position } from "../entities/Position"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeePosition } from "../entities/EmployeePosition"; import { In } from "typeorm"; 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(); // 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(); // } if (day < 0) { month -= 1; day += new Date(end.getFullYear(), end.getMonth(), 0).getDate(); } if (month < 0) { month += 12; year -= 1; } return { year, month, day }; } export function calculateRetireDate(birthDate: Date) { // 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-1); return c; } export function calculateRetireLaw(birthDate: Date) { let dd = birthDate.getDate(); let mm = birthDate.getMonth(); let yy = birthDate.getFullYear(); let flag = true; switch (mm) { case 9: if (dd >= 2) flag = false; break; case 10: flag = false; break; case 11: flag = false; break; } if (flag == true) return new Date(`${yy + 60}-09-30T00:00:00.000+07:00`); return new Date(`${yy + 61}-09-30T00:00:00.000+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, type:string) { const currentRevision = await AppDataSource.getRepository(OrgRevision) .createQueryBuilder("orgRevision") .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); if (!currentRevision) { return; } if (type === "OFFICER") { 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) { return; } const findPosition = await AppDataSource.getRepository(Position) .createQueryBuilder("position") .where("position.posMasterId = :posMasterId", { posMasterId: findProfileInposMaster?.id }) .getMany(); if (!findPosition) { return; } await AppDataSource.getRepository(Position) .createQueryBuilder() .update(Position) .set({ positionIsSelected: false }) .where("id IN (:...ids)", { ids: findPosition.map((item) => item.id) }) .execute(); } if (type === "EMPLOYEE") { const findProfileInEmpPosMaster = await AppDataSource.getRepository(EmployeePosMaster) .createQueryBuilder("employeePosMaster") .where("employeePosMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: currentRevision?.id, }) .andWhere("employeePosMaster.current_holderId = :profileId", { profileId }) .getOne(); await AppDataSource.getRepository(EmployeePosMaster) .createQueryBuilder() .update(EmployeePosMaster) .set({ current_holderId: null }) .where("id = :id", { id: findProfileInEmpPosMaster?.id }) .execute(); if (!findProfileInEmpPosMaster) { return; } const findEmpPosition = await AppDataSource.getRepository(EmployeePosition) .createQueryBuilder("employeePosition") .where("employeePosition.posMasterId = :posMasterId", { posMasterId: findProfileInEmpPosMaster?.id, }) .getMany(); if (!findEmpPosition) { return; } await AppDataSource.getRepository(EmployeePosition) .createQueryBuilder() .update(EmployeePosition) .set({ positionIsSelected: false }) .where("id IN (:...ids)", { ids: findEmpPosition.map((item) => item.id) }) .execute(); } }