<?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 . "/asset_register/includes/modules_db.inc");
//--------------------------------------------------------------------------------

function add_ab_to_cart(&$order, $asset_id, $required_from_date, $required_to_date,  $line_comment)
{
    if ($order->find_cart_item($asset_id))
    {
        display_error(_("For Part :") . $asset_id . " " . "This item is already on this material requisition.  You can change the quantity requested of the existing line if necessary.");
    }
    else
    {
        $order->add_to_cart(count($order->line_items), $asset_id, 0, null, null, null, 
                null, $line_comment, null, null, null, null, null, null, null,
                $required_from_date, $required_to_date);
    }
}

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

function display_ab_header(&$order)
{
	global $Refs;

	start_outer_table(TABLESTYLE2, "width=60%"); // outer table
	table_section(1);
        
        if($order->order_id > 0)
        {
            $ab_cart = $_SESSION['ab_items'];
            if(isset($_POST['issuing_location'])){
            $ab_cart->issuing_location  = $_POST["issuing_location"];
            }
            if(isset($_POST['project_id'])){
            $ab_cart->project_id  = $_POST["project_id"];
            }
            if(isset($_POST['task_id'])){
            $ab_cart->task_id  = $_POST["task_id"];
            }
            if(isset($_POST['approval_path_id'])){
            $ab_cart->approval_path_id  = $_POST["approval_path_id"];
            }

            asset_locations_list_row(_("Issuing Location:"), 'issuing_location', $order->issuing_location);
            text_row(_("Reference:"), 'ab_reference', $order->reference, 10, 20);
            $_POST['mr_date'] = sql2date($order->required_date);
            date_row(_("Date:"), 'ab_date', $_POST['mr_date']);
            table_section(2, "40%");
            project_list_row(_("Project:"), 'project_id', $order->project_id,FALSE,TRUE);
            tasks_list_row(_("Task:"), 'task_id', $order->project_id, $order->task_id,FALSE,TRUE);
//            approval_path_list_row(_("Select Approval Path:"), 'approval_path_id', $order->approval_path_id,FALSE,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:"),"");
            }
            
        }
        else
        {
            //asset_locations_list_row(_("Requesting Location:"), 'requesting_location', null);
            asset_locations_list_row(_("Issuing Location:"), 'issuing_location', null);
            ref_row(_("Reference:"), 'ab_reference', '', $Refs->get_next(ST_MATERIAL_REQ));
            date_row(_("Date:"), 'ab_date', '', false);
            table_section(2, "40%");
            project_list_row(_("Project:"), 'project_id', null, _('New 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(3, "29%");
        yesno_list_row("Make Requisition Complete:", "ab_complete",$order->mr_complete);
//
//    if (!isset($_POST['Increase']))
//    	$_POST['Increase'] = 1;
//    yesno_list_row(_("Type:"), 'Increase', $_POST['Increase'],
//    	_("Positive Adjustment"), _("Negative Adjustment"));

	end_outer_table(1); // outer table
}

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

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

	display_heading($title);
    div_start('items_table');
	start_table(TABLESTYLE, "width=80%");
	$th = array(_("Asset"), _("Required From"), _("Required To"),_("Comment"), "");
	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->quantity);

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

                    $myrow = get_asset($stock_item->stock_id);
                    label_cell($myrow['asset_name']);
                    label_cell($stock_item->required_from_date);
                    label_cell($stock_item->required_to_date);
                    label_cell($stock_item->line_comment);

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

	if ($id == -1)
		mtr_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 mtr_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['asset_id'] = $order->line_items[$id]->stock_id;
                $_POST['line_required_from_date'] = $order->line_items[$id]->required_from_date;
                $_POST['line_required_to_date'] = $order->line_items[$id]->required_to_date;
                $_POST['line_comment'] = $order->line_items[$id]->line_comment;

		hidden('asset_id', $_POST['asset_id']);
//		label_cell($_POST['stock_id']);
		$myrow = get_asset($order->line_items[$id]->stock_id);
                label_cell($myrow['asset_name']);
	    $Ajax->activate('items_table');
	}

        else
        {
            asset_list_cells("", 'asset_id', null,
                _('New asset'), true, check_value('show_inactive'));
        }    
        
        date_cells('', 'line_required_from_date');
        date_cells('', 'line_required_to_date');
        text_cells('', 'line_comment',null,50);
	//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);
 		set_focus('qty');
	}
	else
	{
		submit_cells('AddItem', _("Add Item"), "colspan=2",
		    _('Add new item to document'), true);
	}

	end_row();
}


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

function mtr_options_controls($mtr_cart)
{
	  echo "<br>";
	  start_table();

	  textarea_row(_("Memo"), 'memo_', $mtr_cart->memo_, 50, 3);

	  end_table(1);
}


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

?>