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

function get_sql_for_project_payment_application_inquiry($project_id, $from_date, $to_date)
{
    $sql = "SELECT  ppa.order_no,
                    pm.description,
                    pm.department_id,
                    pm.project_manager_name,
                    dm.name,                 
                    ppa.reference,
                    ppa.ord_date tran_date
            
            FROM ".TB_PREF."project_payment_applications ppa,
                 ".TB_PREF."projects_master pm,
                 ".TB_PREF."debtors_master dm
            
            WHERE ppa.project_id = pm.project_id AND
                  ppa.customer_id = dm.debtor_no AND
                  ppa.ord_date >= ".db_escape(date2sql($from_date))." AND ppa.ord_date <=".db_escape(date2sql($to_date));
            
                  
    if($project_id > 0){
        
        $sql .= " AND ppa.project_id=".db_escape($project_id);
    }
    

    
//    if($project_manager_name == 0){
//        $sql .= " AND ppa.project_manager_name=".db_escape($project_manager_name); 
//    }    
//            
//    if($from_date > $to_date){
//        
//        $sql .= "ORDER BY ppa.date ";
//    }          
    
    $sql .= " ORDER BY order_no ";
    
    return $sql; 
}
        

function view_project_payment_application_header($payment_application_id)
{
   $sql = "SELECT ppa.*,
                pm.description project_description,
                lv.value_name department_name,
                dm.name customer_name,
                pm.project_manager_name,
                ps.description project_site_description,
                cb.br_name cust_branch_name 
            FROM ".TB_PREF."project_payment_applications ppa 
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    ppa.project_id = pm.project_id
                LEFT JOIN ".TB_PREF."debtors_master dm ON
                    ppa.customer_id = dm.debtor_no 
                LEFT JOIN ".TB_PREF."project_sites ps ON
                    ppa.site_id = ps.id 
                LEFT JOIN ".TB_PREF."list_values lv ON
                    pm.department_id = lv.id  
                LEFT JOIN ".TB_PREF."cust_branch cb ON
                    ppa.branch_code = cb.branch_code  

            WHERE ppa.order_no = ".db_escape($payment_application_id);

   $result = db_query($sql, "Could not get view_project_payment_application_header");
   return db_fetch($result);     
}
      
        
function view_project_payment_application_boq_details($project_id, $payment_application_id, $boq_group_id)
{
    $sql = "SELECT  boqi.*,
                    pad.id pad_id,     
                    pad.total_work_done,
                    pad.comment,                 
                    pad.previous_qty,
                    pad.quantity 
                  
            FROM ".TB_PREF."project_boq_items boqi 
                    INNER JOIN ".TB_PREF."project_payment_application_details pad ON 
                            boqi.id = pad.boq_item_id AND 
                            pad.pa_id =".db_escape($payment_application_id)." 
                                    
            WHERE boqi.project_id =".db_escape($project_id)." AND 
                    boqi.group_id =".db_escape($boq_group_id);                 
             
   return db_query($sql, "Could not get view_project_payment_application_boq_details");    
   
}


?>