Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Last active May 3, 2018 23:58
Show Gist options
  • Save rayterrill/731d53f119d4a0c78baaf6a840d3c012 to your computer and use it in GitHub Desktop.
Save rayterrill/731d53f119d4a0c78baaf6a840d3c012 to your computer and use it in GitHub Desktop.
Param(
[Parameter(Mandatory=$True,Position=1)][string]$pdfFile,
[Parameter(Mandatory=$True,Position=2)][string]$eventFile
)
#uses cPDF http://community.coherentpdf.com/
#the pdf file, cpdf, and the event file should be in the current directory
#clean up any previously split ticket pdfs
#splits a pdf $pdfFile by pages into files matching Ticket001.pdf, etc in the same directory
./cpdf -split $pdfFile -o Ticket%%%.pdf
function Send-Gmail($EmailTo, $Subject, $attachments) {
$msg = new-object Net.Mail.MailMessage
$msg.From = "EMAIL ADDRESS GOES HERE"
$msg.To.Add($EmailTo)
$msg.Subject = $Subject
$msg.Body = "Attached are your tickets for the match. DO NOT LOSE THESE TICKETS."
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("EMAIL-ADDRESS-GOES-HERE", "APP-PASSWORD-GOES-HERE");
foreach ($a in $attachments) {
Write-Host "$($a)"
$att = new-object Net.Mail.Attachment($a)
$msg.Attachments.Add($a)
}
$SMTPClient.Send($msg)
$SMTPClient.Dispose()
}
function Find-Tickets($ticketPointer, $numberOfTickets) {
Write-Host "Ticker pointer is $($ticketPointer). Number of tickets is $($numberOfTickets)"
$currentLocation = pwd
$tickets = @()
for ($i=1; $i -le $numberOfTickets; $i++) {
$ticketNumber = $i + $ticketPointer
if ($ticketNumber -lt 10) {
$tickets += "$($currentLocation.Path)\Ticket00$($ticketNumber).pdf"
} elseif ($ticketNumber -ge 10 -AND $ticketNumber -lt 100) {
$tickets += "$($currentLocation.Path)\Ticket0$($ticketNumber).pdf"
} else {
$tickets += "$($currentLocation.Path)\Ticket$($ticketNumber).pdf"
}
}
return $tickets
}
#pull in the ticket csv into an array
$tickets = Import-CSV $eventFile
#only look at items where the status is "Paid" (i.e. not Cancelled, etc)
$tickets = $tickets | Where-Object {$_."Payment state" -eq "Paid"}
#get the unique invoice numbers
$invoiceNumbers = $tickets."Invoice #" | Get-Unique
$totalTickets = 0
foreach ($i in $invoiceNumbers) {
$numberOfTickets = ($tickets | Where-Object {$_."Invoice #" -eq $i}).Count
if (-not $numberOfTickets) { $numberOfTickets = 1 } #no count when there's only 1 ticket
$personToEmail = $tickets | Where-Object {$_."Invoice #" -eq $i -AND $_."Total fee incl. extra costs and guests registration fees" -ne ""}
$ticketsToAttach = Find-Tickets -TicketPointer $totalTickets -NumberOfTickets $numberOfTickets
Write-Host "$($personToEmail.Email) - $($numberOfTickets) - $($ticketsToAttach)"
Send-Gmail -EmailTo $personToEmail.Email -Subject "Timbers vs Minnesota 6/21 - Tickets" -Attachments $ticketsToAttach
$totalTickets = $totalTickets + $numberOfTickets
}
Write-Host "Total Tickets - $($totalTickets)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment