public void FindPurchaseOrderHeaderByPONumberModelTest()
        {
            var testUser    = GetTestUser();
            var testCompany = GetTestCompany(testUser, true);

            // Create a record
            var poh1 = GetTestPurchaseOrderHeader(testCompany, testUser, 1);

            // Try to find it using the PONumber
            var poh2 = PurchasingService.FindPurchaseOrderHeaderByPONumberModel(poh1.OrderNumber.Value, testCompany);

            Assert.IsTrue(poh2 != null, "Error: A non-NULL value was expected");

            // Check thiat it is the same record
            decimal expected = poh1.OrderNumber.Value,
                    actual   = poh2.OrderNumber.Value;

            Assert.IsTrue(expected == actual, $"Error: {actual} was returned when {expected} was expected");

            expected = poh1.Id;
            actual   = poh2.Id;
            Assert.IsTrue(expected == actual, $"Error: {actual} was returned when {expected} was expected");

            PurchasingService.DeletePurchaseOrderHeader(poh1.Id);

            // Now try to find it again
            poh2 = PurchasingService.FindPurchaseOrderHeaderByPONumberModel(poh1.OrderNumber.Value, testCompany);
            Assert.IsTrue(poh2 == null, "Error: A NULL value was expected but an object was returned");
        }
        void processFiles(FileTransferConfigurationModel config)
        {
            WriteTaskLog("Processing received files");

            foreach (var fileName in Directory.GetFiles(config.TargetFolder.TrimEnd('\\')))
            {
                bool   bError   = false;
                string errorMsg = "";

                // Open the file to get the purchase order number from the first data line
                CSVFileService.CSVReader reader = new CSVFileService.CSVReader();

                reader.OpenFile(fileName, true);
                Dictionary <string, string> firstLine = reader.ReadLine();
                reader.Close();

                int poNumber = -1;
                if (Int32.TryParse(firstLine["PO_NUMBER"], out poNumber))
                {
                    // Now we know what the PO Number is, attach the file to the purchase
                    // order notes/attachments so that it can be referenced in emails
                    CompanyModel company = new CompanyModel {
                        Id = config.CompanyId
                    };
                    var poh = PurchasingService.FindPurchaseOrderHeaderByPONumberModel(poNumber, company);
                    if (poh != null)
                    {
                        var note = new NoteModel();
                        bError = createNoteWithAttachment(poh, company, fileName, ref note, ref errorMsg);
                        if (!bError)
                        {
                            // A purchase order records who the 'sales person' was - actually the 'purchasor'.
                            // We need to email every user in the same team as the sales person to notify them
                            // of the slip file being received.

                            // To do this, we need to find what user groups the sales person is in and specifically
                            // look for one which has the name of the brand category of the order in it.
                            // We then email all users in that group. This ensures that when an order is received,
                            // the sales person's collegues are informed so that we don't create a situation where
                            // only one person knows about an order.

                            var userList = PurchasingService.FindOrderPurchasers(poh,
                                                                                 company,
                                                                                 poNumber,
                                                                                 ref errorMsg);
                            if (userList != null)
                            {
                                // Send emails to all the users in the list
                                var error = sendUnpackSlipMessage(poh, company, note, poNumber, userList);
                                if (error.IsError)
                                {
                                    errorMsg = error.Message;
                                    bError   = true;
                                }
                            }
                            else
                            {
                                bError = true;
                            }
                        }
                    }
                    else
                    {
                        errorMsg = $"Error: Failed to find the PurchaseOrderHeaderRecord corresponding to Order Number {poNumber} !";
                        bError   = true;
                    }
                }
                else
                {
                    errorMsg = $"Error: Failed to read PO_NUMBER column from '{fileName}' !";
                    bError   = true;
                }

                if (bError)
                {
                    // Failed to process the file so move it to the error folder
                    // Move the file to an error location.
                    // It may not exist if note/attachment creation has already moved it.
                    // It can be moved back in for later re-processing.
                    WriteTaskLog(errorMsg, LogSeverity.Severe);

                    string moveTarget = fileName.FolderName() + "\\Errors";
                    FileManagerService.FileManagerService.DeleteFile(moveTarget + "\\" + fileName.FileName());
                    var error = FileManagerService.FileManagerService.MoveFile(fileName, moveTarget, false);
                    if (error.IsError)
                    {
                        WriteTaskLog(error.Message, LogSeverity.Severe);
                    }
                }
            }
        }