<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_UOM';
$path_to_root = "..";
//include($path_to_root . "/includes/session.inc");

//page(_($help_context = "Email Template"));

include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/payroll/includes/db/nopay_references_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if (isset($_GET['work_order_id']) && $_GET['work_order_id'] > 0) 
{
    $workorder_id = $_GET['work_order_id'];
}


function get_selected_id(&$selected_id, &$Mode)
{
    foreach( $_POST as $key => $post_var ) {
        if(!is_array( $post_var ) ) {
            $e = substr($key,0,13);
            if($e == "EditMaterial_"){
                $selected_id = $key;
                $Mode = "Edit";
                return ;
            }
            if(isset($_POST['Add_WO_Material'])){
                $selected_id = -1;
                $Mode = "Add_WO_Material";
                return ;
            }
            $d = substr($key,0,15);
            if($d == 'DeleteMaterial_'){
                $selected_id = substr($key,15);
                $Mode = "Delete";
                return ;
            }
        } 
    }
    
    return -1;
    //$Mode = 'ADD_ITEM';
}

get_selected_id($selected_id, $Mode);


if (isset($_GET['wo_material_parent']) && $_GET['wo_material_parent'] > 0) 
{
    $wo_material_parent = $_GET['wo_material'];
}


function display_wo_process_name($wo_material_process)
{
    start_row();
    $name = $wo_material_process["sequence"]." - ".$wo_material_process["workcentre_name"]." - ".$wo_material_process["description"];
    label_cell("<b>".$name."</b>", "colspan=8 align=center");
    end_row();
}



function display_wo_process_material($wo_id, $process_id)
{
    $result = get_wo_process_material($process_id);
	
	$k = 0;
	while ($myrow = db_fetch($result))
	{

		alt_table_row_color($k);

		label_cell($myrow["stock_id"]);
		label_cell($myrow["description"]);
        label_cell($myrow["location_name"]);
        label_cell($myrow["req_unit"]);
        qty_cell($myrow["req_unit_qty"], false, get_qty_dec($myrow["stock_id"]));
        qty_cell($myrow["units_issued"] / $myrow["req_unit_conversion_factor"], false, get_qty_dec($myrow["stock_id"]));
        
 		edit_button_cell("Edit"."Material_".$myrow['id'], _("Edit"));
 		delete_button_cell("Delete"."Material_".$myrow['id'], _("Delete"));
        end_row();

	} //END WHILE LIST LOOP

}

function display_wo_material($wo_id)
{
    global $Ajax;
    
    $result = get_wo_processes($wo_id);
    $Ajax->activate('wo_material');
    
    div_start('wo_material');
	start_table(TABLESTYLE, "width=60%");
	$th = array(_("Process"), _("Component"), _("Location"),
		_("Unit"), _("Quantity"),  _("Qty Issued"), '','');
	table_header($th);

	$k = 0;
	while ($myrow = db_fetch($result))
	{
            display_wo_process_name($myrow);
    
            display_wo_process_material($wo_id, $myrow['id']);
        
	} 
    end_table();
	div_end();
}

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

