using Comal.Classes; using System; using System.Collections.Generic; using InABox.Core; using System.Linq; namespace Comal.Stores { public class ConsignmentStore : BaseStore { protected override void BeforeSave(Consignment entity) { base.BeforeSave(entity); } protected override void AfterSave(Consignment entity) { base.AfterSave(entity); Guid purchaseOrderID = Guid.Empty; //consignment has to be newly completed (usually done via receivals module) - checks further down as well to ensure no repeated saving of documents / batches if (entity.ActualWarehouseArrival == DateTime.MinValue) return; if (entity.HasOriginalValue(x => x.ActualWarehouseArrival) && entity.GetOriginalValue("ActualWarehouseArrival") != DateTime.MinValue) return; //find previously saved PO Items attached to this consignment CoreTable purchaseOrderItemTable = Provider.Query( new Filter(x => x.Consignment.ID).IsEqualTo(entity.ID), new Columns(x => x.ID, x => x.PurchaseOrderLink.ID) ); if (!purchaseOrderItemTable.Rows.Any()) return; List consignmentDocIDs = new List(); if (purchaseOrderItemTable.Rows.FirstOrDefault().Values[1] == null) purchaseOrderID = Guid.Empty; else purchaseOrderID = Guid.Parse(purchaseOrderItemTable.Rows.FirstOrDefault().Values[1].ToString()); CoreTable table = Provider.Query ( new Filter(x => x.EntityLink.ID).IsEqualTo(entity.ID), new Columns(x => x.DocumentLink.ID) ); if (table.Rows.Any()) { consignmentDocIDs = SavePurchaseOrderDocuments(table, purchaseOrderID); } //build filter for querying stock movements attached to PO Items on this consignment Filter filter = new Filter(x => x.OrderItem.ID).IsEqualTo(Guid.Parse(purchaseOrderItemTable.Rows.FirstOrDefault().Values[0].ToString())); foreach (CoreRow row in purchaseOrderItemTable.Rows) { if (row != purchaseOrderItemTable.Rows.FirstOrDefault()) { List list = row.Values; filter = filter.Or(x => x.OrderItem.ID).IsEqualTo(Guid.Parse(list[0].ToString())); } } CoreTable stockMovementTable = Provider.Query( filter, new Columns( x => x.ID, x => x.Notes, x => x.Batch.ID )); if (stockMovementTable.Rows.Any()) { //check if movements already have a batch.id - means photos would have already been saved if (Guid.Parse(stockMovementTable.Rows.FirstOrDefault().Values[2].ToString()) != Guid.Empty) return; //create new stockmovementbatch to attach new stockmovements to (movements created by server) StockMovementBatch batch = new StockMovementBatch(); batch.Type = StockMovementBatchType.Receipt; Provider.Save(batch); //add found stockmovements to batch, add stockmovements to save List movements = new List(); foreach (CoreRow row in stockMovementTable.Rows) { List list1 = row.Values; StockMovement movement = new StockMovement { ID = Guid.Parse(list1[0].ToString()), Notes = list1[1].ToString() }; movement.Batch.ID = batch.ID; movement.Employee.ID = entity.Employee.ID; movement.Notes = movement.Notes + " / Consignment " + entity.Number; movements.Add(movement); } Provider.Save(movements); List stockMovementBatchDocuments = new List(); foreach (Guid id in consignmentDocIDs) { var smd = new StockMovementBatchDocument(); smd.EntityLink.ID = batch.ID; smd.DocumentLink.ID = id; stockMovementBatchDocuments.Add(smd); } Provider.Save(stockMovementBatchDocuments); } } private List SavePurchaseOrderDocuments(CoreTable table, Guid purchaseOrderID) { List consignmentDocIDs = new List(); List purchaseOrderDocuments = new List(); foreach (CoreRow row in table.Rows) { List list = row.Values; Guid docID = Guid.Parse(list[0].ToString()); consignmentDocIDs.Add(docID); PurchaseOrderDocument PODocument = new PurchaseOrderDocument(); PODocument.DocumentLink.ID = docID; PODocument.EntityLink.ID = purchaseOrderID; CoreTable existingPODocs = Provider.Query( new Filter(x => x.DocumentLink.ID).IsEqualTo(docID), //check that PO document doesn't already exist new Columns(x => x.ID)); if (existingPODocs.Rows.Count == 0) purchaseOrderDocuments.Add(PODocument); } if (purchaseOrderDocuments.Any()) Provider.Save(purchaseOrderDocuments); return consignmentDocIDs; } } }