<?php
/**********************************************************************

	Released under the terms of the GNU General Public License, GPL,
***********************************************************************/
$page_security = 'SA_CREATE_GATE_PASS';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Stock Check Sheet
// ----------------------------------------------------------------
$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/services/includes/ui/job_order_entry_ui.inc"); 
include_once($path_to_root . "/assets/includes/db/assets_db.inc"); 

print_dialy_closed_jobs_report();

function rep_get_daily_closed_jobs($from_date, $to_date, $department_id, $section_id) { // this query is used temporary, until wms officers closed jobs from job cards
    $to_date_2 = "$to_date 23:59:59";
    
    $sql = "SELECT jo.*, dep.value_name department_name, sec.value_name section_name, usr.real_name issusing_officer,
                gp.id exit_pass_no, gp.printed_on 
            FROM ".TB_PREF."job_gate_passes gp  
                JOIN ".TB_PREF."job_orders jo 
                    ON gp.jo_id = jo.id 
                JOIN ".TB_PREF."list_values dep  
                    ON jo.department_id = dep.id 
                        AND dep.list_code = 'DEPARTMENTS' 
                JOIN ".TB_PREF."list_values sec  
                    ON jo.section_id = sec.id 
                JOIN ".TB_PREF."users usr   
                    ON jo.created_by = usr.id 
            WHERE gp.printed_on >= ".db_escape($from_date)."    
                AND gp.printed_on <= ".db_escape($to_date_2);
    
        if ($department_id > 0) {
            $sql .= " AND jo.department_id = ".db_escape($department_id);
        }
    
        if ($section_id > 0) {
            $sql .= " AND jo.section_id = ".db_escape($section_id);
        }
    
        $sql .= "ORDER BY gp.printed_on ASC";
        
        return db_query($sql, "rep_get_daily_closed_jobs");
}

function print_dialy_closed_jobs_report() {
    global $path_to_root;

    	$from_date_ = $_POST['PARAM_0'];
    	$to_date_ = $_POST['PARAM_1'];
    	$department_id = $_POST['PARAM_2'];
    	$section_id = $_POST['PARAM_3'];
        $orientation = 'L';

        include_once($path_to_root . "/reporting/includes/pdf_report.inc");
        
        $department = 'All';
        if ($department_id > 0) {
            $department = get_listvalue_name_by_id($department_id);
        }
        
        $section = 'All';
        if ($section_id > 0) {
            $section = get_listvalue_name_by_id($section_id);
        }

        $cols = array($x=0, $x+=40, $x+=50, $x+=140, $x+=135, $x+=50, $x+=130, $x+=90, $x+=50, $x+=90);
        $headers = array(_('Job Card'), _('Job Date'), _('Vehicle/ Tool/ Location'), _('Req. Department'), _('Department'), _('Section'), _('Job Created On'), 
                         _('Exit Pass #'), _('Exit Date'));
        $aligns = array('center', 'center', 'left', 'left', 'left', 'left', 'center', 'center', 'center');

    	$params = array(0 => $from_date_, 1 => $to_date_, 2 => $department, 3 => $section);

   	$rep = new FrontReport(_('Daily Closed Jobs Report'), "DailyClosedJobsReport", 'A4', 9, $orientation);
        
        $rep->SetHeaderType('form_657');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
        
        $from_date = date2sql($from_date_);
        $to_date = date2sql($to_date_);
        $req_deps_array = create_request_departmens_array();
        
        $total_job_cards = 0;

        $result = rep_get_daily_closed_jobs($from_date, $to_date, $department_id, $section_id);
            while ($row = db_fetch($result)) {
		$rep->TextCol(0, 1, $row['id']);
		$rep->TextCol(1, 2, sql2date($row['date_']), -3);
		$rep->TextCol(2, 3, get_asset_formatted_name($row['vehicle_id']), -3);
		$rep->TextCol(3, 4, $req_deps_array[$row['req_department_id']], -3);
		$rep->TextCol(4, 5, $row['department_name'], -3);
		$rep->TextCol(5, 6, $row['section_name'], -3);
		$rep->TextCol(6, 7, $row['created_on']);
		$rep->TextCol(7, 8, $row['exit_pass_no']);
		$rep->TextCol(8, 9, $row['printed_on']); 
                
		$rep->NewLine();
                
                $total_job_cards++;
            }
            
            if ($total_job_cards > 0) {
                $rep->Line($rep->row - 8);
                $rep->NewLine(2);
                $rep->TextCol(8, 9, _('Total Jobs: '.$total_job_cards));
                $rep->Line($rep->row  - 8);
                $rep->NewLine(2);
            } else {
                $rep->NewLine();
            }
        
        $rep->End();
}

?>