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

function get_project_floats()
{
    $sql= "SELECT f.*,
                    pm.description project_description,
                    gc.company_name group_company_name 
            FROM ".TB_PREF."project_floats f 
                JOIN ".TB_PREF."projects_master pm ON 
                    f.project_id = pm.project_id 
                JOIN group_companies gc ON 
                    f.company_code = gc.company_code ";
    
    return $result = db_query($sql, "Could not project floats");
    
}

function get_project_float($id)
{
    $sql= "SELECT pf.*,
                    pm.description project_description 
           FROM ".TB_PREF."project_floats pf 
               JOIN ".TB_PREF."projects_master pm ON 
                    pf.project_id = pm.project_id 
           WHERE pf.id=". db_escape($id); 
    
    $result = db_query($sql, "Could not get_project_float");
    return db_fetch($result);
}


function get_project_float_by_project_id($project_id)
{
    $sql= "SELECT pf.* 
           FROM ".TB_PREF."project_floats pf  
           WHERE pf.project_id=". db_escape($project_id); 
    
    $result = db_query($sql, "Could not get_project_float_by_project_id");
    return db_fetch($result);
}

function add_project_float($project_id, $company_code, $employee_code, $amount, $comment, $inactive)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql= "INSERT INTO ".TB_PREF."project_floats(
                    project_id, company_code, 
                    employee_code, amount, 
                    comment, inactive,
                    created_by,created_on)
           VALUES(".db_escape($project_id).",".db_escape($company_code).","
                    .db_escape($employee_code).",".db_escape($amount).","
                    .db_escape($comment).",".db_escape($inactive).","
                    .db_escape($created_by).",".db_escape($created_on)." )"; 
    
    db_query($sql,"Could not add_project_float");

    
}

function update_project_float($id,$company_code, $employee_code, $amount, $comment, $inactive)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql=  "UPDATE ".TB_PREF."project_floats 
            SET company_code =".db_escape($company_code).",
               employee_code =".db_escape($employee_code).",
               amount =".db_escape($amount).",
               comment =".db_escape($comment).",
               inactive =".db_escape($inactive).",
               updated_by = ".db_escape($updated_by).", 
               updated_on = ".db_escape($updated_on)."  
           WHERE id=".db_escape($id); 
    
    db_query($sql,"Could not update project float details");

}


function add_to_project_float($tran_type, $type_no, $project_id, 
                                $company_code, $employee_code, $amount, 
                                $tran_date)
{
    $balance = get_project_float_balance($project_id);
    
    $balance = $balance + $amount;
    
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."project_float_trans(
                    project_id, tran_type, type_no, 
                    company_code, employee_code,
                    tran_date, amount, balance, 
                    created_on) 
            VALUES (".db_escape($project_id).", ".db_escape($tran_type).", ".db_escape($type_no).", "
                    .db_escape($company_code). ", ".db_escape($employee_code).", "
                    .db_escape($tran_date).", ".db_escape($amount). ", ".db_escape($balance).", "
                    .db_escape($created_on).")";
    
    db_query($sql,"Could not add_to_project_float");
}


function get_project_float_balance($project_id)
{
    $sql = "SELECT SUM(amount) balance 
            FROM ".TB_PREF."project_float_trans 
            WHERE project_id = ". db_escape($project_id);
    
    $bq = db_query($sql,"Could not project float balance");
    $b = db_fetch($bq);
    
    if(isset($b['balance']) && $b['balance'] != 0){
        return $b['balance'];
    }
    else{
        return 0;
    }
}


function check_project_float_exists($project_id)
{
    $sql= "SELECT id 
           FROM ".TB_PREF."project_floats 
           WHERE project_id = ". db_escape($project_id); 
    
    $result = db_query($sql, "Could not get_project_float");
    if(db_num_rows($result) > 0){
        return true;
    }
    else{
        return false;
    }

}



?>