@classytic/payroll 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +168 -489
  2. package/dist/core/index.d.ts +480 -0
  3. package/dist/core/index.js +971 -0
  4. package/dist/core/index.js.map +1 -0
  5. package/dist/index-CTjHlCzz.d.ts +721 -0
  6. package/dist/index.d.ts +967 -0
  7. package/dist/index.js +4352 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/payroll.d.ts +233 -0
  10. package/dist/payroll.js +2103 -0
  11. package/dist/payroll.js.map +1 -0
  12. package/dist/plugin-D9mOr3_d.d.ts +333 -0
  13. package/dist/schemas/index.d.ts +2869 -0
  14. package/dist/schemas/index.js +440 -0
  15. package/dist/schemas/index.js.map +1 -0
  16. package/dist/services/index.d.ts +3 -0
  17. package/dist/services/index.js +1696 -0
  18. package/dist/services/index.js.map +1 -0
  19. package/dist/types-BSYyX2KJ.d.ts +671 -0
  20. package/dist/utils/index.d.ts +873 -0
  21. package/dist/utils/index.js +1046 -0
  22. package/dist/utils/index.js.map +1 -0
  23. package/package.json +54 -37
  24. package/dist/types/config.d.ts +0 -162
  25. package/dist/types/core/compensation.manager.d.ts +0 -54
  26. package/dist/types/core/employment.manager.d.ts +0 -49
  27. package/dist/types/core/payroll.manager.d.ts +0 -60
  28. package/dist/types/enums.d.ts +0 -117
  29. package/dist/types/factories/compensation.factory.d.ts +0 -196
  30. package/dist/types/factories/employee.factory.d.ts +0 -149
  31. package/dist/types/factories/payroll.factory.d.ts +0 -319
  32. package/dist/types/hrm.orchestrator.d.ts +0 -47
  33. package/dist/types/index.d.ts +0 -20
  34. package/dist/types/init.d.ts +0 -30
  35. package/dist/types/models/payroll-record.model.d.ts +0 -3
  36. package/dist/types/plugins/employee.plugin.d.ts +0 -2
  37. package/dist/types/schemas/employment.schema.d.ts +0 -959
  38. package/dist/types/services/compensation.service.d.ts +0 -94
  39. package/dist/types/services/employee.service.d.ts +0 -28
  40. package/dist/types/services/payroll.service.d.ts +0 -30
  41. package/dist/types/utils/calculation.utils.d.ts +0 -26
  42. package/dist/types/utils/date.utils.d.ts +0 -35
  43. package/dist/types/utils/logger.d.ts +0 -12
  44. package/dist/types/utils/query-builders.d.ts +0 -83
  45. package/dist/types/utils/validation.utils.d.ts +0 -33
  46. package/payroll.d.ts +0 -241
  47. package/src/config.js +0 -177
  48. package/src/core/compensation.manager.js +0 -242
  49. package/src/core/employment.manager.js +0 -224
  50. package/src/core/payroll.manager.js +0 -499
  51. package/src/enums.js +0 -141
  52. package/src/factories/compensation.factory.js +0 -198
  53. package/src/factories/employee.factory.js +0 -173
  54. package/src/factories/payroll.factory.js +0 -413
  55. package/src/hrm.orchestrator.js +0 -139
  56. package/src/index.js +0 -172
  57. package/src/init.js +0 -62
  58. package/src/models/payroll-record.model.js +0 -126
  59. package/src/plugins/employee.plugin.js +0 -164
  60. package/src/schemas/employment.schema.js +0 -126
  61. package/src/services/compensation.service.js +0 -231
  62. package/src/services/employee.service.js +0 -162
  63. package/src/services/payroll.service.js +0 -213
  64. package/src/utils/calculation.utils.js +0 -91
  65. package/src/utils/date.utils.js +0 -120
  66. package/src/utils/logger.js +0 -36
  67. package/src/utils/query-builders.js +0 -185
  68. package/src/utils/validation.utils.js +0 -122
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils/logger.ts","../src/config.ts","../src/core/container.ts","../src/core/events.ts","../src/core/plugin.ts","../src/utils/date.ts","../src/factories/employee.factory.ts","../src/enums.ts","../src/utils/query-builders.ts","../src/utils/calculation.ts","../src/errors/index.ts","../src/payroll.ts"],"names":["config","employee","mongoose","payroll"],"mappings":";;;;;AAaA,IAAM,sBAAsB,OAAe;AAAA,EACzC,IAAA,EAAM,CAAC,OAAA,EAAiB,IAAA,KAAmC;AACzD,IAAA,IAAI,IAAA,EAAM;AACR,MAAA,OAAA,CAAQ,GAAA,CAAI,CAAA,gBAAA,EAAmB,OAAO,CA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AAAA;AAAA;AAAA,EAKA,KAAA,GAAiB;AACf,IAAA,IAAI,CAAC,KAAK,OAAA,EAAS;AACjB,MAAA,MAAM,IAAI,MAAM,+CAA+C,CAAA;AAAA,IACjE;AAEA,IAAA,MAAME,QAAAA,GAAU,IAAI,OAAA,EAAQ;AAC5B,IAAAA,SAAQ,UAAA,CAAW;AAAA,MACjB,aAAA,EAAe,KAAK,OAAA,CAAQ,aAAA;AAAA,MAC5B,kBAAA,EAAoB,KAAK,OAAA,CAAQ,kBAAA;AAAA,MACjC,gBAAA,EAAkB,KAAK,OAAA,CAAQ,gBAAA;AAAA,MAC/B,eAAA,EAAiB,KAAK,OAAA,CAAQ,eAAA;AAAA,MAC9B,QAAQ,IAAA,CAAK,OAAA;AAAA,MACb,cAAc,IAAA,CAAK,aAAA;AAAA,MACnB,QAAQ,IAAA,CAAK;AAAA,KACd,CAAA;AAED,IAAA,OAAOA,QAAAA;AAAA,EACT;AACF;AASO,SAAS,qBAAA,GAAwC;AACtD,EAAA,OAAO,IAAI,cAAA,EAAe;AAC5B;AAMA,IAAI,eAAA,GAAkC,IAAA;AAK/B,SAAS,UAAA,GAAsB;AACpC,EAAA,IAAI,CAAC,eAAA,EAAiB;AACpB,IAAA,eAAA,GAAkB,IAAI,OAAA,EAAQ;AAAA,EAChC;AACA,EAAA,OAAO,eAAA;AACT;AAKO,SAAS,YAAA,GAAqB;AACnC,EAAA,eAAA,GAAkB,IAAA;AAClB,EAAA,SAAA,CAAU,aAAA,EAAc;AAC1B;AAMO,IAAMA,WAAU,UAAA;AACvB,IAAO,eAAA,GAAQA","file":"payroll.js","sourcesContent":["/**\r\n * @classytic/payroll - Logger\r\n *\r\n * Pluggable logger abstraction\r\n * Defaults to console, can be replaced with pino, winston, etc.\r\n */\r\n\r\nimport type { Logger } from '../types.js';\r\n\r\n// ============================================================================\r\n// Default Logger Implementation\r\n// ============================================================================\r\n\r\nconst createConsoleLogger = (): Logger => ({\r\n info: (message: string, meta?: Record<string, unknown>) => {\r\n if (meta) {\r\n console.log(`[Payroll] INFO: ${message}`, meta);\r\n } else {\r\n console.log(`[Payroll] INFO: ${message}`);\r\n }\r\n },\r\n error: (message: string, meta?: Record<string, unknown>) => {\r\n if (meta) {\r\n console.error(`[Payroll] ERROR: ${message}`, meta);\r\n } else {\r\n console.error(`[Payroll] ERROR: ${message}`);\r\n }\r\n },\r\n warn: (message: string, meta?: Record<string, unknown>) => {\r\n if (meta) {\r\n console.warn(`[Payroll] WARN: ${message}`, meta);\r\n } else {\r\n console.warn(`[Payroll] WARN: ${message}`);\r\n }\r\n },\r\n debug: (message: string, meta?: Record<string, unknown>) => {\r\n if (process.env.NODE_ENV !== 'production') {\r\n if (meta) {\r\n console.log(`[Payroll] DEBUG: ${message}`, meta);\r\n } else {\r\n console.log(`[Payroll] DEBUG: ${message}`);\r\n }\r\n }\r\n },\r\n});\r\n\r\n// ============================================================================\r\n// Logger State\r\n// ============================================================================\r\n\r\nlet currentLogger: Logger = createConsoleLogger();\r\nlet loggingEnabled = true;\r\n\r\n// ============================================================================\r\n// Logger Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Get the current logger instance\r\n */\r\nexport function getLogger(): Logger {\r\n return currentLogger;\r\n}\r\n\r\n/**\r\n * Set a custom logger instance\r\n */\r\nexport function setLogger(logger: Logger): void {\r\n currentLogger = logger;\r\n}\r\n\r\n/**\r\n * Reset to default console logger\r\n */\r\nexport function resetLogger(): void {\r\n currentLogger = createConsoleLogger();\r\n}\r\n\r\n/**\r\n * Create a child logger with prefix\r\n */\r\nexport function createChildLogger(prefix: string): Logger {\r\n const parent = currentLogger;\r\n return {\r\n info: (message: string, meta?: Record<string, unknown>) =>\r\n parent.info(`[${prefix}] ${message}`, meta),\r\n error: (message: string, meta?: Record<string, unknown>) =>\r\n parent.error(`[${prefix}] ${message}`, meta),\r\n warn: (message: string, meta?: Record<string, unknown>) =>\r\n parent.warn(`[${prefix}] ${message}`, meta),\r\n debug: (message: string, meta?: Record<string, unknown>) =>\r\n parent.debug(`[${prefix}] ${message}`, meta),\r\n };\r\n}\r\n\r\n/**\r\n * Create a silent logger (for testing)\r\n */\r\nexport function createSilentLogger(): Logger {\r\n return {\r\n info: () => {},\r\n error: () => {},\r\n warn: () => {},\r\n debug: () => {},\r\n };\r\n}\r\n\r\n/**\r\n * Enable logging globally\r\n */\r\nexport function enableLogging(): void {\r\n loggingEnabled = true;\r\n}\r\n\r\n/**\r\n * Disable logging globally (useful for production)\r\n */\r\nexport function disableLogging(): void {\r\n loggingEnabled = false;\r\n}\r\n\r\n/**\r\n * Check if logging is enabled\r\n */\r\nexport function isLoggingEnabled(): boolean {\r\n return loggingEnabled;\r\n}\r\n\r\n// ============================================================================\r\n// Logger Proxy Object\r\n// ============================================================================\r\n\r\n/**\r\n * Logger proxy that always delegates to currentLogger\r\n * Respects global logging enabled/disabled state\r\n */\r\nexport const logger: Logger = {\r\n info: (message: string, meta?: Record<string, unknown>) => {\r\n if (loggingEnabled) currentLogger.info(message, meta);\r\n },\r\n error: (message: string, meta?: Record<string, unknown>) => {\r\n if (loggingEnabled) currentLogger.error(message, meta);\r\n },\r\n warn: (message: string, meta?: Record<string, unknown>) => {\r\n if (loggingEnabled) currentLogger.warn(message, meta);\r\n },\r\n debug: (message: string, meta?: Record<string, unknown>) => {\r\n if (loggingEnabled) currentLogger.debug(message, meta);\r\n },\r\n};\r\n\r\nexport default logger;\r\n\r\n","/**\r\n * @classytic/payroll - Configuration\r\n *\r\n * Centralized configuration with type safety\r\n * Configurable defaults for different use cases\r\n */\r\n\r\nimport type {\r\n HRMConfig,\r\n TaxBracket,\r\n SalaryBandRange,\r\n RoleMappingConfig,\r\n OrgRole,\r\n SalaryBand,\r\n Department,\r\n EmploymentType,\r\n PaymentFrequency,\r\n DeepPartial,\r\n} from './types.js';\r\n\r\n// ============================================================================\r\n// Default Configuration\r\n// ============================================================================\r\n\r\nexport const HRM_CONFIG: HRMConfig = {\r\n dataRetention: {\r\n payrollRecordsTTL: 63072000, // 2 years in seconds\r\n exportWarningDays: 30,\r\n archiveBeforeDeletion: true,\r\n },\r\n\r\n payroll: {\r\n defaultCurrency: 'BDT',\r\n allowProRating: true,\r\n attendanceIntegration: true,\r\n autoDeductions: true,\r\n overtimeEnabled: false,\r\n overtimeMultiplier: 1.5,\r\n },\r\n\r\n salary: {\r\n minimumWage: 0,\r\n maximumAllowances: 10,\r\n maximumDeductions: 10,\r\n defaultFrequency: 'monthly',\r\n },\r\n\r\n employment: {\r\n defaultProbationMonths: 3,\r\n maxProbationMonths: 6,\r\n allowReHiring: true,\r\n trackEmploymentHistory: true,\r\n },\r\n\r\n validation: {\r\n requireBankDetails: false,\r\n requireEmployeeId: true,\r\n uniqueEmployeeIdPerOrg: true,\r\n allowMultiTenantEmployees: true,\r\n },\r\n};\r\n\r\n// ============================================================================\r\n// Salary Bands Configuration\r\n// ============================================================================\r\n\r\nexport const SALARY_BANDS: Record<Exclude<SalaryBand, 'custom'>, SalaryBandRange> = {\r\n intern: { min: 10000, max: 20000 },\r\n junior: { min: 20000, max: 40000 },\r\n mid: { min: 40000, max: 70000 },\r\n senior: { min: 70000, max: 120000 },\r\n lead: { min: 100000, max: 200000 },\r\n executive: { min: 150000, max: 500000 },\r\n};\r\n\r\n// ============================================================================\r\n// Tax Brackets Configuration\r\n// ============================================================================\r\n\r\nexport const TAX_BRACKETS: Record<string, TaxBracket[]> = {\r\n BDT: [\r\n { min: 0, max: 300000, rate: 0 },\r\n { min: 300000, max: 400000, rate: 0.05 },\r\n { min: 400000, max: 500000, rate: 0.10 },\r\n { min: 500000, max: 600000, rate: 0.15 },\r\n { min: 600000, max: 3000000, rate: 0.20 },\r\n { min: 3000000, max: Infinity, rate: 0.25 },\r\n ],\r\n USD: [\r\n { min: 0, max: 10000, rate: 0.10 },\r\n { min: 10000, max: 40000, rate: 0.12 },\r\n { min: 40000, max: 85000, rate: 0.22 },\r\n { min: 85000, max: 165000, rate: 0.24 },\r\n { min: 165000, max: 215000, rate: 0.32 },\r\n { min: 215000, max: 540000, rate: 0.35 },\r\n { min: 540000, max: Infinity, rate: 0.37 },\r\n ],\r\n};\r\n\r\n// ============================================================================\r\n// Organization Roles Configuration\r\n// ============================================================================\r\n\r\nexport interface OrgRoleDefinition {\r\n key: OrgRole;\r\n label: string;\r\n description: string;\r\n}\r\n\r\nexport const ORG_ROLES: Record<Uppercase<OrgRole>, OrgRoleDefinition> = {\r\n OWNER: {\r\n key: 'owner',\r\n label: 'Owner',\r\n description: 'Full organization access (set by Organization model)',\r\n },\r\n MANAGER: {\r\n key: 'manager',\r\n label: 'Manager',\r\n description: 'Management and administrative features',\r\n },\r\n TRAINER: {\r\n key: 'trainer',\r\n label: 'Trainer',\r\n description: 'Training and coaching features',\r\n },\r\n STAFF: {\r\n key: 'staff',\r\n label: 'Staff',\r\n description: 'General staff access to basic features',\r\n },\r\n INTERN: {\r\n key: 'intern',\r\n label: 'Intern',\r\n description: 'Limited access for interns',\r\n },\r\n CONSULTANT: {\r\n key: 'consultant',\r\n label: 'Consultant',\r\n description: 'Project-based consultant access',\r\n },\r\n};\r\n\r\nexport const ORG_ROLE_KEYS = Object.values(ORG_ROLES).map((role) => role.key);\r\n\r\n// ============================================================================\r\n// Role Mapping Configuration\r\n// ============================================================================\r\n\r\nexport const ROLE_MAPPING: RoleMappingConfig = {\r\n byDepartment: {\r\n management: 'manager',\r\n training: 'trainer',\r\n sales: 'staff',\r\n operations: 'staff',\r\n finance: 'staff',\r\n hr: 'staff',\r\n marketing: 'staff',\r\n it: 'staff',\r\n support: 'staff',\r\n maintenance: 'staff',\r\n },\r\n\r\n byEmploymentType: {\r\n full_time: 'staff',\r\n part_time: 'staff',\r\n contract: 'consultant',\r\n intern: 'intern',\r\n consultant: 'consultant',\r\n },\r\n\r\n default: 'staff',\r\n};\r\n\r\n// ============================================================================\r\n// Configuration Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate tax based on annual income\r\n */\r\nexport function calculateTax(annualIncome: number, currency = 'BDT'): number {\r\n const brackets = TAX_BRACKETS[currency];\r\n if (!brackets) return 0;\r\n\r\n let tax = 0;\r\n for (const bracket of brackets) {\r\n if (annualIncome > bracket.min) {\r\n const taxableAmount = Math.min(annualIncome, bracket.max) - bracket.min;\r\n tax += taxableAmount * bracket.rate;\r\n }\r\n }\r\n return Math.round(tax);\r\n}\r\n\r\n/**\r\n * Get salary band for a given amount\r\n */\r\nexport function getSalaryBand(amount: number): SalaryBand {\r\n for (const [band, range] of Object.entries(SALARY_BANDS)) {\r\n if (amount >= range.min && amount <= range.max) {\r\n return band as SalaryBand;\r\n }\r\n }\r\n return 'custom';\r\n}\r\n\r\n/**\r\n * Determine the appropriate organization role for an employee\r\n */\r\nexport function determineOrgRole(employmentData: {\r\n department?: Department | string;\r\n type?: EmploymentType | string;\r\n position?: string;\r\n}): OrgRole {\r\n const { department, type: employmentType } = employmentData;\r\n\r\n // Priority 1: Department-based mapping\r\n if (department && department in ROLE_MAPPING.byDepartment) {\r\n return ROLE_MAPPING.byDepartment[department as keyof typeof ROLE_MAPPING.byDepartment];\r\n }\r\n\r\n // Priority 2: Employment type mapping\r\n if (employmentType && employmentType in ROLE_MAPPING.byEmploymentType) {\r\n return ROLE_MAPPING.byEmploymentType[employmentType as keyof typeof ROLE_MAPPING.byEmploymentType];\r\n }\r\n\r\n // Priority 3: Default role\r\n return ROLE_MAPPING.default;\r\n}\r\n\r\n/**\r\n * Get pay periods per year based on frequency\r\n */\r\nexport function getPayPeriodsPerYear(frequency: PaymentFrequency): number {\r\n const periodsMap: Record<PaymentFrequency, number> = {\r\n monthly: 12,\r\n bi_weekly: 26,\r\n weekly: 52,\r\n daily: 365,\r\n hourly: 2080, // Assuming 40 hours/week * 52 weeks\r\n };\r\n return periodsMap[frequency];\r\n}\r\n\r\n/**\r\n * Calculate monthly equivalent from any frequency\r\n */\r\nexport function toMonthlyAmount(amount: number, frequency: PaymentFrequency): number {\r\n const periodsPerYear = getPayPeriodsPerYear(frequency);\r\n return Math.round((amount * periodsPerYear) / 12);\r\n}\r\n\r\n/**\r\n * Calculate annual equivalent from any frequency\r\n */\r\nexport function toAnnualAmount(amount: number, frequency: PaymentFrequency): number {\r\n const periodsPerYear = getPayPeriodsPerYear(frequency);\r\n return Math.round(amount * periodsPerYear);\r\n}\r\n\r\n/**\r\n * Merge configuration with defaults\r\n */\r\nexport function mergeConfig(\r\n customConfig: Partial<HRMConfig> | DeepPartial<HRMConfig> | undefined\r\n): HRMConfig {\r\n if (!customConfig) return HRM_CONFIG;\r\n\r\n return {\r\n dataRetention: { ...HRM_CONFIG.dataRetention, ...customConfig.dataRetention },\r\n payroll: { ...HRM_CONFIG.payroll, ...customConfig.payroll },\r\n salary: { ...HRM_CONFIG.salary, ...customConfig.salary },\r\n employment: { ...HRM_CONFIG.employment, ...customConfig.employment },\r\n validation: { ...HRM_CONFIG.validation, ...customConfig.validation },\r\n };\r\n}\r\n\r\n// ============================================================================\r\n// Default Export\r\n// ============================================================================\r\n\r\nexport default {\r\n HRM_CONFIG,\r\n SALARY_BANDS,\r\n TAX_BRACKETS,\r\n ORG_ROLES,\r\n ORG_ROLE_KEYS,\r\n ROLE_MAPPING,\r\n calculateTax,\r\n getSalaryBand,\r\n determineOrgRole,\r\n getPayPeriodsPerYear,\r\n toMonthlyAmount,\r\n toAnnualAmount,\r\n mergeConfig,\r\n};\r\n\r\n","/**\r\n * @classytic/payroll - Dependency Container\r\n *\r\n * Simple dependency injection container for service management\r\n * Enables clean dependency injection and testing\r\n */\r\n\r\nimport type { Model, ClientSession } from 'mongoose';\r\nimport type { Logger, HRMConfig, SingleTenantConfig } from '../types.js';\r\nimport { getLogger } from '../utils/logger.js';\r\nimport { HRM_CONFIG, mergeConfig } from '../config.js';\r\n\r\n// ============================================================================\r\n// Container Types\r\n// ============================================================================\r\n\r\nexport interface ModelsContainer {\r\n EmployeeModel: Model<any>;\r\n PayrollRecordModel: Model<any>;\r\n TransactionModel: Model<any>;\r\n AttendanceModel?: Model<any> | null;\r\n}\r\n\r\nexport interface ContainerConfig {\r\n models: ModelsContainer;\r\n config?: Partial<HRMConfig>;\r\n singleTenant?: SingleTenantConfig | null;\r\n logger?: Logger;\r\n}\r\n\r\n// ============================================================================\r\n// Container Class\r\n// ============================================================================\r\n\r\nexport class Container {\r\n private static instance: Container | null = null;\r\n \r\n private _models: ModelsContainer | null = null;\r\n private _config: HRMConfig = HRM_CONFIG;\r\n private _singleTenant: SingleTenantConfig | null = null;\r\n private _logger: Logger;\r\n private _initialized = false;\r\n\r\n private constructor() {\r\n this._logger = getLogger();\r\n }\r\n\r\n /**\r\n * Get singleton instance\r\n */\r\n static getInstance(): Container {\r\n if (!Container.instance) {\r\n Container.instance = new Container();\r\n }\r\n return Container.instance;\r\n }\r\n\r\n /**\r\n * Reset instance (for testing)\r\n */\r\n static resetInstance(): void {\r\n Container.instance = null;\r\n }\r\n\r\n /**\r\n * Initialize container with configuration\r\n */\r\n initialize(config: ContainerConfig): void {\r\n if (this._initialized) {\r\n this._logger.warn('Container already initialized, re-initializing');\r\n }\r\n\r\n this._models = config.models;\r\n this._config = mergeConfig(config.config);\r\n this._singleTenant = config.singleTenant ?? null;\r\n \r\n if (config.logger) {\r\n this._logger = config.logger;\r\n }\r\n\r\n this._initialized = true;\r\n\r\n this._logger.info('Container initialized', {\r\n hasEmployeeModel: !!this._models.EmployeeModel,\r\n hasPayrollRecordModel: !!this._models.PayrollRecordModel,\r\n hasTransactionModel: !!this._models.TransactionModel,\r\n hasAttendanceModel: !!this._models.AttendanceModel,\r\n isSingleTenant: !!this._singleTenant,\r\n });\r\n }\r\n\r\n /**\r\n * Check if container is initialized\r\n */\r\n isInitialized(): boolean {\r\n return this._initialized;\r\n }\r\n\r\n /**\r\n * Ensure container is initialized\r\n */\r\n private ensureInitialized(): void {\r\n if (!this._initialized || !this._models) {\r\n throw new Error(\r\n 'Payroll not initialized. Call Payroll.initialize() first.'\r\n );\r\n }\r\n }\r\n\r\n /**\r\n * Get models container\r\n */\r\n getModels(): ModelsContainer {\r\n this.ensureInitialized();\r\n return this._models!;\r\n }\r\n\r\n /**\r\n * Get Employee model\r\n */\r\n getEmployeeModel(): Model<any> {\r\n this.ensureInitialized();\r\n return this._models!.EmployeeModel;\r\n }\r\n\r\n /**\r\n * Get PayrollRecord model\r\n */\r\n getPayrollRecordModel(): Model<any> {\r\n this.ensureInitialized();\r\n return this._models!.PayrollRecordModel;\r\n }\r\n\r\n /**\r\n * Get Transaction model\r\n */\r\n getTransactionModel(): Model<any> {\r\n this.ensureInitialized();\r\n return this._models!.TransactionModel;\r\n }\r\n\r\n /**\r\n * Get Attendance model (optional)\r\n */\r\n getAttendanceModel(): Model<any> | null {\r\n this.ensureInitialized();\r\n return this._models!.AttendanceModel ?? null;\r\n }\r\n\r\n /**\r\n * Get configuration\r\n */\r\n getConfig(): HRMConfig {\r\n return this._config;\r\n }\r\n\r\n /**\r\n * Get specific config section\r\n */\r\n getConfigSection<K extends keyof HRMConfig>(section: K): HRMConfig[K] {\r\n return this._config[section];\r\n }\r\n\r\n /**\r\n * Check if single-tenant mode\r\n */\r\n isSingleTenant(): boolean {\r\n return !!this._singleTenant;\r\n }\r\n\r\n /**\r\n * Get single-tenant config\r\n */\r\n getSingleTenantConfig(): SingleTenantConfig | null {\r\n return this._singleTenant;\r\n }\r\n\r\n /**\r\n * Get organization ID (for single-tenant mode)\r\n */\r\n getOrganizationId(): string | null {\r\n if (!this._singleTenant || !this._singleTenant.organizationId) return null;\r\n return typeof this._singleTenant.organizationId === 'string'\r\n ? this._singleTenant.organizationId\r\n : this._singleTenant.organizationId.toString();\r\n }\r\n\r\n /**\r\n * Get logger\r\n */\r\n getLogger(): Logger {\r\n return this._logger;\r\n }\r\n\r\n /**\r\n * Set logger\r\n */\r\n setLogger(logger: Logger): void {\r\n this._logger = logger;\r\n }\r\n\r\n /**\r\n * Has attendance integration\r\n */\r\n hasAttendanceIntegration(): boolean {\r\n return (\r\n !!this._models?.AttendanceModel &&\r\n this._config.payroll.attendanceIntegration\r\n );\r\n }\r\n\r\n /**\r\n * Create operation context with defaults\r\n */\r\n createOperationContext(\r\n overrides?: Partial<{\r\n userId: string;\r\n userName: string;\r\n userRole: string;\r\n organizationId: string;\r\n session: ClientSession;\r\n }>\r\n ): {\r\n userId?: string;\r\n userName?: string;\r\n userRole?: string;\r\n organizationId?: string;\r\n session?: ClientSession;\r\n } {\r\n const context: Record<string, unknown> = {};\r\n\r\n // Auto-inject organizationId in single-tenant mode\r\n if (this._singleTenant?.autoInject && !overrides?.organizationId) {\r\n context.organizationId = this.getOrganizationId();\r\n }\r\n\r\n return { ...context, ...overrides };\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Convenience Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Get container instance\r\n */\r\nexport function getContainer(): Container {\r\n return Container.getInstance();\r\n}\r\n\r\n/**\r\n * Initialize container\r\n */\r\nexport function initializeContainer(config: ContainerConfig): void {\r\n Container.getInstance().initialize(config);\r\n}\r\n\r\n/**\r\n * Check if container is initialized\r\n */\r\nexport function isContainerInitialized(): boolean {\r\n return Container.getInstance().isInitialized();\r\n}\r\n\r\n/**\r\n * Get models from container\r\n */\r\nexport function getModels(): ModelsContainer {\r\n return Container.getInstance().getModels();\r\n}\r\n\r\n/**\r\n * Get config from container\r\n */\r\nexport function getConfig(): HRMConfig {\r\n return Container.getInstance().getConfig();\r\n}\r\n\r\n/**\r\n * Check if single-tenant mode\r\n */\r\nexport function isSingleTenant(): boolean {\r\n return Container.getInstance().isSingleTenant();\r\n}\r\n\r\n","/**\r\n * @classytic/payroll - Event System\r\n *\r\n * Type-safe event emitter for payroll lifecycle events\r\n * Enables loose coupling and extensibility\r\n */\r\n\r\nimport type {\r\n PayrollEvent,\r\n EmployeeDocument,\r\n PayrollRecordDocument,\r\n ObjectId,\r\n OperationContext,\r\n} from '../types.js';\r\n\r\n// ============================================================================\r\n// Event Payload Types\r\n// ============================================================================\r\n\r\nexport interface EmployeeHiredEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n position: string;\r\n department?: string;\r\n };\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface EmployeeTerminatedEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n name?: string;\r\n };\r\n terminationDate: Date;\r\n reason?: string;\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface EmployeeRehiredEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n position: string;\r\n };\r\n previousTerminationDate?: Date;\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface SalaryUpdatedEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n };\r\n previousSalary: number;\r\n newSalary: number;\r\n effectiveFrom: Date;\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface SalaryProcessedEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n name?: string;\r\n };\r\n payroll: {\r\n id: ObjectId;\r\n period: { month: number; year: number };\r\n grossAmount: number;\r\n netAmount: number;\r\n };\r\n transactionId: ObjectId;\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface SalaryFailedEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n };\r\n period: { month: number; year: number };\r\n error: string;\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface PayrollCompletedEventPayload {\r\n organizationId: ObjectId;\r\n period: { month: number; year: number };\r\n summary: {\r\n total: number;\r\n successful: number;\r\n failed: number;\r\n totalAmount: number;\r\n };\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface PayrollExportedEventPayload {\r\n organizationId: ObjectId;\r\n dateRange: { start: Date; end: Date };\r\n recordCount: number;\r\n format: string;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface CompensationChangedEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n };\r\n changeType: 'allowance_added' | 'allowance_removed' | 'deduction_added' | 'deduction_removed';\r\n details: {\r\n type: string;\r\n amount: number;\r\n };\r\n organizationId: ObjectId;\r\n context?: OperationContext;\r\n}\r\n\r\nexport interface MilestoneAchievedEventPayload {\r\n employee: {\r\n id: ObjectId;\r\n employeeId: string;\r\n name?: string;\r\n };\r\n milestone: {\r\n type: 'tenure' | 'salary' | 'payments';\r\n value: number;\r\n message: string;\r\n };\r\n organizationId: ObjectId;\r\n}\r\n\r\n// ============================================================================\r\n// Event Map\r\n// ============================================================================\r\n\r\nexport interface PayrollEventMap {\r\n 'employee:hired': EmployeeHiredEventPayload;\r\n 'employee:terminated': EmployeeTerminatedEventPayload;\r\n 'employee:rehired': EmployeeRehiredEventPayload;\r\n 'salary:updated': SalaryUpdatedEventPayload;\r\n 'salary:processed': SalaryProcessedEventPayload;\r\n 'salary:failed': SalaryFailedEventPayload;\r\n 'payroll:completed': PayrollCompletedEventPayload;\r\n 'payroll:exported': PayrollExportedEventPayload;\r\n 'compensation:changed': CompensationChangedEventPayload;\r\n 'milestone:achieved': MilestoneAchievedEventPayload;\r\n}\r\n\r\nexport type PayrollEventType = keyof PayrollEventMap;\r\n\r\n// ============================================================================\r\n// Event Handler Types\r\n// ============================================================================\r\n\r\nexport type EventHandler<T> = (payload: T) => void | Promise<void>;\r\n\r\nexport type PayrollEventHandler<K extends PayrollEventType> = EventHandler<\r\n PayrollEventMap[K]\r\n>;\r\n\r\n// ============================================================================\r\n// EventBus Class\r\n// ============================================================================\r\n\r\nexport class EventBus {\r\n private handlers = new Map<\r\n PayrollEventType,\r\n Set<EventHandler<unknown>>\r\n >();\r\n\r\n /**\r\n * Register an event handler\r\n */\r\n on<K extends PayrollEventType>(\r\n event: K,\r\n handler: PayrollEventHandler<K>\r\n ): () => void {\r\n if (!this.handlers.has(event)) {\r\n this.handlers.set(event, new Set());\r\n }\r\n this.handlers.get(event)!.add(handler as EventHandler<unknown>);\r\n\r\n // Return unsubscribe function\r\n return () => this.off(event, handler);\r\n }\r\n\r\n /**\r\n * Register a one-time event handler\r\n */\r\n once<K extends PayrollEventType>(\r\n event: K,\r\n handler: PayrollEventHandler<K>\r\n ): () => void {\r\n const wrappedHandler: PayrollEventHandler<K> = async (payload) => {\r\n this.off(event, wrappedHandler);\r\n await handler(payload);\r\n };\r\n return this.on(event, wrappedHandler);\r\n }\r\n\r\n /**\r\n * Remove an event handler\r\n */\r\n off<K extends PayrollEventType>(\r\n event: K,\r\n handler: PayrollEventHandler<K>\r\n ): void {\r\n const eventHandlers = this.handlers.get(event);\r\n if (eventHandlers) {\r\n eventHandlers.delete(handler as EventHandler<unknown>);\r\n }\r\n }\r\n\r\n /**\r\n * Emit an event\r\n */\r\n async emit<K extends PayrollEventType>(\r\n event: K,\r\n payload: PayrollEventMap[K]\r\n ): Promise<void> {\r\n const eventHandlers = this.handlers.get(event);\r\n if (!eventHandlers || eventHandlers.size === 0) {\r\n return;\r\n }\r\n\r\n const handlers = Array.from(eventHandlers);\r\n await Promise.all(\r\n handlers.map(async (handler) => {\r\n try {\r\n await handler(payload);\r\n } catch (error) {\r\n console.error(`Event handler error for ${event}:`, error);\r\n }\r\n })\r\n );\r\n }\r\n\r\n /**\r\n * Emit event synchronously (fire-and-forget)\r\n */\r\n emitSync<K extends PayrollEventType>(\r\n event: K,\r\n payload: PayrollEventMap[K]\r\n ): void {\r\n void this.emit(event, payload);\r\n }\r\n\r\n /**\r\n * Remove all handlers for an event\r\n */\r\n removeAllListeners(event?: PayrollEventType): void {\r\n if (event) {\r\n this.handlers.delete(event);\r\n } else {\r\n this.handlers.clear();\r\n }\r\n }\r\n\r\n /**\r\n * Get listener count for an event\r\n */\r\n listenerCount(event: PayrollEventType): number {\r\n return this.handlers.get(event)?.size ?? 0;\r\n }\r\n\r\n /**\r\n * Get all registered events\r\n */\r\n eventNames(): PayrollEventType[] {\r\n return Array.from(this.handlers.keys());\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Default EventBus Instance\r\n// ============================================================================\r\n\r\nlet defaultEventBus: EventBus | null = null;\r\n\r\n/**\r\n * Get or create the default event bus\r\n */\r\nexport function getEventBus(): EventBus {\r\n if (!defaultEventBus) {\r\n defaultEventBus = new EventBus();\r\n }\r\n return defaultEventBus;\r\n}\r\n\r\n/**\r\n * Create a new event bus instance\r\n */\r\nexport function createEventBus(): EventBus {\r\n return new EventBus();\r\n}\r\n\r\n/**\r\n * Reset the default event bus (for testing)\r\n */\r\nexport function resetEventBus(): void {\r\n if (defaultEventBus) {\r\n defaultEventBus.removeAllListeners();\r\n }\r\n defaultEventBus = null;\r\n}\r\n\r\n// ============================================================================\r\n// Convenience Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Subscribe to employee hired events\r\n */\r\nexport function onEmployeeHired(\r\n handler: PayrollEventHandler<'employee:hired'>\r\n): () => void {\r\n return getEventBus().on('employee:hired', handler);\r\n}\r\n\r\n/**\r\n * Subscribe to salary processed events\r\n */\r\nexport function onSalaryProcessed(\r\n handler: PayrollEventHandler<'salary:processed'>\r\n): () => void {\r\n return getEventBus().on('salary:processed', handler);\r\n}\r\n\r\n/**\r\n * Subscribe to payroll completed events\r\n */\r\nexport function onPayrollCompleted(\r\n handler: PayrollEventHandler<'payroll:completed'>\r\n): () => void {\r\n return getEventBus().on('payroll:completed', handler);\r\n}\r\n\r\n/**\r\n * Subscribe to milestone achieved events\r\n */\r\nexport function onMilestoneAchieved(\r\n handler: PayrollEventHandler<'milestone:achieved'>\r\n): () => void {\r\n return getEventBus().on('milestone:achieved', handler);\r\n}\r\n\r\n","/**\r\n * @classytic/payroll - Plugin System\r\n *\r\n * Extensible plugin architecture for customization\r\n * Follows patterns from popular libraries like Mongoose, Fastify\r\n */\r\n\r\nimport type { PayrollInstance, PayrollPlugin, Logger } from '../types.js';\r\nimport type { EventBus, PayrollEventType, PayrollEventMap } from './events.js';\r\n\r\n// ============================================================================\r\n// Plugin Context\r\n// ============================================================================\r\n\r\nexport interface PluginContext {\r\n /** Payroll instance */\r\n payroll: PayrollInstance;\r\n /** Event bus for subscribing to events */\r\n events: EventBus;\r\n /** Logger instance */\r\n logger: PluginLogger;\r\n /** Configuration getter */\r\n getConfig: <T>(key: string) => T | undefined;\r\n /** Register a hook */\r\n addHook: <K extends PayrollEventType>(\r\n event: K,\r\n handler: (payload: PayrollEventMap[K]) => void | Promise<void>\r\n ) => () => void;\r\n}\r\n\r\nexport interface PluginLogger {\r\n info(message: string, meta?: Record<string, unknown>): void;\r\n error(message: string, meta?: Record<string, unknown>): void;\r\n warn(message: string, meta?: Record<string, unknown>): void;\r\n debug(message: string, meta?: Record<string, unknown>): void;\r\n}\r\n\r\n// ============================================================================\r\n// Plugin Hooks\r\n// ============================================================================\r\n\r\nexport interface PluginHooks {\r\n /** Called before employee is hired */\r\n beforeHire?: (params: unknown) => void | Promise<void>;\r\n /** Called after employee is hired */\r\n afterHire?: (employee: unknown) => void | Promise<void>;\r\n /** Called before salary is processed */\r\n beforeProcessSalary?: (params: unknown) => void | Promise<void>;\r\n /** Called after salary is processed */\r\n afterProcessSalary?: (result: unknown) => void | Promise<void>;\r\n /** Called before termination */\r\n beforeTerminate?: (params: unknown) => void | Promise<void>;\r\n /** Called after termination */\r\n afterTerminate?: (employee: unknown) => void | Promise<void>;\r\n /** Called on any error */\r\n onError?: (error: Error, context: string) => void | Promise<void>;\r\n}\r\n\r\n// ============================================================================\r\n// ClockIn Plugin Interface\r\n// ============================================================================\r\n\r\nexport interface PayrollPluginDefinition {\r\n name: string;\r\n version?: string;\r\n hooks?: PluginHooks;\r\n init?: (context: PluginContext) => void | Promise<void>;\r\n destroy?: () => void | Promise<void>;\r\n}\r\n\r\n// ============================================================================\r\n// Plugin Manager\r\n// ============================================================================\r\n\r\nexport class PluginManager {\r\n private plugins = new Map<string, PayrollPluginDefinition>();\r\n private hooks = new Map<keyof PluginHooks, Array<NonNullable<PluginHooks[keyof PluginHooks]>>>();\r\n\r\n constructor(private context: PluginContext) {}\r\n\r\n /**\r\n * Register a plugin\r\n */\r\n async register(plugin: PayrollPluginDefinition): Promise<void> {\r\n if (this.plugins.has(plugin.name)) {\r\n throw new Error(`Plugin \"${plugin.name}\" is already registered`);\r\n }\r\n\r\n // Register hooks\r\n if (plugin.hooks) {\r\n for (const [hookName, handler] of Object.entries(plugin.hooks)) {\r\n if (handler) {\r\n this.addHook(hookName as keyof PluginHooks, handler);\r\n }\r\n }\r\n }\r\n\r\n // Initialize plugin\r\n if (plugin.init) {\r\n await plugin.init(this.context);\r\n }\r\n\r\n this.plugins.set(plugin.name, plugin);\r\n this.context.logger.debug(`Plugin \"${plugin.name}\" registered`);\r\n }\r\n\r\n /**\r\n * Unregister a plugin\r\n */\r\n async unregister(name: string): Promise<void> {\r\n const plugin = this.plugins.get(name);\r\n if (!plugin) {\r\n return;\r\n }\r\n\r\n if (plugin.destroy) {\r\n await plugin.destroy();\r\n }\r\n\r\n this.plugins.delete(name);\r\n this.context.logger.debug(`Plugin \"${name}\" unregistered`);\r\n }\r\n\r\n /**\r\n * Add a hook handler\r\n */\r\n private addHook<K extends keyof PluginHooks>(\r\n hookName: K,\r\n handler: NonNullable<PluginHooks[K]>\r\n ): void {\r\n if (!this.hooks.has(hookName)) {\r\n this.hooks.set(hookName, []);\r\n }\r\n this.hooks.get(hookName)!.push(handler);\r\n }\r\n\r\n /**\r\n * Execute hooks for a given event\r\n */\r\n async executeHooks<K extends keyof PluginHooks>(\r\n hookName: K,\r\n ...args: Parameters<NonNullable<PluginHooks[K]>>\r\n ): Promise<void> {\r\n const handlers = this.hooks.get(hookName);\r\n if (!handlers || handlers.length === 0) {\r\n return;\r\n }\r\n\r\n for (const handler of handlers) {\r\n try {\r\n await (handler as (...args: unknown[]) => void | Promise<void>)(...args);\r\n } catch (error) {\r\n this.context.logger.error(`Hook \"${hookName}\" error:`, { error });\r\n // Execute onError hooks\r\n const errorHandlers = this.hooks.get('onError');\r\n if (errorHandlers) {\r\n for (const errorHandler of errorHandlers) {\r\n try {\r\n await (errorHandler as PluginHooks['onError'])!(error as Error, hookName);\r\n } catch {\r\n // Ignore errors in error handlers\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Get registered plugin names\r\n */\r\n getPluginNames(): string[] {\r\n return Array.from(this.plugins.keys());\r\n }\r\n\r\n /**\r\n * Check if plugin is registered\r\n */\r\n hasPlugin(name: string): boolean {\r\n return this.plugins.has(name);\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Plugin Definition Helper\r\n// ============================================================================\r\n\r\n/**\r\n * Define a plugin with type safety\r\n */\r\nexport function definePlugin(\r\n definition: PayrollPluginDefinition\r\n): PayrollPluginDefinition {\r\n return definition;\r\n}\r\n\r\n// ============================================================================\r\n// Built-in Plugins\r\n// ============================================================================\r\n\r\n/**\r\n * Logging plugin - logs all payroll events\r\n */\r\nexport const loggingPlugin = definePlugin({\r\n name: 'logging',\r\n version: '1.0.0',\r\n init: (context) => {\r\n // Subscribe to all events\r\n context.addHook('employee:hired', (payload) => {\r\n context.logger.info('Employee hired', {\r\n employeeId: payload.employee.employeeId,\r\n position: payload.employee.position,\r\n });\r\n });\r\n\r\n context.addHook('salary:processed', (payload) => {\r\n context.logger.info('Salary processed', {\r\n employeeId: payload.employee.employeeId,\r\n amount: payload.payroll.netAmount,\r\n period: payload.payroll.period,\r\n });\r\n });\r\n\r\n context.addHook('employee:terminated', (payload) => {\r\n context.logger.info('Employee terminated', {\r\n employeeId: payload.employee.employeeId,\r\n reason: payload.reason,\r\n });\r\n });\r\n },\r\n hooks: {\r\n onError: (error, context) => {\r\n console.error(`[Payroll Error] ${context}:`, error.message);\r\n },\r\n },\r\n});\r\n\r\n/**\r\n * Metrics plugin - collects payroll metrics\r\n */\r\nexport const metricsPlugin = definePlugin({\r\n name: 'metrics',\r\n version: '1.0.0',\r\n init: (context) => {\r\n const metrics = {\r\n employeesHired: 0,\r\n employeesTerminated: 0,\r\n salariesProcessed: 0,\r\n totalPaid: 0,\r\n errors: 0,\r\n };\r\n\r\n context.addHook('employee:hired', () => {\r\n metrics.employeesHired++;\r\n });\r\n\r\n context.addHook('employee:terminated', () => {\r\n metrics.employeesTerminated++;\r\n });\r\n\r\n context.addHook('salary:processed', (payload) => {\r\n metrics.salariesProcessed++;\r\n metrics.totalPaid += payload.payroll.netAmount;\r\n });\r\n\r\n // Expose metrics on payroll instance\r\n (context.payroll as unknown as { metrics: typeof metrics }).metrics = metrics;\r\n },\r\n hooks: {\r\n onError: (error, context) => {\r\n // Increment error counter\r\n },\r\n },\r\n});\r\n\r\n/**\r\n * Notification plugin - sends notifications for events\r\n */\r\nexport interface NotificationPluginOptions {\r\n onHired?: (employee: { id: unknown; name?: string }) => void | Promise<void>;\r\n onTerminated?: (employee: { id: unknown; name?: string }) => void | Promise<void>;\r\n onSalaryProcessed?: (details: { \r\n employee: { id: unknown; name?: string };\r\n amount: number;\r\n }) => void | Promise<void>;\r\n onMilestone?: (details: {\r\n employee: { id: unknown; name?: string };\r\n milestone: string;\r\n }) => void | Promise<void>;\r\n}\r\n\r\nexport function createNotificationPlugin(\r\n options: NotificationPluginOptions\r\n): PayrollPluginDefinition {\r\n return definePlugin({\r\n name: 'notification',\r\n version: '1.0.0',\r\n init: (context) => {\r\n if (options.onHired) {\r\n context.addHook('employee:hired', async (payload) => {\r\n await options.onHired!({\r\n id: payload.employee.id,\r\n name: payload.employee.position,\r\n });\r\n });\r\n }\r\n\r\n if (options.onTerminated) {\r\n context.addHook('employee:terminated', async (payload) => {\r\n await options.onTerminated!({\r\n id: payload.employee.id,\r\n name: payload.employee.name,\r\n });\r\n });\r\n }\r\n\r\n if (options.onSalaryProcessed) {\r\n context.addHook('salary:processed', async (payload) => {\r\n await options.onSalaryProcessed!({\r\n employee: {\r\n id: payload.employee.id,\r\n name: payload.employee.name,\r\n },\r\n amount: payload.payroll.netAmount,\r\n });\r\n });\r\n }\r\n\r\n if (options.onMilestone) {\r\n context.addHook('milestone:achieved', async (payload) => {\r\n await options.onMilestone!({\r\n employee: {\r\n id: payload.employee.id,\r\n name: payload.employee.name,\r\n },\r\n milestone: payload.milestone.message,\r\n });\r\n });\r\n }\r\n },\r\n });\r\n}\r\n\r\n// Alias for backwards compatibility\r\nexport const notificationPlugin = createNotificationPlugin({});\r\n\r\n","/**\r\n * @classytic/payroll - Date Utilities\r\n *\r\n * Pure, composable, testable date operations\r\n * No side effects, no mutations\r\n */\r\n\r\nimport type { PayPeriodInfo, PaymentFrequency } from '../types.js';\r\n\r\n// ============================================================================\r\n// Date Arithmetic\r\n// ============================================================================\r\n\r\n/**\r\n * Add days to a date\r\n */\r\nexport function addDays(date: Date, days: number): Date {\r\n const result = new Date(date);\r\n result.setDate(result.getDate() + days);\r\n return result;\r\n}\r\n\r\n/**\r\n * Add months to a date\r\n */\r\nexport function addMonths(date: Date, months: number): Date {\r\n const result = new Date(date);\r\n result.setMonth(result.getMonth() + months);\r\n return result;\r\n}\r\n\r\n/**\r\n * Add years to a date\r\n */\r\nexport function addYears(date: Date, years: number): Date {\r\n const result = new Date(date);\r\n result.setFullYear(result.getFullYear() + years);\r\n return result;\r\n}\r\n\r\n/**\r\n * Subtract days from a date\r\n */\r\nexport function subDays(date: Date, days: number): Date {\r\n return addDays(date, -days);\r\n}\r\n\r\n/**\r\n * Subtract months from a date\r\n */\r\nexport function subMonths(date: Date, months: number): Date {\r\n return addMonths(date, -months);\r\n}\r\n\r\n// ============================================================================\r\n// Date Boundaries\r\n// ============================================================================\r\n\r\n/**\r\n * Get the start of a month\r\n */\r\nexport function startOfMonth(date: Date): Date {\r\n const result = new Date(date);\r\n result.setDate(1);\r\n result.setHours(0, 0, 0, 0);\r\n return result;\r\n}\r\n\r\n/**\r\n * Get the end of a month\r\n */\r\nexport function endOfMonth(date: Date): Date {\r\n const result = new Date(date);\r\n result.setMonth(result.getMonth() + 1, 0);\r\n result.setHours(23, 59, 59, 999);\r\n return result;\r\n}\r\n\r\n/**\r\n * Get the start of a year\r\n */\r\nexport function startOfYear(date: Date): Date {\r\n const result = new Date(date);\r\n result.setMonth(0, 1);\r\n result.setHours(0, 0, 0, 0);\r\n return result;\r\n}\r\n\r\n/**\r\n * Get the end of a year\r\n */\r\nexport function endOfYear(date: Date): Date {\r\n const result = new Date(date);\r\n result.setMonth(11, 31);\r\n result.setHours(23, 59, 59, 999);\r\n return result;\r\n}\r\n\r\n/**\r\n * Get the start of a day\r\n */\r\nexport function startOfDay(date: Date): Date {\r\n const result = new Date(date);\r\n result.setHours(0, 0, 0, 0);\r\n return result;\r\n}\r\n\r\n/**\r\n * Get the end of a day\r\n */\r\nexport function endOfDay(date: Date): Date {\r\n const result = new Date(date);\r\n result.setHours(23, 59, 59, 999);\r\n return result;\r\n}\r\n\r\n// ============================================================================\r\n// Date Differences\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate difference in days between two dates\r\n */\r\nexport function diffInDays(start: Date, end: Date): number {\r\n return Math.ceil(\r\n (new Date(end).getTime() - new Date(start).getTime()) / (1000 * 60 * 60 * 24)\r\n );\r\n}\r\n\r\n/**\r\n * Calculate difference in months between two dates\r\n */\r\nexport function diffInMonths(start: Date, end: Date): number {\r\n const startDate = new Date(start);\r\n const endDate = new Date(end);\r\n return (\r\n (endDate.getFullYear() - startDate.getFullYear()) * 12 +\r\n (endDate.getMonth() - startDate.getMonth())\r\n );\r\n}\r\n\r\n/**\r\n * Calculate difference in years between two dates\r\n */\r\nexport function diffInYears(start: Date, end: Date): number {\r\n return Math.floor(diffInMonths(start, end) / 12);\r\n}\r\n\r\n// Aliases for backwards compatibility\r\nexport const daysBetween = diffInDays;\r\nexport const monthsBetween = diffInMonths;\r\n\r\n// ============================================================================\r\n// Day Type Checks\r\n// ============================================================================\r\n\r\n/**\r\n * Check if date is a weekday (Mon-Fri)\r\n */\r\nexport function isWeekday(date: Date): boolean {\r\n const day = new Date(date).getDay();\r\n return day >= 1 && day <= 5;\r\n}\r\n\r\n/**\r\n * Check if date is a weekend (Sat-Sun)\r\n */\r\nexport function isWeekend(date: Date): boolean {\r\n const day = new Date(date).getDay();\r\n return day === 0 || day === 6;\r\n}\r\n\r\n/**\r\n * Get day of week (0=Sunday, 6=Saturday)\r\n */\r\nexport function getDayOfWeek(date: Date): number {\r\n return new Date(date).getDay();\r\n}\r\n\r\n/**\r\n * Get day name\r\n */\r\nexport function getDayName(date: Date): string {\r\n const days = [\r\n 'Sunday',\r\n 'Monday',\r\n 'Tuesday',\r\n 'Wednesday',\r\n 'Thursday',\r\n 'Friday',\r\n 'Saturday',\r\n ];\r\n return days[getDayOfWeek(date)];\r\n}\r\n\r\n// ============================================================================\r\n// Pay Period Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Get pay period for a given month and year\r\n */\r\nexport function getPayPeriod(month: number, year: number): PayPeriodInfo {\r\n const startDate = new Date(year, month - 1, 1);\r\n return {\r\n month,\r\n year,\r\n startDate: startOfMonth(startDate),\r\n endDate: endOfMonth(startDate),\r\n };\r\n}\r\n\r\n/**\r\n * Get current pay period\r\n */\r\nexport function getCurrentPeriod(date = new Date()): { year: number; month: number } {\r\n const d = new Date(date);\r\n return {\r\n year: d.getFullYear(),\r\n month: d.getMonth() + 1,\r\n };\r\n}\r\n\r\n/**\r\n * Get working days in a month\r\n */\r\nexport function getWorkingDaysInMonth(year: number, month: number): number {\r\n const start = new Date(year, month - 1, 1);\r\n const end = endOfMonth(start);\r\n let count = 0;\r\n \r\n const current = new Date(start);\r\n while (current <= end) {\r\n if (isWeekday(current)) {\r\n count++;\r\n }\r\n current.setDate(current.getDate() + 1);\r\n }\r\n \r\n return count;\r\n}\r\n\r\n/**\r\n * Get total days in a month\r\n */\r\nexport function getDaysInMonth(year: number, month: number): number {\r\n return new Date(year, month, 0).getDate();\r\n}\r\n\r\n// ============================================================================\r\n// Employment Date Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate probation end date\r\n */\r\nexport function calculateProbationEnd(\r\n hireDate: Date,\r\n probationMonths: number\r\n): Date | null {\r\n if (!probationMonths || probationMonths <= 0) return null;\r\n return addMonths(hireDate, probationMonths);\r\n}\r\n\r\n/**\r\n * Check if employee is on probation\r\n */\r\nexport function isOnProbation(\r\n probationEndDate: Date | null | undefined,\r\n now = new Date()\r\n): boolean {\r\n if (!probationEndDate) return false;\r\n return now < new Date(probationEndDate);\r\n}\r\n\r\n/**\r\n * Calculate years of service\r\n */\r\nexport function calculateYearsOfService(\r\n hireDate: Date,\r\n terminationDate?: Date | null\r\n): number {\r\n const end = terminationDate || new Date();\r\n const days = diffInDays(hireDate, end);\r\n return Math.max(0, Math.floor((days / 365.25) * 10) / 10);\r\n}\r\n\r\n// ============================================================================\r\n// Range Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Check if a date is within a range\r\n */\r\nexport function isDateInRange(date: Date, start: Date, end: Date): boolean {\r\n const checkDate = new Date(date);\r\n return checkDate >= new Date(start) && checkDate <= new Date(end);\r\n}\r\n\r\n/**\r\n * Get date range for a pay period\r\n */\r\nexport function getPayPeriodDateRange(\r\n month: number,\r\n year: number\r\n): { start: Date; end: Date } {\r\n const period = getPayPeriod(month, year);\r\n return { start: period.startDate, end: period.endDate };\r\n}\r\n\r\n// ============================================================================\r\n// Formatting Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Format date for database storage\r\n */\r\nexport function formatDateForDB(date: Date): string {\r\n if (!date) return '';\r\n return new Date(date).toISOString();\r\n}\r\n\r\n/**\r\n * Parse date from database\r\n */\r\nexport function parseDBDate(dateString: string): Date | null {\r\n if (!dateString) return null;\r\n return new Date(dateString);\r\n}\r\n\r\n/**\r\n * Format period as string (e.g., \"01/2025\")\r\n */\r\nexport function formatPeriod({ month, year }: { month: number; year: number }): string {\r\n return `${String(month).padStart(2, '0')}/${year}`;\r\n}\r\n\r\n/**\r\n * Parse period string back to object\r\n */\r\nexport function parsePeriod(periodString: string): { month: number; year: number } {\r\n const [month, year] = periodString.split('/').map(Number);\r\n return { month, year };\r\n}\r\n\r\n/**\r\n * Format month name\r\n */\r\nexport function getMonthName(month: number): string {\r\n const months = [\r\n 'January', 'February', 'March', 'April', 'May', 'June',\r\n 'July', 'August', 'September', 'October', 'November', 'December',\r\n ];\r\n return months[month - 1] || '';\r\n}\r\n\r\n/**\r\n * Format short month name\r\n */\r\nexport function getShortMonthName(month: number): string {\r\n const months = [\r\n 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',\r\n 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',\r\n ];\r\n return months[month - 1] || '';\r\n}\r\n\r\n// ============================================================================\r\n// Default Export\r\n// ============================================================================\r\n\r\nexport default {\r\n addDays,\r\n addMonths,\r\n addYears,\r\n subDays,\r\n subMonths,\r\n startOfMonth,\r\n endOfMonth,\r\n startOfYear,\r\n endOfYear,\r\n startOfDay,\r\n endOfDay,\r\n diffInDays,\r\n diffInMonths,\r\n diffInYears,\r\n daysBetween,\r\n monthsBetween,\r\n isWeekday,\r\n isWeekend,\r\n getDayOfWeek,\r\n getDayName,\r\n getPayPeriod,\r\n getCurrentPeriod,\r\n getWorkingDaysInMonth,\r\n getDaysInMonth,\r\n calculateProbationEnd,\r\n isOnProbation,\r\n calculateYearsOfService,\r\n isDateInRange,\r\n getPayPeriodDateRange,\r\n formatDateForDB,\r\n parseDBDate,\r\n formatPeriod,\r\n parsePeriod,\r\n getMonthName,\r\n getShortMonthName,\r\n};\r\n\r\n","/**\r\n * @classytic/payroll - Employee Factory\r\n *\r\n * Clean object creation for employee documents\r\n * Builder pattern for fluent API\r\n */\r\n\r\nimport type {\r\n ObjectIdLike,\r\n EmploymentType,\r\n Department,\r\n Compensation,\r\n BankDetails,\r\n WorkSchedule,\r\n Allowance,\r\n Deduction,\r\n PaymentFrequency,\r\n TerminationReason,\r\n} from '../types.js';\r\nimport { calculateProbationEnd } from '../utils/date.js';\r\nimport { HRM_CONFIG } from '../config.js';\r\n\r\n// ============================================================================\r\n// Employee Factory Types\r\n// ============================================================================\r\n\r\nexport interface CreateEmployeeParams {\r\n userId: ObjectIdLike;\r\n organizationId: ObjectIdLike;\r\n employment: {\r\n employeeId?: string;\r\n type?: EmploymentType;\r\n department?: Department | string;\r\n position: string;\r\n hireDate?: Date;\r\n probationMonths?: number;\r\n workSchedule?: WorkSchedule;\r\n };\r\n compensation: {\r\n baseAmount: number;\r\n frequency?: PaymentFrequency;\r\n currency?: string;\r\n allowances?: Array<Partial<Allowance>>;\r\n deductions?: Array<Partial<Deduction>>;\r\n };\r\n bankDetails?: BankDetails;\r\n}\r\n\r\nexport interface EmployeeData {\r\n userId: ObjectIdLike;\r\n organizationId: ObjectIdLike;\r\n employeeId: string;\r\n employmentType: EmploymentType;\r\n status: 'active';\r\n department?: Department;\r\n position: string;\r\n hireDate: Date;\r\n probationEndDate: Date | null;\r\n compensation: Compensation;\r\n workSchedule: WorkSchedule;\r\n bankDetails: BankDetails;\r\n payrollStats: {\r\n totalPaid: number;\r\n paymentsThisYear: number;\r\n averageMonthly: number;\r\n };\r\n}\r\n\r\nexport interface TerminationData {\r\n terminatedAt: Date;\r\n terminationReason: TerminationReason;\r\n terminationNotes?: string;\r\n terminatedBy: {\r\n userId?: ObjectIdLike;\r\n name?: string;\r\n role?: string;\r\n };\r\n}\r\n\r\n// ============================================================================\r\n// Employee Factory\r\n// ============================================================================\r\n\r\nexport class EmployeeFactory {\r\n /**\r\n * Create employee data object\r\n */\r\n static create(params: CreateEmployeeParams): EmployeeData {\r\n const { userId, organizationId, employment, compensation, bankDetails } = params;\r\n const hireDate = employment.hireDate || new Date();\r\n\r\n return {\r\n userId,\r\n organizationId,\r\n employeeId: employment.employeeId || `EMP-${Date.now()}-${Math.random().toString(36).substr(2, 6).toUpperCase()}`,\r\n employmentType: employment.type || 'full_time',\r\n status: 'active',\r\n department: employment.department as Department | undefined,\r\n position: employment.position,\r\n hireDate,\r\n probationEndDate: calculateProbationEnd(\r\n hireDate,\r\n employment.probationMonths ?? HRM_CONFIG.employment.defaultProbationMonths\r\n ),\r\n compensation: this.createCompensation(compensation),\r\n workSchedule: employment.workSchedule || this.defaultWorkSchedule(),\r\n bankDetails: bankDetails || {},\r\n payrollStats: {\r\n totalPaid: 0,\r\n paymentsThisYear: 0,\r\n averageMonthly: 0,\r\n },\r\n };\r\n }\r\n\r\n /**\r\n * Create compensation object\r\n */\r\n static createCompensation(params: {\r\n baseAmount: number;\r\n frequency?: PaymentFrequency;\r\n currency?: string;\r\n allowances?: Array<Partial<Allowance>>;\r\n deductions?: Array<Partial<Deduction>>;\r\n }): Compensation {\r\n return {\r\n baseAmount: params.baseAmount,\r\n frequency: params.frequency || 'monthly',\r\n currency: params.currency || HRM_CONFIG.payroll.defaultCurrency,\r\n allowances: (params.allowances || []).map((a) => ({\r\n type: a.type || 'other',\r\n name: a.name || a.type || 'other',\r\n amount: a.amount || 0,\r\n taxable: a.taxable,\r\n recurring: a.recurring,\r\n effectiveFrom: a.effectiveFrom,\r\n effectiveTo: a.effectiveTo,\r\n })),\r\n deductions: (params.deductions || []).map((d) => ({\r\n type: d.type || 'other',\r\n name: d.name || d.type || 'other',\r\n amount: d.amount || 0,\r\n auto: d.auto,\r\n recurring: d.recurring,\r\n description: d.description,\r\n effectiveFrom: d.effectiveFrom,\r\n effectiveTo: d.effectiveTo,\r\n })),\r\n grossSalary: 0,\r\n netSalary: 0,\r\n effectiveFrom: new Date(),\r\n lastModified: new Date(),\r\n };\r\n }\r\n\r\n /**\r\n * Create allowance object\r\n */\r\n static createAllowance(params: {\r\n type: Allowance['type'];\r\n amount: number;\r\n name?: string;\r\n isPercentage?: boolean;\r\n taxable?: boolean;\r\n recurring?: boolean;\r\n }): Allowance {\r\n return {\r\n type: params.type,\r\n name: params.name || params.type,\r\n amount: params.amount,\r\n isPercentage: params.isPercentage ?? false,\r\n taxable: params.taxable ?? true,\r\n recurring: params.recurring ?? true,\r\n effectiveFrom: new Date(),\r\n };\r\n }\r\n\r\n /**\r\n * Create deduction object\r\n */\r\n static createDeduction(params: {\r\n type: Deduction['type'];\r\n amount: number;\r\n name?: string;\r\n isPercentage?: boolean;\r\n auto?: boolean;\r\n recurring?: boolean;\r\n description?: string;\r\n }): Deduction {\r\n return {\r\n type: params.type,\r\n name: params.name || params.type,\r\n amount: params.amount,\r\n isPercentage: params.isPercentage ?? false,\r\n auto: params.auto ?? false,\r\n recurring: params.recurring ?? true,\r\n description: params.description,\r\n effectiveFrom: new Date(),\r\n };\r\n }\r\n\r\n /**\r\n * Default work schedule\r\n */\r\n static defaultWorkSchedule(): WorkSchedule {\r\n return {\r\n hoursPerWeek: 40,\r\n hoursPerDay: 8,\r\n workingDays: [1, 2, 3, 4, 5], // Mon-Fri\r\n shiftStart: '09:00',\r\n shiftEnd: '17:00',\r\n };\r\n }\r\n\r\n /**\r\n * Create termination data\r\n */\r\n static createTermination(params: {\r\n reason: TerminationReason;\r\n date?: Date;\r\n notes?: string;\r\n context?: {\r\n userId?: ObjectIdLike;\r\n userName?: string;\r\n userRole?: string;\r\n };\r\n }): TerminationData {\r\n return {\r\n terminatedAt: params.date || new Date(),\r\n terminationReason: params.reason,\r\n terminationNotes: params.notes,\r\n terminatedBy: {\r\n userId: params.context?.userId,\r\n name: params.context?.userName,\r\n role: params.context?.userRole,\r\n },\r\n };\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Employee Builder\r\n// ============================================================================\r\n\r\nexport class EmployeeBuilder {\r\n private data: Partial<CreateEmployeeParams> = {\r\n employment: {} as CreateEmployeeParams['employment'],\r\n compensation: {} as CreateEmployeeParams['compensation'],\r\n bankDetails: {},\r\n };\r\n\r\n /**\r\n * Set user ID\r\n */\r\n forUser(userId: ObjectIdLike): this {\r\n this.data.userId = userId;\r\n return this;\r\n }\r\n\r\n /**\r\n * Set organization ID\r\n */\r\n inOrganization(organizationId: ObjectIdLike): this {\r\n this.data.organizationId = organizationId;\r\n return this;\r\n }\r\n\r\n /**\r\n * Set employee ID\r\n */\r\n withEmployeeId(employeeId: string): this {\r\n this.data.employment = { ...this.data.employment!, employeeId };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set department\r\n */\r\n inDepartment(department: Department): this {\r\n this.data.employment = { ...this.data.employment!, department };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set position\r\n */\r\n asPosition(position: string): this {\r\n this.data.employment = { ...this.data.employment!, position };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set employment type\r\n */\r\n withEmploymentType(type: EmploymentType): this {\r\n this.data.employment = { ...this.data.employment!, type };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set hire date\r\n */\r\n hiredOn(date: Date): this {\r\n this.data.employment = { ...this.data.employment!, hireDate: date };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set probation period\r\n */\r\n withProbation(months: number): this {\r\n this.data.employment = { ...this.data.employment!, probationMonths: months };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set work schedule\r\n */\r\n withSchedule(schedule: WorkSchedule): this {\r\n this.data.employment = { ...this.data.employment!, workSchedule: schedule };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set base salary\r\n */\r\n withBaseSalary(\r\n amount: number,\r\n frequency: PaymentFrequency = 'monthly',\r\n currency = 'BDT'\r\n ): this {\r\n this.data.compensation = {\r\n ...this.data.compensation!,\r\n baseAmount: amount,\r\n frequency,\r\n currency,\r\n };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add allowance\r\n */\r\n addAllowance(\r\n type: Allowance['type'],\r\n amount: number,\r\n options: { taxable?: boolean; recurring?: boolean } = {}\r\n ): this {\r\n const allowances = this.data.compensation?.allowances || [];\r\n this.data.compensation = {\r\n ...this.data.compensation!,\r\n allowances: [\r\n ...allowances,\r\n { type, amount, taxable: options.taxable, recurring: options.recurring },\r\n ],\r\n };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add deduction\r\n */\r\n addDeduction(\r\n type: Deduction['type'],\r\n amount: number,\r\n options: { auto?: boolean; recurring?: boolean; description?: string } = {}\r\n ): this {\r\n const deductions = this.data.compensation?.deductions || [];\r\n this.data.compensation = {\r\n ...this.data.compensation!,\r\n deductions: [\r\n ...deductions,\r\n {\r\n type,\r\n amount,\r\n auto: options.auto,\r\n recurring: options.recurring,\r\n description: options.description,\r\n },\r\n ],\r\n };\r\n return this;\r\n }\r\n\r\n /**\r\n * Set bank details\r\n */\r\n withBankDetails(bankDetails: BankDetails): this {\r\n this.data.bankDetails = bankDetails;\r\n return this;\r\n }\r\n\r\n /**\r\n * Build employee data\r\n */\r\n build(): EmployeeData {\r\n if (!this.data.userId || !this.data.organizationId) {\r\n throw new Error('userId and organizationId are required');\r\n }\r\n if (!this.data.employment?.employeeId || !this.data.employment?.position) {\r\n throw new Error('employeeId and position are required');\r\n }\r\n if (!this.data.compensation?.baseAmount) {\r\n throw new Error('baseAmount is required');\r\n }\r\n\r\n return EmployeeFactory.create(this.data as CreateEmployeeParams);\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Factory Function\r\n// ============================================================================\r\n\r\n/**\r\n * Create new employee builder\r\n */\r\nexport function createEmployee(): EmployeeBuilder {\r\n return new EmployeeBuilder();\r\n}\r\n\r\n// ============================================================================\r\n// Default Export\r\n// ============================================================================\r\n\r\nexport default {\r\n EmployeeFactory,\r\n EmployeeBuilder,\r\n createEmployee,\r\n};\r\n\r\n","/**\r\n * @classytic/payroll - Enums\r\n *\r\n * Type-safe enum definitions with const assertions\r\n * Single source of truth for all enum values\r\n */\r\n\r\nimport type {\r\n EmploymentType,\r\n EmployeeStatus,\r\n Department,\r\n PaymentFrequency,\r\n PaymentMethod,\r\n AllowanceType,\r\n DeductionType,\r\n PayrollStatus,\r\n TerminationReason,\r\n HRMTransactionCategory,\r\n SalaryBand,\r\n OrgRole,\r\n} from './types.js';\r\n\r\n// ============================================================================\r\n// Employment Type\r\n// ============================================================================\r\n\r\nexport const EMPLOYMENT_TYPE = {\r\n FULL_TIME: 'full_time',\r\n PART_TIME: 'part_time',\r\n CONTRACT: 'contract',\r\n INTERN: 'intern',\r\n CONSULTANT: 'consultant',\r\n} as const satisfies Record<string, EmploymentType>;\r\n\r\nexport const EMPLOYMENT_TYPE_VALUES = Object.values(EMPLOYMENT_TYPE);\r\n\r\nexport function isValidEmploymentType(value: string): value is EmploymentType {\r\n return EMPLOYMENT_TYPE_VALUES.includes(value as EmploymentType);\r\n}\r\n\r\n// ============================================================================\r\n// Employee Status\r\n// ============================================================================\r\n\r\nexport const EMPLOYEE_STATUS = {\r\n ACTIVE: 'active',\r\n ON_LEAVE: 'on_leave',\r\n SUSPENDED: 'suspended',\r\n TERMINATED: 'terminated',\r\n} as const satisfies Record<string, EmployeeStatus>;\r\n\r\nexport const EMPLOYEE_STATUS_VALUES = Object.values(EMPLOYEE_STATUS);\r\n\r\nexport function isValidEmployeeStatus(value: string): value is EmployeeStatus {\r\n return EMPLOYEE_STATUS_VALUES.includes(value as EmployeeStatus);\r\n}\r\n\r\nexport function isActiveStatus(status: EmployeeStatus): boolean {\r\n return status === EMPLOYEE_STATUS.ACTIVE;\r\n}\r\n\r\nexport function isEmployedStatus(status: EmployeeStatus): boolean {\r\n return status !== EMPLOYEE_STATUS.TERMINATED;\r\n}\r\n\r\nexport function canReceiveSalaryStatus(status: EmployeeStatus): boolean {\r\n return status === EMPLOYEE_STATUS.ACTIVE || status === EMPLOYEE_STATUS.ON_LEAVE;\r\n}\r\n\r\n// ============================================================================\r\n// Department\r\n// ============================================================================\r\n\r\nexport const DEPARTMENT = {\r\n MANAGEMENT: 'management',\r\n TRAINING: 'training',\r\n SALES: 'sales',\r\n OPERATIONS: 'operations',\r\n SUPPORT: 'support',\r\n HR: 'hr',\r\n MAINTENANCE: 'maintenance',\r\n MARKETING: 'marketing',\r\n FINANCE: 'finance',\r\n IT: 'it',\r\n} as const satisfies Record<string, Department>;\r\n\r\nexport const DEPARTMENT_VALUES = Object.values(DEPARTMENT);\r\n\r\nexport function isValidDepartment(value: string): value is Department {\r\n return DEPARTMENT_VALUES.includes(value as Department);\r\n}\r\n\r\n// ============================================================================\r\n// Payment Frequency\r\n// ============================================================================\r\n\r\nexport const PAYMENT_FREQUENCY = {\r\n MONTHLY: 'monthly',\r\n BI_WEEKLY: 'bi_weekly',\r\n WEEKLY: 'weekly',\r\n HOURLY: 'hourly',\r\n DAILY: 'daily',\r\n} as const satisfies Record<string, PaymentFrequency>;\r\n\r\nexport const PAYMENT_FREQUENCY_VALUES = Object.values(PAYMENT_FREQUENCY);\r\n\r\nexport function isValidPaymentFrequency(value: string): value is PaymentFrequency {\r\n return PAYMENT_FREQUENCY_VALUES.includes(value as PaymentFrequency);\r\n}\r\n\r\n// ============================================================================\r\n// Payment Method\r\n// ============================================================================\r\n\r\nexport const PAYMENT_METHOD = {\r\n BANK: 'bank',\r\n CASH: 'cash',\r\n MOBILE: 'mobile',\r\n BKASH: 'bkash',\r\n NAGAD: 'nagad',\r\n ROCKET: 'rocket',\r\n CHECK: 'check',\r\n} as const satisfies Record<string, PaymentMethod>;\r\n\r\nexport const PAYMENT_METHOD_VALUES = Object.values(PAYMENT_METHOD);\r\n\r\nexport function isValidPaymentMethod(value: string): value is PaymentMethod {\r\n return PAYMENT_METHOD_VALUES.includes(value as PaymentMethod);\r\n}\r\n\r\n// ============================================================================\r\n// Allowance Type\r\n// ============================================================================\r\n\r\nexport const ALLOWANCE_TYPE = {\r\n HOUSING: 'housing',\r\n TRANSPORT: 'transport',\r\n MEAL: 'meal',\r\n MOBILE: 'mobile',\r\n MEDICAL: 'medical',\r\n EDUCATION: 'education',\r\n BONUS: 'bonus',\r\n OTHER: 'other',\r\n} as const satisfies Record<string, AllowanceType>;\r\n\r\nexport const ALLOWANCE_TYPE_VALUES = Object.values(ALLOWANCE_TYPE);\r\n\r\nexport function isValidAllowanceType(value: string): value is AllowanceType {\r\n return ALLOWANCE_TYPE_VALUES.includes(value as AllowanceType);\r\n}\r\n\r\n// ============================================================================\r\n// Deduction Type\r\n// ============================================================================\r\n\r\nexport const DEDUCTION_TYPE = {\r\n TAX: 'tax',\r\n LOAN: 'loan',\r\n ADVANCE: 'advance',\r\n PROVIDENT_FUND: 'provident_fund',\r\n INSURANCE: 'insurance',\r\n ABSENCE: 'absence',\r\n OTHER: 'other',\r\n} as const satisfies Record<string, DeductionType>;\r\n\r\nexport const DEDUCTION_TYPE_VALUES = Object.values(DEDUCTION_TYPE);\r\n\r\nexport function isValidDeductionType(value: string): value is DeductionType {\r\n return DEDUCTION_TYPE_VALUES.includes(value as DeductionType);\r\n}\r\n\r\n// ============================================================================\r\n// Payroll Status\r\n// ============================================================================\r\n\r\nexport const PAYROLL_STATUS = {\r\n PENDING: 'pending',\r\n PROCESSING: 'processing',\r\n PAID: 'paid',\r\n FAILED: 'failed',\r\n CANCELLED: 'cancelled',\r\n} as const satisfies Record<string, PayrollStatus>;\r\n\r\nexport const PAYROLL_STATUS_VALUES = Object.values(PAYROLL_STATUS);\r\n\r\nexport function isValidPayrollStatus(value: string): value is PayrollStatus {\r\n return PAYROLL_STATUS_VALUES.includes(value as PayrollStatus);\r\n}\r\n\r\nexport function isCompletedPayrollStatus(status: PayrollStatus): boolean {\r\n return status === PAYROLL_STATUS.PAID;\r\n}\r\n\r\nexport function isPendingPayrollStatus(status: PayrollStatus): boolean {\r\n return status === PAYROLL_STATUS.PENDING || status === PAYROLL_STATUS.PROCESSING;\r\n}\r\n\r\n// ============================================================================\r\n// Termination Reason\r\n// ============================================================================\r\n\r\nexport const TERMINATION_REASON = {\r\n RESIGNATION: 'resignation',\r\n RETIREMENT: 'retirement',\r\n TERMINATION: 'termination',\r\n CONTRACT_END: 'contract_end',\r\n MUTUAL_AGREEMENT: 'mutual_agreement',\r\n OTHER: 'other',\r\n} as const satisfies Record<string, TerminationReason>;\r\n\r\nexport const TERMINATION_REASON_VALUES = Object.values(TERMINATION_REASON);\r\n\r\nexport function isValidTerminationReason(value: string): value is TerminationReason {\r\n return TERMINATION_REASON_VALUES.includes(value as TerminationReason);\r\n}\r\n\r\n// ============================================================================\r\n// HRM Transaction Categories\r\n// ============================================================================\r\n\r\nexport const HRM_TRANSACTION_CATEGORIES = {\r\n SALARY: 'salary',\r\n BONUS: 'bonus',\r\n COMMISSION: 'commission',\r\n OVERTIME: 'overtime',\r\n SEVERANCE: 'severance',\r\n} as const satisfies Record<string, HRMTransactionCategory>;\r\n\r\nexport const HRM_CATEGORY_VALUES = Object.values(HRM_TRANSACTION_CATEGORIES);\r\n\r\nexport function isHRMManagedCategory(category: string): category is HRMTransactionCategory {\r\n return HRM_CATEGORY_VALUES.includes(category as HRMTransactionCategory);\r\n}\r\n\r\n// ============================================================================\r\n// Salary Band\r\n// ============================================================================\r\n\r\nexport const SALARY_BAND = {\r\n INTERN: 'intern',\r\n JUNIOR: 'junior',\r\n MID: 'mid',\r\n SENIOR: 'senior',\r\n LEAD: 'lead',\r\n EXECUTIVE: 'executive',\r\n CUSTOM: 'custom',\r\n} as const satisfies Record<string, SalaryBand>;\r\n\r\nexport const SALARY_BAND_VALUES = Object.values(SALARY_BAND);\r\n\r\nexport function isValidSalaryBand(value: string): value is SalaryBand {\r\n return SALARY_BAND_VALUES.includes(value as SalaryBand);\r\n}\r\n\r\n// ============================================================================\r\n// Organization Role\r\n// ============================================================================\r\n\r\nexport const ORG_ROLE = {\r\n OWNER: 'owner',\r\n MANAGER: 'manager',\r\n TRAINER: 'trainer',\r\n STAFF: 'staff',\r\n INTERN: 'intern',\r\n CONSULTANT: 'consultant',\r\n} as const satisfies Record<string, OrgRole>;\r\n\r\nexport const ORG_ROLE_VALUES = Object.values(ORG_ROLE);\r\n\r\nexport function isValidOrgRole(value: string): value is OrgRole {\r\n return ORG_ROLE_VALUES.includes(value as OrgRole);\r\n}\r\n\r\n// ============================================================================\r\n// Default Export\r\n// ============================================================================\r\n\r\nexport default {\r\n EMPLOYMENT_TYPE,\r\n EMPLOYMENT_TYPE_VALUES,\r\n EMPLOYEE_STATUS,\r\n EMPLOYEE_STATUS_VALUES,\r\n DEPARTMENT,\r\n DEPARTMENT_VALUES,\r\n PAYMENT_FREQUENCY,\r\n PAYMENT_FREQUENCY_VALUES,\r\n PAYMENT_METHOD,\r\n PAYMENT_METHOD_VALUES,\r\n ALLOWANCE_TYPE,\r\n ALLOWANCE_TYPE_VALUES,\r\n DEDUCTION_TYPE,\r\n DEDUCTION_TYPE_VALUES,\r\n PAYROLL_STATUS,\r\n PAYROLL_STATUS_VALUES,\r\n TERMINATION_REASON,\r\n TERMINATION_REASON_VALUES,\r\n HRM_TRANSACTION_CATEGORIES,\r\n HRM_CATEGORY_VALUES,\r\n SALARY_BAND,\r\n SALARY_BAND_VALUES,\r\n ORG_ROLE,\r\n ORG_ROLE_VALUES,\r\n};\r\n\r\n","/**\r\n * @classytic/payroll - Query Builders\r\n *\r\n * Fluent API for building MongoDB queries\r\n * Type-safe, chainable, beautiful\r\n */\r\n\r\nimport mongoose, { Types } from 'mongoose';\r\nimport type {\r\n ObjectIdLike,\r\n EmployeeStatus,\r\n PayrollStatus,\r\n Department,\r\n EmploymentType,\r\n} from '../types.js';\r\n\r\n// ============================================================================\r\n// ObjectId Helpers\r\n// ============================================================================\r\n\r\n/**\r\n * Convert string or ObjectId to ObjectId\r\n */\r\nexport function toObjectId(id: ObjectIdLike): Types.ObjectId {\r\n if (id instanceof Types.ObjectId) return id;\r\n return new Types.ObjectId(id);\r\n}\r\n\r\n/**\r\n * Safely convert to ObjectId (returns null if invalid)\r\n */\r\nexport function safeToObjectId(id: unknown): Types.ObjectId | null {\r\n if (id instanceof Types.ObjectId) return id;\r\n if (typeof id === 'string' && Types.ObjectId.isValid(id)) {\r\n return new Types.ObjectId(id);\r\n }\r\n return null;\r\n}\r\n\r\n/**\r\n * Check if value is valid ObjectId\r\n */\r\nexport function isValidObjectId(value: unknown): boolean {\r\n if (value instanceof Types.ObjectId) return true;\r\n if (typeof value === 'string') return Types.ObjectId.isValid(value);\r\n return false;\r\n}\r\n\r\n// ============================================================================\r\n// Base Query Builder\r\n// ============================================================================\r\n\r\nexport class QueryBuilder<T extends Record<string, unknown> = Record<string, unknown>> {\r\n protected query: T;\r\n\r\n constructor(initialQuery: T = {} as T) {\r\n this.query = { ...initialQuery };\r\n }\r\n\r\n /**\r\n * Add where condition\r\n */\r\n where<K extends string>(field: K, value: unknown): this {\r\n (this.query as Record<string, unknown>)[field] = value;\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $in condition\r\n */\r\n whereIn<K extends string>(field: K, values: unknown[]): this {\r\n (this.query as Record<string, unknown>)[field] = { $in: values };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $nin condition\r\n */\r\n whereNotIn<K extends string>(field: K, values: unknown[]): this {\r\n (this.query as Record<string, unknown>)[field] = { $nin: values };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $gte condition\r\n */\r\n whereGte<K extends string>(field: K, value: unknown): this {\r\n const existing = (this.query as Record<string, Record<string, unknown>>)[field] || {};\r\n (this.query as Record<string, unknown>)[field] = { ...existing, $gte: value };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $lte condition\r\n */\r\n whereLte<K extends string>(field: K, value: unknown): this {\r\n const existing = (this.query as Record<string, Record<string, unknown>>)[field] || {};\r\n (this.query as Record<string, unknown>)[field] = { ...existing, $lte: value };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $gt condition\r\n */\r\n whereGt<K extends string>(field: K, value: unknown): this {\r\n const existing = (this.query as Record<string, Record<string, unknown>>)[field] || {};\r\n (this.query as Record<string, unknown>)[field] = { ...existing, $gt: value };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $lt condition\r\n */\r\n whereLt<K extends string>(field: K, value: unknown): this {\r\n const existing = (this.query as Record<string, Record<string, unknown>>)[field] || {};\r\n (this.query as Record<string, unknown>)[field] = { ...existing, $lt: value };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add between condition\r\n */\r\n whereBetween<K extends string>(field: K, start: unknown, end: unknown): this {\r\n (this.query as Record<string, unknown>)[field] = { $gte: start, $lte: end };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $exists condition\r\n */\r\n whereExists<K extends string>(field: K): this {\r\n (this.query as Record<string, unknown>)[field] = { $exists: true };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $exists: false condition\r\n */\r\n whereNotExists<K extends string>(field: K): this {\r\n (this.query as Record<string, unknown>)[field] = { $exists: false };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add $ne condition\r\n */\r\n whereNot<K extends string>(field: K, value: unknown): this {\r\n (this.query as Record<string, unknown>)[field] = { $ne: value };\r\n return this;\r\n }\r\n\r\n /**\r\n * Add regex condition\r\n */\r\n whereRegex<K extends string>(field: K, pattern: string, flags = 'i'): this {\r\n (this.query as Record<string, unknown>)[field] = { $regex: pattern, $options: flags };\r\n return this;\r\n }\r\n\r\n /**\r\n * Merge another query\r\n */\r\n merge(otherQuery: Record<string, unknown>): this {\r\n this.query = { ...this.query, ...otherQuery } as T;\r\n return this;\r\n }\r\n\r\n /**\r\n * Build and return the query\r\n */\r\n build(): T {\r\n return { ...this.query };\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Employee Query Builder\r\n// ============================================================================\r\n\r\nexport class EmployeeQueryBuilder extends QueryBuilder {\r\n /**\r\n * Filter by organization\r\n */\r\n forOrganization(organizationId: ObjectIdLike): this {\r\n return this.where('organizationId', toObjectId(organizationId));\r\n }\r\n\r\n /**\r\n * Filter by user\r\n */\r\n forUser(userId: ObjectIdLike): this {\r\n return this.where('userId', toObjectId(userId));\r\n }\r\n\r\n /**\r\n * Filter by status(es)\r\n */\r\n withStatus(...statuses: EmployeeStatus[]): this {\r\n if (statuses.length === 1) {\r\n return this.where('status', statuses[0]);\r\n }\r\n return this.whereIn('status', statuses);\r\n }\r\n\r\n /**\r\n * Filter active employees\r\n */\r\n active(): this {\r\n return this.withStatus('active');\r\n }\r\n\r\n /**\r\n * Filter employed employees (not terminated)\r\n */\r\n employed(): this {\r\n return this.whereIn('status', ['active', 'on_leave', 'suspended']);\r\n }\r\n\r\n /**\r\n * Filter terminated employees\r\n */\r\n terminated(): this {\r\n return this.withStatus('terminated');\r\n }\r\n\r\n /**\r\n * Filter by department\r\n */\r\n inDepartment(department: Department | string): this {\r\n return this.where('department', department);\r\n }\r\n\r\n /**\r\n * Filter by position\r\n */\r\n inPosition(position: string): this {\r\n return this.where('position', position);\r\n }\r\n\r\n /**\r\n * Filter by employment type\r\n */\r\n withEmploymentType(type: EmploymentType | string): this {\r\n return this.where('employmentType', type);\r\n }\r\n\r\n /**\r\n * Filter by hire date (after)\r\n */\r\n hiredAfter(date: Date): this {\r\n return this.whereGte('hireDate', date);\r\n }\r\n\r\n /**\r\n * Filter by hire date (before)\r\n */\r\n hiredBefore(date: Date): this {\r\n return this.whereLte('hireDate', date);\r\n }\r\n\r\n /**\r\n * Filter by minimum salary\r\n */\r\n withMinSalary(amount: number): this {\r\n return this.whereGte('compensation.netSalary', amount);\r\n }\r\n\r\n /**\r\n * Filter by maximum salary\r\n */\r\n withMaxSalary(amount: number): this {\r\n return this.whereLte('compensation.netSalary', amount);\r\n }\r\n\r\n /**\r\n * Filter by salary range\r\n */\r\n withSalaryRange(min: number, max: number): this {\r\n return this.whereBetween('compensation.netSalary', min, max);\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Payroll Query Builder\r\n// ============================================================================\r\n\r\nexport class PayrollQueryBuilder extends QueryBuilder {\r\n /**\r\n * Filter by organization\r\n */\r\n forOrganization(organizationId: ObjectIdLike): this {\r\n return this.where('organizationId', toObjectId(organizationId));\r\n }\r\n\r\n /**\r\n * Filter by employee\r\n */\r\n forEmployee(employeeId: ObjectIdLike): this {\r\n return this.where('employeeId', toObjectId(employeeId));\r\n }\r\n\r\n /**\r\n * Filter by period\r\n */\r\n forPeriod(month?: number, year?: number): this {\r\n if (month !== undefined) {\r\n this.where('period.month', month);\r\n }\r\n if (year !== undefined) {\r\n this.where('period.year', year);\r\n }\r\n return this;\r\n }\r\n\r\n /**\r\n * Filter by status(es)\r\n */\r\n withStatus(...statuses: PayrollStatus[]): this {\r\n if (statuses.length === 1) {\r\n return this.where('status', statuses[0]);\r\n }\r\n return this.whereIn('status', statuses);\r\n }\r\n\r\n /**\r\n * Filter paid records\r\n */\r\n paid(): this {\r\n return this.withStatus('paid');\r\n }\r\n\r\n /**\r\n * Filter pending records\r\n */\r\n pending(): this {\r\n return this.whereIn('status', ['pending', 'processing']);\r\n }\r\n\r\n /**\r\n * Filter by date range\r\n */\r\n inDateRange(start: Date, end: Date): this {\r\n return this.whereBetween('period.payDate', start, end);\r\n }\r\n\r\n /**\r\n * Filter exported records\r\n */\r\n exported(): this {\r\n return this.where('exported', true);\r\n }\r\n\r\n /**\r\n * Filter not exported records\r\n */\r\n notExported(): this {\r\n return this.where('exported', false);\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Factory Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Create employee query builder\r\n */\r\nexport function employee(): EmployeeQueryBuilder {\r\n return new EmployeeQueryBuilder();\r\n}\r\n\r\n/**\r\n * Create payroll query builder\r\n */\r\nexport function payroll(): PayrollQueryBuilder {\r\n return new PayrollQueryBuilder();\r\n}\r\n\r\n/**\r\n * Create generic query builder\r\n */\r\nexport function createQueryBuilder<T extends Record<string, unknown> = Record<string, unknown>>(\r\n initialQuery?: T\r\n): QueryBuilder<T> {\r\n return new QueryBuilder(initialQuery);\r\n}\r\n\r\n// ============================================================================\r\n// Convenience Query Builders\r\n// ============================================================================\r\n\r\n/**\r\n * Build employee query from options\r\n */\r\nexport function buildEmployeeQuery(options: {\r\n organizationId: ObjectIdLike;\r\n userId?: ObjectIdLike;\r\n statuses?: EmployeeStatus[];\r\n department?: Department | string;\r\n employmentType?: EmploymentType | string;\r\n}): Record<string, unknown> {\r\n const builder = employee().forOrganization(options.organizationId);\r\n\r\n if (options.userId) {\r\n builder.forUser(options.userId);\r\n }\r\n if (options.statuses) {\r\n builder.withStatus(...options.statuses);\r\n }\r\n if (options.department) {\r\n builder.inDepartment(options.department);\r\n }\r\n if (options.employmentType) {\r\n builder.withEmploymentType(options.employmentType);\r\n }\r\n\r\n return builder.build();\r\n}\r\n\r\n/**\r\n * Build payroll query from options\r\n */\r\nexport function buildPayrollQuery(options: {\r\n employeeId?: ObjectIdLike;\r\n organizationId?: ObjectIdLike;\r\n month?: number;\r\n year?: number;\r\n statuses?: PayrollStatus[];\r\n}): Record<string, unknown> {\r\n const builder = payroll();\r\n\r\n if (options.organizationId) {\r\n builder.forOrganization(options.organizationId);\r\n }\r\n if (options.employeeId) {\r\n builder.forEmployee(options.employeeId);\r\n }\r\n if (options.month || options.year) {\r\n builder.forPeriod(options.month, options.year);\r\n }\r\n if (options.statuses) {\r\n builder.withStatus(...options.statuses);\r\n }\r\n\r\n return builder.build();\r\n}\r\n\r\n// ============================================================================\r\n// Aggregation Pipeline Helpers\r\n// ============================================================================\r\n\r\n/**\r\n * Build aggregation pipeline from stages\r\n */\r\nexport function buildAggregationPipeline(\r\n ...stages: Array<Record<string, unknown> | undefined | null>\r\n): Record<string, unknown>[] {\r\n return stages.filter((stage): stage is Record<string, unknown> => !!stage);\r\n}\r\n\r\n/**\r\n * Match stage\r\n */\r\nexport function matchStage(query: Record<string, unknown>): Record<string, unknown> {\r\n return { $match: query };\r\n}\r\n\r\n/**\r\n * Group stage\r\n */\r\nexport function groupStage(\r\n groupBy: string | null,\r\n aggregations: Record<string, unknown>\r\n): Record<string, unknown> {\r\n return {\r\n $group: {\r\n _id: groupBy,\r\n ...aggregations,\r\n },\r\n };\r\n}\r\n\r\n/**\r\n * Sort stage\r\n */\r\nexport function sortStage(sortBy: Record<string, 1 | -1>): Record<string, unknown> {\r\n return { $sort: sortBy };\r\n}\r\n\r\n/**\r\n * Limit stage\r\n */\r\nexport function limitStage(limit: number): Record<string, unknown> {\r\n return { $limit: limit };\r\n}\r\n\r\n/**\r\n * Skip stage\r\n */\r\nexport function skipStage(skip: number): Record<string, unknown> {\r\n return { $skip: skip };\r\n}\r\n\r\n/**\r\n * Project stage\r\n */\r\nexport function projectStage(fields: Record<string, unknown>): Record<string, unknown> {\r\n return { $project: fields };\r\n}\r\n\r\n/**\r\n * Lookup stage\r\n */\r\nexport function lookupStage(options: {\r\n from: string;\r\n localField: string;\r\n foreignField: string;\r\n as: string;\r\n}): Record<string, unknown> {\r\n return { $lookup: options };\r\n}\r\n\r\n/**\r\n * Unwind stage\r\n */\r\nexport function unwindStage(\r\n path: string,\r\n options: { preserveNullAndEmptyArrays?: boolean } = {}\r\n): Record<string, unknown> {\r\n return { $unwind: { path, ...options } };\r\n}\r\n\r\n// ============================================================================\r\n// Default Export\r\n// ============================================================================\r\n\r\nexport default {\r\n toObjectId,\r\n safeToObjectId,\r\n isValidObjectId,\r\n QueryBuilder,\r\n EmployeeQueryBuilder,\r\n PayrollQueryBuilder,\r\n employee,\r\n payroll,\r\n createQueryBuilder,\r\n buildEmployeeQuery,\r\n buildPayrollQuery,\r\n buildAggregationPipeline,\r\n matchStage,\r\n groupStage,\r\n sortStage,\r\n limitStage,\r\n skipStage,\r\n projectStage,\r\n lookupStage,\r\n unwindStage,\r\n};\r\n\r\n","/**\r\n * @classytic/payroll - Calculation Utilities\r\n *\r\n * Pure, functional, composable financial calculations\r\n * No side effects, highly testable\r\n */\r\n\r\nimport type {\r\n Allowance,\r\n Deduction,\r\n Compensation,\r\n TaxCalculationResult,\r\n CompensationBreakdownResult,\r\n ProRatingResult,\r\n PayPeriodInfo,\r\n} from '../types.js';\r\nimport { diffInDays } from './date.js';\r\n\r\n// ============================================================================\r\n// Basic Math Operations\r\n// ============================================================================\r\n\r\n/**\r\n * Sum array of numbers\r\n */\r\nexport function sum(numbers: number[]): number {\r\n return numbers.reduce((total, n) => total + n, 0);\r\n}\r\n\r\n/**\r\n * Sum by property\r\n */\r\nexport function sumBy<T>(items: T[], getter: (item: T) => number): number {\r\n return items.reduce((total, item) => total + getter(item), 0);\r\n}\r\n\r\n/**\r\n * Sum allowances\r\n */\r\nexport function sumAllowances(allowances: Array<{ amount: number }>): number {\r\n return sumBy(allowances, (a) => a.amount);\r\n}\r\n\r\n/**\r\n * Sum deductions\r\n */\r\nexport function sumDeductions(deductions: Array<{ amount: number }>): number {\r\n return sumBy(deductions, (d) => d.amount);\r\n}\r\n\r\n/**\r\n * Apply percentage to amount\r\n */\r\nexport function applyPercentage(amount: number, percentage: number): number {\r\n return Math.round(amount * (percentage / 100));\r\n}\r\n\r\n/**\r\n * Calculate percentage of total\r\n */\r\nexport function calculatePercentage(part: number, total: number): number {\r\n return total > 0 ? Math.round((part / total) * 100) : 0;\r\n}\r\n\r\n/**\r\n * Round to decimal places\r\n */\r\nexport function roundTo(value: number, decimals = 2): number {\r\n const factor = Math.pow(10, decimals);\r\n return Math.round(value * factor) / factor;\r\n}\r\n\r\n// ============================================================================\r\n// Salary Calculations\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate gross salary from base and allowances\r\n */\r\nexport function calculateGross(\r\n baseAmount: number,\r\n allowances: Array<{ amount: number }>\r\n): number {\r\n return baseAmount + sumAllowances(allowances);\r\n}\r\n\r\n/**\r\n * Calculate net salary from gross and deductions\r\n */\r\nexport function calculateNet(\r\n gross: number,\r\n deductions: Array<{ amount: number }>\r\n): number {\r\n return Math.max(0, gross - sumDeductions(deductions));\r\n}\r\n\r\n/**\r\n * Calculate total compensation\r\n */\r\nexport function calculateTotalCompensation(\r\n baseAmount: number,\r\n allowances: Array<{ amount: number }>,\r\n deductions: Array<{ amount: number }>\r\n): { gross: number; net: number; deductions: number } {\r\n const gross = calculateGross(baseAmount, allowances);\r\n const totalDeductions = sumDeductions(deductions);\r\n const net = calculateNet(gross, deductions);\r\n return { gross, net, deductions: totalDeductions };\r\n}\r\n\r\n// ============================================================================\r\n// Allowance & Deduction Calculation\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate allowance amount (handles percentage-based)\r\n */\r\nexport function calculateAllowanceAmount(\r\n allowance: Pick<Allowance, 'amount' | 'isPercentage' | 'value'>,\r\n baseAmount: number\r\n): number {\r\n if (allowance.isPercentage && allowance.value !== undefined) {\r\n return applyPercentage(baseAmount, allowance.value);\r\n }\r\n return allowance.amount;\r\n}\r\n\r\n/**\r\n * Calculate deduction amount (handles percentage-based)\r\n */\r\nexport function calculateDeductionAmount(\r\n deduction: Pick<Deduction, 'amount' | 'isPercentage' | 'value'>,\r\n baseAmount: number\r\n): number {\r\n if (deduction.isPercentage && deduction.value !== undefined) {\r\n return applyPercentage(baseAmount, deduction.value);\r\n }\r\n return deduction.amount;\r\n}\r\n\r\n/**\r\n * Calculate all allowances with their actual amounts\r\n */\r\nexport function calculateAllowances(\r\n allowances: Allowance[],\r\n baseAmount: number\r\n): Array<Allowance & { calculatedAmount: number }> {\r\n return allowances.map((allowance) => ({\r\n ...allowance,\r\n calculatedAmount: calculateAllowanceAmount(allowance, baseAmount),\r\n }));\r\n}\r\n\r\n/**\r\n * Calculate all deductions with their actual amounts\r\n */\r\nexport function calculateDeductions(\r\n deductions: Deduction[],\r\n baseAmount: number\r\n): Array<Deduction & { calculatedAmount: number }> {\r\n return deductions.map((deduction) => ({\r\n ...deduction,\r\n calculatedAmount: calculateDeductionAmount(deduction, baseAmount),\r\n }));\r\n}\r\n\r\n// ============================================================================\r\n// Compensation Breakdown\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate full compensation breakdown\r\n */\r\nexport function calculateCompensationBreakdown(\r\n compensation: Pick<Compensation, 'baseAmount' | 'allowances' | 'deductions'>\r\n): CompensationBreakdownResult {\r\n const { baseAmount, allowances = [], deductions = [] } = compensation;\r\n\r\n const calculatedAllowances = calculateAllowances(allowances, baseAmount);\r\n const calculatedDeductions = calculateDeductions(deductions, baseAmount);\r\n\r\n const grossAmount =\r\n baseAmount + sumBy(calculatedAllowances, (a) => a.calculatedAmount);\r\n const netAmount =\r\n grossAmount - sumBy(calculatedDeductions, (d) => d.calculatedAmount);\r\n\r\n return {\r\n baseAmount,\r\n allowances: calculatedAllowances,\r\n deductions: calculatedDeductions,\r\n grossAmount,\r\n netAmount: Math.max(0, netAmount),\r\n };\r\n}\r\n\r\n// ============================================================================\r\n// Pro-Rating Calculations\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate pro-rating for mid-month hires\r\n */\r\nexport function calculateProRating(\r\n hireDate: Date,\r\n periodStart: Date,\r\n periodEnd: Date\r\n): ProRatingResult {\r\n const totalDays = diffInDays(periodStart, periodEnd) + 1;\r\n\r\n // Hired before period start - no pro-rating\r\n if (hireDate <= periodStart) {\r\n return {\r\n isProRated: false,\r\n totalDays,\r\n actualDays: totalDays,\r\n ratio: 1,\r\n };\r\n }\r\n\r\n // Hired during the period - pro-rate\r\n if (hireDate > periodStart && hireDate <= periodEnd) {\r\n const actualDays = diffInDays(hireDate, periodEnd) + 1;\r\n const ratio = actualDays / totalDays;\r\n\r\n return {\r\n isProRated: true,\r\n totalDays,\r\n actualDays,\r\n ratio,\r\n };\r\n }\r\n\r\n // Hired after period - no work days\r\n return {\r\n isProRated: false,\r\n totalDays,\r\n actualDays: 0,\r\n ratio: 0,\r\n };\r\n}\r\n\r\n/**\r\n * Apply pro-rating to an amount\r\n */\r\nexport function applyProRating(\r\n amount: number,\r\n proRating: ProRatingResult\r\n): number {\r\n return Math.round(amount * proRating.ratio);\r\n}\r\n\r\n/**\r\n * Calculate pro-rated salary\r\n */\r\nexport function calculateProRatedSalary(\r\n baseAmount: number,\r\n hireDate: Date,\r\n period: PayPeriodInfo\r\n): { amount: number; proRating: ProRatingResult } {\r\n const proRating = calculateProRating(hireDate, period.startDate, period.endDate);\r\n const amount = applyProRating(baseAmount, proRating);\r\n return { amount, proRating };\r\n}\r\n\r\n// ============================================================================\r\n// Tax Calculations\r\n// ============================================================================\r\n\r\n/**\r\n * Apply tax brackets to calculate tax\r\n */\r\nexport function applyTaxBrackets(\r\n amount: number,\r\n brackets: Array<{ min: number; max: number; rate: number }>\r\n): number {\r\n let tax = 0;\r\n\r\n for (const bracket of brackets) {\r\n if (amount > bracket.min) {\r\n const taxableAmount = Math.min(amount, bracket.max) - bracket.min;\r\n tax += taxableAmount * bracket.rate;\r\n }\r\n }\r\n\r\n return Math.round(tax);\r\n}\r\n\r\n/**\r\n * Calculate tax with result\r\n */\r\nexport function calculateTax(\r\n amount: number,\r\n brackets: Array<{ min: number; max: number; rate: number }>\r\n): TaxCalculationResult {\r\n const tax = applyTaxBrackets(amount, brackets);\r\n return {\r\n gross: amount,\r\n tax,\r\n net: amount - tax,\r\n };\r\n}\r\n\r\n// ============================================================================\r\n// Functional Composition\r\n// ============================================================================\r\n\r\n/**\r\n * Pipe functions left-to-right\r\n * pipe(f, g, h)(x) === h(g(f(x)))\r\n */\r\nexport function pipe<T>(...fns: Array<(value: T) => T>): (value: T) => T {\r\n return (value: T) => fns.reduce((acc, fn) => fn(acc), value);\r\n}\r\n\r\n/**\r\n * Compose functions right-to-left\r\n * compose(f, g, h)(x) === f(g(h(x)))\r\n */\r\nexport function compose<T>(...fns: Array<(value: T) => T>): (value: T) => T {\r\n return (value: T) => fns.reduceRight((acc, fn) => fn(acc), value);\r\n}\r\n\r\n/**\r\n * Create an allowance calculator factory\r\n */\r\nexport function createAllowanceCalculator(\r\n allowances: Allowance[]\r\n): (baseSalary: number) => Array<Allowance & { calculatedAmount: number }> {\r\n return (baseSalary: number) => calculateAllowances(allowances, baseSalary);\r\n}\r\n\r\n/**\r\n * Create a deduction calculator factory\r\n */\r\nexport function createDeductionCalculator(\r\n deductions: Deduction[]\r\n): (baseSalary: number) => Array<Deduction & { calculatedAmount: number }> {\r\n return (baseSalary: number) => calculateDeductions(deductions, baseSalary);\r\n}\r\n\r\n// ============================================================================\r\n// Overtime Calculations\r\n// ============================================================================\r\n\r\n/**\r\n * Calculate overtime pay\r\n */\r\nexport function calculateOvertime(\r\n hourlyRate: number,\r\n overtimeHours: number,\r\n multiplier = 1.5\r\n): number {\r\n return Math.round(hourlyRate * overtimeHours * multiplier);\r\n}\r\n\r\n/**\r\n * Calculate hourly rate from monthly salary\r\n */\r\nexport function calculateHourlyRate(\r\n monthlySalary: number,\r\n hoursPerMonth = 176 // 44 hours/week * 4 weeks\r\n): number {\r\n return Math.round(monthlySalary / hoursPerMonth);\r\n}\r\n\r\n/**\r\n * Calculate daily rate from monthly salary\r\n */\r\nexport function calculateDailyRate(\r\n monthlySalary: number,\r\n daysPerMonth = 22\r\n): number {\r\n return Math.round(monthlySalary / daysPerMonth);\r\n}\r\n\r\n// ============================================================================\r\n// Default Export\r\n// ============================================================================\r\n\r\nexport default {\r\n sum,\r\n sumBy,\r\n sumAllowances,\r\n sumDeductions,\r\n applyPercentage,\r\n calculatePercentage,\r\n roundTo,\r\n calculateGross,\r\n calculateNet,\r\n calculateTotalCompensation,\r\n calculateAllowanceAmount,\r\n calculateDeductionAmount,\r\n calculateAllowances,\r\n calculateDeductions,\r\n calculateCompensationBreakdown,\r\n calculateProRating,\r\n applyProRating,\r\n calculateProRatedSalary,\r\n applyTaxBrackets,\r\n calculateTax,\r\n pipe,\r\n compose,\r\n createAllowanceCalculator,\r\n createDeductionCalculator,\r\n calculateOvertime,\r\n calculateHourlyRate,\r\n calculateDailyRate,\r\n};\r\n\r\n","/**\r\n * @classytic/payroll - Error Handling\r\n *\r\n * Custom error classes with error codes and HTTP status\r\n */\r\n\r\nimport type { ErrorCode, HttpError } from '../types.js';\r\n\r\n// ============================================================================\r\n// Base Error Class\r\n// ============================================================================\r\n\r\nexport class PayrollError extends Error implements HttpError {\r\n readonly code: ErrorCode;\r\n readonly status: number;\r\n readonly context?: Record<string, unknown>;\r\n readonly timestamp: Date;\r\n\r\n constructor(\r\n message: string,\r\n code: ErrorCode = 'PAYROLL_ERROR',\r\n status = 500,\r\n context?: Record<string, unknown>\r\n ) {\r\n super(message);\r\n this.name = 'PayrollError';\r\n this.code = code;\r\n this.status = status;\r\n this.context = context;\r\n this.timestamp = new Date();\r\n\r\n // Maintains proper stack trace for where error was thrown\r\n if (Error.captureStackTrace) {\r\n Error.captureStackTrace(this, PayrollError);\r\n }\r\n }\r\n\r\n toJSON(): Record<string, unknown> {\r\n return {\r\n name: this.name,\r\n code: this.code,\r\n message: this.message,\r\n status: this.status,\r\n context: this.context,\r\n timestamp: this.timestamp.toISOString(),\r\n };\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Specific Error Classes\r\n// ============================================================================\r\n\r\n/**\r\n * Not initialized error\r\n */\r\nexport class NotInitializedError extends PayrollError {\r\n constructor(message = 'Payroll not initialized. Call Payroll.initialize() first.') {\r\n super(message, 'NOT_INITIALIZED', 500);\r\n this.name = 'NotInitializedError';\r\n }\r\n}\r\n\r\n/**\r\n * Employee not found error\r\n */\r\nexport class EmployeeNotFoundError extends PayrollError {\r\n constructor(employeeId?: string, context?: Record<string, unknown>) {\r\n super(\r\n employeeId ? `Employee not found: ${employeeId}` : 'Employee not found',\r\n 'EMPLOYEE_NOT_FOUND',\r\n 404,\r\n context\r\n );\r\n this.name = 'EmployeeNotFoundError';\r\n }\r\n}\r\n\r\n/**\r\n * Invalid employee error\r\n */\r\nexport class InvalidEmployeeError extends PayrollError {\r\n constructor(message: string, context?: Record<string, unknown>) {\r\n super(message, 'INVALID_EMPLOYEE', 400, context);\r\n this.name = 'InvalidEmployeeError';\r\n }\r\n}\r\n\r\n/**\r\n * Duplicate payroll error\r\n */\r\nexport class DuplicatePayrollError extends PayrollError {\r\n constructor(\r\n employeeId: string,\r\n month: number,\r\n year: number,\r\n context?: Record<string, unknown>\r\n ) {\r\n super(\r\n `Payroll already processed for employee ${employeeId} in ${month}/${year}`,\r\n 'DUPLICATE_PAYROLL',\r\n 409,\r\n { employeeId, month, year, ...context }\r\n );\r\n this.name = 'DuplicatePayrollError';\r\n }\r\n}\r\n\r\n/**\r\n * Validation error\r\n */\r\nexport class ValidationError extends PayrollError {\r\n readonly errors: string[];\r\n\r\n constructor(errors: string | string[], context?: Record<string, unknown>) {\r\n const errorArray = Array.isArray(errors) ? errors : [errors];\r\n super(errorArray.join(', '), 'VALIDATION_ERROR', 400, context);\r\n this.name = 'ValidationError';\r\n this.errors = errorArray;\r\n }\r\n}\r\n\r\n/**\r\n * Employee terminated error\r\n */\r\nexport class EmployeeTerminatedError extends PayrollError {\r\n constructor(employeeId?: string, context?: Record<string, unknown>) {\r\n super(\r\n employeeId\r\n ? `Cannot perform operation on terminated employee: ${employeeId}`\r\n : 'Cannot perform operation on terminated employee',\r\n 'EMPLOYEE_TERMINATED',\r\n 400,\r\n context\r\n );\r\n this.name = 'EmployeeTerminatedError';\r\n }\r\n}\r\n\r\n/**\r\n * Already processed error\r\n */\r\nexport class AlreadyProcessedError extends PayrollError {\r\n constructor(message: string, context?: Record<string, unknown>) {\r\n super(message, 'ALREADY_PROCESSED', 409, context);\r\n this.name = 'AlreadyProcessedError';\r\n }\r\n}\r\n\r\n/**\r\n * Not eligible error\r\n */\r\nexport class NotEligibleError extends PayrollError {\r\n constructor(message: string, context?: Record<string, unknown>) {\r\n super(message, 'NOT_ELIGIBLE', 400, context);\r\n this.name = 'NotEligibleError';\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Error Factory\r\n// ============================================================================\r\n\r\n/**\r\n * Create error from code\r\n */\r\nexport function createError(\r\n code: ErrorCode,\r\n message: string,\r\n context?: Record<string, unknown>\r\n): PayrollError {\r\n const statusMap: Record<ErrorCode, number> = {\r\n PAYROLL_ERROR: 500,\r\n NOT_INITIALIZED: 500,\r\n EMPLOYEE_NOT_FOUND: 404,\r\n INVALID_EMPLOYEE: 400,\r\n DUPLICATE_PAYROLL: 409,\r\n VALIDATION_ERROR: 400,\r\n EMPLOYEE_TERMINATED: 400,\r\n ALREADY_PROCESSED: 409,\r\n NOT_ELIGIBLE: 400,\r\n };\r\n\r\n return new PayrollError(message, code, statusMap[code] || 500, context);\r\n}\r\n\r\n// ============================================================================\r\n// Type Guards\r\n// ============================================================================\r\n\r\n/**\r\n * Check if error is PayrollError\r\n */\r\nexport function isPayrollError(error: unknown): error is PayrollError {\r\n return error instanceof PayrollError;\r\n}\r\n\r\n/**\r\n * Check if error has specific code\r\n */\r\nexport function isErrorCode(error: unknown, code: ErrorCode): boolean {\r\n return isPayrollError(error) && error.code === code;\r\n}\r\n\r\n/**\r\n * Extract error info for logging\r\n */\r\nexport function extractErrorInfo(error: unknown): {\r\n message: string;\r\n code?: ErrorCode;\r\n status?: number;\r\n context?: Record<string, unknown>;\r\n} {\r\n if (isPayrollError(error)) {\r\n return {\r\n message: error.message,\r\n code: error.code,\r\n status: error.status,\r\n context: error.context,\r\n };\r\n }\r\n\r\n if (error instanceof Error) {\r\n return { message: error.message };\r\n }\r\n\r\n return { message: String(error) };\r\n}\r\n\r\n// ============================================================================\r\n// Error Handling Utilities\r\n// ============================================================================\r\n\r\n/**\r\n * Wrap async function with error handling\r\n */\r\nexport function withErrorHandling<T extends (...args: unknown[]) => Promise<unknown>>(\r\n fn: T,\r\n errorHandler?: (error: unknown) => void\r\n): T {\r\n return (async (...args: Parameters<T>) => {\r\n try {\r\n return await fn(...args);\r\n } catch (error) {\r\n if (errorHandler) {\r\n errorHandler(error);\r\n }\r\n throw error;\r\n }\r\n }) as T;\r\n}\r\n\r\n/**\r\n * Convert unknown error to PayrollError\r\n */\r\nexport function toPayrollError(error: unknown): PayrollError {\r\n if (isPayrollError(error)) {\r\n return error;\r\n }\r\n\r\n if (error instanceof Error) {\r\n return new PayrollError(error.message);\r\n }\r\n\r\n return new PayrollError(String(error));\r\n}\r\n\r\n// ============================================================================\r\n// Legacy Alias\r\n// ============================================================================\r\n\r\nexport { PayrollError as HRMError };\r\n\r\n","/**\r\n * @classytic/payroll - Main Payroll Class\r\n *\r\n * Clean, Stripe-like API for payroll management\r\n * Builder pattern for configuration\r\n */\r\n\r\nimport mongoose, { Model, ClientSession } from 'mongoose';\r\nimport type {\r\n PayrollInitConfig,\r\n HRMConfig,\r\n SingleTenantConfig,\r\n Logger,\r\n ObjectIdLike,\r\n EmployeeDocument,\r\n PayrollRecordDocument,\r\n HireEmployeeParams,\r\n UpdateEmploymentParams,\r\n TerminateEmployeeParams,\r\n ReHireEmployeeParams,\r\n ListEmployeesParams,\r\n UpdateSalaryParams,\r\n AddAllowanceParams,\r\n RemoveAllowanceParams,\r\n AddDeductionParams,\r\n RemoveDeductionParams,\r\n UpdateBankDetailsParams,\r\n ProcessSalaryParams,\r\n ProcessBulkPayrollParams,\r\n PayrollHistoryParams,\r\n PayrollSummaryParams,\r\n ExportPayrollParams,\r\n ProcessSalaryResult,\r\n BulkPayrollResult,\r\n PayrollSummaryResult,\r\n OperationContext,\r\n DeepPartial,\r\n Allowance,\r\n Deduction,\r\n TaxBracket,\r\n} from './types.js';\r\nimport { Container, type ModelsContainer, initializeContainer } from './core/container.js';\r\nimport { EventBus, createEventBus, type PayrollEventMap } from './core/events.js';\r\nimport { PluginManager, type PayrollPluginDefinition, type PluginContext } from './core/plugin.js';\r\nimport { EmployeeFactory } from './factories/employee.factory.js';\r\nimport { HRM_CONFIG, mergeConfig, TAX_BRACKETS } from './config.js';\r\nimport { HRM_TRANSACTION_CATEGORIES } from './enums.js';\r\nimport { employee as employeeQuery, payroll as payrollQuery, toObjectId } from './utils/query-builders.js';\r\nimport { getPayPeriod, diffInDays, addMonths, getWorkingDaysInMonth } from './utils/date.js';\r\nimport { calculateGross, calculateNet, sumAllowances, sumDeductions, applyTaxBrackets } from './utils/calculation.js';\r\nimport { logger as defaultLogger, getLogger, setLogger } from './utils/logger.js';\r\nimport { NotInitializedError, EmployeeNotFoundError, DuplicatePayrollError, NotEligibleError, EmployeeTerminatedError, ValidationError } from './errors/index.js';\r\n\r\n// ============================================================================\r\n// Helper: Check plugin methods exist\r\n// ============================================================================\r\n\r\nfunction hasPluginMethod(obj: unknown, method: string): boolean {\r\n return typeof obj === 'object' && obj !== null && typeof (obj as Record<string, unknown>)[method] === 'function';\r\n}\r\n\r\nfunction assertPluginMethod(obj: unknown, method: string, context: string): void {\r\n if (!hasPluginMethod(obj, method)) {\r\n throw new Error(\r\n `Method '${method}' not found on employee. Did you forget to apply employeePlugin to your Employee schema? ` +\r\n `Context: ${context}`\r\n );\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Helper: Check if date is within range\r\n// ============================================================================\r\n\r\nfunction isEffectiveForPeriod(\r\n item: { effectiveFrom?: Date | null; effectiveTo?: Date | null },\r\n periodStart: Date,\r\n periodEnd: Date\r\n): boolean {\r\n const effectiveFrom = item.effectiveFrom ? new Date(item.effectiveFrom) : new Date(0);\r\n const effectiveTo = item.effectiveTo ? new Date(item.effectiveTo) : new Date('2099-12-31');\r\n \r\n // Item is effective if its range overlaps with the period\r\n return effectiveFrom <= periodEnd && effectiveTo >= periodStart;\r\n}\r\n\r\n// ============================================================================\r\n// Payroll Class\r\n// ============================================================================\r\n\r\nexport class Payroll {\r\n private _container: Container;\r\n private _events: EventBus;\r\n private _plugins: PluginManager | null = null;\r\n private _initialized = false;\r\n\r\n constructor() {\r\n this._container = Container.getInstance();\r\n this._events = createEventBus();\r\n }\r\n\r\n // ========================================\r\n // Initialization\r\n // ========================================\r\n\r\n /**\r\n * Initialize Payroll with models and configuration\r\n */\r\n initialize(config: PayrollInitConfig): this {\r\n const { EmployeeModel, PayrollRecordModel, TransactionModel, AttendanceModel, singleTenant, logger: customLogger, config: customConfig } = config;\r\n\r\n if (!EmployeeModel || !PayrollRecordModel || !TransactionModel) {\r\n throw new Error('EmployeeModel, PayrollRecordModel, and TransactionModel are required');\r\n }\r\n\r\n if (customLogger) {\r\n setLogger(customLogger);\r\n }\r\n\r\n initializeContainer({\r\n models: {\r\n EmployeeModel,\r\n PayrollRecordModel,\r\n TransactionModel,\r\n AttendanceModel: AttendanceModel ?? null,\r\n },\r\n config: customConfig as Partial<HRMConfig>,\r\n singleTenant: singleTenant ?? null,\r\n logger: customLogger,\r\n });\r\n\r\n // Setup plugin manager\r\n const pluginContext: PluginContext = {\r\n payroll: this as unknown as import('./types.js').PayrollInstance,\r\n events: this._events,\r\n logger: getLogger(),\r\n getConfig: <T = unknown>(key: string): T | undefined => {\r\n const config = this._container.getConfig();\r\n return (config as unknown as Record<string, T>)[key];\r\n },\r\n addHook: (event, handler) => this._events.on(event, handler),\r\n };\r\n this._plugins = new PluginManager(pluginContext);\r\n\r\n this._initialized = true;\r\n\r\n getLogger().info('Payroll initialized', {\r\n hasAttendanceIntegration: !!AttendanceModel,\r\n isSingleTenant: !!singleTenant,\r\n });\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Check if initialized\r\n */\r\n isInitialized(): boolean {\r\n return this._initialized;\r\n }\r\n\r\n /**\r\n * Ensure initialized\r\n */\r\n private ensureInitialized(): void {\r\n if (!this._initialized) {\r\n throw new NotInitializedError();\r\n }\r\n }\r\n\r\n /**\r\n * Get models\r\n */\r\n private get models(): ModelsContainer {\r\n this.ensureInitialized();\r\n return this._container.getModels();\r\n }\r\n\r\n /**\r\n * Get config\r\n */\r\n private get config(): HRMConfig {\r\n return this._container.getConfig();\r\n }\r\n\r\n // ========================================\r\n // Plugin System\r\n // ========================================\r\n\r\n /**\r\n * Register a plugin\r\n */\r\n async use(plugin: PayrollPluginDefinition): Promise<this> {\r\n this.ensureInitialized();\r\n await this._plugins!.register(plugin);\r\n return this;\r\n }\r\n\r\n /**\r\n * Subscribe to events\r\n */\r\n on<K extends keyof PayrollEventMap>(\r\n event: K,\r\n handler: (payload: PayrollEventMap[K]) => void | Promise<void>\r\n ): () => void {\r\n return this._events.on(event, handler);\r\n }\r\n\r\n // ========================================\r\n // Employment Lifecycle\r\n // ========================================\r\n\r\n /**\r\n * Hire a new employee\r\n */\r\n async hire(params: HireEmployeeParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { userId, employment, compensation, bankDetails, context } = params;\r\n const session = context?.session;\r\n\r\n // Auto-inject organizationId in single-tenant mode\r\n const organizationId = params.organizationId ?? this._container.getOrganizationId();\r\n if (!organizationId) {\r\n throw new Error('organizationId is required (or configure single-tenant mode)');\r\n }\r\n\r\n // Check for existing active employee\r\n const existingQuery = employeeQuery()\r\n .forUser(userId)\r\n .forOrganization(organizationId)\r\n .employed()\r\n .build();\r\n\r\n let existing = this.models.EmployeeModel.findOne(existingQuery);\r\n if (session) existing = existing.session(session);\r\n \r\n if (await existing) {\r\n throw new Error('User is already an active employee in this organization');\r\n }\r\n\r\n const employeeData = EmployeeFactory.create({\r\n userId,\r\n organizationId,\r\n employment,\r\n compensation: {\r\n ...compensation,\r\n currency: compensation.currency || this.config.payroll.defaultCurrency,\r\n },\r\n bankDetails,\r\n });\r\n\r\n const [employee] = await this.models.EmployeeModel.create([employeeData], { session });\r\n\r\n // Emit event\r\n this._events.emitSync('employee:hired', {\r\n employee: {\r\n id: employee._id,\r\n employeeId: employee.employeeId,\r\n position: employee.position,\r\n department: employee.department,\r\n },\r\n organizationId: employee.organizationId,\r\n context,\r\n });\r\n\r\n getLogger().info('Employee hired', {\r\n employeeId: employee.employeeId,\r\n organizationId: organizationId.toString(),\r\n position: employment.position,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Update employment details\r\n * NOTE: Status changes to 'terminated' must use terminate() method\r\n */\r\n async updateEmployment(params: UpdateEmploymentParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, updates, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n if (employee.status === 'terminated') {\r\n throw new EmployeeTerminatedError(employee.employeeId);\r\n }\r\n\r\n // IMPORTANT: Block direct status change to 'terminated' - must use terminate()\r\n if (updates.status === 'terminated') {\r\n throw new ValidationError(\r\n 'Cannot set status to terminated directly. Use the terminate() method instead to ensure proper history tracking.',\r\n { field: 'status' }\r\n );\r\n }\r\n\r\n const allowedUpdates = ['department', 'position', 'employmentType', 'status', 'workSchedule'];\r\n for (const [key, value] of Object.entries(updates)) {\r\n if (allowedUpdates.includes(key)) {\r\n (employee as Record<string, unknown>)[key] = value;\r\n }\r\n }\r\n\r\n await employee.save({ session });\r\n\r\n getLogger().info('Employee updated', {\r\n employeeId: employee.employeeId,\r\n updates: Object.keys(updates),\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Terminate employee\r\n */\r\n async terminate(params: TerminateEmployeeParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, terminationDate = new Date(), reason = 'resignation', notes, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n // Check plugin method exists\r\n assertPluginMethod(employee, 'terminate', 'terminate()');\r\n\r\n (employee as unknown as { terminate: (reason: string, date: Date) => void }).terminate(reason, terminationDate);\r\n\r\n if (notes) {\r\n employee.notes = (employee.notes || '') + `\\nTermination: ${notes}`;\r\n }\r\n\r\n await employee.save({ session });\r\n\r\n // Emit event\r\n this._events.emitSync('employee:terminated', {\r\n employee: {\r\n id: employee._id,\r\n employeeId: employee.employeeId,\r\n },\r\n terminationDate,\r\n reason,\r\n organizationId: employee.organizationId,\r\n context,\r\n });\r\n\r\n getLogger().info('Employee terminated', {\r\n employeeId: employee.employeeId,\r\n reason,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Re-hire terminated employee\r\n */\r\n async reHire(params: ReHireEmployeeParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, hireDate = new Date(), position, department, compensation, context } = params;\r\n const session = context?.session;\r\n\r\n if (!this.config.employment.allowReHiring) {\r\n throw new Error('Re-hiring is not enabled');\r\n }\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n // Check plugin method exists\r\n assertPluginMethod(employee, 'reHire', 'reHire()');\r\n\r\n (employee as unknown as { reHire: (date: Date, position?: string, department?: string) => void }).reHire(hireDate, position, department);\r\n\r\n if (compensation) {\r\n employee.compensation = { ...employee.compensation, ...compensation } as typeof employee.compensation;\r\n }\r\n\r\n await employee.save({ session });\r\n\r\n // Emit event\r\n this._events.emitSync('employee:rehired', {\r\n employee: {\r\n id: employee._id,\r\n employeeId: employee.employeeId,\r\n position: employee.position,\r\n },\r\n organizationId: employee.organizationId,\r\n context,\r\n });\r\n\r\n getLogger().info('Employee re-hired', {\r\n employeeId: employee.employeeId,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Get employee by ID\r\n */\r\n async getEmployee(params: {\r\n employeeId: ObjectIdLike;\r\n populateUser?: boolean;\r\n session?: ClientSession;\r\n }): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, populateUser = true, session } = params;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n if (populateUser) query = query.populate('userId', 'name email phone');\r\n\r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * List employees\r\n */\r\n async listEmployees(params: ListEmployeesParams): Promise<{\r\n docs: EmployeeDocument[];\r\n totalDocs: number;\r\n page: number;\r\n limit: number;\r\n }> {\r\n this.ensureInitialized();\r\n const { organizationId, filters = {}, pagination = {} } = params;\r\n\r\n let queryBuilder = employeeQuery().forOrganization(organizationId);\r\n\r\n if (filters.status) queryBuilder = queryBuilder.withStatus(filters.status);\r\n if (filters.department) queryBuilder = queryBuilder.inDepartment(filters.department);\r\n if (filters.employmentType) queryBuilder = queryBuilder.withEmploymentType(filters.employmentType);\r\n if (filters.minSalary) queryBuilder = queryBuilder.withMinSalary(filters.minSalary);\r\n if (filters.maxSalary) queryBuilder = queryBuilder.withMaxSalary(filters.maxSalary);\r\n\r\n const query = queryBuilder.build();\r\n const page = pagination.page || 1;\r\n const limit = pagination.limit || 20;\r\n const sort = pagination.sort || { createdAt: -1 };\r\n\r\n const [docs, totalDocs] = await Promise.all([\r\n this.models.EmployeeModel.find(query)\r\n .populate('userId', 'name email phone')\r\n .sort(sort)\r\n .skip((page - 1) * limit)\r\n .limit(limit),\r\n this.models.EmployeeModel.countDocuments(query),\r\n ]);\r\n\r\n return { docs, totalDocs, page, limit };\r\n }\r\n\r\n // ========================================\r\n // Compensation Management\r\n // ========================================\r\n\r\n /**\r\n * Update employee salary\r\n */\r\n async updateSalary(params: UpdateSalaryParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, compensation, effectiveFrom = new Date(), context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n if (employee.status === 'terminated') {\r\n throw new EmployeeTerminatedError(employee.employeeId);\r\n }\r\n\r\n const oldSalary = employee.compensation.netSalary;\r\n\r\n if (compensation.baseAmount !== undefined) {\r\n employee.compensation.baseAmount = compensation.baseAmount;\r\n }\r\n if (compensation.frequency) {\r\n employee.compensation.frequency = compensation.frequency;\r\n }\r\n if (compensation.currency) {\r\n employee.compensation.currency = compensation.currency;\r\n }\r\n\r\n employee.compensation.effectiveFrom = effectiveFrom;\r\n \r\n // Check plugin method exists before calling\r\n if (hasPluginMethod(employee, 'updateSalaryCalculations')) {\r\n (employee as unknown as { updateSalaryCalculations: () => void }).updateSalaryCalculations();\r\n }\r\n\r\n await employee.save({ session });\r\n\r\n // Emit event\r\n this._events.emitSync('salary:updated', {\r\n employee: { id: employee._id, employeeId: employee.employeeId },\r\n previousSalary: oldSalary || 0,\r\n newSalary: employee.compensation.netSalary || 0,\r\n effectiveFrom,\r\n organizationId: employee.organizationId,\r\n context,\r\n });\r\n\r\n getLogger().info('Salary updated', {\r\n employeeId: employee.employeeId,\r\n oldSalary,\r\n newSalary: employee.compensation.netSalary,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Add allowance to employee\r\n */\r\n async addAllowance(params: AddAllowanceParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, type, amount, taxable = true, recurring = true, effectiveFrom = new Date(), effectiveTo, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n if (employee.status === 'terminated') {\r\n throw new EmployeeTerminatedError(employee.employeeId);\r\n }\r\n\r\n if (!employee.compensation.allowances) {\r\n employee.compensation.allowances = [];\r\n }\r\n\r\n employee.compensation.allowances.push({\r\n type,\r\n name: type,\r\n amount,\r\n taxable,\r\n recurring,\r\n effectiveFrom,\r\n effectiveTo,\r\n });\r\n\r\n if (hasPluginMethod(employee, 'updateSalaryCalculations')) {\r\n (employee as unknown as { updateSalaryCalculations: () => void }).updateSalaryCalculations();\r\n }\r\n await employee.save({ session });\r\n\r\n getLogger().info('Allowance added', {\r\n employeeId: employee.employeeId,\r\n type,\r\n amount,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Remove allowance from employee\r\n */\r\n async removeAllowance(params: RemoveAllowanceParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, type, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n const before = employee.compensation.allowances?.length || 0;\r\n \r\n if (hasPluginMethod(employee, 'removeAllowance')) {\r\n (employee as unknown as { removeAllowance: (type: string) => void }).removeAllowance(type);\r\n } else {\r\n // Fallback if plugin not applied\r\n if (employee.compensation.allowances) {\r\n employee.compensation.allowances = employee.compensation.allowances.filter(\r\n (a: Allowance) => a.type !== type\r\n );\r\n }\r\n }\r\n \r\n const after = employee.compensation.allowances?.length || 0;\r\n\r\n if (before === after) {\r\n throw new Error(`Allowance type '${type}' not found`);\r\n }\r\n\r\n await employee.save({ session });\r\n\r\n getLogger().info('Allowance removed', {\r\n employeeId: employee.employeeId,\r\n type,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Add deduction to employee\r\n */\r\n async addDeduction(params: AddDeductionParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, type, amount, auto = false, recurring = true, description, effectiveFrom = new Date(), effectiveTo, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n if (employee.status === 'terminated') {\r\n throw new EmployeeTerminatedError(employee.employeeId);\r\n }\r\n\r\n if (!employee.compensation.deductions) {\r\n employee.compensation.deductions = [];\r\n }\r\n\r\n employee.compensation.deductions.push({\r\n type,\r\n name: type,\r\n amount,\r\n auto,\r\n recurring,\r\n description,\r\n effectiveFrom,\r\n effectiveTo,\r\n });\r\n\r\n if (hasPluginMethod(employee, 'updateSalaryCalculations')) {\r\n (employee as unknown as { updateSalaryCalculations: () => void }).updateSalaryCalculations();\r\n }\r\n await employee.save({ session });\r\n\r\n getLogger().info('Deduction added', {\r\n employeeId: employee.employeeId,\r\n type,\r\n amount,\r\n auto,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Remove deduction from employee\r\n */\r\n async removeDeduction(params: RemoveDeductionParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, type, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n const before = employee.compensation.deductions?.length || 0;\r\n \r\n if (hasPluginMethod(employee, 'removeDeduction')) {\r\n (employee as unknown as { removeDeduction: (type: string) => void }).removeDeduction(type);\r\n } else {\r\n // Fallback if plugin not applied\r\n if (employee.compensation.deductions) {\r\n employee.compensation.deductions = employee.compensation.deductions.filter(\r\n (d: Deduction) => d.type !== type\r\n );\r\n }\r\n }\r\n \r\n const after = employee.compensation.deductions?.length || 0;\r\n\r\n if (before === after) {\r\n throw new Error(`Deduction type '${type}' not found`);\r\n }\r\n\r\n await employee.save({ session });\r\n\r\n getLogger().info('Deduction removed', {\r\n employeeId: employee.employeeId,\r\n type,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n /**\r\n * Update bank details\r\n */\r\n async updateBankDetails(params: UpdateBankDetailsParams): Promise<EmployeeDocument> {\r\n this.ensureInitialized();\r\n const { employeeId, bankDetails, context } = params;\r\n const session = context?.session;\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId));\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n employee.bankDetails = { ...employee.bankDetails, ...bankDetails };\r\n await employee.save({ session });\r\n\r\n getLogger().info('Bank details updated', {\r\n employeeId: employee.employeeId,\r\n });\r\n\r\n return employee;\r\n }\r\n\r\n // ========================================\r\n // Payroll Processing\r\n // ========================================\r\n\r\n /**\r\n * Process salary for single employee\r\n * \r\n * ATOMICITY: This method creates its own transaction if none provided.\r\n * All database operations (PayrollRecord, Transaction, Employee stats) \r\n * are atomic - either all succeed or all fail.\r\n */\r\n async processSalary(params: ProcessSalaryParams): Promise<ProcessSalaryResult> {\r\n this.ensureInitialized();\r\n const { employeeId, month, year, paymentDate = new Date(), paymentMethod = 'bank', context } = params;\r\n\r\n // CRITICAL: Use provided session OR create a new transaction\r\n const providedSession = context?.session;\r\n const session = providedSession || await mongoose.startSession();\r\n const shouldManageTransaction = !providedSession && session != null;\r\n\r\n try {\r\n if (shouldManageTransaction) {\r\n await session.startTransaction();\r\n }\r\n\r\n let query = this.models.EmployeeModel.findById(toObjectId(employeeId)).populate('userId', 'name email');\r\n if (session) query = query.session(session);\r\n \r\n const employee = await query;\r\n if (!employee) {\r\n throw new EmployeeNotFoundError(employeeId.toString());\r\n }\r\n\r\n // Check eligibility - with plugin method verification\r\n const canReceive = hasPluginMethod(employee, 'canReceiveSalary')\r\n ? (employee as unknown as { canReceiveSalary: () => boolean }).canReceiveSalary()\r\n : (employee.status === 'active' && (employee.compensation?.baseAmount || 0) > 0);\r\n\r\n if (!canReceive) {\r\n throw new NotEligibleError('Employee is not eligible to receive salary');\r\n }\r\n\r\n // Check for existing payroll\r\n const existingQuery = payrollQuery()\r\n .forEmployee(employeeId)\r\n .forPeriod(month, year)\r\n .whereIn('status', ['paid', 'processing'])\r\n .build();\r\n\r\n let existingRecordQuery = this.models.PayrollRecordModel.findOne(existingQuery);\r\n if (session) existingRecordQuery = existingRecordQuery.session(session);\r\n const existingRecord = await existingRecordQuery;\r\n \r\n if (existingRecord) {\r\n throw new DuplicatePayrollError(employee.employeeId, month, year);\r\n }\r\n\r\n const period = { ...getPayPeriod(month, year), payDate: paymentDate };\r\n const breakdown = await this.calculateSalaryBreakdown(employee, period, session);\r\n\r\n // Handle userId - could be ObjectId, populated doc, or null\r\n const userIdValue = employee.userId\r\n ? (typeof employee.userId === 'object' && '_id' in employee.userId\r\n ? (employee.userId as { _id: mongoose.Types.ObjectId })._id\r\n : employee.userId)\r\n : undefined;\r\n\r\n const [payrollRecord] = await this.models.PayrollRecordModel.create([{\r\n organizationId: employee.organizationId,\r\n employeeId: employee._id,\r\n userId: userIdValue,\r\n period,\r\n breakdown,\r\n status: 'processing',\r\n paymentMethod,\r\n processedAt: new Date(),\r\n processedBy: context?.userId ? toObjectId(context.userId) : undefined,\r\n }], session ? { session } : {});\r\n\r\n const [transaction] = await this.models.TransactionModel.create([{\r\n organizationId: employee.organizationId,\r\n type: 'expense',\r\n category: HRM_TRANSACTION_CATEGORIES.SALARY,\r\n amount: breakdown.netSalary,\r\n method: paymentMethod,\r\n status: 'completed',\r\n date: paymentDate,\r\n referenceId: employee._id,\r\n referenceModel: 'Employee',\r\n handledBy: context?.userId ? toObjectId(context.userId) : undefined,\r\n notes: `Salary payment - ${(employee.userId as { name?: string })?.name || employee.employeeId} (${month}/${year})`,\r\n metadata: {\r\n employeeId: employee.employeeId,\r\n payrollRecordId: payrollRecord._id,\r\n period: { month, year },\r\n breakdown: {\r\n base: breakdown.baseAmount,\r\n allowances: sumAllowances(breakdown.allowances),\r\n deductions: sumDeductions(breakdown.deductions),\r\n tax: breakdown.taxAmount || 0,\r\n gross: breakdown.grossSalary,\r\n net: breakdown.netSalary,\r\n },\r\n },\r\n }], session ? { session } : {});\r\n\r\n // Update payroll record with transaction reference\r\n payrollRecord.transactionId = transaction._id;\r\n payrollRecord.status = 'paid';\r\n payrollRecord.paidAt = paymentDate;\r\n await payrollRecord.save(session ? { session } : {});\r\n\r\n // Update employee payroll stats\r\n await this.updatePayrollStats(employee, breakdown.netSalary, paymentDate, session);\r\n\r\n // Commit transaction if we created it\r\n if (shouldManageTransaction) {\r\n await session.commitTransaction();\r\n }\r\n\r\n // Emit event (after commit to ensure data is persisted)\r\n this._events.emitSync('salary:processed', {\r\n employee: {\r\n id: employee._id,\r\n employeeId: employee.employeeId,\r\n name: (employee.userId as { name?: string })?.name,\r\n },\r\n payroll: {\r\n id: payrollRecord._id,\r\n period: { month, year },\r\n grossAmount: breakdown.grossSalary,\r\n netAmount: breakdown.netSalary,\r\n },\r\n transactionId: transaction._id,\r\n organizationId: employee.organizationId,\r\n context,\r\n });\r\n\r\n getLogger().info('Salary processed', {\r\n employeeId: employee.employeeId,\r\n month,\r\n year,\r\n amount: breakdown.netSalary,\r\n });\r\n\r\n return { payrollRecord, transaction, employee };\r\n\r\n } catch (error) {\r\n // Rollback transaction if we created it\r\n if (shouldManageTransaction && session?.inTransaction()) {\r\n await session.abortTransaction();\r\n }\r\n throw error;\r\n } finally {\r\n // End session if we created it\r\n if (shouldManageTransaction && session) {\r\n await session.endSession();\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Process bulk payroll\r\n * \r\n * ATOMICITY STRATEGY: Each employee is processed in its own transaction.\r\n * This allows partial success - some employees can succeed while others fail.\r\n * Failed employees don't affect successful ones.\r\n */\r\n async processBulkPayroll(params: ProcessBulkPayrollParams): Promise<BulkPayrollResult> {\r\n this.ensureInitialized();\r\n const { organizationId, month, year, employeeIds = [], paymentDate = new Date(), paymentMethod = 'bank', context } = params;\r\n\r\n const query: Record<string, unknown> = { organizationId: toObjectId(organizationId), status: 'active' };\r\n if (employeeIds.length > 0) {\r\n query._id = { $in: employeeIds.map(toObjectId) };\r\n }\r\n\r\n const employees = await this.models.EmployeeModel.find(query);\r\n\r\n const results: BulkPayrollResult = {\r\n successful: [],\r\n failed: [],\r\n total: employees.length,\r\n };\r\n\r\n // Process each employee in its own transaction for isolation\r\n for (const employee of employees) {\r\n try {\r\n // Each processSalary call manages its own transaction\r\n const result = await this.processSalary({\r\n employeeId: employee._id,\r\n month,\r\n year,\r\n paymentDate,\r\n paymentMethod,\r\n context: { ...context, session: undefined }, // Don't pass session - let processSalary create its own\r\n });\r\n\r\n results.successful.push({\r\n employeeId: employee.employeeId,\r\n amount: result.payrollRecord.breakdown.netSalary,\r\n transactionId: result.transaction._id,\r\n });\r\n } catch (error) {\r\n results.failed.push({\r\n employeeId: employee.employeeId,\r\n error: (error as Error).message,\r\n });\r\n\r\n getLogger().error('Failed to process salary', {\r\n employeeId: employee.employeeId,\r\n error: (error as Error).message,\r\n });\r\n }\r\n }\r\n\r\n // Emit completed event\r\n this._events.emitSync('payroll:completed', {\r\n organizationId: toObjectId(organizationId),\r\n period: { month, year },\r\n summary: {\r\n total: results.total,\r\n successful: results.successful.length,\r\n failed: results.failed.length,\r\n totalAmount: results.successful.reduce((sum, r) => sum + r.amount, 0),\r\n },\r\n context,\r\n });\r\n\r\n getLogger().info('Bulk payroll processed', {\r\n organizationId: organizationId.toString(),\r\n month,\r\n year,\r\n total: results.total,\r\n successful: results.successful.length,\r\n failed: results.failed.length,\r\n });\r\n\r\n return results;\r\n }\r\n\r\n /**\r\n * Get payroll history\r\n */\r\n async payrollHistory(params: PayrollHistoryParams): Promise<PayrollRecordDocument[]> {\r\n this.ensureInitialized();\r\n const { employeeId, organizationId, month, year, status, pagination = {} } = params;\r\n\r\n let queryBuilder = payrollQuery();\r\n if (employeeId) queryBuilder = queryBuilder.forEmployee(employeeId);\r\n if (organizationId) queryBuilder = queryBuilder.forOrganization(organizationId);\r\n if (month || year) queryBuilder = queryBuilder.forPeriod(month, year);\r\n if (status) queryBuilder = queryBuilder.withStatus(status);\r\n\r\n const query = queryBuilder.build();\r\n const page = pagination.page || 1;\r\n const limit = pagination.limit || 20;\r\n const sort = pagination.sort || { 'period.year': -1, 'period.month': -1 };\r\n\r\n return this.models.PayrollRecordModel.find(query)\r\n .populate('employeeId', 'employeeId position department')\r\n .populate('userId', 'name email')\r\n .populate('transactionId', 'amount method status date')\r\n .sort(sort)\r\n .skip((page - 1) * limit)\r\n .limit(limit);\r\n }\r\n\r\n /**\r\n * Get payroll summary\r\n */\r\n async payrollSummary(params: PayrollSummaryParams): Promise<PayrollSummaryResult> {\r\n this.ensureInitialized();\r\n const { organizationId, month, year } = params;\r\n\r\n const query: Record<string, unknown> = { organizationId: toObjectId(organizationId) };\r\n if (month) query['period.month'] = month;\r\n if (year) query['period.year'] = year;\r\n\r\n const [summary] = await this.models.PayrollRecordModel.aggregate([\r\n { $match: query },\r\n {\r\n $group: {\r\n _id: null,\r\n totalGross: { $sum: '$breakdown.grossSalary' },\r\n totalNet: { $sum: '$breakdown.netSalary' },\r\n totalDeductions: { $sum: { $sum: '$breakdown.deductions.amount' } },\r\n totalTax: { $sum: { $ifNull: ['$breakdown.taxAmount', 0] } },\r\n employeeCount: { $sum: 1 },\r\n paidCount: { $sum: { $cond: [{ $eq: ['$status', 'paid'] }, 1, 0] } },\r\n pendingCount: { $sum: { $cond: [{ $eq: ['$status', 'pending'] }, 1, 0] } },\r\n },\r\n },\r\n ]);\r\n\r\n return summary || {\r\n totalGross: 0,\r\n totalNet: 0,\r\n totalDeductions: 0,\r\n totalTax: 0,\r\n employeeCount: 0,\r\n paidCount: 0,\r\n pendingCount: 0,\r\n };\r\n }\r\n\r\n /**\r\n * Export payroll data\r\n */\r\n async exportPayroll(params: ExportPayrollParams): Promise<PayrollRecordDocument[]> {\r\n this.ensureInitialized();\r\n const { organizationId, startDate, endDate } = params;\r\n\r\n const query = {\r\n organizationId: toObjectId(organizationId),\r\n 'period.payDate': { $gte: startDate, $lte: endDate },\r\n };\r\n\r\n const records = await this.models.PayrollRecordModel.find(query)\r\n .populate('employeeId', 'employeeId position department')\r\n .populate('userId', 'name email')\r\n .populate('transactionId', 'amount method status date')\r\n .sort({ 'period.year': -1, 'period.month': -1 });\r\n\r\n // Mark as exported\r\n await this.models.PayrollRecordModel.updateMany(query, {\r\n exported: true,\r\n exportedAt: new Date(),\r\n });\r\n\r\n // Emit event\r\n this._events.emitSync('payroll:exported', {\r\n organizationId: toObjectId(organizationId),\r\n dateRange: { start: startDate, end: endDate },\r\n recordCount: records.length,\r\n format: 'json',\r\n });\r\n\r\n getLogger().info('Payroll data exported', {\r\n organizationId: organizationId.toString(),\r\n count: records.length,\r\n });\r\n\r\n return records;\r\n }\r\n\r\n // ========================================\r\n // Helper Methods\r\n // ========================================\r\n\r\n /**\r\n * Calculate salary breakdown with proper handling for:\r\n * - Effective dates on allowances/deductions\r\n * - Pro-rating for mid-period hires AND terminations\r\n * - Tax calculation\r\n * - Working days vs calendar days for attendance\r\n */\r\n private async calculateSalaryBreakdown(\r\n employee: EmployeeDocument,\r\n period: { month: number; year: number; startDate: Date; endDate: Date; payDate: Date },\r\n session?: ClientSession\r\n ): Promise<import('./types.js').PayrollBreakdown> {\r\n const comp = employee.compensation;\r\n let baseAmount = comp.baseAmount;\r\n\r\n // Calculate working days in the period (excluding weekends)\r\n const workingDaysInMonth = getWorkingDaysInMonth(period.year, period.month);\r\n \r\n // Pro-rating calculation considering both hire date AND termination date\r\n const proRating = this.calculateProRatingAdvanced(\r\n employee.hireDate,\r\n employee.terminationDate || null,\r\n period.startDate,\r\n period.endDate,\r\n workingDaysInMonth\r\n );\r\n\r\n if (proRating.isProRated && this.config.payroll.allowProRating) {\r\n baseAmount = Math.round(baseAmount * proRating.ratio);\r\n }\r\n\r\n // Filter allowances by effective date\r\n const effectiveAllowances = (comp.allowances || [])\r\n .filter((a) => isEffectiveForPeriod(a, period.startDate, period.endDate))\r\n .filter((a) => a.recurring !== false);\r\n\r\n // Filter deductions by effective date\r\n const effectiveDeductions = (comp.deductions || [])\r\n .filter((d) => isEffectiveForPeriod(d, period.startDate, period.endDate))\r\n .filter((d) => d.auto || d.recurring);\r\n\r\n // Apply pro-rating to allowances if needed\r\n const allowances = effectiveAllowances.map((a) => ({\r\n type: a.type,\r\n amount: proRating.isProRated && this.config.payroll.allowProRating\r\n ? Math.round(a.amount * proRating.ratio)\r\n : a.amount,\r\n taxable: a.taxable ?? true,\r\n }));\r\n\r\n // Apply pro-rating to deductions if needed\r\n const deductions = effectiveDeductions.map((d) => ({\r\n type: d.type,\r\n amount: proRating.isProRated && this.config.payroll.allowProRating\r\n ? Math.round(d.amount * proRating.ratio)\r\n : d.amount,\r\n description: d.description,\r\n }));\r\n\r\n // Attendance deduction calculation (uses working days, not calendar days)\r\n let attendanceDeduction = 0;\r\n if (this.models.AttendanceModel && this.config.payroll.attendanceIntegration) {\r\n attendanceDeduction = await this.calculateAttendanceDeduction(\r\n employee._id,\r\n employee.organizationId,\r\n period,\r\n baseAmount / proRating.workingDays, // Daily rate based on working days\r\n proRating.workingDays,\r\n session\r\n );\r\n }\r\n\r\n if (attendanceDeduction > 0) {\r\n deductions.push({\r\n type: 'absence',\r\n amount: attendanceDeduction,\r\n description: 'Unpaid leave deduction',\r\n });\r\n }\r\n\r\n // Calculate gross salary\r\n const grossSalary = calculateGross(baseAmount, allowances);\r\n \r\n // Calculate taxable amount (only taxable allowances)\r\n const taxableAllowances = allowances.filter(a => a.taxable);\r\n const taxableAmount = baseAmount + sumAllowances(taxableAllowances);\r\n \r\n // Calculate tax\r\n let taxAmount = 0;\r\n const currency = comp.currency || this.config.payroll.defaultCurrency;\r\n const taxBrackets = TAX_BRACKETS[currency] || [];\r\n \r\n if (taxBrackets.length > 0 && this.config.payroll.autoDeductions) {\r\n // Annualize the taxable amount for tax bracket calculation\r\n const annualTaxable = taxableAmount * 12;\r\n const annualTax = applyTaxBrackets(annualTaxable, taxBrackets);\r\n taxAmount = Math.round(annualTax / 12); // Monthly tax\r\n }\r\n\r\n // Add tax to deductions if applicable\r\n if (taxAmount > 0) {\r\n deductions.push({\r\n type: 'tax',\r\n amount: taxAmount,\r\n description: 'Income tax',\r\n });\r\n }\r\n\r\n // Calculate net salary\r\n const netSalary = calculateNet(grossSalary, deductions);\r\n\r\n return {\r\n baseAmount,\r\n allowances,\r\n deductions,\r\n grossSalary,\r\n netSalary,\r\n taxableAmount,\r\n taxAmount,\r\n workingDays: proRating.workingDays,\r\n actualDays: proRating.actualDays,\r\n proRatedAmount: proRating.isProRated ? baseAmount : 0,\r\n attendanceDeduction,\r\n };\r\n }\r\n\r\n /**\r\n * Advanced pro-rating calculation that handles:\r\n * - Mid-period hires\r\n * - Mid-period terminations\r\n * - Working days (not calendar days)\r\n */\r\n private calculateProRatingAdvanced(\r\n hireDate: Date,\r\n terminationDate: Date | null,\r\n periodStart: Date,\r\n periodEnd: Date,\r\n workingDaysInMonth: number\r\n ): {\r\n isProRated: boolean;\r\n ratio: number;\r\n workingDays: number;\r\n actualDays: number;\r\n } {\r\n const hire = new Date(hireDate);\r\n const termination = terminationDate ? new Date(terminationDate) : null;\r\n \r\n // Determine the actual start and end dates for this employee in this period\r\n const effectiveStart = hire > periodStart ? hire : periodStart;\r\n const effectiveEnd = termination && termination < periodEnd ? termination : periodEnd;\r\n \r\n // If employee wasn't active during this period\r\n if (effectiveStart > periodEnd || (termination && termination < periodStart)) {\r\n return {\r\n isProRated: true,\r\n ratio: 0,\r\n workingDays: workingDaysInMonth,\r\n actualDays: 0,\r\n };\r\n }\r\n \r\n // Calculate working days in the effective period\r\n const totalDays = diffInDays(periodStart, periodEnd) + 1;\r\n const actualDays = diffInDays(effectiveStart, effectiveEnd) + 1;\r\n \r\n // For working days calculation, we use a simple ratio\r\n // A more accurate implementation would count actual working days\r\n const ratio = actualDays / totalDays;\r\n const actualWorkingDays = Math.round(workingDaysInMonth * ratio);\r\n \r\n const isProRated = hire > periodStart || (termination !== null && termination < periodEnd);\r\n\r\n return {\r\n isProRated,\r\n ratio,\r\n workingDays: workingDaysInMonth,\r\n actualDays: actualWorkingDays,\r\n };\r\n }\r\n\r\n /**\r\n * Calculate attendance deduction using working days (not calendar days)\r\n */\r\n private async calculateAttendanceDeduction(\r\n employeeId: mongoose.Types.ObjectId,\r\n organizationId: mongoose.Types.ObjectId,\r\n period: { month: number; year: number; startDate: Date; endDate: Date },\r\n dailyRate: number,\r\n expectedWorkingDays: number,\r\n session?: ClientSession\r\n ): Promise<number> {\r\n try {\r\n if (!this.models.AttendanceModel) return 0;\r\n\r\n let query = this.models.AttendanceModel.findOne({\r\n tenantId: organizationId,\r\n targetId: employeeId,\r\n targetModel: 'Employee',\r\n year: period.year,\r\n month: period.month,\r\n });\r\n if (session) query = query.session(session);\r\n\r\n const attendance = await query;\r\n if (!attendance) return 0;\r\n\r\n const workedDays = (attendance as { totalWorkDays?: number }).totalWorkDays || 0;\r\n \r\n // Calculate absent days based on expected working days (not calendar days)\r\n const absentDays = Math.max(0, expectedWorkingDays - workedDays);\r\n\r\n return Math.round(absentDays * dailyRate);\r\n } catch (error) {\r\n getLogger().warn('Failed to calculate attendance deduction', {\r\n employeeId: employeeId.toString(),\r\n error: (error as Error).message,\r\n });\r\n return 0;\r\n }\r\n }\r\n\r\n private async updatePayrollStats(\r\n employee: EmployeeDocument,\r\n amount: number,\r\n paymentDate: Date,\r\n session?: ClientSession\r\n ): Promise<void> {\r\n if (!employee.payrollStats) {\r\n employee.payrollStats = {\r\n totalPaid: 0,\r\n paymentsThisYear: 0,\r\n averageMonthly: 0,\r\n };\r\n }\r\n\r\n employee.payrollStats.totalPaid = (employee.payrollStats.totalPaid || 0) + amount;\r\n employee.payrollStats.lastPaymentDate = paymentDate;\r\n employee.payrollStats.paymentsThisYear = (employee.payrollStats.paymentsThisYear || 0) + 1;\r\n employee.payrollStats.averageMonthly = Math.round(\r\n employee.payrollStats.totalPaid / employee.payrollStats.paymentsThisYear\r\n );\r\n employee.payrollStats.nextPaymentDate = addMonths(paymentDate, 1);\r\n\r\n await employee.save(session ? { session } : {});\r\n }\r\n\r\n // ========================================\r\n // Static Factory\r\n // ========================================\r\n\r\n /**\r\n * Create a new Payroll instance\r\n */\r\n static create(): Payroll {\r\n return new Payroll();\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Payroll Builder\r\n// ============================================================================\r\n\r\nexport interface ModelsConfig {\r\n EmployeeModel: Model<any>;\r\n PayrollRecordModel: Model<any>;\r\n TransactionModel: Model<any>;\r\n AttendanceModel?: Model<any> | null;\r\n}\r\n\r\nexport interface PayrollBuilderOptions {\r\n models?: ModelsConfig;\r\n config?: DeepPartial<HRMConfig>;\r\n singleTenant?: SingleTenantConfig | null;\r\n logger?: Logger;\r\n}\r\n\r\nexport class PayrollBuilder {\r\n private _models: ModelsConfig | null = null;\r\n private _config: DeepPartial<HRMConfig> | undefined;\r\n private _singleTenant: SingleTenantConfig | null = null;\r\n private _logger: Logger | undefined;\r\n\r\n /**\r\n * Set models\r\n */\r\n withModels(models: ModelsConfig): this {\r\n this._models = models;\r\n return this;\r\n }\r\n\r\n /**\r\n * Set config overrides\r\n */\r\n withConfig(config: DeepPartial<HRMConfig>): this {\r\n this._config = config;\r\n return this;\r\n }\r\n\r\n /**\r\n * Enable single-tenant mode\r\n * \r\n * Use this when building a single-organization HRM (no organizationId needed)\r\n * \r\n * @example\r\n * ```typescript\r\n * const payroll = createPayrollInstance()\r\n * .withModels({ EmployeeModel, PayrollRecordModel, TransactionModel })\r\n * .withSingleTenant({ organizationId: 'my-company' })\r\n * .build();\r\n * ```\r\n */\r\n withSingleTenant(config: SingleTenantConfig): this {\r\n this._singleTenant = config;\r\n return this;\r\n }\r\n\r\n /**\r\n * Enable single-tenant mode (shorthand)\r\n * \r\n * Alias for withSingleTenant() - consistent with @classytic/clockin API\r\n * \r\n * @example\r\n * ```typescript\r\n * const payroll = createPayrollInstance()\r\n * .withModels({ ... })\r\n * .forSingleTenant() // ← No organizationId needed!\r\n * .build();\r\n * ```\r\n */\r\n forSingleTenant(config: SingleTenantConfig = {}): this {\r\n return this.withSingleTenant(config);\r\n }\r\n\r\n /**\r\n * Set custom logger\r\n */\r\n withLogger(logger: Logger): this {\r\n this._logger = logger;\r\n return this;\r\n }\r\n\r\n /**\r\n * Build and initialize Payroll instance\r\n */\r\n build(): Payroll {\r\n if (!this._models) {\r\n throw new Error('Models are required. Call withModels() first.');\r\n }\r\n\r\n const payroll = new Payroll();\r\n payroll.initialize({\r\n EmployeeModel: this._models.EmployeeModel,\r\n PayrollRecordModel: this._models.PayrollRecordModel,\r\n TransactionModel: this._models.TransactionModel,\r\n AttendanceModel: this._models.AttendanceModel,\r\n config: this._config,\r\n singleTenant: this._singleTenant,\r\n logger: this._logger,\r\n });\r\n\r\n return payroll;\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Factory Function\r\n// ============================================================================\r\n\r\n/**\r\n * Create a new Payroll builder\r\n */\r\nexport function createPayrollInstance(): PayrollBuilder {\r\n return new PayrollBuilder();\r\n}\r\n\r\n// ============================================================================\r\n// Singleton Instance\r\n// ============================================================================\r\n\r\nlet payrollInstance: Payroll | null = null;\r\n\r\n/**\r\n * Get or create singleton Payroll instance\r\n */\r\nexport function getPayroll(): Payroll {\r\n if (!payrollInstance) {\r\n payrollInstance = new Payroll();\r\n }\r\n return payrollInstance;\r\n}\r\n\r\n/**\r\n * Reset singleton (for testing)\r\n */\r\nexport function resetPayroll(): void {\r\n payrollInstance = null;\r\n Container.resetInstance();\r\n}\r\n\r\n// ============================================================================\r\n// Exports\r\n// ============================================================================\r\n\r\nexport const payroll = getPayroll();\r\nexport default payroll;\r\n"]}