PlanillaHoras_VBA/DataProcessor.vba

136 lines
5.0 KiB
Plaintext
Raw Normal View History

2025-03-01 09:37:31 -03:00
' Module: DataProcessor
' Description: Core data processing functions
Public Sub ProcessWorkHourData(fechaDesde As Date, fechaHasta As Date, exportResults As Boolean, facturaNum As String)
Dim wsHoras As Worksheet, wsFdl As Worksheet
Dim ultimaFilaHoras As Long
Dim i As Long, j As Long, k As Integer
Dim pdfExportado As Boolean
' Update invoice number
ThisWorkbook.Sheets(SHEET_NOTA).Cells(6, 3).Value = facturaNum
' Validate date range
If Not ValidateDateRange(fechaDesde, fechaHasta) Then
Exit Sub
End If
' Clear existing content in FDL sheets
ClearFDLSheets
' Set references to worksheets
Set wsHoras = ThisWorkbook.Sheets(SHEET_HORAS)
Set wsFdl = ThisWorkbook.Sheets(SHEET_FDL_1)
ultimaFilaHoras = wsHoras.Cells(Rows.Count, 5).End(xlUp).Row
' Initialize variables for destination row and entry count
j = PRIMERA_FILA_FDL
k = 0
' Process each row in Horas sheet
For i = 3 To ultimaFilaHoras
If CDate(wsHoras.Cells(i, 5).Value2) >= fechaDesde And CDate(wsHoras.Cells(i, 5).Value2) <= fechaHasta Then
' Copy data to current FDL sheet
CopyRowToFDL wsHoras, wsFdl, i, j
j = j + 2: k = k + 1
pdfExportado = False
' Handle sheet transitions at MAX_ENTRIES_PER_FDL entries
If k = MAX_ENTRIES_PER_FDL Then
If exportResults Then
ExportToPDF wsFdl, Format(fechaDesde, "dd-MM"), Format(wsFdl.Cells(j - 2, 1).Value2, "dd-MM")
End If
Set wsFdl = ThisWorkbook.Sheets(SHEET_FDL_2)
j = PRIMERA_FILA_FDL
pdfExportado = True
End If
' Handle sheet transitions at MAX_ENTRIES_PER_FDL * 2 entries
If k = MAX_ENTRIES_PER_FDL * 2 Then
If exportResults Then
ExportToPDF wsFdl, Format(wsFdl.Cells(PRIMERA_FILA_FDL, 1).Value2, "dd-MM"), Format(wsFdl.Cells(j - 2, 1).Value2, "dd-MM")
End If
Set wsFdl = ThisWorkbook.Sheets(SHEET_FDL_3)
j = PRIMERA_FILA_FDL
pdfExportado = True
End If
' Exit if we exceed maximum entries
If k > MAX_TOTAL_ENTRIES Then Exit For
End If
Next i
' Export final sheet if not already exported
If exportResults And Not pdfExportado Then
ExportToPDF wsFdl, Format(wsFdl.Cells(PRIMERA_FILA_FDL, 1).Value2, "dd-MM"), Format(fechaHasta, "dd-MM")
End If
' Export additional documents if needed
If exportResults Then
ExportToPDF ThisWorkbook.Sheets(SHEET_NOTA), "Fattura_" & Format(fechaDesde, "dd-MM"), Format(fechaHasta, "dd-MM")
ExportToPDF ThisWorkbook.Sheets(SHEET_FOGLIO_1), "Expenses_" & Format(fechaDesde, "dd-MM"), Format(fechaHasta, "dd-MM")
ExportToExcel Format(fechaDesde, "dd-MM"), Format(fechaHasta, "dd-MM")
End If
End Sub
Private Sub CopyRowToFDL(wsSource As Worksheet, wsTarget As Worksheet, sourceRow As Long, targetRow As Long)
' Copy data from source row to target row in FDL sheet
wsTarget.Cells(targetRow, 1).Value2 = wsSource.Cells(sourceRow, 5).Value2
wsTarget.Cells(targetRow, 3).Value = Format(wsSource.Cells(sourceRow, 6).Value, "hh:mm")
wsTarget.Cells(targetRow, 4).Value = wsSource.Cells(sourceRow, 7).Value
wsTarget.Cells(targetRow, 5).Value = wsSource.Cells(sourceRow, 8).Value
wsTarget.Cells(targetRow, 6).Value = wsSource.Cells(sourceRow, 9).Value
' Set optional values only if they're greater than 0
If wsSource.Cells(sourceRow, 14).Value > 0 Then
wsTarget.Cells(targetRow, 7).Value = wsSource.Cells(sourceRow, 14).Value
End If
If wsSource.Cells(sourceRow, 15).Value > 0 Then
wsTarget.Cells(targetRow, 8).Value = wsSource.Cells(sourceRow, 15).Value
End If
wsTarget.Cells(targetRow, 10).Value = wsSource.Cells(sourceRow, 2).Value
End Sub
Private Sub ClearFDLSheets()
' Clear content in all FDL sheets
ThisWorkbook.Sheets(SHEET_FDL_1).Range("A17:P44").ClearContents
ThisWorkbook.Sheets(SHEET_FDL_2).Range("A17:P44").ClearContents
ThisWorkbook.Sheets(SHEET_FDL_3).Range("A17:P44").ClearContents
End Sub
Public Sub RunPythonLogProcessor()
Dim objShell As Object
' Create shell object
Set objShell = VBA.CreateObject("WScript.Shell")
' Execute Python script
objShell.Run "cmd /c " & PYTHON_EXE_PATH & " " & SCRIPT_PATH & " > " & LOG_FILE_PATH & " 2>&1", 0, True
Set objShell = Nothing
End Sub
Public Sub RefreshLogData()
Dim wb As Workbook
' Open log Excel file
Set wb = Workbooks.Open(LOG_EXCEL_PATH)
' Refresh all connections
wb.RefreshAll
' Wait for refresh to complete
Application.Wait Now + TimeValue("00:00:05")
' Close without saving
wb.Close SaveChanges:=False
' Refresh current workbook
ThisWorkbook.RefreshAll
End Sub