| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.Database;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PRS.Shared;
- internal class Update_8_55 : DatabaseUpdateScript
- {
- public override VersionNumber Version => new(8, 55);
- public override bool Update()
- {
- Logger.Send(LogType.Information, "", $"Updating uninitialised stock movement TransferIDs");
- var transactionIDs = DbFactory.NewProvider(Logger.Main)
- .Query(
- Filter<StockMovement>.Where(x => x.Type).InList(StockMovementType.TransferOut, StockMovementType.TransferIn)
- .And(x => x.TransferID).IsEqualTo(null),
- Columns.None<StockMovement>()
- .Add(x => x.Transaction))
- .Rows
- .Select(x => x.Get<StockMovement, Guid>(x => x.Transaction))
- .ToHashSet();
- Logger.Send(LogType.Information, "", $"Found {transactionIDs.Count} uninitialised transactions");
- var total = transactionIDs.Count;
- while (transactionIDs.Count > 0)
- {
- Logger.Send(LogType.Information, "", $"Updating Stock Movements: {(double)(total - transactionIDs.Count) / total * 100:F2}%");
- var nIDs = Math.Min(transactionIDs.Count, 100);
- var ids = new Guid[nIDs];
- var i = 0;
- foreach(var id in transactionIDs)
- {
- if (i == nIDs) break;
- ids[i] = id;
- ++i;
- }
- foreach(var id in ids)
- {
- transactionIDs.Remove(id);
- }
- var movementsTable = DbFactory.NewProvider(Logger.Main)
- .Query(
- Filter<StockMovement>.Where(x => x.Transaction).InList(ids)
- .And(x => x.Type).InList(StockMovementType.TransferOut, StockMovementType.TransferIn)
- .And(x => x.TransferID).IsEqualTo(null),
- Columns.None<StockMovement>()
- .Add(x => x.ID)
- .Add(x => x.Transaction)
- .Add(x => x.Type)
- .Add(x => x.Date)
- .Add(x => x.System));
- var movements =
- movementsTable
- .ToObjects<StockMovement>()
- .GroupByDictionary(x => x.Transaction);
-
- Logger.Send(LogType.Information, "", $"Updating {movementsTable.Rows.Count} movements");
- var updates = new List<StockMovement>();
- foreach(var (transaction, transactionMovements) in movements)
- {
- transactionMovements.SortBy(x => x.Date);
- while(transactionMovements.Count > 0)
- {
- var movement = transactionMovements[0];
- transactionMovements.RemoveAt(0);
- var otherType = movement.Type == StockMovementType.TransferOut ? StockMovementType.TransferIn : StockMovementType.TransferOut;
- var transfer = Guid.NewGuid();
- movement.TransferID = transfer;
- var first = transactionMovements.FirstOrDefault(x => x.Type == otherType);
- if(first is not null)
- {
- first.TransferID = transfer;
- transactionMovements.Remove(first);
- updates.Add(movement);
- updates.Add(first);
- }
- }
- }
- DbFactory.NewProvider(Logger.Main).Save(updates);
- }
- Logger.Send(LogType.Information, "", $"Finished updating uninitialised stock movement TransferIDs");
- return true;
- }
- }
|