Last active
October 5, 2020 03:54
-
-
Save zeqk/671aeabb9b597dfc0b4c93b3d65ea37c to your computer and use it in GitHub Desktop.
Script para obtener los totales a partir de los PDF del S-21 Modo de uso ```powershell
.\Get-Totals.ps1 2021 09
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "RemarksAPIdentifier":"PA", | |
| "RPTotalsFile" :"C:/S-21 2021/S-21 2021 2022 PR.pdf", | |
| "APTotalsFile" :"C:/S-21 2021/S-21 2021 2022 PA.pdf", | |
| "PubsTotalsFile" :"C:/S-21 2021/S-21 2021 2022 P.pdf", | |
| "PRFolder":"C:/S-21 2021/PR", | |
| "PubsFolder":"C:/S-21 2021/P" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Install-Nuget { | |
| $sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" | |
| $targetNugetExe = "$PSScriptRoot\nuget.exe" | |
| If( -Not (Test-Path $targetNugetExe)){ | |
| Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe | |
| } | |
| } | |
| function Install-IText { | |
| # Download nuget.exe | |
| Install-Nuget | |
| # nuget install itext7 | |
| Invoke-Expression "$PSScriptRoot\nuget.exe install iText7 -OutputDirectory $PSScriptRoot\lib" | |
| # requires itextsharp.dll | |
| Add-Type -Path "$PSScriptRoot\lib\itext7.7.1.12\lib\netstandard1.6\itext.kernel.dll" | |
| Add-Type -Path "$PSScriptRoot\lib\itext7.7.1.12\lib\netstandard1.6\itext.forms.dll" | |
| Add-Type -Path "$PSScriptRoot\lib\Common.Logging.3.4.1\lib\netstandard1.3\Common.Logging.dll" | |
| Add-Type -Path "$PSScriptRoot\lib\Common.Logging.Core.3.4.1\lib\netstandard1.0\Common.Logging.Core.dll" | |
| Add-Type -Path "$PSScriptRoot\lib\Portable.BouncyCastle.1.8.5\lib\netstandard1.3\BouncyCastle.Crypto.dll" | |
| } | |
| function GetPublisherMonthReport | |
| { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$publisherCardPath, | |
| [Parameter(Mandatory=$true)] | |
| [int]$serviceYear, | |
| [Parameter(Mandatory=$true)] | |
| [int]$month | |
| ) | |
| try { | |
| # Get all pdf fields | |
| $reader = [iText.Kernel.Pdf.PdfReader]::new($publisherCardPath) | |
| $PdfDoc = [iText.Kernel.Pdf.PdfDocument]::new($reader) | |
| $Form = [iText.Forms.PdfAcroForm]::getAcroForm($PdfDoc, $False) | |
| $Fields = $Form.getFormFields() | |
| # The PDF contains two tables, one table for one service year | |
| # The first table fields starts with the prefix "1", and the second with "2" | |
| # $fieldPrefix | |
| # $remarksSufix | |
| If(($Fields | Where-Object {$_.key -eq "Service Year"}).Value.GetValue() -Eq $serviceYear) { | |
| $fieldPrefix = "1" | |
| $remarksSufix = "" | |
| } ElseIf(($Fields | Where-Object {$_.key -eq "Service Year_2"}).Value.GetValue() -Eq $serviceYear) { | |
| $fieldPrefix = "2" | |
| $remarksSufix = "_2" | |
| } Else { | |
| Write-Warning "$name's card has no service year $serviceYear" | |
| return; | |
| } | |
| # Each table record ends with the month number (counting from september) | |
| # $fieldSufix | |
| # $monthName | |
| switch ($month) { | |
| 9 { | |
| $fieldSufix = 1 | |
| $monthName = "September" | |
| } | |
| 10 { | |
| $fieldSufix = 2 | |
| $monthName = "October" | |
| } | |
| 11 { | |
| $fieldSufix = 3 | |
| $monthName = "November" | |
| } | |
| 12 { | |
| $fieldSufix = 4 | |
| $monthName = "December" | |
| } | |
| 1 { | |
| $fieldSufix = 5 | |
| $monthName = "January" | |
| } | |
| 2 { | |
| $fieldSufix = 6 | |
| $monthName = "February" | |
| } | |
| 3 { | |
| $fieldSufix = 7 | |
| $monthName = "March" | |
| } | |
| 4 { | |
| $fieldSufix = 8 | |
| $monthName = "April" | |
| } | |
| 5 { | |
| $fieldSufix = 9 | |
| $monthName = "May" | |
| } | |
| 6 { | |
| $fieldSufix = 10 | |
| $monthName = "June" | |
| } | |
| 7 { | |
| $fieldSufix = 11 | |
| $monthName = "July" | |
| } | |
| 8 { | |
| $fieldSufix = 12 | |
| $monthName = "August" | |
| } | |
| Default { return; } | |
| } | |
| # Get the values from the pdf fields | |
| $name = ($Fields | Where-Object {$_.key -eq "Name"}).Value.GetValue().ToString() | |
| $place = ($Fields | Where-Object {$_.key -eq "$fieldPrefix-Place_$fieldSufix"}).Value.GetValue() ?? 0 | |
| $video = ($Fields | Where-Object {$_.key -eq "$fieldPrefix-Video_$fieldSufix"}).Value.GetValue() ?? 0 | |
| $hours = ($Fields | Where-Object {$_.key -eq "$fieldPrefix-Hours_$fieldSufix"}).Value.GetValue() ?? 0 | |
| $rv = ($Fields | Where-Object {$_.key -eq "$fieldPrefix-RV_$fieldSufix"}).Value.GetValue() ?? 0 | |
| $studies = ($Fields | Where-Object {$_.key -eq "$fieldPrefix-Studies_$fieldSufix"}).Value.GetValue() ?? 0 | |
| $remarks = ($Fields | Where-Object {$_.key -eq "Remarks$monthName$remarksSufix"}).Value.GetValue() ?? "" | |
| $monthReport = [PSCustomObject]@{ | |
| Place = [int]$place.ToString() | |
| Video = [int]$video.ToString() | |
| Hours = [int]$hours.ToString() | |
| RV = [int]$rv.ToString() | |
| Studies = [int]$studies.ToString() | |
| Hola = "hola" | |
| Remarks = $remarks.ToString() | |
| } | |
| # Maybe some card was not filled. | |
| If($monthReport.Hours -le 0) { | |
| Write-Warning "$name's card has no hours entered for $month/$serviceYear" | |
| } | |
| return $monthReport | |
| } catch { | |
| Write-Error $_.Exception.Message | |
| } | |
| finally { | |
| $PdfDoc.Close() | |
| } | |
| } | |
| function GetMonthTotals | |
| { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$cardsPath, | |
| [Parameter(Mandatory=$true)] | |
| [int]$serviceYear, | |
| [Parameter(Mandatory=$true)] | |
| [int]$month, | |
| [Parameter(Mandatory=$false)] | |
| [string]$remarksPattern | |
| ) | |
| $publishersTotalsItems = Get-ChildItem "$cardsPath/*.pdf" | ForEach-Object { GetPublisherMonthReport $_.FullName $serviceYear $month } | |
| $totalMonthReport = [PSCustomObject]@{ | |
| Place = 0 | |
| Video = 0 | |
| Hours = 0 | |
| RV = 0 | |
| Studies = 0 | |
| Count = [int]0 | |
| } | |
| ForEach ($item in $publishersTotalsItems) { | |
| If($remarksPattern -And ($item.Remarks -NotMatch $remarksPattern)) { | |
| continue | |
| } | |
| $totalMonthReport.Place += $item.Place | |
| $totalMonthReport.Video += $item.Video | |
| $totalMonthReport.Hours += $item.Hours | |
| $totalMonthReport.RV += $item.RV | |
| $totalMonthReport.Studies += $item.Studies | |
| $totalMonthReport.Count++ | |
| } | |
| return $totalMonthReport; | |
| } | |
| function Get-Totals | |
| { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [int]$serviceYear, | |
| [Parameter(Mandatory=$true)] | |
| [int]$month | |
| ) | |
| Install-IText | |
| $config = Get-Content -Raw -Path $PSScriptRoot/config.json | ConvertFrom-Json | |
| # The Auxiliar Pioners are identify by some word in the Remarks (ex. "AP") | |
| $remarksAPIdentifier = $config.RemarksAPIdentifier | |
| $pubsTotals = GetMonthTotals $config.PubsFolder $serviceYear $month "^((?!$remarksAPIdentifier).)*$" | |
| $apTotals = GetMonthTotals $config.PubsFolder $serviceYear $month "$remarksAPIdentifier" | |
| $rpTotals = GetMonthTotals $config.PRFolder $serviceYear $month | |
| Write-Host "Publishers Totals" | |
| Write-Host ($pubsTotals | Format-Table | Out-String) | |
| Write-Host "Auxiliar Pioners Totals" | |
| Write-Host ($apTotals | Format-Table | Out-String) | |
| Write-Host "Regular Pioners Totals" | |
| Write-Host ($rpTotals | Format-Table | Out-String) | |
| } | |
| Get-Totals $args[0] $args[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment