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

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

function add_mio_to_cart(&$order, $new_item, $new_item_qty, $required_date, $line_comment)
{
    if ($order->find_cart_item($new_item))
    {
        display_error(_("For Part :") . $new_item . " " . "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), $new_item, $new_item_qty, null, null, $required_date, $line_comment);
    }
}

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

function display_stock_reservation_release_header(&$order)
{
	global $Refs, $approval_statuses;
    
    if(isset($order->order_id) && $order->order_id > 0){
        $project_id = $order->project_id;
        $user_id = $_SESSION['wa_current_user']->user;
        $cap = can_access_project($project_id, $user_id);
        if($cap['success'] !== true) 
        {
            display_error($cap["message"]);
            set_focus('project_id');
            display_footer_exit();
            exit;
        }  
    }
            

    start_outer_table(TABLESTYLE2, "width=80%");

    table_section(1);
//    label_row(_("Requisition No:"), $order->order_id, "class='tableheader2'");
//    label_row(_("Requisition Reference:"), $order->reference, "class='tableheader2'");
//    label_row(_("Project Description:"), $order->project_description, "class='tableheader2'");
//    label_row(_("Task Description:"), $order->task_description, "class='tableheader2'");
    label_row(_("Location"), $order->location_name, "class='tableheader2'");
    label_row(_("Project"), $order->project_description, "class='tableheader2'");
    //label_row(_("Approval Path"), $order->approval_path_description, "class='tableheader2'");

    table_section(2, "35%");
    label_row(_("Requisition No:"), $order->order_id, "class='tableheader2'");
    label_row(_("Requisition Reference:"), $order->reference, "class='tableheader2'");
    label_row(_("Required Date:"), $order->required_date, "class='tableheader2'");
    
    
    table_section(3, "35%");
    label_row(_("Movement Type"), $order->movement_type_name, "class='tableheader2'");
    $linked_field_values = get_movement_type_linked_field_values($order->linked_field_type, $order->linked_field_id);
    if(strlen($linked_field_values["field_name"]) > 0){
        label_row($linked_field_values["field_name"], $linked_field_values['value'], "class='tableheader2'");
    }
    
    label_row(_("Requestor:"), $order->requestor_name, "class='tableheader2'");
//    $s = get_project_trans_view_str(98, "1&source_trans_no=".ST_MATERIAL_REQ, "Approval Status");
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $order->order_id."&source_type_id=".ST_STOCK_RELEASE, "Approval Status");
    //label_row($s,$approval_statuses[$order->approval_status], "class='tableheader2'");
    label_row(_("Requisition Comment:"), $order->memo_, "class='tableheader2'");
    
    end_outer_table(1);


    start_outer_table(TABLESTYLE2, "width=40%"); // outer table
    table_section(1);
    ref_row(_("Reference:"), 'srr_reference', '', $Refs->get_next(ST_STOCK_RELEASE));

    table_section(2, "50%");
    date_row(_("Date:"), 'srr_date', '', false);
    
    //yesno_list_row("Make Requisition Complete:", "mtr_complete",$order->mr_complete);
    
    end_outer_table(1); // outer table
}

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

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

	display_heading($title);
    div_start('items_table');
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("Item Code"), _("Item Description"), _("Reserved. Unit Qty"), _("Res. Unit"), 
                    _("Qty Released"), _("Release. Unit"), _("Release. Qty"),  _("Reserved Date"),_("Comment"), _("Job"));
    
	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 * $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);
                    
                    $dec = get_qty_dec($stock_item->stock_id);
//                    qty_cell($stock_item->requested_qty, false, $dec);
//                    label_cell($stock_item->units);
                    qty_cell($stock_item->tran_qty, false, $dec);
                    label_cell($stock_item->alternate_unit_name);
                    $tran_unit_issued_qty = $stock_item->issued_qty / $stock_item->convertion_factor;
                    qty_cell($tran_unit_issued_qty, false, $dec);
                    
                    if(get_post($stock_item->row_id.'_tran_unit') != ''){
                        $current_tran_unit = get_post($stock_item->row_id.'_tran_unit');
                    }
                    else{
                        $current_tran_unit = $stock_item->tran_unit;
                    }
                    
                    
                    $alternate_unit = get_alternate_unit_info($stock_item->stock_id, $current_tran_unit);
                    if($alternate_unit){
                        $alternate_unit_convertion_factor = $alternate_unit['convertion_factor'];
                    }
                    else{
                        $alternate_unit_convertion_factor = 1;
                    }
//                    units_list_cells(null, $stock_item->row_id.'_tran_unit', $stock_item->stock_id, $alternate_unit['alternate_unit'], false, true);
                    
                    if(list_updated($stock_item->row_id.'_tran_unit') || !isset($_POST[$stock_item->row_id."_tran_qty"] )){
                        $base_unit_req_qty = $stock_item->tran_qty * $stock_item->convertion_factor;
                        $suggested_issu_qty = ( $base_unit_req_qty - $stock_item->issued_qty) / $alternate_unit_convertion_factor;
                        if($suggested_issu_qty < 0){
                            $suggested_issu_qty = 0;
                        }
                        $_POST[$stock_item->row_id."_tran_qty"] = $suggested_issu_qty;
                    }
                    else{
                        $suggested_issu_qty = $_POST[$stock_item->row_id."_tran_qty"];
                    }
                    
//                    qty_cells(null, $stock_item->row_id."_qty", $suggested_issu_qty, null, null, $dec);
                    
                    units_list_cells(null, $stock_item->row_id.'_tran_unit', $stock_item->stock_id, null, false, true);
                    //$unit_of_parent = $_POST['tran_unit'];
                    $unit_of_parent = get_post($stock_item->row_id.'_tran_unit');

                    $item_info = get_item_edit_info($stock_item->stock_id);
                    $_POST['stock_control_method'] = $item_info["stock_control_method"];
                    
//                    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='.$stock_item->stock_id.'&popup=1&parent='.$stock_item->row_id.'_tran_qty'.'&unit_of_parent='.$unit_of_parent, '', '',  ICON_VIEW);
//                        qty_cells(null, $stock_item->row_id.'_tran_qty', $suggested_issu_qty, 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='.$stock_item->stock_id.'&popup=1&parent='.$stock_item->row_id.'_tran_qty'.'&unit_of_parent='.$unit_of_parent, '', '',  ICON_VIEW);
//                        qty_cells(null, $stock_item->row_id.'_tran_qty', $suggested_issu_qty, null, $lot_popup, $dec);
//                    }
//                    else{
//                        $Ajax->activate('items_table');
//                        qty_cells(null, $stock_item->row_id.'_tran_qty', $suggested_issu_qty, null, null, $dec);
//                    }
                    
                    display_quantity_cells(get_post('guid'), $order->trans_type, $stock_item->line_ref, $stock_item->stock_id, 
                                            $item_info["stock_control_method"], $stock_item->row_id.'_', $suggested_issu_qty, $unit_of_parent, 
                                            $dec = 2, "items_table");
                    
                    label_cell($stock_item->required_date);
                    text_cells(null, $stock_item->row_id.'_line_comment',null,30);
                    
                    project_sites_list_cells(null, $stock_item->row_id.'_project_site_id', $order->project_id, $stock_item->project_site_id,'');
                    
                    /*
                    edit_button_cell("Edit$line_no", _("Edit"),
                            _('Edit document line'));
                    delete_button_cell("Delete$line_no", _("Delete"),
                            _('Remove line from document'));
                     * 
                     */
                    end_row();
                    
                    $dec = 0;
		}
		else
		{
			//mio_edit_item_controls($order, $line_no);
		}
	}

	if ($id == -1)
		//mio_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 srr_options_controls()
{
	  echo "<br>";
	  start_table();

	  textarea_row(_("Material Transfer Comment:"), 'memo_', null, 50, 3);

	  end_table(1);
}


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

function view_requisition_transfer_header($mrt_no, $suppress_view_link=false)
{
    $myrow = get_material_issue_header($mrt_no);

    if (strlen($myrow[0]) == 0)
    {
    	display_note(_("The material requisition transfer sent is not valid."));
    	exit;
    }

   	start_table(TABLESTYLE, "width=80%");

	$th = array(_("#"), _("Reference"), _("Issuing Location"), _("Receiving Location"),
			_("Source MR No"), _("Date"), _("Issued By"));

	table_header($th);
	start_row();
	if ($suppress_view_link)
		label_cell($myrow["mrt_no"]);
	else
		//label_cell(get_trans_view_str(ST_MATERIAL_ISSUE, $myrow["mrt_no"]));
            label_cell(get_inventory_trans_view_str(ST_MATERIAL_ISSUE, $myrow["mrt_no"]), "class='tableheader2'");
        
	label_cell($myrow["reference"]);
	label_cell($myrow["issuing_location_name"]);
	//view_stock_status_cell($myrow["stock_id"], $myrow["StockItemName"]);
	label_cell($myrow["receiving_location_name"]);
        label_cell(get_trans_view_str(ST_MATERIAL_REQ, $myrow["source_mtr_no"]));
	label_cell(sql2date($myrow["created_on"]));
        label_cell($myrow["created_user_name"]);

	end_row();

	comments_display_row(ST_MATERIAL_ISSUE, $mrt_no);

	end_table();

}

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

function view_material_requisition_transfer_details($mrt_no, $suppress_view_link=false)
{
    $result = get_material_issue_details($mrt_no, ST_REQTRANSFER);

   	start_table(TABLESTYLE, "width=80%");

	$th = array(_("Itemcode"), _("Item Description"), _("Qualtity"), _("Units"), _("Unit Cost"));

	table_header($th);
        
        while ($myrow = db_fetch($result)){
            if($myrow["qty"] > 0){
                start_row();

                label_cell($myrow["stock_id"]);
                view_stock_status_cell($myrow["stock_id"], $myrow["description"]);

                $quantity = $myrow["qty"];

                $dec = get_qty_dec($myrow["stock_id"]);
                qty_cell($quantity, false, $dec);
                
                $alternate_unit = get_unit_name($myrow['tran_unit']);
                label_cell($alternate_unit['name']);
                amount_decimal_cell($myrow['standard_cost']);


                end_row();
            }
        }


	end_table();

}


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


?>