<?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_material_return_header(&$order)
{
	global $Refs, $approval_statuses, $Ajax;

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

    table_section(1, "35%");
    label_row(_("Issued Location"), $order->issuing_location_name, "class='tableheader2'");
//    label_row(_("Receiving Location"), $order->receiving_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, "30%");
    //label_row(_("Requesting Location:"), $order->receiving_location, "class='tableheader2'");
    label_row(_("Requisition No"), get_trans_view_str(ST_MATERIAL_REQ, $order->order_id), "class='tableheader2'");
    label_row(_("Requisition Reference"), $order->reference, "class='tableheader2'");
    label_row(_("Required Date"), sql2date($order->required_date), "class='tableheader2'");
    
//    $pr_limk = "No PR";
//    if($order->pr_no > 0){
//        $pr_limk = get_trans_view_str(ST_PURCH_REQ,$order->pr_no);
//    }
//    label_row(_("PR No"), $pr_limk , "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_id=".ST_MATERIAL_REQ, "Approval Status");
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $order->order_id."&source_type_id=".ST_MATERIAL_REQ, "Approval Status");
    label_row($s,$approval_statuses[$order->approval_status], "class='tableheader2'");
    
    
    end_outer_table(1);
    
    if (strlen($order->memo_) > 0) {
        start_table(TABLESTYLE2);
            label_row(_("MR Comment:"), nl2br($order->memo_), "class='tableheader2'");
        end_table(1);
    }

    $Ajax->activate('group_company_employee_code');

    start_outer_table(TABLESTYLE2, "width=65%"); // outer table
    table_section(1, "40%");
    ref_row(_("Reference"), 'rm_reference', '', $Refs->get_next(ST_MATERIAL_RETURN));
    locations_list_row(_("Receiving Location:"), 'receiving_location', $order->receiving_location, false, false);
    
    table_section(2, "20%");
    date_row("Date:", 'rm_date');
    
    table_section(3, "40%");
    group_companies_list_row("Company: ", "group_company_code", null, false, true);
    group_company_employees_simple_list_row("Employee:", "group_company_employee_code", get_post('group_company_code'), null, '');
    
    
    
//    yesno_list_row("Make Requisition Complete", "mr_complete");
    
    end_outer_table(1); // outer table
}

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

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

	display_heading($title);
    div_start('items_table');
	start_table(TABLESTYLE, "width=100%");
	$th = array(_("ID"), _("Item Code"), _("Item Description"), _("Req. Unit Qty"), _("Req. Unit"), 
                    _("Qty Issued"), _("Qty Returned"), _("Return Unit"),_("Return Qty"), _("Required 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);

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

                    label_cell($stock_item->row_id, 'align=center');
                    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/$stock_item->convertion_factor, 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);
                    
                    $tran_unit_returned_qty = $stock_item->returned_qty / $stock_item->convertion_factor;
                    qty_cell($tran_unit_returned_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"];
//                    }
                    
                    if(!isset($_POST[$stock_item->row_id."_tran_qty"] )){
                        $_POST[$stock_item->row_id."_tran_qty"] = 0.00;
                    }
                    $suggested_return_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 = 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_return_qty, $unit_of_parent, 
                                            $dec = 2, "items_table");
                    
                    label_cell($stock_item->required_date);
                    label_cell($stock_item->line_comment);
                    //label_cell($stock_item->project_site_description);
                    
                    if(!isset($_POST[$stock_item->row_id.'_project_site_id'] )){
                        $_POST[$stock_item->row_id."_project_site_id"] = $stock_item->project_site_id;
                    }
                    project_sites_list_cells(null, $stock_item->row_id.'_project_site_id', $order->project_id, null,'');
                    
                    if($_POST['stock_control_method'] == SKCM_LOTS || $_POST['stock_control_method'] == SKCM_SERIAL_NOS){
                        button_cell("UpdateItem", _("Update"), _('Confirm changes'), ICON_UPDATE);
//                        hidden('LineNo', $line_no);
                    }
                    else{
                        label_cell("");
                    }
                    
                    check_cells(null, $stock_item->row_id.'_check_mr_item');
                    /*
                    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 mrt_options_controls()
{
	  echo "<br>";
	  start_table();

	  textarea_row(_("Material Return 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"));
//
//	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);
//
//
//                end_row();
//            }
//        }
//
//
//	end_table();
//
//}
//

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


?>