123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Core;
- using InABox.Database;
- namespace Comal.Stores
- {
- public class UserPlatform
- {
- public string UserID { get; set; }
- public string Platform { get; set; }
- public string Version { get; set; }
- }
- public static class PlatformCache
- {
- static PlatformCache()
- {
- Platforms = new List<UserPlatform>();
- }
- public static List<UserPlatform> Platforms { get; }
- }
- public class BaseStore<T> : Store<T> where T : Entity, new()
- {
- protected override Filter<T> PrepareFilter(Filter<T> filter)
- {
- CheckPlatformVersion();
- return base.PrepareFilter(filter);
- }
- protected override void BeforeSave(T entity)
- {
- CheckPlatformVersion();
- base.BeforeSave(entity);
- }
- protected override void BeforeDelete(T entity)
- {
- CheckPlatformVersion();
- base.BeforeDelete(entity);
- }
- private void CheckPlatformVersion()
- {
- if (string.IsNullOrEmpty(UserID) || string.IsNullOrEmpty(Platform) || string.IsNullOrEmpty(Version))
- return;
- var platform = PlatformCache.Platforms.FirstOrDefault(x => x.UserID.Equals(UserID) && x.Platform.Equals(Platform));
- if (platform == null)
- {
- platform = new UserPlatform { UserID = UserID, Platform = Platform, Version = "???" };
- PlatformCache.Platforms.Add(platform);
- }
- if (!platform.Version.Equals(Version))
- {
- platform.Version = Version;
- var prop = DatabaseSchema.Property(typeof(User), Platform);
- if (prop != null && prop.Getter() != null && prop.Setter() != null)
- {
- var user = Provider.Load(new Filter<User>(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault();
- if (user != null)
- {
- var value = prop.Getter().Invoke(user);
- if (!Version.Equals(value))
- {
- prop.Setter().Invoke(user, Version);
- Provider.Save(user);
- }
- }
- }
- }
- }
- protected void EnsureJobMaterials(ProductLink product, JobLink job, ProductStyleLink style, StockDimensions dimensions)
- {
- // if (product.ID == Guid.Empty || job.ID == Guid.Empty)
- // return;
- //
- // var jobmaterials = Provider.List(
- // new Filter<JobMaterial>(x => x.Product.ID).IsEqualTo(product.ID)
- // .And(x => x.Job.ID).IsEqualTo(job.ID)
- // .And(x => x.Style.ID).IsEqualTo(style.ID)
- // .And(x => x.Dimensions.Unit.ID).IsEqualTo(dimensions.Unit.ID)
- // .And(x => x.Dimensions.Length).IsEqualTo(dimensions.Length)
- // .And(x => x.Dimensions.Width).IsEqualTo(dimensions.Width)
- // .And(x => x.Dimensions.Height).IsEqualTo(dimensions.Height)
- // .And(x => x.Dimensions.Weight).IsEqualTo(dimensions.Weight)
- // ,
- // new Columns<JobMaterial>(x => x.ID)
- // );
- // if (!jobmaterials.Any())
- // {
- // var material = new JobMaterial();
- // material.Product.ID = product.ID;
- // material.Job.ID = job.ID;
- // material.Style.ID = style.ID;
- // material.Dimensions.CopyFrom(dimensions);
- // Provider.Save(material);
- // }
- }
-
- protected void CleanupJobMaterials(Guid id, ProductLink Product, JobLink Job, ProductStyleLink Style, StockDimensions Dimensions, bool delete)
- {
- // if (Product.ID == Guid.Empty || Job.ID == Guid.Empty)
- // return;
- //
- // var hasProduct = !delete && Product.HasOriginalValue(x=>x.ID);
- // var hasJob = !delete && Job.HasOriginalValue(x=>x.ID);
- // var hasStyle = !delete && Style.HasOriginalValue(x=>x.ID);
- // var hasUnit = !delete && Dimensions.HasOriginalValue(x=>x.Unit.ID);
- // var hasLength = !delete && Dimensions.HasOriginalValue(x => x.Length);
- // var hasWidth = !delete && Dimensions.HasOriginalValue(x => x.Width);
- // var hasHeight = !delete && Dimensions.HasOriginalValue(x => x.Height);
- // var hasWeight = !delete && Dimensions.HasOriginalValue(x => x.Weight);
- // if (delete || hasProduct || hasJob || hasStyle || hasUnit || hasLength || hasWidth || hasHeight || hasWeight)
- // {
- // var productid = hasProduct ? Product.GetOriginalValue(x=>x.ID) : Product.ID;
- // var jobid = hasJob ? Job.GetOriginalValue(x=>x.ID) : Job.ID;
- // var styleid = hasStyle ? Style.GetOriginalValue(x=>x.ID) : Style.ID;
- // var unitid = hasUnit ? Dimensions.GetOriginalValue(x=>x.Unit.ID) : Dimensions.Unit.ID;
- // var length = hasLength ? Dimensions.GetOriginalValue(x=>x.Length) : Dimensions.Length;
- // var width = hasWidth ? Dimensions.GetOriginalValue(x=>x.Width) : Dimensions.Width;
- // var height = hasHeight ? Dimensions.GetOriginalValue(x=>x.Height) : Dimensions.Height;
- // var weight = hasWeight ? Dimensions.GetOriginalValue(x=>x.Weight) : Dimensions.Weight;
- //
- // var bomtask = Task.Run(() =>
- // {
- // return Provider.List(
- // new Filter<JobBillOfMaterialsItem>(x => x.Product.ID).IsEqualTo(productid)
- // .And(x => x.Job.ID).IsEqualTo(jobid)
- // .And(x => x.Style.ID).IsEqualTo(styleid)
- // .And(x => x.ID).IsNotEqualTo(id)
- // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
- // .And(x => x.Dimensions.Length).IsEqualTo(length)
- // .And(x => x.Dimensions.Width).IsEqualTo(width)
- // .And(x => x.Dimensions.Height).IsEqualTo(height)
- // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
- // ,
- // new Columns<JobBillOfMaterialsItem>(x => x.ID)
- // ).Any();
- // });
- //
- // var reqtask = Task.Run(() =>
- // {
- // return Provider.List(
- // new Filter<JobRequisitionItem>(x => x.Product.ID).IsEqualTo(productid)
- // .And(x => x.Requisition.Job.ID).IsEqualTo(jobid)
- // .And(x => x.Style.ID).IsEqualTo(styleid)
- // .And(x => x.ID).IsNotEqualTo(id)
- // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
- // .And(x => x.Dimensions.Length).IsEqualTo(length)
- // .And(x => x.Dimensions.Width).IsEqualTo(width)
- // .And(x => x.Dimensions.Height).IsEqualTo(height)
- // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
- // ,
- // new Columns<JobRequisitionItem>(x => x.ID)
- // ).Any();
- // });
- //
- // var movetask = Task.Run(() =>
- // {
- // return Provider.List(
- // new Filter<StockMovement>(x => x.Product.ID).IsEqualTo(productid)
- // .And(x => x.Job.ID).IsEqualTo(jobid)
- // .And(x => x.Style.ID).IsEqualTo(styleid)
- // .And(x => x.ID).IsNotEqualTo(id)
- // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
- // .And(x => x.Dimensions.Length).IsEqualTo(length)
- // .And(x => x.Dimensions.Width).IsEqualTo(width)
- // .And(x => x.Dimensions.Height).IsEqualTo(height)
- // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
- // ,
- // new Columns<StockMovement>(x => x.ID)
- // ).Any();
- // });
- //
- // var potask = Task.Run(() =>
- // {
- // return Provider.List(
- // new Filter<PurchaseOrderItem>(x => x.ProductLink.ID).IsEqualTo(productid)
- // .And(x => x.Job.ID).IsEqualTo(jobid)
- // .And(x => x.StyleLink.ID).IsEqualTo(styleid)
- // .And(x => x.ID).IsNotEqualTo(id)
- // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
- // .And(x => x.Dimensions.Length).IsEqualTo(length)
- // .And(x => x.Dimensions.Width).IsEqualTo(width)
- // .And(x => x.Dimensions.Height).IsEqualTo(height)
- // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
- // ,
- // new Columns<PurchaseOrderItem>(x => x.ID)
- // ).Any();
- // });
- //
- // Task.WaitAll(bomtask, reqtask, movetask, potask);
- //
- // if (bomtask.Result || reqtask.Result || movetask.Result || potask.Result)
- // return;
- //
- // var remove = Provider.Query(
- // new Filter<JobMaterial>(x => x.Product.ID).IsEqualTo(productid)
- // .And(x => x.Job.ID).IsEqualTo(jobid)
- // .And(x => x.Style.ID).IsEqualTo(styleid)
- // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
- // .And(x => x.Dimensions.Length).IsEqualTo(length)
- // .And(x => x.Dimensions.Width).IsEqualTo(width)
- // .And(x => x.Dimensions.Height).IsEqualTo(height)
- // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
- // ,
- // new Columns<JobMaterial>(x => x.ID)
- // ).Rows.Select(x => x.ToObject<JobMaterial>());
- //
- // Provider.Delete(remove);
- // }
- }
-
- protected void UnlinkTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity)
- where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
- where TEntity : Entity
- where TEntityLink : IEntityLink<TEntity>, new()
- {
- var kanbans = Provider.Query(
- new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID).And(x => x.Kanban.Locked).IsEqualTo(true),
- new Columns<TEntityKanban>(
- x => x.Entity.ID,
- x => x.Kanban.ID,
- x => x.Kanban.Locked
- )
- );
- if (!kanbans.Rows.Any())
- return;
- var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
- kanban.Locked = false;
- Provider.Save(kanban);
- }
- protected void UpdateTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity, Func<TEntity, KanbanCategory> category)
- where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
- where TEntity : Entity
- where TEntityLink : IEntityLink<TEntity>, new()
- {
- var kanbans = Provider.Query(
- new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID),
- new Columns<TEntityKanban>(
- x => x.Entity.ID,
- x => x.Kanban.ID,
- x => x.Kanban.Category,
- x => x.Kanban.Closed,
- x => x.Kanban.Completed,
- x => x.Kanban.EmployeeLink.ID,
- x => x.Kanban.Notes,
- x => x.Kanban.DueDate,
- x => x.Kanban.Title,
- x => x.Kanban.Summary
- )
- );
- if (!kanbans.Rows.Any())
- return;
- var oldcategory = Kanban.StringToCategory(kanbans.Rows.First().Get<TEntityKanban, string>(c => c.Kanban.Category));
- var newcategory = category(entity);
- if (!Equals(newcategory, oldcategory))
- {
- var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
- kanban.Category = Kanban.CategoryToString(newcategory);
- var notes = kanban.Notes.ToList();
- notes.Add(string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, UserID, "Task Status updated by Requisition Progress"));
- kanban.Notes = notes.ToArray();
- kanban.Locked = true;
- FindSubStore<Kanban>().Save(kanban, "Updated due to Requisition Status Change");
- }
- }
-
- protected void NotifyEmployee<TEntity>(
- TEntity entity,
- Func<TEntity, Guid> employeeid,
- Func<TEntity, bool> filter,
- Func<TEntity, string> title,
- Func<TEntity, string> body
- ) where TEntity : Entity
- {
- if (!filter(entity))
- return;
- var employees = Provider.Query(
- new Filter<Employee>(x => x.ID).IsEqualTo(employeeid(entity)).Or(x => x.UserLink.ID).IsEqualTo(UserGuid),
- new Columns<Employee>(x => x.ID).Add(x => x.UserLink.ID).Add(x => x.Name)
- ).Rows.Select(r => new Tuple<Guid, Guid, string>(
- r.Get<Employee, Guid>(c => c.ID),
- r.Get<Employee, Guid>(c => c.UserLink.ID),
- r.Get<Employee, string>(c => c.Name)
- )
- );
- var recipient = employees.FirstOrDefault(x => Equals(x.Item1, employeeid(entity)));
- var sender = employees.FirstOrDefault(x => Equals(x.Item2, UserGuid));
- if (recipient == null || sender == null)
- return;
- if (recipient.Item2 == UserGuid)
- return;
- var notification = new Notification();
- notification.Employee.ID = recipient.Item1;
- notification.Sender.ID = sender.Item1;
- notification.Title = title(entity);
- var sb = new StringBuilder();
- sb.AppendLine(string.Format("Dear {0},\n", recipient.Item3.Split(' ').First()));
- sb.Append(body.Invoke(entity));
- if (sender != null)
- sb.AppendLine(string.Format("\n\nRegards,\n{0}.", sender.Item3.Split(' ').First()));
- notification.Description = sb.ToString();
- notification.EntityType = typeof(TEntity).EntityName();
- notification.EntityID = entity.ID;
- FindSubStore<Notification>().Save(notification, "");
- }
- }
- }
|