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

function dashboard_project_cash_flow($from_month_display_order, $to_month_display_order, $project_id) {
    
    $project = get_project($project_id);
    $overhead_percentage = $project['overhead_percentage'];
    
    br();
        
    $cash_in_derect_months = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($cash_in_derect_months);
    
    start_table(TABLESTYLE, "width=$table_width%");
        
        $th = array("Type");
            foreach ($cash_in_derect_months 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("Cash In - Direct"), "colspan=14");
            end_row();
            
            $project_total_commitment = dashboard_cf_cash_in_direct($project_id, $cash_in_derect_months);
            start_row();
            label_cell(text_bold("Cash In - Direct Total"), 'align=right');
                
            foreach ($cash_in_derect_months 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');
            
            end_row();
            
            start_row();
            label_cell(text_bold("Cash In - Indirect"), "colspan=14");
            end_row();
            
            
            $cash_in_inderect_months = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
            $cf_months = $cash_in_inderect_months;
            dashboard_adjust_month_ranges_dates($cash_in_inderect_months, -30);
            $project_total_commitment = dashboard_cf_cash_in_indirect($project_id, $cash_in_inderect_months);
            start_row();
            label_cell(text_bold("Cash In - Indirect Total"), 'align=right');
                
            foreach ($cash_in_inderect_months 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');
            
            end_row();
            
            
            start_row();
            label_cell(text_bold("All Cash Ins Total"), 'align=right');
                
            $cash_in_total = 0;
            foreach ($cash_in_derect_months as $key=>&$month1) {
                $cash_in_direct_month_value = $month1['month_value'];
                $cash_in_indirect_month_value = $cash_in_inderect_months[$key]['month_value'];                

                $cash_in_month_value = $cash_in_direct_month_value + $cash_in_indirect_month_value;
                $cash_in_total += $cash_in_month_value;

                $cf_months[$key]['cash_in_month_value'] = $cash_in_month_value;
                
                $str_total = number_format($cash_in_month_value,2);

                label_cell(text_bold($str_total), 'align=right');

            }   
            label_cell(text_bold(number_format($cash_in_total,2)), 'align=right');
            
            end_row();
            
            
            start_row();
            label_cell(text_bold("Cash Out - Direct"), "colspan=14");
            end_row();
            
            $cash_out_derect_months = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
            $project_total_commitment = dashboard_cf_cash_out_direct($project_id, $cash_out_derect_months);
            start_row();
            label_cell(text_bold("Cash Out - Direct Total"), 'align=right');
                
            foreach ($cash_out_derect_months 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; 
                
                $s = "sdgb";
            }   
            label_cell(text_bold(number_format($project_total_commitment,2)), 'align=right');
            
            end_row();
            
            
            start_row();
            label_cell(text_bold("Cash Out - Indirect"), "colspan=14");
            end_row();
            
            $cash_out_inderect_months = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
            dashboard_adjust_month_ranges_dates($cash_out_inderect_months, -30);
            $project_total_commitment = dashboard_cf_cash_out_indirect($project_id, $cash_out_inderect_months);
            start_row();
            label_cell(text_bold("Cash Out - Indirect Total"), 'align=right');
                
            foreach ($cash_out_inderect_months 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; 
                
                $s = "sdgb";
            }   
            label_cell(text_bold(number_format($project_total_commitment,2)), 'align=right');
            
            end_row();
            
            start_row();
            label_cell(text_bold("All Cash Outs Total"), 'align=right');
                
            $cash_out_total = 0;
            foreach ($cash_out_derect_months as $key=>&$month1) {
                $cash_out_direct_month_value = $month1['month_value'];
                $cash_out_indirect_month_value = $cash_out_inderect_months[$key]['month_value'];

                $cash_out_month_value = $cash_out_direct_month_value + $cash_out_indirect_month_value;
                $cash_out_total += $cash_out_month_value;
                
                $cf_months[$key]['cash_out_month_value'] = $cash_out_month_value;

                $str_total = number_format($cash_out_month_value,2);

                label_cell(text_bold($str_total), 'align=right');

            }   
            label_cell(text_bold(number_format($cash_out_total,2)), 'align=right');
            
            end_row();
            
            start_row();
            label_cell(text_bold("Period Cash Flow"), 'align=right');
                
            $cash_flow_total = 0;
            foreach ($cf_months as $key=>&$month) {
                $all_cash_in_month_value = $month['cash_in_month_value'];
                $all_cash_out_month_value = $month['cash_out_month_value'];

                $cash_flow_month_value = $all_cash_in_month_value - $all_cash_out_month_value;
                $cash_flow_total += $cash_flow_month_value;
                
                $str_total = number_format($cash_flow_month_value,2);

                if($cash_flow_month_value<0){ 
                    label_cell(text_bold(number_format($cash_flow_month_value,2)), 'align=right style=color:red');
                }else{
                    label_cell(text_bold(number_format($cash_flow_month_value,2)), 'align=right');
                }
            }   

            if($cash_flow_total<0){ 
                label_cell(text_bold(number_format($cash_flow_total,2)), 'align=right style=color:red');
            }else{
                label_cell(text_bold(number_format($cash_flow_total,2)), 'align=right');
            }
            
            end_row();
            
            end_table();
    
}

function dashboard_cf_cash_out_direct($project_id, &$month_ranges_array){
    
    $project_total_commitment = 0;
    
    $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_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_cf_subcontractor_invoice_payments_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $month_ranges_array, $type_total);  
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_cf_utility_bills_payments_summary($project_id, ST_UTILITY_BILLS, $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_cf_project_credit_note_payments_summary($project_id, ST_PROJECT_CREDIT_NOTE, $month_ranges_array, $type_total, true);  
    $project_total_commitment += $type_total;
    
    return $project_total_commitment;
}



function dashboard_cf_project_credit_note_payments_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $name = "Project Credit Note Payments";
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

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

        $parameters = "pid=".$project_id."&type=".DASH_VW_PROJECT_CF_PROJECT_CREDIT_NOTE_PAYMENTS."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $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_cf_subcontractor_invoice_payments_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $link = "Subcontractor Invoice Payments";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

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

        $parameters = "pid=".$project_id."&type=".DASH_VW_PROJECT_CF_SUBCONTRACTOR_INVOICE_PAYMENTS."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $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_cf_utility_bills_payments_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $name = "Utility Bill Payments";
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

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

        $parameters = "pid=".$project_id."&type=".DASH_VW_PROJECT_CF_UTILITY_BILL_PAYMENTS."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $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_cf_cash_out_indirect($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_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_purchase_requisition_issues_summary($project_id, ST_PR_ISSUE, $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_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_miscellaneous_project_costs_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_cf_cash_in_direct($project_id, &$month_ranges_array){
    
    $project_total_commitment = 0;
    
    $type_total = 0;
    dashboard_cf_project_invoice_receipts_summary($project_id, ST_PROJECT_INVOICE, $month_ranges_array, $type_total, true);  
    $project_total_commitment += $type_total;    
    
    $type_total = 0; 
    dashboard_pl_iou_bank_payment_returns_summary($project_id, $month_ranges_array, $type_total, true); 
    $project_total_commitment += $type_total;

    $type_total = 0;
    dashboard_cf_subcontractor_credit_note_receipts_summary($project_id, ST_SUBCONTRACTOR_CREDIT_NOTE, $month_ranges_array, $type_total, true);  
    $project_total_commitment += $type_total;
    
    $type_total = 0; 
    dashboard_cf_utility_bill_credit_note_receipts_summary($project_id, ST_UTILITY_BILL_CREDIT_NOTE, $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_cf_project_invoice_receipts_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment)
{
    start_row();
    $link = "Project Invoice Receipts";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

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

        $parameters = "pid=".$project_id."&type=".DASH_VW_PROJECT_CF_PROJECT_INVOICE_RECEIPTS."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
        if($total != 0){
            $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $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_cf_subcontractor_credit_note_receipts_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment, $make_positive=false)
{
    start_row();
    $link = "Subcontractor Credit Note Receipts";
    label_cell($link);
    
    foreach ($month_ranges_array as &$month) {

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

        $parameters = "pid=".$project_id."&type=".DASH_VW_PROJECT_CF_SUBCONTRACTOR_CREDIT_NOTE_RECEIPTS."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $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_cf_utility_bill_credit_note_receipts_summary($project_id, $tran_type, &$month_ranges_array, &$project_total_commitment, $make_positive=false)
{
    
    start_row();
    $name = "Utility Bill Credit Note Receipts";
    label_cell($name);
    
    foreach ($month_ranges_array as &$month) {

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

        $parameters = "pid=".$project_id."&type=".DASH_VW_PROJECT_CF_UTILITY_BILL_CREDIT_NOTE_RECEIPTS."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date);
//        $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $parameters, number_format($total,2));
        if($total != 0){
            $amount_link = get_dashboard_view_str(DASH_SC_PROJECT_CASH_FLOW, $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_cf_cash_in_indirect($project_id, &$month_ranges_array){
    
    $project_total_commitment = 0;
    
    $type_total = 0;    
    dashboard_pl_material_returns_summary($project_id, ST_MATERIAL_RETURN, $month_ranges_array, $type_total, true); 
    $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_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;
    
//    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;
}


