diff --git a/src/entities/view/viewCurrentTenureEmployee.ts b/src/entities/view/viewCurrentTenureEmployee.ts new file mode 100644 index 00000000..294e7045 --- /dev/null +++ b/src/entities/view/viewCurrentTenureEmployee.ts @@ -0,0 +1,151 @@ +import { ViewColumn, ViewEntity } from "typeorm"; + +@ViewEntity({ + expression: ` + SELECT + DATE(MIN(commandDateAffect)) AS commandDateAffect, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) AS days_diff, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days', + positionName, + positionCee, + posNo, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileEmployeeId, + ROW_NUMBER() OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect ASC) AS orderNumber + FROM + profileSalary + WHERE + profileEmployeeId IS NOT NULL AND + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileEmployeeId, positionName + + UNION ALL + + SELECT + CURDATE() AS commandDateAffect, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', + NULL AS positionName, + NULL AS positionCee, + NULL AS posNo, + NULL AS positionType, + NULL AS positionLevel, + NULL AS OrgRoot, + NULL AS orgChild1, + NULL AS orgChild2, + NULL AS orgChild3, + NULL AS orgChild4, + NULL AS commandCode, + NULL AS commandName, + NULL AS commandNo, + NULL AS commandYear, + 'Comparison with current date' AS remark, + profileEmployeeId, + NULL AS orderNumber + FROM + profileSalary + WHERE + profileEmployeeId IS NOT NULL AND + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileEmployeeId + + ORDER BY + profileEmployeeId, + commandDateAffect ASC + `, +}) +export class viewCurrentTenureEmployee { + @ViewColumn() + commandDateAffect: Date; + + @ViewColumn() + days_diff: number; + + @ViewColumn() + Years: number; + + @ViewColumn() + Months: number; + + @ViewColumn() + Days: number; + + @ViewColumn() + positionName: string; + + @ViewColumn() + positionCee: string; + + @ViewColumn() + posNo: string; + + @ViewColumn() + positionExecutive: string; + + @ViewColumn() + positionType: string; + + @ViewColumn() + positionLevel: string; + + @ViewColumn() + OrgRoot: string; + + @ViewColumn() + orgChild1: string; + + @ViewColumn() + orgChild2: string; + + @ViewColumn() + orgChild3: string; + + @ViewColumn() + orgChild4: string; + + @ViewColumn() + commandCode: number; + + @ViewColumn() + commandName: string; + + @ViewColumn() + commandNo: string; + + @ViewColumn() + commandYear: number; + + @ViewColumn() + remark: string; + + @ViewColumn() + profileEmployeeId: string; + + @ViewColumn() + orderNumber: number; +} \ No newline at end of file diff --git a/src/entities/view/viewCurrentTenureOfficer.ts b/src/entities/view/viewCurrentTenureOfficer.ts new file mode 100644 index 00000000..5899687c --- /dev/null +++ b/src/entities/view/viewCurrentTenureOfficer.ts @@ -0,0 +1,151 @@ +import { ViewColumn, ViewEntity } from "typeorm"; + +@ViewEntity({ + expression: ` + SELECT + DATE(MIN(commandDateAffect)) AS commandDateAffect, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) AS days_diff, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days', + positionName, + positionCee, + posNo, + positionExecutive, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileId, + ROW_NUMBER() OVER (PARTITION BY profileId ORDER BY commandDateAffect ASC) AS orderNumber + FROM + profileSalary + WHERE + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileId, positionName + + UNION ALL + + SELECT + CURDATE() AS commandDateAffect, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', + NULL AS positionName, + NULL AS positionCee, + NULL AS posNo, + NULL AS positionExecutive, + NULL AS positionType, + NULL AS positionLevel, + NULL AS OrgRoot, + NULL AS orgChild1, + NULL AS orgChild2, + NULL AS orgChild3, + NULL AS orgChild4, + NULL AS commandCode, + NULL AS commandName, + NULL AS commandNo, + NULL AS commandYear, + 'Comparison with current date' AS remark, + profileId, + NULL AS orderNumber + FROM + profileSalary + WHERE + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileId + + ORDER BY + profileId, + commandDateAffect ASC + `, +}) +export class viewCurrentTenureOfficer { + @ViewColumn() + commandDateAffect: Date; + + @ViewColumn() + days_diff: number; + + @ViewColumn() + Years: number; + + @ViewColumn() + Months: number; + + @ViewColumn() + Days: number; + + @ViewColumn() + positionName: string; + + @ViewColumn() + positionCee: string; + + @ViewColumn() + posNo: string; + + @ViewColumn() + positionExecutive: string; + + @ViewColumn() + positionType: string; + + @ViewColumn() + positionLevel: string; + + @ViewColumn() + OrgRoot: string; + + @ViewColumn() + orgChild1: string; + + @ViewColumn() + orgChild2: string; + + @ViewColumn() + orgChild3: string; + + @ViewColumn() + orgChild4: string; + + @ViewColumn() + commandCode: number; + + @ViewColumn() + commandName: string; + + @ViewColumn() + commandNo: string; + + @ViewColumn() + commandYear: number; + + @ViewColumn() + remark: string; + + @ViewColumn() + profileId: string; + + @ViewColumn() + orderNumber: number; +} diff --git a/src/migration/1740558529773-addViewCurrentTenure.ts b/src/migration/1740558529773-addViewCurrentTenure.ts new file mode 100644 index 00000000..fc2d96e8 --- /dev/null +++ b/src/migration/1740558529773-addViewCurrentTenure.ts @@ -0,0 +1,172 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddViewCurrentTenure1740558529773 implements MigrationInterface { + name = 'AddViewCurrentTenure1740558529773' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE VIEW \`view_current_tenure_officer\` AS + SELECT + DATE(MIN(commandDateAffect)) AS commandDateAffect, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) AS days_diff, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days', + positionName, + positionCee, + posNo, + positionExecutive, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileId, + ROW_NUMBER() OVER (PARTITION BY profileId ORDER BY commandDateAffect ASC) AS orderNumber + FROM + profileSalary + WHERE + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileId, positionName + + UNION ALL + + SELECT + CURDATE() AS commandDateAffect, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', + NULL AS positionName, + NULL AS positionCee, + NULL AS posNo, + NULL AS positionExecutive, + NULL AS positionType, + NULL AS positionLevel, + NULL AS OrgRoot, + NULL AS orgChild1, + NULL AS orgChild2, + NULL AS orgChild3, + NULL AS orgChild4, + NULL AS commandCode, + NULL AS commandName, + NULL AS commandNo, + NULL AS commandYear, + 'Comparison with current date' AS remark, + profileId, + NULL AS orderNumber + FROM + profileSalary + WHERE + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileId + + ORDER BY + profileId, + commandDateAffect ASC + `); + await queryRunner.query(`INSERT INTO \`bma_ehr_organization_demo\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, ["bma_ehr_organization_demo","VIEW","view_current_tenure_officer","SELECT\n DATE(MIN(commandDateAffect)) AS commandDateAffect,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) AS days_diff,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days',\n positionName,\n positionCee,\n posNo,\n positionExecutive,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileId,\n ROW_NUMBER() OVER (PARTITION BY profileId ORDER BY commandDateAffect ASC) AS orderNumber\n FROM\n profileSalary\n WHERE\n commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16)\n GROUP BY\n profileId, positionName\n\n UNION ALL\n\n SELECT\n CURDATE() AS commandDateAffect,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days',\n NULL AS positionName,\n NULL AS positionCee,\n NULL AS posNo,\n NULL AS positionExecutive,\n NULL AS positionType,\n NULL AS positionLevel,\n NULL AS OrgRoot,\n NULL AS orgChild1,\n NULL AS orgChild2,\n NULL AS orgChild3,\n NULL AS orgChild4,\n NULL AS commandCode,\n NULL AS commandName,\n NULL AS commandNo,\n NULL AS commandYear,\n 'Comparison with current date' AS remark,\n profileId,\n NULL AS orderNumber\n FROM\n profileSalary\n WHERE\n commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16)\n GROUP BY\n profileId\n\n ORDER BY\n profileId,\n commandDateAffect ASC"]); + await queryRunner.query(`CREATE VIEW \`view_current_tenure_employee\` AS + SELECT + DATE(MIN(commandDateAffect)) AS commandDateAffect, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) AS days_diff, + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF( + DAY, + LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, \`order\` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days', + positionName, + positionCee, + posNo, + positionType, + positionLevel, + OrgRoot, + orgChild1, + orgChild2, + orgChild3, + orgChild4, + commandCode, + commandName, + commandNo, + commandYear, + remark, + profileEmployeeId, + ROW_NUMBER() OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect ASC) AS orderNumber + FROM + profileSalary + WHERE + profileEmployeeId IS NOT NULL AND + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileEmployeeId, positionName + + UNION ALL + + SELECT + CURDATE() AS commandDateAffect, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff, + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months', + TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days', + NULL AS positionName, + NULL AS positionCee, + NULL AS posNo, + NULL AS positionType, + NULL AS positionLevel, + NULL AS OrgRoot, + NULL AS orgChild1, + NULL AS orgChild2, + NULL AS orgChild3, + NULL AS orgChild4, + NULL AS commandCode, + NULL AS commandName, + NULL AS commandNo, + NULL AS commandYear, + 'Comparison with current date' AS remark, + profileEmployeeId, + NULL AS orderNumber + FROM + profileSalary + WHERE + profileEmployeeId IS NOT NULL AND + commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16) + GROUP BY + profileEmployeeId + + ORDER BY + profileEmployeeId, + commandDateAffect ASC + `); + await queryRunner.query(`INSERT INTO \`bma_ehr_organization_demo\`.\`typeorm_metadata\`(\`database\`, \`schema\`, \`table\`, \`type\`, \`name\`, \`value\`) VALUES (DEFAULT, ?, DEFAULT, ?, ?, ?)`, ["bma_ehr_organization_demo","VIEW","view_current_tenure_employee","SELECT\n DATE(MIN(commandDateAffect)) AS commandDateAffect,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) AS days_diff,\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(\n DAY,\n LAG(MIN(commandDateAffect)) OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect, `order` ASC), MIN(commandDateAffect)) % 30.4375 AS 'Days',\n positionName,\n positionCee,\n posNo,\n positionType,\n positionLevel,\n OrgRoot,\n orgChild1,\n orgChild2,\n orgChild3,\n orgChild4,\n commandCode,\n commandName,\n commandNo,\n commandYear,\n remark,\n profileEmployeeId,\n ROW_NUMBER() OVER (PARTITION BY profileEmployeeId ORDER BY commandDateAffect ASC) AS orderNumber\n FROM\n profileSalary\n WHERE\n profileEmployeeId IS NOT NULL AND\n commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16)\n GROUP BY\n profileEmployeeId, positionName\n\n UNION ALL\n\n SELECT\n CURDATE() AS commandDateAffect,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) AS days_diff,\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 365.2524 AS 'Years',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) / 30.4375 % 12 AS 'Months',\n TIMESTAMPDIFF(DAY, MAX(commandDateAffect), CURDATE()) % 30.4375 AS 'Days',\n NULL AS positionName,\n NULL AS positionCee,\n NULL AS posNo,\n NULL AS positionType,\n NULL AS positionLevel,\n NULL AS OrgRoot,\n NULL AS orgChild1,\n NULL AS orgChild2,\n NULL AS orgChild3,\n NULL AS orgChild4,\n NULL AS commandCode,\n NULL AS commandName,\n NULL AS commandNo,\n NULL AS commandYear,\n 'Comparison with current date' AS remark,\n profileEmployeeId,\n NULL AS orderNumber\n FROM\n profileSalary\n WHERE\n profileEmployeeId IS NOT NULL AND\n commandCode IN (1, 2, 3, 4, 8, 10, 11, 12, 15, 16)\n GROUP BY\n profileEmployeeId\n\n ORDER BY\n profileEmployeeId,\n commandDateAffect ASC"]); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DELETE FROM \`bma_ehr_organization_demo\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","view_current_tenure_employee","bma_ehr_organization_demo"]); + await queryRunner.query(`DROP VIEW \`view_current_tenure_employee\``); + await queryRunner.query(`DELETE FROM \`bma_ehr_organization_demo\`.\`typeorm_metadata\` WHERE \`type\` = ? AND \`name\` = ? AND \`schema\` = ?`, ["VIEW","view_current_tenure_officer","bma_ehr_organization_demo"]); + await queryRunner.query(`DROP VIEW \`view_current_tenure_officer\``); + } + +}