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

function display_project_details1($project_id) 
{       

    $proj = get_project_record($project_id);
    start_table(TABLESTYLE_NOBORDER, "width=70%");
        start_row();
            label_cell(text_bold($proj['description']), 'align=center');
        end_row();
    if(strlen($proj['long_description']) > 0){ 
        start_row();
            label_cell($proj['long_description'], 'align=center');
        end_row();
    }
    end_table(2);
}

function display_project_details($project_id, $from_date, $to_date) 
{       
    if(!($project_id > 0)){
        return;
    }
    
    display_project_details1($project_id);
    
    $myrow = get_approve_project_details($project_id);
    display_project_details2($myrow, $from_date, $to_date);
    $dep_approved_all_budgets_total = display_project_details3($myrow, $from_date, $to_date);
    
    return $dep_approved_all_budgets_total;
              
}

function display_project_details2($myrow, $from_date, $to_date) 
{       
    global $approval_statuses;
    
    $project_id = $myrow['project_id'];
    
    display_heading2("Project Details");
//    br(1);
    start_outer_table(TABLESTYLE2);

    table_section(1);
    table_section_title(_("Project"));
        
	//------------------------------------------------------------------------------------            

            
    
            label_row("No:", $myrow['project_id']);
            label_row("Code:", $myrow['project_code']);
            
            label_row("Department:", $myrow['department_name']);
            label_row("Customer:", $myrow['customer_name']);
            label_row("Start Date:", sql2date($myrow["start_date"]));
            label_row("End Date:", sql2date($myrow["end_date"]));
            label_row("Project Manager Name:", $myrow['project_manager_name']);
            label_row("(System) Project Manager:", $myrow["real_name"]);
            label_row("Contact Person:", $myrow['contact_person']);
            label_row("Project Status:", $myrow['project_status_name']);

            
            table_section(2);
            table_section_title(_("Accounts"));
            $revenue_account = $myrow['revenue_account']."-".$myrow['revenue_account_name'];
            label_row("Revenue Account:", $revenue_account);
            
            $cost_account = $myrow['cost_account']."-".$myrow['cost_account_name'];
            label_row("Cost Account:", $cost_account);
            
            
            table_section_title(_("Approvals"));
            
            $s = get_project_trans_view_str(ST_APPROVAL_LOG, $project_id."&source_type_id=".ST_APPROVE_PROJECT, "Approval Status");
            label_row($s,$approval_statuses[$myrow['approval_status']]);
          
            global $path_to_root;
            
            $no_project_revisions = get_project_revisions_count($project_id);
            $project_revisions_link = "<a href='$path_to_root/projects/inquiry/project_revisions_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $no_project_revisions . "</a>";
            label_row("Project Revisions", $project_revisions_link);
            
            $no_budget_revisions = get_budget_revisions_count($project_id);
            $budget_revisions_link = "<a href='$path_to_root/projects/inquiry/task_revisions_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $no_budget_revisions . "</a>";
            label_row("Budget Revisions", $budget_revisions_link);
    
        end_outer_table(2);
        
//        hidden('project_id',$project_id);
                

//        submit('update_app_project', _("Approve Project"), true, '', 'default');
        
              
}

