<?php

function company_company_profit_loss_graph_display(&$pg4, $group_company_code, $from_month_display_order, $to_month_display_order)
{
    company_company_profit_loss_graph_set_values($pg4, $group_company_code, $from_month_display_order, $to_month_display_order);

    $random_id = "ccplg_SAW_".$from_month_display_order."_".time();
    
    $pg4->axis_value_names_angle = -30;
    
    $data = array();
    $data['title'] = "Company 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($pg4, $data, 1, $random_id);//line graph = 4
}

function company_company_profit_loss_graph_set_values(&$pg4, $group_company_code, $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 ($pg4 != null) {
                $pg4->x[$i] = $name;
                
                $invoices = get_company_invoices_month_total($month['display_order']);
                $expenses = get_company_expenses_month_total($group_company_code, $month['display_order']);                
//                $overhead = get_company_overhead_month_total($expenses);            
                $profit =  $invoices - ($expenses);         
                $pg4->y[$i] = $expenses;
                $pg4->z[$i] = $invoices;
                $pg4->z1[$i] = $profit;
                
                $pg4->colour_type[$i] = set_bar_colour_type($invoices);               
            }
            $i++;
        }
        return $title;
}

function get_company_invoices_month_total($month_display_order){
    
    $from_month_display_order = $to_month_display_order = $month_display_order;
    
    $project_invoices = get_company_pl_ipcs($from_month_display_order, $to_month_display_order); 
    $project_credit_notes = get_company_pl_ipc_credit_notes($from_month_display_order, $to_month_display_order); 
    $project_invoices_credit_notes = $project_invoices + $project_credit_notes;

    $sales_invoices = get_company_pl_sales_invoices($from_month_display_order, $to_month_display_order); 
    $sales_credit_notes = get_company_pl_sales_credit_notes($from_month_display_order, $to_month_display_order); 
    $sales_invoices_credit_notes = $sales_invoices + $sales_credit_notes;
        
    return $project_invoices_credit_notes + $sales_invoices_credit_notes;
}


