<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_PAYMENTS_ENTRY';
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/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/approve_i_owe_you_enties_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_payments_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_floats_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_expenses_entry_db.inc");
include_once($path_to_root . "/petty_cash/includes/ui/petty_cash_ui.inc");
include_once($path_to_root . "/petty_cash/includes/ui/petty_cash_payments_entry_ui.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_expenses_db.inc");
include_once($path_to_root . "/petty_cash/includes/petty_cash_functions.inc");
include_once($path_to_root . "/petty_cash/includes/db/project_floats_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_advances_inquiry_db.inc");

include_once($path_to_root . "/includes/approval_functions.inc");

include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");

include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");


        
        
$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Petty Cash Payments Entry"), false, false, "", $js);

//----------------------------------------------------------------------------------------
simple_page_mode(true);

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

if(isset($_POST['ModifyMRNumber']))
{
    $petty_cash_id=$_POST['ModifyMRNumber'] ;
}
else
{    
    if(isset($_POST['petty_cash_id'])){
        $petty_cash_id=$_POST['petty_cash_id'] ; 
    }
}

//if (isset($_GET['IOU_ID'])) 
//{
//	//return;
//        
//    $trans_no = $_GET['IOU_ID'];
//	$trans_type = ST_I_OWE_YOU;
//    $mf_message = get_session_variable('iou_batch_messages');
//    clear_session_variable('iou_batch_messages');
//    
//	display_notification_centered($mf_message);
//        
//	display_note(print_document_link($trans_no, _("&Print this I Owe You"), true, $trans_type), 0, 0);
//
//	hyperlink_no_params("$path_to_root/petty_cash/i_owe_you_payments_entry_select.php", _("Make a payment for another I Owe You"));
//
////	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
//
//	display_footer_exit();
//}

//if (isset($_GET['IOU_Batch_ID'])) 
//{
//	//return;
//        
//    $trans_no = $_GET['IOU_Batch_ID'];
//	$trans_type = ST_I_OWE_YOU_BATCH;
//    $mf_message = get_session_variable('iou_batch_messages');
//    clear_session_variable('iou_batch_messages');
//    
//	display_notification_centered($mf_message);
//        
//	display_note(print_document_link($trans_no, _("&Print this I Owe You Batch"), true, $trans_type), 0, 0);
//
//	hyperlink_no_params("$path_to_root/petty_cash/i_owe_you_payments_entry_select.php", _("Make a payment for another I Owe You"));
//
////	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
//
//	display_footer_exit();
//}


if (isset($_GET['Payment_ID'])) 
{
	//return;
        
    $trans_no = $_GET['Payment_ID'];
	$trans_type = ST_I_OWE_YOU_PAYMENT;
    $mf_message = get_session_variable('iou_batch_messages');
    clear_session_variable('iou_batch_messages');
    
	display_notification_centered($mf_message);
        
	display_note(print_document_link($trans_no, _("&Print this I Owe You Payment"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/petty_cash/i_owe_you_payments_entry_select.php", _("Make a payment for another Petty Cash Expense"));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}







//function display_i_owe_you_batch_header($batch_no)
//{
//    
//}


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


if ($Mode == 'ADD_ITEM') 
{
    $ap = add_petty_cash_payment($petty_cash_id, $selected_id);
    if($ap["success"] === true){
        $Mode = 'RESET';
        display_notification($ap["message"]);
    }
    else{
        if(isset($ap["focus_to"])){
            set_focus($ap["focus_to"]);
        }
        display_error($ap["message"]);
    }
} 


if ($Mode == 'UPDATE_ITEM') 
{
    $up = update_petty_cash_payment($petty_cash_id, $selected_id);
    if($up["success"] === true){
        $Mode = 'RESET';
        $selected_id = -1;
        display_notification($up["message"]);
    }
    else{
        if(isset($up["focus_to"])){
           set_focus($up["focus_to"]);
        }
        display_error($up["message"]);
    }
}
//--------------------------------------------------------------------------------------------------


if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

if(isset($_POST['Add_Make_Payment'])){
    $ap = add_petty_cash_payment($petty_cash_id, $selected_id);
    if($ap["success"] !== true){
        if(isset($ap["focus_to"])){
            set_focus($ap["focus_to"]);
        }
        display_error($ap["message"]);
    }
    else{
        $mp = make_petty_cash_payment($petty_cash_id, $ap["trans_no"]);
        if($mp["success"] !== true){
            display_error($ap["message"]);
        }
        else{
            $Mode = 'RESET';
            $message = $mp["message"];
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$ap["trans_no"]);  
        }  
    }
}


if(isset($_POST['Update_Make_Payment'])){
    $up = update_petty_cash_payment($petty_cash_id, $selected_id);
    if($up["success"] !== true){
        if(isset($up["focus_to"])){
            set_focus($up["focus_to"]);
        }
        display_error($up["message"]);
    }
    else{
        $mp = make_petty_cash_payment($petty_cash_id, $up["trans_no"]);
        if($mp["success"] !== true){
            display_error($mp["message"]);
        }
        else{
            $Mode = 'RESET';
            $message = $mp["message"];
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$selected_id);  
        }
    }
}


if(isset($_POST['Complete_Batch'])){
    $cpceb = complete_petty_cash_expenses_batch($petty_cash_id);
    if($cpceb["success"] !== true){
        display_error($cpceb["message"]);
    }
    else{
        $Mode = 'RESET';
        $message = $cpceb["message"];
        set_session_variable('iou_batch_messages', $message);
        meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$selected_id);  
    }    
}


if(isset($_POST['Keep_Balance_Complete_Batch'])){
    $cpceb = complete_petty_cash_expenses_batch($petty_cash_id, true);
    if($cpceb["success"] !== true){
        display_error($cpceb["message"]);
    }
    else{
        $Mode = 'RESET';
        $message = $cpceb["message"];
        set_session_variable('iou_batch_messages', $message);
        meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$selected_id);  
    }    
}