function display_project_details3($myrow, $from_date, $to_date) 
{       
    
    $project_id = $myrow['project_id'];
    
    $cost_budget_details = get_project_tasks_cost_budgets_total($myrow["project_id"]);
    $overhead_budget_details = get_project_tasks_overhead_budgets_total($myrow["project_id"]);
    $dep_approved_all_budgets_total = $cost_budget_details['current_budgets_total'] + $overhead_budget_details['current_budgets_total'];
    
    if (!user_view_available_budget()){
        return $dep_approved_all_budgets_total;
    }
    
    display_heading2("Project Values");
//    br(1);
    start_outer_table(TABLESTYLE2);

    
            
            
            table_section(1);
            table_section_title(_("Department Approved Values"));
            
            $proj_approved_project_value = $myrow['project_value'];
            $dep_approved_project_value = $myrow['editing_project_value'];
            
//            $proj_approved_required_overhead_value = $proj_approved_project_value * $myrow['overhead_percentage'] / 100;
//            label_row("Minimum Required Overhead Value:", number_format($proj_approved_required_overhead_value, 2)." (".number_format($myrow['overhead_percentage'], 2)."%)");
//          
            
            //label_row("Project Approved Budgets Total:", number_format($myrow['approved_budgets_total'], 2));
            
            //==================Department approved values=====================
//            start_row();
//            label_cell(text_bold("Department Approved Values"), "colspan=2 align=center");
//            end_row();
            
            label_row("Project Value:", number_format($dep_approved_project_value, 2));
            
            $dep_approved_required_overhead_value = $dep_approved_project_value * $myrow['overhead_percentage'] / 100;
            label_row("Minimum Required Overhead Value:", number_format($dep_approved_required_overhead_value, 2)." (".number_format($myrow['overhead_percentage'], 2)."%)");
          
           
            
            
            //----cost budgets----
            $dep_approved_cost_budgets_percentage = 0;
            if($dep_approved_project_value > 0){
                $dep_approved_cost_budgets_percentage = $cost_budget_details['current_budgets_total']/$dep_approved_project_value*100;  
            }
            
            $approved_budgets_text = number_format($cost_budget_details['current_budgets_total'],2)." (".number_format($dep_approved_cost_budgets_percentage,2)."%)";
            label_row("Cost Budgets Total:", $approved_budgets_text);     

            //----overhead budgets----            
            $dep_approved_overhead_budgets_percentage = 0;
            if($dep_approved_all_budgets_total > 0){
                $dep_approved_overhead_budgets_percentage = $overhead_budget_details['current_budgets_total']/$cost_budget_details['current_budgets_total']*100;  
            }
            
            if($dep_approved_required_overhead_value < $overhead_budget_details['current_budgets_total'] + 0.0000001){
                $approved_budgets_text = number_format($overhead_budget_details['current_budgets_total'],2)." (".number_format($dep_approved_overhead_budgets_percentage,2)."%)";
            }
            else{
                //PROJECT_TASK_DETAIL_TYPE_CATEGORIES OVERHEAD
                $overhead_lv_rec = get_listvalue_by_code("PROJECT_TASK_DETAIL_TYPE_CATEGORIES", "OVERHEAD");
                $approved_budgets_text = "<font color='red'>".number_format($overhead_budget_details['current_budgets_total'],2)." (".number_format($dep_approved_overhead_budgets_percentage,2)."%)";
//                                            ." - ".'"'.$overhead_lv_rec['value_name'].'"'." budgets total not sufficient to cover the ".'"'."Minimum Required Overhead".'"'."</font>";
            
            }
                
            label_row("Overhead Budgets Total:", $approved_budgets_text); 
            label_row("Total Cost:", number_format($dep_approved_all_budgets_total,2)); 

            
            if($dep_approved_all_budgets_total > 0 && $dep_approved_project_value > 0){
                $dep_expected_profit = $dep_approved_project_value - $dep_approved_all_budgets_total; 
                $dep_expected_profit_percentage = $dep_expected_profit/$dep_approved_project_value*100;
                $dep_expected_profit_text = number_format($dep_expected_profit,2)." (".number_format($dep_expected_profit_percentage,2)."%)";
            }
            else{
                $dep_expected_profit_text = "Department budget approval pending";
            }
            label_row("Expected Profit:", text_bold($dep_expected_profit_text));     
            
            $dep_unapproved_all_budgets_total = $cost_budget_details['unapproved_budgets_total'] + $overhead_budget_details['unapproved_budgets_total'];
            if($dep_unapproved_all_budgets_total != 0){
                label_row("Unapproved Budgets Total:", '<font color="red">'. text_bold(number_format($dep_unapproved_all_budgets_total, 2)).'</font>');
            }
            else{
                label_row("Unapproved Budgets Total:", number_format(0, 2));
            }
            
            //==================Project approved values=====================
            table_section(2);
            table_section_title(_("Project Approved Values"));
//            start_row();
//            label_cell(text_bold("Project Approved Values"), "colspan=2 align=center");
//            end_row();
            
            label_row("Project Value:", number_format($proj_approved_project_value, 2));
            $proj_approved_required_overhead_value = $proj_approved_project_value * $myrow['overhead_percentage'] / 100;
            label_row("Minimum Required Overhead Value:", number_format($proj_approved_required_overhead_value, 2)." (".number_format($myrow['overhead_percentage'], 2)."%)");
          
            
            $proj_approved_all_budgets_total =  $myrow['approved_cost_budgets_total'] +  $myrow['approved_overhead_budgets_total'];
            
            //----cost budgets----
            $proj_approved_cost_budgets_percentage = 0;
            if($proj_approved_all_budgets_total > 0){
                $proj_approved_cost_budgets_percentage = $myrow['approved_cost_budgets_total']/$proj_approved_project_value*100;  
            }
            $approved_budgets_text = number_format($myrow['approved_cost_budgets_total'],2)." (".number_format($proj_approved_cost_budgets_percentage,2)."%)";
            label_row("Cost Budgets Total:", $approved_budgets_text);      
            
            
            //----overhead budgets----            
            $proj_approved_overhead_budgets_percentage = 0;
            if($proj_approved_all_budgets_total > 0){
                $proj_approved_overhead_budgets_percentage = $myrow['approved_overhead_budgets_total']/$myrow['approved_cost_budgets_total']*100;  
            }
            $approved_budgets_text = number_format($myrow['approved_overhead_budgets_total'],2)." (".number_format($proj_approved_overhead_budgets_percentage,2)."%)";
            label_row("Overhead Budgets Total:", $approved_budgets_text); 
            label_row("Total Cost:", number_format($proj_approved_all_budgets_total,2)); 
            
            global $approval_statuses;
            if($proj_approved_all_budgets_total > 0){
                $proj_expected_profit = $proj_approved_project_value - $proj_approved_all_budgets_total; 
                $proj_expected_profit_percentage = $proj_expected_profit/$proj_approved_project_value*100;
                $proj_expected_profit_text = number_format($proj_expected_profit,2)." (".number_format($proj_expected_profit_percentage,2)."%)";
            }
            else{
                
                $proj_expected_profit_text = $approval_statuses[$myrow['approval_status']];
            }
            label_row("Expected Profit:", text_bold($proj_expected_profit_text));                 
            
            
    
        end_outer_table(1);
        
//        hidden('project_id',$project_id);
                

//        submit('update_app_project', _("Approve Project"), true, '', 'default');
        
        return $dep_approved_all_budgets_total;
              
}


