<?php


function project_pl_table_month_range_project_wise_display($department_id, $from_month_display_order, $to_month_display_order)
{
//    display_heading2("Project P&L");
    display_title("Project Cumulative Profit & Loss");
    
    start_table(TABLESTYLE, "width=95%");
    $th = array("Project", "Invoices (IPCs)", "Subcontractor Invoices", "Petty Cash Payments", "Utility Bills", 
                "Transport", "Direct Purchases", "Material Issues", "Tool Usage Costs", "Project Expenses", "H/O Overhead", "Profit/loss");
    $th = array("Project",  
                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("Direct ".PHP_EOL." Purchases"), 
                nl2br("Stock ".PHP_EOL." Material".PHP_EOL." Issues"), 
                nl2br("Sales ".PHP_EOL." Material ".PHP_EOL." Purchases"), 
                nl2br("Month ".PHP_EOL." Expenses"), 
                nl2br("Overhead"), 
                nl2br("Period ".PHP_EOL." Profit/Loss"));


    table_header($th);
    $projects = get_department_projects($department_id);
    
    $k = $dep_project_invoices = $dep_direct_sales_invoices = $dep_sub_invoices_credit_notes = $dep_iou_petty_cash_expenses = 
    $dep_utility_bills_and_credits = $dep_transport = $dep_direct_purchases_and_returns = 
    $dep_material_issues_and_returns = $dep_project_expenses = $dep_overhead = $dep_profit_lost = $dep_tool_usage_costs = 0;
    
    while($project = db_fetch($projects)){
        alt_table_row_color($k);

        label_cell($project['description']);
        
//        $ipc_plus_last_ipa_value = get_project_pl_ipc_plus_last_ipa_value($project['project_id'], $from_month_display_order, $to_month_display_order); 
        $project_invoices = get_project_pl_ipcs($project['project_id'], $from_month_display_order, $to_month_display_order); 
        amount_cell($project_invoices);
        $dep_project_invoices += $project_invoices;
        
        $direct_sales_invoices = get_project_pl_direct_sales_invoices($project['project_id'], $from_month_display_order, $to_month_display_order); 
        amount_cell($direct_sales_invoices);
        $dep_direct_sales_invoices += $direct_sales_invoices;
        
        $project_income = $project_invoices + $direct_sales_invoices;
        amount_cell($project_income, true);
        
        $sub_invoices = get_project_pl_subcontractor_invoices($project['project_id'], $from_month_display_order, $to_month_display_order); 
        $sub_credit_notes = get_project_pl_subcontractor_credit_notes($project['project_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_project_pl_iou_petty_cash_expenses($project['project_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_project_pl_utility_bills($project['project_id'], $from_month_display_order, $to_month_display_order); 
        $utility_credit = get_project_pl_utility_bill_credits($project['project_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_project_pl_transport($project['project_id'], $from_month_display_order, $to_month_display_order); 
        amount_cell($transport);
        $dep_transport += $transport;
        
        $direct_purchases = get_project_pl_direct_material_purchases($project['project_id'], $from_month_display_order, $to_month_display_order); 
        $direct_purchase_returns = get_project_pl_direct_material_purchase_credit_notes($project['project_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;
        
        $material_issue = get_project_pl_material_issues($project['project_id'], $from_month_display_order, $to_month_display_order); 
        $material_returns = get_project_pl_material_returns($project['project_id'], $from_month_display_order, $to_month_display_order); 
        $material_issues_and_returns = $material_issue - $material_returns;
        amount_cell($material_issues_and_returns);
        $dep_material_issues_and_returns += $material_issues_and_returns;
        
        $tool_usage_costs = get_project_pl_tool_usage_charges($project['project_id'], $from_month_display_order, $to_month_display_order); 
        amount_cell($tool_usage_costs);
        $dep_tool_usage_costs += $tool_usage_costs;
        
        $project_expenses = $sub_invoices_credit_notes + $iou_petty_cash_expenses + $utility_bills_and_credits + 
                            $transport + $direct_purchases_and_returns + $material_issues_and_returns + $tool_usage_costs;

        amount_cell($project_expenses, true);
        
        $dep_project_expenses += $project_expenses;
//        $overhead = get_project_pl_overhead($project['project_id'], $month_b['month_begin_date'], $month_e['month_end_date']);
        
        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;
        amount_cell($overhead);
        $dep_overhead += $overhead;
        
        $profit_lost = $project_income - ($project_expenses + $overhead);
        if($profit_lost >= 0){
            amount_cell($profit_lost);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$profit_lost).")").'</font>', "align=right");
        }
        
        $dep_profit_lost += $profit_lost;
        

        end_row();
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($dep_project_invoices, true);
        amount_cell($dep_direct_sales_invoices, true);
        amount_cell($project_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_material_issues_and_returns, true);
        amount_cell($dep_tool_usage_costs, true);
        amount_cell($dep_project_expenses, true);
        amount_cell($dep_overhead, true);
//        amount_cell($dep_profit_lost, true);
        if($dep_profit_lost >= 0){
            amount_cell($profit_lost, true);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$dep_profit_lost).")").'</font>', "align=right");
        }
        end_row();
    end_table();
}

?>