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

function add_to_order(&$order, $new_item, $new_item_qty, $standard_cost, $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 order.  You can change the quantity ordered of the existing line if necessary.");
	else
         $order->add_to_cart (count($order->line_items), $new_item, $new_item_qty, $standard_cost, null, null,null,
                 null, 0, 0, 0, 0, 0, null, 0,null, null,null, null, null,$tran_unit, $tran_qty, 
                 $convertion_factor);
}

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

function display_order_header(&$order)
{
    global $Refs, $Ajax;
    div_start('header_tbl');
    start_outer_table(TABLESTYLE2, "width=70%"); // outer table
    table_section(1);

    locations_list_row(_("Location:"), 'StockLocation', null);

    project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);
    tasks_list_row(_("Task:"), 'task_id', $_POST['project_id'], null, _('Select a task'), true);
    if ($_POST['task_id'] != -1)
    {
        $result = get_task_approval_path($_POST['task_id']);
        label_row(_("Approval Path:"), $result['approval_path_name']);
    }
    else {
        label_row(_("Approval Path:"),"");
    }

    table_section(2, "33%");
      
    ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_INVADJUST));
    date_row(_("Date:"), 'AdjDate', '', true);

    if(count($order->line_items) > 0 && isset($order->sample_items)){
        $Ajax->activate('header_tbl');
        if($order->sample_items == 1){
            label_row(_("Sample Items:"), 'Yes');
        }
        else{
            label_row(_("Sample Items:"), 'No');
        }
    }
    else{
        $Ajax->activate('header_tbl');
        yesno_list_row(_("Sample Items:"), 'sample_items');
    }
            
    
	table_section(3, "33%");

    movement_types_list_row(_("Movement Type:"), 'type', ST_INVADJUST, null);

    if (!isset($_POST['Increase']))
    	$_POST['Increase'] = 1;
    yesno_list_row(_("Type:"), 'Increase', $_POST['Increase'],
    	_("Positive Adjustment"), _("Negative Adjustment"));

	end_outer_table(1); // outer table
    div_end();
}

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

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

	display_heading($title);
        div_start('items_table');
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("Item Code"), _("Item Description"), _("Unit"),
		_("Quantity"), _("Unit Cost"), _("Total"), "");
	if ( count($order->line_items)) $th[] = '';

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

	$id = find_submit('Edit');
	foreach ($order->line_items as $line_no=>$stock_item)
	{

		$total += ($stock_item->standard_cost * ($stock_item->tran_qty * $stock_item->convertion_factor));

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

			view_stock_status_cell($stock_item->stock_id);
			label_cell($stock_item->item_description);
            qty_cell($stock_item->tran_qty, false, get_qty_dec($stock_item->stock_id));
            label_cell($stock_item->alternate_unit_name);
			amount_decimal_cell($stock_item->standard_cost);
			amount_cell($stock_item->standard_cost * ($stock_item->tran_qty * $stock_item->convertion_factor));

			edit_button_cell("Edit$line_no", _("Edit"),
				_('Edit document line'));
			delete_button_cell("Delete$line_no", _("Delete"),
				_('Remove line from document'));
			end_row();
		}
		else
		{
			adjustment_edit_item_controls($order, $line_no);
		}
	}

	if ($id == -1)
		adjustment_edit_item_controls($order);

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

    end_table();
	div_end();
}

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

function adjustment_edit_item_controls(&$order, $line_no=-1)
{
	global $Ajax;
	start_row();

	$dec2 = 0;
	$id = find_submit('Edit');
	if ($line_no != -1 && $line_no == $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);
		$_POST['tran_qty'] = qty_format($order->line_items[$id]->tran_qty, 
			$order->line_items[$id]->stock_id, $dec);
		$_POST['std_cost'] = price_decimal_format($order->line_items[$id]->standard_cost, $dec2);
		$_POST['units'] = $order->line_items[$id]->units;
        $_POST['stock_control_method'] = $order->line_items[$id]->stock_control_method;
           $_SESSION['lot_detail_line'] = $order->line_items[$id]->lot_details;
		hidden('stock_id', $_POST['stock_id']);
		label_cell($_POST['stock_id']);
		label_cell($order->line_items[$id]->item_description, 'nowrap');
	    $Ajax->activate('items_table');
	}
	else
	{
    	stock_costable_items_list_cells(null, 'stock_id', null, false, true);
		if (list_updated('stock_id')) {
			    $Ajax->activate('units');
                $Ajax->activate('tran_unit');
                $Ajax->activate('tran_qty');
			    $Ajax->activate('qty');
			    $Ajax->activate('std_cost');
		}
        
        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['std_cost'] = price_decimal_format($item_info["standard_cost"], $dec2);
            $_POST['units'] = $item_info["units"];
            $_POST['stock_control_method'] = $item_info["stock_control_method"];
            
            hidden('qty', $_POST['qty']);
            hidden('units', $_POST['units']);
            
        }
        
        if ($line_no != -1 && $line_no == $id)
        {
            label_cell($order->line_items[$id]->alternate_unit_name);
            $unit_of_parent = $order->line_items[$id]->tran_unit;
        }
        else{
            units_list_cells(null, 'tran_unit', $_POST['stock_id'], null, false, true);
            $unit_of_parent = $_POST['tran_unit'];
        }
        

        if($_POST['stock_control_method'] == SKCM_LOTS){
            $Ajax->activate('items_table');
            //$lot_popup = viewer_link('', 'inventory/manage/lot_list.php?stock_id='.$_POST['stock_id'].'&popup=1&parent=tran_qty', '', '',  ICON_VIEW);
            $lot_popup = viewer_link('', 'inventory/manage/lot_list.php?value_ref='.get_post('guid').'&line_ref='.$order->line_ref().'&stock_id='.$_POST['stock_id'].'&popup=1&parent=tran_qty&unit_of_parent='.$unit_of_parent, '', '',  ICON_VIEW);
            qty_cells(null, 'tran_qty', null, null, $lot_popup, $dec);
        }
        elseif($_POST['stock_control_method'] == SKCM_SERIAL_NOS){
            $Ajax->activate('items_table');
            //$lot_popup = viewer_link('', 'inventory/manage/lot_list.php?stock_id='.$_POST['stock_id'].'&popup=1&parent=tran_qty', '', '',  ICON_VIEW);
            $lot_popup = viewer_link('', 'inventory/manage/serial_no_list.php?value_ref='.get_post('guid').'&stock_id='.$_POST['stock_id'].'&popup=1&parent=tran_qty&unit_of_parent='.$unit_of_parent, '', '',  ICON_VIEW);
            qty_cells(null, 'tran_qty', null, null, $lot_popup, $dec);
        }
        else{
            $Ajax->activate('items_table');
            qty_cells(null, 'tran_qty', null, null, null, $dec);
        }


	amount_cells(null, 'std_cost', null, null, null, $dec2);
	label_cell("&nbsp;");

	if ($id != -1)
	{
		button_cell('UpdateItem', _("Update"),
				_('Confirm changes'), ICON_UPDATE);
		button_cell('CancelItemChanges', _("Cancel"),
				_('Cancel changes'), ICON_CANCEL);
		hidden('LineNo', $line_no);
        hidden('line_ref', $order->line_ref());
 		set_focus('qty');
	}
	else
	{
		submit_cells('AddItem', _("Add Item"), "colspan=2",
		    _('Add new item to document'), true);
        hidden('line_ref', $order->line_ref());
	}

	end_row();
}


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

function adjustment_options_controls()
{
	  echo "<br>";
	  start_table();

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

	  end_table(1);
}


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

?>