Exemplo n.º 1
0
        public SupplyResponse Put(string id, [FromBody] VirtualItem item)
        {
            if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(item.VirtualItemName) ||
                string.IsNullOrWhiteSpace(item.VirtualItemId))
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }

            var entity = _dbContext.VirtualItem.AsNoTracking().SingleOrDefault(p => p.VirtualItemId.Equals(id));

            if (entity == null)
            {
                return(Post(item));
            }

            if (entity.VirtualItemId != item.VirtualItemId &&
                _dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId == item.VirtualItemId) != null)
            {
                return(SupplyResponse.DuplicateEntry("virtual item", item.VirtualItemId));
            }

            item.Id = entity.Id;
            var entry = _dbContext.Attach(item);

            //var entry = _dbContext.Entry(entity);
            //entry.CurrentValues.SetValues(item);
            entry.State = EntityState.Modified;
            _dbContext.SaveChanges();
            return(Get(item.VirtualItemId));
        }
        public SupplyResponse Put(int id, [FromBody] Restaurant restaurant)
        {
            var entity = _dbContext.Restaurant.AsNoTracking().SingleOrDefault(p => p.RestaurantId == id);
            if (entity == null) return Post(restaurant);
            if (string.IsNullOrWhiteSpace(restaurant.RestaurantName) ||
                string.IsNullOrWhiteSpace(restaurant.RestaurantLocation) ||
                restaurant.RestaurantTypeId == 0) return SupplyResponse.RequiredFieldEmpty();

            if (_dbContext.RestaurantType.SingleOrDefault(p => p.RestaurantTypeId == restaurant.RestaurantTypeId) ==
                null) return SupplyResponse.NotFound("restaurant type", restaurant.RestaurantTypeId + "");

            restaurant.RestaurantId = id;
            _dbContext.Attach(restaurant);
            _dbContext.Entry(restaurant).State = EntityState.Modified;
            _dbContext.SaveChanges();
            return Get(id);
        }
Exemplo n.º 3
0
        public SupplyResponse Put(int id, [FromBody] Supplier supplier)
        {
            var entity = _dbContext.Supplier.AsNoTracking().SingleOrDefault(p => p.SupplierId == id);

            if (entity == null)
            {
                return(SupplyResponse.NotFound("supplier", id + ""));
            }
            if (string.IsNullOrWhiteSpace(supplier.SupplierName))
            {
                return(SupplyResponse.BadRequest("Required Field is Empty"));
            }
            supplier.SupplierId = id;
            _dbContext.Attach(supplier);
            _dbContext.Entry(supplier).State = EntityState.Modified;
            _dbContext.SaveChanges();
            return(Get(id));
        }
Exemplo n.º 4
0
        public SupplyResponse Put(int id, [FromBody] Category category)
        {
            var entity = _dbContext.Category.AsNoTracking().SingleOrDefault(p => p.CategoryId == id);

            if (entity == null)
            {
                return(Post(category));
            }
            if (string.IsNullOrWhiteSpace(category.CategoryName))
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }
            category.CategoryId = id;
            _dbContext.Attach(category);
            _dbContext.Entry(category).State = EntityState.Modified;
            _dbContext.SaveChanges();
            return(Get(id));
        }
Exemplo n.º 5
0
        public SupplyResponse Put(int id, [FromBody] AgreementWrapper agreement)
        {
            var currentUser = HttpContext.User;
            var dbUser      =
                _dbContext.User.Include(p => p.RestaurantManager).ThenInclude(p => p.Restaurant)
                .SingleOrDefault(p => currentUser.FindFirst(ClaimTypes.Name).Value.Equals(p.UserName));

            if (dbUser == null)
            {
                return(SupplyResponse.Fail("Unauthorize", "Your are not the user in the system."));
            }

            if (agreement.Details == null ||
                agreement.Items == null || agreement.SupplierId == 0)
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }

            if (agreement.StartDate > agreement.ExpiryDate)
            {
                return(SupplyResponse.BadRequest("Start date cannot be later than Expiry Date"));
            }

            var _dbAgreement = _dbContext.Agreement.AsNoTracking().SingleOrDefault(p => id == p.AgreementId);

            if (_dbAgreement == null)
            {
                return(SupplyResponse.NotFound("Agreement", id + ""));
            }
            if (_dbAgreement.AgreementType != agreement.AgreementType)
            {
                return(SupplyResponse.BadRequest("Agreement Type Cannot be change"));
            }


            //BPA
            if (agreement.AgreementType == AgreementType.Blanket)
            {
                ICollection <QuantityItems>     items = new List <QuantityItems>();
                BlanketPurchaseAgreementDetails details;
                try
                {
                    foreach (var item in agreement.Items)
                    {
                        items.Add(item.ToObject <QuantityItems>());
                    }

                    if (!items.Any())
                    {
                        return(SupplyResponse.BadRequest("Agreement Line is Empty"));
                    }

                    details = agreement.Details.ToObject <BlanketPurchaseAgreementDetails>();
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    return(SupplyResponse.BadRequest("Request Format Fail"));
                }

                var dbLine = new Dictionary <string, BlanketPurchaseAgreementLine>();

                foreach (var item in items)
                {
                    if (item.MinimumQuantity <= 0 && item.PromisedQuantity <= 0)
                    {
                        return(SupplyResponse.BadRequest($"Item {item.SupplierItemId} has a zero or negative quantity"));
                    }

                    var dbItem = _dbContext.Item.SingleOrDefault(p => item.SupplierItemId == p.SupplierItemId);
                    if (dbItem == null)
                    {
                        return(SupplyResponse.NotFound("supplier item", item.SupplierItemId));
                    }

                    if (dbLine.ContainsKey(item.SupplierItemId))
                    {
                        return(SupplyResponse.DuplicateEntry("Request Item", item.SupplierItemId));
                    }

                    dbLine[item.SupplierItemId] = new BlanketPurchaseAgreementLine
                    {
                        ItemId           = dbItem.Id,
                        MinimumQuantity  = item.MinimumQuantity,
                        PromisedQuantity = item.PromisedQuantity,
                        Price            = item.Price,
                        Unit             = item.Unit,
                    };
                }

                var _dbDetails = _dbContext.BlanketPurchaseAgreementDetails.AsNoTracking()
                                 .SingleOrDefault(p => p.AgreementId == _dbAgreement.AgreementId);
                _dbContext.Remove(_dbDetails);

                var lines = _dbContext.BlanketPurchaseAgreementLine.Select(p => p)
                            .Where(p => p.AgreementId == _dbAgreement.AgreementId);

                foreach (var line in lines)
                {
                    _dbContext.Remove(line);
                }

                _dbContext.SaveChanges();

                var dbAgreement = new Agreement
                {
                    AgreementId       = _dbAgreement.AgreementId,
                    AgreementType     = AgreementType.Blanket,
                    Currency          = agreement.Currency,
                    StartDate         = agreement.StartDate,
                    ExpiryDate        = agreement.ExpiryDate,
                    SupplierId        = agreement.SupplierId,
                    CreateBy          = _dbAgreement.CreateBy,
                    TermsAndCondition = agreement.TermsAndCondition
                };
                var agreementEntry = _dbContext.Agreement.Attach(dbAgreement);
                agreementEntry.State = EntityState.Modified;
                _dbContext.SaveChanges();
                _dbContext.Entry(dbAgreement).State = EntityState.Detached;


                details.AgreementId = _dbAgreement.AgreementId;
                _dbContext.BlanketPurchaseAgreementDetails.Add(details);
                _dbContext.SaveChanges();


                foreach (var line in dbLine.Values)
                {
                    line.AgreementId = _dbAgreement.AgreementId;
                    var entry = _dbContext.BlanketPurchaseAgreementLine.Add(line);
                    _dbContext.SaveChanges();
                    entry.State = EntityState.Detached;
                }

                _dbContext.SaveChanges();
                return(Get(dbAgreement.AgreementId));
            }
            else if (agreement.AgreementType == AgreementType.Contract) //CPA
            {
                ICollection <QuantityItems>      items = new List <QuantityItems>();
                ContractPurchaseAgreementDetails details;
                try
                {
                    foreach (var item in agreement.Items)
                    {
                        items.Add(item.ToObject <QuantityItems>());
                    }

                    if (!items.Any())
                    {
                        return(SupplyResponse.BadRequest("Agreement Line is Empty"));
                    }

                    details = agreement.Details.ToObject <ContractPurchaseAgreementDetails>();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    return(SupplyResponse.BadRequest("Request Format Fail"));
                }

                var dbLine = new List <ContractPurchaseAgreementLine>();

                foreach (var item in items)
                {
                    var dbItem = _dbContext.Item.SingleOrDefault(p => item.SupplierItemId == p.SupplierItemId);
                    if (dbItem == null)
                    {
                        return(SupplyResponse.NotFound("supplier item", item.SupplierItemId));
                    }

                    dbLine.Add(new ContractPurchaseAgreementLine()
                    {
                        ItemId = dbItem.Id,
                    });
                }


                //Remove Details
                var _dbDetails = _dbContext.ContractPurchaseAgreementDetails.AsNoTracking()
                                 .SingleOrDefault(p => p.AgreementId == _dbAgreement.AgreementId);
                _dbContext.Remove(_dbDetails);


                //Remove Lines
                var lines = _dbContext.ContractPurchaseAgreementLine.Select(p => p)
                            .Where(p => p.AgreementId == _dbAgreement.AgreementId);

                foreach (var line in lines)
                {
                    _dbContext.Remove(line);
                }

                _dbContext.SaveChanges();


                //Create a new replacement agreement
                var dbAgreement = new Agreement
                {
                    AgreementType     = AgreementType.Contract,
                    Currency          = agreement.Currency,
                    StartDate         = agreement.StartDate,
                    ExpiryDate        = agreement.ExpiryDate,
                    SupplierId        = agreement.SupplierId,
                    CreateBy          = _dbAgreement.CreateBy,
                    AgreementId       = _dbAgreement.AgreementId,
                    TermsAndCondition = agreement.TermsAndCondition
                };

                //Update Agreement
                _dbContext.Attach(dbAgreement).State = EntityState.Modified;


                //Add back new details
                details.AgreementId = _dbAgreement.AgreementId;
                _dbContext.ContractPurchaseAgreementDetails.Add(details);
                _dbContext.SaveChanges();

                //Add back lines
                foreach (var line in dbLine)
                {
                    line.AgreementId = _dbAgreement.AgreementId;
                    var entry = _dbContext.ContractPurchaseAgreementLine.Add(line);
                    _dbContext.SaveChanges();

                    entry.State = EntityState.Detached;
                }

                _dbContext.SaveChanges();

                return(Get(_dbAgreement.AgreementId));
            }
            else if (agreement.AgreementType == AgreementType.Planned)
            {
                ICollection <QuantityItems>     items = new List <QuantityItems>();
                PlannedPurchaseAgreementDetails details;
                try
                {
                    foreach (var item in agreement.Items)
                    {
                        items.Add(item.ToObject <QuantityItems>());
                    }

                    if (!items.Any())
                    {
                        return(SupplyResponse.BadRequest("Agreement Line is Empty"));
                    }

                    details = agreement.Details.ToObject <PlannedPurchaseAgreementDetails>();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    return(SupplyResponse.BadRequest("Request Format Fail"));
                }

                var dbLine = new Dictionary <string, PlannedPurchaseAgreementLine>();

                foreach (var item in items)
                {
                    if (item.Quantity <= 0)
                    {
                        return(SupplyResponse.BadRequest($"Item {item.SupplierItemId} has a zero or negative quantity"));
                    }

                    var dbItem = _dbContext.Item.SingleOrDefault(p => item.SupplierItemId == p.SupplierItemId);
                    if (dbItem == null)
                    {
                        return(SupplyResponse.NotFound("supplier item", item.SupplierItemId));
                    }

                    if (dbLine.ContainsKey(item.SupplierItemId))
                    {
                        return(SupplyResponse.DuplicateEntry("Request Item", item.SupplierItemId));
                    }

                    dbLine[item.SupplierItemId] = new PlannedPurchaseAgreementLine
                    {
                        ItemId   = dbItem.Id,
                        Quantity = item.Quantity,

                        Price = item.Price,
                        Unit  = item.Unit
                    };
                }

                //Remove Details
                var _dbDetails = _dbContext.PlannedPurchaseAgreementDetails.AsNoTracking()
                                 .SingleOrDefault(p => p.AgreementId == _dbAgreement.AgreementId);
                _dbContext.Remove(_dbDetails);


                //Remove Lines
                var lines = _dbContext.PlannedPurchaseAgreementLine.Select(p => p)
                            .Where(p => p.AgreementId == _dbAgreement.AgreementId);

                foreach (var line in lines)
                {
                    _dbContext.Remove(line);
                }

                _dbContext.SaveChanges();

                var dbAgreement = new Agreement
                {
                    AgreementType     = AgreementType.Planned,
                    Currency          = agreement.Currency,
                    StartDate         = agreement.StartDate,
                    ExpiryDate        = agreement.ExpiryDate,
                    SupplierId        = agreement.SupplierId,
                    CreateBy          = dbUser.UserId,
                    AgreementId       = _dbAgreement.AgreementId,
                    TermsAndCondition = agreement.TermsAndCondition
                };
                _dbContext.Attach(dbAgreement).State = EntityState.Modified;
                _dbContext.SaveChanges();

                details.AgreementId = _dbAgreement.AgreementId;
                _dbContext.PlannedPurchaseAgreementDetails.Add(details);
                _dbContext.SaveChanges();

                foreach (var line in dbLine.Values)
                {
                    line.AgreementId = _dbAgreement.AgreementId;
                    var entry = _dbContext.PlannedPurchaseAgreementLine.Add(line);
                    _dbContext.SaveChanges();
                    entry.State = EntityState.Detached;
                }

                _dbContext.SaveChanges();

                return(Get(dbAgreement.AgreementId));
            }

            return(SupplyResponse.NotFound("Agreement Type", agreement.AgreementType + ""));
        }