Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Van Hoesen jvanhoesen

View GitHub Profile
@jvanhoesen
jvanhoesen / ZipArchiveCreateBtn.cs
Created Mar 28, 2022
Button to create .Zip file from files attached to Acumatica document
View ZipArchiveCreateBtn.cs
public PXAction<ARInvoice> CreateZip;
[PXButton(CommitChanges = false)]
[PXUIField(DisplayName = "Create .Zip", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update, Enabled = true)]
protected virtual IEnumerable createZip(PXAdapter pxAdapter)
{
if (Base.Document.Current != null)
{
UploadFileMaintenance uploadFileMaint = PXGraph.CreateInstance<UploadFileMaintenance>();
@jvanhoesen
jvanhoesen / ZipArchiveOpenBtn.cs
Created Mar 28, 2022
Opens a zip file attached to an Acumatica document
View ZipArchiveOpenBtn.cs
public PXAction<ARInvoice> OpenZip;
[PXButton(CommitChanges = false)]
[PXUIField(DisplayName = "Open .Zip", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update, Enabled = true)]
protected virtual IEnumerable openZip(PXAdapter pxAdapter)
{
if(Base.Document.Current != null)
{
//Retrieves the sample .Zip file from the invoice
Guid zipFile = PXNoteAttribute.GetFileNotes(Base.Document.Cache, Base.Document.Current).First();
@jvanhoesen
jvanhoesen / ZipArchiveRetrieve.cs
Created Mar 28, 2022
Retrieving a file from ZipArchive
View ZipArchiveRetrieve.cs
//Creates a stream from the specified file within the .ZIP file
using (Stream fileStream = zip.OpenRead(info.Name))
{
using(MemoryStream innerStream = new MemoryStream())
{
fileStream.CopyTo(innerStream);
//Create Acumatica file from .Zip file content
fileInfo = new PX.SM.FileInfo(info.Name, null, innerStream.ToArray());
@jvanhoesen
jvanhoesen / ZipArchiveIterate.cs
Created Mar 28, 2022
Iterate through file names in Archive
View ZipArchiveIterate.cs
//Iterate through all files and retrieve each files ItemInfo containing it's name
foreach (ZipArchive.ItemInfo info in zip.GetFiles())
{
//Use file name to retrieve file from .Zip Archive
}
@jvanhoesen
jvanhoesen / ZipArchiveOpen.cs
Created Mar 28, 2022
Open existing .Zip file
View ZipArchiveOpen.cs
//Retrieving .ZIP file attached to Acumatica document
PX.SM.FileInfo fileInfo = uploadFileMaint.GetFile(zipFile);
if(fileInfo != null)
{
//Create a new memory stream from .ZIP file data
using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
{
//Create new instance of ZipArchive to interact with .ZIP file
using (ZipArchive zip = ZipArchive.CreateFrom(stream, true))
{
@jvanhoesen
jvanhoesen / FileAddition.cs
Created Feb 2, 2022
Addition of file to created .Zip archive
View FileAddition.cs
using (ZipArchive zip = ZipArchive.CreateFrom(zipStream, false))
{
using (MemoryStream fileStream = new MemoryStream())
{
fileStream.Seek(0, SeekOrigin.Begin);
file.Write(fileStream);
fileStream.Seek(0, SeekOrigin.Begin);
View ZipArchiveCreate.cs
using (MemoryStream zipStream = new MemoryStream())
{
zipStream.Seek(0, SeekOrigin.Begin);
//2nd Parameter determines if .Zip is read only
using (ZipArchive zip = ZipArchive.CreateFrom(zipStream, false))
{
//Read or write data
}
}
@jvanhoesen
jvanhoesen / Prefetch.cs
Created Jun 2, 2021
Prefetch default implementation vs advanced implementation
View Prefetch.cs
//Default implementation
public void Prefetch()
{
_InventoryItems.Clear();
foreach (InventoryItem item in PXDatabase.SelectRecords<InventoryItem>())
{
_InventoryItems[item.InventoryID.Value] = item;
}
}
@jvanhoesen
jvanhoesen / AAInventoryItemCache.cs
Created Jun 2, 2021
Updated IPrefetchable implementation utilizing AACache
View AAInventoryItemCache.cs
internal sealed class AAInventoryItemCache : IPrefetchable
{
private Dictionary<int, InventoryItem> _InventoryItems = new Dictionary<int, InventoryItem>();
public void Prefetch()
{
_InventoryItems.Clear();
foreach (InventoryItem item in AACache<InventoryItem>.SelectRecords())
{
@jvanhoesen
jvanhoesen / Properties.cs
Last active Jun 2, 2021
Property declaration of AACache
View Properties.cs
private static CacheStaticInfo _staticInfo;
private static Type _pXCacheExtensionCollectionType;
protected static CacheStaticInfo StaticInfo
{
get
{
if (_staticInfo == null)
{