<?php

function display_project_expenses_schedule_details($project_id, $from_month_id, $to_month_id){
    start_table(TABLESTYLE, "width=50%");
        $th = array(_("Id"), _("Month"), _("Value"), "Comment");
        table_header($th);
        $k = 0; //row colour counter
        $total = 0;
        $project_expenses = get_project_expenses_schedule_month_range($project_id, $from_month_id, $to_month_id);
        
        while($myrow = db_fetch($project_expenses)){
            $suffix = $myrow["month_id"]."_".$_POST['project_id'];
            alt_table_row_color($k);
                label_cell($myrow["pxs_id"], "align=center");
                $mid = $myrow["month_id"];
                label_cell($myrow["month_name"]);
                
                $vv = $myrow["value"];
                if(!isset($_POST["value_".$suffix])){
                    $_POST["value_".$suffix] = $myrow["value"];
                }
                
                if(list_updated("project_id")){
                    $_POST["value_".$suffix] = $myrow["value"];
                }
                $mval = $_POST["value_".$suffix];
                amount_cells("", "value_".$suffix, null, null, null, 2);
                $total += input_num("value_".$suffix);
                
                if(!isset($_POST["comment_".$suffix])){
                    $_POST["comment_".$suffix] = $myrow["comment"];
                }
                text_cells(null, "comment_".$suffix, null, 50, 2000);
            end_row();
        }
        start_row();
            label_cell("Total", "align=right colspan=2");
            amount_cell($total, "align=right colspan=1");            
        end_row();
        start_row();
            label_cell("Project Total", "align=right colspan=2");
            
            $pro_tot = 0;
            $project_total = get_project_record($project_id);
            if (isset($project_total['project_id']) && $project_total['project_id'] > 0) {
                $pro_tot = $project_total['approved_cost_budgets_total'] + $project_total['approved_overhead_budgets_total'];
            }
            amount_cell($pro_tot, "align=right colspan=1");            
        end_row();
            label_cell("Balance", "align=right colspan=2");
            $balance = $pro_tot - $total;
            if($balance != 0){
                label_cell('<font color="red">'. text_bold(number_format($balance, 2)).'</font>', "align=right");
            }
            else{
                label_cell('');
            }        
        end_row();
    end_table();
}

function clear_project_expenses_schedule_data(){
    unset($_POST['long_description']);
}

function can_process_project_expenses_schedule(){    
    if($_POST['from_month_id'] > $_POST['to_month_id']){  
        display_error( _("From month should be earlier than to month."));
        set_focus('from_month_id');
        return false;
    }
    if(!($_POST['project_id'] > 0)){
        display_error(_("Project can not be empty."));
        set_focus('project_id');
        return false;
    } 

    return true;
}

function delete_selected_project_expenses_schedule($project_id, $from_month_id ,$to_month_id){
    $months = get_months_range($from_month_id ,$to_month_id);
    $in_string = null;
    while($month = db_fetch($months)){
        $in_string .= $month['id'].',';
    }
    
    if(strlen($in_string) > 0){
        $edited_in_string = substr($in_string, 0 , -1);
        return delete_project_expenses_schedule($project_id, $edited_in_string);
    }else{
        return array("success" => true, "message" => "");
    }
}

?>