function display_wo_material_edit_controls($workorder_id, &$Mode, $selected_id)
{
    global $Ajax;
    $Ajax->activate('wo_material_controls');
    
    div_start('wo_material_controls');
    start_table(TABLESTYLE2);
    
    //$selected_id = substr($selected_id,13);
    if($selected_id == -1){
        $id = -1;
    }
    else{
        $id = substr($selected_id,13);
    }
            
    if ($id != -1)
	{
            //editing a selected component from the link to the line item
            $myrow = get_component_from_wo_material($id);

            $_POST['wo_material_process_id'] = $myrow["process_id"];
            $_POST['wo_material_loc_code'] = $myrow["loc_code"];
            $_POST['wo_material_stock_id'] = $myrow["stock_id"]; // by Tom Moulton
            //$_POST['bom_material_workcentre_id']  = $myrow["workcentre_added"];
            $_POST['wo_material_tran_unit']  = $myrow["req_unit"];
            $_POST['wo_material_tran_qty'] = number_format2($myrow["req_unit_qty"], get_qty_dec($myrow["stock_id"]));
            $_POST['wo_material_comment']  = $myrow["comment"];
            
        
        hidden('selected_id', $selected_id);
	}
	
    br(2);
    
    workorder_processes_list_row('Process', 'wo_material_process_id', $workorder_id);

    if($selected_id != -1 && isset($_POST['wo_material_stock_id'])){
        $item = get_stock_item_edit_info($_POST['wo_material_stock_id']);
        label_row("Component:", $_POST['wo_material_stock_id']." - ".$item['description']);
        hidden('wo_material_stock_id', $_POST['wo_material_stock_id']);
    }
    elseif($selected_id == -1 ){
        $Ajax->activate('wo_material_stock_id');
        start_row();
        label_cell(_("Component:"), "class='label'");

        echo "<td>";
//        if(isset($_POST['wo_material_stock_id'])){
//            echo stock_component_items_list('wo_material_stock_id', '', $_POST['wo_material_stock_id'], false, true);
//
//        }
//        else{
//            echo stock_component_items_list('wo_material_stock_id', $workorder_id, null, false, true);

            echo stock_items_list('wo_material_stock_id', null, false, true);
//        }
        echo "</td>";
        end_row();
    }
    
    
    if (list_updated('wo_material_component')) 
    {
        $Ajax->activate('wo_material_tran_qty');
        $Ajax->activate('wo_material_tran_unit');
    }
    
	
	hidden('stock_id', $workorder_id);

	locations_list_row(_("Location to Draw From:"), 'wo_material_loc_code', null, false, false);
    if(isset($_POST['wo_material_stock_id'])){
        units_list_row("Unit", 'wo_material_tran_unit', $_POST['wo_material_stock_id']);
    }
    else{
        units_list_row("Unit", 'wo_material_tran_unit', '');
    }
	//workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
	$dec = get_qty_dec(get_post('wo_material_stock_id'));
	$_POST['wo_material_tran_qty'] = number_format2(input_num('wo_material_tran_qty',1), $dec);
	qty_row(_("Quantity:"), 'wo_material_tran_qty', null, null, null, $dec);
    textarea_row(_("Comment:"), "wo_material_comment", null, 31, 3);
    
	end_table(1);
	
    
if ($id > 0) {
        br();
        submit_center_first('Update_WO_Material', _("Update"), '', 'default');
        submit_center_last('Delete_WO_Material', _("Cancel"), _("Cancel Edition"), '');
    }
    else
    {
        br();
        submit_center_first('Add_WO_Material', _("Add"), '', 'default');
    }
    div_end();
}

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




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

function build_workorder_material_array(&$wom)
{
    
    $wom['wo_material_process_id'] = get_post('wo_material_process_id');
    $wom['wo_material_stock_id'] = get_post('wo_material_stock_id');
    $wom['wo_material_loc_code'] = $_POST['wo_material_loc_code'];
    $wom['wo_material_tran_unit'] = $_POST['wo_material_tran_unit'];
    $wom['wo_material_tran_qty'] = input_num('wo_material_tran_qty');
    $wom['wo_material_comment'] = $_POST['wo_material_comment'];
    $wom['wo_material_cf'] = 1;
    $wom['user_id']  = $_SESSION['wa_current_user']->user;
   

}

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

function save_workorder_material($wom, $selected_id){
   // return;
    
    $id = substr($selected_id,13);
    
    $ccwom = can_commit_wo_material($wom, $id);
    if($ccwom['success'] !== true){
        return $ccwom;
    }
    else{
        $swom = save_wo_material($wom, $id);
        return $swom;
    }
    
}

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

function delete_workorder_material($selected_id){
//    $id = substr($selected_id,13);
    $id = $selected_id;
    $ccwol = can_delete_wo_material($id);
    if($ccwol['success'] !== true){
        return $ccwol;
    }
    else{
        delete_wo_material($id);
        return array("success" => true);
    }
}

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

//--------------------------------------------------------------------------------------------------
if ($Mode=='Add_WO_Material') 
{
    build_workorder_material_array($wom);
    $awom = save_workorder_material($wom, -1);   
    if($awom['success'] === true){
        $Mode = '';
        $selected_id = -1;
        display_notification("Workorder material has been added");
    }
    else{
        display_error($awom['message']);
        if(isset($awom['focus_to'])){
            set_focus($awom['focus_to']);
        }
    }
}


if(isset($_POST['Update_WO_Material'])) 
{
    build_workorder_material_array($wom);
    $uwom = save_workorder_material($wom, $_POST['selected_id']);  
    if($uwom['success'] === true){
        $Mode = '';
        $selected_id = -1;
        unset($_POST['selected_id']);
    }
    else{
        display_error($uwom['message']);
        if(isset($uwom['focus_to'])){
            set_focus($uwom['focus_to']);
        }
    }
}


if ($Mode == 'Delete')
{
        //$id = substr($selected_id,9);
        $dwom = delete_workorder_material($selected_id);
        if($dwom['success'] !== true){
            display_error($dwom['message']);
        }
        else{
            display_notification(_('Selected item has been deleted'));
            $Mode = 'RESET';
        }
	
}


if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

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

global $Ajax;

start_form();

display_wo_material($workorder_id);

$Ajax->Activate('wo_material_controls');

if(substr($selected_id,0,13) == "EditMaterial_"){
    display_wo_material_edit_controls($workorder_id, $Mode, $selected_id);
}
else{
    display_wo_material_edit_controls($workorder_id, $Mode, -1);
}
    


end_form();

//end_page();

?>
