public ActionResult Import(EditPurchaseImportDataMappingsViewModel model, string command)
        {
            if (command.ToLower() == "import")
            {
                var error = PurchasingService.ValidateOrders(CurrentCompany, CurrentUser, model.Data.Headings);
                if (error.IsError)
                {
                    prepareViewModel(model);
                    model.Data = FileImportService.GetData(CurrentCompany, CurrentUser);
                    model.SetError(ErrorIcon.Error, error.Message);

                    return(View("ImportMappings", model));
                }
                else
                {
                    error = PurchasingService.ImportOrders(CurrentCompany,
                                                           CurrentUser,
                                                           model.LocationId,
                                                           model.Data.Headings);
                    if (error.IsError)
                    {
                        prepareViewModel(model);
                        model.Data = FileImportService.GetData(CurrentCompany, CurrentUser);
                        model.SetError(ErrorIcon.Error, error.Message);

                        return(View("ImportMappings", model));
                    }
                    else
                    {
                        // Successfully imported, so redirect to purchase orders screen
                        return(RedirectToAction("Purchases", "Purchasing", new { Area = "Purchasing" }));
                    }
                }
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
Beispiel #2
0
        public void ImportOrdersTest()
        {
            var testUser = GetTestUser();
            //var testCompany = GetTestCompany(testUser, true);
            var testCompany = GetTestCompanyAU();

            List <string> headings = new List <string>();

            headings.Add("ProductCode");
            headings.Add("UnitPrice");
            headings.Add("Quantity");
            headings.Add("SupplierName");

            // Create a temporary file of test data
            string tempFile = Path.GetTempFileName();

            var productList = db.FindProducts()
                              .Where(p => p.PrimarySupplierId != null)
                              .ToList();

            using (var sw = new StreamWriter(tempFile)) {
                // Write a random number of products
                string temp = "";
                for (int i = 0; i < headings.Count(); i++)
                {
                    if (!string.IsNullOrEmpty(temp))
                    {
                        temp += ",";
                    }
                    temp += headings[i];
                }
                sw.WriteLine(temp);

                for (int i = 0; i < 25; i++)
                {
                    int rnd     = RandomInt(0, productList.Count() - 1);
                    var product = productList[rnd];

                    string line = "\"" + product.ItemNumber + "\",";
                    line += RandomInt(1, 25).ToString() + ",";
                    line += rnd.ToString() + ",\"";
                    line += product.Supplier.Name + "\"";
                    sw.WriteLine(line);

                    productList.RemoveAt(rnd);      // So we don't get the same product again
                }
            }

            // Now import the file into the database
            FileImportService.FileImportService fis = new FileImportService.FileImportService(db);

            var error = fis.UploadFile(testCompany, testUser, tempFile, true);

            Assert.IsTrue(!error.IsError, error.Message);

            // Validate the items
            error = PurchasingService.ValidateOrders(testCompany, testUser, headings);
            Assert.IsTrue(!error.IsError, error.Message);

            // Now create orders
            error = PurchasingService.ImportOrders(testCompany, testUser, testCompany.DefaultLocationID.Value, headings);
            Assert.IsTrue(!error.IsError, error.Message);
        }