function display_task_category_summary($Project_id, $dep_approved_all_budgets_total)
{
    display_heading2("Budget Category Summary");

    start_table(TABLESTYLE, "width=70%");

    $th = array(_("Budget Category"), _("Approved"), _("%"), 
                _("Used"), _("Allocated"), _("Available"), "Unapproved");
      
    table_header($th);

//    $current_budgets_total = $bdget_totals['current_budgets_total'];
    $project_total_budgets = $project_total_budget_used = $project_total_budget_allocated = 0;
    $project_total_approved_available_budget = $project_total_unapproved_budgets_total = 0;
    
    $boq_item_group_res = get_task_category_summary($Project_id);
    while($row = db_fetch($boq_item_group_res))
    {
        start_row();
        label_cell($row['category_name']);
        amount_decimal_cell($row['total_budget']);
        $project_total_budgets += $row['total_budget'];
        
        if($dep_approved_all_budgets_total>0){
            label_cell(number_format($row['total_budget']/$dep_approved_all_budgets_total*100,2), "align=right");
        }
        else{
            label_cell(number_format(0,2), "align=right");
        }
//        amount_decimal_cell($row['total_new_budget']);
        amount_decimal_cell($row['total_budget_used']);
        $project_total_budget_used += $row['total_budget_used'];
        
        amount_decimal_cell($row['total_budget_allocated']);
        $project_total_budget_allocated += $row['total_budget_allocated'];
        
        $approved_available_budget = $row['total_budget'] - ($row['total_budget_used'] + $row['total_budget_allocated']);
        amount_decimal_cell($approved_available_budget);
        $project_total_approved_available_budget += $approved_available_budget;
        
        $unapproved_budgets_total = $row['total_new_budget'] - $row['total_budget'];
        if($unapproved_budgets_total != 0){
            label_cell('<font color="red">'. number_format($unapproved_budgets_total, 2).'</font>', "align=right");
        }
        else{
            amount_decimal_cell(0);
        }
        $project_total_unapproved_budgets_total += $unapproved_budgets_total;
        
        end_row();

    }
    
    start_row();
        label_cell(text_bold("Project Totals"), "align=right");
        amount_cell($project_total_budgets, true);
        label_cell("");
        
        $project_total_budget_used_perc_text = $project_total_budget_allocated_perc_text = $project_total_approved_available_budget_perc_text = "";
        if($project_total_budgets > 0){
            $project_total_budget_used_perc_text = "(".percent_format($project_total_budget_used/$project_total_budgets*100)."%)";
            $project_total_budget_allocated_perc_text = "(".percent_format($project_total_budget_allocated/$project_total_budgets*100)."%)";
            $project_total_approved_available_budget_perc_text = "(".percent_format($project_total_approved_available_budget/$project_total_budgets*100)."%)";
            $project_total_unapproved_budgets_total_perc_text = "(".percent_format($project_total_unapproved_budgets_total/$project_total_budgets*100)."%)";
        }
        label_cell(text_bold(price_format($project_total_budget_used).$project_total_budget_used_perc_text), "align=right");
        label_cell(text_bold(price_format($project_total_budget_allocated).$project_total_budget_allocated_perc_text), "align=right");
        label_cell(text_bold(price_format($project_total_approved_available_budget).$project_total_approved_available_budget_perc_text), "align=right");
        
        if($project_total_unapproved_budgets_total != 0){
            label_cell('<font color="red">'.text_bold(price_format($project_total_unapproved_budgets_total).$project_total_unapproved_budgets_total_perc_text).'</font>', "align=right");
        }
        else{
            label_cell(text_bold(price_format($project_total_unapproved_budgets_total)),"align=right");
        }
        
        end_row();

    
    end_table();
}
        
        
function display_project_profit_and_loss($project_id, $from_date, $to_date){
    
    display_heading2("Proft and Loss");
//    br(1);
    
    start_table(TABLESTYLE, "width=90%");
    $th = array(_("Details"), _("Amount"), _("%"));
      
    table_header($th);

    start_row();
    label_cell(text_bold("Expenses"), "colspan=3");
    end_row();
    
    $project_total_commitment = display_project_expenses($project_id, $from_date, $to_date);
    
    start_row();
    label_cell(text_bold("Revenue"), "colspan=3");
    end_row();
    
    $project_total_revenue = display_project_receipts($project_id, $from_date, $to_date);    
    
    $project_total_revenue += display_project_advances($project_id, $from_date, $to_date);
    
    $project_total_revenue += display_project_retentions($project_id, $from_date, $to_date);
    
    start_row();
    label_cell(text_bold("Total Revenue"), "align=right");
    amount_cell($project_total_revenue, true);
    label_cell('');
    end_row();
    
    
    print_project_cumulated_work_done($project_id, ST_PROJECT_INVOICE, $from_date, $to_date);
//    $cumulative_work_done = display_project_retentions($project_id, $from_date, $to_date);
//    start_row();
//    label_cell(text_bold("Cumulative Work Done"), "align=right");
//    amount_cell($cumulative_work_done, true);
//    label_cell('');
//    end_row();
    
//    print_project_summary($project_total_commitment, $project_total_revenue);
    
    if (!user_view_available_budget()){
        end_table();
        return;
    }
    
    start_row();
    label_cell(text_bold("Project Summary"), "colspan=3");
    end_row();
    
    $project = get_project_record($project_id);
    start_row();
    label_cell(text_bold("Project Value"));
    amount_cell($project['project_value'], true);
    label_cell('');
    end_row();
    
    start_row();
    label_cell(text_bold("Total billed as at date"));
    amount_cell($project_total_revenue, true);
    if($project['project_value'] > 0){
        label_cell(text_bold(number_format($project_total_revenue/$project['project_value']*100,2)."%"), "align=right");
    }
    else{
        label_cell("");
    }
    end_row();
    
    $approved_budgets_total = $project['approved_cost_budgets_total'] + $project['approved_overhead_budgets_total'];
    start_row();
    label_cell(text_bold("Estimated Cost")." (Project approved budgets total)");
    amount_cell($approved_budgets_total, true);
    label_cell('');
    end_row();
    
    start_row();
    label_cell(text_bold("Cost spent as at date"));
    amount_cell($project_total_commitment, true);
    if($approved_budgets_total> 0){
        label_cell(text_bold(number_format($project_total_commitment/$approved_budgets_total*100,2)."%"), "align=right");
    }
    else{
        label_cell('');
    }
    end_row();
    
    $profit = $project_total_revenue - $project_total_commitment;
    start_row();
    label_cell(text_bold("Profit / (Loss) as at date"));
    if($profit > 0){
        label_cell(text_bold(number_format($profit,2)), "align=right");
    }
    else{
        label_cell(text_bold('<font color="red">'."(".number_format(abs($profit),2).")".'</red>'), "align=right");
    }
    label_cell('');
    end_row();
    
    end_table();
}


function display_project_cost_allocations($project_id, $from_date, $to_date){
    
    display_heading2("Cost Allocations");
//    br(1);
    
    start_table(TABLESTYLE, "width=100%");
    $th = array(_("Details"), _("Amount"));
      
    table_header($th);

    start_row();
    label_cell(text_bold("Expenses"), "colspan=2");
    end_row();
    

    $project_total_revenue = 0;
    
    print_project_material_requisitions_summary($project_id, ST_MATERIAL_REQ, $from_date, $to_date, $project_total_revenue);  

    print_project_purchase_requisitions_summary($project_id, ST_PURCH_REQ, $from_date, $to_date, $project_total_revenue);  
    
    print_project_subcontractor_purchase_orders_summary($project_id, ST_SUBCONTRACTOR_PURCH_ORDER, $from_date, $to_date, $project_total_revenue);  

    print_project_i_owe_yous_summary($project_id, ST_I_OWE_YOU, $from_date, $to_date, $project_total_revenue);  
    
    print_project_vehicle_requests_summary($project_id, ST_VEHICLE_REQUEST, $from_date, $to_date, $project_total_revenue);  

    print_project_allocations_total_summary($project_total_revenue); 
        
    end_table();
    
    return $project_total_revenue;
}

function  print_project_allocations_total_summary($project_total_revenue)
{

    start_row();
    label_cell(text_bold("Total"), "align=right");
    amount_cell($project_total_revenue, true);
    end_row();

}


function  print_project_summary($project_total_commitment, $project_total_revenue)
{

    start_row();
    label_cell("Project Total");
    label_cell("");
    label_cell($project_total_commitment + $project_total_revenue);
    end_row();

}

