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);
        }
示例#2
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));
        }
示例#3
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));
        }
示例#4
0
        public SupplyResponse Post([FromBody] ICollection <ItemRequest> itemRequest)
        {
            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."));
            }
            var restaurantManager = dbUser.RestaurantManager;

            if (restaurantManager == null)
            {
                return(SupplyResponse.Fail("Unauthorize", "Your are not the restaurant manager."));
            }

            if (itemRequest == null || !itemRequest.Any())
            {
                return(SupplyResponse.BadRequest("Request Item cannot be empty."));
            }

            var itemMap  = new Dictionary <int, int>();
            var itemList = new List <RequestItem>();

            foreach (var item in itemRequest)
            {
                var virtualItem =
                    _dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId.Equals(item.VirtualItemId));
                if (virtualItem == null)
                {
                    return(SupplyResponse.NotFound("virtual item", item.VirtualItemId));
                }
                if (itemMap.ContainsKey(virtualItem.Id))
                {
                    itemMap[virtualItem.Id] += item.Quantity;
                }
                else
                {
                    itemMap.Add(virtualItem.Id, item.Quantity);
                }
            }

            foreach (var(itemId, qty) in itemMap)
            {
                var requestItem = new RequestItem
                {
                    VirtualItemId = itemId,
                    Quantity      = qty
                };
                itemList.Add(requestItem);
            }

            var restaurantId = restaurantManager.RestaurantId;
            var userId       = dbUser.UserId;

            var request = new Models.Request
            {
                RestaurantId   = restaurantId,
                RequestCreator = userId,
                CreateTime     = DateTime.Now
            };

            _dbContext.Request.Add(request);
            _dbContext.SaveChanges();

            var requestId = request.RequestId;

            _dbContext.Entry(request).State = EntityState.Detached;


            foreach (var item in itemList)
            {
                item.RequestId = requestId;
                _dbContext.RequestItem.Add(item);
                _dbContext.SaveChanges();
                _dbContext.Entry(item).State = EntityState.Detached;
            }

            return(Get(request.RequestId));
        }
示例#5
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 + ""));
        }