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

//function get_petty_cash_payments($iou_id)
//{
//        $sql= "SELECT pcp.* 
//               FROM " .TB_PREF."petty_cash_payments pcp 
//               WHERE  pcp.iou_id=". db_escape($iou_id)." 
//               ORDER BY pcp.payment_date";
//        
//	return $result=db_query($sql,"Could not get_petty_cash_payments");	
//       //  db_fetch($result);    
//}
//
//function get_petty_cash_payment($payment_id)
//{
//        $sql= "SELECT * 
//               FROM " .TB_PREF."petty_cash_payments 
//               WHERE id=". db_escape($payment_id);
//        
//        $result = db_query($sql,"Could not get_petty_cash_payment");	
//        return db_fetch($result);
//    
//}

//function release_petty_cash_payment($iou_id, $payment_id)
//{
//    $updated_by = $_SESSION['wa_current_user']->user;
//    $updated_on = date('Y-m-d H:i:s');
//        
//        
//	$sql = "UPDATE ".TB_PREF."petty_cash_payments 
//            SET approval_status = ".db_escape(AS_PAID).", 
//                    updated_by = ".db_escape($updated_by).", 
//                    updated_on = ".db_escape($updated_on)."   
//            WHERE id = ".$payment_id;
//        
//    db_query($sql,"Could not release_petty_cash_payment");
//        
//    $payments = get_claim_payments_total($iou_id);
//    $totals = get_claim_detail_totals($iou_id);
//    
//    if($payments['payment_total'] >= $totals['requested_total'] && $payments['payment_total'] >= $totals['approved_total']){
//        update_i_owe_you_status($iou_id, "M");
//    }
//        
//}

//function add_petty_cash_payment($iou_id, $payment_date, $amount, $payment_comment)
//{
//    $created_by = $_SESSION['wa_current_user']->user;
//    $created_on = date('Y-m-d H:i:s');        
//        
//	$sql = "INSERT INTO ".TB_PREF."petty_cash_payments (
//                    iou_id, payment_date, 
//                    amount, comment,
//                    approval_status,
//                    created_by, created_on) 
//                VALUES ("
//                    .db_escape($iou_id).", ".db_escape(date2sql($payment_date)).", "
//                    .db_escape($amount).", ".db_escape($payment_comment).", " 
//                    .db_escape(AS_NEW).", "
//                    .db_escape($created_by).", ".db_escape($created_on).")";
//        
//    db_query($sql,"Could not add_petty_cash_payment");
//        
//    return db_insert_id();
//}

//function update_petty_cash_payment($payment_id, $payment_date, $amount, $payment_comment)
//{
//    $updated_by = $_SESSION['wa_current_user']->user;
//    $updated_on = date('Y-m-d H:i:s');
//        
//        
//	$sql = "UPDATE ".TB_PREF."petty_cash_payments 
//            SET payment_date = ".db_escape(date2sql($payment_date)).", 
//                    amount = ".db_escape($amount).", 
//                    comment = ".db_escape($payment_comment).",
//                    updated_by = ".db_escape($updated_by).", 
//                    updated_on = ".db_escape($updated_on)."   
//            WHERE id = ".$payment_id;
//        
//    db_query($sql,"Could not update_petty_cash_payment");
//        
//    return db_num_affected_rows();  
//}

//function get_claim_payments_total($iou_id)
//{
//        $sql= "SELECT SUM(amount) payment_total  
//               FROM ".TB_PREF."petty_cash_payments  
//               WHERE iou_id=".db_escape($iou_id);
//        
//	$result=db_query($sql,"Could not get_claim_payments_total");	
//        return  db_fetch($result);    
//}

//function delete_claim_payment($rec_id)
//{
//        $sql = "DELETE FROM ".TB_PREF."petty_cash_payments   
//                WHERE id=".db_escape($rec_id);
//
//        db_query($sql,"Could not delete_claim_payment");
//        
//        return db_num_affected_rows();
//}

