Example #1
0
        public SupplyResponse Post([FromBody] UserRequest user)
        {
            if (string.IsNullOrWhiteSpace(user.Name) || string.IsNullOrWhiteSpace(user.UserName) ||
                string.IsNullOrWhiteSpace(user.Password))
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }
            var entity = _dbContext.User.AsNoTracking().SingleOrDefault(p => p.UserName == user.UserName);

            if (entity != null)
            {
                return(SupplyResponse.DuplicateEntry("user", user.UserName));
            }
            var dbUser = new User
            {
                Name     = user.Name,
                UserName = user.UserName,
                Password = HashUtilities.HashPassword(user.Password),
                UserType = user.UserType
            };

            _dbContext.User.Add(dbUser);
            _dbContext.SaveChanges();
            return(Get(dbUser.UserId));
        }
Example #2
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 AddToCategory(int id, [FromBody] IdRequest idRequest)
        {
            var category = _dbContext.Category.SingleOrDefault(p => p.CategoryId == id);

            var vItm = _dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId == idRequest.Id);

            if (category == null)
            {
                return(SupplyResponse.NotFound("category", id + ""));
            }
            if (vItm == null)
            {
                return(SupplyResponse.NotFound("virtual item", idRequest.Id + ""));
            }

            var cItem = _dbContext.CategoryItem.SingleOrDefault(p =>
                                                                p.CategoryId == category.CategoryId && p.VirtualItem.VirtualItemId == vItm.VirtualItemId);

            if (cItem != null)
            {
                return(SupplyResponse.DuplicateEntry("category item",
                                                     $"\"{cItem.VirtualItemId} in {cItem.CategoryId}\""));
            }

            var cateItm = new CategoryItem
            {
                CategoryId    = category.CategoryId,
                VirtualItemId = vItm.Id
            };

            _dbContext.CategoryItem.Add(cateItm);
            _dbContext.SaveChanges();
            return(Get(category.CategoryId));
        }
        public SupplyResponse Post(string id, [FromBody] IdRequest idRequest)
        {
            if (idRequest == null || string.IsNullOrWhiteSpace(idRequest.Id))
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }
            var vItem = _dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId == idRequest.Id);
            var item  = _dbContext.Item.SingleOrDefault(p => p.SupplierItemId == id);

            if (item == null)
            {
                return(SupplyResponse.NotFound("item", id));
            }
            if (vItem == null)
            {
                return(SupplyResponse.NotFound("virtual item", idRequest.Id));
            }

            if (_dbContext.VirtualIdMap.SingleOrDefault(p =>
                                                        p.ItemId == item.Id && p.VirtualItemId == vItem.Id) != null)
            {
                return(SupplyResponse.DuplicateEntry("virtual map", $"{id}<->{idRequest.Id}"));
            }

            _dbContext.VirtualIdMap.Add(new VirtualIdMap {
                ItemId = item.Id, VirtualItemId = vItem.Id
            });
            _dbContext.SaveChanges();
            return(SupplyResponse.Ok());
        }
Example #5
0
 public SupplyResponse Post([FromBody] VirtualItem item)
 {
     if (string.IsNullOrWhiteSpace(item.VirtualItemName) || string.IsNullOrWhiteSpace(item.VirtualItemId))
     {
         return(SupplyResponse.RequiredFieldEmpty());
     }
     if (_dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId == item.VirtualItemId) != null)
     {
         return(SupplyResponse.DuplicateEntry("item", item.VirtualItemId));
     }
     item.Id = 0;
     _dbContext.VirtualItem.Add(item);
     _dbContext.SaveChanges();
     return(Get(item.VirtualItemId));
 }
Example #6
0
        public SupplyResponse Post([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"));
            }


            //BPA
            if (agreement.AgreementType == AgreementType.Blanket)
            {
                //Get data from request
                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"));
                }


                //Verfiy&Process request object
                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,
                    };
                }

                //Create Agreement Object

                var dbAgreement = new Agreement
                {
                    AgreementType     = AgreementType.Blanket,
                    Currency          = agreement.Currency,
                    StartDate         = agreement.StartDate,
                    ExpiryDate        = agreement.ExpiryDate,
                    SupplierId        = agreement.SupplierId,
                    CreateBy          = dbUser.UserId,
                    TermsAndCondition = agreement.TermsAndCondition
                };
                _dbContext.Agreement.Add(dbAgreement);
                _dbContext.SaveChanges();
                var agreementId = dbAgreement.AgreementId;
                _dbContext.Entry(dbAgreement).State = EntityState.Detached;

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

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

                return(Get(agreementId));
            }


            //CPA
            if (agreement.AgreementType == AgreementType.Contract)
            {
                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,
                    });
                }

                var dbAgreement = new Agreement
                {
                    AgreementType     = AgreementType.Contract,
                    Currency          = agreement.Currency,
                    StartDate         = agreement.StartDate,
                    ExpiryDate        = agreement.ExpiryDate,
                    SupplierId        = agreement.SupplierId,
                    CreateBy          = dbUser.UserId,
                    TermsAndCondition = agreement.TermsAndCondition
                };

                _dbContext.Agreement.Add(dbAgreement);
                _dbContext.SaveChanges();
                var agreementId = dbAgreement.AgreementId;
                _dbContext.Entry(dbAgreement).State = EntityState.Detached;

                details.AgreementId = agreementId;
                _dbContext.ContractPurchaseAgreementDetails.Add(details);
                _dbContext.SaveChanges();

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

                return(Get(agreementId));
            }



            //PPA
            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
                    };
                }

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

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

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

                return(Get(agreementId));
            }

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