initial commit
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Data.Entities;
|
||||
using Indotalent.Data.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Infrastructure.Database.Demo;
|
||||
|
||||
public static class TransferOutSeeder
|
||||
{
|
||||
public static async Task GenerateDataAsync(AppDbContext context)
|
||||
{
|
||||
if (await context.TransferOut.AnyAsync()) return;
|
||||
|
||||
var random = new Random();
|
||||
var transferStatusValues = Enum.GetValues(typeof(TransferStatus)).Cast<TransferStatus>().ToList();
|
||||
var entityName = nameof(TransferOut);
|
||||
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.Count < 2) 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))
|
||||
{
|
||||
var transactionDates = GetRandomDays(date.Year, date.Month, 6);
|
||||
|
||||
foreach (DateTime transDate in transactionDates)
|
||||
{
|
||||
var fromId = GetRandomValue(warehouses, random);
|
||||
var toId = GetRandomValue(warehouses.Where(x => x != fromId).ToList(), random);
|
||||
|
||||
var autoNo = await context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/"
|
||||
);
|
||||
|
||||
var transferOut = new TransferOut
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
TransferReleaseDate = transDate,
|
||||
Status = transferStatusValues[random.Next(transferStatusValues.Count)],
|
||||
WarehouseFromId = fromId,
|
||||
WarehouseToId = toId,
|
||||
};
|
||||
await context.TransferOut.AddAsync(transferOut);
|
||||
|
||||
int numberOfProducts = random.Next(3, 6);
|
||||
for (int i = 0; i < numberOfProducts; i++)
|
||||
{
|
||||
var product = GetRandomValue(products, random);
|
||||
|
||||
var autoNoIVT = await context.GenerateAutoNumberAsync(
|
||||
entityName: ivtEntityName,
|
||||
prefixTemplate: $"{ivtEntityName.ToShortNameConsonant(3)}/{{Year}}/"
|
||||
);
|
||||
|
||||
var inventoryTransaction = new InventoryTransaction
|
||||
{
|
||||
ModuleId = transferOut.Id,
|
||||
ModuleName = entityName,
|
||||
ModuleCode = "TO-OUT",
|
||||
ModuleNumber = transferOut.AutoNumber,
|
||||
MovementDate = transferOut.TransferReleaseDate!.Value,
|
||||
Status = (InventoryTransactionStatus)transferOut.Status,
|
||||
AutoNumber = autoNoIVT,
|
||||
WarehouseId = transferOut.WarehouseFromId,
|
||||
ProductId = product.Id,
|
||||
Movement = (double)random.Next(1, 10)
|
||||
};
|
||||
|
||||
context.CalculateInvenTrans(inventoryTransaction);
|
||||
await context.InventoryTransaction.AddAsync(inventoryTransaction);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static T GetRandomValue<T>(List<T> list, Random random)
|
||||
{
|
||||
return list[random.Next(list.Count)];
|
||||
}
|
||||
|
||||
private static DateTime[] GetRandomDays(int year, int month, int count)
|
||||
{
|
||||
var random = new Random();
|
||||
int daysInMonthCount = DateTime.DaysInMonth(year, month);
|
||||
var daysInMonth = Enumerable.Range(1, daysInMonthCount).ToList();
|
||||
|
||||
var selectedDays = new List<int>();
|
||||
for (int i = 0; i < count && daysInMonth.Count > 0; i++)
|
||||
{
|
||||
int index = random.Next(daysInMonth.Count);
|
||||
selectedDays.Add(daysInMonth[index]);
|
||||
daysInMonth.RemoveAt(index);
|
||||
}
|
||||
|
||||
return selectedDays.Select(day => new DateTime(year, month, day)).ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user