<?php
/**********************************************************************
***********************************************************************/
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/ui/items_cart.inc");

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

function add_to_issue(&$order, $new_item, $new_item_qty, $tran_unit, $tran_qty, $convertion_factor)
{
//    if ($order->find_cart_item($new_item))
//         display_error(_("For Part :") . $new_item . " " . "This item is already on this issue.  You can change the quantity issued of the existing line if necessary.");
//    else
        $order->add_to_cart (count($order->line_items), $new_item, $new_item_qty, null, null, null, null,
                 null, 0, 0, 0, 0, 0, null, 0,null, null,null, null, null,$tran_unit, $tran_qty, 
                 $convertion_factor);
}
//---------------------------------------------------------------------------------

function display_wo_pr_items($title, &$order)
{
	global $path_to_root;

	display_heading($title);
    div_start('items_table');
	start_table(TABLESTYLE, "width=100% colspan=7");
	$th = array(_("Process"), _("Item Code"), _("Item Description"), 
                _("WO Unit"), _("WO Qty"), _("Iss. Location"),_("Qty Issued"),
                _("Qty. Ordered"),
                _("Ordering Qty"),  'Required Date', "");
	//if ( count($order->line_items)) $th[] = '';

	table_header($th);
//	$total = 0;
	$k = 0;  //row colour counter

    $on_going_edit_id = find_submit('OnGoingEdit');
	$id = find_submit('Edit');
    
    if(list_updated('tran_unit')){
        $id = $on_going_edit_id;
        $line_no = $id;
    }
    
	foreach ($order->line_items as $line_no=>$stock_item)
	{

//		$total += ($stock_item->standard_cost * $stock_item->quantity);

		if ($id != $line_no)
		{
    		alt_table_row_color($k);

            label_cell($stock_item->process);
            
			view_stock_status_cell($stock_item->stock_id);
			label_cell($stock_item->item_description);
//    		qty_cell($stock_item->quantity, false, get_qty_dec($stock_item->stock_id));
//			label_cell($stock_item->units);
            
            $dec = get_qty_dec($stock_item->stock_id);
            

                label_cell($stock_item->req_unit);
                //label_cell($stock_item->req_unit_qty);
                qty_cell($stock_item->req_unit_qty, false, $dec);
                
                label_cell($stock_item->location_name);
                
                //label_cell($stock_item->units_issued/ $stock_item->req_unit_conversion_factor);
                qty_cell($stock_item->units_issued/ $stock_item->req_unit_conversion_factor, false, $dec);


            
            
            
//            if($stock_item->tran_unit == ""){
//                label_cell($stock_item->units);
//            }
//            else{
//                label_cell($stock_item->tran_unit);
//            }
            
            
            //qty_cell($stock_item->tran_qty, false, $dec);
            
            //qty_cell($stock_item->qty_ordered, false, $dec);
            label_cell(get_manufacturing_trans_view_str(ST_WO_MATERIAL_PR, $stock_item->row_id, number_format($stock_item->qty_ordered,2)), "align=right");
            
            if(!isset($_POST[$stock_item->row_id."_item_required_date"])){
                $_POST[$stock_item->row_id."_item_required_date"] = $stock_item->required_date;
            }

           
            if(isset($_POST[$stock_item->row_id."_qty"])){
                $suggested_issu_qty = $_POST[$stock_item->row_id."_qty"];
            }
            else{
                $suggested_issu_qty = $stock_item->req_unit_qty - $stock_item->qty_ordered;
            }
            if($suggested_issu_qty < 0){
                $suggested_issu_qty = 0;
            }
            
            qty_cells(null, $stock_item->row_id."_qty", $suggested_issu_qty, null, null, $dec);
            
            date_cells(null, $stock_item->row_id."_item_required_date");
            
            
            check_cells(null, $stock_item->row_id.'_check_wo_pr_item');
            
//            
//			edit_button_cell("Edit$line_no", _("Edit"),
//				_('Edit document line'));
//			delete_button_cell("Delete$line_no", _("Delete"),
//				_('Remove line from document'));
			end_row();
		}
		else
		{
//			issue_edit_item_controls($order, $line_no);
		}
	}

//	if ($id == -1)
//		issue_edit_item_controls($order);

//	label_row(_("Total"), number_format2($total,user_price_dec()), "colspan=5", "align=right");

    end_table();
    
	div_end();
}

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