function get_company_expenses_month_total($group_company_code, $month_display_order){
    if($group_company_code == "SAW"){
        $company_codes_in = "'SAW', 'SAWC', 'LPS'"; 
    }
    elseif($group_company_code == "VEE"){
        $company_codes_in = "'VEE', 'VEEC'"; 
    }
    
    $from_month_display_order = $to_month_display_order = $month_display_order;
    
    $sub_invoices = get_company_pl_subcontractor_invoices($from_month_display_order, $to_month_display_order); 
    $sub_credit_notes = get_company_pl_subcontractor_credit_notes($from_month_display_order, $to_month_display_order); 
    $sub_invoices_credit_notes = $sub_invoices + $sub_credit_notes;

//    $iou_petty_cash_expenses = get_company_pl_iou_petty_cash_expenses($from_month_display_order, $to_month_display_order);
    $iou_petty_cash_payments = get_company_cf_iou_payments($month_display_order, $month_display_order);
    $iou_advance_payments = get_company_cf_iou_advance_payments($month_display_order, $month_display_order);
    $iou_petty_cash_expenses = $iou_petty_cash_payments + $iou_advance_payments;

    $utility_bill = get_company_pl_utility_bills($from_month_display_order, $to_month_display_order); 
    $utility_credit = get_company_pl_utility_bill_credits($from_month_display_order, $to_month_display_order); 
    $utility_bills_and_credits = $utility_bill + $utility_credit;

    $transport = get_company_pl_transport($from_month_display_order, $to_month_display_order); 

    $direct_purchases = get_company_pl_direct_purchases($from_month_display_order, $to_month_display_order); 
    $direct_purchase_returns = get_company_pl_direct_purchase_returns($from_month_display_order, $to_month_display_order); 
    $direct_purchases_and_returns = $direct_purchases - $direct_purchase_returns;

    $material_issue = get_company_pl_stock_material_purchases($from_month_display_order, $to_month_display_order); 
    $material_returns = get_company_pl_stock_material_purchase_returns($from_month_display_order, $to_month_display_order); 
    $material_issues_and_returns = $material_issue - $material_returns;
    
    $sales_despatches = get_company_pl_sales_material_purchases($from_month_display_order, $to_month_display_order); 
    $sales_credit_notes = get_company_pl_sales_material_purchase_returns($from_month_display_order, $to_month_display_order); 
    $sales_despatches_and_credit_notes = $sales_despatches - $sales_credit_notes;

    $salary_components_in = SCVT_TOTAL_SALARY_COST;
    $salary_cost = get_summary_payrolls_month_range_company_total($company_codes_in, $salary_components_in, $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 + $material_issues_and_returns +
                        $sales_despatches_and_credit_notes + $salary_cost;
    
    return $project_expenses;
}

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

function get_company_pl_ipcs($from_month_display_order, $to_month_display_order){

    $receipts_types_in = SCVT_PROJECT_INVOICES_CERTIFIED;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_ipc_credit_notes($from_month_display_order, $to_month_display_order){

    $receipts_types_in = SCVT_PROJECT_CREDIT_NOTES_CERTIFIED;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_sales_invoices($from_month_display_order, $to_month_display_order){

    $receipts_types_in = SCVT_SALES_INVOICES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_sales_credit_notes($from_month_display_order, $to_month_display_order){

    $receipts_types_in = SCVT_SALES_CREDIT_NOTES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_sales_despatches($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_SALES_DESPATCHES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_sales_despatch_returns($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_SALES_DESPATCH_RETURNS;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_subcontractor_invoices($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_SUBCONTRACTOR_INVOICES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_subcontractor_credit_notes($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_SUBCONTRACTOR_CREDIT_NOTES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_iou_petty_cash_expenses($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_I_OWE_YOU_PAYMENTS.",".SCVT_I_OWE_YOU_ADVANCES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_utility_bills($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_UTILITY_BILLS;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_utility_bill_credits($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_UTILITY_BILL_CREDIT_NOTES;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_transport($from_month_display_order, $to_month_display_order){
    
    $receipts_types_in = SCVT_VEHICLE_RUNNING_CHART;
    
    return get_summary_projects_month_range_company_value($from_month_display_order, $to_month_display_order, $receipts_types_in);
}

function get_company_pl_stock_material_purchases($from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_SUPPLIER_INVOICES;
    $project_types_in = PROJ_TYPE_STOCK;
    
    return get_summary_project_types_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in, $project_types_in);
}

function get_company_pl_stock_material_purchase_returns($from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_SUPPLIER_CREDIT_NOTES;
    $project_types_in = PROJ_TYPE_STOCK;
    
    return get_summary_project_types_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in, $project_types_in);
}

function get_company_pl_direct_purchases($from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_SUPPLIER_INVOICES;
    $project_types_in = PROJ_TYPE_CONSUMPTION.",".PROJ_TYPE_DEPT_EXPENSES;
    
    return get_summary_project_types_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in, $project_types_in);
}


function get_company_pl_direct_purchase_returns($from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_SUPPLIER_CREDIT_NOTES;
    $project_types_in = PROJ_TYPE_CONSUMPTION.",".PROJ_TYPE_DEPT_EXPENSES;
    
    return get_summary_project_types_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in, $project_types_in);
}

function get_company_pl_sales_material_purchases($from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_SUPPLIER_INVOICES;
    $project_types_in = PROJ_TYPE_SALES_COST;
    
    return get_summary_project_types_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in, $project_types_in);
}

function get_company_pl_sales_material_purchase_returns($from_month_display_order, $to_month_display_order){
    
    $value_types_in = SCVT_SUPPLIER_CREDIT_NOTES;
    $project_types_in = PROJ_TYPE_SALES_COST;
    
    return get_summary_project_types_month_range_company_value($from_month_display_order, $to_month_display_order, $value_types_in, $project_types_in);
}

?>