<?php


function department_pl_table_month_range_department_wise_display($group_company_code, $from_month_display_order, $to_month_display_order)
{
    $title = "Profit & Loss - Division Wise";
    display_title($title);
    
    start_table(TABLESTYLE, "width=85%");
    $th = array("Department",  
                nl2br("Project ".PHP_EOL." Invoices"), 
                nl2br("Sales ".PHP_EOL." Invoices"), 
                nl2br("Period ".PHP_EOL." Income"), 
                nl2br("Subcontractor ".PHP_EOL." Invoices"), 
                nl2br("IOU and ".PHP_EOL." Petty Cash ".PHP_EOL." Expenses"), 
                nl2br("Utility Bills"), 
                nl2br("Transport ".PHP_EOL." Expenses"), 
                nl2br("Site Material ".PHP_EOL." Purchases"), 
                nl2br("Inventory ".PHP_EOL." Material".PHP_EOL." Purchases"), 
                nl2br("Sales ".PHP_EOL." Material ".PHP_EOL." Purchases"), 
                nl2br("Period ".PHP_EOL." Expenses"), 
                nl2br("Overhead"), 
                nl2br("Period ".PHP_EOL." Profit/Loss"), 
                nl2br("Cumulative ".PHP_EOL." Profit/Loss"));

    table_header($th);
    $result = get_group_company_all_list_values($group_company_code, 'DEPARTMENTS', "'Y'");
    
    $k = $dep_project_invoices_credit_notes = $dep_sales_invoices_credit_notes = $dep_income = 
    $dep_sub_invoices_credit_notes = $dep_iou_petty_cash_expenses = $dep_utility_bills_and_credits = 
    $dep_transport = $dep_direct_purchases_and_returns = $dep_stock_material_purchases_and_returns = $dep_sales_material_purchases_and_returns = 
    $dep_expenses = $dep_overhead = $cumulative_profit_lost = 0;
    
    while($department = db_fetch($result)){
        alt_table_row_color($k);

        label_cell($department['value_name']);
        
        //Income
        $project_invoices = get_department_pl_ipcs($department['id'], $from_month_display_order, $to_month_display_order);         
        $project_credit_notes = get_department_pl_ipc_credit_notes($department['id'], $from_month_display_order, $to_month_display_order); 
        $project_invoices_credit_notes = $project_invoices + $project_credit_notes;
        amount_cell($project_invoices_credit_notes);
        $dep_project_invoices_credit_notes += $project_invoices_credit_notes;
        
        $sales_invoices = get_department_pl_sales_invoices($department['id'], $from_month_display_order, $to_month_display_order); 
        $sales_credit_notes = get_department_pl_sales_credit_notes($department['id'], $from_month_display_order, $to_month_display_order); 
        $sales_invoices_credit_notes = $sales_invoices + $sales_credit_notes;
        amount_cell($sales_invoices_credit_notes);
        $dep_sales_invoices_credit_notes += $sales_invoices_credit_notes;
        
        $income = $project_invoices_credit_notes + $sales_invoices_credit_notes;
        amount_cell($income, true);
        $dep_income += $income;
        
        
        $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;
        amount_cell($sub_invoices_credit_notes);
        $dep_sub_invoices_credit_notes += $sub_invoices_credit_notes;
        
        $iou_petty_cash_expenses = get_department_pl_iou_petty_cash_expenses($department['id'], $from_month_display_order, $to_month_display_order); 
        amount_cell($iou_petty_cash_expenses);
        $dep_iou_petty_cash_expenses += $iou_petty_cash_expenses;
        
        $utility_bill = get_department_pl_utility_bills($department['id'], $from_month_display_order, $to_month_display_order); 
        $utility_credit = get_department_pl_utility_bill_credit_notes($department['id'], $from_month_display_order, $to_month_display_order); 
        $utility_bills_and_credits = $utility_bill - $utility_credit;
        amount_cell($utility_bills_and_credits);
        $dep_utility_bills_and_credits += $utility_bills_and_credits;
        
        $transport = get_department_pl_transport($department['id'], $from_month_display_order, $to_month_display_order); 
        amount_cell($transport);
        $dep_transport += $transport;
        
        $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;
        amount_cell($direct_purchases_and_returns);
        $dep_direct_purchases_and_returns += $direct_purchases_and_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;
        amount_cell($stock_material_purchases_and_returns);
        $dep_stock_material_purchases_and_returns += $stock_material_purchases_and_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;
        amount_cell($sales_material_purchases_and_returns);
        $dep_sales_material_purchases_and_returns += $sales_material_purchases_and_returns;
        
        $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;
        
        amount_cell($expenses, true);
        $dep_expenses += $expenses;
        
        $oh_factor = 0.2;
        
        $overhead = $expenses * $oh_factor;
        amount_cell($overhead);
        $dep_overhead += $overhead;
        
        $profit_lost = $income - ($expenses + $overhead);
        if($profit_lost >= 0){
            amount_cell($profit_lost);
        }
        else{
            label_cell('<font color="red">'."(".price_format(-$profit_lost).")".'</font>', "align=right");
        }
        
        $cumulative_profit_lost += $profit_lost;
        
        
        if($cumulative_profit_lost >= 0){
            amount_cell($cumulative_profit_lost);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$cumulative_profit_lost).")").'</font>', "align=right");
        }

        end_row();
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($dep_project_invoices_credit_notes, true);
        amount_cell($dep_sales_invoices_credit_notes, true);
        amount_cell($dep_income, true);
        amount_cell($dep_sub_invoices_credit_notes, true);
        amount_cell($dep_iou_petty_cash_expenses, true);
        amount_cell($dep_utility_bills_and_credits, true);
        amount_cell($dep_transport, true);
        amount_cell($dep_direct_purchases_and_returns, true);
        amount_cell($dep_stock_material_purchases_and_returns, true);
        amount_cell($dep_sales_material_purchases_and_returns, true);
        amount_cell($dep_expenses, true);
        amount_cell($dep_overhead, true);
        label_cell("");
        if($cumulative_profit_lost >= 0){
            amount_cell($cumulative_profit_lost);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$cumulative_profit_lost).")").'</font>', "align=right");
        }
        end_row();
    end_table();
}


