<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_EXPENSES_SCHEDULE';	
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/projects/includes/db/projects_expenses_schedule_db.inc");
include_once($path_to_root . "/dashboard/includes/db/dashboard_db.inc");
include_once($path_to_root . "/projects/includes/ui/projects_expenses_schedule_ui.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");

$js = '';

if($use_popup_windows){
    $js .= get_js_open_window(900, 500);
}

if($use_date_picker){
    $js .= get_js_date_picker();
}

page($help_context = "Project Expenses Schedule", false, false, "", $js);

check_db_has_projects(_("There are no projects defined in the system."));

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------

$input_error = 0;
global $Ajax;

if(isset($_GET['project_id'])){
    $_POST['project_id'] = $_GET['project_id'];
}

//---------------------------------------------------------------------------------------------------
start_form();
br(2);

if(isset($_POST['update']) && can_process_project_expenses_schedule()){
    
    begin_transaction();
        $msg = delete_selected_project_expenses_schedule($_POST['project_id'], $_POST['from_month_id'] ,$_POST['to_month_id']);
        if($msg['success'] !== true){
            display_error($msg['message']);
            return false;
        }

        $result = get_project_expenses_schedule_month_range($_POST['project_id'], $_POST['from_month_id'] ,$_POST['to_month_id']);
        $error = 0;
        while($row = db_fetch($result)){
            $suffix = $row["month_id"]."_".$_POST['project_id'];
            $msg = add_or_update_projects_expenses_schedule_detail($_POST['project_id'], $row['month_id'], input_num('value_'.$suffix),
                                                                    $_POST["comment_".$suffix], $row['pxs_id']);
            if($msg['success'] !== true){
                $error = 1;
                display_error($msg['message']);
                break;
            }
        }
        
        if($error > 0){
            return false;
        }

    commit_transaction();
    $project_name = get_project_record($_POST['project_id']);
    $msg = _($project_name['description']." project expenses schedule has been added.");
    display_notification($msg);
}

//--------------------------------------------------------------------------------------------------------

start_table(TABLESTYLE_NOBORDER);
    start_row();
        project_list_cells(_("Select a project:"), 'project_id', null, _('Select a project'), true, check_value('show_inactive'));
    end_row();
    end_table();
    
    if(list_updated('project_id')){
        $min_max_month = get_project_expenses_schedule_min_and_max_maonths($_POST['project_id']);
        if($min_max_month['min_month_id'] > 0){
            $_POST['from_month_id'] = $min_max_month['min_month_id'];
        }
        if($min_max_month['max_month_id'] > 0){
            $_POST['to_month_id'] = $min_max_month['max_month_id'];
        }
    }
    
start_table(TABLESTYLE_NOBORDER);
    $Ajax->activate('from_month_id');
    $Ajax->activate('to_month_id');
    start_row();
        months_list_cells("From Month:", 'from_month_id', null, false, true);    
        months_list_cells("To Month:", 'to_month_id', null, false, true);
    end_row();
end_table(1);


$Ajax->activate('price_table');
div_start('price_table');
    display_project_expenses_schedule_details($_POST['project_id'], $_POST['from_month_id'], $_POST['to_month_id']);
div_end();

//------------------------------------------------------------------------------------------------

    br();

    if(@$_GET['popup']){
        hidden('_tabs_sel', get_post('_tabs_sel'));
        hidden('popup', @$_GET['popup']);
    }

    div_start('price_details');
        submit_center_first("update", "Update", false, false, ICON_SUBMIT);
        submit_center_last("calculate", "Calculate", false, false, ICON_SUBMIT);
    div_end();
    br();

end_form();
end_page(@$_GET['popup'], false, false)

?>