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


function get_sql_for_pc_view($project_id = null, $task_id = null, $filterType = null, 
            $created_on_from = null, $created_on_to = null)
{
        $filter = "";
        
        if(!($project_id > 0))
        {
                $project_id = 0;  
        }
        
        if($task_id > 0)
        {
                $filter .= " AND pc.task_id = ".db_escape($task_id);            
        }

        if ($filterType > 0)
        {
                $filter .= " AND pc.tran_type = ".db_escape($filterType); 
        }

        //$filter = "";

            $sql = "SELECT
                        pc.id,
                        boqg.description boq_item_group_description,
                        pc.task_id,
                        pt.description task_description ,
                        pc.tran_type,
                        pc.tran_no,
                        pc.added_on,
                        pc.amount,
                        pc.total_cost 
                        
                    FROM 
                        ".TB_PREF."task_costs pc 
                            JOIN ".TB_PREF."project_tasks pt ON 
                                pc.task_id = pt.task_id 
                            JOIN ".TB_PREF."projects_master pm ON 
                                pt.project_id = pm.project_id 
                            JOIN ".TB_PREF."project_task_groups ptg ON 
                                pt.task_group_id = ptg.id 
                            LEFT JOIN ".TB_PREF."project_boq_item_groups boqg ON 
                                ptg.boq_group_id = boqg.id
                    WHERE  
                        pc.added_on >= ".db_escape(date2sql($created_on_from))." AND
                        pc.added_on <= ".db_escape(date2sql($created_on_to))." AND                         
                        pt.project_id = ".db_escape($project_id)." "
                        .$filter." 
                    ORDER BY
                        pc.id";
        

	return $sql;
}

function view_project_cost_details($cost_id)
{
  
       $sql = "SELECT
                        pm.description project_description,
                        lv.value_name task_description ,
                        pc.ref_tran_type,
                        pc.ref_tran_no,
                        pc.deduction,
                        pc.tran_date,
                        pc.amount,
                        pc.comment
                    FROM 
                        ".TB_PREF."task_cost_adjustments pc,
                        ".TB_PREF."projects_master pm,
                        ".TB_PREF."project_tasks pt, 
                        ".TB_PREF."list_values lv 
                    WHERE 
                        pc.task_id = pt.task_id AND 
                        pt.type_id = lv.id AND
                        pt.project_id = pm.project_id AND 
                        pc.id = ".db_escape($cost_id);
        
        //$filter = "";
        $result = db_query($sql,"Project cost details cound not found");
	return db_fetch($result);
}



function view_project_cost_transaction_details($task_id)
{
    $sql = "SELECT
                pc.id,
                pc.tran_type,
                pc.tran_no,
                pc.added_on,
                pc.amount,
                pc.total_cost 

            FROM 
                ".TB_PREF."task_costs pc   

            WHERE  pc.task_id = ".$task_id."
            ORDER BY
                pc.added_on";
        
    return db_query($sql,"view_project_cost_transaction_details");
}

function view_task_details($task_id)
{
	$sql = "SELECT pt.*,
                    pm.description project_description,
                    boqg.description boq_item_group_description,
                    ptg.description task_group_description,
                    dtn.value_name detail_type_name 
                FROM ".TB_PREF."project_tasks pt 
                    JOIN ".TB_PREF."projects_master pm ON 
                        pt.project_id = pm.project_id 
                    JOIN ".TB_PREF."project_task_groups ptg ON 
                        pt.task_group_id = ptg.id 
                    JOIN ".TB_PREF."list_values dtn ON 
                        pt.detail_type_code = dtn.value_code AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'  
                    LEFT JOIN ".TB_PREF."project_boq_item_groups boqg ON 
                        boqg.id = ptg.boq_group_id
            WHERE  pt.task_id =".db_escape($task_id);
	
	$result = db_query($sql,"task details could not be retreived");
	
	return db_fetch($result);
}
?>