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

$page_security = 'SA_CHEQUE_PRINTING_TEMPLATES';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$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 . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/admin/db/cheque_printing_templates_db.inc");


$js = '';
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "Cheque Printing Templates"));
//------------------------------------------------------------------------------------------
simple_page_mode(true);

//if (isset($_GET['ModifyMRNumber']))
//{
//	  $_POST['ModifyMRNumber'] = $_GET['ModifyMRNumber'];
//}

function check_data()
{  
    // Page
    if(input_num('page_width') < 0.001){
        display_error( _("Page width should be greater than zero."));
        set_focus('page_width');
        return false;
    }
     
    if(input_num('page_length') < 0.001){
        display_error( _("Page length should be greater than zero."));
        set_focus('page_length');
        return false;
    }
    
    // Date
    if(input_num('full_date_y') > input_num('page_width')){
        display_error( _("Value can not be greater than page width."));
        set_focus('full_date_y');
        return false;
    }
     
    if(input_num('year_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('year_x');
        return false;
    }
    
    if(input_num('month_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('month_x');
        return false;
    }
    
    if(input_num('date_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('date_x');
        return false;
    }
    
    // Amount text    
    if(input_num('amount_text_y') > input_num('page_width')){
        display_error( _("Value can not be greater than page width."));
        set_focus('amount_text_y');
        return false;
    }
     
    if(input_num('amount_text_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('amount_text_x');
        return false;
    }
    
    
    // Amount value    
    if(input_num('amount_value_y') > input_num('page_width')){
        display_error( _("Value can not be greater than page width."));
        set_focus('amount_value_y');
        return false;
    }
     
    if(input_num('amount_value_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('amount_value_x');
        return false;
    }
    
    // Amount value    

    if(input_num('crossing_y') > input_num('page_width')){
        display_error( _("Value can not be greater than page width."));
        set_focus('full_date_y');
        return false;
    }

    if(input_num('crossing_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('crossing_x');
        return false;
    }
    
    
    // Amount value    
    if(input_num('account_name_y') > input_num('page_width')){
        display_error( _("Value can not be greater than page width."));
        set_focus('account_name_y');
        return false;
    }
     
    if(input_num('account_name_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('account_name_x');
        return false;
    }
    
    // Signatories   
    if(input_num('signatories_y') > input_num('page_width')){
        display_error( _("Value can not be greater than page width."));
        set_focus('signatories_y');
        return false;
    }
    
    if(input_num('signatory1_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('signatory1_x');
        return false;
    }
    
    if(input_num('signatory2_x') > input_num('page_length')){
        display_error( _("Value can not be greater than page length."));
        set_focus('signatory2_x');
        return false;
    }
    
    
    return true;    
}


function clear_data()
{
    unset($_POST['id']);
    unset($_POST['name']);
    unset($_POST['amount']);
    unset($_POST['account_code']);
}

//--------------------------------------------------------------------------------------------------

if(isset($_POST['Add']) && check_data()){
 
    if(!($_POST['font_size'] > 0)){
        $_POST['font_size'] = 9;
    }
    add_cheque_printing_templates( input_num('bank_id'), input_num('page_width'), input_num('page_length'), input_num('font_size'),
            
                                    input_num('full_date_y'), $_POST['year_format'], input_num('year_x'), input_num('year_width'),
                                    input_num('month_x'), input_num('month_width'),  input_num('date_x'), input_num('date_width'),
            
                                    input_num('amount_text_x'),  input_num('amount_text_y'), input_num('amount_text_width'), 
                                    input_num('amount_text_line_height'), input_num('amount_text_offset'), 
            
                                    input_num('amount_value_x'), input_num('amount_value_y'), 
            
                                    input_num('crossing_id'), input_num('crossing_x'), input_num('crossing_y'),
            
                                    input_num('account_name_x'), input_num('account_name_y'),
                                    
                                    input_num('company_name_x'), input_num('company_name_y'),
            
                                    input_num('signatories_y'), $_POST['signatory1_name'], input_num('signatory1_x'),
                                    $_POST['signatory2_name'], input_num('signatory2_x'));    
    $Mode = 'RESET';
    display_notification("New Cheque printing templates has been added.");
    clear_data();
    

}    
 
if(isset($_POST['Update']) && check_data()){
    
    if(!($_POST['font_size'] > 0)){
        $_POST['font_size'] = 9;
    }
    
    update_cheque_printing_templates(input_num('bank_id'), input_num('page_width'), input_num('page_length'), input_num('font_size'),
            
                                        input_num('full_date_y'), $_POST['year_format'], input_num('year_x'), input_num('year_width'),
                                        input_num('month_x'), input_num('month_width'),  input_num('date_x'), input_num('date_width'),
            
                                        input_num('amount_text_x'),  input_num('amount_text_y'), input_num('amount_text_width'), 
                                        input_num('amount_text_line_height'), input_num('amount_text_offset'), 
            
                                        input_num('amount_value_x'), input_num('amount_value_y'), 
            
                                        input_num('crossing_id'), input_num('crossing_x'), input_num('crossing_y'),
            
                                        input_num('account_name_x'), input_num('account_name_y'),
            
                                        input_num('company_name_x'), input_num('company_name_y'),
            
                                        input_num('signatories_y'), $_POST['signatory1_name'], input_num('signatory1_x'),
                                        $_POST['signatory2_name'], input_num('signatory2_x'));    

    $Mode = 'RESET';
    display_notification("Selected Cheque printing templates has been updated");
    clear_data();
    
 }

 
 if ($Mode == 'Delete')
{
    $recs = delete_cheque_printing_templates($selected_id);

    if($recs == 1){
        display_notification(_("Cheque printing templates has been deleted."));
    }
    else{
        display_error(_("Cheque printing templates could not be deleted."));
    }
    $Mode = 'RESET';
}

if (isset($_POST['cancel'])){
        //$Mode = 'RESET';
        $selected_id = -1;
        $sav = get_post('show_inactive');
	unset($_POST);
} 
//------------------------------------------------------------------------------------------------

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

//if (!@$_GET['popup'])	

start_form();

//inactive_control_row($th);

//---------------------------------------------------------------------------------------------

if(!isset($_POST['amount'])){
    $_POST['amount'] = 0;
}
    
if($selected_id > 0){
   $_POST['bank_id'] =  $selected_id;
   hidden('selected_id', -1);
}


start_table(TABLESTYLE_NOBORDER);
    value_list_cells("Bank:", 'bank_id', 'BANKS', null, 'Select a Bank', true);
end_table(1);



function display_edit_controls($bank_id, $myrow)
{
    if($myrow){
        $_POST['page_width'] = $myrow["page_width"];
        $_POST['page_length'] = $myrow["page_length"];
        $_POST['font_size'] = $myrow["font_size"];
        
        $_POST['full_date_y'] = $myrow['full_date_y'];
        $_POST['year_format'] = $myrow['year_format'];
        $_POST['year_x'] = $myrow["year_x"];
        $_POST['year_width'] = $myrow["year_width"];
        $_POST['month_x'] = $myrow["month_x"];
        $_POST['month_width'] = $myrow['month_width'];
        $_POST['date_x'] = $myrow["date_x"]; 
        $_POST['date_width'] = $myrow['date_width'];
        
        $_POST['amount_text_x'] = $myrow['amount_text_x'];
        $_POST['amount_text_y'] = $myrow['amount_text_y'];
        $_POST['amount_text_width'] = $myrow['amount_text_width'];
        $_POST['amount_text_line_height'] = $myrow['amount_text_line_height'];   
        $_POST['amount_text_offset'] = $myrow['amount_text_offset'];
        
        $_POST['amount_value_x'] = $myrow['amount_value_x'];
        $_POST['amount_value_y'] = $myrow['amount_value_y']; 
        
        $_POST['crossing_id'] = $myrow['crossing_id'];  
        $_POST['crossing_x'] = $myrow['crossing_x']; 
        $_POST['crossing_y'] = $myrow['crossing_y'];
        
        $_POST['account_name_x'] = $myrow['account_name_x'];
        $_POST['account_name_y'] = $myrow['account_name_y'];   
        
        $_POST['company_name_x'] = $myrow['company_name_x'];
        $_POST['company_name_y'] = $myrow['company_name_y'];   
        
        $_POST['signatories_y'] = $myrow['signatories_y'];  
        $_POST['signatory1_name'] = $myrow['signatory1_name']; 
        $_POST['signatory1_x'] = $myrow['signatory1_x'];
        $_POST['signatory2_name'] = $myrow['signatory2_name'];
        $_POST['signatory2_x'] = $myrow['signatory2_x'];        
    }
    else{
        $_POST['page_width'] = 0;
        $_POST['page_length'] = 0;
        $_POST['font_size'] = 9;
        $_POST['full_date_y'] = 0; 
        $_POST['year_format'] = 'YYYY'; 
        $_POST['year_x'] = 0;
        $_POST['year_width'] = 0;
        $_POST['month_x'] = 0;
        $_POST['month_width'] = 0;
        $_POST['date_x'] = 0; 
        $_POST['date_width'] = 0;
        
        $_POST['amount_text_x'] = 0;
        $_POST['amount_text_y'] = 0;
        $_POST['amount_text_width'] = 0;
        $_POST['amount_text_line_height'] = 0;   
        $_POST['amount_text_offset'] = 0;          
        $_POST['amount_value_x'] = 0;
        $_POST['amount_value_y'] = 0;
//        $_POST['instruction_id'] = 0;  
        $_POST['crossing_id'] = 0;  
        $_POST['crossing_x'] = 0;  
        $_POST['crossing_y'] = 0;
        
        $_POST['account_name_x'] = 0;
        $_POST['account_name_y'] = 0;   
        
        $_POST['company_name_x'] = 0;
        $_POST['company_name_y'] = 0;  
        
        $_POST['signatories_y'] = 0;  
        $_POST['signatory1_name'] = "";  
        $_POST['signatory1_x'] = 0;
        $_POST['signatory2_name'] = "";
        $_POST['signatory2_x'] = 0;   
    }
         
    
    start_table();
    start_row();
    $stock_img_link = "<img id='Cheque_Printing_Template.img' alt = '[Cheque_Printing_Template.jpg".
			"]' src='".company_path().'/images/'.item_img_name('Cheque_Printing_Template').
			".jpg?nocache=".rand()."'"." height='350' border='0'>";

	label_cell($stock_img_link);
    end_row();
    end_table();
    
    br(2);
    
    display_note("All lengths are in millimeters.");

    start_outer_table(TABLESTYLE2);

	table_section(1);
    table_section_title("Page");
    amount_row("Page Width:", 'page_width', null, null, null, 1);
    amount_row("Page Length:", 'page_length', null, null, null, 1);  
    amount_row("Font Size:", 'font_size', null, null, null, 0);  
    
    table_section_title("Date");
    amount_row("Full Date Y:", 'full_date_y', null, null, null, 1);
    
    $values_array['YYYY'] = 'YYYY';
    $values_array['YY'] = 'YY';
    custom_values_list_row("Year Format:", 'year_format', $values_array);
    amount_row("Year X:", 'year_x', null, null, null, 1);
    amount_row("Year Width:", 'year_width', null, null, null, 1);
    amount_row("Month X:", 'month_x', null, null, null, 1);
    amount_row("Month Width:", 'month_width', null, null, null, 1);
    amount_row("Date X:", 'date_x', null, null, null, 1);
    amount_row("Date Width:", 'date_width', null, null, null, 1);
    
    table_section_title("Crossing");
    value_list_row("Default Crossing:", 'crossing_id', 'CHEQUE_CROSSINGS', null, '');
    amount_row("Crossing X:", 'crossing_x', null, null, null, 1);
    amount_row("Crossing Y:", 'crossing_y', null, null, null, 1);
    
    table_section_title("Account Name");
    amount_row("Account Name X:", 'account_name_x', null, null, null, 1);
    amount_row("Account Name Y:", 'account_name_y', null, null, null, 1); 
    
    
    table_section(2);

    table_section_title("Amount In Words");
    amount_row("Amount Text X:", 'amount_text_x', null, null, null, 1);
    amount_row("Amount Text Y:", 'amount_text_y', null, null, null, 1);    
    amount_row("Amount Text Width:", 'amount_text_width', null, null, null, 1);
    amount_row("Amount Text Line Height:", 'amount_text_line_height', null, null, null, 1);   
    amount_row("Amount Text Offset:", 'amount_text_offset', null, null, null, 1); 
    
    table_section_title("Amount In Numbers");
    amount_row("Amount Value X:", 'amount_value_x', null, null, null, 1);
    amount_row("Amount Value Y:", 'amount_value_y', null, null, null, 1);   
   // amount_row("Instruction ID:", 'instruction_id', null, null, null, 0);
    
    table_section_title("Company Name");
    amount_row("Company Name X:", 'company_name_x', null, null, null, 1);
    amount_row("Company Name Y:", 'company_name_y', null, null, null, 1); 
    
    
    table_section_title("Signatories");
    amount_row("Signatories Y:", 'signatories_y', null, null, null, 1);  
    text_row("Signatory1 Name", 'signatory1_name', null, 30, 50);
    amount_row("Signatory1 X:", 'signatory1_x', null, null, null, 1);
    text_row("Signatory2 Name", 'signatory2_name', null, 30, 50);
    amount_row("Signatory2 X:", 'signatory2_x', null, null, null, 1);
    
    
    end_table(1);
    
    end_outer_table(1);

}

$myrow = get_cheque_printing_template($_POST['bank_id']);

display_edit_controls($_POST['bank_id'], $myrow);

if($_POST['bank_id'] > 0){
    if($myrow){
        submit_center_first('Update', _("Update"), '', 'default');
        submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
    }
    else{        
        submit_center_first('Add', _("ADD NEW"), '', 'default');
        submit_center_last('cancel', _("Cancel"), _("Cancel"), 'cancel');

    }
}




if (!@$_GET['popup'])
{
    end_form();
    end_page(@$_GET['popup'], false, false);        
}



?>
