| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.Linq;
 
- using System.Threading.Tasks;
 
- using Comal.Classes;
 
- using InABox.Core;
 
- namespace Comal.Stores
 
- {
 
-     internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
 
-     {
 
-         private void UpdateStockMovements(PurchaseOrderItem entity)
 
-         {
 
-             if (!entity.Product.IsValid())
 
-             {
 
-                 Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Product.ID is blank!");
 
-                 return;
 
-             }
 
-             var locationid = entity.StockLocation.ID;
 
-             var locationValid = entity.StockLocation.IsValid();
 
-             var movementtask = new Task<List<StockMovement>>(() =>
 
-             {
 
-                 var result = Provider.Query(
 
-                     new Filter<StockMovement>(x => x.OrderItem.ID).IsEqualTo(entity.ID),
 
-                     new Columns<StockMovement>(
 
-                         x => x.ID,
 
-                         x => x.Date,
 
-                         x => x.Product.ID,
 
-                         x => x.Received,
 
-                         x => x.Employee.ID,
 
-                         x => x.OrderItem.ID,
 
-                         x => x.Job.ID,
 
-                         x => x.Location.ID,
 
-                         x => x.Dimensions.Unit.ID,
 
-                         x => x.Dimensions.Unit.Formula,
 
-                         x => x.Dimensions.Unit.Format,
 
-                         x => x.Dimensions.Quantity,
 
-                         x => x.Dimensions.Length,
 
-                         x => x.Dimensions.Width,
 
-                         x => x.Dimensions.Height,
 
-                         x => x.Dimensions.Weight,
 
-                         x => x.Notes,
 
-                         x => x.Cost,
 
-                         x => x.Dimensions.Unit.HasHeight,
 
-                         x => x.Dimensions.Unit.HasLength,
 
-                         x => x.Dimensions.Unit.HasWidth,
 
-                         x => x.Dimensions.Unit.HasWeight,
 
-                         x => x.Dimensions.Unit.HasQuantity,
 
-                         x => x.Dimensions.Unit.Formula,
 
-                         x => x.Dimensions.Unit.Format,
 
-                         x => x.Dimensions.Unit.Code,
 
-                         x => x.Dimensions.Unit.Description
 
-                     )
 
-                 ).Rows.Select(x => x.ToObject<StockMovement>()).ToList();
 
-                 if (!result.Any())
 
-                     result.Add(new StockMovement());
 
-                 return result;
 
-             });
 
-             movementtask.Start();
 
-             var producttask = new Task<CoreRow?>(() =>
 
-             {
 
-                 return Provider.Query(
 
-                     new Filter<Product>(x => x.ID).IsEqualTo(entity.Product.ID),
 
-                     new Columns<Product>(
 
-                         x => x.ID,
 
-                         x => x.DefaultLocation.ID,
 
-                         x => x.Warehouse.ID,
 
-                         x => x.Dimensions.Unit.ID,
 
-                         x => x.Dimensions.Unit.Formula,
 
-                         x => x.Dimensions.Unit.Format,
 
-                         x => x.Dimensions.Quantity,
 
-                         x => x.Dimensions.Length,
 
-                         x => x.Dimensions.Width,
 
-                         x => x.Dimensions.Height,
 
-                         x => x.Dimensions.Weight,
 
-                         x => x.NonStock,
 
-                         x => x.Dimensions.Unit.HasHeight,
 
-                         x => x.Dimensions.Unit.HasLength,
 
-                         x => x.Dimensions.Unit.HasWidth,
 
-                         x => x.Dimensions.Unit.HasWeight,
 
-                         x => x.Dimensions.Unit.HasQuantity,
 
-                         x => x.Dimensions.Unit.Formula,
 
-                         x => x.Dimensions.Unit.Format,
 
-                         x => x.Dimensions.Unit.Code,
 
-                         x => x.Dimensions.Unit.Description,
 
-                         x => x.TotalStock,
 
-                         x => x.AverageCost
 
-                     )
 
-                 ).Rows.FirstOrDefault();
 
-             });
 
-             producttask.Start();
 
-             var locationtask = new Task<CoreTable>(() =>
 
-             {
 
-                 return Provider.Query(
 
-                     new Filter<StockLocation>(x => x.Default).IsEqualTo(true),
 
-                     new Columns<StockLocation>(x => x.ID, x => x.Warehouse.ID, x => x.Warehouse.Default)
 
-                 );
 
-             });
 
-             locationtask.Start();
 
-             Task.WaitAll(movementtask, producttask, locationtask);
 
-             var movements = movementtask.Result;
 
-             var productrow = producttask.Result;
 
-             var defaultlocations = locationtask.Result;
 
-             if (entity.Qty == 0)
 
-             {
 
-                 Logger.Send(LogType.Information, UserID, "PurchaseOrderItem Qty is blank!");
 
-                 return;
 
-             }
 
-             if (productrow == null)
 
-             {
 
-                 Logger.Send(LogType.Information, UserID, "Cannot Find PurchaseOrderItem.Product.ID!");
 
-                 return;
 
-             }
 
-             if (productrow.Get<Product, bool>(x => x.NonStock))
 
-             {
 
-                 Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Product is marked as Non Stock!");
 
-                 return;
 
-             }
 
-             
 
-             if (!locationValid)
 
-             {
 
-                 Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Location.ID is blank!");
 
-                 if (productrow != null)
 
-                 {
 
-                     var productlocationid = productrow.EntityLinkID<Product, StockLocationLink>(x => x.DefaultLocation) ?? Guid.Empty;
 
-                     if (productlocationid != Guid.Empty)
 
-                     {
 
-                         Logger.Send(LogType.Information, UserID, "- Using Product.DefaultLocation.ID as location");
 
-                         locationid = productlocationid;
 
-                     }
 
-                     else
 
-                     {
 
-                         var productwarehouseid = productrow.Get<Product, Guid>(c => c.Warehouse.ID);
 
-                         var row = defaultlocations.Rows.FirstOrDefault(r => r.Get<StockLocation, Guid>(c => c.Warehouse.ID) == productwarehouseid);
 
-                         if (row != null)
 
-                         {
 
-                             Logger.Send(LogType.Information, UserID, "- Using Product.Warehouse -> Default as location");
 
-                             locationid = row.Get<StockLocation, Guid>(x => x.ID);
 
-                         }
 
-                     }
 
-                 }
 
-                 if (locationid == Guid.Empty)
 
-                 {
 
-                     var row = defaultlocations.Rows.FirstOrDefault(r => r.Get<StockLocation, bool>(c => c.Warehouse.Default));
 
-                     if (row != null)
 
-                     {
 
-                         Logger.Send(LogType.Information, UserID, "- Using Default Warehouse -> Default Location as location");
 
-                         locationid = row.Get<StockLocation, Guid>(x => x.ID);
 
-                     }
 
-                 }
 
-                 if (locationid == Guid.Empty)
 
-                 {
 
-                     Logger.Send(LogType.Information, UserID, "- Cannot find Location : Skipping Movement Creation");
 
-                     return;
 
-                 }
 
-             }
 
-             
 
-             if (
 
-                 (entity.Dimensions.Unit.ID == Guid.Empty)
 
-                 && (entity.Dimensions.Height == 0)
 
-                 && (entity.Dimensions.Width == 0)
 
-                 && (entity.Dimensions.Length == 0)
 
-                 && (entity.Dimensions.Weight == 0)
 
-                 )
 
-             {
 
-                 Logger.Send(LogType.Information, UserID, "PurchaseOrderItem.Unit Size is zero!");
 
-                 entity.Dimensions.CopyFrom(productrow.ToObject<Product>().Dimensions);
 
-             }
 
-             var product = productrow.ToObject<Product>();
 
-             
 
-             var poqty = entity.Qty * (Math.Abs(entity.Dimensions.Value) > 0.0001F ? entity.Dimensions.Value : 1.0F);
 
-             var pocost = entity.Cost * entity.Qty;
 
-             
 
-             var totalqty = product.TotalStock + poqty;
 
-             var totalcost = (product.TotalStock * product.AverageCost) + pocost;
 
-             var averagecost = Math.Abs(totalqty) > 0.0001F
 
-                 ? totalcost / totalqty
 
-                 : pocost / poqty;
 
-             if (Math.Abs(averagecost - product.AverageCost) > 0.0001F)
 
-             {
 
-                 product.AverageCost = averagecost;
 
-                 FindSubStore<Product>().Save(product,"Updated Average Cost");
 
-             }
 
-             foreach (var movement in movements)
 
-             {
 
-                 movement.Batch.Type = StockMovementBatchType.Receipt;
 
-                 movement.Date = entity.ReceivedDate;
 
-                 movement.Product.ID = entity.Product.ID;
 
-                 movement.Received = entity.Qty;
 
-                 movement.Employee.ID = Guid.Empty;
 
-                 movement.OrderItem.ID = entity.ID;
 
-                 movement.Job.ID = entity.Job.ID;
 
-                 movement.Location.ID = locationid;
 
-                 movement.Style.ID = entity.Style.ID;
 
-                 movement.Style.Code = entity.Style.Code;
 
-                 movement.Style.Description = entity.Style.Description;
 
-                 movement.Notes = string.Format("Received on PO {0}", entity.PurchaseOrderLink.PONumber);
 
-                 movement.Cost = entity.Cost;
 
-                 movement.Dimensions.Unit.ID = entity.Dimensions.Unit.ID;
 
-                 movement.Dimensions.Height = entity.Dimensions.Height;
 
-                 movement.Dimensions.Length = entity.Dimensions.Length;
 
-                 movement.Dimensions.Width = entity.Dimensions.Width;
 
-                 movement.Dimensions.Weight = entity.Dimensions.Weight;
 
-                 movement.Dimensions.Quantity = entity.Dimensions.Quantity;
 
-                 movement.Dimensions.UnitSize = entity.Dimensions.UnitSize;
 
-                 movement.Dimensions.Value = entity.Dimensions.Value;
 
-                 movement.Dimensions.UnitSize = entity.Dimensions.UnitSize;
 
-             }
 
-             var updates = movements.Where(x => x.IsChanged());
 
-             if (updates.Any())
 
-                 FindSubStore<StockMovement>().Save(updates, "Updated by Purchase Order Modification");
 
-         }
 
-         private void DeleteStockMovements(PurchaseOrderItem entity)
 
-         {
 
-             var movements = Provider.Query(
 
-                 new Filter<StockMovement>(x => x.OrderItem.ID).IsEqualTo(entity.ID),
 
-                 new Columns<StockMovement>(x => x.ID)
 
-             ).Rows.Select(x => x.ToObject<StockMovement>());
 
-             if (movements.Any())
 
-                 FindSubStore<StockMovement>().Delete(movements, "Purchase Order Item marked as Unreceived");
 
-         }
 
-         
 
-         protected override void AfterSave(PurchaseOrderItem entity)
 
-         {
 
-             base.AfterSave(entity);
 
-             if (entity.ReceivedDate.IsEmpty())
 
-                 DeleteStockMovements(entity);
 
-             else
 
-             {
 
-                 UpdateStockMovements(entity);
 
-                 UpdateJobRequiItems(entity);
 
-             }
 
-         }
 
-         private void UpdateJobRequiItems(PurchaseOrderItem entity)
 
-         {
 
-             var table = Provider.Query(
 
-                 new Filter<JobRequisitionItem>(x => x.PurchaseOrderItem.ID).IsEqualTo(entity.ID),
 
-                 new Columns<JobRequisitionItem>(x => x.ID, x => x.Status)
 
-                 );
 
-             if (table.Rows.Any())
 
-             {
 
-                 JobRequisitionItem item = table.Rows.FirstOrDefault().ToObject<JobRequisitionItem>();
 
-                 if (item.Status == JobRequisitionItemStatus.TreatmentOnOrder)
 
-                     item.Status = JobRequisitionItemStatus.TreatmentReceived;
 
-                 else
 
-                     item.Status = JobRequisitionItemStatus.Received;
 
-                 Provider.Save(item);
 
-             }
 
-         }
 
-         protected override void BeforeDelete(PurchaseOrderItem entity)
 
-         {
 
-             base.BeforeDelete(entity);
 
-             DeleteStockMovements(entity);
 
-         }
 
-         protected override void AfterDelete(PurchaseOrderItem entity)
 
-         {
 
-             base.AfterDelete(entity);
 
-             RemoveJobRequisitionItemLink(entity);
 
-         }
 
-         private void RemoveJobRequisitionItemLink(PurchaseOrderItem entity)
 
-         {
 
-             CoreTable table = Provider.Query<JobRequisitionItem>
 
-                 (
 
-                 new Filter<JobRequisitionItem>(x => x.PurchaseOrderItem.ID).IsEqualTo(entity.ID),
 
-                 new Columns<JobRequisitionItem>(x => x.ID, x => x.PurchaseOrderItem.PurchaseOrderLink.PONumber, x => x.Status)
 
-                 );
 
-             if (table.Rows.Any())
 
-             {
 
-                 JobRequisitionItem item = table.Rows.FirstOrDefault().ToObject<JobRequisitionItem>();
 
-                 item.PurchaseOrderItem.PurchaseOrderLink.PONumber = "";
 
-                 item.Status = JobRequisitionItemStatus.NotChecked;
 
-                 Provider.Save<JobRequisitionItem>(item);
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |