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


function view_i_owe_you_float_reimbursement_details($id)
{
    $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.float_reimbursement_id=".db_escape($id);
    
    $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=".db_escape($id);
    
    $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=".db_escape($id);
    
    $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_id = pt.id AND 
                    ioup.float_reimbursement_id = ". db_escape($id)." 
            ORDER BY created_on, id ";
    
    $sql = $sql1." UNION ".$sql2." UNION ".$sql3." UNION ".$sql4;
    
  return db_query($sql, "Could not view_i_owe_you_float_reimbursement_details");   
}

        

function view_float_reimbursement_header($id)
{
        $sql= "SELECT 
            flr.*,
            fl.name float_name 
        FROM ".TB_PREF."i_owe_you_float_reimbursements flr 
                INNER JOIN ".TB_PREF."petty_cash_floats fl ON 
                        flr.float_id = fl.id 
                
        WHERE flr.id = ".db_escape($id);
        
	$result = db_query($sql,"Could not view_i_owe_you_advances_header");	
    
    return db_fetch($result);
}
  

function view_petty_cash_float_reimbursement_float_reimbursements($id)
{
    
    $sql = "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= ". db_escape($id);


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


function view_petty_cash_float_reimbursement_float_adjustments($id)
{
    
$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,         
                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= ". db_escape($id);

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


function view_petty_cash_float_reimbursement_iou_payments($id)
{    
    $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,   
                    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.float_id = pt.id AND 
                    ioup.expense_source IN(".ST_I_OWE_YOU.") AND 
                    ioup.float_reimbursement_id= ". db_escape($id)." 
            ORDER BY created_on, id ";

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


function view_petty_cash_float_reimbursement_petty_cash_payments($id)
{
    
    $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,   
                    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.float_id = pt.id AND 
                    ioup.expense_source IN(".ST_PETTY_CASH.") AND 
                    ioup.float_reimbursement_id= ". db_escape($id)." 
            ORDER BY created_on, id ";

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


function view_petty_cash_float_reimbursement_direct_petty_cash_payments($id)
{
    
    $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,   
                    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.float_id = pt.id AND 
                    ioup.expense_source IN(".ST_VEHICLE_RUNNING_CHART.",".ST_VEHICLE_ALLOCATION.") AND 
                    ioup.float_reimbursement_id= ". db_escape($id)."  
            ORDER BY created_on, id ";

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



function view_petty_cash_float_reimbursement_iou_advances($id)
{
    
    $sql = "SELECT ".ST_I_OWE_YOU_ADVANCES_BATCH." type_id,
                    a.batch_no id,
                    d.iou_id,
                    '' project_description,
                    pt.name,
                    a.created_on tran_date,
                    d.amount,   
                    0 advances_total,  
                    d.row_state,
                    0 batch_no,
                    d.float_id,
                    '' comment,
                    a.created_on 
            FROM ".TB_PREF."i_owe_you_advances a,
                 ".TB_PREF."i_owe_you_advances_details d,
                 ".TB_PREF."petty_cash_floats pt   
                                      
            WHERE a.batch_no = d.batch_no AND 
                    d.float_id = pt.id AND 
                    d.float_reimbursement_id= ". db_escape($id)." 
            ORDER BY created_on, a.batch_no, d.id ";

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


function view_petty_cash_float_reimbursement_project_float_advances($id)
{
    
    $sql = "SELECT ".ST_PROJECT_ADVANCE_BATCH." type_id,
                    a.id,
                    0 iou_id,
                    '' project_description,
                    pt.name,
                    a.tran_date,
                    d.amount,   
                    0 advances_total,  
                    d.row_state,
                    0 batch_no,
                    a.float_id,
                    '' comment,
                    a.created_on 
            FROM ".TB_PREF."project_advance_batches a,
                 ".TB_PREF."project_advance_batch_details d,
                 ".TB_PREF."petty_cash_floats pt   
                                      
            WHERE a.id = d.pab_id AND 
                    a.float_id = pt.id AND 
                    d.float_reimbursement_id=".db_escape($id)." 
            ORDER BY created_on, a.id, d.id ";

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


function view_petty_cash_float_reimbursement_iou_expense_batches($id)
{    
    
    $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.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.float_reimbursement_id = ". db_escape($id)." 
            ORDER BY ioub.created_on, ioub.batch_no";
    
  return db_query($sql, "Could not get_i_owe_you_payments_for_float_reimbursement");   
}


function view_petty_cash_float_reimbursement_supplier_payments($id)
{
    
$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,         
                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 st.float_reimbursement_id=".db_escape($id)."  AND 
                st.type = ".ST_SUPPAYMENT." AND 
                pt.id = st.float_id";

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


function view_petty_cash_float_reimbursement_details($type_id, $trans_no)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."float_reimbursement_details 
            WHERE tran_type = ".db_escape($type_id)."
                AND type_no = ".db_escape($trans_no);
    
    return db_query($sql, "view_petty_cash_float_reimbursement_details");   
}

function view_petty_cash_float_reimbursement_transactions($id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."float_reimbursement_details 
            WHERE reimbursement_id = ".db_escape($id);
    
    return db_query($sql, "view_petty_cash_float_reimbursement_transactions");   
}