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

function get_petty_cash_floats()
{
    $sql= "SELECT f.*, cmfl.account_name, cmcr.account_name credit_account_name   
            FROM ".TB_PREF."petty_cash_floats f 
                    LEFT JOIN ".TB_PREF."chart_master cmfl 
                        ON f.account_code = cmfl.account_code 
                    LEFT JOIN ".TB_PREF."chart_master cmcr 
                        ON f.credit_account = cmcr.account_code ";
    
    return $result = db_query($sql, "Could not petty cash floats");
    
}

function get_petty_cash_float_line($id)
{
    $sql= "SELECT * 
           FROM ".TB_PREF."petty_cash_floats
           WHERE id=". db_escape($id); 
    
    $result = db_query($sql, "Could not petty cash floats");
    return db_fetch($result);
}

function add_petty_cash_float($name, $amount, $reimbursement_method, $account_code, $credit_account)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql= "INSERT INTO ".TB_PREF."petty_cash_floats(name, amount, reimbursement_method, account_code, credit_account, created_by, created_on)
           VALUES(".db_escape($name).",".db_escape($amount).",".db_escape($reimbursement_method).","
                .db_escape($account_code).",".db_escape($credit_account).","
                .db_escape($created_by).",".db_escape($created_on)." )"; 
    
    db_query($sql,"Could not add petty cash float details");
    
}

function update_petty_cash_float($id, $name, $amount, $reimbursement_method, $account_code, $credit_account)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql=  "UPDATE ".TB_PREF."petty_cash_floats 
            SET name =".db_escape($name).",
                amount =".db_escape($amount).",
                reimbursement_method=".db_escape($reimbursement_method).",
                account_code =".db_escape($account_code).",
                credit_account =".db_escape($credit_account).",
                updated_by = ".db_escape($updated_by).", 
                updated_on = ".db_escape($updated_on)."  
           WHERE id=".db_escape($id); 
    
    db_query($sql,"Could not update petty cash float details");

}

function delete_petty_cash_float($selected_id)
{
    $sql = "DELETE FROM ".TB_PREF."petty_cash_floats    
                WHERE id=".db_escape($selected_id);

    db_query($sql,"Could not delete petty cash float");
        
    return db_num_affected_rows();
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
function get_petty_cash_float_users($float_id)
{
    $sql= "SELECT pc.id,
                  pc.blocked,
                  u.real_name,
                  pc.created_on
           FROM ".TB_PREF."petty_cash_float_users pc 
                LEFT JOIN ".TB_PREF."users u ON
                        pc.user_id = u.id 
           WHERE pc.float_id = ". db_escape($float_id)." 
           ORDER BY u.real_name ";
    
    return $result = db_query($sql, "Could not petty cash users");
   
    
}

function add_petty_cash_user($float_id,$user_id,$block)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql= "INSERT INTO ".TB_PREF."petty_cash_float_users(float_id,user_id,blocked,created_by,created_on)
           VALUES(".db_escape($float_id).",".db_escape($user_id).",".db_escape($block).",".db_escape($created_by).",".db_escape($created_on)." )"; 
    
    db_query($sql,"Could not add petty cash user");
        
 
    return db_insert_id();
    
}

function get_petty_cash_user($id)
{
    $sql= "SELECT * 
           FROM ".TB_PREF."petty_cash_float_users
           WHERE id=". db_escape($id); 
    
    $result = db_query($sql, "Could not petty cash user");
    return db_fetch($result);
}

function update_petty_cash_user($id,$float_id,$user_id,$block)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql=  "UPDATE ".TB_PREF."petty_cash_float_users 
            SET float_id =".db_escape($float_id).",
               user_id =".db_escape($user_id).",
               blocked =".db_escape($block).",    
               updated_by = ".db_escape($updated_by).", 
               updated_on = ".db_escape($updated_on)."  
           WHERE id=".db_escape($id); 
    
    db_query($sql,"Could not update petty cash user");    
}

function delete_petty_cash_user($selected_id)
{
    $sql = "DELETE FROM ".TB_PREF."petty_cash_float_users    
                WHERE id=".db_escape($selected_id);

    db_query($sql,"Could not delete petty cash user");
        
    return db_num_affected_rows();
}


function get_petty_cash_float_user($float_id, $user_id)
{
    $sql= "SELECT * 
           FROM ".TB_PREF."petty_cash_float_users
           WHERE float_id=". db_escape($float_id)." AND 
               user_id=". db_escape($user_id);
    
    $result = db_query($sql, "Could not get_petty_cash_float_user");
    return db_fetch($result);
}

function check_petty_cash_float_access($float_id, $user_id)
{
    $float_user = get_petty_cash_float_user($float_id, $user_id);
    if(isset($float_user['user_id'])){
        if($float_user['blocked'] == true){
            return array("success" => false, "message" => "Your access has been blocked for this foat.");
        }
        return array("success" => true, "message" => ""); 
    }
    else{
         return array("success" => false, "message" => "You do not have permission to create transactions for this float.");
    }
}

function get_petty_cash_float_name($id) {
    $sql= "SELECT name 
           FROM ".TB_PREF."petty_cash_floats
           WHERE id = ". db_escape($id); 
    
    $result = db_query($sql, "get_petty_cash_float_name");
    
    return db_result_value($result, 'name', '');
}