<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTY_CASH_PAYMENTS_ENTRY';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

include_once($path_to_root . "/includes/ui/items_cart.inc");
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/i_owe_you_expenses_db.inc");
include_once($path_to_root . "/petty_cash/includes/ui/petty_cash_ui.inc");
include_once($path_to_root . "/petty_cash/includes/ui/i_owe_you_payments_entry_ui.inc");
include_once($path_to_root . "/petty_cash/includes/ui/i_owe_you_bank_payments_entry_ui.inc");
include_once($path_to_root . "/petty_cash/includes/db/project_floats_db.inc");
include_once($path_to_root . "/petty_cash/includes/petty_cash_functions.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_advances_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_advances_inquiry_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_banking.inc");
include_once($path_to_root . "/inventory/includes/db/petty_cash_item_receive_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/transport/includes/db/vehicles_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_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 = "I Owe You Payments Entry"), false, false, "", $js);

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

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

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

global $systypes_array;


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);

	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 = $_GET['Pay_type'];
        $mf_message = get_session_variable('iou_batch_messages');
        clear_session_variable('iou_batch_messages');
    
	display_notification_centered($mf_message);
        
        if($trans_type > 0){
            display_note(print_document_link($trans_no, _("&Print this ".$systypes_array[$trans_type]), 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();
}







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


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


if ($Mode == 'ADD_ITEM' && $_POST['payment_method'] == 0) 
{
    $ap = add_iou_payment($iou_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' && $_POST['payment_method'] == 0) 
{
    $up = update_iou_payment($iou_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 ($Mode == 'Delete' && $_POST['payment_method'] == 0)
{
        $diou_msg = delete_iou_payment_entry($iou_id, $selected_id);
        
        if($diou_msg["success"] !== true){
            display_error($diou_msg["message"]);                
        }
        else{
            display_notification($diou_msg["message"]);
            $Mode = 'RESET';
        }

	
}

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


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

if(isset($_POST['Add_Make_Payment'])){
    $ap = add_iou_payment($iou_id, $selected_id);
    if($ap["success"] !== true){
        if(isset($ap["focus_to"])){
            set_focus($ap["focus_to"]);
        }
        display_error($ap["message"]);
    }
    else{
        begin_transaction();
        if($_POST['payment_method'] == 1){
            if($_POST['is_return'] == 1){
                $pay_type = ST_IOU_BANKPAYMENT_RETURN;
            }
            else{
                $pay_type = ST_IOU_BANKPAYMENT;
            }
            $mp = add_iou_bank_payment($iou_id, $selected_id);
        }else{
            $mp = make_iou_payment($iou_id, $ap["trans_no"]);
            $pay_type = ST_I_OWE_YOU_PAYMENT;
        }
        
        if($mp["success"] !== true){
            cancel_transaction();
            display_error($mp["message"]);
        }
        else{
            commit_transaction();
            $Mode = 'RESET';
            if(input_num('bank_account') > 0){
                $message = ($systypes_array[$pay_type]." #".$mp["trans_no"]." has been created.");
            }else{
                $message = ("Payment ".$mp["trans_no"]." has been created and released.");
            }
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$mp["trans_no"]."&Pay_type=".$pay_type);  
        }  
    }
}



if(isset($_POST['Add_Make_Payment_Close_IOU_For_Payments'])){
    $ap = add_iou_payment($iou_id, $selected_id);
    if($ap["success"] !== true){
        if(isset($ap["focus_to"])){
            set_focus($ap["focus_to"]);
        }
        display_error($ap["message"]);
    }
    else{
        begin_transaction();
        if($_POST['payment_method'] == 1){
            if($_POST['is_return'] == 1){
                $pay_type = ST_IOU_BANKPAYMENT_RETURN;
            }
            else{
                $pay_type = ST_IOU_BANKPAYMENT;
            }
            $mp = add_iou_bank_payment($iou_id, $selected_id);
        }else{
            $mp = make_iou_payment($iou_id, $ap["trans_no"]);
            $pay_type = ST_I_OWE_YOU_PAYMENT;
        }
        if($mp["success"] !== true){
            cancel_transaction();
            display_error($mp["message"]);
        }
        else{
            close_i_owe_you_for_payments($iou_id);
            commit_transaction();
            $Mode = 'RESET';
            if(input_num('bank_account') > 0){
                $message = ($systypes_array[$pay_type]." #".$mp["trans_no"]." has been created and I Owe You ".$iou_id." has been closed for payments.");
            }else{
                $message = ($systypes_array[$pay_type]." #".$selected_id." has been released and I Owe You ".$iou_id." has been closed for payments.");
            }
            display_notification($message);   
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$mp["trans_no"]."&Pay_type=".$pay_type);  
        }
    }
}



if(isset($_POST['Add_Make_Payment_Complete_Batch'])){
    $ap = add_iou_payment($iou_id, $selected_id);
    if($ap["success"] !== true){
        if(isset($ap["focus_to"])){
            set_focus($ap["focus_to"]);
        }
        display_error($ap["message"]);
    }
    else{
        begin_transaction();
        if($_POST['payment_method'] == 1){
            if($_POST['is_return'] == 1){
                $pay_type = ST_IOU_BANKPAYMENT_RETURN;
            }
            else{
                $pay_type = ST_IOU_BANKPAYMENT;
            }
            $mp = add_iou_bank_payment($iou_id, $selected_id);
        }else{
            $mp = make_iou_payment($iou_id, $ap["trans_no"]);
            $pay_type = ST_I_OWE_YOU_PAYMENT;
        }
        if($mp["success"] !== true){
            cancel_transaction();
            display_error($ap["message"]);
        }
        else{
            commit_transaction();
            $iou = get_i_owe_you_record($iou_id);
            $ccioub = can_complete_iou_batch_with_expenses($iou['batch_no']);
            if($ccioub["success"] !== true){
                display_error($ccioub["message"]);
            }
            else{
                $cb = complete_iou_batch_with_expenses($iou['batch_no']);
                if($cb["success"] !== true){
                    display_error($cb["message"]);
                }
                else{
                    $Mode = 'RESET';
                    if(input_num('bank_account') > 0){
                        $message = ($systypes_array[$pay_type]." #".$mp["trans_no"]." has been created and I Owe You batch ".$cb["tran_no"]." has been closed.");
                    }else{
                        $message = ($systypes_array[$pay_type]." #".$selected_id." has been released and I Owe You batch ".$cb["tran_no"]." has been closed.");
                    }
                    display_notification($message);  
                    set_session_variable('iou_batch_messages', $message);
                    meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$mp["trans_no"]."&Pay_type=".$pay_type);  
                } 
            }
        }
    }
}


if(isset($_POST['Update_Make_Payment']) && $_POST['payment_method'] == 0){
    $up = update_iou_payment($iou_id, $selected_id);
    if($up["success"] !== true){
        if(isset($up["focus_to"])){
            set_focus($up["focus_to"]);
        }
        display_error($up["message"]);
    }
    else{
        begin_transaction();
        $mp = make_iou_payment($iou_id, $up["trans_no"]);
        $pay_type = ST_I_OWE_YOU_PAYMENT;
        if($mp["success"] !== true){
            cancel_transaction();
            display_error($ap["message"]);
        }
        else{
            commit_transaction();
            $Mode = 'RESET';
            $message = ("Payment $selected_id has been released.");
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$selected_id."&Pay_type=".$pay_type);    
        }
    }
}


if(isset($_POST['Update_Make_Payment_Close_IOU_For_Payments']) && $_POST['payment_method'] == 0){
    $up = update_iou_payment($iou_id, $selected_id);
    if($up["success"] !== true){
        if(isset($up["focus_to"])){
            set_focus($up["focus_to"]);
        }
        display_error($up["message"]);
    }
    else{
        begin_transaction();
        $mp = make_iou_payment($iou_id, $up["trans_no"]);
        $pay_type = ST_I_OWE_YOU_PAYMENT;
        if($mp["success"] !== true){
            cancel_transaction();
            display_error($ap["message"]);
        }
        else{
            commit_transaction();
            close_i_owe_you_for_payments($iou_id);
            $Mode = 'RESET';
            $message = ("Payment $selected_id has been released and I Owe You ".$iou_id." has been closed for payments.");  
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$selected_id."&Pay_type=".$pay_type);   
        }
    }
}



if(isset($_POST['Update_Make_Payment_Complete_Batch']) && $_POST['payment_method'] == 0){
    $up = update_iou_payment($iou_id, $selected_id);
    if($up["success"] !== true){
        if(isset($up["focus_to"])){
            set_focus($up["focus_to"]);
        }
        display_error($up["message"]);
    }
    else{
        begin_transaction();
        $mp = make_iou_payment($iou_id, $up["trans_no"]);
        $pay_type = ST_I_OWE_YOU_PAYMENT;
        if($mp["success"] !== true){
            cancel_transaction();
            display_error($ap["message"]);
        }
        else{
            commit_transaction();
            $iou = get_i_owe_you_record($iou_id);
            $ccioub = can_complete_iou_batch_with_expenses($iou['batch_no']);
            if($ccioub["success"] !== true){
                display_error($ccioub["message"]);
            }
            else{
                $cb = complete_iou_batch_with_expenses($iou['batch_no']);
                if($cb["success"] !== true){
                    display_error($cb["message"]);
                }
                else{
                    $Mode = 'RESET';
                    $message = ("Payment $selected_id has been released and I Owe You batch ".$cb["tran_no"]." has been closed.");
                    set_session_variable('iou_batch_messages', $message);
                    meta_forward($_SERVER['PHP_SELF'], "Payment_ID=".$selected_id."&Pay_type=".$pay_type);  
                } 
            }
        }
    }
}

if(isset($_POST['Complete_Batch'])){

    $iou = get_i_owe_you_record($iou_id);
    $ccioub = can_complete_iou_batch_with_expenses($iou['batch_no']);
    if($ccioub["success"] !== true){
        display_error($ccioub["message"]);
    }
    else{
        $cb = complete_iou_batch_with_expenses($iou['batch_no']);
        if($cb["success"] !== true){
            display_error($cb["message"]);
        }
        else{
            $Mode = 'RESET';
            $message = ("I Owe You batch ".$cb["tran_no"]." has been completed.");
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "IOU_Batch_ID=".$iou['batch_no']);
        } 
    }
}

