diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 59a70c0..c862e93 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -1694,13 +1694,12 @@ export class SalaryPeriodController extends Controller { .take(body.pageSize) .getManyAndCount(); - const result = salaryProfile.map((profile) => ({ - ...profile, - posExecutive: - profile.positionExecutiveField - ? `${profile.posExecutive}(${profile.positionExecutiveField})` - : profile.posExecutive ?? null, - })); + const result = salaryProfile.map((profile) => ({ + ...profile, + posExecutive: profile.positionExecutiveField + ? `${profile.posExecutive}(${profile.positionExecutiveField})` + : profile.posExecutive ?? null, + })); return new HttpSuccess({ data: result, total }); } @@ -2537,293 +2536,291 @@ export class SalaryPeriodController extends Controller { @Request() request: RequestWithUser, ) { try { - snapshot = snapshot.toLocaleUpperCase(); - const salaryPeriod = await this.salaryPeriodRepository.findOne({ - where: { id: salaryPeriodId }, - }); - if (!salaryPeriod) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการสร้างเงินเดือน"); - } - - const salaryOrg = await this.salaryOrgRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, - }); - const salaryProfile = await this.salaryProfileRepository.find({ - where: { salaryOrgId: In(salaryOrg.map((x) => x.id)) }, - }); - await this.salaryProfileRepository.remove(salaryProfile, { data: request }); - await this.salaryOrgRepository.remove(salaryOrg, { data: request }); - - const salaryOrgEmployee = await this.salaryOrgEmployeeRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, - }); - const salaryProfileEmployee = await this.salaryProfileEmployeeRepository.find({ - where: { salaryOrgId: In(salaryOrgEmployee.map((x) => x.id)) }, - }); - await this.salaryProfileEmployeeRepository.remove(salaryProfileEmployee, { data: request }); - await this.salaryOrgEmployeeRepository.remove(salaryOrgEmployee, { data: request }); - let orgs = await new CallAPI().GetData(request, "/org/unauthorize/active/root/id"); - - //snap บางสำนัก - //.chin - // const targetRootIds = [ - // "d7e98989-b5ce-47d6-93c3-ab63ed486348", - // "e0545eca-5d0a-4a1c-8bbd-e3e25c2521db", - // "26989ffa-d5ab-4bbd-ac97-130646cd1da6", - // "6f9b30e1-757a-40d5-b053-61eb1b91c0f0", - // "eaf65f33-25e9-4956-9dba-5d909f5eb595", - // "a3efed2c-3f4b-476d-95e6-9f7e0585ae25", - // ]; - //.me - // const targetRootIds = [ - // "b89a4467-7ee3-4706-8db7-f366555f826c", - // "585648a9-e634-43fc-9360-5fd4189136ab", - // "d6e3daa0-284a-428f-aa43-0750fa74e974", - // "ed3ddcfb-f882-4499-817b-aff73e5be87c", - // "f8ce98ca-a691-4c89-abde-875f559afb3a", - // ]; - - // orgs = orgs.filter((x: any) => targetRootIds.includes(x.rootId)); - let total = 1000; - // let _orgProfiles = await new CallAPI().PostData(request, "/org/unauthorize/salary/gen", { - let _orgProfiles = await new CallAPI().PostData(request, "/org/unauthorize/new-salary/gen", { - page: 1, - pageSize: 1000, - keyword: "", - year: salaryPeriod.year, - period: salaryPeriod.period, - }); - let orgProfiles = _orgProfiles.data; - total = _orgProfiles.total; - console.log(`total: ${total}`); - - if (total > 1000) { - const page = Math.ceil(total / 1000); - for (let index = 2; index <= page; index++) { - await new CallAPI() - // .PostData(request, "/org/unauthorize/salary/gen", { - .PostData(request, "/org/unauthorize/new-salary/gen", { - page: index, - pageSize: 1000, - keyword: "", - year: salaryPeriod.year, - period: salaryPeriod.period, - }) - .then((x) => { - Array.prototype.push.apply(orgProfiles, x.data); - }); + snapshot = snapshot.toLocaleUpperCase(); + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: salaryPeriodId }, + }); + if (!salaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการสร้างเงินเดือน"); } - } - total = 1000; - let orgProfileEmployees: any; - let _orgProfileEmployees = await new CallAPI().PostData( - request, - // "/org/unauthorize/salary/employee/gen", - "/org/unauthorize/new-salary/employee/gen", - { + + const salaryOrg = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + }); + const salaryProfile = await this.salaryProfileRepository.find({ + where: { salaryOrgId: In(salaryOrg.map((x) => x.id)) }, + }); + await this.salaryProfileRepository.remove(salaryProfile, { data: request }); + await this.salaryOrgRepository.remove(salaryOrg, { data: request }); + + const salaryOrgEmployee = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + }); + const salaryProfileEmployee = await this.salaryProfileEmployeeRepository.find({ + where: { salaryOrgId: In(salaryOrgEmployee.map((x) => x.id)) }, + }); + await this.salaryProfileEmployeeRepository.remove(salaryProfileEmployee, { data: request }); + await this.salaryOrgEmployeeRepository.remove(salaryOrgEmployee, { data: request }); + let orgs = await new CallAPI().GetData(request, "/org/unauthorize/active/root/id"); + + //snap บางสำนัก + //.chin + // const targetRootIds = [ + // "d7e98989-b5ce-47d6-93c3-ab63ed486348", + // "e0545eca-5d0a-4a1c-8bbd-e3e25c2521db", + // "26989ffa-d5ab-4bbd-ac97-130646cd1da6", + // "6f9b30e1-757a-40d5-b053-61eb1b91c0f0", + // "eaf65f33-25e9-4956-9dba-5d909f5eb595", + // "a3efed2c-3f4b-476d-95e6-9f7e0585ae25", + // ]; + //.me + // const targetRootIds = [ + // "b89a4467-7ee3-4706-8db7-f366555f826c", + // "585648a9-e634-43fc-9360-5fd4189136ab", + // "d6e3daa0-284a-428f-aa43-0750fa74e974", + // "ed3ddcfb-f882-4499-817b-aff73e5be87c", + // "f8ce98ca-a691-4c89-abde-875f559afb3a", + // ]; + + // orgs = orgs.filter((x: any) => targetRootIds.includes(x.rootId)); + let total = 1000; + // let _orgProfiles = await new CallAPI().PostData(request, "/org/unauthorize/salary/gen", { + let _orgProfiles = await new CallAPI().PostData(request, "/org/unauthorize/new-salary/gen", { page: 1, pageSize: 1000, keyword: "", year: salaryPeriod.year, period: salaryPeriod.period, - }, - ); - orgProfileEmployees = _orgProfileEmployees.data; - total = _orgProfileEmployees.total; - console.log(`totalEmp: ${total}`); - if (total > 1000) { - const page = Math.ceil(total / 1000); - for (let index = 2; index <= page; index++) { - await new CallAPI() - // .PostData(request, "/org/unauthorize/salary/employee/gen", { - .PostData(request, "/org/unauthorize/new-salary/employee/gen", { - page: index, - pageSize: 1000, - keyword: "", - year: salaryPeriod.year, - period: salaryPeriod.period, - }) - .then((x) => { - Array.prototype.push.apply(orgProfileEmployees, x.data); - }); - } - } - - let revisionId = await new CallAPI().GetData(request, "/org/unauthorize/revision/latest"); - let _null: any = null; - const beforeSalaryPeriod = structuredClone(salaryPeriod); - salaryPeriod.revisionId = revisionId; - salaryPeriod.lastUpdateUserId = request.user.sub; - salaryPeriod.lastUpdateFullName = request.user.name; - salaryPeriod.lastUpdatedAt = new Date(); - await this.salaryPeriodRepository.save(salaryPeriod, { data: request }); - setLogDataDiff(request, { before: beforeSalaryPeriod, after: salaryPeriod }); - - await Promise.all( - orgs.map(async (root: any) => { - let salaryOrgNew = Object.assign(new SalaryOrg()); - delete salaryOrgNew.id; - const beforeSalaryOrgNew = structuredClone(salaryOrgNew); - - salaryOrgNew.salaryPeriodId = salaryPeriod.id; - salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = root.rootId; - salaryOrgNew.rootDnaId = root.rootDnaId ?? _null; - salaryOrgNew.root = root.root; - salaryOrgNew.revisionId = salaryPeriod.revisionId; - salaryOrgNew.snapshot = snapshot; - salaryOrgNew.createdUserId = request.user.sub; - salaryOrgNew.createdFullName = request.user.name; - salaryOrgNew.lastUpdateUserId = request.user.sub; - salaryOrgNew.lastUpdateFullName = request.user.name; - salaryOrgNew.createdAt = new Date(); - salaryOrgNew.lastUpdatedAt = new Date(); - salaryOrgNew.group = "GROUP1"; - await this.salaryOrgRepository.save(salaryOrgNew, { data: request }); - setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); - console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgNew: ${salaryOrgNew.id}`); - if (salaryPeriod.period != "SPECIAL") { - delete salaryOrgNew.id; - salaryOrgNew.group = "GROUP2"; - await this.salaryOrgRepository.save(salaryOrgNew, { data: request }); - setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); - } - }), - ); - - await Promise.all( - orgs.map(async (root: any) => { - let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); - const beforeSalaryOrgNew = structuredClone(salaryOrgNew); - - salaryOrgNew.salaryPeriodId = salaryPeriod.id; - salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = root.rootId; - salaryOrgNew.rootDnaId = root.rootDnaId ?? _null; - salaryOrgNew.root = root.root; - salaryOrgNew.revisionId = salaryPeriod.revisionId; - salaryOrgNew.snapshot = snapshot; - salaryOrgNew.createdUserId = request.user.sub; - salaryOrgNew.createdFullName = request.user.name; - salaryOrgNew.lastUpdateUserId = request.user.sub; - salaryOrgNew.lastUpdateFullName = request.user.name; - salaryOrgNew.group = "GROUP1"; - salaryOrgNew.createdAt = new Date(); - salaryOrgNew.lastUpdatedAt = new Date(); - await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request }); - setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); - console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgEmployeeNew: ${salaryOrgNew.id}`); - if (salaryPeriod.period != "SPECIAL") { - delete salaryOrgNew.id; - salaryOrgNew.group = "GROUP2"; - await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request }); - setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); - } - }), - ); - if (salaryPeriod.period != "SPECIAL") { - //console.log(`step1`); - console.time("⏱ Step1: Load SalaryOrg (New)"); - const salaryOrgList = await this.salaryOrgRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot }, }); - const salaryOrgMap = new Map( - salaryOrgList.map((org) => [`${org.rootId}-${org.group}`, org]) + let orgProfiles = _orgProfiles.data; + total = _orgProfiles.total; + console.log(`total: ${total}`); + + if (total > 1000) { + const page = Math.ceil(total / 1000); + for (let index = 2; index <= page; index++) { + await new CallAPI() + // .PostData(request, "/org/unauthorize/salary/gen", { + .PostData(request, "/org/unauthorize/new-salary/gen", { + page: index, + pageSize: 1000, + keyword: "", + year: salaryPeriod.year, + period: salaryPeriod.period, + }) + .then((x) => { + Array.prototype.push.apply(orgProfiles, x.data); + }); + } + } + total = 1000; + let orgProfileEmployees: any; + let _orgProfileEmployees = await new CallAPI().PostData( + request, + // "/org/unauthorize/salary/employee/gen", + "/org/unauthorize/new-salary/employee/gen", + { + page: 1, + pageSize: 1000, + keyword: "", + year: salaryPeriod.year, + period: salaryPeriod.period, + }, ); - console.timeEnd("⏱ Step1: Load SalaryOrg (New)"); + orgProfileEmployees = _orgProfileEmployees.data; + total = _orgProfileEmployees.total; + console.log(`totalEmp: ${total}`); + if (total > 1000) { + const page = Math.ceil(total / 1000); + for (let index = 2; index <= page; index++) { + await new CallAPI() + // .PostData(request, "/org/unauthorize/salary/employee/gen", { + .PostData(request, "/org/unauthorize/new-salary/employee/gen", { + page: index, + pageSize: 1000, + keyword: "", + year: salaryPeriod.year, + period: salaryPeriod.period, + }) + .then((x) => { + Array.prototype.push.apply(orgProfileEmployees, x.data); + }); + } + } - console.time("⏱ Step2: Load SalaryProfileOld (SNAP1)"); - let salaryOldMap = new Map(); - if (snapshot === "SNAP2") { - const salaryOldList = await this.salaryProfileRepository - .createQueryBuilder("profile") - .innerJoin("profile.salaryOrg", "org") - .where("org.salaryPeriodId = :periodId", { periodId: salaryPeriod.id }) - .andWhere("org.snapshot = :snapshot", { snapshot: "SNAP1" }) - .getMany(); + let revisionId = await new CallAPI().GetData(request, "/org/unauthorize/revision/latest"); + let _null: any = null; + // const beforeSalaryPeriod = structuredClone(salaryPeriod); + salaryPeriod.revisionId = revisionId; + salaryPeriod.lastUpdateUserId = request.user.sub; + salaryPeriod.lastUpdateFullName = request.user.name; + salaryPeriod.lastUpdatedAt = new Date(); + await this.salaryPeriodRepository.save(salaryPeriod, { data: request }); + // setLogDataDiff(request, { before: beforeSalaryPeriod, after: salaryPeriod }); - salaryOldMap = new Map( - salaryOldList.map((item) => [item.citizenId, item]) + await Promise.all( + orgs.map(async (root: any) => { + let salaryOrgNew = Object.assign(new SalaryOrg()); + delete salaryOrgNew.id; + // const beforeSalaryOrgNew = structuredClone(salaryOrgNew); + + salaryOrgNew.salaryPeriodId = salaryPeriod.id; + salaryOrgNew.status = "PENDING"; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.rootDnaId = root.rootDnaId ?? _null; + salaryOrgNew.root = root.root; + salaryOrgNew.revisionId = salaryPeriod.revisionId; + salaryOrgNew.snapshot = snapshot; + salaryOrgNew.createdUserId = request.user.sub; + salaryOrgNew.createdFullName = request.user.name; + salaryOrgNew.lastUpdateUserId = request.user.sub; + salaryOrgNew.lastUpdateFullName = request.user.name; + salaryOrgNew.createdAt = new Date(); + salaryOrgNew.lastUpdatedAt = new Date(); + salaryOrgNew.group = "GROUP1"; + await this.salaryOrgRepository.save(salaryOrgNew, { data: request }); + // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); + console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgNew: ${salaryOrgNew.id}`); + if (salaryPeriod.period != "SPECIAL") { + delete salaryOrgNew.id; + salaryOrgNew.group = "GROUP2"; + await this.salaryOrgRepository.save(salaryOrgNew, { data: request }); + // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); + } + }), + ); + + await Promise.all( + orgs.map(async (root: any) => { + let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); + // const beforeSalaryOrgNew = structuredClone(salaryOrgNew); + + salaryOrgNew.salaryPeriodId = salaryPeriod.id; + salaryOrgNew.status = "PENDING"; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.rootDnaId = root.rootDnaId ?? _null; + salaryOrgNew.root = root.root; + salaryOrgNew.revisionId = salaryPeriod.revisionId; + salaryOrgNew.snapshot = snapshot; + salaryOrgNew.createdUserId = request.user.sub; + salaryOrgNew.createdFullName = request.user.name; + salaryOrgNew.lastUpdateUserId = request.user.sub; + salaryOrgNew.lastUpdateFullName = request.user.name; + salaryOrgNew.group = "GROUP1"; + salaryOrgNew.createdAt = new Date(); + salaryOrgNew.lastUpdatedAt = new Date(); + await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request }); + // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); + console.log(`✅ [SNAP: ${snapshot}] บันทึก salaryOrgEmployeeNew: ${salaryOrgNew.id}`); + if (salaryPeriod.period != "SPECIAL") { + delete salaryOrgNew.id; + salaryOrgNew.group = "GROUP2"; + await this.salaryOrgEmployeeRepository.save(salaryOrgNew, { data: request }); + // setLogDataDiff(request, { before: beforeSalaryOrgNew, after: salaryOrgNew }); + } + }), + ); + if (salaryPeriod.period != "SPECIAL") { + //console.log(`step1`); + console.time("⏱ Step1: Load SalaryOrg (New)"); + const salaryOrgList = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot }, + }); + const salaryOrgMap = new Map( + salaryOrgList.map((org) => [`${org.rootId}-${org.group}`, org]), ); - } - console.timeEnd("⏱ Step2: Load SalaryProfileOld (SNAP1)"); - - console.time("⏱ Step3: Process Profiles"); - - const allProfilesToSave: SalaryProfile[] = []; - - for (const profile of orgProfiles) { - let group = "GROUP1"; - const { posType, posLevel } = profile; - - if ( - (posType === "ทั่วไป" && posLevel === "ทักษะพิเศษ") || - (posType === "วิชาการ" && ["เชี่ยวชาญ", "ทรงคุณวุฒิ"].includes(posLevel)) || - (posType === "อำนวยการ" && posLevel === "สูง") || - (posType === "บริหาร" && ["ต้น", "สูง"].includes(posLevel)) - ) { - group = "GROUP2"; - } - - const salaryOrgNew = salaryOrgMap.get(`${profile.rootId}-${group}`); - if (!salaryOrgNew) { - console.warn(`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrg สำหรับ rootId: ${profile.rootId}`); - continue; - } - - const salaryProfileNew = Object.assign(new SalaryProfile(), profile); - delete salaryProfileNew.id; - - salaryProfileNew.salaryOrgId = salaryOrgNew.id; - salaryProfileNew.revisionId = salaryPeriod.revisionId; - salaryProfileNew.createdUserId = request.user.sub; - salaryProfileNew.createdFullName = request.user.name; - salaryProfileNew.lastUpdateUserId = request.user.sub; - salaryProfileNew.lastUpdateFullName = request.user.name; - salaryProfileNew.createdAt = new Date(); - salaryProfileNew.lastUpdatedAt = new Date(); + console.timeEnd("⏱ Step1: Load SalaryOrg (New)"); + console.time("⏱ Step2: Load SalaryProfileOld (SNAP1)"); + let salaryOldMap = new Map(); if (snapshot === "SNAP2") { - const salaryOld = salaryOldMap.get(salaryProfileNew.citizenId); - salaryProfileNew.type = salaryOld?.type ?? "PENDING"; - salaryProfileNew.amount = salaryOld?.amount ?? 0; - salaryProfileNew.amountSpecial = salaryOld?.amountSpecial ?? 0; - salaryProfileNew.amountUse = salaryOld?.amountUse ?? 0; - salaryProfileNew.positionSalaryAmount = salaryOld?.positionSalaryAmount ?? 0; - salaryProfileNew.remark = salaryOld?.remark ?? null; - salaryProfileNew.isNext = salaryOld?.isNext ?? false; - salaryProfileNew.isSpecial = salaryOld?.isSpecial ?? false; - salaryProfileNew.isReserve = salaryOld?.isReserve ?? false; - salaryProfileNew.isRetired = salaryOld?.isRetired ?? false; - salaryProfileNew.isGood = salaryOld?.isGood ?? false; + const salaryOldList = await this.salaryProfileRepository + .createQueryBuilder("profile") + .innerJoin("profile.salaryOrg", "org") + .where("org.salaryPeriodId = :periodId", { periodId: salaryPeriod.id }) + .andWhere("org.snapshot = :snapshot", { snapshot: "SNAP1" }) + .getMany(); + + salaryOldMap = new Map(salaryOldList.map((item) => [item.citizenId, item])); + } + console.timeEnd("⏱ Step2: Load SalaryProfileOld (SNAP1)"); + + console.time("⏱ Step3: Process Profiles"); + + const allProfilesToSave: SalaryProfile[] = []; + + for (const profile of orgProfiles) { + let group = "GROUP1"; + const { posType, posLevel } = profile; + + if ( + (posType === "ทั่วไป" && posLevel === "ทักษะพิเศษ") || + (posType === "วิชาการ" && ["เชี่ยวชาญ", "ทรงคุณวุฒิ"].includes(posLevel)) || + (posType === "อำนวยการ" && posLevel === "สูง") || + (posType === "บริหาร" && ["ต้น", "สูง"].includes(posLevel)) + ) { + group = "GROUP2"; + } + + const salaryOrgNew = salaryOrgMap.get(`${profile.rootId}-${group}`); + if (!salaryOrgNew) { + console.warn(`⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrg สำหรับ rootId: ${profile.rootId}`); + continue; + } + + const salaryProfileNew = Object.assign(new SalaryProfile(), profile); + delete salaryProfileNew.id; + + salaryProfileNew.salaryOrgId = salaryOrgNew.id; + salaryProfileNew.revisionId = salaryPeriod.revisionId; + salaryProfileNew.createdUserId = request.user.sub; + salaryProfileNew.createdFullName = request.user.name; + salaryProfileNew.lastUpdateUserId = request.user.sub; + salaryProfileNew.lastUpdateFullName = request.user.name; + salaryProfileNew.createdAt = new Date(); + salaryProfileNew.lastUpdatedAt = new Date(); + + if (snapshot === "SNAP2") { + const salaryOld = salaryOldMap.get(salaryProfileNew.citizenId); + salaryProfileNew.type = salaryOld?.type ?? "PENDING"; + salaryProfileNew.amount = salaryOld?.amount ?? 0; + salaryProfileNew.amountSpecial = salaryOld?.amountSpecial ?? 0; + salaryProfileNew.amountUse = salaryOld?.amountUse ?? 0; + salaryProfileNew.positionSalaryAmount = salaryOld?.positionSalaryAmount ?? 0; + salaryProfileNew.remark = salaryOld?.remark ?? null; + salaryProfileNew.isNext = salaryOld?.isNext ?? false; + salaryProfileNew.isSpecial = salaryOld?.isSpecial ?? false; + salaryProfileNew.isReserve = salaryOld?.isReserve ?? false; + salaryProfileNew.isRetired = salaryOld?.isRetired ?? false; + salaryProfileNew.isGood = salaryOld?.isGood ?? false; + } + + // const beforeSalaryProfileNew = structuredClone(salaryProfileNew); + // setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); + allProfilesToSave.push(salaryProfileNew); } - const beforeSalaryProfileNew = structuredClone(salaryProfileNew); - setLogDataDiff(request, { before: beforeSalaryProfileNew, after: salaryProfileNew }); - allProfilesToSave.push(salaryProfileNew); - } + console.timeEnd("⏱ Step3: Process Profiles"); - console.timeEnd("⏱ Step3: Process Profiles"); + console.time("⏱ Step4: Save All Profiles"); + // await this.salaryProfileRepository.save(allProfilesToSave, { chunk: 500, data: request }); - console.time("⏱ Step4: Save All Profiles"); - // await this.salaryProfileRepository.save(allProfilesToSave, { chunk: 500, data: request }); + await this.salaryProfileRepository + .createQueryBuilder() + .insert() + .into(SalaryProfile) + .values(allProfilesToSave) + .orIgnore() + .execute(); + console.timeEnd("⏱ Step4: Save All Profiles"); - await this.salaryProfileRepository - .createQueryBuilder() - .insert() - .into(SalaryProfile) - .values(allProfilesToSave) - .orIgnore() - .execute(); - console.timeEnd("⏱ Step4: Save All Profiles"); + console.timeEnd("⏱ TOTAL SalaryProfile Process"); - console.timeEnd("⏱ TOTAL SalaryProfile Process"); - - //**********************/ - console.log("mlength", orgProfileEmployees.length); - const profilesEmpToSave: SalaryProfileEmployee[] = []; - console.time("⏱ SalaryProfileEmployee - Total Time"); - for (const profile of orgProfileEmployees) { + //**********************/ + console.log("mlength", orgProfileEmployees.length); + const profilesEmpToSave: SalaryProfileEmployee[] = []; + console.time("⏱ SalaryProfileEmployee - Total Time"); + for (const profile of orgProfileEmployees) { const salaryOrgNew = await this.salaryOrgEmployeeRepository.findOne({ where: { salaryPeriodId: salaryPeriod.id, @@ -2837,7 +2834,7 @@ export class SalaryPeriodController extends Controller { //console.log(`step8`); let salaryProfileNew = Object.assign(new SalaryProfileEmployee(), profile); delete salaryProfileNew.id; - const beforeSalaryProfileNew = structuredClone(salaryProfileNew); + // const beforeSalaryProfileNew = structuredClone(salaryProfileNew); salaryProfileNew.salaryOrgId = salaryOrgNew.id; salaryProfileNew.revisionId = salaryPeriod.revisionId; @@ -2872,7 +2869,7 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; salaryProfileNew.positionSalaryDayAmount = - salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; + salaryOld == null ? 0 : salaryOld.positionSalaryDayAmount; salaryProfileNew.positionSalaryAmountPer = salaryOld == null ? 0 : salaryOld.positionSalaryAmountPer; salaryProfileNew.remark = salaryOld == null ? null : salaryOld.remark; @@ -2903,138 +2900,169 @@ export class SalaryPeriodController extends Controller { `⚠️ [SNAP: ${snapshot}] ไม่พบ salaryOrgEmployee สำหรับ rootId: ${profile.rootId}`, ); } - }; + }; console.timeEnd("⏱ SalaryProfileEmployee - Total Time"); } - const salaryOrgNew = await this.salaryOrgRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, - relations: ["salaryProfiles"], - }); - const salaryOrgEmployeeNew = await this.salaryOrgEmployeeRepository.find({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, - relations: ["salaryProfiles"], - }); - if (salaryPeriod.period == "OCT") { - const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ - where: { - year: salaryPeriod.year, - period: "APR", - }, + const salaryOrgNew = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + relations: ["salaryProfiles"], }); - await Promise.all( - salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { - let totalAmount = 0; - if (salaryPeriodAPROld != null) { - const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ - where: { - salaryPeriodId: salaryPeriodAPROld.id, - rootDnaId: _salaryOrg.rootDnaId, - group: _salaryOrg.group, - snapshot: "SNAP2", - }, - relations: ["salaryProfiles"], - }); - totalAmount = - salaryOrgSnap2Old == null - ? 0 - : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); - } + const salaryOrgEmployeeNew = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + relations: ["salaryProfiles"], + }); + if (salaryPeriod.period == "OCT") { + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: salaryPeriod.year, + period: "APR", + }, + }); + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + let totalAmount = 0; + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootDnaId: _salaryOrg.rootDnaId, + group: _salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } - const before_salaryOrg = structuredClone(_salaryOrg); - if (snapshot == "SNAP2") { - const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ - where: { - salaryPeriodId: salaryPeriod.id, - rootDnaId: _salaryOrg.rootDnaId, - group: _salaryOrg.group, - snapshot: "SNAP1", - }, - }); - if (salaryOrgSnap1 == null) { + const before_salaryOrg = structuredClone(_salaryOrg); + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootDnaId: _salaryOrg.rootDnaId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.sixPercentAmount = totalProfile * 0.06; + _salaryOrg.spentAmount = totalAmount; + _salaryOrg.remainingAmount = totalProfile * 0.06 - totalAmount; + + //เพิ่มคำนวน 15% + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor( + (_salaryOrg.salaryProfiles.length * 15) / 100, + ); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + } else { + _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; + _salaryOrg.total = salaryOrgSnap1.total; + _salaryOrg.sixPercentAmount = salaryOrgSnap1.sixPercentAmount; + _salaryOrg.spentAmount = salaryOrgSnap1.spentAmount; + _salaryOrg.useAmount = salaryOrgSnap1.useAmount; + _salaryOrg.remainingAmount = salaryOrgSnap1.remainingAmount; + + //เพิ่มคำนวน 15% + _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; + _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; + _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; + _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; + } + } else { const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; _salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.sixPercentAmount = totalProfile * 0.06; _salaryOrg.spentAmount = totalAmount; _salaryOrg.remainingAmount = totalProfile * 0.06 - totalAmount; - + //เพิ่มคำนวน 15% _salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - } else { - _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; - _salaryOrg.total = salaryOrgSnap1.total; - _salaryOrg.sixPercentAmount = salaryOrgSnap1.sixPercentAmount; - _salaryOrg.spentAmount = salaryOrgSnap1.spentAmount; - _salaryOrg.useAmount = salaryOrgSnap1.useAmount; - _salaryOrg.remainingAmount = salaryOrgSnap1.remainingAmount; - - //เพิ่มคำนวน 15% - _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; - _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; - _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; - _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; } - } else { - const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); - _salaryOrg.currentAmount = totalProfile; - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.sixPercentAmount = totalProfile * 0.06; - _salaryOrg.spentAmount = totalAmount; - _salaryOrg.remainingAmount = totalProfile * 0.06 - totalAmount; - - //เพิ่มคำนวน 15% - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; - _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - } + _salaryOrg.createdUserId = request.user.sub; + _salaryOrg.createdFullName = request.user.name; + _salaryOrg.lastUpdateUserId = request.user.sub; + _salaryOrg.lastUpdateFullName = request.user.name; + _salaryOrg.createdAt = new Date(); + _salaryOrg.lastUpdatedAt = new Date(); + await this.salaryOrgRepository.save(_salaryOrg, { data: request }); + setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); + }), + ); + await Promise.all( + salaryOrgEmployeeNew.map(async (_salaryOrg: SalaryOrgEmployee) => { + let totalAmount = 0; + const before_salaryOrg = structuredClone(_salaryOrg); - _salaryOrg.createdUserId = request.user.sub; - _salaryOrg.createdFullName = request.user.name; - _salaryOrg.lastUpdateUserId = request.user.sub; - _salaryOrg.lastUpdateFullName = request.user.name; - _salaryOrg.createdAt = new Date(); - _salaryOrg.lastUpdatedAt = new Date(); - await this.salaryOrgRepository.save(_salaryOrg, { data: request }); - setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); - }), - ); - await Promise.all( - salaryOrgEmployeeNew.map(async (_salaryOrg: SalaryOrgEmployee) => { - let totalAmount = 0; - const before_salaryOrg = structuredClone(_salaryOrg); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgEmployeeRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootDnaId: _salaryOrg.rootDnaId, + group: _salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } - if (salaryPeriodAPROld != null) { - const salaryOrgSnap2Old: any = await this.salaryOrgEmployeeRepository.findOne({ - where: { - salaryPeriodId: salaryPeriodAPROld.id, - rootDnaId: _salaryOrg.rootDnaId, - group: _salaryOrg.group, - snapshot: "SNAP2", - }, - relations: ["salaryProfiles"], - }); - totalAmount = - salaryOrgSnap2Old == null - ? 0 - : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); - } + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgEmployeeRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootDnaId: _salaryOrg.rootDnaId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.sixPercentAmount = totalProfile * 0.06; + _salaryOrg.spentAmount = totalAmount; + _salaryOrg.remainingAmount = totalProfile * 0.06 - totalAmount; - if (snapshot == "SNAP2") { - const salaryOrgSnap1 = await this.salaryOrgEmployeeRepository.findOne({ - where: { - salaryPeriodId: salaryPeriod.id, - rootDnaId: _salaryOrg.rootDnaId, - group: _salaryOrg.group, - snapshot: "SNAP1", - }, - }); - if (salaryOrgSnap1 == null) { + //เพิ่มคำนวน 15% + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor( + (_salaryOrg.salaryProfiles.length * 15) / 100, + ); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + } else { + _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; + _salaryOrg.total = salaryOrgSnap1.total; + _salaryOrg.sixPercentAmount = salaryOrgSnap1.sixPercentAmount; + _salaryOrg.spentAmount = salaryOrgSnap1.spentAmount; + _salaryOrg.useAmount = salaryOrgSnap1.useAmount; + _salaryOrg.remainingAmount = salaryOrgSnap1.remainingAmount; + + //เพิ่มคำนวน 15% + _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; + _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; + _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; + _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; + } + } else { const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); _salaryOrg.currentAmount = totalProfile; _salaryOrg.total = _salaryOrg.salaryProfiles.length; @@ -3047,141 +3075,113 @@ export class SalaryPeriodController extends Controller { _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - - } else { - _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; - _salaryOrg.total = salaryOrgSnap1.total; - _salaryOrg.sixPercentAmount = salaryOrgSnap1.sixPercentAmount; - _salaryOrg.spentAmount = salaryOrgSnap1.spentAmount; - _salaryOrg.useAmount = salaryOrgSnap1.useAmount; - _salaryOrg.remainingAmount = salaryOrgSnap1.remainingAmount; - - //เพิ่มคำนวน 15% - _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; - _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; - _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; - _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; - } - } else { - const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); - _salaryOrg.currentAmount = totalProfile; - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.sixPercentAmount = totalProfile * 0.06; - _salaryOrg.spentAmount = totalAmount; - _salaryOrg.remainingAmount = totalProfile * 0.06 - totalAmount; - //เพิ่มคำนวน 15% - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; - _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + _salaryOrg.createdUserId = request.user.sub; + _salaryOrg.createdFullName = request.user.name; + _salaryOrg.lastUpdateUserId = request.user.sub; + _salaryOrg.lastUpdateFullName = request.user.name; + _salaryOrg.createdAt = new Date(); + _salaryOrg.lastUpdatedAt = new Date(); + await this.salaryOrgEmployeeRepository.save(_salaryOrg, { data: request }); + setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); + }), + ); + } else if (salaryPeriod.period == "APR") { + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + // const before_salaryOrg = structuredClone(_salaryOrg); - } - - _salaryOrg.createdUserId = request.user.sub; - _salaryOrg.createdFullName = request.user.name; - _salaryOrg.lastUpdateUserId = request.user.sub; - _salaryOrg.lastUpdateFullName = request.user.name; - _salaryOrg.createdAt = new Date(); - _salaryOrg.lastUpdatedAt = new Date(); - await this.salaryOrgEmployeeRepository.save(_salaryOrg, { data: request }); - setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); - }), - ); - } else if (salaryPeriod.period == "APR") { - await Promise.all( - salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { - const before_salaryOrg = structuredClone(_salaryOrg); - - if (snapshot == "SNAP2") { - const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ - where: { - salaryPeriodId: salaryPeriod.id, - rootDnaId: _salaryOrg.rootDnaId, - group: _salaryOrg.group, - snapshot: "SNAP1", - }, - }); - if (salaryOrgSnap1 == null) { + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootDnaId: _salaryOrg.rootDnaId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor( + (_salaryOrg.salaryProfiles.length * 15) / 100, + ); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + } else { + _salaryOrg.total = salaryOrgSnap1.total; + _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; + _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; + _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; + _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; + } + } else { _salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - } else { - _salaryOrg.total = salaryOrgSnap1.total; - _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; - _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; - _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; - _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; } - } else { - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; - _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - } - _salaryOrg.createdUserId = request.user.sub; - _salaryOrg.createdFullName = request.user.name; - _salaryOrg.lastUpdateUserId = request.user.sub; - _salaryOrg.lastUpdateFullName = request.user.name; - _salaryOrg.createdAt = new Date(); - _salaryOrg.lastUpdatedAt = new Date(); - await this.salaryOrgRepository.save(_salaryOrg, { data: request }); - setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); - }), - ); - await Promise.all( - salaryOrgEmployeeNew.map(async (_salaryOrg: SalaryOrgEmployee) => { - const before_salaryOrg = structuredClone(_salaryOrg); + _salaryOrg.createdUserId = request.user.sub; + _salaryOrg.createdFullName = request.user.name; + _salaryOrg.lastUpdateUserId = request.user.sub; + _salaryOrg.lastUpdateFullName = request.user.name; + _salaryOrg.createdAt = new Date(); + _salaryOrg.lastUpdatedAt = new Date(); + await this.salaryOrgRepository.save(_salaryOrg, { data: request }); + // setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); + }), + ); + await Promise.all( + salaryOrgEmployeeNew.map(async (_salaryOrg: SalaryOrgEmployee) => { + // const before_salaryOrg = structuredClone(_salaryOrg); - if (snapshot == "SNAP2") { - const salaryOrgSnap1 = await this.salaryOrgEmployeeRepository.findOne({ - where: { - salaryPeriodId: salaryPeriod.id, - rootDnaId: _salaryOrg.rootDnaId, - group: _salaryOrg.group, - snapshot: "SNAP1", - }, - }); - if (salaryOrgSnap1 == null) { + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgEmployeeRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootDnaId: _salaryOrg.rootDnaId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor( + (_salaryOrg.salaryProfiles.length * 15) / 100, + ); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + // console.log("fifteenAPR: case 1"); + } else { + _salaryOrg.total = salaryOrgSnap1.total; + _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; + _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; + _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; + _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; + // console.log("fifteenAPR: case 2"); + } + } else { _salaryOrg.total = _salaryOrg.salaryProfiles.length; _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - // console.log("fifteenAPR: case 1"); - } else { - _salaryOrg.total = salaryOrgSnap1.total; - _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; - _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; - _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; - _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; - // console.log("fifteenAPR: case 2"); + // console.log("fifteenAPR: case 3"); } - } else { - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; - _salaryOrg.remainQuota = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - // console.log("fifteenAPR: case 3"); - } - _salaryOrg.createdUserId = request.user.sub; - _salaryOrg.createdFullName = request.user.name; - _salaryOrg.lastUpdateUserId = request.user.sub; - _salaryOrg.lastUpdateFullName = request.user.name; - _salaryOrg.createdAt = new Date(); - _salaryOrg.lastUpdatedAt = new Date(); - await this.salaryOrgEmployeeRepository.save(_salaryOrg, { data: request }); - setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); - }), - ); - } - console.log( - `✅✅✅ [SNAPSHOT:${snapshot} เสร็จสิ้น]) ✅✅✅`, - ); - return new HttpSuccess(); + _salaryOrg.createdUserId = request.user.sub; + _salaryOrg.createdFullName = request.user.name; + _salaryOrg.lastUpdateUserId = request.user.sub; + _salaryOrg.lastUpdateFullName = request.user.name; + _salaryOrg.createdAt = new Date(); + _salaryOrg.lastUpdatedAt = new Date(); + await this.salaryOrgEmployeeRepository.save(_salaryOrg, { data: request }); + // setLogDataDiff(request, { before: before_salaryOrg, after: _salaryOrg }); + }), + ); + } + console.log(`✅✅✅ [SNAPSHOT:${snapshot} เสร็จสิ้น]) ✅✅✅`); + return new HttpSuccess(); } catch (err) { console.error(`❌ error processing employee:`, err); }