hasokeric
3/23/2015 - 7:02 PM

Epicor Helpers

Epicor Helpers


	// ---------------------------------------------------------------------------------------
	// HELPER FUNCTIONS
	// ---------------------------------------------------------------------------------------

	/**
	 * Rounds & Converts a value
	 *
	 * @type 	Custom Function
	 * @return	decimal
	 */
	private decimal Round(decimal value, string format)
	{
		string newValue = string.Format(format, Math.Round(Convert.ToDecimal(value), 4));
		return Convert.ToDecimal(newValue);
	}

	private decimal Round(decimal value)
	{
		return Round(value, "{0:0.####}");
	}

	/**
	 * Rounds & Converts a value
	 *
	 * @type 	Custom Function
	 * @return	decimal
	 */
	private decimal Floor(decimal value, string format)
	{
		string newValue = string.Format(format, Math.Floor(Convert.ToDecimal(value)));
		return Convert.ToDecimal(newValue);
	}

	private decimal Floor(decimal value)
	{
		return Floor(value, "{0:0.##}");
	}

	/**
	 * Math Floors a Value and if 0 has a Backup Value
	 *
	 * @type 	Custom Function
	 * @return	decimal
	 */
	private decimal FloorBackup(decimal value)
	{
		const decimal BACKUP_VALUE = 0.5m;
		decimal result = Math.Floor(Convert.ToDecimal(value));

		if (result > 0) {
			return result;
		}

		return BACKUP_VALUE;
	}

	/**
	 * Format a value and convert it back to Decimal
	 *
	 * @type 	Custom Function
	 * @var 	value
	 * @var 	format "{0:0.#0}"
	 * @return	decimal
	 */
	private decimal Format(decimal value, string format)
	{
		string newValue = string.Format(format, Convert.ToDecimal(value));
		return Convert.ToDecimal(newValue);
	}

	private decimal Format(decimal value)
	{
		return Format(value, "{0:0.#0}");
	}

	/**
	 * Disable a Field and Set its EpiBinding value to 0
	 * it will automatically detect the UI fields EpiBinding
	 *
	 * NOTE: This here must be improved because on some controls it does not work
	 *
	 * @var 	c The UI field name
	 * @type 	Custom Function
	 */
	private void disableField<T>(T c) where T : IEpiControl
	{
		// Detect Controls EpiBinding
		string epiBinding = c.EpiBinding;
		string[] epiBindings = epiBinding.Split('.');

		if (epiBinding == "") {
			return;
		}

		string dataViewName = epiBindings[0];
		string columnName = epiBindings[1];

		EpiDataView dv = (EpiDataView)oTrans.EpiDataViews[ dataViewName ];

		// Disable and Set to 0
		// TODO: What About String and Combo
		dv.dataView[dv.Row][ columnName ] = 0;
		c.Enabled = false;
	}

	/**
	 * Get a Control Fields EpiBinding
	 *
	 * @var 	c The UI field name
	 * @type 	Custom Function
	 * @return	array
	 */
	private string[] GetFieldEpiBinding<T>(T c) where T : IEpiControl
	{
		// Detect Controls EpiBinding
		string epiBinding = c.EpiBinding;
		string[] epiBindings = epiBinding.Split('.'); // TODO: Refactor

		return epiBindings;
	}

	/**
	 * Loops through a Group and detects all the Field EpiBindings
	 * then sets all the columns to 0
	 *
	 * @var 	grp The UI Group field name
	 * @var 	bChildrenGroups loop through children groups too?
	 * @type 	Custom Function
	 */
	private void SetGroupFieldsToZero(EpiGroupBox grp, bool bChildrenGroups)
	{
		// Variables
		EpiDataView dv = null;

		foreach (Control cntrl in grp.Controls)
		{
			if (cntrl is EpiLabel | cntrl is EpiButton) {
				continue;
			}
			// Recursive: Get Children GroupBoxes too
			else if (cntrl is EpiGroupBox && bChildrenGroups) {
				SetGroupFieldsToZero((EpiGroupBox) cntrl);
			}
			else
			{
				string[] epiBindings = GetFieldEpiBinding((IEpiControl) cntrl);

				if (epiBindings[0] == "") {
					return;
				}

				// Initialize dv once
				if (dv == null) {
					dv = (EpiDataView)oTrans.EpiDataViews[ epiBindings[0] ];
				}

				// Reset Column to 0
				dv.dataView[dv.Row][ epiBindings[1] ] = 0;
			}
		}
	}

	private void SetGroupFieldsToZero(EpiGroupBox grp)
	{
		SetGroupFieldsToZero(grp, true);
	}

	/**
	 * Loops through a Group and detects all the Field EpiBindings
	 * then sets the ExtendedProperty to Readonly
	 *
	 * @var 	grp The UI Group field name
	 * @var 	bChildrenGroups loop through children groups too?
	 * @type 	Custom Function
	 */
	private void SetGroupFieldsExtendedReadOnly(EpiGroupBox grp, bool bChildrenGroups)
	{
		// Variables
		EpiDataView dv = null;

		foreach (Control cntrl in grp.Controls)
		{
			if (cntrl is EpiLabel | cntrl is EpiButton) {
				continue;
			}
			// Recursive: Get Children GroupBoxes too
			else if (cntrl is EpiGroupBox && bChildrenGroups) {
				SetGroupFieldsExtendedReadOnly((EpiGroupBox) cntrl);
			}
			else
			{
				string[] epiBindings = GetFieldEpiBinding((IEpiControl) cntrl);

				if (epiBindings[0] == "") {
					return;
				}

				// Initialize dv once
				if (dv == null) {
					dv = (EpiDataView)oTrans.EpiDataViews[ epiBindings[0] ];
				}

				// Check if Column Exists
				if (dv.dataView.Table.Columns.Contains( epiBindings[1] ))
				{
					dv.dataView.Table.Columns[ epiBindings[1] ].ExtendedProperties["ReadOnly"] = true;
				}
			}
		}
	}

	private void SetGroupFieldsExtendedReadOnly(EpiGroupBox grp)
	{
		SetGroupFieldsExtendedReadOnly(grp, true);
	}

	/**
	 * Get Epicor Data View Helper
	 *
	 * @var 	tableName EpiBinding Table Name
	 * @type 	Custom Function
	 * @return	EpiDataView
	 */
	private EpiDataView GetDataView(string tableName)
	{
		return (EpiDataView)oTrans.EpiDataViews[ tableName ];
	}

	/**
	 * Based on the Current Row and Line Level
	 * generate a unique sequence number which
	 * combines the Sequence + Line together
	 *
	 * @type 	Custom Function
	 * @return	int
	 */
	private string GetUniqueIndex(string tableName)
	{
		EpiDataView dv = (EpiDataView)oTrans.EpiDataViews[ tableName ];
		System.Data.DataRow row = dv.CurrentDataRow;

		string ret = "";

		switch (tableName)
		{
			case "JobMtl":
				ret = row["QuoteLine"].ToString() + "" + row["MtlSeq"].ToString();
				break;

			case "JobOper":
				ret = row["QuoteLine"].ToString() + "" + row["OprSeq"].ToString();
				break;

			case "UD06View":
				ret = row["Key2"].ToString() + "" + row["Key5"].ToString();
				break;
		}

		return ret;
	}