<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_WIP_COSTS';
if (!@$_GET['popup'])
	$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.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/project_wip_db.inc");

page(_($help_context = "Project WIP Costs"));

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

if (!@$_GET['popup'])
	start_form();


if (!@$_GET['popup'])
{
//	echo "<center>" . _("Item:"). "&nbsp;";
//	echo stock_costable_items_list('stock_id', $_POST['stock_id'], false, true);
//
//	echo "<hr></center>";
}
else
	br(2);
div_start('show_heading');

br();
div_end();




function can_process_project_pl($year_code, $month_code, $department_id)
{
    $result = get_project_pl_report_wip_entries($year_code, $month_code, $department_id);
    
    while($row = db_fetch($result)){
        
        $value = input_num($row['description']."_".$row['wip_type_name']."_".'value');
               
        if(!check_num2($value, 0)){
            display_error($row['description']." - ".$row['wip_type_name'].": Value cannot be negative.");
            set_focus($row['description']."_".$row['wip_type_name']."_".'value');
            return false;
        }       
       
    }
     return true;
}




function save_project_pl_wip_entries($year_code, $month_code, $department_id)
{
    delete_project_pl_report_wip_entry($year_code, $month_code, $department_id);
      
    $result = get_project_pl_report_wip_entries($year_code, $month_code, $department_id);


    while($myrow = db_fetch($result))
    {       
        $value = input_num($myrow['description']."_".$myrow['wip_type_name']."_".'value');
        
        if($value > 0){
            add_project_pl_report_wip_entry($_POST['year_code'], $_POST['month_code'], 
                        $myrow['project_id'], $myrow['wip_type_id'], 
                                        $value);            
        }
    }
    
    $msg = "Entered project WIP details have been saved.";
    display_notification($msg);
    
}


function display_project_pl_wip_entries($year_code, $month_code, $department_id){
    
    start_table(TABLESTYLE, "width=50%");

    $th = array(_("WIP Type"), _("Value"));
    table_header($th);

    $j = 1;
    $k=0; //row colour counter

    $result = get_project_pl_report_wip_entries($year_code, $month_code, $department_id);

    $previous_project_id = $project_wip_total = 0;
    
    while($myrow = db_fetch($result))
    {      
        if($previous_project_id != $myrow['project_id'] && $myrow['project_id'] > 0){
            if($previous_project_id > 0){
                start_row();
                label_cell("Total",'align=right');
                amount_cell($project_wip_total);
                $project_wip_total = 0;
                end_row();
            }
            
            start_row();
            label_cell(nl2br("<b>".$myrow['description']." (".$myrow['project_id'].")</b>\n\r".$myrow['debtor_ref']." - ".$myrow['customer_name']), "colspan=2");
            end_row();
//            start_row();
//            label_cell($myrow['debtor_ref']." - ".$myrow['customer_name'], "colspan=2");
//            end_row();
            
        }
        alt_table_row_color($k);
//        label_cell($myrow['description']);
        label_cell(spaces(10).$myrow['wip_type_name']);
        
       $_POST[$myrow['description']."_".$myrow['wip_type_name']."_".'value'] = $myrow['value'];
        amount_cells(null, $myrow['description']."_".$myrow['wip_type_name']."_".'value');
        $value = input_num($myrow['description']."_".$myrow['wip_type_name']."_".'value');

        $project_wip_total += $myrow['value'];
        

        $previous_project_id = $myrow['project_id'];
    }
    
    start_row();
            label_cell("Total",'align=right');
            amount_cell($project_wip_total);
            $project_wip_total = 0;
            end_row();

    end_table(1);
}



if(isset($_POST['UpdateData'])){
    if(can_process_project_pl($_POST['year_code'], $_POST['month_code'], $_POST['department_id'])){
       save_project_pl_wip_entries($_POST['year_code'], $_POST['month_code'], $_POST['department_id']);
    } 
}

start_table();
value_list_code_row(_("Year:"), 'year_code', 'YEARS', null, "Select a year", true);  
value_list_code_row(_("Month:"), 'month_code', 'MONTHS', null, "Select a month", true);  
value_list_row(_("Department:"), 'department_id', 'DEPARTMENTS', null, "Select a Department", true); 
end_table();
br();
br();
   
div_start('reorders');

display_project_pl_wip_entries($_POST['year_code'], $_POST['month_code'], $_POST['department_id']);
div_end();


submit_center('UpdateData', _("Update"), true, false, 'default');

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}


?>
