134 lines
3.6 KiB
C#
134 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using Humanizer;
|
|
using InventoryTraker.Web.Core;
|
|
using InventoryTraker.Web.Data;
|
|
using InventoryTraker.Web.Identity;
|
|
using InventoryTraker.Web.Utilities;
|
|
using Microsoft.AspNet.Identity;
|
|
using Microsoft.AspNet.Identity.EntityFramework;
|
|
|
|
namespace InventoryTraker.Web
|
|
{
|
|
public static class SeedData
|
|
{
|
|
public static void Init()
|
|
{
|
|
using (var context = new AppDbContext())
|
|
{
|
|
if (!context.Users.Any())
|
|
{
|
|
var manager = new ApplicationUserManager(new UserStore<User>(context));
|
|
manager.Create(new User
|
|
{
|
|
Email = "james.kolpack@gmail.com",
|
|
UserName = "James Kolpack",
|
|
}, "hgBdFiJTK");
|
|
|
|
manager.Create(new User
|
|
{
|
|
Email = "bbanegas@ethra.org",
|
|
UserName = "Brandi Banegas",
|
|
}, "3v9qxe");
|
|
|
|
manager.Create(new User
|
|
{
|
|
Email = "ccecil@ethra.org",
|
|
UserName = "Cyndie Cecil",
|
|
}, "95kdsxa");
|
|
|
|
manager.Create(new User
|
|
{
|
|
Email = "knorton@ethra.org",
|
|
UserName = "Kay Norton",
|
|
}, "rt9pmz1");
|
|
}
|
|
|
|
if (!context.Inventories.Any())
|
|
{
|
|
AddInventoryTypes(context);
|
|
|
|
context.SaveChanges();
|
|
|
|
AddInventory(context);
|
|
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void AddInventoryTypes(AppDbContext context)
|
|
{
|
|
var folder = HttpContext.Current.Server.MapPath("~/App_Data");
|
|
var inventoryTypeFile = Path.Combine(folder, "InventoryTypeSeedData.xlsx");
|
|
var parser = new InventoryTypeParser(new FileInfo(inventoryTypeFile));
|
|
foreach (var inventoryType in parser.Parse())
|
|
{
|
|
context.InventoryTypes.Add(inventoryType);
|
|
}
|
|
}
|
|
|
|
private static void AddInventory(AppDbContext context)
|
|
{
|
|
var r = new Random(1);
|
|
var inventoryTypes = context.InventoryTypes.ToList();
|
|
var cityNames = new List<string> {"Maryville", "Wartburg", "Clinton", "Oak Ridge", "Alcoa", "Jellico"};
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
var inventoryType = inventoryTypes.ElementAt(r.Next(0, context.InventoryTypes.Count()));
|
|
var addedDate = DateTime.Today.AddMonths(-r.Next(1, 24));
|
|
var expiration = addedDate.AddMonths(r.Next(12, 48));
|
|
var memo = "New " + inventoryType.Name;
|
|
var quantity = r.Next(5, 112);
|
|
|
|
var previousTransaction = new Transaction
|
|
{
|
|
AddedQuantity = quantity, Memo = "Arrival", CurrentQuantity = quantity, TransactionDate = addedDate, Timestamp = addedDate
|
|
};
|
|
var inventory = new Inventory
|
|
{
|
|
InventoryType = inventoryType,
|
|
ExpirationDate = expiration,
|
|
AddedDate = addedDate,
|
|
Memo = memo,
|
|
Quantity = quantity,
|
|
Transactions = new List<Transaction> { previousTransaction}
|
|
};
|
|
context.Inventories.Add(inventory);
|
|
|
|
for (int j = 0; j < 5 && previousTransaction.CurrentQuantity > 0; j++)
|
|
{
|
|
var transactionDate = previousTransaction.TransactionDate.AddDays(r.Next(1, 100));
|
|
if (transactionDate >= DateTime.Today)
|
|
break;
|
|
|
|
var quantityRemoved = r.Next(1, 100);
|
|
if (quantityRemoved > previousTransaction.CurrentQuantity)
|
|
quantityRemoved = previousTransaction.CurrentQuantity;
|
|
|
|
var destCity = cityNames.ElementAt(r.Next(0, cityNames.Count));
|
|
|
|
var transaction = new Transaction
|
|
{
|
|
RemovedQuantity = quantityRemoved,
|
|
CurrentQuantity = previousTransaction.CurrentQuantity - quantityRemoved,
|
|
Inventory = inventory,
|
|
Memo = $"Distributed to {destCity}",
|
|
TransactionDate = transactionDate,
|
|
Timestamp = transactionDate
|
|
};
|
|
|
|
inventory.Quantity = transaction.CurrentQuantity;
|
|
|
|
inventory.Transactions.Add(transaction);
|
|
|
|
previousTransaction = transaction;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |