Exemplo n.º 1
0
        public Item Save(Item model)
        {
            _db.ExecuteStrategy(() =>
            {
                var isNew = model.ItemId == 0;

                if (isNew && string.IsNullOrEmpty(model.SKU))
                {
                    GenerateSKU(model);
                }

                _db.Item
                .PrepareModel(model)
                .ToUpper(u => new { u.Name })
                .SaveChanges();

                if (isNew)
                {
                    model.Transaction.ItemId = model.ItemId;
                    model.Transaction.UIN    = model.UIN;
                    model.Transaction.FIN    = model.FIN;

                    _db.Transaction.Add(model.Transaction);
                    _db.ItemLocation.Add(model.Transaction.ToClass(new ItemLocation()));
                    _db.SaveChanges();

                    TransactionService.CalculateAverageCosts(_db, model.Transaction);
                }
            });

            return(Find(f => f.ItemId == model.ItemId));
        }
Exemplo n.º 2
0
        public Transaction Save(Transaction model)
        {
            _db.ExecuteStrategy(() =>
            {
                var isNew            = model.TransactionId == 0;
                Transaction oldModel = null;

                if (!isNew)
                {
                    oldModel = _db.Transaction
                               .AsNoTracking()
                               .FirstOrDefault(f => f.TransactionId == model.TransactionId);
                }

                _db.Transaction
                .PrepareModel(model)
                .SaveChanges();

                var itemLocation = _db.ItemLocation.FirstOrDefault(f =>
                                                                   f.ItemId == model.ItemId &&
                                                                   f.LocationId == model.LocationId
                                                                   );

                if (itemLocation != null)
                {
                    if (isNew && model.TransactionTypeId != ETransactionType.UnusableReturn)
                    {
                        itemLocation.Quantity += model.Quantity;
                    }
                    else if (oldModel != null)
                    {
                        if (
                            new[]
                        {
                            ETransactionType.CustomerSale,
                            ETransactionType.DamagedStock
                        }.Contains(model.TransactionTypeId)
                            )
                        {
                            model.Quantity = -1 * Math.Abs(model.Quantity);
                        }

                        if (model.TransactionTypeId != ETransactionType.UnusableReturn)
                        {
                            itemLocation.Quantity = itemLocation.Quantity - oldModel.Quantity + model.Quantity;
                        }
                    }
                }
                else
                {
                    _db.ItemLocation.Add(model.ToClass(new ItemLocation()));
                }

                _db.SaveChanges();
                CalculateAverageCosts(_db, model);
            });

            return(Find(f => f.TransactionId == model.TransactionId));
        }
Exemplo n.º 3
0
        public Location Save(Location model)
        {
            _db.ExecuteStrategy(() =>
            {
                var isNew = model.LocationId == 0;

                _db.Location
                .PrepareModel(model)
                .ToUpper(u => new { u.Name })
                .SaveChanges();


                if (isNew)
                {
                }

                _db.SaveChanges();
            });

            return(Find(f => f.LocationId == model.LocationId));
        }
Exemplo n.º 4
0
        private Transaction TransferInventory(Transaction model)
        {
            _db.ExecuteStrategy(() =>
            {
                //Transaction in
                var transactionIn = model.ToClass(new Transaction());

                transactionIn.TransactionTypeId = ETransactionType.TransferIn;
                transactionIn.LocationId        = model.LocationToId;

                _db.Transaction.Add(transactionIn);

                var itemLocationTo = _db.ItemLocation.FirstOrDefault(f =>
                                                                     f.ItemId == transactionIn.ItemId &&
                                                                     f.LocationId == transactionIn.LocationId
                                                                     );

                if (itemLocationTo != null)
                {
                    itemLocationTo.Quantity += transactionIn.Quantity;

                    if (itemLocationTo.Quantity < 0)
                    {
                        throw new ApplicationException(
                            "La cantidad a transferir debe ser igual o menor a la disponible en el stock"
                            );
                    }

                    if (itemLocationTo.Quantity == 0)
                    {
                        _db.Entry(itemLocationTo).State = EntityState.Deleted;
                    }
                }
                else
                {
                    _db.ItemLocation.Add(transactionIn.ToClass(new ItemLocation()));
                }

                //Transaction out
                model.TransactionTypeId = ETransactionType.TransferOut;
                model.Quantity          = -1 * Math.Abs(model.Quantity);

                _db.Transaction
                .PrepareModel(model)
                .SaveChanges();

                var itemLocationFrom = _db.ItemLocation.FirstOrDefault(f =>
                                                                       f.ItemId == model.ItemId &&
                                                                       f.LocationId == model.LocationId
                                                                       );

                if (itemLocationFrom != null)
                {
                    itemLocationFrom.Quantity += model.Quantity;

                    if (itemLocationFrom.Quantity < 0)
                    {
                        throw new ApplicationException(
                            "La cantidad a transferir debe ser igual o menor a la disponible en el stock"
                            );
                    }

                    if (itemLocationFrom.Quantity == 0)
                    {
                        _db.Entry(itemLocationFrom).State = EntityState.Deleted;
                    }
                }
                else
                {
                    throw new ApplicationException(
                        "Error realizando la transferencia"
                        );
                }

                _db.SaveChanges();
            });

            return(model);
        }