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


function get_project_id_by_name($project_name)
{
    $sql = "SELECT *
            FROM ".TB_PREF."projects_master
            WHERE description=".db_escape($project_name);

    $result = db_query($sql);
  
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['project_id']);
    }

        return array("success" => false, "message" => "Unknown project name $project_name");
}


function get_customer_id_by_name($customer_name)
{
    $sql = "SELECT *
            FROM ".TB_PREF."debtors_master
            WHERE name=".db_escape($customer_name);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['debtor_no']);
    }

        return array("success" => false, "message" => "Unknown customer name $customer_name");
}


function get_branch_id_by_name($branch_name, $debtor_no)
{
    $sql = "SELECT *
            FROM ".TB_PREF."cust_branch
            WHERE br_name=".db_escape($branch_name)." AND
                debtor_no=".db_escape($debtor_no);                   
    
    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['debtor_no'], "tax_group_id"=> $row['tax_group_id']);
    }

        return array("success" => false, "message" => "Unknown branch name $branch_name");
}


function get_payment_term_id_by_name($payment_term)
{
    $sql = "SELECT *
            FROM ".TB_PREF."payment_terms
            WHERE terms=".db_escape($payment_term);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['terms_indicator']);
    }

        return array("success" => false, "message" => "Unknown payment term name $payment_term");
}



function get_sales_person_id_by_name($salesman_name)
{
    $sql = "SELECT *
            FROM ".TB_PREF."salesman
            WHERE salesman_name=".db_escape($salesman_name);
             

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['salesman_code']);
    }

        return array("success" => false, "message" => "Unknown salesman name $salesman_name");
}

//-----------------------------------------------------------------------------------

function check_payment_application_id($payment_application_id, $project_id)
{
    $sql = "SELECT order_no 
            FROM ".TB_PREF."project_payment_applications
            WHERE order_no=".db_escape($payment_application_id)." 
                    AND project_id = ".db_escape($project_id);
             

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['order_no']);
    }

        return array("success" => false, "message" => "Unknown payment application for the project");
}

//-----------------------------------------------------------------------------------


function get_tax_group_id_by_name($tax_group_name)
{
    $sql = "SELECT id 
            FROM ".TB_PREF."tax_groups
            WHERE name=".db_escape($tax_group_name);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['id']);
    }

        return array("success" => false, "message" => "Unknown tax type group");
}


//-----------------------------------------------------------------------------------


function get_tax_type_id_by_name($tax_type_name)
{
    $sql = "SELECT id 
            FROM ".TB_PREF."tax_types
            WHERE name=".db_escape($tax_type_name);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['id']);
    }

        return array("success" => false, "message" => "Unknown tax type name $tax_type_name");
}





function check_customer_project_sales_order_id($project_sales_order_id, $customer_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."project_sales_orders
            WHERE order_no=".db_escape($project_sales_order_id)." 
                    AND debtor_no = ".db_escape($customer_id);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['order_no']);
    }

        return array("success" => false, "message" => "Unknown project sales order for the customer");
}




function get_unit_id_by_name($unit)
{
    $sql = "SELECT *
            FROM ".TB_PREF."item_units
            WHERE name=".db_escape($unit);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['abbr']);
    }

        return array("success" => false, "message" => "Unknown unit name $unit");
    
}


function get_line_category_id_by_name($category_name)
{
    $sql = "SELECT id category_id
            FROM ".TB_PREF."list_values
            WHERE value_name=".db_escape($category_name);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['category_id']);
    }

        return array("success" => false, "message" => "Unknown category name $category_name");
    
}



function get_debit_account_id_by_name($debit_account_code)
{
    $sql = "SELECT account_code debit_account_code
            FROM ".TB_PREF."chart_master
            WHERE account_name=".db_escape($debit_account_code);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['debit_account_code']);
    }

        return array("success" => false, "message" => "Unknown debit account name $debit_account_code");
}


function get_credit_account_id_by_name($credit_account_name)
{
    $sql = "SELECT account_code credit_account_code
            FROM ".TB_PREF."chart_master
            WHERE account_name=".db_escape($credit_account_name);

    $result = db_query($sql);
   
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['credit_account_code']);
    }

        return array("success" => false, "message" => "Unknown credit account name $credit_account_name");

}
 
 
 
 
?>