114 lines
4.3 KiB
C#
114 lines
4.3 KiB
C#
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Data.Entities;
|
|
using Indotalent.Data.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Infrastructure.Database.Demo;
|
|
|
|
public static class StockCountSeeder
|
|
{
|
|
public static async Task GenerateDataAsync(AppDbContext context)
|
|
{
|
|
if (await context.StockCount.AnyAsync()) return;
|
|
|
|
var random = new Random();
|
|
var stockCountStatusValues = Enum.GetValues(typeof(StockCountStatus)).Cast<StockCountStatus>().ToList();
|
|
var entityName = nameof(StockCount);
|
|
var ivtEntityName = nameof(InventoryTransaction);
|
|
|
|
var products = await context.Product
|
|
.Where(x => x.Physical == true)
|
|
.ToListAsync();
|
|
|
|
var warehouses = await context.Warehouse
|
|
.Where(x => x.SystemWarehouse == false)
|
|
.Select(x => x.Id)
|
|
.ToListAsync();
|
|
|
|
if (!products.Any() || !warehouses.Any()) return;
|
|
|
|
var dateFinish = DateTime.Now;
|
|
var dateStart = new DateTime(dateFinish.AddMonths(-12).Year, dateFinish.AddMonths(-12).Month, 1);
|
|
|
|
for (DateTime date = dateStart; date < dateFinish; date = date.AddMonths(1))
|
|
{
|
|
DateTime[] transactionDates = GetRandomDays(date.Year, date.Month, 6);
|
|
|
|
foreach (var transDate in transactionDates)
|
|
{
|
|
var autoNo = await context.GenerateAutoNumberAsync(
|
|
entityName: entityName,
|
|
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/"
|
|
);
|
|
|
|
var stockCount = new StockCount
|
|
{
|
|
AutoNumber = autoNo,
|
|
CountDate = transDate,
|
|
Status = stockCountStatusValues[random.Next(stockCountStatusValues.Count)],
|
|
WarehouseId = GetRandomValue(warehouses, random),
|
|
};
|
|
await context.StockCount.AddAsync(stockCount);
|
|
|
|
int numberOfProducts = random.Next(3, 6);
|
|
for (int i = 0; i < numberOfProducts; i++)
|
|
{
|
|
var product = products[random.Next(products.Count)];
|
|
|
|
var stock = context.GetStock(stockCount.WarehouseId, product.Id);
|
|
var qtyCount = stock + (double)random.Next(-10, 10);
|
|
|
|
if (qtyCount <= 0.0)
|
|
{
|
|
if (qtyCount < 0.0)
|
|
{
|
|
qtyCount = Math.Abs(qtyCount) * 2.0;
|
|
}
|
|
else if (qtyCount == 0.0)
|
|
{
|
|
qtyCount = qtyCount + (double)random.Next(40, 50);
|
|
}
|
|
}
|
|
|
|
var autoNoIVT = await context.GenerateAutoNumberAsync(
|
|
entityName: ivtEntityName,
|
|
prefixTemplate: $"{ivtEntityName.ToShortNameConsonant(3)}/{{Year}}/"
|
|
);
|
|
|
|
var inventoryTransaction = new InventoryTransaction
|
|
{
|
|
ModuleId = stockCount.Id,
|
|
ModuleName = entityName,
|
|
ModuleCode = "COUNT",
|
|
ModuleNumber = stockCount.AutoNumber,
|
|
MovementDate = stockCount.CountDate!.Value,
|
|
Status = (InventoryTransactionStatus)stockCount.Status,
|
|
AutoNumber = autoNoIVT,
|
|
WarehouseId = stockCount.WarehouseId,
|
|
ProductId = product.Id,
|
|
QtySCCount = qtyCount,
|
|
};
|
|
|
|
context.CalculateInvenTrans(inventoryTransaction);
|
|
await context.InventoryTransaction.AddAsync(inventoryTransaction);
|
|
}
|
|
}
|
|
}
|
|
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
private static DateTime[] GetRandomDays(int year, int month, int count)
|
|
{
|
|
var random = new Random();
|
|
var daysInMonth = DateTime.DaysInMonth(year, month);
|
|
return Enumerable.Range(1, count)
|
|
.Select(_ => new DateTime(year, month, random.Next(1, daysInMonth + 1)))
|
|
.ToArray();
|
|
}
|
|
|
|
private static T GetRandomValue<T>(IList<T> list, Random random)
|
|
{
|
|
return list[random.Next(list.Count)];
|
|
}
|
|
} |