<?php

function project_project_profit_loss_graph_display(&$pg0, $project_id, $from_month_display_order, $to_month_display_order)
{
    project_project_profit_loss_graph_set_values($pg0, $project_id, $from_month_display_order, $to_month_display_order);

    $random_id = 'ppplg_'.$project_id."_".$from_month_display_order."_".time();
    
    $pg0->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Project Month Profit & Loss";
    $data['x_axis_name'] = "Month";
    $data['y_axis_name'] = "Value";
    $data['graph1_name'] = "Expenses";
    $data['graph2_name'] = "Invoices";
    $data['graph3_name'] = "Profit";
    
    
    //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 project_project_profit_loss_graph_set_values(&$pg0, $project_id, $from_month_display_order, $to_month_display_order) 
{
    $title = "";
    
    $project = get_project_record($project_id);
    
        $result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
        $i = 0;
        while ($month = db_fetch($result)) {

            $name = $month["description"];
            if ($pg0 != null) {
                $pg0->x[$i] = $name;
                
                $invoices = get_project_income_month_total($project_id, $month['display_order']);
                $expenses = get_project_expenses_month_total($project_id, $month['display_order']);                
                $overhead = get_project_overhead_month_total($project, $expenses);            
                $profit =  $invoices - ($expenses + $overhead);         
//                $profit = -2500000;
                $pg0->y[$i] = $expenses;
                $pg0->z[$i] = $invoices;
                $pg0->z1[$i] = $profit;
                
                $pg0->colour_type[$i] = set_bar_colour_type($invoices);
                
            }
            
            
            $i++;
            
//            if($i == 1 && $myrow['achievement'] == null){
//                $title = "No data for ".$myrow['division_name'];
//            }
//            else{
//                $title = $myrow['division_name'];
//            }
        }

        return $title;

}

function get_project_income_month_total($project_id, $month_display_order)
{
    $ipc_plus_last_ipa_value = get_project_pl_ipcs($project_id, $month_display_order, $month_display_order); 
    
    $direct_sales_invoices = get_project_pl_direct_sales_invoices($project_id, $month_display_order, $month_display_order);
        
    return $ipc_plus_last_ipa_value + $direct_sales_invoices;
}


function get_project_expenses_month_total($project_id, $month_display_order)
{
    $from_month_display_order = $to_month_display_order = $month_display_order;
    
    $sub_invoices = get_project_pl_subcontractor_invoices($project_id, $from_month_display_order, $to_month_display_order); 
    $sub_credit_notes = get_project_pl_subcontractor_credit_notes($project_id, $from_month_display_order, $to_month_display_order); 
    $sub_invoices_credit_notes = $sub_invoices + $sub_credit_notes;

    $iou_petty_cash_expenses = get_project_pl_iou_petty_cash_expenses($project_id, $from_month_display_order, $to_month_display_order); 

    $utility_bill = get_project_pl_utility_bills($project_id, $from_month_display_order, $to_month_display_order); 
    $utility_credit = get_project_pl_utility_bill_credits($project_id, $from_month_display_order, $to_month_display_order); 
    $utility_bills_and_credits = $utility_bill + $utility_credit;

    $transport = get_project_pl_transport($project_id, $from_month_display_order, $to_month_display_order); 

    $direct_purchases = get_project_pl_direct_material_purchases($project_id, $from_month_display_order, $to_month_display_order); 
    $direct_purchase_returns = get_project_pl_direct_material_purchase_credit_notes($project_id, $from_month_display_order, $to_month_display_order); 
    $direct_purchases_and_returns = $direct_purchases - $direct_purchase_returns;

    $material_issue = get_project_pl_material_issues($project_id, $from_month_display_order, $to_month_display_order); 
    $material_returns = get_project_pl_material_returns($project_id, $from_month_display_order, $to_month_display_order); 
    $material_issues_and_returns = $material_issue - $material_returns;
        
    $direct_sales_despatches = get_project_pl_direct_sales_despatches($project_id, $month_display_order, $month_display_order); 
    $direct_sales_despatch_returns = get_project_pl_direct_sales_despatch_returns($project_id, $month_display_order, $month_display_order); 
    $direct_sales_despatches_and_returns = $direct_sales_despatches - $direct_sales_despatch_returns;
        
//    $tool_usage_charges = get_project_pl_tool_usage_charges($project_id, $from_month_display_order, $to_month_display_order);
    $tool_usage_charges = 0;
    
    $project_expenses = $sub_invoices_credit_notes + $iou_petty_cash_expenses + $utility_bills_and_credits + 
                        $transport + $direct_purchases_and_returns + $material_issues_and_returns + 
                        $direct_sales_despatches_and_returns + $tool_usage_charges;
    
    return $project_expenses;

        
}

function get_project_overhead_month_total($project, $project_expenses)
{
    if($project['approved_cost_budgets_total']>0){
            $oh_factor = $project['approved_overhead_budgets_total'] / $project['approved_cost_budgets_total'];
    }
    else{
        $oh_factor = 0;
    }
    $overhead = $project_expenses * $oh_factor;
    
    return $overhead;
}

?>



















































































































































































