function display_project_receipts($project_id, $from_date, $to_date){
    
    $project_total_revenue = 0;
    
    print_project_invoices_certified_summary($project_id, ST_PROJECT_INVOICE, $from_date, $to_date, $project_total_revenue);  

    print_project_credit_notes_certified_summary($project_id, ST_PROJECT_CREDIT_NOTE, $from_date, $to_date, $project_total_revenue);  
    
    $_POST['INVOICES_TOTAL'] = $project_total_revenue;
    
    print_project_invoices_pending_summary($project_id, ST_PROJECT_INVOICE, $from_date, $to_date, $project_total_revenue);  

//    print_project_credit_notes_pending_summary($project_id, ST_PROJECT_CREDIT_NOTE, $from_date, $to_date, $project_total_revenue);  
    
    
    
    print_project_receipts_total_summary($project_total_revenue); 
    
    return $project_total_revenue;
    
}

function  print_project_receipts_total_summary($project_total_revenue)
{

    start_row();
    label_cell(text_bold("Invoices Total"), "align=right");
    amount_cell($project_total_revenue, true);
    label_cell('');
    end_row();

}

function display_project_advances($project_id, $from_date, $to_date){
    
    $project_advances_total = $project_advances_settled_total = $subcontractor_advances_total = $subcontractor_advances_recovered_total = 0;
    
    print_project_advances_summary($project_id, ST_PROJECT_INVOICE, $from_date, $to_date, $project_advances_total);  // ADVANCE

    print_project_advances_settled_summary($project_id, ST_PROJECT_INVOICE, $from_date, $to_date, $project_advances_settled_total);  //ADVANCE_RECOVERED
    
    print_subcontractor_advances_paid_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date, $subcontractor_advances_total);  //ADVANCE

    print_subcontractor_advances_recovered_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date, $subcontractor_advances_recovered_total); //ADVANCE_RECOVERED 
    
    
    $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 display_project_retentions($project_id, $from_date, $to_date){
    
    $retentions_total = $retentions_recovered_total = $retentions_held = $retentions_settled = 0;
    
    print_project_retentions_summary($project_id, ST_PROJECT_INVOICE, $from_date, $to_date, $retentions_total);  //RETENTION

    print_project_retentions_recovered_summary($project_id, ST_PROJECT_INVOICE, $from_date, $to_date, $retentions_recovered_total); //RETENTION_RECOVERED
    
    print_subcontractor_retentions_held_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date, $retentions_held);  //RETENTION

    print_subcontractor_retentions_setlled_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date, $retentions_settled); //RETENTION_RECOVERED
    
    $retentions_balance = $retentions_total + $retentions_recovered_total - $retentions_held + $retentions_settled;
    
    start_row();
    label_cell(text_bold("Retentions Balance"), "align=right");
    amount_cell($retentions_balance, true);
    label_cell('');
    end_row();

    return $retentions_balance;
}


function  print_project_credit_notes_pending_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $total = get_project_credit_notes_pending_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Project Credit Notes - Pending (IPAs)");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_credit_notes_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue -= $total;
}


function  print_project_invoices_pending_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $total = get_project_invoices_pending_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Project Invoices - Pending (IPAs)");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}


function  print_project_credit_notes_certified_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $total = get_project_credit_notes_certified_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Project Credit Notes");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_credit_notes_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue -= $total;
}

function  print_project_invoices_certified_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $total = get_project_invoices_certified_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Project Invoices - Certified (IPCs)");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}

function  print_project_retentions_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $trans_no = $total = "NF";
    
    $line_category_code = "'RETENTION'";
    
    $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']);
    }
    
    start_row();
    $name = ("Project Retentions (with the client)");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
//    $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));
//    label_cell($amount_link, "align=right");
    
    if($total != "NF"){
        $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
    }
    else{
//        $link = number_format($total,2);
        $link = "Not found";
    }
    label_cell($link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}

function  print_subcontractor_retentions_held_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    
    $line_category_code = "'RETENTION'";
    
    $total = get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);

    start_row();
    $name = ("Subcontractor Retentions Held");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_invoice_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    $parameters = "project_id=".$project_id."&tran_type=SUB_RH&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));
    label_cell($amount_link, "align=right");

    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}

function  print_project_cumulated_work_done($project_id, $tran_type, $from_date, $to_date)
{
    $trans_no = $total = 0;
    
    $line_category_code = "'FINAL_BILL','CUMULATIVE_WORKDONE'";
    
    $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 = $row['amount'];
    }
    
    start_row();
    $name = ("Cumulative Workdone");
//    global $path_to_root;
//    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//    label_cell($link);
    
    $text = text_bold('<font color="blue">'.$name.'</blue>');
    label_cell($text);
//    $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));
//    label_cell($amount_link, "align=right");
    
    if($total > 0){
        $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
        $link = text_bold('<font color="blue">'.$link.'</blue>');
    }
    else{
//        $link = number_format($total,2);
        $link = "Not found";
    }
    label_cell($link, "align=right");
    label_cell('');
    end_row();
    
//    $project_total_revenue += $total;
}

function  print_project_retentions_recovered_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $trans_no = $total = "NF";
    
    $line_category_code = "'RETENTION_RECOVERED'";
    
    $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']);
    }
    
    
    start_row();
    $name = ("Project Retentions Recovered");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    
//    $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 != "NF"){
        $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
    }
    else{
//        $link = number_format(-$total,2);
        $link = "Not found";
    }
    label_cell($link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}

function  print_subcontractor_retentions_setlled_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{    
    $line_category_code = "'RETENTION_RECOVERED'";
    
    $total = get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);
    
    start_row();
    $name = ("Subcontractor Retentions Released");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_invoice_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    
    $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));

    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}

function  print_project_advances_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $trans_no = $total = "NF";
    
    $line_category_code = "'ADVANCE'";
    
    $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']);
    }
    
    
    start_row();
    $name = ("Project Advances Received");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    
//    $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 != "NF"){
        $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
    }
    else{
//        $link = number_format(-$total,2);
        $link = "Not found";
    }
    
    label_cell($link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}

function  print_project_advances_settled_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{
    $trans_no = $total = "NF";
    
    $line_category_code = "'ADVANCE_RECOVERED'";
    
    $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']);
    }
    
    
    start_row();
    $name = ("Project Advances Settled");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_billing_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    
//    $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 != "NF"){
        $link = get_project_trans_view_str($tran_type, $trans_no, number_format($total,2));
    }
    else{
//        $link = number_format($total,2);
        $link = "Not found";
    }
    label_cell($link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue -= $total;
}


function  print_subcontractor_advances_paid_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{

    $line_category_code = "'ADVANCE'";
    
    $total = get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);
    
    start_row();
    $name = ("Subcontractor Advances Paid");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_invoice_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    
    $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));
    
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue += $total;
}


