Skip to content

Instantly share code, notes, and snippets.

@JACOBNOTES
Last active December 28, 2022 14:57
Show Gist options
  • Save JACOBNOTES/b3cbb1c6a8da05f58d36d61425ad607f to your computer and use it in GitHub Desktop.
Save JACOBNOTES/b3cbb1c6a8da05f58d36d61425ad607f to your computer and use it in GitHub Desktop.
Blog_GI_Reader
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PX.Objects;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.AR;
using PX.Data.Maintenance.GI;
namespace cwGIReadDemo.GraphExt
{
#region GI Reader Classes
public class GILineRecord
{
// modify this depending on the GI that you are trying to read.
public string AcctCD { get; set; }
public string AcctName { get; set; }
public decimal? UnpaidBalance { get; set; }
}
public class GIRecordTableFieldValues
{
public string caption { get; set; }
public string tableName { get; set; }
public string fieldName { get; set; }
public object fieldValue { get; set; }
public int recordNumber { get; set; }
}
#endregion
public class CustomerMaint_Extension : PXGraphExtension<CustomerMaint>
{
public static bool IsActive() => (true);
#region Read GI Example
public PXAction<Customer> ReadCreditHoldGI;
[PXUIField(DisplayName = "Read Credit Hold GI", Enabled = true)]
[PXProcessButton()]
public virtual IEnumerable readCreditHoldGI(PXAdapter adapter)
{
//string giTitle = "AR-CreditHoldCustomers";
string giTitle = "DB-ARLargeTrans";
int totalRecords = 0;
PXLongOperation.StartOperation(Base, delegate ()
{
CustomerMaint graph = PXGraph.CreateInstance<CustomerMaint>();
List<GILineRecord> lineRecords = ReadGI(giTitle, graph);
totalRecords = lineRecords?.Count ?? 0;
});
// wait for finish.
PXLongOperation.WaitCompletion(Base.UID);
Base.BAccount.Ask($"Finished Reading {totalRecords} records in the GI", MessageButtons.OK);
return adapter.Get();
}
#endregion
#region GI Reader methods
public static List<GILineRecord> ReadGI(string giTitle, PXGraph _graph)
{
List<GILineRecord> lineRecordsForReturn = new List<GILineRecord>();
List<GIRecordTableFieldValues> tableFieldValues = new List<GIRecordTableFieldValues>();
try
{
int recordCount = 0;
// find the GI by name
GIDesign design = PXSelectReadonly<GIDesign, Where<GIDesign.name, Equal<Required<GIDesign.name>>>>.Select(_graph, giTitle);
// create an instance of the pxGenericInqGrph
PXGenericInqGrph graph = PXGenericInqGrph.CreateInstance(design.DesignID.Value);
if (design != null)
{
//Loops through each returned result row of Generic Inquiry
foreach (GenericResult resultRow in graph.Views["Results"].SelectMulti())
{
recordCount++;
//Loops through objects returned from one row (Customer table, then ARInvoice table etc.)
foreach (string key in resultRow.Values.Keys)
{
//Loops through list of each table object and the fields we need values from for each data key
foreach (GIResult resultMap in PXSelectReadonly<GIResult,
Where<GIResult.designID, Equal<Required<GIResult.designID>>,
And<GIResult.objectName, Equal<Required<GIResult.objectName>>>>>
.Select(graph, new object[] { design.DesignID.Value, key }))
{
//retrieves field value from data object specified, key = ALIAS of table name
var result = graph.Caches[resultRow.Values[key].GetType()].GetValue(resultRow.Values[key], resultMap.Field);
// collect values
tableFieldValues.Add(new GIRecordTableFieldValues
{
fieldName = resultMap.Field,
caption = resultMap.Caption,
recordNumber = recordCount,
tableName = key,
fieldValue = result
});
}
}
// end of field collection
// if we are finding records then add them to our final output list.
if (recordCount > 0)
{
GILineRecord record = new GILineRecord();
record.AcctCD = GIGetStringValue("", "acctCD", recordCount, tableFieldValues);
record.AcctName = GIGetStringValue("", "acctName", recordCount, tableFieldValues);
record.UnpaidBalance = GIGetDecimalValue("", "unpaidBalance", recordCount, tableFieldValues);
lineRecordsForReturn.Add(record);
tableFieldValues.Clear();
}
}
}
return lineRecordsForReturn;
}
catch (Exception ex)
{
ex.Data.Add("ReadGI", "method");
throw;
}
}
public static DateTime? GIGetDateValue(string table, string field, int recordNumber, List<GIRecordTableFieldValues> tableFieldValues)
{
DateTime? ret = DateTime.Parse("01/01/1990");
try
{
if (string.IsNullOrEmpty(table))
{
ret = (DateTime?)tableFieldValues.Find(i => i.recordNumber == recordNumber &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue;
}
else
{
ret = (DateTime?)tableFieldValues.Find(i => i.recordNumber == recordNumber &&
i.tableName.Equals(table, StringComparison.CurrentCultureIgnoreCase) &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue;
}
}
catch (Exception)
{
return ret;
}
return ret;
}
public static string GIGetStringValue(string table, string field, int recordNumber, List<GIRecordTableFieldValues> tableFieldValues)
{
string ret = String.Empty;
try
{
if (string.IsNullOrEmpty(table))
{
ret = tableFieldValues.Find(i => i.recordNumber == recordNumber &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue.ToString();
}
else
{
ret = tableFieldValues.Find(i => i.recordNumber == recordNumber &&
i.tableName.Equals(table, StringComparison.CurrentCultureIgnoreCase) &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue.ToString();
}
}
catch (Exception)
{
return ret;
}
return ret;
}
public static decimal? GIGetDecimalValue(string table, string field, int recordNumber, List<GIRecordTableFieldValues> tableFieldValues)
{
decimal? ret = 0;
try
{
if (string.IsNullOrEmpty(table))
{
ret = (decimal?)tableFieldValues.Find(
i => i.recordNumber == recordNumber &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue;
}
else
{
ret = (decimal?)tableFieldValues.Find(i => i.recordNumber == recordNumber &&
i.tableName.Equals(table, StringComparison.CurrentCultureIgnoreCase) &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue;
}
}
catch (Exception)
{
return ret;
}
return ret;
}
public static int? GIGetIntValue(string table, string field, int recordNumber, List<GIRecordTableFieldValues> tableFieldValues)
{
int? ret = 0;
try
{
if (string.IsNullOrEmpty(table))
{
ret = (int?)tableFieldValues.Find(
i => i.recordNumber == recordNumber &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue;
}
else
{
ret = (int?)tableFieldValues.Find(i => i.recordNumber == recordNumber &&
i.tableName.Equals(table, StringComparison.CurrentCultureIgnoreCase) &&
i.fieldName.Equals(field, StringComparison.CurrentCultureIgnoreCase)).fieldValue;
}
}
catch (Exception)
{
return ret;
}
return ret;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment