<?php

function department_department_profit_loss_graph_display(&$pg1, $department_id, $from_month_display_order, $to_month_display_order)
{
    department_department_profit_loss_graph_set_values($pg1, $department_id, $from_month_display_order, $to_month_display_order);

    $random_id = 'ddplg_'.$department_id."_".$from_month_display_order."_".time();
    
    $pg1->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Department 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";

    display_graph($pg1, $data, 1, $random_id);//line graph = 4
}

function department_department_profit_loss_graph_set_values(&$pg1, $department_id, $from_month_display_order, $to_month_display_order) 
{
    $title = "";
    
        $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 ($pg1 != null) {
                $pg1->x[$i] = $name;
                
                $invoices = get_department_invoices_month_total($department_id, $month['display_order']);
                $expenses = get_department_expenses_month_total($department_id, $month['display_order']);                
                $overhead = get_department_overhead_month_total($expenses);            
                $profit =  $invoices - ($expenses + $overhead);         
                $pg1->y[$i] = $expenses;
                $pg1->z[$i] = $invoices;
                $pg1->z1[$i] = $profit;
                
                $pg1->colour_type[$i] = set_bar_colour_type($invoices);               
            }
            $i++;
        }
        return $title;
}

function get_department_invoices_month_total($department_id, $month_display_order){
    
    $project_invoices = get_department_pl_ipcs($department_id, $month_display_order, $month_display_order);         
    $project_credit_notes = get_department_pl_ipc_credit_notes($department_id, $month_display_order, $month_display_order); 
    $project_invoices_credit_notes = $project_invoices + $project_credit_notes;


    $sales_invoices = get_department_pl_sales_invoices($department_id, $month_display_order, $month_display_order); 
    $sales_credit_notes = get_department_pl_sales_credit_notes($department_id, $month_display_order, $month_display_order); 
    $sales_invoices_credit_notes = $sales_invoices + $sales_credit_notes;

    $income = $project_invoices_credit_notes + $sales_invoices_credit_notes;

    return $income;
}


function get_department_expenses_month_total($department_id, $month_display_order){
    
    $from_month_display_order = $to_month_display_order = $month_display_order;
    
    $sub_invoices = get_department_pl_subcontractor_invoices($department_id, $from_month_display_order, $to_month_display_order); 
    $sub_credit_notes = get_department_pl_subcontractor_credit_notes($department_id, $from_month_display_order, $to_month_display_order); 
    $sub_invoices_credit_notes = $sub_invoices + $sub_credit_notes;

    $iou_petty_cash_expenses = get_department_pl_iou_petty_cash_expenses($department_id, $from_month_display_order, $to_month_display_order); 

    $utility_bill = get_department_pl_utility_bills($department_id, $from_month_display_order, $to_month_display_order); 
    $utility_credit = get_department_pl_utility_bill_credits($department_id, $from_month_display_order, $to_month_display_order); 
    $utility_bills_and_credits = $utility_bill + $utility_credit;

    $transport = get_department_pl_transport($department_id, $from_month_display_order, $to_month_display_order); 

    $direct_purchases = get_department_pl_direct_purchases($department_id, $from_month_display_order, $to_month_display_order); 
    $direct_purchase_returns = get_department_pl_direct_purchase_returns($department_id, $from_month_display_order, $to_month_display_order); 
    $direct_purchases_and_returns = $direct_purchases - $direct_purchase_returns;

    $stock_material_purchases = get_department_pl_stock_material_purchases($department_id, $from_month_display_order, $to_month_display_order); 
    $stock_material_purchase_returns = get_department_pl_stock_material_purchase_returns($department_id, $from_month_display_order, $to_month_display_order); 
    $stock_material_purchases_and_returns = $stock_material_purchases - $stock_material_purchase_returns;

    $sales_material_purchases = get_department_pl_sales_material_purchases($department_id, $from_month_display_order, $to_month_display_order); 
    $sales_material_purchase_returns = get_department_pl_sales_material_purchase_returns($department_id, $from_month_display_order, $to_month_display_order); 
    $sales_material_purchases_and_returns = $sales_material_purchases - $sales_material_purchase_returns;

    
    $tool_usage_costs = get_department_pl_tool_usage_costs($department_id, $from_month_display_order, $to_month_display_order); 
        
    $project_expenses = $sub_invoices_credit_notes + $iou_petty_cash_expenses + $utility_bills_and_credits + 
                        $transport + $direct_purchases_and_returns + $stock_material_purchases_and_returns + 
                        $sales_material_purchases_and_returns + $tool_usage_costs;
    
    return $project_expenses;
}


function get_department_overhead_month_total($department_expenses){
    
    $overhead = $department_expenses * 0.2;
    
    return $overhead;
}




function get_department_pl_utility_bill_credits($department_id, $from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_UTILITY_BILL_CREDIT_NOTES;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}



function get_department_pl_pr_issues($department_id, $from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_PR_ISSUES;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}


function get_department_pl_site_material_returns($department_id, $from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_SITE_MATERIAL_RETURNS;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}

function get_department_pl_tool_usage_costs($department_id, $from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_TOOL_USAGE_CHARGES;
    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}

?>