Prepaid Invoice Requests

SOLVED

Our developers have written an interface to pull and push AR customer data.  Invoices (IN) are not a problem, but prepaid invoices (PP) produce no results and no errors. I'm pasting the code below.  Any ideas?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LASO.SageConnection
{
class SageInvoiceLookup : IDisposable
{
private const int k_error = 0;
private const int k_found_invoice = 1;
private const int k_new_invoice = 2;
private const string k_invoice = "IN";
private const string k_creditMemo = "CM";
private const string k_prepayment = "PP";

private bool _disposed = false;
private DispatchObject _pvx = null;
private DispatchObject _session = null;
private SageConstants _sageConstants = null;
private DispatchObject _invoice = null;
private SageInvoiceItem _invoiceDetails = null;
private SageCustomerLookup _customer = null;

private List<string> _keyFields = new List<string> { "InvoiceType$", "InvoiceNo$" };

public SageInvoiceLookup(DispatchObject session, DispatchObject pvx, SageConstants sageConstants)
{
_session = session;
_pvx = pvx;
_sageConstants = sageConstants;

ActivateModule();
CreateInvoiceActiveRecord();

// Set Custom Fields

}

~SageInvoiceLookup()
{
Dispose(false);
}

public bool InvoiceExists(int invoiceId, InvoiceType invoiceType)
{
var typeOfInvoice = InvoiceTypeValue(invoiceType);
return RecordExists(invoiceId,typeOfInvoice);
}

private string InvoiceTypeValue(InvoiceType invoiceType)
{
var result = string.Empty;
switch (invoiceType)
{
case InvoiceType.CreditMemo: result = "CM"; break;
case InvoiceType.Inovice: result = "IN"; break;
case InvoiceType.Prepayment: result = "PP"; break;
}

return result;
}

private void ActivateModule()
{
try
{
int taskId = (int)_session.InvokeMethod("nLookupTask", "AR_Customer_ui");
_session.InvokeMethod("nSetProgram", taskId);
}
catch (Exception ex)
{
var message = string.Format("Sage Error: [Set Program: A/R Customer] Message: {0}", _pvx.GetProperty("sLastErrorMsg").ToString());
var sage_ex = new SageConnectionException(message);
throw (sage_ex);
}
}

private int FindRecord(int invoiceId, string invoiceType)
{
SetField("InvoiceType$", invoiceType);
SetField("InvoiceNo$", invoiceId.ToString("D7"));
int recordType = (int)_invoice.InvokeMethod("nFind");

LASO.Logging.EventLog.LogInformation($"InvoiceId {invoiceId:D7} InvoiceType: {invoiceType} Results: {recordType}");

if (recordType == 0)
{
var invoiceError = _invoice.GetProperty("sLastErrorMsg").ToString();
var message = string.Format("Invoice Lookup Error: {0}", invoiceError);
LASO.Logging.EventLog.LogInformation(message);

}
return recordType;
}

private void CreateInvoiceActiveRecord()
{
try
{
_invoice = new DispatchObject(_pvx.InvokeMethod("NewObject", "AR_Invoice_svc", _session.GetObject()));

if (_invoice == null)
{
ReportConnectionError();
}
}
catch (Exception ex)
{
ReportConnectionError(ex);
}
}

private bool RecordExists(int invoiceId, string invoiceType)
{
try
{
var invoiceStatus = FindRecord(invoiceId, invoiceType);
var result = invoiceStatus.Equals(k_found_invoice);

LASO.Logging.EventLog.LogInformation($"Record Exists: ID {invoiceId} type: {invoiceType} Result: {result}");

return result;
}
catch (Exception ex)
{
var invoiceError = _invoice.GetProperty("sLastErrorMsg").ToString();
var message = string.Format("Invoice Lookup: {0}", invoiceError);
throw new SageInvoiceException(message, ex);
}

}

private void InitializeInvoiceLinesObject()
{
_invoiceDetails = new SageInvoiceItem(_invoice,_sageConstants);
}

public string InvoiceNo
{
get { return _invoice.GetProperty("InvoiceNo$").ToString(); }
}

private void SetField(string field, string value)
{
string fn = GetSetFunction(field);
string fieldName = FormatFieldName(field);

if (string.IsNullOrWhiteSpace(value))
{
value = string.Empty;
}

try
{
var retVal = (int)_invoice.InvokeMethod(fn, fieldName, value);
if (retVal == 1) return;

ReportError(field, value);
}
catch (Exception ex)
{
ReportError(field, value, ex);
}

}

private string GetSetFunction(string field)
{
return _keyFields.Contains(field) ? "nSetKeyValue" : "nSetValue";
}

private string FormatFieldName(string field)
{
return (field.EndsWith("$")) ? field : string.Format("{0}$", field);
}

private void ReportConnectionError(Exception ex = null)
{
var message = string.Format("Sage Error: [Module: A/R] [Function: NewObject Class: AR_Invoice_svc] Message: {0}", _pvx.GetProperty("sLastErrorMsg").ToString());
var cex = (ex == null) ? new SageConnectionException(message) : new SageConnectionException(message, ex);
throw (cex);

}

private void ReportError(string field, string value, Exception ex = null)
{
var fieldNames = MakeFieldMap();
var fieldName = fieldNames[field];
var error = _invoice.GetProperty("sLastErrorMsg").ToString();
string message = string.Format("Error setting the customer {0} to {1}: {2}", fieldName, value, error);

var exception = (ex == null) ? new SageCustomerException(message) : new SageCustomerException(message, ex);
exception.FieldName = fieldNames[field];
exception.Value = value;

throw (exception);

}
private Dictionary<string, string> MakeFieldMap()
{
var map = new Dictionary<string, string>();
map.Add("InvoiceNo$", "Invoice Number");
map.Add("CustomerNo$", "Dealer Account #");
map.Add("TaxSchedule$", "Tax Schedule");
map.Add("SalespersonNo$", "Sales Person");
map.Add("TermsCode$", "Terms");
return map;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

if (disposing)
{
releaseResource(_invoice);
releaseResource(_session);
releaseResource(_pvx);
}

_disposed = true;
}

private void releaseResource(DispatchObject resource)
{
if (resource == null) return;
if (resource.GetObject() == null) return;
resource.Dispose();
}

}
}