function get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_in, $from_date, $to_date)
{
    $total_advance = 0;
    
    $subcontractors_res = get_project_subcontractors($project_id, $line_category_in);
    while ($subcontractor = db_fetch($subcontractors_res)) {
        $subcontractor_id = $subcontractor["supplier_id"];
        if($subcontractor_id == 1571){
            $s=234;
        }
        $advance_res = get_subcontractor_invoice_advance_paid($project_id, $subcontractor_id, $line_category_in, $from_date, $to_date);
        while ($advance = db_fetch($advance_res)) {
            $total_advance += abs($advance['amount']);
        }
    }
    
    return $total_advance;
 
}

function  print_subcontractor_advances_recovered_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_revenue)
{

    $line_category_code = "'ADVANCE_RECOVERED'";
    
    $total = get_subcontractor_invoices_advances_paid_summary($project_id, $line_category_code, $from_date, $to_date);
    
    start_row();
    $name = ("Subcontractor Advances Recovered");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_invoice_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
//    label_cell($name);
    
    $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));

    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_revenue = $total;
}


function display_project_expenses($project_id, $from_date, $to_date){
    
    $project_total_commitment = 0;
    
    display_material_issues_summary($project_id, ST_MATERIAL_ISSUE, $from_date, $to_date, $project_total_commitment);  

    display_material_returns_summary($project_id, ST_MATERIAL_RETURN, $from_date, $to_date, $project_total_commitment); 
    
    display_daily_item_issues_summary($project_id, ST_DAILY_ITEM_ISSUE, $from_date, $to_date, $project_total_commitment);  

    display_daily_item_returns_summary($project_id, ST_DAILY_ITEM_RETURN, $from_date, $to_date, $project_total_commitment);  
    
//    display_material_purchased_invoices_summary($project_id, ST_SUPPINVOICE, $from_date, $to_date, $project_total_commitment);  
//
//    display_material_purchase_credit_notes_summary($project_id, ST_SUPPCREDIT, $from_date, $to_date, $project_total_commitment);  
    
    display_purchase_requisition_issues_summary($project_id, ST_PR_ISSUE, $from_date, $to_date, $project_total_commitment);  
    
    display_purchase_requisition_returns_summary($project_id, ST_PR_RETURN, $from_date, $to_date, $project_total_commitment);  
    
    display_site_material_return_summary($project_id, ST_SITE_MATERIAL_RETURN, $from_date, $to_date, $project_total_commitment); 
    
    display_subcontractor_invoices_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date, $project_total_commitment);  

    display_subcontractor_credit_notes_summary($project_id, ST_SUBCONTRACTOR_CREDIT_NOTE, $from_date, $to_date, $project_total_commitment);  

    display_transport_expenses_summary($project_id, $from_date, $to_date, $project_total_commitment);  
    
    display_iou_bank_payments_summary($project_id, $from_date, $to_date, $project_total_commitment); 
    
    display_iou_bank_payment_returns_summary($project_id, $from_date, $to_date, $project_total_commitment); 
    
    display_petty_cash_payments_summary($project_id, $from_date, $to_date, $project_total_commitment); 
    
    display_iou_advances_summary($project_id, $from_date, $to_date, $project_total_commitment); 
    
    display_utility_bills_summary($project_id, ST_UTILITY_BILLS, $from_date, $to_date, $project_total_commitment);  

    display_utility_bill_credit_notes_summary($project_id, ST_UTILITY_BILL_CREDIT_NOTE, $from_date, $to_date, $project_total_commitment); 
    
    display_tool_usage_charges_summary($project_id, ST_TOOL_USAGE_CHARGE, $from_date, $to_date, $project_total_commitment);  

    display_monthly_project_costs_summary($project_id, $from_date, $to_date, $project_total_commitment);  
    
    display_miscellaneous_project_costs_summary($project_id, $from_date, $to_date, $project_total_commitment);  
    
    display_project_cost_adjustments_summary($project_id, $from_date, $to_date, $project_total_commitment);  
    

//    display_subcontractor_retentions_summary($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date, $project_total_commitment);  
//
//    display_subcontractor_retention_recovered_summary($project_id, ST_SUBCONTRACTOR_CREDIT_NOTE, $from_date, $to_date, $project_total_commitment);
    
//    display_wip_costs_summary($project_id, $from_date, $to_date, $project_total_commitment);  
    
    
    
    display_project_expenses_total_summary($project_total_commitment, "Expenses Total (Before Overheads)");
    
    $monthly_project_costs = 0;
//    display_monthly_project_costs_summary($project_id, $from_date, $to_date, $monthly_project_costs);  
        
    display_project_overhead($project_id, $project_total_commitment);
    
    $project_total_commitment += $monthly_project_costs;
    display_project_expenses_total_summary($project_total_commitment, "Expenses Total (With Overheads)");
    
    return $project_total_commitment;
}

function display_material_returns_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = -get_material_returns_summary($project_id, $from_date, $to_date);
    
    start_row();
    global $path_to_root;
    $link = "<a href='$path_to_root/inventory/inquiry/material_returns_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Material Returns" . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_material_issues_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_material_issues_summary($project_id, $from_date, $to_date);
    
    start_row();
    
    global $path_to_root;
    $link = "<a href='$path_to_root/inventory/inquiry/material_issues_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Material Issues" . "</a>";
//    $link = "<a href='$path_to_root/inventory/inquiry/material_issues_inquiry.php'  target=_blank>" . "Material Issues" . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_daily_item_returns_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_daily_item_issues_summary($project_id, $from_date, $to_date, $tran_type);
    
    start_row();
    global $path_to_root;
    $link = "<a href='$path_to_root/inventory/inquiry/daily_item_return_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Daily Item Returns" . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_daily_item_issues_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_daily_item_issues_summary($project_id, $from_date, $to_date, $tran_type);
    
    start_row();
    
    global $path_to_root;
    $link = "<a href='$path_to_root/inventory/inquiry/daily_item_issue_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . "Daily Item Issues" . "</a>";
//    $link = "<a href='$path_to_root/inventory/inquiry/material_issues_inquiry.php'  target=_blank>" . "Material Issues" . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_material_purchased_invoices_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = 0;
    
