123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Linq;
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores
- {
- public class EmployeeProductStore : BaseStore<EmployeeProduct>
- {
- private void CheckIssue(EmployeeProduct entity)
- {
- if (entity.Issued.IsEmpty())
- {
- if (entity.IssuedMovement.IsValid())
- {
- var movement = new StockMovement { ID = entity.IssuedMovement.ID };
- FindSubStore<StockMovement>().Delete(movement, "Employee/Product Issue Deleted");
- entity.IssuedMovement.ID = Guid.Empty;
- }
- }
- else
- {
- if (!entity.IssuedMovement.IsValid())
- {
- var movement = new StockMovement();
- movement.Product.ID = entity.Product.ID;
- movement.Location.ID = entity.Product.DefaultLocation.ID;
- movement.Date = entity.Issued;
- movement.Issued = entity.Quantity;
- movement.Received = 0.0F;
-
- movement.Dimensions.Unit.ID = entity.Product.Dimensions.Unit.ID;
- movement.Dimensions.Unit.Synchronise(entity.Product.Dimensions);
- movement.Dimensions.Length = entity.Product.Dimensions.Length;
- movement.Dimensions.Width = entity.Product.Dimensions.Width;
- movement.Dimensions.Height = entity.Product.Dimensions.Height;
- movement.Dimensions.Weight = entity.Product.Dimensions.Weight;
-
- movement.Qty = 0.0F - movement.Issued * movement.Dimensions.Value;
- movement.Notes = string.Format("Issued to " + entity.Employee.Name);
- FindSubStore<StockMovement>().Save(movement, "Employee/Product Issue Created");
- entity.IssuedMovement.ID = movement.ID;
- }
- else
- {
- var movement = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.IssuedMovement.ID)).FirstOrDefault();
- if (movement == null)
- movement = new StockMovement();
- movement.Product.ID = entity.Product.ID;
- movement.Dimensions.CopyFrom(entity.Product.Dimensions);
- movement.Location.ID = entity.Product.DefaultLocation.ID;
- movement.Date = entity.Issued;
- movement.Issued = entity.Quantity;
- movement.Received = 0.0F;
- movement.Qty = 0.0F - movement.Issued * movement.Dimensions.Value;
- movement.Notes = string.Format("Issued to " + entity.Employee.Name);
- FindSubStore<StockMovement>().Save(movement, "Employee/Product Issue Updated");
- entity.IssuedMovement.ID = movement.ID;
- }
- }
- }
- private void CheckReturn(EmployeeProduct entity)
- {
- if (entity.Returned.IsEmpty())
- {
- if (entity.ReturnedMovement.IsValid())
- {
- var movement = new StockMovement { ID = entity.ReturnedMovement.ID };
- FindSubStore<StockMovement>().Delete(movement, "Employee/Product Return Deleted");
- entity.ReturnedMovement.ID = Guid.Empty;
- }
- }
- else
- {
- if (!entity.ReturnedMovement.IsValid())
- {
- var movement = new StockMovement();
- movement.Product.ID = entity.Product.ID;
- movement.Dimensions.CopyFrom(entity.Product.Dimensions);
- movement.Location.ID = entity.Product.DefaultLocation.ID;
- movement.Date = entity.Returned;
- movement.Issued = 0.0F;
- movement.Received = entity.Quantity;
- movement.Qty = movement.Received * movement.Dimensions.Value;
- movement.Notes = string.Format("Returned by " + entity.Employee.Name);
- FindSubStore<StockMovement>().Save(movement, "Employee/Product Return Created");
- entity.ReturnedMovement.ID = movement.ID;
- }
- else
- {
- var movement = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ReturnedMovement.ID)).FirstOrDefault();
- if (movement == null)
- movement = new StockMovement();
- movement.Product.ID = entity.Product.ID;
- movement.Dimensions.CopyFrom(entity.Product.Dimensions);
- movement.Location.ID = entity.Product.DefaultLocation.ID;
- movement.Date = entity.Returned;
- movement.Issued = 0.0F;
- movement.Received = entity.Quantity;
- movement.Qty = movement.Received * movement.Dimensions.Value;
- movement.Notes = string.Format("Returned by " + entity.Employee.Name);
- FindSubStore<StockMovement>().Save(movement, "Employee/Product Return Updated");
- entity.ReturnedMovement.ID = movement.ID;
- }
- }
- }
- protected override void BeforeSave(EmployeeProduct entity)
- {
- base.BeforeSave(entity);
- CheckIssue(entity);
- CheckReturn(entity);
- }
- protected override void BeforeDelete(EmployeeProduct entity)
- {
- base.BeforeDelete(entity);
- var movements = Provider.Load(new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.IssuedMovement.ID).Or(x => x.ID)
- .IsEqualTo(entity.ReturnedMovement.ID));
- FindSubStore<StockMovement>().Delete(movements, "Entity/Product Deleted");
- }
- }
- }
|