<?php


function company_profit_loss_table_month_wise_display($group_company_code, $from_month_display_order, $to_month_display_order)
{

    display_title("Company 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("Inventory ".PHP_EOL." Purchases"), 
                nl2br("Sales Material ".PHP_EOL." Purchases"), 
                nl2br("Salary ".PHP_EOL." Expenses"), 
                nl2br("Loan ".PHP_EOL." Interests"), 
                nl2br("Month ".PHP_EOL." Expenses"), 
                nl2br("Month ".PHP_EOL." Profit/Loss"), 
                nl2br("Cumulative ".PHP_EOL." Profit/Loss"));

    table_header($th);
    
    if($group_company_code == "SAW"){
        $company_codes_in = "'SAW', 'SAWC', 'LPS'"; 
    }
    elseif($group_company_code == "VEE"){
        $company_codes_in = "'VEE', 'VEEC'"; 
    }
    
    $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_salary_cost = 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_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;
        amount_cell($project_invoices_credit_notes);
        $total_project_invoices_credit_notes += $project_invoices_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;
        amount_cell($sales_invoices_credit_notes);
        $total_sales_invoices_credit_notes += $sales_invoices_credit_notes;
        
        amount_cell($project_invoices_credit_notes + $sales_invoices_credit_notes, true);
        
        //Expenses
        $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;
        amount_cell($sub_invoices_credit_notes);
        $total_sub_invoices_credit_notes += $sub_invoices_credit_notes;
        
        $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;
        amount_cell($iou_petty_cash_expenses);
        $total_iou_petty_cash_expenses += $iou_petty_cash_expenses;
        
        $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;
        amount_cell($utility_bills_and_credits);
        $total_utility_bills_and_credits += $utility_bills_and_credits;
        
        $transport = get_company_pl_transport($from_month_display_order, $to_month_display_order); 
        amount_cell($transport);
        $total_transport += $transport;
        
        $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;
        amount_cell($direct_purchases_and_returns);
        $total_direct_purchases_and_returns += $direct_purchases_and_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;
        amount_cell($material_issues_and_returns);
        $total_material_issues_and_returns += $material_issues_and_returns;
        
        $sales_despatches = get_company_pl_sales_material_purchases($from_month_display_order, $to_month_display_order); 
        $sales_despatch_returns = get_company_pl_sales_material_purchase_returns($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;

        $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);
        amount_cell($salary_cost);
        $total_salary_cost += $salary_cost;
        
        $month_loan_interests = 0;
        $loan_payments = get_month_loan_repayments_summary($month_display_order, $month_display_order);
        if(isset($loan_payments['capital_paid'])){
//            $capital_paid = $loan_payments['capital_paid'];
            $interest_paid = $loan_payments['interest_paid'];
            $late_interest_paid = $loan_payments['late_interest_paid'];
            $month_loan_interests = $interest_paid + $late_interest_paid;
        }
        amount_cell($month_loan_interests);
        $total_loan_interests += $month_loan_interests;
        
        $month_expenses = ($sub_invoices_credit_notes 
                            + $iou_petty_cash_expenses 
                            + $utility_bills_and_credits 
                            + $transport 
                            + $direct_purchases_and_returns  
                            + $material_issues_and_returns 
                            + $salary_cost 
                            + $month_loan_interests );
        amount_cell($month_expenses, true);
        
        $month_frofit_loss = $project_invoices_credit_notes + $sales_credit_notes - $month_expenses;
        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);
        }
        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);
        amount_cell($total_salary_cost, true);
        amount_cell($total_loan_interests, 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
                            + $total_salary_cost
                            + $total_loan_interests;
        amount_cell($total_expenses, true); 
        label_cell('');

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