//    $result = get_material_purchased_invoices($tran_type, $project_id, $from_date, $to_date);
//    // Loop and get the SUM to avoid duplication due to single invoice for multiple POs
//    // Issue still there with comboned multiple invoices for multiple POs
//    while($inv = db_fetch($result))
//    {
//         $total += $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
//    }
    
    $total = get_material_purchased_invoices($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    global $path_to_root;
    $name = "Material Purchased Invoices";
    $link = "<a href='$path_to_root/purchasing/inquiry/supplier_inquiry.php?filterType=1"."&FromDate=".sql2date($from_date)."&ToDate=".sql2date($to_date)."' target=_blank>" .$name. "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_material_purchase_credit_notes_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = 0;
    
//    $result = get_material_purchased_credit_notes($tran_type, $project_id, $from_date, $to_date);
//    // Loop and get the SUM to avoid duplication due to single CN for multiple POs
//    // Issue still there with comboned multiple CNs for multiple POs
//    while($inv = db_fetch($result))
//    {
//         $total += $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
//    }
    
    $total = get_material_purchased_credit_notes($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    global $path_to_root;
    $name = "Material Purchased Credit Notes";
    $link = "<a href='$path_to_root/purchasing/inquiry/supplier_inquiry.php?filterType=4"."&FromDate=".sql2date($from_date)."&ToDate=".sql2date($to_date)."' target=_blank>" .$name. "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_subcontractor_invoices_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_subcontractor_invoices_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    
    global $path_to_root;
    $name = "Subcontractor Invoices";
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_invoice_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_subcontractor_retentions_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_subcontractor_invoices_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    
    global $path_to_root;
    $name = "Subcontractor Retentions";
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_invoice_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_subcontractor_credit_notes_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_subcontractor_credit_notes_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    global $path_to_root;
    $name = "Subcontractor Credit Notes";
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_credit_note_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_subcontractor_retention_recovered_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_subcontractor_credit_notes_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    global $path_to_root;
    $name = "Subcontractor Retention Recovered";
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_credit_note_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function display_transport_expenses_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $tran_type = ST_VEHICLE_RUNNING_CHART;
    
    $total = get_vehicle_running_chart_entries_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Vehicle Running Chart Entries");
    global $path_to_root;
    $link = "<a href='$path_to_root/transport/inquiry/vehicle_running_chart_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_project_cost_adjustments_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $tran_type = ST_PROJECT_COST_ADJUST;
    
    $total = get_project_cost_adjustments_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Project Cost Adjustments");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/project_cost_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&filterType=".ST_PROJECT_COST_ADJUST."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_project_overhead($project_id, &$project_total_commitment)
{
    
    $project = get_project_record($project_id);
    
//    if($project['approved_cost_budgets_total'] > 0){
//        $overhead_factor = $project['approved_overhead_budgets_total'] / $project['approved_cost_budgets_total'];
//    }
//    else{
//        $overhead_factor = 0;
//    }
    
    $overhead_factor = $project['overhead_percentage']/100;
            
    $overhead_perc = number_format($overhead_factor*100,2);
    
    $overhead = $project_total_commitment * $overhead_factor;

    start_row();
    $name = ("Project Overhead (".$overhead_perc."%)");
    label_cell($name);
//    label_cell($overhead, "align=right");
    amount_cell($overhead);
    label_cell('');
    end_row();
    
    $project_total_commitment += $overhead;
}


function display_petty_cash_payments_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $total = 0;
    $tran_type = ST_I_OWE_YOU_PAYMENT;
    $result = get_petty_cash_payments($project_id, $from_date, $to_date);
    while($inv = db_fetch($result))
    {
         $total += $inv["payment_amount"];
    }
    
    start_row();
    $name = ("Petty Cash Payments");
    global $path_to_root;
    $link = "<a href='$path_to_root/petty_cash/inquiry/petty_cash_payments_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function display_iou_bank_payments_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $tran_type = ST_IOU_BANKPAYMENT;
    $total = get_iou_bank_payments_summary($project_id, $from_date, $to_date, $tran_type);
    $total *= -1;
    
    start_row();
    $name = ("IOU Bank Payments");
    global $path_to_root;
    $link = "<a href='$path_to_root/petty_cash/inquiry/iou_bank_payments_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&trans_type=".$tran_type."' target=_blank>" . $name . "</a>";
    label_cell($link);
    
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function display_iou_bank_payment_returns_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $tran_type = ST_IOU_BANKPAYMENT_RETURN;
    $total = get_iou_bank_payments_summary($project_id, $from_date, $to_date, $tran_type);
    $total *= -1;
    
    start_row();
    $name = ("IOU Bank Payment Returns");
    global $path_to_root;
    $link = "<a href='$path_to_root/petty_cash/inquiry/iou_bank_payments_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&trans_type=".$tran_type."' target=_blank>" . $name . "</a>";
    label_cell($link);
    
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function display_iou_advances_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $tran_type =  ST_I_OWE_YOU_ADVANCES;
    
    $total = get_iou_advances_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("I Owe You Advances");
    global $path_to_root;
    $link = "<a href='$path_to_root/petty_cash/inquiry/i_owe_you_advances_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    
    
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_miscellaneous_project_costs_summary($project_id, $from_date, $to_date, &$project_total_commitment){
    
    $tran_type = ST_MISCELLANEOUS_PROJECT_COST;
    
    $total = get_miscellaneous_project_costs_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Miscellaneous Project Costs");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/miscellaneous_project_cost_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_monthly_project_costs_summary($project_id, $from_date, $to_date, &$project_total_commitment){
    
    $tran_type = ST_MONTHLY_PROJECT_COSTS;
    
    $total = get_monthly_project_costs_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Monthly Project Costs");
    global $path_to_root;
    $link = "<a href='$path_to_root/gl/inquiry/monthly_project_costs_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function display_wip_costs_summary($project_id, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_wip_costs_summary($project_id, $from_date, $to_date);
    
    start_row();
    label_cell("Project WIP Costs");

    $parameters = "project_id=".$project_id;
    $amount_link = get_data_view_str(DV_PROJECT_WIP_COSTS, $parameters, number_format($total,2));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_project_expenses_total_summary($project_total_commitment, $display_text = "Expenses Total")
{
    start_row();
    label_cell(text_bold($display_text), "align=right");
    amount_cell($project_total_commitment, true);
    label_cell('');
    end_row();
}


function display_utility_bills_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_utility_bills_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    
    global $path_to_root;
    $name = "Utility Bills";
    $link = "<a href='$path_to_root/gl/inquiry/utility_bills_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_utility_bill_credit_notes_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_utility_bill_credit_notes_summary($tran_type, $project_id, $from_date, $to_date);
    
    start_row();
    
    global $path_to_root;
    $name = "Utility Bill Credit Notes";
    $link = "<a href='$path_to_root/gl/inquiry/utility_bill_credit_notes_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_tool_usage_charges_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment)
{
    $total = get_tool_usage_charges_summary($project_id, $from_date, $to_date);
    
    start_row();
    
    global $path_to_root;
    $name = "Tool Usage Charges";
    $link = "<a href='$path_to_root/assets/inquiry/tool_usage_charges_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function  print_project_material_requisitions_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_allocations)
{
    $total = get_material_requisitions_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Material Requisition Allocations");
    global $path_to_root;
    $link = "<a href='$path_to_root/inventory/inquiry/material_requisitions_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    end_row();
    
    $project_total_allocations += $total;
}


function  print_project_purchase_requisitions_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_allocations)
{
    $total = get_purchase_requisitions_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Purchase Requisitions");
    global $path_to_root;
    $link = "<a href='$path_to_root/purchasing/inquiry/purchase_requisition_allocation_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    end_row();
    
    $project_total_allocations += $total;
}


function  print_project_subcontractor_purchase_orders_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_allocations)
{
    $total = get_subcontractor_purchase_orders_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Subcontractor Purchase Orders");
    global $path_to_root;
    $link = "<a href='$path_to_root/projects/inquiry/subcontractor_purchase_orders_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    end_row();
    
    $project_total_allocations += $total;
}


function  print_project_i_owe_yous_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_allocations)
{
    $total = get_i_owe_yous_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("I Owe Yous");
    global $path_to_root;
    $link = "<a href='$path_to_root/petty_cash/inquiry/i_owe_you_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    end_row();
    
    $project_total_allocations += $total;
}

function  print_project_vehicle_requests_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_allocations)
{
    $total = get_vehicle_requests_summary($project_id, $from_date, $to_date);
    
    start_row();
    $name = ("Vehicle Requests");
    global $path_to_root;
    $link = "<a href='$path_to_root/transport/inquiry/vehicle_requests_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    end_row();
    
    $project_total_allocations += $total;
}


