Splits paychecks and sends them by email to employees
# split the big payolls pdf in segments of two pages
#set( $paychecks = $SplitTool.splitSelection($document.id, '1-2,3-4,5-6,7-8', 'admin'))
# iterate over the segments, each segment is the paycheck of a single employee
#foreach( $paycheck in $paychecks )
# parse the segment body and save it in the paycheckBody variable
#set( $paycheckBody = $ContextTool.getBean('DocumentManager').parseDocument($paycheck, null) )
# inspect the body of the segment to understand what is the employee
# in order to rename the segment file and define the email to send the paycheck to
#if($paycheckBody.toUpperCase().contains("JOHN DOE") )
#set( $newname = $document.title + "-john.pdf")
#set( $recipient = "john[at]acme.com")
#elseif($paycheckBody.toUpperCase().contains("MARIO ROSSI") )
#set( $newname = $document.title + "-mario.pdf")
#set( $recipient = "mario[at]acme.com")
#elseif( $paycheckBody.toUpperCase().contains("ELENA BORGHI") )
#set( $newname = $document.title + "-elena.pdf")
#set( $recipient = "elena[at]acme.com")
#elseif( $paycheckBody.toUpperCase().contains("ALDO MORO") )
#set( $newname = $document.title + "-aldo.pdf")
#set( $recipient = "aldo[at]acme.com")
#end
# rename the current segment(the user's paycheck pdf)
$paycheck.setFileName($newname);
$DocTool.store($paycheck);
# notify the employee sending him his paycheck document
$MailTool.sendDocument($paycheck, null, $recipient, "Acme Paycheck", "Dear employee, please find your new paycheck in attachment of this email.
Remember to save it in your records
Best Regards
Acme paycheck department");
#end
This script works on the currently selected document referenced by the variable document that is considered a big PDF containing all the paychecks of a months.
The document is splitted in segments of two pages each segment is saved as a single payroll PDF and it's content is inspected to discover what is the employee the payroll belongs to in order to send it to him by email.