if(isset($_POST['Keep_Balance_Complete_Batch'])){

    $iou = get_i_owe_you_record($iou_id);
    $ccioub = can_complete_iou_batch_with_expenses($iou['batch_no'], true);
    if($ccioub["success"] !== true){
        display_error($ccioub["message"]);
    }
    else{
        $cb = complete_iou_batch_with_expenses($iou['batch_no'], true);
        if($cb["success"] !== true){
            display_error($cb["message"]);
        }
        else{
            $Mode = 'RESET';
            $message = ("I Owe You batch ".$cb["tran_no"]." has been completed.");
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "IOU_Batch_ID=".$iou['batch_no']);
        } 
    }
}

if(isset($_POST['Complete_IOU_With_No_Expenses'])){

    $iou = get_i_owe_you_record($iou_id);
    $ccioub = can_complete_iou_with_no_expenses($iou_id);
    if($ccioub["success"] !== true){
        display_error($ccioub["message"]);
    }
    else{
        $cb = complete_iou_with_no_expenses($iou['batch_no']);
        if($cb["success"] !== true){
            display_error($cb["message"]);
        }
        else{
            $Mode = 'RESET';
            $message = ("I Owe You batch ".$cb["tran_no"]." has been completed.");
            set_session_variable('iou_batch_messages', $message);
            meta_forward($_SERVER['PHP_SELF'], "IOU_Batch_ID=".$iou['batch_no']);
        } 
    }
}




if(isset($_POST['Close_IOU_For_Payments'])){

        close_i_owe_you_for_payments($iou_id);

        $Mode = 'RESET';
        $message = ("I Owe You ".$iou_id." has been closed for payments.");
        set_session_variable('iou_batch_messages', $message);
        meta_forward($_SERVER['PHP_SELF'], "IOU_ID=".$selected_id);
        
    
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();

//if((isset($batch_no) && $batch_no > 0) || $iou_id > 0){
if($iou_id > 0){
    $iou = get_i_owe_you_row($iou_id);
    $batch_no = 0;
    if(isset($iou['batch_no'])){
        $batch_no = $iou['batch_no'];
    }

    $total_paid = display_batch_i_owe_yous($batch_no, $iou_id);
    //$i_owe_you = display_i_owe_you_header($iou_id);

    if($batch_no > 0){
        display_i_owe_you_batch_expences($batch_no, $total_paid);
    }
    else{
        display_note("No expences entered yet for this I Owe You.");

    }

    br();

    //display_i_owe_you_payments($iou_id);

    display_i_owe_you_payment_edit_controls($selected_id, $Mode, $iou_id);
}

br();

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


?>