function display_site_material_return_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment){
    $total = get_site_material_return_summary($project_id, $from_date, $to_date);
    $total = $total * (-1);
    start_row();
    global $path_to_root, $systypes_array;
    $name = "Site Material Returns";
    $link = "<a href='$path_to_root/inventory/inquiry/site_material_returns_inquiry.php?project_id=$project_id"."&FromDate=".sql2date($from_date)."&ToDate=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}
function display_purchase_requisition_issues_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment){
    $total = get_purchase_requisition_issues_summary($project_id, $from_date, $to_date);
//    $total = $total * (-1);
    start_row();
    global $path_to_root, $systypes_array;
    $name = "Direct Purchases (".$systypes_array[$tran_type].")";
    $link = "<a href='$path_to_root/purchasing/inquiry/pr_issues_inquiry.php?project_id=$project_id"."&FromDate=".sql2date($from_date)."&ToDate=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}

function display_purchase_requisition_returns_summary($project_id, $tran_type, $from_date, $to_date, &$project_total_commitment){
    $total = get_purchase_requisition_returns_summary($project_id, $from_date, $to_date);
    $total = $total * (-1);
    start_row();
    global $path_to_root, $systypes_array;
    $name = "Direct Purchase Returns (".$systypes_array[$tran_type]."s)";
    $link = "<a href='$path_to_root/purchasing/inquiry/pr_returns_inquiry.php?project_id=$project_id"."&FromDate=".sql2date($from_date)."&ToDate=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
    label_cell($link);
    $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));
    label_cell($amount_link, "align=right");
    label_cell('');
    end_row();
    
    $project_total_commitment += $total;
}


function display_project_billing_details($project_id)
{
    global $systypes_array;
    
    display_heading2("Billing Schedule");
    
//    br(1);    
    $bs_result = get_project_billing_schedule($project_id);
    
    if(db_num_rows($bs_result) == 0){
        display_note("No billing schedule found");
        br(1);
        return;
    }

    start_table(TABLESTYLE, "width=90%");

    $th = array(_("Type"), _("Date"),_("Percentage %")
                    , _("Amount"), _("Comment"), 
                    _("Type"), _("Inv/CN No"), "Inv/CN Date",
                    "Amount", "Allocated", "Ourstanding");

    table_header($th);

    $k = 0; //row colour counter


    $billing_total = $invoices_total = $paid_total = $outstanding_total = 0;  

    while ($myrow = db_fetch($bs_result))
    {
//        alt_table_row_color($k);

        label_cell($myrow["billing_type_name"]);
        label_cell(sql2date($myrow["date"]), 'align=center');
        amount_cell($myrow["percentage"]);
        amount_cell($myrow["amount"]); 
        $billing_total += $myrow["amount"];

        label_cell($myrow["comment"]);

        $invoice_amount = $paid_amount = $outstanding_amount = 0;  
        $invoice_date = $link = $type = "";
        
        $invoice_no = '';
        if ($myrow['invoice_no'] > 0) 
        {
            $invoice_no = $myrow['invoice_no'];
            $invoice = get_customer_trans_record(ST_PROJECT_INVOICE, $invoice_no);
            if(isset($invoice['trans_no']) && $invoice['trans_no'] > 0 ){
                $invoice_amount = $invoice['ov_amount'] + $invoice['ov_gst'] + $invoice['ov_freight'] + $invoice['ov_freight_tax'] - $invoice['ov_discount'];
                $paid_amount = $invoice['alloc'];
                $outstanding_amount = $invoice_amount - $paid_amount; 
                $invoice_date = sql2date($invoice['tran_date']);
                
                $link = get_trans_view_str(ST_PROJECT_INVOICE, $invoice_no);
                $type = $systypes_array[ST_PROJECT_INVOICE];
            }

        }
        

        $credit_note_no = '';
        if ($myrow['credit_note_no'] > 0) 
        {
            $credit_note_no = $myrow['credit_note_no'];
            $invoice = get_customer_trans_record(ST_PROJECT_CREDIT_NOTE, $credit_note_no);
            if(isset($invoice['trans_no']) && $invoice['trans_no'] > 0 ){
                $invoice_amount -= $invoice['ov_amount'] + $invoice['ov_gst'] + $invoice['ov_freight'] + $invoice['ov_freight_tax'] - $invoice['ov_discount'];
                $paid_amount = -$invoice['alloc'];
                $outstanding_amount = $invoice_amount - $paid_amount;  
                $invoice_date = sql2date($invoice['tran_date']);
                
                $link = get_trans_view_str(ST_PROJECT_CREDIT_NOTE, $credit_note_no);
                $type = $systypes_array[ST_PROJECT_CREDIT_NOTE];
            }

        }
        

        label_cell($type); 
        label_cell($link, 'align=center'); 

        if ($myrow['invoice_no'] > 0 || $myrow['credit_note_no'] > 0){
            label_cell($invoice_date, "align=center");
            amount_cell($invoice_amount);
            amount_cell($paid_amount);
            amount_cell($outstanding_amount);
        }
        else{
            label_cell('');
            label_cell('');
            label_cell('');
            label_cell('');
        }

        end_row();

        $invoices_total += $invoice_amount;
        $paid_total += $paid_amount;
        $outstanding_total += $outstanding_amount;  

    }

    start_row();
        label_cell('Total', "align=right colspan=3");
        amount_cell($billing_total, true);
        label_cell('', "colspan=4");
        amount_cell($invoices_total, true);
        amount_cell($paid_total, true);
        amount_cell($outstanding_total, true);
//        label_cell('', "colspan=2");
    end_row();


    $project = get_project_record($project_id);
    start_row();
        label_cell('Project Value', "align=right colspan=3");
        amount_cell($project['project_value'], true);
        label_cell('', "colspan=7");
    end_row();
    start_row();
        label_cell('Balance', "align=right colspan=3");
        $balance = ($project['project_value'] - $billing_total);
        if($balance != 0){
            label_cell('<font color="red">'. text_bold(number_format($balance, 2)).'</font>', "align=right");
        }
        else{
            label_cell('');
        }
        label_cell('', "colspan=7");
    end_row();
    

    end_table();
}



function display_project_expenses_schedule_details($project_id, $width=45){
    
    display_heading2("Expenses Schedule");
    
//    br(1);    
    $project_expenses = get_project_expenses_schedule($project_id);
    
    if(db_num_rows($project_expenses) == 0){
        display_note("No expenses schedule found");
        br(1);
        return;
    }
    
    
    start_table(TABLESTYLE, "width=$width%");
        $th = array(_("Month"), _("Value"), "Comment");
        table_header($th);
        $k = 0; //row colour counter
        $total = 0;
        
        while($myrow = db_fetch($project_expenses)){
//            alt_table_row_color($k);
//                label_cell($myrow["pxs_id"], "align=center");
                $mid = $myrow["month_id"];
                label_cell($myrow["month_name"]);

                amount_cell($myrow["value"]);
                $total += $myrow["value"];
                
                label_cell($myrow["comment"]);
                
            end_row();
        }
        start_row();
            label_cell("Total", "align=right colspan=1");
            amount_cell($total, "align=right colspan=1");  
            label_cell("");
        end_row();
        start_row();
            $project_total = get_project_record($project_id);
            label_cell("Project Total", "align=right colspan=1");
            $pro_tot = $project_total['approved_cost_budgets_total'] + $project_total['approved_overhead_budgets_total'];
            amount_cell($project_total['approved_cost_budgets_total'] + $project_total['approved_overhead_budgets_total'], "align=right colspan=1"); 
            label_cell("");
        end_row();
            label_cell("Balance", "align=right colspan=1");
            $balance = $pro_tot - $total;
            if($balance != 0){
                label_cell('<font color="red">'. text_bold(number_format($balance, 2)).'</font>', "align=right");
            }
            else{
                label_cell('');
            }
            label_cell("");
//            amount_cell($pro_tot - $total, "align=right colspan=1");            
        end_row();
    end_table();
}

function display_project_collections($project_id, $from_date, $to_date)
{
    display_heading2("Collections");
//    br(1);
    
    start_table(TABLESTYLE, "width=100%");
    $th = array(_("Details"), _("Amount"));
      
    table_header($th);
    
    $inv_total = get_project_invoices_certified_summary_with_taxes(ST_PROJECT_INVOICE, $project_id, $from_date, $to_date);
    $cn_total = get_project_credit_notes_certified_summary_with_taxes(ST_PROJECT_CREDIT_NOTE, $project_id, $from_date, $to_date);

    
//    $_POST['INVOICES_TOTAL'] = $project_total_revenue;
    
    $invoices_total = $inv_total - $cn_total;
    start_row();
    label_cell('Approved Invoices Total');
    amount_cell($invoices_total);
    end_row();
    
    start_row();
    $name = ("Collections Total");
//    global $path_to_root;
//    $link = "<a href='$path_to_root/inventory/inquiry/material_requisitions_inquiry.php?project_id=".$project_id."&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."' target=_blank>" . $name . "</a>";
//    label_cell($link);
    
    label_cell($name);
    
    
    $invoices_collection = get_project_invoices_collection_summary($project_id, $from_date, $to_date);
    $credit_notes_payments = get_project_credit_notes_payments_summary($project_id, $from_date, $to_date);
    $total = $invoices_collection - $credit_notes_payments;
    $parameters = "project_id=".$project_id."&tran_type=COLLECTIONS&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));
    label_cell($amount_link, "align=right");
    end_row();
    
    
    $balance = $invoices_total - $total;
    $balance = $balance >= 0 ?  $balance : 0;
    start_row();
    label_cell('Balance');
    amount_cell($balance);
    end_row();
    
    end_table();
    

}


function display_project_petty_cash_expenses($project_id, $from_date, $to_date) {
    display_heading2("Petty Cash Expenses");
    
    start_table(TABLESTYLE, "width=100%");
        $th = array(_("Details"), _("Amount"));
        table_header($th);
            $ap_pc_exp = get_project_petty_cash_expense_summary($project_id, $from_date, $to_date, array(AS_APPROVED));
            start_row();
                label_cell('Approved Petty Cash Expenses');
                $parameters = "project_id=".$project_id."&tran_type=PCE_AP&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&approved=1";
                $approved_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($ap_pc_exp, 2));
                label_cell($approved_link, "align=right");
            end_row();
            $approval_status_array = array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR);
            $unap_pc_exp = get_project_petty_cash_expense_summary($project_id, $from_date, $to_date, $approval_status_array);
            start_row();
                label_cell('Unapproved Petty Cash Expenses');
                $parameters = "project_id=".$project_id."&tran_type=PCE_UAP&from_date=".sql2date($from_date)."&to_date=".sql2date($to_date)."&approved=0";
                $unapproved_link = get_data_view_str(DV_PROJECT_TRANSACTION_TYPE_TRANSACTIONS, $parameters, number_format($unap_pc_exp, 2));
                label_cell($unapproved_link, "align=right");
            end_row();
    end_table();
}