function approve_petty_cash_payment($id)
{
    $current_user_id = $_SESSION['wa_current_user']->user;

    $payment = get_petty_cash_payment($id);
    $petty_cash_request = get_i_owe_you_row($payment['iou_id']);

    $info = approve_document(ST_PETTY_CASH_PAYMENT, $payment['id'], $petty_cash_request['project_id'], 
                        $payment['amount'], $payment['next_approval_group_id'], 
                        $current_user_id, $payment['row_state']);

    if($info['success'] !== true){
        cancel_transaction();
        return array("success" => $info['success'], "message" => $info['message']);
    }

    /* Budget allocation is in the Purchase Requisition
     * PO requires approval only
     */
//    if($info['approval_status'] == AS_APPROVED){            
//        $apc_msg = allocate_to_task(ST_PURCHORDER, $po_no, 
//                        $cart->task_id, $po_total, 
//                        $current_user_id);
//        if($apc_msg['success'] !== true){
//            cancel_transaction();
//            return array("success" => $apc_msg['success'], "message" => $info['msg']);
//        }
//    }
    
 //   return array("success" => true, "message" => $info['message']);            
        
}


function get_petty_cash_float_reimbursements_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_PETTY_CASH_FLOAT_REIMBUR." type_id, 
                    pcfr.id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    pcfr.tran_date,
                    pcfr.amount,      
                    pcfr.reimbursed_amount,      
                    0 advances_total,
                    pcfr.row_state,
                    0 batch_no,
                    pcfr.from_float_id float_id,
                    CONCAT('Paid to ', (SELECT name FROM ".TB_PREF."petty_cash_floats WHERE id = pcfr.float_id)) comment,
                    pcfr.created_on 
                FROM ".TB_PREF."i_owe_you_float_reimbursements pcfr 
                        LEFT JOIN ".TB_PREF."petty_cash_floats pt ON 
                                pcfr.from_float_id = pt.id 

                WHERE ABS(pcfr.amount) > pcfr.reimbursed_amount AND 
                    pt.id=". db_escape($float_id)." AND 
                    pcfr.created_on >= ".db_escape(date2sql($from_date))." AND 
                    pcfr.created_on <= ".db_escape(date2sql($to_date_midnight));


      return db_query($sql, "Could not get_petty_cash_float_adjustments_for_float_reimbursement");   
}


function get_petty_cash_float_adjustments_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
$sql = "SELECT ".ST_PETTY_CASH_FLOAT_ADJ." type_id, 
                pcfa.id,
                0 iou_id,
                '' project_description,
                pt.name,
                pcfa.tran_date,
                pcfa.amount * -1 amount,         
                pcfa.reimbursed_amount,         
                0 advances_total,
                pcfa.row_state,
                0 batch_no,
                pcfa.float_id,
                pcfa.comment,
                pcfa.created_on 
            FROM ".TB_PREF."petty_cash_float_adjustments pcfa 
                    LEFT JOIN ".TB_PREF."petty_cash_floats pt ON 
                            pcfa.float_id = pt.id 

            WHERE ABS(pcfa.amount) > pcfa.reimbursed_amount AND 
                pt.id=". db_escape($float_id)." AND 
                pcfa.created_on >= ".db_escape(date2sql($from_date))." AND 
                pcfa.created_on <= ".db_escape(date2sql($to_date_midnight));

    
  return db_query($sql, "Could not get_petty_cash_float_adjustments_for_float_reimbursement");   
}


function get_supplier_payments_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
$sql = "SELECT ".ST_SUPPAYMENT." type_id, 
                st.trans_no id,
                0 iou_id,
                '' project_description,
                pt.name,
                st.tran_date,
                (st.ov_amount - st.ov_discount)*-1 amount,      
                st.reimbursed_amount,
                0 advances_total,
                0 row_state,
                0 batch_no,
                st.float_id,
                '' comment,
                st.created_on 
            FROM ".TB_PREF."supp_trans st 
                    JOIN ".TB_PREF."petty_cash_floats pt ON 
                            st.float_id = pt.id 
            WHERE ABS((st.ov_amount - st.ov_discount)) > st.reimbursed_amount AND 
                st.type = ".ST_SUPPAYMENT." AND 
                pt.id=". db_escape($float_id)." AND 
                st.created_on >= ".db_escape(date2sql($from_date))." AND 
                st.created_on <= ".db_escape(date2sql($to_date_midnight));

    
  return db_query($sql, "Could not get_petty_cash_float_adjustments_for_float_reimbursement");   
}

function get_cash_transactions_for_float_reimbursement($trans_type, $float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    if($trans_type == ST_CASH_PAYMENT){
        $sign = -1;
    }
    if($trans_type == ST_CASH_RECEIPT){
        $sign = 1;
    }
    
$sql = "SELECT tran_type type_id, 
                j.type_no id,
                0 iou_id,
                '' project_description,
                pt.name,
                j.tran_date,
                (j.amount * -1) amount,      
                j.reimbursed_amount,
                0 advances_total,
                j.row_state,
                0 batch_no,
                j.float_id,
                '' comment,
                j.tran_date 
            FROM ".TB_PREF."petty_cash_float_trans j  
                    JOIN ".TB_PREF."petty_cash_floats pt ON 
                            j.float_id = pt.id 
            WHERE ABS(j.amount) > j.reimbursed_amount AND 
                j.tran_type = ".$trans_type." AND 
                pt.id=". db_escape($float_id)." AND 
                j.tran_date >= ".db_escape(date2sql($from_date))." AND 
                j.tran_date <= ".db_escape(date2sql($to_date_midnight));

    
  return db_query($sql, "get_cash_transactions_for_float_reimbursement");   
}

function get_iou_payments_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_I_OWE_YOU_PAYMENT." type_id,
                    ioup.id id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    ioup.payment_date tran_date,
                    ioup.amount,   
                    ioup.reimbursed_amount,   
                    0 advances_total,  
                    ioup.row_state,
                    0 batch_no,
                    ioup.float_id,
                    ioup.comment comment,
                    ioup.created_on 
            FROM ".TB_PREF."i_owe_you_payments ioup,
                 ".TB_PREF."petty_cash_floats pt 
                                      
            WHERE ABS(ioup.amount) > ioup.reimbursed_amount AND 
                    ioup.float_id = pt.id AND 
                    ioup.expense_source IN(".ST_I_OWE_YOU.") AND 
                    ioup.float_id = ". db_escape($float_id)." AND 
                    ioup.created_on >= ".db_escape(date2sql($from_date))." AND 
                    ioup.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY created_on, id ";

    
  return db_query($sql, "Could not get_i_owe_you_payments_for_float_reimbursement");   
}


function get_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_PETTY_CASH_PAYMENT." type_id,
                    ioup.id id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    ioup.payment_date tran_date,
                    ioup.amount,   
                    ioup.reimbursed_amount,   
                    0 advances_total,  
                    ioup.row_state,
                    0 batch_no,
                    ioup.float_id,
                    ioup.comment comment,
                    ioup.created_on 
            FROM ".TB_PREF."i_owe_you_payments ioup,
                 ".TB_PREF."petty_cash_floats pt 
                                      
            WHERE ABS(ioup.amount) > ioup.reimbursed_amount AND 
                    ioup.float_id = pt.id AND 
                    ioup.expense_source IN(".ST_PETTY_CASH.") AND 
                    ioup.float_id = ". db_escape($float_id)." AND 
                    ioup.created_on >= ".db_escape(date2sql($from_date))." AND 
                    ioup.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY created_on, id ";

    
  return db_query($sql, "Could not get_i_owe_you_payments_for_float_reimbursement");   
}


function get_direct_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_PETTY_CASH_DIRECT_PAYMENTS." type_id,
                    ioup.id id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    ioup.payment_date tran_date,
                    ioup.amount,   
                    ioup.reimbursed_amount,   
                    0 advances_total,  
                    ioup.row_state,
                    0 batch_no,
                    ioup.float_id,
                    ioup.comment comment,
                    ioup.created_on 
            FROM ".TB_PREF."i_owe_you_payments ioup,
                 ".TB_PREF."petty_cash_floats pt 
                                      
            WHERE ABS(ioup.amount) > ioup.reimbursed_amount AND 
                    ioup.float_id = pt.id AND 
                    ioup.expense_source IN(".ST_VEHICLE_RUNNING_CHART.",".ST_VEHICLE_ALLOCATION.") AND 
                    ioup.float_id = ". db_escape($float_id)." AND 
                    ioup.created_on >= ".db_escape(date2sql($from_date))." AND 
                    ioup.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY created_on, id ";

    
  return db_query($sql, "Could not get_i_owe_you_payments_for_float_reimbursement");   
}



function get_iou_advances_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_I_OWE_YOU_ADVANCES." type_id,
                    d.id,
                    d.iou_id,
                    '' project_description,
                    pt.name,
                    a.created_on tran_date,
                    d.amount,   
                    d.reimbursed_amount,   
                    0 advances_total,  
                    d.row_state,
                    0 batch_no,
                    d.float_id,
                    '' comment,
                    a.created_on,
                    d.id line_id 
            FROM ".TB_PREF."i_owe_you_advances a,
                 ".TB_PREF."i_owe_you_advances_details d,
                 ".TB_PREF."petty_cash_floats pt   
                                      
            WHERE d.amount > d.reimbursed_amount AND 
                    a.batch_no = d.batch_no AND 
                    d.float_id = pt.id AND 
                    d.float_id = ". db_escape($float_id)." AND 
                    a.created_on >= ".db_escape(date2sql($from_date))." AND 
                    a.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY created_on, a.batch_no, d.id ";

    
  return db_query($sql, "Could not get_iou_advances_for_float_reimbursement");   
}


function get_project_float_advances_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_PROJECT_ADVANCE." type_id,
                    d.id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    a.tran_date,
                    d.amount,   
                    d.reimbursed_amount,   
                    0 advances_total,  
                    d.row_state,
                    0 batch_no,
                    a.float_id,
                    '' comment,
                    a.created_on,
                    d.id line_id 
            FROM ".TB_PREF."project_advance_batches a,
                 ".TB_PREF."project_advance_batch_details d,
                 ".TB_PREF."petty_cash_floats pt   
                                      
            WHERE ABS(d.amount) > d.reimbursed_amount AND 
                    a.id = d.pab_id AND 
                    a.float_id = pt.id AND 
                    a.float_id = ". db_escape($float_id)." AND 
                    a.created_on >= ".db_escape(date2sql($from_date))." AND 
                    a.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY created_on, a.id, d.id ";

    
  return db_query($sql, "Could not get_project_float_advances_for_float_reimbursement");   
}

function get_iou_expenses_for_float_reimbursement($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
    $sql = "SELECT ".ST_I_OWE_YOU_BATCH." type_id,
                    ioub.batch_no id,
                    ioub.batch_no iou_id,
                    pm.description project_description,
                    pt.name,
                    ioub.approved_on tran_date,
                    ioub.amount,   
                    ioub.reimbursed_amount,   
                    ioub.advances_total,  
                    ioub.row_state,
                    ioub.batch_no,
                    ioub.float_id,
                    '' comment,
                    ioub.created_on 
            FROM ".TB_PREF."i_owe_you_batches ioub,
                 ".TB_PREF."projects_master pm,
                 ".TB_PREF."petty_cash_floats pt
                                      
            WHERE ioub.project_id = pm.project_id AND
                    ioub.float_id = pt.id AND 
                    ioub.approval_status = ". db_escape(AS_APPROVED)." AND 
                    ioub.amount > ioub.reimbursed_amount AND 
                    pt.id=". db_escape($float_id)." AND 
                    ioub.created_on >= ".db_escape(date2sql($from_date))." AND 
                    ioub.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY ioub.created_on, ioub.batch_no";
    
  return db_query($sql, "Could not get_iou_expenses_for_float_reimbursement");   
}

function get_i_owe_you_float_reimbursement_details($float_id, $from_date, $to_date)
{
    $to_date_midnight = add_days($to_date, 1);
    
//    $sql1 = "SELECT ".ST_I_OWE_YOU_BATCH." type_id,
//                    ioub.batch_no id,
//                    ioub.batch_no iou_id,
//                    pm.description project_description,
//                    pt.name,
//                    ioub.approved_on tran_date,
//                    ioub.amount,   
//                    ioub.advances_total,  
//                    ioub.row_state,
//                    ioub.batch_no,
//                    ioub.float_id,
//                    '' comment,
//                    ioub.created_on 
//            FROM ".TB_PREF."i_owe_you_batches ioub,
//                 ".TB_PREF."i_owe_you iou,
//                 ".TB_PREF."projects_master pm,
//                 ".TB_PREF."petty_cash_floats pt
//                                      
//            WHERE ioub.project_id = pm.project_id AND
//                    ioub.float_id = pt.id AND 
//                    ioub.approval_status = ". db_escape(AS_APPROVED)." AND 
//                    ioub.float_reimbursement_id=0 AND 
//                    pt.id=". db_escape($float_id)." AND 
//                    ioub.created_on >= ".db_escape(date2sql($from_date))." AND 
//                    ioub.created_on <= ".db_escape(date2sql($to_date_midnight));
    
    
    // IOU Payment 
    
    $sql2 = "SELECT ".ST_PETTY_CASH_FLOAT_ADJ." type_id, 
                pcfa.id,
                0 iou_id,
                '' project_description,
                pt.name,
                pcfa.tran_date,
                -pcfa.amount,         
                0 advances_total,
                pcfa.row_state,
                0 batch_no,
                pcfa.float_id,
                pcfa.comment,
                pcfa.created_on 
            FROM ".TB_PREF."petty_cash_float_adjustments pcfa 
                    LEFT JOIN ".TB_PREF."petty_cash_floats pt ON 
                            pcfa.float_id = pt.id 
            WHERE pcfa.float_reimbursement_id=0 AND 
                pt.id=". db_escape($float_id)." AND 
                pcfa.created_on >= ".db_escape(date2sql($from_date))." AND 
                pcfa.created_on <= ".db_escape(date2sql($to_date_midnight));
    
    $sql3 = "SELECT ".ST_PETTY_CASH_FLOAT_REIMBUR." type_id, 
                pcfr.id,
                0 iou_id,
                '' project_description,
                pt.name,
                pcfr.tran_date,
                pcfr.amount,      
                0 advances_total,
                pcfr.row_state,
                0 batch_no,
                pcfr.from_float_id float_id,
                CONCAT('Paid to ', (SELECT name FROM ".TB_PREF."petty_cash_floats WHERE id = pcfr.float_id)) comment,
                pcfr.created_on 
            FROM ".TB_PREF."i_owe_you_float_reimbursements pcfr 
                    LEFT JOIN ".TB_PREF."petty_cash_floats pt ON 
                            pcfr.from_float_id = pt.id 
            WHERE pcfr.float_reimbursement_id=0 AND 
                pt.id=". db_escape($float_id)." AND 
                pcfr.created_on >= ".db_escape(date2sql($from_date))." AND 
                pcfr.created_on <= ".db_escape(date2sql($to_date_midnight));
        
    $sql4 = "SELECT ".ST_PETTY_CASH_DIRECT_PAYMENTS." type_id,
                    ioup.id id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    ioup.payment_date tran_date,
                    ioup.amount,   
                    0 advances_total,  
                    ioup.row_state,
                    0 batch_no,
                    ioup.float_id,
                    ioup.comment comment,
                    ioup.created_on 
            FROM ".TB_PREF."i_owe_you_payments ioup,
                 ".TB_PREF."petty_cash_floats pt 
                                      
            WHERE ioup.iou_id=0 AND 
                    ioup.float_reimbursement_id=0 AND 
                    ioup.float_id = pt.id AND 
                    ioup.float_id = ". db_escape($float_id)." AND 
                    ioup.created_on >= ".db_escape(date2sql($from_date))." AND 
                    ioup.created_on <= ".db_escape(date2sql($to_date_midnight))." 
            ORDER BY created_on, id ";
    
    $sql = $sql1." UNION ".$sql2." UNION ".$sql3." UNION ".$sql4;
    
  return db_query($sql, "Could not get I Owe You Float Reimbursements");   
}



//function update_petty_cash_reimbursement($bank_account, $cheque_number, )
//{
//    $sql = "UPDATE ".TB_PREF."petty_cash_float_reimbursements
//            SET bank_account=$bank_account ,
//                cheque_number=$cheque_number ,
//                date=$date
//            WHERE id=".$id;
//    
//    db_query($sql, "Could not update selected");  
//    return db_num_affected_rows();    
//    
//}

//----------------------------------------------------------------------------------------
//function add_selected_petty_cash_reimbursement_total_amount($total_amount)
//{
//    $sql = "INSERT INTO ".TB_PREF."petty_cash_float_reimbursements
//                     (amount)
//                     
//                   VALUES (".db_escape($total_amount).")";                           
//        
//    db_query($sql, "Could not insert total amount of selected ID");
//    return db_insert_id();   
//    
//}

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

