<?php

function add_to_monthly_summary_projects($month_id, $department_id, $project_id, 
                                            $calculation_type, $value_type, $value, 
                                            $calculation_run_id)
{
    if($value == 0){
        return;
    }
    
    global $summary_calculation_value_types;
    
	$sql = "INSERT INTO ".TB_PREF."monthly_summary_projects
                    (month_id, department_id, 
                    project_id, 
                    calculation_type, value_type, 
                    value, calculation_run_id) 
		VALUES( ".db_escape($month_id).",".db_escape($department_id).","
                    .db_escape($project_id).","
                    .db_escape($calculation_type).",".db_escape($value_type).","
                    .db_escape($value).",".db_escape($calculation_run_id).")";

	db_query($sql,"add_to_project_monthly_summary");
        
        $message = "month_id: ".$month_id
                    .",department_id: ".$department_id 
                    .",project_id: ".$project_id 
                    .",calculation_type: ".$calculation_type 
                    .",value_type: ".$value_type." - ".$summary_calculation_value_types[$value_type] 
                    .",value: ".$value;                                             
        write_summary_calculation_log($calculation_run_id, $message);
}


function get_monthly_summary_projects_next_project($department_id, $last_project_id)
{
    
    
    $sql="SELECT pm.project_id, pm.department_id, pm.description                
            FROM ".TB_PREF."projects_master pm                     
            WHERE pm.department_id = ".db_escape($department_id)."  
                AND pm.project_id > ".db_escape($last_project_id)." 
            ORDER BY pm.project_id  LIMIT 1";

    $result = db_query($sql,"get_monthly_summary_projects_next_project");
    return db_fetch($result);
}


function delete_monthly_summary_projects($month_id, $department_id, $calculation_type, $project_id)
{
    $sql = "DELETE FROM ".TB_PREF."monthly_summary_projects  
            WHERE month_id = ".db_escape($month_id)." 
                AND department_id = ".db_escape($department_id)." 
                AND calculation_type = ".db_escape($calculation_type)." 
                AND project_id > ".db_escape($project_id);

    db_query($sql,"delete_monthly_summary_projects");
}

function get_monthly_summary_projects_last_completed_project_id($month_id, $department_id, $calculation_type)
{
    $sql = "SELECT MAX(project_id) project_id 
            FROM ".TB_PREF."monthly_summary_projects  
            WHERE month_id = ".db_escape($month_id)." 
                AND department_id = ".db_escape($department_id)." 
                AND calculation_type = ".db_escape($calculation_type)." 
            ORDER BY project_id";

    $result = db_query($sql,"get_monthly_summary_projects_last_completed_project_id");
    $project = db_fetch($result);
    if(isset($project['project_id']) && $project['project_id'] > 0){
        return $project['project_id'];
    }
    else{
        return 0;
    }
}