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


function add_petty_cash_expense_account($type_id, $debit_account, $coc_account)                                 
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
	$sql = "INSERT INTO ".TB_PREF."petty_cash_expense_accounts 
                        (type_id, debit_account, coc_account,
                            created_by, created_on)
                                                    
		VALUES (". db_escape($type_id).",
                        ".db_escape($debit_account).", ".db_escape($coc_account).","
                        . "".db_escape($created_by).", ".db_escape($created_on).")";
                    
    db_query($sql, "add_petty_cash_expense_account");
    return db_insert_id(); 
}

function update_petty_cash_expense_account($id, $debit_account, $coc_account)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."petty_cash_expense_accounts  SET 
                  
                    debit_account=".db_escape($debit_account).",
                    coc_account=".db_escape($coc_account).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."                        

            WHERE type_id=".db_escape($id);

    db_query($sql, "update_petty_cash_expense_account");
}


function get_all_petty_cash_expense_accounts()
{
    $sql = "SELECT pic.*,
                    da.account_name debit_account_name,
                    ca.account_name coc_account_name,
                    lv.value_name category_name
                    
            FROM ".TB_PREF."petty_cash_expense_accounts pic 
                LEFT JOIN ".TB_PREF."list_values lv ON
                    pic.type_id = lv.id

                LEFT JOIN ".TB_PREF."chart_master da ON 
                    pic.debit_account = da.account_code 
                    
                LEFT JOIN ".TB_PREF."chart_master ca ON 
                    pic.coc_account = ca.account_code  
                    
            WHERE 1=1 
            ORDER BY lv.value_name";	
    
    return db_query($sql, "get_all_petty_cash_expense_accounts");
}


function get_petty_cash_expense_account($selected_id)
{
    $sql = "SELECT * FROM ".TB_PREF."petty_cash_expense_accounts  WHERE type_id=".db_escape($selected_id);

    $result = db_query($sql, "get_petty_cash_expense_account");

    return db_fetch($result);
}

function delete_petty_cash_expense_account($id)
{
    $sql="DELETE FROM ".TB_PREF."petty_cash_expense_accounts WHERE type_id=".db_escape($id);

    db_query($sql, "delete_petty_cash_expense_account");
}


function get_petty_cash_expense_account_detail($selected_id)
{
    $sql = "SELECT spil.*,
                    lv.value_name type_name

            FROM ".TB_PREF."petty_cash_expense_accounts spil
                LEFT JOIN ".TB_PREF."list_values lv ON
                    spil.type_id = lv.id

            WHERE spil.type_id=".db_escape($selected_id);
    
    $result = db_query($sql, 'get_petty_cash_expense_account_detail');
    return db_fetch($result);    
}


function get_petty_cash_expense_account_record($category_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."petty_cash_expense_accounts
            WHERE type_id=".db_escape($category_id);
    
    $result = db_query($sql, "get_petty_cash_expense_account_record");
    return db_fetch($result);    
}



