<?php
/**********************************************************************
***********************************************************************/

function dashboard_project_profit_and_loss($from_month_display_order, $to_month_display_order, $project_id) {
    
//    display_heading2('(Based on Goods Received Notes and Tax Free Item Costs)');  
    
    $project = get_project($project_id);
    $overhead_percentage = $project['overhead_percentage'];
    
    br();
        
    $expense_month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($expense_month_ranges_array);
    
    start_table(TABLESTYLE, "width=$table_width%");
        
        $th = array("Expense Type");
            foreach ($expense_month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }
            $th[] = "Total";

            table_header($th);
            
//            $from_month = get_month_by_display_order($from_month_display_order);
//            $to_month = get_month_by_display_order($to_month_display_order);
//            
//            $from_date = $from_month['month_begin_date'];
//            $to_date = $to_month['month_end_date'];
            
            start_row();
            label_cell(text_bold("Expenses"), "colspan=14");
            end_row();
            
            $project_total_commitment = dashboard_pl_project_expenses($project_id, $expense_month_ranges_array);
            start_row();
            label_cell(text_bold("Period Expenses Total"), 'align=right');
                
            foreach ($expense_month_ranges_array as &$month) {
                $month_totals = $month['month_totals'];
                $total = 0;
                foreach ($month_totals as $month_type_total) {
                    $total += $month_type_total;
                }
                $str_count = number_format($total,2);

                label_cell(text_bold($str_count), 'align=right');
                
                $month['month_value'] = $total;
                $overhead = $total * $overhead_percentage/100;
                $month['overhead'] = $overhead;
                
                $s = "sdgb";
            }   
            label_cell(text_bold(number_format($project_total_commitment,2)), 'align=right');
            
            start_row();
            label_cell(text_bold("Overhead (".$overhead_percentage."%)"), 'align=right');
                
            foreach ($expense_month_ranges_array as $month1) {
                $Overhead_display = $month1['overhead'];
                $str_count = number_format($Overhead_display,2);

                label_cell(text_bold($str_count), 'align=right');
            }   
            $total_Overhead = $project_total_commitment * $overhead_percentage/100;
            label_cell(text_bold(number_format($total_Overhead,2)), 'align=right');
            end_row();
            
            $project_total_commitment += $total_Overhead;
            
            
            start_row();
            label_cell(text_bold("Revenue"), "colspan=14");
            end_row();
            
            $revenue_month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
            
            $project_total_revenue = dashboard_pl_project_receipts($project_id, $revenue_month_ranges_array);   
            

            start_row();
            label_cell(text_bold("Period Revenue Total"), 'align=right');
                
            foreach ($revenue_month_ranges_array as &$month) {
                $month_totals = $month['month_totals'];
                $total = 0;
                foreach ($month_totals as $month_type_total) {
                    $total += $month_type_total;
                }
                $month['month_value'] = $total;
                $str_count = number_format($total,2);

                label_cell(text_bold($str_count), 'align=right');
            }   

            label_cell(text_bold(number_format($project_total_revenue,2)), 'align=right');
            end_row();
            
            
            start_row();
            label_cell(text_bold("Advances"), "colspan=14");
            end_row();
            $advances_month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
            
            $advances_balance = dashboard_pl_project_advances($project_id, $advances_month_ranges_array);
            start_row();
            label_cell(text_bold("Advances Balance"), 'align=right');
                
            foreach ($advances_month_ranges_array as &$month) {
                $month_totals = $month['month_totals'];
                $total = 0;
                foreach ($month_totals as $month_type_total) {
                    $total += $month_type_total;
                }
                $month['month_value'] = $total;
                $str_count = number_format($total,2);

                label_cell(text_bold($str_count), 'align=right');
            }   

            label_cell(text_bold(number_format($advances_balance,2)), 'align=right');
            end_row();
            start_row();
            
            
            label_cell(text_bold("Retentions"), "colspan=14");
            end_row();
            $retentions_month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
            
            $retentions_balance = dashboard_pl_project_retentions($project_id, $retentions_month_ranges_array);
            start_row();
            label_cell(text_bold("Retentions Balance"), 'align=right');
                
            foreach ($retentions_month_ranges_array as &$month) {
                $month_totals = $month['month_totals'];
                $total = 0;
                foreach ($month_totals as $month_type_total) {
                    $total += $month_type_total;
                }
                $month['month_value'] = $total;
                $str_count = number_format($total,2);

                label_cell(text_bold($str_count), 'align=right');
            }   

            label_cell(text_bold(number_format($retentions_balance,2)), 'align=right');
            end_row();
            
            start_row();
            label_cell(text_bold("Period Profit & Loss"), 'align=right');
                
            $final_pl = 0;
            foreach ($expense_month_ranges_array as $key=>&$month) {
                $expenses = -$month['month_value'];
                $overhead = -$month['overhead'];
                $revenue = $revenue_month_ranges_array[$key]['month_value'];
                $advances = $advances_month_ranges_array[$key]['month_value'];
                $retentions = $retentions_month_ranges_array[$key]['month_value'];

                $month_pl = $expenses + $overhead + $revenue + $advances + $retentions;
//                $str_count = number_format($month_pl,2);

                if($month_pl<0){ 
                    label_cell(text_bold(number_format($month_pl,2)), 'align=right style=color:red');
                }else{
                    label_cell(text_bold(number_format($month_pl,2)), 'align=right');
                }
//                label_cell(text_bold($str_count), 'align=right');
                 
                $final_pl += $month_pl; 
            }   
//            label_cell(text_bold(number_format($final_pl,2)), 'align=right');
            if($final_pl<0){ 
                label_cell(text_bold(number_format($final_pl,2)), 'align=right style=color:red');
            }else{
                label_cell(text_bold(number_format($final_pl,2)), 'align=right');
            }
            
    end_table(2);
}


function dashboard_pl_project_expenses($project_id, &$month_ranges_array){
    
    $project_total_commitment = 0;
    
    $type_total = 0;    
    dashboard_pl_material_issues_summary($project_id, ST_MATERIAL_ISSUE, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;

    $type_total = 0;    
    dashboard_pl_material_returns_summary($project_id, ST_MATERIAL_RETURN, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_daily_item_issues_summary($project_id, ST_DAILY_ITEM_ISSUE, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;

    $type_total = 0;
    dashboard_pl_daily_item_returns_summary($project_id, ST_DAILY_ITEM_RETURN, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_purchase_requisition_issues_summary($project_id, ST_PR_ISSUE, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_purchase_requisition_returns_summary($project_id, ST_PR_RETURN, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_site_material_return_summary($project_id, ST_SITE_MATERIAL_RETURN, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_subcontractor_invoices_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_subcontractor_credit_notes_summary($project_id, ST_SUBCONTRACTOR_CREDIT_NOTE, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_transport_expenses_summary($project_id, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_pl_iou_bank_payments_summary($project_id, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_pl_iou_bank_payment_returns_summary($project_id, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_pl_petty_cash_payments_summary($project_id, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_pl_iou_advances_summary($project_id, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_pl_utility_bills_summary($project_id, ST_UTILITY_BILLS, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;

    $type_total = 0; 
    dashboard_pl_utility_bill_credit_notes_summary($project_id, ST_UTILITY_BILL_CREDIT_NOTE, $month_ranges_array, $type_total); 
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_tool_usage_charges_summary($project_id, ST_TOOL_USAGE_CHARGE, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_monthly_project_costs_summary($project_id, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_miscellaneous_project_costs_summary($project_id, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0;
    dashboard_pl_project_cost_adjustments_summary($project_id, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    

    
//    dashboard_project_expenses_total_summary($project_total_commitment, "Expenses Total (Before Overheads)");
    
    $monthly_project_costs = 0;
//    dashboard_monthly_project_costs_summary($project_id, $from_date, $to_date, $monthly_project_costs);  
        
//    dashboard_project_overhead($project_id, $project_total_commitment);
    
    $project_total_commitment += $monthly_project_costs;
//    dashboard_project_expenses_total_summary($project_total_commitment, "Expenses Total (With Overheads)");
    
    return $project_total_commitment;
}



function dashboard_pl_material_issues_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    
    start_row();


    label_cell("Material Issues" );
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = get_material_issues_summary($project_id, $from_date, $to_date);
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
        
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    
    amount_cell($project_total_commitment);
    end_row();

}

function dashboard_pl_material_returns_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment, $make_positive=false)
{
    
    start_row();
    $link = "Material Returns";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_material_returns_summary($project_id, $from_date, $to_date);
        if($make_positive){
            
        }else{
            $total *= -1;
        }
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;    
        
        $month['month_totals'][$tran_type] = $total;
    }
    amount_cell($project_total_commitment);
    end_row();
    
}

function dashboard_pl_daily_item_issues_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    
    start_row();
    $link =  "Daily Item Issues";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_daily_item_issues_summary($project_id, $from_date, $to_date, $tran_type);
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
    
}


function dashboard_pl_daily_item_returns_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment,$make_positive=false)
{
    
    start_row();
    $link =  "Daily Item Returns";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_daily_item_issues_summary($project_id, $from_date, $to_date, $tran_type);
        if($make_positive){
            $total *= -1;
        }
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_purchase_requisition_issues_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment){
    
    
    start_row();
    $link =  "Purchase Requisition Issues";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        
        $total = get_purchase_requisition_issues_summary($project_id, $from_date, $to_date);
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_purchase_requisition_returns_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment, $make_positive=false){
    
    start_row();
    $link =  "Purchase Requisition Returns";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        
        $total = get_purchase_requisition_returns_summary($project_id, $from_date, $to_date);
        if($make_positive){
            
        }else{
            $total *= -1;
        }

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_site_material_return_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment,$make_positive=false){
    
    start_row();
    $link = "Site Material Returns";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_site_material_return_summary($project_id, $from_date, $to_date);
        if($make_positive){
            
        }else{
            $total *= -1;
        }

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_subcontractor_invoices_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $link = "Subcontractor Invoices";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_subcontractor_invoices_summary($tran_type, $project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_subcontractor_credit_notes_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment, $make_positive=false)
{
    start_row();
    $link = "Subcontractor Credit Notes";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        
        $total = get_subcontractor_credit_notes_summary($tran_type, $project_id, $from_date, $to_date);
        if($make_positive){
            $total *= -1;
        }

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_transport_expenses_summary($project_id, &$month_ranges_array, &$project_total_commitment)
{
    $tran_type = ST_VEHICLE_RUNNING_CHART;
    
    start_row();
    $link = "Vehicle Running Chart Entries";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = get_vehicle_running_chart_entries_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;
        
    }
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_iou_bank_payments_summary($project_id, &$month_ranges_array, &$project_total_commitment)
{    
    $tran_type = ST_IOU_BANKPAYMENT;
    
    start_row();
    $name = ("IOU Bank Payments");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_iou_bank_payments_summary($project_id, $from_date, $to_date, $tran_type);
        $total *= -1;
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
    
}

function dashboard_pl_iou_bank_payment_returns_summary($project_id, &$month_ranges_array, &$project_total_commitment, $make_positive=false)
{
    $tran_type = ST_IOU_BANKPAYMENT_RETURN;
    start_row();
    $name = ("IOU Bank Payment Returns");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_iou_bank_payments_summary($project_id, $from_date, $to_date, $tran_type);
        if($make_positive){
            
        }else{
            $total *= -1;
        }

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}


function dashboard_pl_petty_cash_payments_summary($project_id, &$month_ranges_array, &$project_total_commitment)
{
    $total = 0;
    $tran_type = ST_I_OWE_YOU_PAYMENT;
    
    start_row();
    $name = ("Petty Cash Payments");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        $total = 0;
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        
        $result = get_petty_cash_payments($project_id, $from_date, $to_date);
        while($inv = db_fetch($result))
        {
             $total += $inv["payment_amount"];
        }
    
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_iou_advances_summary($project_id, &$month_ranges_array, &$project_total_commitment)
{
    $tran_type =  ST_I_OWE_YOU_ADVANCES;
    
    start_row();
    $name = ("I Owe You Advances");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = get_iou_advances_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_utility_bills_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $name = "Utility Bills";
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        
        $total = get_utility_bills_summary($tran_type, $project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_utility_bill_credit_notes_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment, $make_positive=false)
{
    
    start_row();
    $name = "Utility Bill Credit Notes";
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_utility_bill_credit_notes_summary($tran_type, $project_id, $from_date, $to_date);
        if($make_positive){
            $total *= -1;
        }

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_tool_usage_charges_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $name = "Tool Usage Charges";
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_tool_usage_charges_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_monthly_project_costs_summary($project_id, &$month_ranges_array, &$project_total_commitment){
    
    $tran_type = ST_MONTHLY_PROJECT_COSTS;
    
    start_row();
    $name = ("Monthly Project Costs");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_monthly_project_costs_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_miscellaneous_project_costs_summary($project_id, &$month_ranges_array, &$project_total_commitment){
    
    $tran_type = ST_MISCELLANEOUS_PROJECT_COST;
    
    start_row();
    $name = ("Miscellaneous Project Costs");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = get_miscellaneous_project_costs_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}

function dashboard_pl_project_cost_adjustments_summary($project_id, &$month_ranges_array, &$project_total_commitment)
{
    $tran_type = ST_PROJECT_COST_ADJUST;
    
    start_row();
    $name = ("Project Cost Adjustments");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_project_cost_adjustments_summary($project_id, $from_date, $to_date);
        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_commitment += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_commitment);
    end_row();
}



function dashboard_pl_project_receipts($project_id, &$month_ranges_array){
    
    $project_total_revenue = 0;
    
    $type_total = 0;
    dashboard_pl_project_invoices_certified_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $type_total);  
    $project_total_revenue += $type_total;

    $type_total = 0;
    dashboard_pl_project_credit_notes_certified_summary($project_id, ST_PROJECT_CREDIT_NOTE, $month_ranges_array, $type_total);  
    $project_total_revenue += $type_total;
    
//    $_POST['INVOICES_TOTAL'] = $project_total_revenue;
    
    $type_total = 0;
    dashboard_pl_project_invoices_pending_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $type_total);  
    $project_total_revenue += $type_total;
    
    $type_total = 0;
    dashboard_pl_project_credit_notes_pending_summary($project_id, ST_PROJECT_CREDIT_NOTE, $month_ranges_array, $project_total_revenue);  
    $project_total_revenue += $type_total;
//    
//    
//    dashboard_project_receipts_total_summary($project_total_revenue); 
    
    return $project_total_revenue;
    
}


function  dashboard_pl_project_invoices_certified_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    start_row();
    $name = ("Project Invoices - Certified (IPCs)");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_project_invoices_certified_summary($tran_type, $project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_revenue);
    end_row();
}


function  dashboard_pl_project_credit_notes_certified_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    start_row();
    $name = ("Project Credit Notes");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_project_credit_notes_certified_summary($tran_type, $project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=$tran_type&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format(-$total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_revenue);
    end_row();
}



function  dashboard_pl_project_invoices_pending_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    start_row();
    $name = ("Project Invoices - Pending (IPAs)");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_project_invoices_pending_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=108P&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_revenue);
    end_row();
}



function  dashboard_pl_project_credit_notes_pending_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    start_row();
    $name = ("Project Credit Notes - Pending (IPAs)");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_project_credit_notes_pending_summary($project_id, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=128P&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format(-$total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_revenue);
    end_row();
}



function dashboard_pl_project_advances($project_id, &$month_ranges_array){
    
    $project_advances_total = $project_advances_settled_total = $subcontractor_advances_total = $subcontractor_advances_recovered_total = 0;
    
    dashboard_pl_project_advances_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $project_advances_total);  

    dashboard_pl_project_advances_settled_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $project_advances_settled_total);  
    
    dashboard_pl_subcontractor_advances_paid_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $month_ranges_array, $subcontractor_advances_total);  

    dashboard_pl_subcontractor_advances_recovered_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $month_ranges_array, $subcontractor_advances_recovered_total);  
    
    
    $advances_balance = $project_advances_total + $project_advances_settled_total + $subcontractor_advances_total + $subcontractor_advances_recovered_total;
//    start_row();
//    label_cell(text_bold("Advances Balance"), "align=right");
//    amount_cell($advances_balance, true);
//    label_cell('');
//    end_row();

    return $advances_balance;
    
}

function  dashboard_pl_project_advances_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    $trans_no = $total = 0;
    
    $line_category_code = "'ADVANCE'";
    
    start_row();
    $name = ("Project Advances Received");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $row = get_project_invoices_retentions_summary($tran_type, $project_id, $from_date, $to_date, $line_category_code);
        if(is_array($row)){
            $trans_no = $row['order_no'];
            $total = -abs($row['amount']);
        }

        if($total != 0){
            $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
        }
        else{
            $link = number_format(0,2);
        }

        label_cell($link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        
    }
    
    amount_cell($project_total_revenue);
    end_row();
}



function  dashboard_pl_project_advances_settled_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    $trans_no = $total = 0;
    
    $line_category_code = "'ADVANCE_RECOVERED'";
    
    start_row();
    $name = ("Project Advances Settled");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        
        $row = get_project_invoices_retentions_summary($tran_type, $project_id, $from_date, $to_date, $line_category_code);
        if(is_array($row)){
            $trans_no = $row['order_no'];
            $total = abs($row['amount']);
        }

        if($total != 0){
            $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
        }
        else{
            $link = number_format(0,2);
        }
        label_cell($link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] += $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
}



function  dashboard_pl_subcontractor_advances_paid_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{

    $line_category_code = "'ADVANCE'";
    
    start_row();
    $name = ("Subcontractor Advances Paid");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=SUB_AP&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }

        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
    
}

function  dashboard_pl_subcontractor_advances_recovered_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{

    $line_category_code = "'ADVANCE_RECOVERED'";
    
    start_row();
    $name = ("Subcontractor Advances Recovered");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = -get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);


        $parameters = "project_id=".$project_id."&tran_type=SUB_AR&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }

        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] += $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
}

function dashboard_pl_project_retentions($project_id, &$month_ranges_array){
    
    $retentions_total = $retentions_recovered_total = $retentions_held = $retentions_settled = 0;
    
    dashboard_pl_project_retentions_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $retentions_total);  

    dashboard_pl_project_retentions_recovered_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $retentions_recovered_total); 
    
    dashboard_pl_subcontractor_retentions_held_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $month_ranges_array, $retentions_held);  

    dashboard_pl_subcontractor_retentions_setlled_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $month_ranges_array, $retentions_settled); 
    
    $retentions_balance = $retentions_total + $retentions_recovered_total + $retentions_held + $retentions_settled;

    return $retentions_balance;
}



