Here the complete section of code.
internal static bool ExportPOBasedInvoice(SessionHolder holder, ApTransaction apTransaction, ExportResponse response, string batch, string customer)
{
int batchEnabled;
bool objectDropped = false;
SessionUtility.SetUIClass(holder.Session, "PO_ReceiptOfInvoice_Ui");
using (dynamic receiptInvoice = SessionUtility.NewObject(holder, "PO_Receipt_bus"))
{
batchEnabled = receiptInvoice.nBatchEnabled;
bool recordCreated = false;
string receiptNumber = "";
try
{
SetReceiptHeader(holder, apTransaction, receiptInvoice, ref receiptNumber, ref recordCreated, batch, taxEnabled);
string MASVer = "";
holder.Session.nGetParameter("SYS", "Version$", ref MASVer);
bool sageSupportMultiplePO = !MASVer.StartsWith("6.1") && !MASVer.StartsWith("6.0");
if (sageSupportMultiplePO)
{
ApplyPOs(receiptInvoice, apTransaction);
}
else
{
SessionUtility.SetPOToInvoice(receiptInvoice,apTransaction.purchaseOrders[0]);
}
SessionUtility.WriteRecord(receiptInvoice);
response.response.content = "OK";
response.responseCode = FinSysResponseCode.OK;
receiptInvoice.DropObject();
objectDropped = true;
if (DoPosting(batchEnabled, apTransaction))
{
HandlePosting(holder, apTransaction, "PO_ReceiptRegister");
}
}
catch (Sage100Exception e)
{
Logger.Error("Export of invoice failed", e);
try
{
HandleBrokenReceiptRecord(recordCreated, receiptNumber, receiptInvoice);
}
catch (Exception e2)
{
Logger.Error("There was a problem deleting the broken record", e2);
}
if (!objectDropped)
{
receiptInvoice.DropObject();
}
response.responseCode = FinSysResponseCode.Error;
}
}
return batchEnabled == 1;
}
private static void SetReceiptHeader(SessionHolder holder, ApTransaction apTransaction, dynamic receiptInvoice, ref string receiptNumber, ref bool recordCreated, string batch, bool taxEnabled)
{
recordCreated = SetReceiptKeys(receiptInvoice, ref receiptNumber, batch);
Dictionary<string, object> values = new Dictionary<string, object>()
{
{ "PurchaseOrderNo$", apTransaction.purchaseOrders[0]},
{ "InvoiceNo$", apTransaction.transactionId },
};
SessionUtility.SetValues(receiptInvoice, values);
}
private static bool SetReceiptKeys(dynamic receiptInvoice, ref string receiptNumber, string batch)
{
receiptNumber = SessionUtility.GetNextReceiptNo(receiptInvoice);
Dictionary<string, string> keysValues = new Dictionary<string, string>()
{
{ "ReceiptType$", "I"},
{ "ReceiptNo$", receiptNumber},
};
int batchEnabled = receiptInvoice.nBatchEnabled;
if (batchEnabled == 1)
{
SessionUtility.SetKeysWithBatch(receiptInvoice, keysValues, batch);
}
else
{
SessionUtility.SetKeys(receiptInvoice, keysValues);
}
return true;
}
private static void HandleBrokenReceiptRecord(bool recordCreated, string receiptNumber, dynamic receipts)
{
if (recordCreated)
{
Dictionary<string, string> keysValues = new Dictionary<string, string>()
{
{ "ReceiptType$", "I"},
{ "ReceiptNo$", receiptNumber},
};
SessionUtility.HandleBrokenRecord(receipts, keysValues);
}
}
public static void SetValues(dynamic businessObject, Dictionary<string, object> values, bool write = false)
{
foreach (string column in values.Keys)
{
ExecuteMethodIntegerReturn(businessObject, (FunctionIntegerReturn)(() => { return businessObject.nSetValue(column, values[column]); }),
"setting column " + column + " with value " + values[column]);
}
if (write)
{
WriteRecord(businessObject);
}
}
public static void WriteRecord(dynamic record)
{
ExecuteMethodIntegerReturn(record, (FunctionIntegerReturn)(() => { return record.nWrite(); }), "writing a new record to file");
}
private static void ApplyPOs(dynamic receiptInvoice, ApTransaction apTransaction)
{
string firstPO = apTransaction.purchaseOrders[0];
SessionUtility.ApplyPrimaryPOToReceipt(receiptInvoice, firstPO);
foreach (string poID in apTransaction.purchaseOrders)
{
if (poID.Equals(firstPO))
{
continue;
}
SessionUtility.ApplyPOToReceipt(receiptInvoice, poID);
}
SessionUtility.ApplyPurchaseOrderCopyLines(receiptInvoice);
}
internal static void SetPOToInvoice(dynamic receiptInvoice, string poID)
{
ExecuteMethodNoReturn(receiptInvoice, (FunctionVoidReturn)(() => { receiptInvoice.nCopyFromPurchaseOrder(poID); }), "copying purchase order's header data");
ExecuteMethodNoReturn(receiptInvoice, (FunctionVoidReturn)(() => { receiptInvoice.nCopyPurchaseOrderLines(0); }), "copying purchase order's lines to invoice");
}
------------------------------
Bob Osborn
Consultant
ACI Consulting
------------------------------
Original Message:
Sent: 05-10-2022 12:34
From: Robert Osborn
Subject: Clearing PO_ReceiptReturnMatRqByPO record via scripting
Here is what he sent to me:
This is a simplification of what my code does:
bool objectDropped=false;using (dynamic receiptInvoice = SessionUtility.NewObject(holder, "PO_Receipt_bus")) { try { //Setting the Keys ..... receiptInvoice.nSetKey(); //Setting other fields ............ receiptInvoice.DropObject(); objectDropped=true; } catch (Exception e) { //Setting the keys of the record (Same keys as in try) ......... receiptInvoice.nSetKey(); receiptInvoice.nDelete() if (!objectDropped) { receiptInvoice.DropObject(); } } }
------------------------------
Bob Osborn
Consultant
ACI Consulting
------------------------------
Original Message:
Sent: 05-10-2022 01:45
From: Alnoor Cassim
Subject: Clearing PO_ReceiptReturnMatRqByPO record via scripting
Let me start off by asking how do they delete the ROI Entry once it's in a bad state? IOW are they using the scripting Delete() method or another way? Also are they generating ROI Entries via a button script on a Sage screen or via BOI?
------------------------------
Alnoor Cassim
Email: alnoor@asifocus.com
Ph: