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


function display_batch_bank_transfer_details($type_id, $trans_no)
{
    global $systypes_array;
    
    $result = get_batch_bank_transfer_details($type_id, $trans_no);
    div_start('saved_iou_details');

    start_table(TABLESTYLE, "width=90%");

    $th = array("Type","#","Reference", "Date", "Amount", _("Person/Item"),"GL Trans Type", "Comments", "");
    table_header($th);

    $iou_total = 0;
    $k=0; //row colour counter

    while ($row = db_fetch($result))
    {
        alt_table_row_color($k);

        //label_cells('',$myrow["id"]);
        label_cell($systypes_array[$row["source_type"]]);
        $link = get_trans_view_str($row["source_type"], $row["source_trans_no"]);
        label_cell($link, "align=center");
        label_cell($row["reference"]);
        label_cell($row["trans_date"]);
        amount_cell(-$row["amount"]);
        label_cell(payment_person_name($row["person_type_id"], $row["person_id"]));
        label_cell($row["gl_trans_type_name"]);
        label_cell(get_comments_string($row["source_type"], $row["source_trans_no"]));
        button_cell('DelTran'.$row["id"],  _("Delete"));

        
        $iou_total += -$row["amount"];
    }
    
    start_row();
        label_cell("Total", "colspan=4 align=right");
        amount_cell($iou_total, true);
    end_row();
    
    end_table(1);
    div_end();
}




function display_batch_bank_transfer_filtering_controls()
{
    global $systypes_array;
    
    $tranaction_types = array();
    
    
    $tranaction_types[ST_IOU_BANKPAYMENT] = $systypes_array[ST_IOU_BANKPAYMENT];
    $tranaction_types[ST_SUPPAYMENT] = $systypes_array[ST_SUPPAYMENT];
    $tranaction_types[ST_BANKPAYMENT] = $systypes_array[ST_BANKPAYMENT];
    $tranaction_types[ST_PETTY_CASH_FLOAT_REIMBUR] = $systypes_array[ST_PETTY_CASH_FLOAT_REIMBUR];
    
    asort($tranaction_types); 
//    $tranaction_types[-1] = 'Select a gl trans type';
//    array_push($tranaction_types, $_);
    
    $tranaction_types = array(-1 => 'Select a gl trans type') + $tranaction_types;
        
    start_table(TABLESTYLE2);
    custom_values_list_cells(_("Type:"), "filtering_type_id", $tranaction_types);
//    project_boq_item_groups_list_row("BOQ Group", "boq_group_id", $_POST['project_id'], null, "Select a BOQ Item Group", true);
    date_cells("From Date:", "filtering_from_date", $_POST['tran_date'], null, 0, -1, 0);
    date_cells("To Date:", "filtering_to_date", $_POST['tran_date']);
    submit_cells('SearchTrans', _("Search"), '', '', 'default');
    end_table(1);
    
}

function build_batch_bank_transfer_source_transactions_array(&$trans, $trans_no, $to_bank_act, $filtering_type_id, $filtering_from_date, $filtering_to_date)
{

    switch ($filtering_type_id) {
        case ST_IOU_BANKPAYMENT:
        case ST_SUPPAYMENT:
        case ST_BANKPAYMENT:
        case ST_PETTY_CASH_FLOAT_REIMBUR:
            $result = get_batch_bank_transfer_source_iou_bank_payments($trans_no, $to_bank_act, $filtering_type_id, $filtering_from_date, $filtering_to_date);
            break;

        default:
            break;
    }
    
    if(isset($result)){
        
        while ($row = db_fetch($result)) {
            $a = array(); 

            $a["type"] = $filtering_type_id; 
            $a["trans_no"] = $row["trans_no"];
            $a["reference"] = $row["reference"];
            $a["amount"] = -$row["amount"];
            $a["trans_date"] = $row["trans_date"];
            $a["person_type_id"] = $row["person_type_id"];
            $a["person_id"] = $row["person_id"];
            $a["person_detail_id"] = $row["person_detail_id"];
            $a["gl_trans_type_name"] = $row["gl_trans_type_name"];
            $trans[$row["trans_no"]] = $a;
         }
    }
        
     
}
function display_batch_bank_transfer_edit_controls($trans_no, $to_bank_act, $filtering_type_id, $filtering_from_date_, $filtering_to_date_)
{
    global $systypes_array, $Ajax;
    
    $trans = array();
    
    $filtering_from_date = date2sql($filtering_from_date_);
    $filtering_to_date = date2sql($filtering_to_date_);
    
    build_batch_bank_transfer_source_transactions_array($trans, $trans_no, $to_bank_act, $filtering_type_id, $filtering_from_date, $filtering_to_date);
    
    $Ajax->activate('iou_details');

    div_start('iou_details');

    start_table(TABLESTYLE, "width=90%");

    $th = array("Type", "#","Reference",_("Date"), _("Amount"), _("Person/Item"), "GL Trans Type", "Comments", "");
    table_header($th);

    $k=0; //row colour counter


    foreach ($trans as $key => $line) {
    
        alt_table_row_color($k);
        label_cell($systypes_array[$filtering_type_id]);
        $link = get_trans_view_str($filtering_type_id, $key);
        label_cell($link);
        label_cell($line["reference"]);
        label_cell($line["trans_date"]);
        amount_cell($line["amount"]); 
        label_cell(payment_person_name($line["person_type_id"],$line["person_id"]));
        label_cell($line["gl_trans_type_name"]);
	label_cell(get_comments_string($filtering_type_id, $line["trans_no"])); 
//        submit_cells('Add_Tran_'.$key, _("Add"), '', '', 'Add');
        button_cell('AddTran'.$key,  _("Add"));
        
        end_row();
    }

    end_table(1);
    div_end();
}



function display_batch_bank_transfer_header($type_id, $trans_no)
{
    global $approval_statuses, $Refs;
    if(!isset($_POST['amount'])){
        $_POST['amount'] = 0;
    }
    if(!isset($_POST['tran_date'])){
        $_POST['tran_date'] = '';
    }
    if(!isset($_POST['comment'])){
        $_POST['comment'] = '';
    }
    if(!isset($_POST['approval_status'])){
        $_POST['approval_status'] = 0;
    }

    if($trans_no > 0)
    {
        $myrow = get_batch_bank_transfer_record($type_id, $trans_no);

        $_POST['id'] = $myrow["id"];

        set_post_not_set_string("reference", $myrow["reference"]);
        set_post_not_set_string("tran_date", sql2date($myrow["trans_date"]));
        set_post_not_set_string("comment", $myrow["comment"]);
        
        set_post_not_set_numeric("from_bank_act", $myrow["from_bank_act"]);
        set_post_not_set_numeric("to_bank_act", $myrow["to_bank_act"]);
        set_post_not_set_numeric("approval_status", $myrow["approval_status"]);
        
          
    }else{
        $_POST['reference'] = $Refs->get_next($type_id);
    }

    br();

    

    start_table(TABLESTYLE2);

        if($trans_no > 0){
            label_row("Trans. No:", $trans_no); 
        }else{
            label_row("Trans. No:", "New"); 
        }
        
        text_row("Reference:", "reference", $_POST['reference'], 20, 100); 

        date_row("Date:", "tran_date", $_POST['tran_date']);
        
        bank_accounts_list_row("From (Crediting) Bank Account", "from_bank_act", null, false, "Select a bank account");
        
        $count = get_batch_bank_transfer_items_count($type_id, $trans_no);
        if($count > 0){
            $bank_account_name = get_bank_account_formatted_name($myrow["to_bank_act"]);
            $bank_account = get_bank_account($myrow["to_bank_act"]);
            label_row("To (Debiting) Bank Account", $bank_account_name); 
            hidden("to_bank_act", $bank_account['id']);
        }else{
            bank_accounts_list_row("To (Debiting) Bank Account", "to_bank_act", null, false, "Select a bank account");
        }
        
        textarea_row('Comments:', 'comment', $_POST['comment'], 35, 2);
        
        $details_total = get_batch_bank_transfer_details_total($type_id, $trans_no);
        label_row("Trans. No:", number_format($details_total,2)); 
        

        $s = get_project_trans_view_str(ST_APPROVAL_LOG, $trans_no."&source_type_id=".$type_id, "Approval Status");
        label_row($s,$approval_statuses[$_POST['approval_status']]);

        hidden("trans_no", $trans_no); 

    //    if($selected_id > 0){
    //        set_focus('type_id');
    //    }
    //}
    end_table(1);
    
    
}


function handle_delete_batch_bank_transfer_item($type_id, $trans_no, $del_trans_no)
{
    global $systypes_array;
    
    $msg = can_delete_batch_bank_transfer_item($type_id, $trans_no, $del_trans_no);
    if($msg['success'] !== true){
        return $msg;        
    }

    begin_transaction();
    
    $d = delete_batch_bank_transfer_detail_item($type_id, $trans_no, $del_trans_no);
    if($d !== 1){
        cancel_transaction();
        $message = ("Another user has updated ".$systypes_array[$type_id]." #".$trans_no);
        $set_focus = '';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    $msg = update_batch_bank_transfer_header_total($type_id, $trans_no);
    if($msg['success'] !== true){
        cancel_transaction();
        return $msg;        
    }
    
    commit_transaction();
    $message = "A transaction has been removed in ".$systypes_array[$type_id]." #".$trans_no;
    display_notification($message);
}

function can_delete_batch_bank_transfer_item($type_id, $trans_no, $source_trans_no)
{	
    global $systypes_array;
    
    $batch = get_batch_bank_transfer_record($type_id, $trans_no);
    if(!(in_array($batch['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_REOPENED)))){
        $message = ("This ".$systypes_array[ST_BATCH_BANK_TRANSFER_REQ]." status does not allow further updating.");
        $set_focus = '';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
        
    return array("success" => true, "message" => "");
    
}


function handle_save_batch_bank_transfer_item($type_id, &$trans_no, $filtering_type_id, $new_trans_no)
{
    global $systypes_array;
    
    begin_transaction();
    $msg = save_batch_bank_transfer($type_id, $trans_no, $filtering_type_id, $new_trans_no);
    if($msg['success'] !== true){
        display_error_cancel($msg['message']);        
    }else{
        commit_transaction();
        $message = "New transaction added to ".$systypes_array[$type_id]." #".$trans_no;
        display_notification($message);
    }    
}


function handle_save_batch_bank_transfer_header($type_id, $trans_no)
{
    global $systypes_array;
    
    $msg = can_commit_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
        display_error_cancel($msg['message']);       
    }

    begin_transaction();
    
    $msg = save_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
        cancel_transaction();
       display_error_cancel($msg['message']);  
    }
    
    commit_transaction();
    
    $message = $systypes_array[$type_id]." #".$trans_no." has been updated.";
    display_notification($message);
}


function handle_release_batch_bank_transfer_header($type_id, $trans_no)
{
    global $systypes_array;
    
    $msg = can_commit_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
        display_error_cancel($msg['message']);
        return;
    }    

    begin_global_transaction();
    
    $msg = save_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
       display_error_cancel($msg['message']);  
       return;
    }
    
    $msg = can_release_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
        display_error_cancel($msg['message']);
        return;       
    }    
    
    $msg = release_batch_bank_transfer_for_approval($type_id, $trans_no);
    if($msg['success'] !== true){
        display_error_cancel($msg['message']);
        return;  
    }
    
    commit_transaction();
    $message = $msg['message'];
    $AddedID = $msg['trans_no'];
    display_notification($message);
    meta_forward($_SERVER['PHP_SELF'], "AddedID=$AddedID"); 
}