function add_i_owe_you_float_reimbursement($float_id, $reference, $from_float_id, 
                                            $bank_account_id, $credit_account, $debit_account, 
                                            $cheque_number, $tran_date, $total_amount,
                                            $bank_payment_id)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');        
    
    $sql = "INSERT INTO ".TB_PREF."i_owe_you_float_reimbursements
                     (float_id, reference, from_float_id,
                     bank_account_id, credit_account, 
                     debit_account, cheque_number, 
                     tran_date, amount,
                     bank_payment_id, 
                     created_by, created_on)
             VALUES(".db_escape($float_id).",".db_escape($reference).",".db_escape($from_float_id).",
                     ".db_escape($bank_account_id).",".db_escape($credit_account).",
                     ".db_escape($debit_account).",".db_escape($cheque_number).",
                     ".db_escape($tran_date).",".db_escape($total_amount).",
                     ".db_escape($bank_payment_id).",
                     ".db_escape($created_by).",".db_escape($created_on).")";

    db_query($sql, "Could not add petty cash cheques");

    return db_insert_id();
        
}

//function get_petty_cash_payments_trans_number()
//{
//    $sql = "SELECT *
//            FROM ".TB_PREF."petty_cash_payments
//            WHERE 1=1"; 
//    
//    return db_query($sql);    
//}

//function update_petty_cash_float_reimbursement_trans_no($trans_no, $fake_id)
//{
//    $sql = "UPDATE ".TB_PREF."petty_cash_payments
//            SET trans_number = $trans_no
//            WHERE trans_number=".$fake_id;
//
//  db_query($sql, "Could not update trans number");  
//  return db_num_affected_rows();     
//}
//

function update_reimbursement_float_reimbursement_amount($id, $reimbursed_amount, $row_state)
{
    $sql = "UPDATE ".TB_PREF."i_owe_you_float_reimbursements 
            SET reimbursed_amount = $reimbursed_amount ,
                row_state =". db_new_row_state($row_state)."  
            WHERE ABS(amount) >= ".db_escape($reimbursed_amount)." AND 
                    id =".db_escape($id)." AND 
                    row_state =".db_escape($row_state);
    
    db_query($sql, "update_reimbursement_float_reimbursement_amount");  
    return db_num_affected_rows();    
    
}




function add_float_reimbursement_details($reimbursement_id, $batch_tran_type, $batch_type_no, $tran_type, $type_no, $reimbursement_amount)
{

    $sql = "INSERT INTO ".TB_PREF."float_reimbursement_details (
                reimbursement_id, 
                batch_tran_type, batch_type_no, 
                tran_type, type_no, 
                amount)                       
            VALUES ("
                .db_escape($reimbursement_id).", "
                .db_escape($batch_tran_type).", ".db_escape($batch_type_no).", "
                .db_escape($tran_type).", ".db_escape($type_no).", "
                .db_escape($reimbursement_amount).")";
                    
        
    db_query($sql,"add_float_reimbursement_details");
    
    return db_insert_id();

}

//

function get_transaction_total_reimbursed_amount($tran_tye, $tyep_no)
{
    $sql = "SELECT SUM(amount) total_reimbursed_amount
            FROM ".TB_PREF."float_reimbursement_details 
            WHERE tran_type =".db_escape($tran_tye)." AND 
                    type_no =".db_escape($tyep_no);
    
    $result = db_query($sql, "get_transaction_total_reimbursed_amount");  
    
    return db_result_value($result, 'total_reimbursed_amount');   
    
}

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

function get_bank_float_payments($float_id, $bank_acc_id)
{
	$sql = "SELECT bt.*, 
                    lv.value_name gl_trans_type_name 

		FROM ".TB_PREF."bank_trans bt 
                    JOIN ".TB_PREF."list_values lv 
                        ON lv.id = bt.gl_trans_type_id 
                    LEFT JOIN ".TB_PREF."i_owe_you_float_reimbursements fr 
                        ON bt.id = fr.bank_payment_id 

		WHERE bt.type=".ST_BANKPAYMENT." 
                    AND bt.person_type_id = ".PT_FLOAT." 
                    AND bt.person_id = ".db_escape($float_id)." 
                    AND bt.bank_act = ".db_escape($bank_acc_id)." 
                    AND fr.id IS NULL 
                ORDER BY bt.trans_date, bt.id";

	return db_query($sql, "get_bank_float_payments");
}

function check_bank_payment_already_used($bank_payment_id)
{
    $sql = "SELECT fr.* 
            FROM ".TB_PREF."i_owe_you_float_reimbursements fr 
            WHERE fr.bank_payment_id  = ".db_escape($bank_payment_id);
    
    $result = db_query($sql, "check_bank_payment_already_used");
    
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success"=>false, "message"=>"Selected bank payment already used in float reimbursement #".$row['id']);
    }
    else{
        return array("success"=>true, "message"=>"");
    }
    
}