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

function get_petty_cash_float_adjustments($float_id)
{
        $sql= "SELECT * 
               FROM ".TB_PREF."petty_cash_float_adjustments 
               WHERE id=".db_escape($float_id);             
        
return db_query($sql,"Could not get petty cash float adjustments");	
       //  db_fetch($result);    
}


function petty_cash_float_adjustments_for_edit($id)
{
        $sql= "SELECT *
               FROM ".TB_PREF."petty_cash_float_adjustments 
               WHERE id=". db_escape($id);
        
        $result = db_query($sql,"Could not get petty cash float adjustments");	
        return db_fetch($result);
    
}

function add_petty_cash_float_adjustment($reference, $is_return,$float_id, $tran_date, $amount, $comment)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');        
        
	$sql = "INSERT INTO ".TB_PREF."petty_cash_float_adjustments 
                    ( reference, is_return, float_id,
                      tran_date, amount, comment,
                      created_by, created_on) 
                      
            VALUES (".db_escape($reference)."," 
                     .db_escape($is_return).","
                     .db_escape($float_id).","  
                     .db_escape(date2sql($tran_date))."," 
                     .db_escape($amount)."," 
                     .db_escape($comment)."," 
                     .db_escape($created_by).","
                     .db_escape($created_on).")";
                     
        
    db_query($sql,"Could not add petty cash float adjustments ");        
    return db_insert_id();

}


function delete_petty_cash_float_adjustments($rec_id)
{
        $sql = "DELETE FROM ".TB_PREF."petty_cash_float_adjustments   
                WHERE id=".db_escape($rec_id);

        db_query($sql,"Could not delete petty_cash_float_adjustments");
        
        return db_num_affected_rows();
}


function update_adjustment_float_reimbursement_amount($id, $reimbursed_amount, $row_state)
{
    $sql = "UPDATE ".TB_PREF."petty_cash_float_adjustments
            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_adjustment_float_reimbursement_amount");  
    return db_num_affected_rows();    
    
}


function view_petty_cash_float_adjustment($id)
{
        $sql= "SELECT fa.*,
                    pf.name float_name,
                    u.real_name user_name
               FROM ".TB_PREF."petty_cash_float_adjustments fa
                    INNER JOIN ".TB_PREF."petty_cash_floats pf ON
                            pf.id = fa.float_id
                    INNER JOIN ".TB_PREF."users u ON
                            u.id = fa.created_by
               WHERE fa.id=".db_escape($id);             
        
        $result = db_query($sql,"Could not get petty cash float adjustments");	
        return db_fetch($result);    
}




function view_petty_cash_float_adjustment_by_type_id_and_trans_no($type_id, $trans_no)

{

        $sql= "SELECT fa.*,

                    pf.name float_name,

                    u.real_name user_name

               FROM ".TB_PREF."petty_cash_float_adjustments fa

                    INNER JOIN ".TB_PREF."petty_cash_floats pf ON

                            pf.id = fa.float_id

                    INNER JOIN ".TB_PREF."users u ON

                            u.id = fa.created_by

               WHERE fa.type=".db_escape($type_id)."     

                    AND fa.trans_no=".db_escape($trans_no);             

        

        $result = db_query($sql,"Could not get petty cash float adjustments");	

        return db_fetch($result);    

}







function add_petty_cash_transaction_currency_denominations($trans_type, $trans_no, $denomination_id, $quantity)

{

    $created_by = $_SESSION['wa_current_user']->user;

    $created_on = date('Y-m-d H:i:s');        

        

	$sql = "INSERT INTO ".TB_PREF."petty_cash_currency_denominations  

                    ( type , trans_no, 

                      denomination_id, quantity, 

                      created_by, created_on) 

                      

            VALUES (".db_escape($trans_type).",".db_escape($trans_no).","

                     .db_escape($denomination_id).",".db_escape($quantity).","  

                     .db_escape($created_by).",".db_escape($created_on).")";

                     

        

    db_query($sql,"add_petty_cash_transaction_currency_denominations");       

    

}





function delete_petty_cash_transaction_currency_denominations($trans_type, $trans_no)

{

    $sql = "DELETE FROM ".TB_PREF."petty_cash_currency_denominations  

            WHERE type = ".db_escape($trans_type)."  

                    AND trans_no = ".db_escape($trans_no);



    return db_query($sql, "delete_petty_cash_transaction_currency_denominations");   

    

}





function get_petty_cash_transaction_currency_denominations($trans_type, $trans_no)

{

    $sql = "SELECT cd.id, cd.value_code, cd.value_name,

		d.denomination_id, d.quantity 

            FROM ".TB_PREF."list_values cd  

                LEFT JOIN ".TB_PREF."petty_cash_currency_denominations d  

                    ON cd.id = d.denomination_id 

                    AND d.type = ".db_escape($trans_type)."  

                    AND d.trans_no = ".db_escape($trans_no)." 

            WHERE cd.list_code = 'CURRENCY_DENOMINATIONS' 

            ORDER BY cd.display_order ";  



    return db_query($sql, "get_petty_cash_transaction_currency_denominations");   

    

}



function view_petty_cash_transaction_currency_denominations($trans_type, $trans_no)

{

    $sql = "SELECT cd.id, cd.value_code, cd.value_name,

		d.denomination_id, d.quantity 

            FROM ".TB_PREF."list_values cd  

                JOIN ".TB_PREF."petty_cash_currency_denominations d  

                    ON cd.id = d.denomination_id 

            WHERE cd.list_code = 'CURRENCY_DENOMINATIONS' 

                    AND d.type = ".db_escape($trans_type)."  

                    AND d.trans_no = ".db_escape($trans_no)." 

            ORDER BY cd.display_order ";  



    return db_query($sql, "view_petty_cash_transaction_currency_denominations");   

    

}