function save_batch_bank_transfer($type_id, &$trans_no, $source_type, $source_trans_no)
{
    global $Refs;
    
    $new = false;
    if(!($trans_no > 0)){
        $new = true; 
    }
    
    $msg = can_commit_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
        return $msg;        
    }
    
    $msg = can_commit_batch_bank_transfer_item($type_id, $trans_no);
    if($msg['success'] !== true){
        return $msg;        
    }
    
    $msg = save_batch_bank_transfer_header($type_id, $trans_no);
    if($msg['success'] !== true){
        return $msg;        
    }else{
        $trans_no = $msg['trans_no'];
        set_session_variable("ST_BATCH_BANK_TRANSFER_REQ_no", $trans_no);
    }
    
    $msg = save_batch_bank_transfer_item($type_id, $trans_no, $source_type, $source_trans_no);
    if($msg['success'] !== true){
        return $msg;        
    }
    
    $msg = update_batch_bank_transfer_header_total($type_id, $trans_no);
    if($msg['success'] !== true){
        return $msg;        
    }
    
    if($new){
        $Refs->save($type_id, $trans_no, $_POST['reference']);
    }
    
    return array("success" => true, "trans_no" => $trans_no,"message" => "");
}


function save_batch_bank_transfer_header($type_id, $trans_no)
{
    $trans_date = date2sql($_POST['tran_date']);
    $batch = get_batch_bank_transfer_record($type_id, $trans_no);
    if(is_array($batch)){
        update_batch_bank_transfer_header($type_id, $trans_no, $trans_date, $_POST['reference'],
                                            $_POST['from_bank_act'], $_POST['to_bank_act'], $_POST['comment']);
        return array("success" => true, "trans_no" => $trans_no, "message" => "");
    }else{
        $new_no = get_next_trans_no($type_id);
        $no = add_batch_bank_transfer_header($type_id, $new_no, $_POST['reference'], input_num('amount'),
                    $_POST['from_bank_act'], $_POST['to_bank_act'], $trans_date, $_POST['comment']);
        return array("success" => true, "trans_no" => $new_no, "message" => "");
    }
}


function save_batch_bank_transfer_item($type_id, $trans_no, $source_type, $source_trans_no)
{

    $d = get_batch_bank_transfer_details_record($type_id, $trans_no, $source_type, $source_trans_no);
    if(is_array($d)){
        $message = "Selected item already attached in this transaction.";
        return array("success" => false, "trans_no" => $trans_no, "message" => $message);
    }else{
        $no = add_batch_bank_transfer_detail($type_id, $trans_no, $source_type, $source_trans_no);
        return array("success" => true, "trans_no" => $no, "message" => "");
    }
}


function can_commit_batch_bank_transfer_item($type_id, $trans_no)
{
    global $systypes_array;
    
//    if($trans_no > 0){
//        $n1 = get_batch_bank_transfer_items_count($type_id, $trans_no);
//        if(!($n1 > 0)){
//            $message = ("This ".$systypes_array[$type_id]." does not have any details.");
//            $set_focus = '';
//            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
//        }
//    }

    return array("success" => true, "message" => "");
}

