<?php


function department_profit_loss_table_month_wise_display($department_id, $from_month_display_order, $to_month_display_order)
{

    display_title("Department Profit & Loss");
    
    start_table(TABLESTYLE, "width=95%");
    $th = array("Month",  
                nl2br("Project ".PHP_EOL." Invoices"), 
                nl2br("Sales ".PHP_EOL." Invoices"), 
                nl2br("Month ".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("Stock ".PHP_EOL." Material".PHP_EOL." Issues"), 
                nl2br("Sales ".PHP_EOL." Material ".PHP_EOL." Purchases"), 
                nl2br("Month ".PHP_EOL." Expenses"), 
                nl2br("Overhead"), 
                nl2br("Month ".PHP_EOL." Profit/Loss"), 
                nl2br("Cumulative ".PHP_EOL." Profit/Loss"));

    table_header($th);
    

    $months_result = get_months_range_by_display_order($from_month_display_order, $to_month_display_order);
    
    $k = $total_project_invoices_credit_notes = $total_sales_invoices_credit_notes = 
            $total_sub_invoices_credit_notes = $total_iou_petty_cash_expenses = $total_transport = 
            $total_utility_bills_and_credits = $total_direct_purchases_and_returns = $total_material_issues_and_returns = 
            $total_loan_interests = $total_sales_despatches_and_credit_notes = $total_frofit_loss = 
            $total_overhead = 0;
    
//    $project = get_project_record($project_id);
    
    while($month = db_fetch($months_result)){
        alt_table_row_color($k);

        label_cell($month['description']);
        $month_display_order = $month['display_order'];
        
        
        //Income
        $from_month_display_order = $to_month_display_order = $month_display_order;
        
        $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);
        $total_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);
        $total_sales_invoices_credit_notes += $sales_invoices_credit_notes;
        
        $month_income = $project_invoices_credit_notes + $sales_invoices_credit_notes;
        amount_cell($month_income, true);
        
        //Expenses
        $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);
        $total_sub_invoices_credit_notes += $sub_invoices_credit_notes;
        
        $iou_petty_cash_payments = get_department_cf_iou_payments($department_id, $month_display_order, $month_display_order);
        $iou_advance_payments = get_department_cf_iou_advance_payments($department_id, $month_display_order, $month_display_order);
        $iou_petty_cash_expenses = $iou_petty_cash_payments + $iou_advance_payments;
        amount_cell($iou_petty_cash_expenses);
        $total_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_credits($department_id, $from_month_display_order, $to_month_display_order); 
        $utility_bills_and_credits = $utility_bill + $utility_credit;
        amount_cell($utility_bills_and_credits);
        $total_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);
        $total_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);
        $total_direct_purchases_and_returns += $direct_purchases_and_returns;
        
        $material_issue = get_department_pl_stock_material_issues($department_id, $from_month_display_order, $to_month_display_order); 
        $material_returns = get_department_pl_stock_material_returns($department_id, $from_month_display_order, $to_month_display_order); 
        $material_issues_and_returns = $material_issue - $material_returns;
        amount_cell($material_issues_and_returns);
        $total_material_issues_and_returns += $material_issues_and_returns;
        
        $sales_despatches = get_department_pl_sales_material_purchases($department_id, $from_month_display_order, $to_month_display_order); 
        $sales_despatch_returns = get_department_pl_sales_material_purchase_returns($department_id, $from_month_display_order, $to_month_display_order); 
        $sales_despatches_and_despatch_returns = $sales_despatches - $sales_despatch_returns;
        amount_cell($sales_despatches_and_despatch_returns);
        $total_sales_despatches_and_credit_notes += $sales_despatches_and_despatch_returns;

        $month_expenses = $sub_invoices_credit_notes 
                            + $iou_petty_cash_expenses 
                            + $utility_bills_and_credits 
                            + $transport 
                            + $direct_purchases_and_returns  
                            + $material_issues_and_returns;
        
        amount_cell($month_expenses, true);
        
        $overhead = $month_expenses *0.2;
        $total_overhead += $overhead;
        
        amount_cell($overhead);        
        
        $month_frofit_loss = $month_income - ($month_expenses + $overhead);
        if($month_frofit_loss >= 0){
            amount_cell($month_frofit_loss);
        }
        else{
            label_cell('<font color="red">'."(".price_format(-$month_frofit_loss).")".'</font>', "align=right");
        }
        $total_frofit_loss += $month_frofit_loss;
        
        if($total_frofit_loss >= 0){
            amount_cell($total_frofit_loss, true);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$total_frofit_loss).")").'</font>', "align=right");
        }
        end_row();
        
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($total_project_invoices_credit_notes, true);
        amount_cell($total_sales_invoices_credit_notes, true);
        $total_income = $total_project_invoices_credit_notes + $total_sales_invoices_credit_notes;
        amount_cell($total_income, true);
        
        amount_cell($total_sub_invoices_credit_notes, true);
        amount_cell($total_iou_petty_cash_expenses, true);
        amount_cell($total_utility_bills_and_credits, true);
        amount_cell($total_transport, true);
        amount_cell($total_direct_purchases_and_returns, true);
        amount_cell($total_material_issues_and_returns, true);
        amount_cell($total_sales_despatches_and_credit_notes, true);

        $total_expenses = $total_sub_invoices_credit_notes
                            + $total_iou_petty_cash_expenses
                            + $total_utility_bills_and_credits
                            + $total_transport
                            + $total_direct_purchases_and_returns
                            + $total_material_issues_and_returns
                            + $total_sales_despatches_and_credit_notes;
        amount_cell($total_expenses, true); 
        amount_cell($total_overhead, true); 
        label_cell('');

        if($total_frofit_loss >= 0){
            amount_cell($total_frofit_loss, true);
        }
        else{
            label_cell('<font color="red">'.text_bold("(".price_format(-$total_frofit_loss).")").'</font>', "align=right");
        }
    
        
        end_row();
    end_table();
}

