<?php

function display_remove_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_remove_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 removing from the batch.");
            return array("success" => false, "message" => $message);      
        }
        
        $batch_no = $iou_hd_row['batch_no'];
        
        if (! ($batch_no > 0)) {
            $message = ("This ".$systypes_array[ST_I_OWE_YOU]." is not assigned to any ".$systypes_array[ST_I_OWE_YOU_BATCH].".");
            return array("success" => false, "message" => $message);      
        }
        
    $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 removed from ".$systypes_array[ST_I_OWE_YOU_BATCH].".";
        
        return array("success" => false, "message" => $message);
    }
        
    $batch = get_i_owe_you_batch_header_record($batch_no);
        $current_user_id = $_SESSION['wa_current_user']->user;
        $ced_msg = can_edit_document(ST_I_OWE_YOU_BATCH, $iou_hd_row['project_id'], $batch['next_approval_group_id'], $current_user_id, 
                                     $batch['created_by'], $batch_no);
        if($ced_msg['success'] !== true){
            display_error($ced_msg['message']);
            set_focus('batch_task');
            return false;
        }
        
    $bt_ap_st = $batch['approval_status'];
        if (in_array($bt_ap_st, array(AS_APPROVED, AS_REJECTED, AS_COMPLETED))) {
            $message = ("Status of ".$systypes_array[ST_I_OWE_YOU_BATCH]." #$batch_no does not allow further updating.");
            return array("success" => false, "message" => $message);      
        }
        
    $ious_result = get_batch_i_owe_yous($batch_no);
        $nm_ious = db_num_rows($ious_result);
            if ($nm_ious == 1) {
                $message = ("This is the only ".$systypes_array[ST_I_OWE_YOU]." in the ".$systypes_array[ST_I_OWE_YOU_BATCH]." and it cannot be removed");
                return array("success" => false, "message" => $message);      
            }
            
    $rest_ious_total_row = get_i_owe_you_batch_totals($batch_no, $iou_no);
    $rest_ious_total = $rest_ious_total_row['iou_batch_total'];
    
    $batch_expenses_total = get_i_owe_you_batch_expenses_total($batch_no);
    
        if (! ($batch_expenses_total <= $rest_ious_total)) {
            $message = ("Total of remaining ".$systypes_array[ST_I_OWE_YOU]."s (".
                        number_format($rest_ious_total, 2).") exceed the value of ".$systypes_array[ST_I_OWE_YOU_BATCH]." #$batch_no
                        (".number_format($batch_expenses_total, 2)."). Not possible to remove this ".$systypes_array[ST_I_OWE_YOU].". ");
            return array("success" => false, "message" => $message);      
        }
        
    return array("success" => true, "message" => "", "iou_row_state" => $iou_hd_row['row_state'], "batch_no" => $batch_no, 
                                    "batch_row_state" => $batch['row_state']); 
}

function handle_remove_i_owe_you($iou_no) {
    global $systypes_array;
    
    $msg = can_process_remove_i_owe_you($iou_no);   
        if ($msg["success"] !== true) {   
            return $msg;
        }
        
    $batch_no = $msg['batch_no'];
        
    begin_transaction();
        $n_af = update_remove_i_owe_you_from_batch_no($iou_no, $msg['iou_row_state'], $batch_no);
            if ($n_af !== 1) {
                cancel_transaction();
                return array('success' => false, 'message' => _("Another user has updated ".$systypes_array[ST_I_OWE_YOU]." #$iou_no."));
            }
            
        $n_af2 = update_i_owe_you_batch_row_state($batch_no, $msg['batch_row_state']);
            if ($n_af2 !== 1) {
                cancel_transaction();
                return array('success' => false, 'message' => _("Another user has updated ".$systypes_array[ST_I_OWE_YOU_BATCH]." #$batch_no."));
            }
            
        commit_transaction();

        return array('success' => true, 
                     'message' => _($systypes_array[ST_I_OWE_YOU]." #$iou_no has been removed from ".$systypes_array[ST_I_OWE_YOU_BATCH])."#$batch_no.");
}