Overview
Thefun_act_nomina function is the heart of the NominaSoft payroll system. This function calculates the complete payroll for all active employees for a specific year, month, and period. It processes both mandatory legal concepts (salary, transportation allowance, deductions) and optional concepts (bonuses, overtime), generating detailed payroll records in the tab_nomina table.
This function orchestrates the entire payroll calculation workflow by:
- Validating input parameters against system configuration
- Iterating through all employees
- Calculating mandatory and optional payroll concepts
- Computing accruals (devengados) and deductions (deducidos)
- Generating individual payroll records for each employee-concept combination
Function Signature
Parameters
The payroll year to process. Must match the year configured in
tab_pmtros.ano_nomina.Example: 2025The payroll month to process (1-12). Must match the month configured in
tab_pmtros.mes_nomina.Example: 1 (January)The payroll period within the month:
1= First period (first half of month for biweekly, full month for monthly)2= Second period (second half of month for biweekly)
tab_pmtros.ind_perio_pago (‘Q’ for biweekly/quincenal, ‘M’ for monthly/mensual).Example: 1Return Value
ReturnsBOOLEAN:
TRUE- Payroll was successfully calculated and saved totab_nominaFALSE- An error occurred during processing (see exception messages)
Algorithm and Business Logic
1. Parameter Loading and Validation
The function begins by loading the system parameters fromtab_pmtros:
- Input year must match
tab_pmtros.ano_nomina(SQLSTATE 22008) - Input month must match
tab_pmtros.mes_nomina(SQLSTATE 22008) - Period must be ≤ 2 (SQLSTATE 22008)
2. Payroll Reset
The function deletes any existing payroll records for the specified period to ensure a clean recalculation:3. Employee Iteration
For each active employee intab_emplea, the function retrieves:
id_emplea- Employee IDnom_emplea- Employee first nameape_emplea- Employee last nameval_sal_basico- Base salary
4. Mandatory Concepts Processing
For each employee, the function processes all mandatory legal concepts (ind_legal = TRUE, neto_pagado = FALSE):
A. Accruals (Devengados - ind_operacion = TRUE)
Basic Salary Calculation:
B. Deductions (Deducidos - ind_operacion = FALSE)
Percentage-Based Deductions:
5. Record Generation
For each employee-concept combination, a record is inserted intotab_nomina:
6. Optional Concepts (Novedades)
The function includes logic structure for processing non-mandatory concepts (ind_legal = FALSE) such as:
- Bonuses (bonificaciones)
- Overtime hours (horas extras diurnas/nocturnas/festivas)
- Other variable concepts
Usage Examples
Example 1: Process January 2025 First Period
TRUE if successful
This will:
- Delete existing payroll for 2025-01, period 1
- Calculate payroll for all employees
- Generate individual records for each concept (salary, transportation, deductions)
- Store results in
tab_nomina
Example 2: Process February 2025 Second Period (Biweekly)
TRUE if successful (assuming biweekly payment configuration)
Example 3: Monthly Payroll Processing
Example 4: Error Handling
FALSE with notice: “El año, o el mes, o el período no corresponden al de PMTROS… Arréglelo Bestia”
Exception Handling
The function implements comprehensive error handling with specific SQLSTATE codes:Invalid Year, Month, or PeriodRaised when input parameters don’t match system configuration in
tab_pmtros.Message: “El año, o el mes, o el período no corresponden al de PMTROS… Arréglelo Bestia”Resolution: Verify ano_nomina, mes_nomina in tab_pmtros match your input parameters, and ensure period ≤ 2.NULL Value in Required FieldRaised when attempting to insert NULL into a NOT NULL column.Message: “Está mandando un NULO en el ID… Sea serio”Resolution: Ensure all required fields have valid values before processing.
Foreign Key ViolationRaised when referencing a non-existent record (e.g., invalid cargo/position).Message: “El Cargo no existe… Créelo y vuelva, o ni se aparezca más por acá”Resolution: Create the referenced record (cargo, concepto, etc.) before running payroll.
Duplicate RecordRaised when attempting to insert a duplicate record.Message: “El registro ya existe.. Trabaje bien o ábrase llaveee”Resolution: This should not occur as the function deletes existing records first. Check for concurrent execution.
String Too LongRaised when a string value exceeds the column’s maximum length.Message: “El nombre es muy corto.. Es de su abuelita?”Resolution: Ensure all string values comply with column length constraints.
Insert FailedRaised when an INSERT operation fails unexpectedly.Resolution: Check database constraints, trigger logic, and data integrity.
Database Dependencies
Tables Read
tab_pmtros- System parameters (payment period, SMLV, etc.)tab_emplea- Employee master datatab_conceptos- Payroll concepts (salary, deductions, bonuses)
Tables Modified
tab_nomina- Payroll records (DELETE existing + INSERT new records)
Required Configuration
Before running this function, ensure:-
System Parameters (
tab_pmtros) are configured:ano_nomina,mes_nomina- Current payroll periodind_perio_pago- ‘Q’ (biweekly) or ‘M’ (monthly)val_smlv- Current minimum wageval_auxtrans- Transportation allowance amountind_num_trans- SMLV multiplier for transport eligibilitynum_diasmes- Days in the period (typically 30)id_concep_sb- Concept ID for basic salaryid_concep_at- Concept ID for transportation allowance
-
Employees (
tab_emplea) exist with validval_sal_basico -
Concepts (
tab_conceptos) are defined with proper configuration
Performance Considerations
- The function processes all employees in the database in a single execution
- Uses cursors for iterating through employees and concepts
- Deletes and regenerates all payroll records for the period
- Execution time increases linearly with number of employees and concepts
- Consider scheduling during off-peak hours for large employee bases
Best Practices
-
Always validate parameters before calling:
-
Check results after execution:
-
Run reports to verify calculations:
- Backup before execution in production environments
- Test thoroughly with sample data before production use
Related Functions
- fun_act_empleado - Employee management
- fun_act_concepto - Payroll concept management
- fun_get_neto_pagado - Calculate net payment per employee
Notes
- This function uses PostgreSQL PL/pgSQL language
- The commented code (lines 224-258) shows planned support for optional concepts (novedades)
- Function includes detailed RAISE NOTICE statements for debugging purposes
- All monetary calculations respect the payment period configuration (Q/M)