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

function add_petty_cash_expenses_header($batch_no, $reference, $type_id, $project_id, 
                                        $float_id, $group_company_code, $employee_code,
                                        $tran_date, $comments)                                        
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');           
    
    $sql = "INSERT INTO ".TB_PREF."petty_cash_header(
                    reference, company_code, 
                    employee_code, project_id, 
                    float_id, 
                    type, tran_date, 
                    comments, batch_no,  
                    created_by, created_on) 
            VALUES (".db_escape($reference).", ".db_escape($group_company_code).", "
                    .db_escape($employee_code).", ".db_escape($project_id).", "
                    .db_escape($float_id).", "
                    .db_escape($type_id).", ".db_escape(date2sql($tran_date)).", "
                    .db_escape($comments).", ".db_escape($batch_no).", "                   
                    .db_escape($created_by).", ".db_escape($created_on).")";

    db_query($sql);
    
    return db_insert_id();

}


function update_petty_cash_expenses_header($pe_id, $reference, $type_id, $project_id, 
                                            $group_company_code, $employee_code,
                                            $tran_date, $comments)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
        
    $sql = "UPDATE ".TB_PREF."petty_cash_header
                SET 
                    reference=".db_escape($reference).",
                    type=".db_escape($type_id).",
                    project_id=".db_escape($project_id).", 
                    company_code=".db_escape($group_company_code).", 
                    employee_code=".db_escape($employee_code).",                    
                    tran_date=".db_escape(date2sql($tran_date)).", 
                    comments=".db_escape($comments).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."
            WHERE id=".db_escape($pe_id)." AND 
                    batch_complete = 0";
    
    db_query($sql);            
    
    return db_num_affected_rows();
    
}


function get_petty_cash_expenses_header($peh_id)
{
    $sql = "SELECT pceh.*,
                    ioub.approval_status,
                    pm.description project_description, 
                    f.name float_name,
                    pt.value_name type_name,
                    ioub.display_status  
            FROM ".TB_PREF."petty_cash_header pceh 
                    JOIN ".TB_PREF."i_owe_you_batches ioub ON
                            pceh.batch_no = ioub.batch_no
                    JOIN ".TB_PREF."projects_master pm ON
                            pceh.project_id = pm.project_id 
                    JOIN ".TB_PREF."petty_cash_floats f ON
                            pceh.float_id = f.id 
                    JOIN ".TB_PREF."list_values pt ON
                            pceh.type = pt.id 
                
            WHERE pceh.id=".db_escape($peh_id);
    
    $result = db_query($sql);
    return db_fetch($result);    
}

function get_petty_cash_expenses_header_by_batch_no($batch_no)
{
    $sql = "SELECT *
            FROM ".TB_PREF."petty_cash_header 
            WHERE batch_no=".db_escape($batch_no);
    
    $result = db_query($sql);
    return db_fetch($result);    
}


function update_petty_cash_expense_header_total($peh_id, $total, $row_state)
{
    $sql = "UPDATE ".TB_PREF."petty_cash_header
                SET amount=".db_escape($total).",
                    row_state = ".db_new_row_state($row_state)." 
            WHERE id=".db_escape($peh_id)." 
                AND row_state = ".db_escape($row_state)." 
                AND batch_complete = 0 ";
    
    db_query($sql);               
    
    return db_num_affected_rows();
}


function complete_petty_cash_expenses_header($id)
{
	$sql = "UPDATE ".TB_PREF."petty_cash_header  
            SET batch_complete = 1 
            WHERE id = ".$id." AND batch_complete = 0 ";
        
    db_query($sql,"Could not complete_petty_cash_expenses_header"); 
    
    return db_num_affected_rows();
}



?>