function can_release_batch_bank_transfer_header($type_id, $trans_no)
{	
    global $systypes_array;
    
        if(!($trans_no > 0)){
            $message = ("This ".$systypes_array[$type_id]." is not saved. It can not be approved.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
    
        $batch = get_batch_bank_transfer_record($type_id, $trans_no);
        if(!(in_array($batch['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_REOPENED)))){
            $message = ("This ".$systypes_array[ST_BATCH_BANK_TRANSFER_REQ]." status does not allow further updating.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
        
        $count = get_batch_bank_transfer_items_count($type_id, $trans_no);
        if(!($count > 0)){
            $message = ("This ".$systypes_array[$type_id]." does not have any details. It can not be approved.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
        
        $total = get_batch_bank_transfer_details_total($type_id, $trans_no);
        if(check_num2($total, 0.0001)) 
        {
            $message = ("Transaction total amount has to be greater than zero.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        } 
        
        if($batch['fund_transfer_id'] > 0)
        {
            $message = ("This ".$systypes_array[$type_id]." has been posted to ".$systypes_array[ST_BANKTRANSFER]." #".$batch['fund_transfer_id'].".");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        } 
    
    return array("success" => true, "message" => "");
}

function can_commit_batch_bank_transfer_header($type_id, $trans_no)
{	
    global $systypes_array;
    
    if($trans_no > 0){
        $batch = get_batch_bank_transfer_record($type_id, $trans_no);
        if(!(in_array($batch['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_REOPENED)))){
            $message = ("This ".$systypes_array[ST_BATCH_BANK_TRANSFER_REQ]." status does not allow further updating.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
        
        if(check_num2(-$batch['amount'], 0.0001)) 
        {
            $message = ("Transaction total amount has to be greater than zero.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        } 
    }        

    if (strlen($_POST['reference']) == 0)
    {
        $message = "Reference is mandatory.";
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }

    $ref_msg = reference_exists($_POST['reference'], $type_id, $trans_no);
    if($ref_msg)
    {
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $ref_msg);
    }
    
    if (!is_date($_POST['tran_date']))
    {
        $message = "Date entered is not a valid date.";
        $set_focus = 'tran_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if (!is_date_in_fiscalyear($_POST['tran_date'])) {
        $message = (_("The entered date is not in fiscal year."));
        $set_focus = ('tran_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    $msg = is_accounting_period_open($_POST['tran_date']);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('tran_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    if (!($_POST['from_bank_act'] > 0))
    {
        $message = "Please select from bank account.";
        $set_focus = 'from_bank_act';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if (!($_POST['to_bank_act'] > 0))
    {
        $message = "Please select to bank account.";
        $set_focus = 'to_bank_act';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if ($_POST['from_bank_act'] == $_POST['to_bank_act']) 
    {
        $message = (_("The source and destination bank accouts cannot be the same."));
        $set_focus = ('ToBankAccount');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
        
    if (!is_date_in_fiscalyear($_POST['tran_date']))
    {
        $message = (_("The entered date is not in fiscal year."));
        $set_focus = ('tran_date');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }

    $msg = is_accounting_period_open($_POST['tran_date']);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('tran_date');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if (!db_has_currency_rates(get_bank_account_currency($_POST['from_bank_act']), $_POST['tran_date'])){
        $message = "No currency rates found for the from bank account.";
         $set_focus = 'from_bank_act';
         return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    if (!db_has_currency_rates(get_bank_account_currency($_POST['to_bank_act']), $_POST['tran_date'])){
        $message = "No currency rates found for the to bank account.";
         $set_focus = 'to_bank_act';
         return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }

        
    return array("success" => true, "message" => "");
    
}


function release_batch_bank_transfer_for_approval($type_id, $trans_no)
{
    global $systypes_array;

    $batch = get_batch_bank_transfer_record($type_id, $trans_no);    
    
    $fund_transfer_ref = get_next_reference_for_auto_generation(ST_BANKTRANSFER);    
    $ref_msg = reference_exists($fund_transfer_ref, ST_BANKTRANSFER);
    if($ref_msg != ""){
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $ref_msg);
    }
        
    $trans_date_ = sql2date($batch['trans_date']);
    $fund_trans_no = add_bank_transfer($batch['from_bank_act'], $batch['to_bank_act'],
		$trans_date_, $batch['amount'], $fund_transfer_ref, $batch['comment'], 0, 0);
    
    $msg = update_batch_bank_transfer_approved($type_id, $trans_no, $fund_trans_no);
    if ($msg['success'] !== true){
        $message = $msg['message'];
        return array("success" => false, "focus_to" => "", "message" => $message);
    }
    
    $message = $systypes_array[ST_BANKTRANSFER]." #".$fund_trans_no." created.";
    return array("success" => true, "trans_no" => $fund_trans_no, "message" => $message);
}





function update_batch_bank_transfer($iou_id)
{
    $header_row_state = get_post('row_state');
    $cd = can_process_batch_bank_transfer($iou_id, $header_row_state);
    if($cd["success"] !== true) 
    {
        return $cd;
    }
    
    begin_transaction();
    $recs = update_i_owe_you_header($iou_id, $_POST['float_id'], $_POST['group_company_code'], $_POST['employee_code'], input_num('total_amount'),
                                    $_POST['type_id'], $_POST['tran_date'], $_POST['comment'], 
                                    $header_row_state);
    
    if($recs == 1){
        if(!($_POST['boq_group_id'] > 0)){
            $boq_group_id = 0;
        }
        else{
            $boq_group_id = $_POST['boq_group_id'];
        }
        $q = get_i_owe_you_details_new($iou_id, $_POST['project_id'], $boq_group_id);
        while ($iou_line = db_fetch($q))
        {
            if(input_num("amount_".$iou_line["task_id"]) > 0){
                if($iou_line['row_state'] === null ){
                    add_i_owe_you_detail($iou_id, $iou_line['task_id'], 
                                        input_num("amount_".$iou_line["task_id"]), $_POST['comment_'.$iou_line["task_id"]]);
                }
                else{
                    $un = update_i_owe_you_detail($iou_line['id'], input_num("amount_".$iou_line["task_id"]),
                                        $iou_line['task_id'], $_POST['comment_'.$iou_line["task_id"]],
                                        $iou_line['row_state']);
                    if($un != 1){
                        $recs = 0;
                        break;
                    }
                }
            }
            elseif(input_num("amount_".$iou_line["task_id"]) == 0 && $iou_line['id'] > 0){
                $dn = delete_i_owe_you_detail($iou_line['id'], $iou_line['row_state']);
                if($dn != 1){
                    $recs = 0;
                    break;
                }
            }
        }
    }
    
    if($recs == 1){
        commit_transaction();
        return array("success" => true, "trans_no" => $iou_id, "message" => "I Owe You $iou_id has been updated.");
    }
    else{
        cancel_transaction();
        return array("success" => false, "message" => "I Owe You $iou_id could not be updated.");
    }
}





function add_batch_bank_transfer()
{
    global $Refs;
    
    $cd = can_process_batch_bank_transfer(-1);
    if($cd["success"] !== true) 
    {
        return $cd;
    }
    
    begin_transaction();
    $iou_id = add_i_owe_you_header($_POST['ref'], $_POST['type_id'],  $_POST['project_id'], 
                                    $_POST['float_id'], $_POST['group_company_code'], $_POST['employee_code'], input_num('total_amount'),                                   
                                    $_POST['tran_date'], $_POST['comment'], AS_NEW);
    
    if(!($_POST['boq_group_id'] > 0)){
        $boq_group_id = 0;
    }
    else{
        $boq_group_id = $_POST['boq_group_id'];
    }
    $q = get_i_owe_you_details_new(-1, $_POST['project_id'], $boq_group_id);
    while ($iou_line = db_fetch($q))
    {
        if(input_num("amount_".$iou_line["task_id"]) > 0){
            add_i_owe_you_detail($iou_id,  $iou_line['task_id'], 
                                input_num("amount_".$iou_line["task_id"]), $_POST['comment_'.$iou_line["task_id"]]);
        }
    }

    if($iou_id > 0){
        $Refs->save(ST_I_OWE_YOU, $iou_id, $_POST['ref']);
        commit_transaction();
        set_session_variable("ST_I_OWE_YOU_iou_no", $iou_id);
        return array("success" => true, "trans_no" => $iou_id, "message" => "I Owe You $iou_id has been added.");
    }
    else{
        cancel_transaction();
        return array("success" => false, "message" => "I Owe You could not be added.");
    }
}


function can_release_batch_bank_transfer($iou_id)
{

    $detail_total = get_i_owe_you_details_sum($iou_id);

    if ($detail_total != input_num('total_amount'))
   	{
        $message = "Total Amount and the total of amounts entered in the details do not match.";
        $set_focus = 'amount';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
   	}
    
    return array("success" => true, "message" => "");
    
}



function handle_remove_batch_bank_transfer_entries($type_id, $trans_no)
{
    global $systypes_array;
    
    $batch = get_batch_bank_transfer_record($type_id, $trans_no);
    if(!($batch['fund_transfer_id'] > 0)){
        $message = "No ".$systypes_array[ST_BANKTRANSFER]." created for this ".$systypes_array[ST_BATCH_BANK_TRANSFER_REQ];
        display_error($message);       
    }

    begin_transaction();
    
    $n = update_batch_bank_transfer_details_removed($type_id, $trans_no);
    if(!($n > 0)){
        $message = "Another user has updated this transaction.";
        display_error_cancel($message);  
    }
    
    commit_transaction();
    
    $message = "All entries of ".$systypes_array[$type_id]." #".$trans_no." has been removed.";
    display_notification($message);
}