//function issue_edit_item_controls(&$order, $line_no=-1)
//{
//	global $Ajax;
//	start_row();
//
//    $_POST['tran_qty'] = 0;
//    
//
//    $qty = 0;
//    $units = '';
//    $std_cost = 0;
//    
//    $on_going_edit_id = find_submit('OnGoingEdit');
//	$id = find_submit('Edit');
//    
//    if(list_updated('tran_unit')){
//        $id = $on_going_edit_id;
//        $line_no = $id;
//    }
//    
//    if($on_going_edit_id != $id){
//        unset($_POST['tran_unit']);
//        unset($_POST['loc_code']);
//    }
//    
////    elseif($on_going_edit_id != $id && $id == -1){
////        $id = $on_going_edit_id;
////    }
//    
//	if ($line_no != -1 && $line_no == $id){
//        $dec = get_qty_dec($order->line_items[$id]->stock_id);
//        $_POST['process_id'] = $order->line_items[$id]->process_id;
//		$_POST['stock_id'] = $order->line_items[$id]->stock_id;
//		$_POST['qty'] = qty_format($order->line_items[$id]->quantity, $order->line_items[$id]->stock_id, $dec);
//        
//        if(!isset($_POST['tran_unit']) || $_POST['tran_unit'] == ""){
//            $_POST['tran_unit'] = $order->line_items[$id]->tran_unit;
//        }
//        
//
//        if(list_updated('tran_unit')){
//            $tran_unit = $_POST['tran_unit'];
//            $old_base_unit_qty = $order->line_items[$id]->quantity;
//            get_convertion_factor2($_POST['stock_id'], $tran_unit, $cf);
//            $_POST['tran_unit'] = $tran_unit;
//            $_POST['cf'] = $cf;
//            
//            $_POST['tran_qty'] = $old_base_unit_qty / $cf;
//        }
//        else{
//            $_POST['tran_qty'] = $order->line_items[$id]->tran_qty;
//        }
//        
//		$_POST['tran_qty'] = qty_format($_POST['tran_qty'] , $order->line_items[$id]->stock_id, $dec);
//		$std_cost = $order->line_items[$id]->standard_cost;
//		$_POST['units'] = $order->line_items[$id]->units;
//        
//
//        hidden('process_id', $_POST['process_id']);
//        label_cell($order->line_items[$id]->process);
//        
//		hidden('stock_id', $_POST['stock_id']);
//		label_cell($_POST['stock_id']);
//		label_cell($order->line_items[$id]->item_description);
//	    $Ajax->activate('items_table');
//	}
//	else{
//  		$wo_details = get_work_order($_SESSION['issue_items']->order_id);
//
//        workorder_processes_list_cells(null, 'process_id', $_SESSION['issue_items']->order_id, null, '');
//  		//stock_component_items_list_cells(null, 'stock_id', $wo_details["stock_id"], null, false, true);
//        stock_items_list_cells (null,'stock_id', null, false, true);
//		if (list_updated('stock_id')) {
//			    $Ajax->activate('units');
//			    $Ajax->activate('qty');
//			    $Ajax->activate('std_cost');
//                $Ajax->activate('tran_unit');
//                $Ajax->activate('tran_qty');
//		}
//        
//        if (list_updated('tran_unit')) {
//            $Ajax->activate('std_cost');
//        }
//
//    	$item_info = get_item_edit_info($_POST['stock_id']);
//            $dec = $item_info['decimals'];
//            $_POST['qty'] = number_format2(0, $dec);
//            $_POST['units'] = $item_info["units"];
//            $_POST['issued_qty'] = number_format2(0, $dec);
//            //$_POST['line_required_date'] = $_POST['mtr_date'];
//            $_POST['line_comment'] = "";
//                
//    }
//    
//    if($line_no > 0 && isset($order->line_items[$id]->req_unit)){
//        label_cell($order->line_items[$id]->req_unit);
//        //label_cell($order->line_items[$id]->req_unit_qty);
//        qty_cell($order->line_items[$id]->req_unit_qty, false, $dec);
//        //label_cell($order->line_items[$id]->units_issued / $order->line_items[$id]->req_unit_conversion_factor);
//        qty_cell($order->line_items[$id]->units_issued / $order->line_items[$id]->req_unit_conversion_factor, false, $dec);
//        
//        if(!isset($_POST['loc_code'])){
//            $_POST['loc_code'] = $order->line_items[$id]->loc_code;
//        }
//    }
//    else{
//        label_cell('Manualy added item',"align=center colspan=3");      
//    }
//    
//    
//    locations_list_cells(null, 'loc_code', null, null, false);
//        
//    if ($line_no != -1 && $line_no == $id)
//	{
//            $alternate_unit = get_alternate_unit_details($_POST['tran_unit']);
//            $_POST['std_cost'] = $_POST['std_cost'];
//            if($alternate_unit == ""){
//                //label_cell($order->line_items[$id]->units);
//                units_list_cells(null, 'tran_unit', $_POST['stock_id'], null, false, true);
//            }
//            else{
//                //label_cell($order->line_items[$id]->tran_unit);
//                units_list_cells(null, 'tran_unit', $_POST['stock_id'], null, false, true);
//            }
//            hidden('OnGoingEdit'.$id, $id); 
//    }
//    else{
//        units_list_cells(null, 'tran_unit', $_POST['stock_id'], null, false, true);
//        $alternate_unit = get_alternate_unit_details($_POST['tran_unit']);
//        if($alternate_unit != ""){
//            $convertion_factor = $alternate_unit['convertion_factor'];
//            $_POST['std_cost'] = input_num('std_cost')*$convertion_factor;
//            $_POST['std_cost'] = price_decimal_format($_POST['std_cost'], $dec2);
//        }
//
//    }
//    
//    //qty_cells(null, 'tran_qty', $_POST['tran_qty'], null, null, $dec);
//    
//    
//
//    $item_info = get_item_edit_info($_POST['stock_id']);
//    $_POST['stock_control_method'] = $item_info["stock_control_method"];
//
//    if ($line_no != -1 && $line_no == $id){
//        $stock_id = $order->line_items[$id]->stock_id;
//        $line_ref = $order->line_items[$id]->line_ref;
//        //$tran_qty = $order->line_items[$id]->tran_qty;
//        $tran_qty = $_POST['tran_qty'];
//    }
//    else{
//        $stock_id = '';
//        if(isset($_POST['stock_id'])){
//            $stock_id = $_POST['stock_id'];
//        }
//        $line_ref = $order->get_line_ref();
//        $tran_qty = 0;
//    }
//    
//    $unit_of_parent = get_post('tran_unit');
//    $cell_prefix = '';
//    
//    display_quantity_cells(get_post('guid'), $order->trans_type, $line_ref, $stock_id, 
//                            $_POST['stock_control_method'], $cell_prefix, $tran_qty, $unit_of_parent, 
//                            $dec = 2, "items_table");
//
//    hidden('qty', $qty);
//    hidden('units', $units); 
//
//    
//	//amount_cells(null, 'std_cost', $_POST['std_cost']);
//	hidden('std_cost', $std_cost);
//	//amount_cell($std_cost);
//    label_cell('');
//	if ($id != -1)
//	{
//		button_cell('UpdateItem', _("Update"),
//				_('Confirm changes'), ICON_UPDATE);
//		button_cell('CancelItemChanges', _("Cancel"),
//				_('Cancel changes'), ICON_CANCEL);
//		hidden('LineNo', $line_no);
// 		set_focus('qty');
//	}
//	else
//	{
//		submit_cells('AddItem', _("Add Item"), "colspan=2",
//		    _('Add new item to document'), true);
//	}
//
//    hidden('line_ref', $order->get_line_ref());
//    
//	end_row();
//}
//

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

function wo_pr_options_controls($woid)
{
	global $Refs;

	echo "<br>";
	start_table();
    ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_PURCH_REQ));
 
 	if (!isset($_POST['IssueType']))
 		$_POST['IssueType'] = 0;
 
// 	yesno_list_row(_("Type:"), 'IssueType', $_POST['IssueType'],
// 		_("Return Items to Location"), _("Issue Items to Work order"));
// 
 
    if(!isset($_POST['project_id'])){
        $wo = get_work_order_header($woid);
        $_POST['project_id'] = $wo['project_id'];
        $_POST['task_id'] = $wo['task_id'];

        
    }
    
    locations_list_row("Location:", "pr_location", null, "Select a location", false);
    project_list_row("Project:", "project_id", null, '', true);
    tasks_list_row("Task :", "task_id", $_POST['project_id'], null, '');
//    supplier_list_row("Supplier :", "supp_id", get_post('supp_id'), null, '');
    date_row(_("PR Date:"), 'date_');

	textarea_row(_("Memo"), 'memo_', null, 50, 3);

	end_table(1);
}
//---------------------------------------------------------------------------------

?>