function get_department_pl_ipcs($department_id, $from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_PROJECT_INVOICES_CERTIFIED;    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in);
}

function get_department_pl_ipc_credit_notes($department_id, $from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_PROJECT_CREDIT_NOTES_CERTIFIED;    
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in);
}

function get_department_pl_sales_invoices($department_id, $from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_SALES_INVOICES;    
    $row = get_summary_sales_month_range_department_total($department_id, $value_types_in, $from_month_display_order, $to_month_display_order);
    $total = $row['order_total'] - $row['discounts_total'] + $row['taxes_total'];
    return $total;
}

function get_department_pl_sales_credit_notes($department_id, $from_month_display_order, $to_month_display_order){
    $value_types_in = SCVT_SALES_CREDIT_NOTES;    
    $row = get_summary_sales_month_range_department_total($department_id, $value_types_in, $from_month_display_order, $to_month_display_order);
    $total = $row['order_total'] - $row['discounts_total'] + $row['taxes_total'];
    return $total;
}

function get_department_pl_subcontractor_invoices($department_id, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_SUBCONTRACTOR_INVOICES;    
    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_subcontractor_credit_notes($department_id, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_SUBCONTRACTOR_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_iou_petty_cash_expenses($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $receipts_types_in);
}

function get_department_pl_utility_bills($department_id, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_UTILITY_BILLS;    
    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_utility_bill_credit_notes($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_transport($department_id, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_VEHICLE_RUNNING_CHART;    
    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_stock_material_purchases($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in, $project_types_in);
}

function get_department_pl_stock_material_purchase_returns($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in, $project_types_in);
}

function get_department_pl_sales_material_purchases($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in, $project_types_in);
}

function get_department_pl_sales_material_purchase_returns($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in, $project_types_in);
}

function get_department_pl_overhead($department_id, $from_month_display_order, $to_month_display_order)
{
    $receipts_types_in = SCVT_MONTHLY_PROJECT_COSTS;
    
    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_direct_purchases($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in, $project_types_in);
}


function get_department_pl_direct_purchase_returns($department_id, $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_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in, $project_types_in);
}

function get_department_pl_stock_material_issues($department_id, $from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_MATERIAL_ISSUES;  
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in);
}

function get_department_pl_stock_material_returns($department_id, $from_month_display_order, $to_month_display_order)
{
    $value_types_in = SCVT_MATERIAL_RETURNS;
    return get_summary_projects_month_range_department_value($from_month_display_order, $to_month_display_order, $department_id, $value_types_in);
}
?>