<?php
/*********************************************************************************
 * The contents of this file are subject to the TimeTrex Public License Version
 * 1.1.0 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.TimeTrex.com/TPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by TimeTrex" logo and
 *    (ii) the TimeTrex copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: TimeTrex Open Source
 * The Initial Developer of the Original Code is TimeTrex Payroll Services
 * Portions created by TimeTrex are Copyright (C) 2004-2007 TimeTrex Payroll Services;
 * All Rights Reserved.
 *
 ********************************************************************************/
/*
 * $Revision: 1255 $
 * $Id: export_pay_stubs.php 1255 2007-09-21 21:41:37Z ipso $
 * $Date: 2007-09-21 14:41:37 -0700 (Fri, 21 Sep 2007) $
 */
require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'global.inc.php');
require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR .'..'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'CLI.inc.php');

//
//
// Main
//
//
if ( $argc <= 1 OR in_array ($argv[1], array('--help', '-help', '-h', '-?') ) ) {
	$help_output = "Usage: export_pay_stubs.php [OPTIONS] [Output Directory]\n";
	$help_output .= "  Options:\n";
	$help_output .= "    -c [Company ID]			Defaults to 1\n";
	$help_output .= "    -t [Pay Period Date Type]		Start/End/Transaction/Last\n";
	$help_output .= "                              		Last will use the latest pay period\n";
	$help_output .= "    -d [Pay Period Date]		Format: YYYYMMDD\n";

	echo $help_output;

} else {
	//FIXME: Use Pears Console_GetArgs package to handle these better.

	//Handle command line arguments
	$last_arg = count($argv)-1;

	if ( in_array('-c', $argv) ) {
		$company_id = strtolower( trim($argv[array_search('-c', $argv)+1]) );
	} else {
		$company_id = 1;
	}

	if ( in_array('-t', $argv) ) {
		$date_type = strtolower( trim($argv[array_search('-t', $argv)+1]) );
	} else {
		$date_type = 'last';
		echo "Pay Period Date Type not specified, assuming: Last\n";
	}

	$pay_period_date = NULL;
	if ( in_array('-d', $argv) ) {
		$pay_period_date = TTDate::getBeginDayEpoch( strtotime( trim($argv[array_search('-d', $argv)+1]) ) );
	} else {
		echo "Pay Period Date not specified, assuming: Last\n";
	}

	if ( isset($argv[$last_arg]) AND $argv[$last_arg] != '' ) {
		if ( !file_exists( $argv[$last_arg] ) OR !is_writable( $argv[$last_arg] ) ) {
			echo "Output Directory: ". $argv[$last_arg] ." does not exists or is not writable!\n";
			exit;
		} else {
			$output_directory = $argv[$last_arg];
		}
	}

	if ( $date_type != 'last' ) {
		echo "Searching for Pay Period ". ucfirst($date_type) ." Date: ". TTDate::getDate('DATE', $pay_period_date ) ."...\n";
	} else {
		echo "Searching for Last Pay Period...\n";
	}

	$pplf = new PayPeriodListFactory();
	$pplf->getPayPeriodsWithPayStubsByCompanyId( $company_id, NULL, array('a.start_date' => 'desc') );
	if ( $pplf->getRecordCount() > 0 ) {
		$x=0;
		$found_pay_period = FALSE;
		foreach( $pplf as $pp_obj ) {

			if ( $date_type == 'start' AND TTDate::getBeginDayEpoch($pp_obj->getStartDate()) == $pay_period_date ) {
				$found_pay_period = TRUE;
			} elseif ( $date_type == 'end' AND TTDate::getBeginDayEpoch($pp_obj->getEndDate()) == $pay_period_date ) {
				$found_pay_period = TRUE;
			} elseif ( $date_type == 'transaction' AND TTDate::getBeginDayEpoch($pp_obj->getTransactionDate()) == $pay_period_date ) {
				$found_pay_period = TRUE;
			} elseif ( $date_type == 'last' ) {
				//Last pay period
				$found_pay_period = TRUE;
			}

			if ( $found_pay_period == TRUE ) {
				echo "Found Pay Period: Start: ". TTDate::getDate('DATE', $pp_obj->getStartDate() ) .' End: '. TTDate::getDate('DATE', $pp_obj->getEndDate() ) .' Transaction: '. TTDate::getDate('DATE', $pp_obj->getTransactionDate() ) ."\n";
				$pay_period_id = $pp_obj->getId();
				break;
			}

			$x++;
		}
	}

	if ( isset($pay_period_id) ) {
		$pslf = new PayStubListFactory();
		$pslf->getByCompanyIdAndPayPeriodId( $company_id, $pay_period_id );
		if ( $pslf->getRecordCount() > 0 ) {
			echo "Export Directory: ". $output_directory ."\n";
			$i=1;
			foreach( $pslf as $tmp_ps_obj ) {
				$pslf_b = new PayStubListFactory();
				$pslf_b->getById( $tmp_ps_obj->getId() );
				if ( $pslf_b->getRecordCount() > 0 ) {
					$ps_obj = $pslf_b->getCurrent();

					if ( is_object( $ps_obj->getUserObject() ) ) {
						$file_name = $output_directory . DIRECTORY_SEPARATOR . 'pay_stub_'. $ps_obj->getUserObject()->getUserName() .'_'. date('Ymd', $ps_obj->getStartDate() ) .'.pdf';

						$output = $pslf->getPayStub( $pslf_b, TRUE );
						if ( $output !== FALSE ) {
							echo "  $i. Exporting Pay Stub for: ". $ps_obj->getUserObject()->getFullName(). "\t\tFile: ". $file_name ."\n";

							file_put_contents( $file_name, $output );
							unset($output);

							$i++;
						} else {
							echo "  ERROR: Unable to Export Pay Stub.\n";
						}
					}
				}
			}
		} else {
			echo "ERROR: No Pay Stubs Found in Pay Period!\n";
		}
	} else {
		echo "ERROR: No Pay Period Found Matching that Date!!\n";
	}

	echo "Done!\n";
}
//Debug::Display();
?>