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


function get_sql_for_project_sales_orders_inquiry($project_id, $customer_id, $from_date, $to_date)
{
    $sql = "SELECT pso.order_no,
                    pm.description project_description,
                    dm.name customer_name,
                    cb.br_name branch_name,
                    pso.reference,
                    pso.ord_date,
                    pt.terms payment_terms,
                    s.salesman_name,
                    pso.po_reference,
                    pso.rate_include_taxes,
                    tg.name tax_group_name

            FROM ".TB_PREF."project_sales_orders pso, 
                    ".TB_PREF."projects_master pm,
                    ".TB_PREF."debtors_master dm,
                    ".TB_PREF."cust_branch cb,
                    ".TB_PREF."payment_terms pt,
                    ".TB_PREF."salesman s,
                    ".TB_PREF."tax_groups tg                               

            WHERE pso.project_id = pm.project_id AND
                    dm.debtor_no = pso.debtor_no AND
                    cb.branch_code = pso.branch_code AND
                    pt.terms_indicator = pso.payment_terms AND
                    s.salesman_code = pso.salesman_code AND
                    tg.id = pso.tax_group_id AND
                    pso.ord_date >=".db_escape(date2sql($from_date))." AND  pso.ord_date <=".db_escape(date2sql($to_date));
      
   
    if($project_id > 0){
        $sql .= "AND pso.project_id=".db_escape($project_id);
    }
    
    if($customer_id > 0){
        $sql .= "AND pso.debtor_no=".db_escape($customer_id);
    }
    
//    if($project_id > 0){
//        $sql .= "AND pso.project_id=".db_escape($project_id);
//    }
//    
    
    
    return $sql;
    
}
        

function view_project_sales_orders_header($trans_no)
{
    $sql = "SELECT pso.order_no,
                    pm.description project_description,
                    dm.name customer_name,
                    cb.br_name branch_name,
                    pso.reference,
                    pso.ord_date,
                    pt.terms payment_terms,
                    s.salesman_name,
                    pso.po_reference,
                    pso.rate_include_taxes,
                    tg.name tax_group_name,
                    pso.rate_include_taxes 

            FROM ".TB_PREF."project_sales_orders pso, 
                    ".TB_PREF."projects_master pm,
                    ".TB_PREF."debtors_master dm,
                    ".TB_PREF."cust_branch cb,
                    ".TB_PREF."payment_terms pt,
                    ".TB_PREF."salesman s,
                    ".TB_PREF."tax_groups tg                               

            WHERE pso.project_id = pm.project_id AND
                    dm.debtor_no = pso.debtor_no AND
                    cb.branch_code = pso.branch_code AND
                    pt.terms_indicator = pso.payment_terms AND
                    s.salesman_code = pso.salesman_code AND
                    tg.id = pso.tax_group_id AND
                    pso.order_no=".db_escape($trans_no);         
    
    $result = db_query($sql);
    return db_fetch($result);

}


function view_project_sales_orders_details($trans_no)
{
    $sql = "SELECT psod.*,
                    boqi.description boq_iem_description 
            FROM ".TB_PREF."project_sales_order_details psod 
                    LEFT JOIN ".TB_PREF."project_boq_items boqi ON 
                            psod.item_id = boqi.id 
            WHERE order_no=".db_escape($trans_no);
    
    return db_query($sql);   
    
}

        
        
?>