function  dashboard_pl_project_retentions_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    $trans_no = $total = 0;
    
    $line_category_code = "'RETENTION'";
    start_row();
    $name = ("Project Retentions (with the client)");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $row = get_project_invoices_retentions_summary($tran_type, $project_id, $from_date, $to_date, $line_category_code);
        if(is_array($row)){
            $trans_no = $row['order_no'];
            $total = abs($row['amount']);
        }

        if($total != 0){
            $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
        }
        else{
            $link = number_format(0,2);
        }
        label_cell($link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
}

function  dashboard_pl_project_retentions_recovered_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    $trans_no = $total = 0;
    
    $line_category_code = "'RETENTION_RECOVERED'";
    start_row();
    $name = ("Project Retentions Recovered");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $row = get_project_invoices_retentions_summary($tran_type, $project_id, $from_date, $to_date, $line_category_code);
        if(is_array($row)){
            $trans_no = $row['order_no'];
            $total = -abs($row['amount']);
        }
    
        if($total != 0){
            $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
        }
        else{
            $link = number_format(0,2);
        }
        label_cell($link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
}

function  dashboard_pl_subcontractor_retentions_held_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{
    
    $line_category_code = "'RETENTION'";
    
    start_row();
    $name = ("Subcontractor Retentions Held");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
    
        $total = -get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=SUB_RH&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
        
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }
        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] = $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
}


function  dashboard_pl_subcontractor_retentions_setlled_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_revenue)
{    
    $line_category_code = "'RETENTION_RECOVERED'";
    
    start_row();
    $name = ("Subcontractor Retentions Released");
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {
        
        $from_date = $month['month_begin'];
        $to_date = $month['month_end'];
        $total = get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);

        $parameters = "project_id=".$project_id."&tran_type=SUB_RS&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($total,2));
        }else{
            $amount_link = number_format(0,2);
        }

        label_cell($amount_link, "align=right");
        $project_total_revenue += $total;
        
        $month['month_totals'][$tran_type] += $total;        
        $total = 0;
    }
    
    amount_cell($project_total_revenue);
    end_row();
}
