Merge branch 'develop' of github.com:Frappet/bma-ehr-organization into develop
This commit is contained in:
commit
aee012fa3f
4 changed files with 290 additions and 21 deletions
|
|
@ -177,11 +177,11 @@ export class ProfileController extends Controller {
|
|||
LastName: profile.lastName != null ? profile.lastName : "",
|
||||
DateOfBirth:
|
||||
profile.birthDate != null
|
||||
? Extension.ToThaiNumber(Extension.ToThaiFullDate(profile.birthDate))
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate_perfixMonthYear(profile.birthDate))
|
||||
: "",
|
||||
DateRetire:
|
||||
profile.dateRetire != null
|
||||
? Extension.ToThaiNumber(Extension.ToThaiFullDate(profile.dateRetire))
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate_perfixMonthYear(profile.dateRetire))
|
||||
: "",
|
||||
RegistrationAddress: Extension.ToThaiNumber(
|
||||
`${_regisAddres}${_subDistrict}${_district}${_province}${registrationZipCode}`,
|
||||
|
|
@ -197,11 +197,13 @@ export class ProfileController extends Controller {
|
|||
: "",
|
||||
AppointText:
|
||||
profile.dateAppoint != null
|
||||
? Extension.ToThaiNumber(Extension.ToThaiFullDate(profile.dateAppoint))
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate_perfixMonthYear(profile.dateAppoint))
|
||||
: "",
|
||||
SalaryDate:
|
||||
profile.profileSalary.length > 0 && profile.profileSalary[0].date != null
|
||||
? Extension.ToThaiNumber(Extension.ToThaiFullDate(profile.profileSalary[0].date))
|
||||
? Extension.ToThaiNumber(
|
||||
Extension.ToThaiShortDate_perfixMonthYear(profile.profileSalary[0].date),
|
||||
)
|
||||
: "",
|
||||
PositionName: profile.position != null ? profile.position : "",
|
||||
OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`,
|
||||
|
|
@ -307,13 +309,15 @@ export class ProfileController extends Controller {
|
|||
Prefix: profiles?.prefix != null ? profiles.prefix : "",
|
||||
FirstName: profiles?.firstName != null ? profiles.firstName : "",
|
||||
LastName: profiles?.lastName != null ? profiles.lastName : "",
|
||||
FullName: `${profiles?.prefix} ${profiles?.firstName} ${profiles?.lastName}`,
|
||||
FullName: `${profiles?.prefix}${profiles?.firstName} ${profiles?.lastName}`,
|
||||
BirthDay: profiles?.birthDate
|
||||
? Extension.ToThaiNumber(new Date(profiles.birthDate).getDate().toString())
|
||||
: null,
|
||||
BirthDayText:
|
||||
profiles.birthDate != null
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate(profiles.birthDate).toString())
|
||||
? Extension.ToThaiNumber(
|
||||
Extension.ToThaiShortDate_noPrefix(profiles.birthDate).toString(),
|
||||
)
|
||||
: "",
|
||||
BirthMonth: profiles?.birthDate
|
||||
? Extension.ToThaiNumber(new Date(profiles.birthDate).getMonth() + (1).toString())
|
||||
|
|
@ -323,7 +327,9 @@ export class ProfileController extends Controller {
|
|||
: null,
|
||||
BirthYearText:
|
||||
profiles.birthDate != null
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate(profiles.birthDate).toString())
|
||||
? Extension.ToThaiNumber(
|
||||
Extension.ToThaiShortDate_noPrefix(profiles.birthDate).toString(),
|
||||
)
|
||||
: "",
|
||||
Address: "",
|
||||
District: "",
|
||||
|
|
@ -336,21 +342,21 @@ export class ProfileController extends Controller {
|
|||
profileFamilyCouple?.couplePrefix ||
|
||||
profileFamilyCouple?.coupleFirstName ||
|
||||
profileFamilyCouple?.coupleLastNameOld
|
||||
? `${profileFamilyCouple?.couplePrefix ?? ""} ${profileFamilyCouple?.coupleFirstName ?? ""} ${profileFamilyCouple?.coupleLastNameOld ?? ""}`.trim()
|
||||
? `${profileFamilyCouple?.couplePrefix ?? ""}${profileFamilyCouple?.coupleFirstName ?? ""} ${profileFamilyCouple?.coupleLastNameOld ?? ""}`.trim()
|
||||
: null,
|
||||
FatherPrefix: profileFamilyFather?.fatherPrefix ?? "",
|
||||
FatherFullName:
|
||||
profileFamilyFather?.fatherPrefix ||
|
||||
profileFamilyFather?.fatherFirstName ||
|
||||
profileFamilyFather?.fatherLastName
|
||||
? `${profileFamilyFather?.fatherPrefix ?? ""} ${profileFamilyFather?.fatherFirstName ?? ""} ${profileFamilyFather?.fatherLastName ?? ""}`.trim()
|
||||
? `${profileFamilyFather?.fatherPrefix ?? ""}${profileFamilyFather?.fatherFirstName ?? ""} ${profileFamilyFather?.fatherLastName ?? ""}`.trim()
|
||||
: null,
|
||||
MotherPrefix: profileFamilyMother?.motherPrefix ?? "",
|
||||
MotherFullName:
|
||||
profileFamilyMother?.motherPrefix ||
|
||||
profileFamilyMother?.motherFirstName ||
|
||||
profileFamilyMother?.motherLastName
|
||||
? `${profileFamilyMother?.motherPrefix ?? ""} ${profileFamilyMother?.motherFirstName ?? ""} ${profileFamilyMother?.motherLastName ?? ""}`.trim()
|
||||
? `${profileFamilyMother?.motherPrefix ?? ""}${profileFamilyMother?.motherFirstName ?? ""} ${profileFamilyMother?.motherLastName ?? ""}`.trim()
|
||||
: null,
|
||||
OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`,
|
||||
Division: "",
|
||||
|
|
@ -358,7 +364,7 @@ export class ProfileController extends Controller {
|
|||
StartDate: profiles?.dateStart,
|
||||
AppointDate: profiles?.dateAppoint ?? "",
|
||||
BirthDate: profiles?.birthDate
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate(profiles.birthDate))
|
||||
? Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(profiles.birthDate))
|
||||
: null,
|
||||
RetireDate:
|
||||
profiles.dateRetireLaw != null
|
||||
|
|
@ -375,7 +381,7 @@ export class ProfileController extends Controller {
|
|||
CertificateType: item.certificateType ?? null,
|
||||
Issuer: item.issuer ?? null,
|
||||
CertificateNo: Extension.ToThaiNumber(item.certificateNo) ?? null,
|
||||
IssueDate: Extension.ToThaiNumber(Extension.ToThaiShortDate(item.issueDate)) ?? null,
|
||||
IssueDate: Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.issueDate)) ?? null,
|
||||
}));
|
||||
const trainings = await this.trainingRepository.find({
|
||||
select: ["startDate", "endDate", "place", "department", "name"],
|
||||
|
|
@ -386,12 +392,14 @@ export class ProfileController extends Controller {
|
|||
Start:
|
||||
item.startDate == null
|
||||
? ""
|
||||
: Extension.ToThaiNumber(Extension.ToThaiShortDate(item.startDate)),
|
||||
: Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.startDate)),
|
||||
End:
|
||||
item.endDate == null ? "" : Extension.ToThaiNumber(Extension.ToThaiShortDate(item.endDate)),
|
||||
item.endDate == null
|
||||
? ""
|
||||
: Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.endDate)),
|
||||
Date:
|
||||
item.startDate && item.endDate
|
||||
? `${Extension.ToThaiNumber(Extension.ToThaiShortDate(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiShortDate(item.endDate))}`
|
||||
? `${Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.endDate))}`
|
||||
: "",
|
||||
Level: "",
|
||||
Degree: item.name,
|
||||
|
|
@ -425,7 +433,7 @@ export class ProfileController extends Controller {
|
|||
: Extension.ToThaiNumber(new Date(item.endDate).getFullYear().toString()),
|
||||
Date:
|
||||
item.startDate && item.endDate
|
||||
? `${Extension.ToThaiNumber(Extension.ToThaiShortDate(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiShortDate(item.endDate))}`
|
||||
? `${Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.startDate))} - ${Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.endDate))}`
|
||||
: "",
|
||||
Level: item.educationLevel ?? "",
|
||||
Degree: item.degree && item.field ? `${item.degree} ${item.field}` : "",
|
||||
|
|
@ -445,7 +453,7 @@ export class ProfileController extends Controller {
|
|||
});
|
||||
|
||||
const Salary = salarys.map((item) => ({
|
||||
SalaryDate: Extension.ToThaiNumber(Extension.ToThaiShortDate(item.date)) ?? null,
|
||||
SalaryDate: Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(item.date)) ?? null,
|
||||
Position: item.position ?? null,
|
||||
PosNo: item.posNo ?? null,
|
||||
Salary: "",
|
||||
|
|
@ -457,7 +465,7 @@ export class ProfileController extends Controller {
|
|||
item.positionSalaryAmount == null
|
||||
? null
|
||||
: Extension.ToThaiNumber(item.positionSalaryAmount.toString()),
|
||||
FullName: `${profiles?.prefix} ${profiles?.firstName} ${profiles?.lastName}`,
|
||||
FullName: `${profiles?.prefix}${profiles?.firstName} ${profiles?.lastName}`,
|
||||
OcFullPath: `${_child4}${_child3}${_child2}${_child1}${_root}`,
|
||||
}));
|
||||
return new HttpSuccess({
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import {
|
|||
CreateProfileEmployee,
|
||||
UpdateProfileEmployee,
|
||||
ProfileEmployeeHistory,
|
||||
UpdatePositionTempProfileEmployee
|
||||
} from "../entities/ProfileEmployee";
|
||||
import { EmployeePosLevel } from "../entities/EmployeePosLevel";
|
||||
import { EmployeePosType } from "../entities/EmployeePosType";
|
||||
|
|
@ -44,6 +45,11 @@ import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple";
|
|||
import { ProfileFamilyMother } from "../entities/ProfileFamilyMother";
|
||||
import { ProfileFamilyFather } from "../entities/ProfileFamilyFather";
|
||||
import Extension from "../interfaces/extension";
|
||||
import { OrgRoot } from "../entities/OrgRoot";
|
||||
import { OrgChild1 } from "../entities/OrgChild1";
|
||||
import { OrgChild2 } from "../entities/OrgChild2";
|
||||
import { OrgChild3 } from "../entities/OrgChild3";
|
||||
import { OrgChild4 } from "../entities/OrgChild4";
|
||||
|
||||
@Route("api/v1/org/profile-employee")
|
||||
@Tags("ProfileEmployee")
|
||||
|
|
@ -71,6 +77,11 @@ export class ProfileEmployeeController extends Controller {
|
|||
private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline);
|
||||
private educationRepository = AppDataSource.getRepository(ProfileEducation);
|
||||
private salaryRepository = AppDataSource.getRepository(ProfileSalary);
|
||||
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
|
||||
private child1Repository = AppDataSource.getRepository(OrgChild1);
|
||||
private child2Repository = AppDataSource.getRepository(OrgChild2);
|
||||
private child3Repository = AppDataSource.getRepository(OrgChild3);
|
||||
private child4Repository = AppDataSource.getRepository(OrgChild4);
|
||||
|
||||
/**
|
||||
* report ประวัติแบบย่อ ลูกจ้าง
|
||||
|
|
@ -785,11 +796,20 @@ export class ProfileEmployeeController extends Controller {
|
|||
position: _data.position,
|
||||
posNo: shortName,
|
||||
employeeClass: _data.employeeClass == null ? null : _data.employeeClass,
|
||||
dateAppoint: _data.dateAppoint,
|
||||
govAge: Extension.CalculateGovAge(_data.dateAppoint,0,0),
|
||||
age: Extension.CalculateAgeStrV2(_data.birthDate,0,0),
|
||||
dateAppoint: _data.dateAppoint,
|
||||
dateStart: _data.dateStart,
|
||||
createdAt: _data.createdAt,
|
||||
createdAt: _data.createdAt,
|
||||
dateRetireLaw: _data.dateRetireLaw,
|
||||
draftOrgEmployeeStatus: null
|
||||
draftOrganizationOrganization:
|
||||
_data.nodeTemp == "0" ? _data.rootTemp :
|
||||
_data.nodeTemp == "1" ? _data.child1Temp :
|
||||
_data.nodeTemp == "2" ? _data.child2Temp :
|
||||
_data.nodeTemp == "3" ? _data.child3Temp :
|
||||
_data.nodeTemp == "4" ? _data.child4Temp : null,
|
||||
draftPositionEmployee: _data.positionTemp,
|
||||
draftOrgEmployeeStatus: _data.statusTemp
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
|
@ -1504,6 +1524,138 @@ export class ProfileEmployeeController extends Controller {
|
|||
return new HttpSuccess(formattedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* API บันทึกตำแหน่งลูกจ้างชั่วคราว
|
||||
*
|
||||
* @summary บันทึกตำแหน่งลูกจ้างชั่วคราว (ADMIN)
|
||||
*
|
||||
* @param {string} id Id ทะเบียนประวัติลูกจ้างชั่วคราว
|
||||
*/
|
||||
@Put("position/{id}")
|
||||
async positionProfileEmployee(
|
||||
@Request() request: RequestWithUser,
|
||||
@Path() id: string,
|
||||
@Body() body: UpdatePositionTempProfileEmployee,
|
||||
) {
|
||||
if (body.posLevelId === "") body.posLevelId = null;
|
||||
if (body.posTypeId === "") body.posTypeId = null;
|
||||
|
||||
if (body.posLevelId && !(await this.posLevelRepo.findOneBy({ id: body.posLevelId }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
|
||||
}
|
||||
if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
|
||||
}
|
||||
const profileEmp = await this.profileRepo.findOneBy({ id });
|
||||
if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
|
||||
|
||||
switch (body.node) {
|
||||
case 0: {
|
||||
const data = await this.orgRootRepository.findOne({
|
||||
where: { id: body.nodeId },
|
||||
});
|
||||
if (data != null) {
|
||||
profileEmp.rootIdTemp = data.id;
|
||||
profileEmp.rootTemp = data.orgRootName;
|
||||
profileEmp.rootShortNameTemp = data.orgRootShortName;
|
||||
}
|
||||
}
|
||||
case 1: {
|
||||
const data = await this.child1Repository.findOne({
|
||||
where: { id: body.nodeId },
|
||||
relations: ["orgRoot"]
|
||||
});
|
||||
if (data != null) {
|
||||
profileEmp.rootIdTemp = data.orgRoot.id;
|
||||
profileEmp.rootTemp = data.orgRoot.orgRootName;
|
||||
profileEmp.rootShortNameTemp = data.orgRoot.orgRootShortName;
|
||||
profileEmp.child1IdTemp = data.id;
|
||||
profileEmp.child1Temp = data.orgChild1Name;
|
||||
profileEmp.child1ShortNameTemp = data.orgChild1ShortName;
|
||||
}
|
||||
}
|
||||
case 2: {
|
||||
const data = await this.child2Repository.findOne({
|
||||
where: { id: body.nodeId },
|
||||
relations: ["orgRoot", "orgChild1"]
|
||||
});
|
||||
if (data != null) {
|
||||
profileEmp.rootIdTemp = data.orgRoot.id;
|
||||
profileEmp.rootTemp = data.orgRoot.orgRootName;
|
||||
profileEmp.rootShortNameTemp = data.orgRoot.orgRootShortName;
|
||||
profileEmp.child1IdTemp = data.orgChild1.id;
|
||||
profileEmp.child1Temp = data.orgChild1.orgChild1Name;
|
||||
profileEmp.child1ShortNameTemp = data.orgChild1.orgChild1ShortName;
|
||||
profileEmp.child2IdTemp = data.id;
|
||||
profileEmp.child2Temp = data.orgChild2Name;
|
||||
profileEmp.child2ShortNameTemp = data.orgChild2ShortName;
|
||||
}
|
||||
}
|
||||
case 3: {
|
||||
const data = await this.child3Repository.findOne({
|
||||
where: { id: body.nodeId },
|
||||
relations: ["orgRoot", "orgChild1", "orgChild2"]
|
||||
});
|
||||
if (data != null) {
|
||||
profileEmp.rootIdTemp = data.orgRoot.id;
|
||||
profileEmp.rootTemp = data.orgRoot.orgRootName;
|
||||
profileEmp.rootShortNameTemp = data.orgRoot.orgRootShortName;
|
||||
profileEmp.child1IdTemp = data.orgChild1.id;
|
||||
profileEmp.child1Temp = data.orgChild1.orgChild1Name;
|
||||
profileEmp.child1ShortNameTemp = data.orgChild1.orgChild1ShortName;
|
||||
profileEmp.child2IdTemp = data.orgChild2.id;
|
||||
profileEmp.child2Temp = data.orgChild2.orgChild2Name;
|
||||
profileEmp.child2ShortNameTemp = data.orgChild2.orgChild2ShortName;
|
||||
profileEmp.child3IdTemp = data.id;
|
||||
profileEmp.child3Temp = data.orgChild3Name;
|
||||
profileEmp.child3ShortNameTemp = data.orgChild3ShortName;
|
||||
}
|
||||
}
|
||||
case 4: {
|
||||
const data = await this.child4Repository.findOne({
|
||||
where: { id: body.nodeId },
|
||||
relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3"]
|
||||
});
|
||||
if (data != null) {
|
||||
profileEmp.rootIdTemp = data.orgRoot.id;
|
||||
profileEmp.rootTemp = data.orgRoot.orgRootName;
|
||||
profileEmp.rootShortNameTemp = data.orgRoot.orgRootShortName;
|
||||
profileEmp.child1IdTemp = data.orgChild1.id;
|
||||
profileEmp.child1Temp = data.orgChild1.orgChild1Name;
|
||||
profileEmp.child1ShortNameTemp = data.orgChild1.orgChild1ShortName;
|
||||
profileEmp.child2IdTemp = data.orgChild2.id;
|
||||
profileEmp.child2Temp = data.orgChild2.orgChild2Name;
|
||||
profileEmp.child2ShortNameTemp = data.orgChild2.orgChild2ShortName;
|
||||
profileEmp.child3IdTemp = data.orgChild3.id;
|
||||
profileEmp.child3Temp = data.orgChild3.orgChild3Name;
|
||||
profileEmp.child3ShortNameTemp = data.orgChild3.orgChild3ShortName;
|
||||
profileEmp.child4IdTemp = data.id;
|
||||
profileEmp.child4Temp = data.orgChild4Name;
|
||||
profileEmp.child4ShortNameTemp = data.orgChild4ShortName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
profileEmp.lastUpdateUserId = request.user.sub;
|
||||
profileEmp.lastUpdateFullName = request.user.name;
|
||||
profileEmp.nodeTemp = String(body.node);
|
||||
profileEmp.nodeIdTemp = body.nodeId;
|
||||
profileEmp.orgRevisionIdTemp = body.orgRevisionId;
|
||||
profileEmp.posmasterIdTemp = body.posmasterId;
|
||||
profileEmp.posMasterNoTemp = body.posMasterNo;
|
||||
profileEmp.positionIdTemp = body.positionId;
|
||||
profileEmp.positionTemp = body.position;
|
||||
profileEmp.positionFieldTemp = body.positionField;
|
||||
profileEmp.posTypeIdTemp = String(body.posTypeId);
|
||||
profileEmp.posTypeNameTemp = body.posTypeName;
|
||||
profileEmp.posLevelIdTemp = String(body.posLevelId);
|
||||
profileEmp.posLevelNameTemp = body.posLevelName;
|
||||
profileEmp.statusTemp = "REPORT";
|
||||
this.profileRepo.merge(profileEmp, body);
|
||||
await this.profileRepo.save(profileEmp);
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API เช็คเลขบัตร
|
||||
*
|
||||
|
|
|
|||
|
|
@ -721,3 +721,18 @@ export type UpdateProfileAddressEmployee = {
|
|||
currentSubDistrictId?: string | null;
|
||||
currentZipCode?: string | null;
|
||||
};
|
||||
|
||||
export type UpdatePositionTempProfileEmployee = {
|
||||
posmasterId: string ;
|
||||
node: number ;
|
||||
nodeId: string ;
|
||||
orgRevisionId: string ;
|
||||
positionId: string ;
|
||||
posMasterNo: string ;
|
||||
position: string ;
|
||||
positionField: string ;
|
||||
posTypeId: string | null;
|
||||
posTypeName: string ;
|
||||
posLevelId: string | null;
|
||||
posLevelName: string ;
|
||||
};
|
||||
|
|
@ -112,6 +112,28 @@ class Extension {
|
|||
);
|
||||
}
|
||||
|
||||
public static ToThaiShortDate_noPrefix(value: Date) {
|
||||
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
||||
return (
|
||||
value.getDate() +
|
||||
" " +
|
||||
Extension.ToThaiShortMonth(value.getMonth() + 1) +
|
||||
" " +
|
||||
yy.toString().slice(-2)
|
||||
);
|
||||
}
|
||||
|
||||
public static ToThaiShortDate_perfixMonthYear(value: Date) {
|
||||
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
|
||||
return (
|
||||
value.getDate() +
|
||||
" เดือน " +
|
||||
Extension.ToThaiShortMonth(value.getMonth() + 1) +
|
||||
" พ.ศ. " +
|
||||
yy
|
||||
);
|
||||
}
|
||||
|
||||
public static sumObjectValues(array: any, propertyName: any) {
|
||||
let sum = 0;
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
|
|
@ -155,6 +177,78 @@ class Extension {
|
|||
}
|
||||
return citizen;
|
||||
}
|
||||
|
||||
public static CalculateGovAge(appointDate: Date, plusYear: number = 0, subtractYear: number = 0): number {
|
||||
if (appointDate == null || appointDate == undefined) return 0
|
||||
const now = new Date();
|
||||
if (now.getMonth() - appointDate.getMonth() >= 6) {
|
||||
return (now.getFullYear() - appointDate.getFullYear()) + 1 + plusYear - subtractYear;
|
||||
}
|
||||
else {
|
||||
return (now.getFullYear() - appointDate.getFullYear()) + plusYear - subtractYear;
|
||||
}
|
||||
}
|
||||
|
||||
public static CalculateAge(appointDate: Date, plusYear: number = 0, subtractYear: number = 0) {
|
||||
let currentDate = new Date().getTime();
|
||||
let appointDateTime = new Date(appointDate).getTime();
|
||||
let ageInMilliseconds = currentDate - appointDateTime;
|
||||
let ageInDays = ageInMilliseconds / (1000 * 60 * 60 * 24);
|
||||
let years = Math.floor(ageInDays / 365.25) + plusYear - subtractYear;
|
||||
return years;
|
||||
}
|
||||
|
||||
public static CalculateAgeStrV2(date: Date, plusYear: number = 0, subtractYear: number = 0) {
|
||||
if (date == null || date == undefined) return ""
|
||||
const currentDate = new Date();
|
||||
if (date > currentDate) {
|
||||
throw new Error("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน");
|
||||
}
|
||||
|
||||
let years = currentDate.getFullYear() - date.getFullYear();
|
||||
let months = 0;
|
||||
let days = 0;
|
||||
|
||||
if (currentDate.getMonth() < date.getMonth() || (currentDate.getMonth() === date.getMonth() && currentDate.getDate() < date.getDate())) {
|
||||
years--;
|
||||
months = 12 - date.getMonth() + currentDate.getMonth();
|
||||
|
||||
if (currentDate.getDate() < date.getDate()) {
|
||||
months--;
|
||||
let lastMonthDays = 0;
|
||||
if (currentDate.getMonth() === 0) {
|
||||
lastMonthDays = new Date(currentDate.getFullYear() - 1, 11, 0).getDate();
|
||||
}
|
||||
else {
|
||||
lastMonthDays = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
|
||||
days = lastMonthDays - date.getDate() + currentDate.getDate();
|
||||
}
|
||||
}
|
||||
else {
|
||||
days = currentDate.getDate() - date.getDate();
|
||||
}
|
||||
}
|
||||
else {
|
||||
months = currentDate.getMonth() - date.getMonth();
|
||||
|
||||
if (currentDate.getDate() < date.getDate()) {
|
||||
months--;
|
||||
let lastMonthDays = 0;
|
||||
if (currentDate.getMonth() === 0) {
|
||||
lastMonthDays = new Date(currentDate.getFullYear() - 1, 11, 0).getDate();
|
||||
}
|
||||
else {
|
||||
lastMonthDays = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
|
||||
days = lastMonthDays - date.getDate() + currentDate.getDate();
|
||||
}
|
||||
}
|
||||
else {
|
||||
days = currentDate.getDate() - date.getDate();
|
||||
}
|
||||
}
|
||||
years += plusYear - subtractYear;
|
||||
return `${years} ปี ${months} เดือน ${days} วัน`;
|
||||
}
|
||||
}
|
||||
|
||||
export default Extension;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue