<?php

function department_project_expenses_invoices_and_collections_graph_display(&$pg0, $department_id, $from_month_display_order, $to_month_display_order)
{
    department_project_expenses_invoices_and_collections_graph_set_values($pg0, $department_id, $from_month_display_order, $to_month_display_order);

    $random_id = 'dpecg_'.$department_id."_".$from_month_display_order."_".time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Department Project Expenses Invoices and Collections";
    $data['x_axis_name'] = "Month";
    $data['y_axis_name'] = "Value";
    $data['graph1_name'] = "Expenses";
    $data['graph2_name'] = "Invoices";
    $data['graph3_name'] = "Collections";
    
    
    //echo "<a href='".$path_to_root."/admin/dashboard_uda_level_1.php?month_id=".$month_id."&division_code=UDA_0001&level=".$level."'>";
    display_graph($pg0, $data, 1, $random_id);//line graph = 4
//    source_graphic($today, $title, , $pg0, "Target", _("Achievement"), 1, $random_id, "Trend");
    //echo "</a>";
}

function department_project_expenses_invoices_and_collections_graph_set_values(&$pg0, $department_id, $from_month_display_order, $to_month_display_order) 
{
    $title = "";

    $invoices_types_in = SCVT_PROJECT_INVOICES_CERTIFIED.",".SCVT_PROJECT_CREDIT_NOTES_CERTIFIED.",".SCVT_PROJECT_INVOICES_PENDING.",".SCVT_PROJECT_CREDIT_NOTES_PENDING;

    $expenses_types_in = SCVT_MATERIAL_ISSUES.",".SCVT_MATERIAL_RETURNS.",".
                            SCVT_PR_ISSUES.",".SCVT_SITE_MATERIAL_RETURNS.",". 
                            SCVT_SUBCONTRACTOR_INVOICES.",".SCVT_SUBCONTRACTOR_CREDIT_NOTES.",". 
                            SCVT_VEHICLE_RUNNING_CHART.",".
                            SCVT_I_OWE_YOU_PAYMENTS.",".SCVT_I_OWE_YOU_ADVANCES.",".
                            SCVT_UTILITY_BILLS.",".SCVT_UTILITY_BILL_CREDIT_NOTES.",".
                            SCVT_MISCELLANEOUS_PROJECT_COSTS;
    
    $collections_types_in = SCVT_PROJECT_INVOICE_RECEIPTS.",".SCVT_PROJECT_CREDIT_NOTE_PAYMENTS;

        $result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
        $i = 0;
        while ($myrow = db_fetch($result)) {

            $name = $myrow["description"];
            if ($pg0 != null) {
                $pg0->x[$i] = $name;
                
                $expenses = get_department_expenses_total($department_id, $myrow['id'], $expenses_types_in);
                $invoices = get_department_receipts_total($department_id, $myrow['id'], $invoices_types_in);                
                $collections = get_department_receipts_total($department_id, $myrow['id'], $collections_types_in);    
                        
                $pg0->y[$i] = $expenses;
                $pg0->z[$i] = $invoices;
                $pg0->z1[$i] = $collections;

                
//                $pg0->y[$i] = $myrow['target'];
//                $pg0->z1[$i] = $pg0->z[$i]; // for trend calculation
                $pg0->colour_type[$i] = set_bar_colour_type($collections);
                
                
//                $pg0->y[$i] = $myrow['achievement'];
//                $pg0->z[$i] = $myrow['target'];
//                $pg0->z1[$i] = $myrow['target'];
//                $pg0->colour_type[$i] = set_bar_colour_type($myrow['achievement']);
            }
            
            
            $i++;
            
//            if($i == 1 && $myrow['achievement'] == null){
//                $title = "No data for ".$myrow['division_name'];
//            }
//            else{
//                $title = $myrow['division_name'];
//            }
        }

        return $title;

}

function get_department_expenses_total($department_id, $month_id, $expenses_types_in)
{
    $sql = "SELECT SUM(value) expenses_total  
            FROM ".TB_PREF."monthly_summary_projects  
            WHERE department_id = ".db_escape($department_id)."
                AND month_id = ".db_escape($month_id)." 
                AND value_type IN ($expenses_types_in)";
    
    $result = db_query($sql);
    $row = db_fetch($result);
    
        if($row['expenses_total'] == null){
            return $row['expenses_total'] = 0;
        }else{
            return $row['expenses_total'];
        }
}

function get_department_receipts_total($department_id, $month_id, $receipts_types_in)
{
    $sql = "SELECT SUM(value) receipts_total  
            FROM ".TB_PREF."monthly_summary_projects  
            WHERE department_id = ".db_escape($department_id)."
                AND month_id = ".db_escape($month_id)." 
                AND value_type IN ($receipts_types_in)";
    
    $result = db_query($sql);
    $row = db_fetch($result);
     
        if($row['receipts_total'] == null){
            return $row['receipts_total'] = 0;
        }else{
            return $row['receipts_total'];
        }
}

?>







































































































































































































