//---------------------------------------------------------------------------------------------
        /// <summary>
        /// Updates the record in the VAT100 table
        /// </summary>
        /// <param name="aVATRecord"></param>
        /// <returns></returns>
        public int UpdateVAT100Entry(VAT100Record aVATRecord, string ForCompanyCode)
        {
            IVAT100 oVAT100 = null;

            int Res = 0;

            LogText("Updating DB record for CorrelationID " + aVATRecord.correlationID);

            // Find the record, based on Correlation ID
            string path = GetCompanyPath(ForCompanyCode);

            if (path == "")
            {
                return(12); // File not found
            }
            // PKR. 15/09/2015. ABSEXCH-16839. Close toolkit before changing path.
            if (tToolkit.Status == TToolkitStatus.tkOpen)
            {
                tToolkit.CloseToolkit();
            }
            tToolkit.Configuration.DataDirectory = path;
            Res = tToolkit.OpenToolkit();

            if (Res == 0)
            {
                try
                {
                    try
                    {
                        // Look for the entry to update
                        oVAT100 = (tToolkit as ICSNFToolkit).VAT100;
                        Res     = oVAT100.GetFirst();
                        while (Res == 0)
                        {
                            // See if it's the required record
                            if (oVAT100.vatCorrelationId == aVATRecord.correlationID)
                            {
                                // Matched
                                // Update the values
                                IVAT100 uRec = oVAT100.Update();

                                if (uRec != null)
                                {
                                    uRec.vatIRMark              = aVATRecord.IRMark;
                                    uRec.vatDateSubmitted       = aVATRecord.dateSubmitted;
                                    uRec.vatDocumentType        = aVATRecord.documentType;
                                    uRec.vatPeriod              = aVATRecord.VATPeriod;
                                    uRec.vatUserName            = aVATRecord.username;
                                    uRec.vatStatus              = aVATRecord.status;
                                    uRec.vatPollingInterval     = aVATRecord.pollingInterval;
                                    uRec.vatDueOnOutputs        = aVATRecord.VATDueOnOutputs;
                                    uRec.vatDueOnECAcquisitions = aVATRecord.VATDueOnECAcquisitions;
                                    uRec.vatTotal             = aVATRecord.VATTotal;
                                    uRec.vatReclaimedOnInputs = aVATRecord.VATReclaimedOnInputs;
                                    uRec.vatNet = aVATRecord.VATNet;
                                    uRec.vatNetSalesAndOutputs    = aVATRecord.netSalesAndOutputs;
                                    uRec.vatNetPurchasesAndInputs = aVATRecord.netPurchasesAndInputs;
                                    uRec.vatNetECSupplies         = aVATRecord.netECSupplies;
                                    uRec.vatNetECAcquisition      = aVATRecord.netECAcquisitions;
                                    uRec.vatHMRCNarrative         = aVATRecord.hmrcNarrative;
                                    uRec.vatNotifyEmail           = aVATRecord.notifyEmail;
                                    uRec.vatPollingURL            = aVATRecord.PollingURL;

                                    Res = uRec.Save();

                                    if (Res != 0)
                                    {
                                        LogText("Could not update VAT 100 database entry for Correlation ID " + aVATRecord.correlationID +
                                                ".\r\n" + tToolkit.LastErrorString);
                                    }
                                }
                                else
                                {
                                    LogText("Failed to create Update object.");
                                    // PKR. TODO: This needs updating to whatever value means that we couldn't get an Update object.
                                    Res             = -1;
                                    lastErrorString = "Could not update VAT 100 database entry for Correlation ID " + aVATRecord.correlationID;
                                }
                                // We found the record, so we don't need to look further
                                break;
                            } // if correct record

                            Res = oVAT100.GetNext();
                        } // while we have a record
                    }
                    catch (Exception ex)
                    {
                        lastErrorString = ex.Message;
                        LogText("Could not update VAT 100 database entry for Correlation ID " + aVATRecord.correlationID + ".\r\n" + ex.Message);
                    }
                }
                finally
                {
                    Res = tToolkit.CloseToolkit();
                }
            }
            else
            {
                LogText("UpdateVAT100Entry : Failed to open COM toolkit");
                lastErrorString = "UpdateVAT100Entry : Failed to open COM toolkit";
            }

            return(Res);
        }
        //---------------------------------------------------------------------------------------------
        /// <summary>
        /// Adds a VAT100 record to the database
        /// </summary>
        /// <param name="aVATRecord"></param>
        /// <returns></returns>
        public int AddVAT100Entry(VAT100Record aVATRecord, string ForCompanyCode)
        {
            IVAT100 oVAT100 = null;
            int     Res     = -1;

#if DEBUG
            Log.Add(string.Format("Adding record for [{0}] :", ForCompanyCode));
            Log.Add(string.Format(" Correlation ID : [{0}]", aVATRecord.correlationID));
            Log.Add(string.Format(" HMRC Narrative : [{0}]", aVATRecord.hmrcNarrative));
#endif
            string path = GetCompanyPath(ForCompanyCode);
            if (path == "")
            {
                return(12); // File not found
            }
            // PKR. 15/09/2015. ABSEXCH-16839. Close toolkit before changing path.
            if (tToolkit.Status == TToolkitStatus.tkOpen)
            {
                tToolkit.CloseToolkit();
            }
            tToolkit.Configuration.DataDirectory = path;
            Res = tToolkit.OpenToolkit();

            if (Res == 0)
            {
                try
                {
                    try
                    {
                        oVAT100 = (tToolkit as ICSNFToolkit).VAT100.Add();

                        oVAT100.vatCorrelationId       = aVATRecord.correlationID;
                        oVAT100.vatIRMark              = aVATRecord.IRMark;
                        oVAT100.vatDateSubmitted       = aVATRecord.dateSubmitted;
                        oVAT100.vatDocumentType        = aVATRecord.documentType;
                        oVAT100.vatPeriod              = aVATRecord.VATPeriod;
                        oVAT100.vatUserName            = aVATRecord.username;
                        oVAT100.vatStatus              = aVATRecord.status;
                        oVAT100.vatPollingInterval     = aVATRecord.pollingInterval;
                        oVAT100.vatDueOnOutputs        = aVATRecord.VATDueOnOutputs;
                        oVAT100.vatDueOnECAcquisitions = aVATRecord.VATDueOnECAcquisitions;
                        oVAT100.vatTotal             = aVATRecord.VATTotal;
                        oVAT100.vatReclaimedOnInputs = aVATRecord.VATReclaimedOnInputs;
                        oVAT100.vatNet = aVATRecord.VATNet;
                        oVAT100.vatNetSalesAndOutputs    = aVATRecord.netSalesAndOutputs;
                        oVAT100.vatNetPurchasesAndInputs = aVATRecord.netPurchasesAndInputs;
                        oVAT100.vatNetECSupplies         = aVATRecord.netECSupplies;
                        oVAT100.vatNetECAcquisition      = aVATRecord.netECAcquisitions;
                        oVAT100.vatNotifyEmail           = aVATRecord.notifyEmail;
                        oVAT100.vatPollingURL            = aVATRecord.PollingURL;
                        oVAT100.vatHMRCNarrative         = aVATRecord.hmrcNarrative;

                        Res = oVAT100.Save();

                        if (Res != 0)
                        {
                            LogText("An error occurred adding a record to the VAT 100 database.\r\n" + tToolkit.LastErrorString);
                        }
                    }
                    catch (Exception ex)
                    {
                        lastErrorString = ex.Message;
                        LogText("AddVAT100Entry : " + ex.Message);
                    }
                }
                finally
                {
                    Res = tToolkit.CloseToolkit();
                }
            }
            else
            {
                LogText("Failed to open COM toolkit");
                lastErrorString = "AddVAT100Entry : Failed to open COM toolkit";
            }

            return(Res);
        }