<?php

function all_departments_project_expenses_invoices_and_collections_graph_display(&$pg3, $company_code, $from_month_display_order, $to_month_display_order)
{
    all_departments_project_expenses_invoices_and_collections_graph_set_values($pg3, $company_code, $from_month_display_order, $to_month_display_order);

    $random_id = 'adpecg_'.$company_code."_".$from_month_display_order."_".time();
    
    $pg3->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Project Expenses Invoices and Collections";
    $data['x_axis_name'] = "Departments";
    $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($pg3, $data, 1, $random_id);
//    source_graphic($today, $title, , $pg3, "Target", _("Achievement"), 1, $random_id, "Trend");
    //echo "</a>";
}

function all_departments_project_expenses_invoices_and_collections_graph_set_values(&$pg3, $group_company_code, $from_month_display_order, $to_month_display_order) 
{
    $title = "";
    
    if($group_company_code == "SAW"){
        $company_codes_in = "'SAW', 'SAWC', 'LPS'"; 
    }
    elseif($group_company_code == "VEE"){
        $company_codes_in = "'VEE', 'VEEC'"; 
    }
    
    $receipts_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_group_company_all_list_values($group_company_code, 'DEPARTMENTS');
        
        $data = array();
        while ($myrow = db_fetch($result)) {

            $name = $myrow["value_name"];
            
//            $expenses = get_summary_projects_departmets_total($month_id, db_escape($myrow['value_code']), $expenses_types_in);
            $expenses = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $myrow['id'], $expenses_types_in);
                    
//            $receipts = get_summary_projects_departmets_total($month_id, db_escape($myrow['value_code']), $receipts_types_in);  
            $receipts = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $myrow['id'], $receipts_types_in);
            
            $collections = get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $myrow['id'], $collections_types_in);
            

//            $expenses = rand(0,10);
//            $collections = rand(0,100000);
            
            $line = array("name" =>$name, "expenses" => $expenses, "receipts" => $receipts, "collections" => $collections);
            $data[] = $line;
            
        }

        array_multisort(array_column($data, "expenses"), SORT_DESC, $data);
        
        $i = 0;
        foreach ($data as $value) {
            if ($pg3 != null) {
                $pg3->x[$i] = $value['name'];
                
//                $expenses = get_summary_projects_departmets_total($month_id, db_escape($myrow['value_code']), $expenses_types_in);
//                $receipts = get_summary_projects_departmets_total($month_id, db_escape($myrow['value_code']), $receipts_types_in);                

                $pg3->y[$i] = $value['expenses'];
                $pg3->z[$i] = $value['receipts'];
                $pg3->z1[$i] = $value['collections'];
                
                $pg3->colour_type[$i] = set_bar_colour_type($value['expenses']);
                
            }
            
            
            $i++;
            
        }

        
        return $title;

}