if ($Mode == 'Delete')
{
    $dpap_msg = delete_petty_cash_payment($petty_cash_id, $selected_id);
    if($dpap_msg['success'] !== true){        
        display_error($dpap_msg['message']);
    }
    else{
        display_notification($dpap_msg['message']);
        $Mode = 'RESET';    
    }	
}

if ($Mode == 'RESET')
{
   $selected_id = -1;
}


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

start_form();

$batch_no = $total_paid = 0;

display_petty_cash_header($petty_cash_id);


$total_paid += display_petty_cash_payments(ST_PETTY_CASH, $petty_cash_id, true);
    
$pch = get_petty_cash_expenses_header($petty_cash_id);

if(isset($pch['batch_no'])){
    $batch_no = $pch['batch_no'];
}

if($batch_no > 0){
    display_petty_cash_batch_expenses($batch_no);
}
else{
    display_note("No expences found.");
    br();
}



if($pch['project_id'] > 0 ){
    display_project_float_message($pch['project_id'], $pch['company_code'], $pch['employee_code']);
}

br();
//display_i_owe_you_payments($petty_cash_id);

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

    $_POST['id'] = 0;
    //$_POST['expence_description'] = "";
    $_POST['payment_date'] = ""; 
    $_POST['is_return'] ="";
    $_POST['amount'] = "";
    $_POST['payment_comment'] ="";
  
        
if($selected_id > 0 ){
    if ($Mode =='Edit'){
        $id = $selected_id;

        $myrow = get_i_owe_you_payment($id);       
   
        $_POST['id'] = $myrow["id"];       
        $_POST['payment_date'] = sql2date($myrow["payment_date"]);
        $_POST['is_return'] = $myrow["is_return"];
        $_POST['amount'] = $myrow["amount"];
        if(($myrow['is_return']) == 1){
            $amount = $_POST['amount'];   
            $amount *= -1;        
            $_POST['amount'] = $amount;  
        }
        $_POST['payment_comment'] = $myrow["comment"]; 
        hidden('id', $_POST['id']);
    }
            

    hidden('selected_id', $selected_id);
    hidden('petty_cash_id', $petty_cash_id);

    start_table(TABLESTYLE2);

    label_row("Petty Cash No:", $petty_cash_id);
        
    if($_POST['is_return'] ==  true){
        $is_return_name = "Return";
    }
    else{
        $is_return_name = "Pay";
    }
    label_row("Type", $is_return_name);
    hidden('is_return', $_POST['is_return']);
    
    date_row("Date:", "payment_date"); 
    
    amount_row('Amount:', 'amount', $_POST['amount'], null, null, 2);
    textarea_row("Comment:", "payment_comment",null,30,3);
    end_table(1);
    br();
    
    
    submit_add_or_update_center($selected_id == -1, '', 'both');
    br();
    submit_center_first('Update_Make_Payment', _("Update Make Payment (Complete)"), '', 'default');

}    
else{
    hidden('selected_id', $selected_id);
    hidden('petty_cash_id', $petty_cash_id);

    start_table(TABLESTYLE2);
    //value_list_row("Expense Type:", "expense_type_id", "PETTY_CASH_EXPENSE_TYPES", null, '');
    label_row("Petty Cash No:", $petty_cash_id);
//    batch_i_owe_you_list_row("I Owe You:","i_owe_you_id", $batch_no, $petty_cash_id, $selected_id, "Select an I Owe you");
    yesno_list_row("Type", 'is_return', $_POST['is_return'], $name_yes='return', $name_no='add');
    date_row("Date:", "payment_date");
    
    amount_row('Amount:', 'amount', $_POST['amount'], null, null, 2);
    textarea_row("Comment:", "payment_comment",null,30,3);
    end_table(1);

    br();
    submit_add_or_update_center($selected_id == -1, '', 'both');
    
    br();
    submit_center_first('Add_Make_Payment', _("Add and Make Payment (Complete)"), '', 'default');
    
}

br(2);
    submit_center_first('Complete_Batch', _("Complete Batch"), '', 'default');
    submit_center_last('Keep_Balance_Complete_Batch', _("Keep Balance with Project Folat and Complete Batch"), '', 'default');
    
br();

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


?>
