<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PURCHASEPRICING';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");

if (!@$_GET['popup'])
	page(_($help_context = "Supplier Purchasing Data"));

check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
check_db_has_suppliers(_("There are no suppliers defined in the system."));

//----------------------------------------------------------------------------------------
simple_page_mode(true);
if (isset($_GET['stock_id']))
{
	$_POST['stock_id'] = $_GET['stock_id'];
}

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

function check_data()
{
   	if ($_POST['supplier_id'] == "" || !isset($_POST['supplier_id']))
   	{
      	display_error( _("There is no supplier selected."));
		set_focus('stock_id');
        return false;
   	}
    
    if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
   	{
      	display_error( _("There is no item selected."));
		set_focus('stock_id');
        return false;
   	}
    
    if ($_POST['suppliers_uom'] == "0" || !isset($_POST['suppliers_uom']))
   	{
      	display_error( _("There is no unit selected."));
		set_focus('stock_id');
        return false;
   	}
   	
    if (!check_num('price', 0))
   	{
      	display_error( _("The price entered was not numeric or positive."));
        set_focus('price');
        return false;
   	}
    
    if (!check_num('discount', 0))
   	{
      	display_error( _("The discount entered was not numeric or positive."));
        set_focus('discount');
        return false;
   	}
    
    return true;

}


if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{
    if(check_data()){
    
        if($_POST['project_id'] <= 0){
            $_POST['project_id'] = 0;
        }
    
        if ($Mode == 'ADD_ITEM') 
        {
            add_item_purchasing_data($_POST['supplier_id'], $_POST['stock_id'], 
                                input_num('price',0), input_num('discount',0),
                                $_POST['project_id'], $_POST['suppliers_uom'], 
                                $_POST['effective_date'], $_POST['expiry_date'],
                                $_POST['supplier_description'], $_POST['supplier_itemcode']);
            display_notification(_("This supplier purchasing data has been added."));
        } 
        else
        {
            update_item_purchasing_data($selected_id, input_num('price',0), input_num('discount',0),
                                $_POST['project_id'], $_POST['suppliers_uom'], 
                                $_POST['effective_date'], $_POST['expiry_date'], 
                                $_POST['supplier_description'], $_POST['supplier_itemcode']);
            display_notification(_("Supplier purchasing data has been updated."));
        }
        $Mode = 'RESET';
        
    }
}

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

if ($Mode == 'Delete')
{
	delete_item_purchasing_data($selected_id);
	display_notification(_("The purchasing data item has been sucessfully deleted."));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	$Ajax->activate('_page_body');
}

if (list_updated('stock_id')) 
	$Ajax->activate('price_table');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();

if (!isset($_POST['stock_id']))
	$_POST['stock_id'] = get_global_stock_item();

if (!@$_GET['popup'])
{
	echo "<center>" . _("Item:"). "&nbsp;";
	//Chaitanya : All items can be purchased
	echo stock_items_list('stock_id', $_POST['stock_id'], false, true);
	//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
	echo "<hr></center>";
}
else
	br(2);

set_global_stock_item($_POST['stock_id']);

$mb_flag = get_mb_flag($_POST['stock_id']);

if ($mb_flag == -1)
{
	display_error(_("Entered item is not defined. Please re-enter."));
	set_focus('stock_id');
}
else
{
	$result = get_items_purchasing_data($_POST['stock_id']);
  	div_start('price_table');
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There is no purchasing data set up for the part selected"));
    }
    else
    {
        start_table(TABLESTYLE, "width=80%");

		$th = array(_("Supplier"), _("Project"), _("Unit"),
                _("Effective Date"), _("Expiry Date"),
                _("Currency"), _("Price"), "Discount",
                _("Supplier's Code"), _("Supplier's Description"), "", "");

        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
			alt_table_row_color($k);

            label_cell($myrow["supp_name"]);
            label_cell($myrow["project_name"]);
            
            label_cell($myrow["unit_name"]);
            
            $effective_date = "";
            if($myrow["effective_date"] != INFINITE_START_DATE){
                $effective_date = sql2date($myrow["effective_date"]);
            }
            
            $expiry_date = "";
            if($myrow["expiry_date"] != INFINITE_END_DATE){
                $expiry_date = sql2date($myrow["expiry_date"]);
            }
            label_cell($effective_date);
            label_cell($expiry_date);
            label_cell($myrow["curr_code"]);
            amount_decimal_cell($myrow["price"]);
            amount_decimal_cell($myrow["discount"]);
            
            label_cell($myrow["supplier_itemcode"]);
            label_cell($myrow["supplier_description"]);
		 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
		 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
 div_end();
}

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

$dec2 = 6;
if ($Mode =='Edit')
{
	$myrow = get_item_purchasing_data($selected_id, $_POST['stock_id']);

    $supp_name = $myrow["supp_name"];
    $_POST['supplier_id'] = $myrow["supplier_id"];
    $_POST['project_id'] = $myrow["project_id"];
    $_POST['price'] = price_decimal_format($myrow["price"], $dec2);
    $_POST['discount'] = price_decimal_format($myrow["discount"], $dec2);
    $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
    $_POST['effective_date'] = sql2date($myrow["effective_date"]);
    $_POST['expiry_date'] = sql2date($myrow["expiry_date"]);
    $_POST['supplier_itemcode'] = $myrow["supplier_itemcode"];
    $_POST['supplier_description'] = $myrow["supplier_description"];
}

br();
hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}

start_table(TABLESTYLE2);

if ($Mode == 'Edit')
{
	hidden('supplier_id', $_POST['supplier_id']);
	label_row(_("Supplier:"), $supp_name);
}
else
{
	supplier_list_row(_("Supplier:"), 'supplier_id', null, "", true);
	$_POST['price'] = $_POST['discount'] = $_POST['suppliers_uom'] = $_POST['conversion_factor'] = $_POST['supplier_itemcode'] = $_POST['supplier_description'] = "";
}
project_list_row("Project:", "project_id", null, "");

//text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
stock_units_list_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, true, "");

if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
{
   	//$_POST['conversion_factor'] = maxprec_format(3);
    $_POST['conversion_factor'] = number_format2(1, 3);
}

date_row("Effective Date", "effective_date");
date_row("Expiry Date", "expiry_date");

amount_row(_("Price:"), 'price', null,'', get_supplier_currency($selected_id), $dec2);
//amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor', maxprec_format($_POST['conversion_factor']), null, null, 'max');
amount_row(_("Discount:"), 'discount', null, null, null, 2);
text_row(_("Supplier's Code:"), 'supplier_itemcode', null, 20, 20);
text_row(_("Supplier's Description:"), 'supplier_description', null, 50, 51);

end_table(1);

submit_add_or_update_center($selected_id == -1, '', 'both');

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>
