<?php

function display_close_i_owe_you($iou_no) { 
    view_iou_header($iou_no);

    br();
    view_iou_details($iou_no);

    br(1);

    display_i_owe_you_all_payments($iou_no, false);
}

function can_process_close_i_owe_you($iou_no) { 
    global $systypes_array;
    
    $iou_hd_row = get_i_owe_you_record($iou_no);
        if ($iou_hd_row['approval_status'] != AS_APPROVED) {
            $message = ("Status of this ".$systypes_array[ST_I_OWE_YOU]." does not allow closing.");
            return array("success" => false, "message" => $message);      
        }
        
        $batch_no = $iou_hd_row['batch_no'];
        
        if ($batch_no > 0) {
            $batch_row = get_i_owe_you_batch_header_record($batch_no);
                if (! in_array($batch_row['approval_status'], array(AS_CLOSED, AS_REJECTED))) {
                    $message = ("This ".$systypes_array[ST_I_OWE_YOU]." belongs to ".$systypes_array[ST_I_OWE_YOU_BATCH]." #$batch_no. ".
                                $systypes_array[ST_I_OWE_YOU_BATCH]." has to be Closed or Rejected. This ".$systypes_array[ST_I_OWE_YOU]
                                ." cannot be closed.");
                    
                    return array("success" => false, "message" => $message);      
                }
        } else {
            // if there has no batch number allow closing the I owe you
        }
        
    $advace = get_i_owe_you_advances_paid_total($iou_no);
    $iou_bank_total = get_i_owe_you_bank_payments_total($iou_no);
    $bank_payments = -($iou_bank_total);
    
    $pay_tot = get_i_owe_you_payments_total($iou_no, 0);
        
    $payments_total = $pay_tot + $advace + $bank_payments;
        
    if ($payments_total > 0) { 
        $m = " Paid and unpaid ".$systypes_array[ST_I_OWE_YOU_PAYMENT]."s total (Rs.".number_format($pay_tot,2)
            .") + ".$systypes_array[ST_I_OWE_YOU_ADVANCES]."s total (Rs.".number_format($advace,2)
            .") + ".$systypes_array[ST_IOU_BANKPAYMENT]."s total (Rs.".number_format($bank_payments,2).")";
        
        $message = "This ".$systypes_array[ST_I_OWE_YOU]." has total payments of ". number_format($payments_total, 2)." <br>($m )<br> When there are "
                . "payments ".$systypes_array[ST_I_OWE_YOU]." cannot be closed.";
        
        return array("success" => false, "message" => $message);
    }
        
        return array("success" => true, "message" => "", "iou_row_state" => $iou_hd_row['row_state']); 
}

function handle_close_i_owe_you($iou_no) {
    global $systypes_array;
    
    $msg = can_process_close_i_owe_you($iou_no);   
        if ($msg["success"] !== true) {   
            return $msg;
        }
        
    $iou = get_i_owe_you_record($iou_no);
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    $row_state = $iou['row_state'];
        
    begin_transaction();

        $iou_details = get_i_owe_you_details($iou_no);
        $task_costs_summary = array();
        while ($myrow = db_fetch($iou_details)) {
            add_to_task_cost($task_costs_summary, $myrow['task_id'], $myrow['amount'], $myrow['amount'], $myrow['amount']);
        }

        get_task_cost_details($task_costs_summary);

        $rta_msg = remove_task_allocations(ST_I_OWE_YOU, $iou['id'], $task_costs_summary, $current_user_id);
            if($rta_msg['success'] !== true){
                cancel_transaction();
                
                return array("success" => false, "message" => $rta_msg['message']);
            }

        $msg_2 = direct_update_document_status(ST_I_OWE_YOU, $iou_no, AS_CLOSED, $row_state);
            if($msg_2['success'] === true){
                commit_transaction();
                
                return array("success" => true, "message" => $systypes_array[ST_I_OWE_YOU]." #$iou_no has been closed.");
            } else {
                cancel_transaction();
                
                return array("success" => false, "message" => $msg_2['message']);
            }
}