<?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");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
//--------------------------------------------------------------------------------

function add_to_order(&$order, $new_item, $new_item_qty, $standard_cost, $tran_unit, $tran_qty, $convertion_factor)
{
    $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);
    
//    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(TABLESTYLE, "width=80%");

	table_section(1);
	
	locations_list_row(_("From Location:"), 'FromStockLocation', null, '', false);
	locations_list_row(_("To Location:"), 'ToStockLocation', null, '', false);

    project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);
    if(isset($order->job_reference) && !list_updated('project_id')){
        label_row(_("Job Reference:"), $order->job_reference);
    }

    if(list_updated('project_id') && $_POST['project_id'] != -1)
    {
        $job_ref = get_job_ref($_POST['project_id']);
        $order->job_reference = $job_ref['description'];
        label_row(_("Job Reference:"), $order->job_reference);
    }    

//    if ($_POST['project_id'] != -1)
//    {
//        $result = get_approval_path($_POST['project_id'], "MR");
//        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_LOCTRANSFER));

    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:"), 'movement_type', ST_LOCTRANSFER, null, '', true);
    
    if (list_updated('movement_type')) {
        unset($_POST['linked_field_id']);
    }
    
    $linked_field = 0;
    $movement_type = get_movement_type($_POST['movement_type']);
    if(isset($movement_type['linked_field']) && strlen($movement_type['linked_field']) > 0){
        $linked_field = $movement_type['linked_field'];
    }
    
    switch ($linked_field) {
        case IMLF_CUSTOMER:
            customer_list_row(_("Customer:"), 'linked_field_id', null, '');
            break;
        case IMLF_SUPPLIER:
            supplier_list_row(_("Supplier:"), 'linked_field_id', null, '');
            break;
        case IMLF_EMPLOYEE:
            employees_simple_list_row("Employee:", 'linked_field_id', null, '');
            break;
        case IMLF_VEHICLE:
            vehicles_list_row('Vehicle:', 'linked_field_id', null, '', true);
            break;
        case IMLF_ASSET:
            label_row(_("Asset:"), asset_list('linked_field_id', null, ''));
            break;
        default:
            hidden('linked_field_id', 0);
            break;
    } 
    
    hidden('linked_field_type', $linked_field);

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

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

function display_transfer_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"),  _("Job"),'');
	if ( count($order->line_items)) $th[] = '';
	table_header($th);
	$subtotal = 0;
	$k = 0;  //row colour counter

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

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

			view_stock_status_cell($stock_item->stock_id);
    		label_cell($stock_item->item_description);
    		
            if($stock_item->tran_unit == ""){
                label_cell($stock_item->units);
            }
            else{
                label_cell($stock_item->tran_unit);
            }

            qty_cell($stock_item->tran_qty, false, get_qty_dec($stock_item->stock_id));
            
            label_cell($stock_item->project_site_description);
                
			edit_button_cell("Edit$line_no", _("Edit"),
				_('Edit document line'));
			delete_button_cell("Delete$line_no", _("Delete"),
				_('Remove line from document'));
    		end_row();
		}
		else
		{
			transfer_edit_item_controls($order, $line_no);
		}
	}

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

    end_table();
	div_end();
}

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

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

	$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['units'] = $order->line_items[$id]->units;
                $_POST['stock_control_method'] = $order->line_items[$id]->stock_control_method;

		hidden('stock_id', $_POST['stock_id']);
		label_cell($_POST['stock_id']);
		label_cell($order->line_items[$id]->item_description);
                $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('qty');
                            $Ajax->activate('tran_unit');
                            $Ajax->activate('tran_qty');
		}

            $item_info = get_item_edit_info($_POST['stock_id']);
                if(isset($item_info['decimals']) && $item_info['decimals'] > 0){
                    $dec = $item_info['decimals'];
                    $_POST['qty'] = number_format2(0, $dec);
                    $_POST['units'] = $item_info["units"];
                    $_POST['stock_control_method'] = $item_info["stock_control_method"];
                }else{
                    $dec = 0;
                    $_POST['qty'] = 0;
                    $_POST['units'] = '';
                    $_POST['stock_control_method'] = 0;
                }
        }

//        qty_cells(null, 'tran_qty', null, null, null, $dec);
//        
//        if ($line_no != -1 && $line_no == $id)
//	{
//            $tran_unit = $order->line_items[$id]->tran_unit;
////            $_POST['std_cost'] = $_POST['std_cost'];
//            if($tran_unit == ""){
//                label_cell($order->line_items[$id]->units);
//            }
//            else{
//                label_cell($order->line_items[$id]->tran_unit);
//            }
//        }
//        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);
//            }
//            
//        }
    
        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').'&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);
//        }
    
//        if(isset($order->line_items[$id]->row_id) && $order->line_items[$id]->row_id != 0){
//            $line_ref = $order->line_items[$id]->row_id;
//        }
//        else{
//            $line_ref = null;
//        }
        
        if ($line_no != -1 && $line_no == $id){
            $line_ref = $order->line_items[$id]->line_ref;
            $tran_qty = $order->line_items[$id]->tran_qty;
        }
        else{
            $line_ref = $order->get_line_ref();
            $tran_qty = 0;
        }
    
        
        display_quantity_cells('guid', ST_LOCTRANSFER, $line_ref, $_POST['stock_id'],
                                $_POST['stock_control_method'], '', $tran_qty, $unit_of_parent, 
                                $dec, 'items_table');
                
        if ($line_no != -1 && $line_no == $id){
            project_sites_list_cells(null, 'project_site_id', $_POST['project_id'], $order->line_items[$id]->project_site_id,'');
        }
        else{
            project_sites_list_cells(null, 'project_site_id', $_POST['project_id'], null,'');
        }
        
//        hidden('qty', $_POST['qty']);
//        hidden('units', $_POST['units']);

	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->get_line_ref());
 		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 transfer_options_controls()
{
	  echo "<br>";
	  start_table();

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

	  end_table(1);
}


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

?>