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));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        public SupplyResponse Put(int id, [FromBody] ICollection <ItemRequest> itemRequest)
        {
            var request = _dbContext.Request.Include(p => p.RequestItem).SingleOrDefault(p => p.RequestId == id);

            if (request == null)
            {
                return(SupplyResponse.NotFound("Request", id + ""));
            }

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

            ICollection <RequestItem> requestItems;

            if ((requestItems = request.RequestItem) != null)
            {
                foreach (var items in requestItems)
                {
                    _dbContext.Remove(items);
                }
            }

            _dbContext.SaveChanges();

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

            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
                {
                    RequestId     = request.RequestId,
                    VirtualItemId = itemId,
                    Quantity      = qty
                };
                _dbContext.RequestItem.Add(requestItem);
                _dbContext.SaveChanges();
            }

            return(Get(request.RequestId));
        }
        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());
        }
        public SupplyResponse Put(int id)
        {
            var order = _dbContext.DespatchInstruction.Include(p => p.Request)
                        .ThenInclude(p => p.RequestItem).ThenInclude(p => p.VirtualItem).Include(p => p.Request)
                        .ThenInclude(p => p.Restaurant)
                        .SingleOrDefault(p => p.DespatchInstructionId == id);

            if (order == null)
            {
                return(SupplyResponse.NotFound("Purchase Order", id + ""));
            }

            order.Request.RequestStatus = RequestStatus.Delivering;

            _dbContext.SaveChanges();

            order.DespatchInstructionStatus = 1;

            _dbContext.SaveChanges();

            _dbContext.DeliveryNote.Add(new DeliveryNote
            {
                CreateTime = DateTime.Now,
                RequestId  = order.RequestId
            });

            _dbContext.SaveChanges();

            return(Get(id));
        }
Exemplo n.º 6
0
        public SupplyResponse Delete(int id)
        {
            var datas = _dbContext.DataCache.SingleOrDefault(p => p.DataCacheId == id);

            _dbContext.DataCache.Remove(datas);
            _dbContext.SaveChanges();
            return(SupplyResponse.Ok());
        }
Exemplo n.º 7
0
 public SupplyResponse Post([FromBody] Category category)
 {
     if (string.IsNullOrWhiteSpace(category.CategoryName))
     {
         return(SupplyResponse.RequiredFieldEmpty());
     }
     _dbContext.Category.Add(category);
     _dbContext.SaveChanges();
     return(Get(category.CategoryId));
 }
Exemplo n.º 8
0
 public SupplyResponse Post([FromBody] Supplier supplier)
 {
     if (string.IsNullOrWhiteSpace(supplier.SupplierName) || string.IsNullOrWhiteSpace(supplier.SupplierContactPerson) || string.IsNullOrWhiteSpace(supplier.SupplierAddress))
     {
         return(SupplyResponse.BadRequest("Required Field is Empty"));
     }
     _dbContext.Supplier.Add(supplier);
     _dbContext.SaveChanges();
     return(Get(supplier.SupplierId));
 }
Exemplo n.º 9
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));
 }
        public SupplyResponse AddRestaurantType([FromBody] RestaurantType nameRequest)
        {
            if (string.IsNullOrWhiteSpace(nameRequest.RestaurantTypeName))
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }
            var restaurantType = new RestaurantType
            {
                RestaurantTypeName = nameRequest.RestaurantTypeName
            };

            var entity = _dbContext.RestaurantType.Add(restaurantType);

            _dbContext.SaveChanges();
            return(SupplyResponse.Ok(entity.Entity));
        }
        public SupplyResponse Post([FromBody] Restaurant 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 + "");

            var stock = new Stock();
            _dbContext.Stock.Add(stock);
            _dbContext.SaveChanges();

            restaurant.StockId = stock.StockId;
            _dbContext.Restaurant.Add(restaurant);
            _dbContext.SaveChanges();
            return Get(restaurant.RestaurantId);
        }
        public SupplyResponse AddRestaurantManager(int id, [FromBody] IntIdRequest idRequest)
        {
            if (id == 0 || idRequest.Id == 0)
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }

            var restaurant = _dbContext.Restaurant.SingleOrDefault(p => id == p.RestaurantId);

            var user = _dbContext.User.SingleOrDefault(p => p.UserId == idRequest.Id);

            if (restaurant == null)
            {
                return(SupplyResponse.NotFound("restaurant", id + ""));
            }

            if (user == null)
            {
                return(SupplyResponse.NotFound("user", idRequest.Id + ""));
            }

            var restaurantManager = _dbContext.RestaurantManager.Include(p => p.Restaurant)
                                    .SingleOrDefault(p => p.UserId == idRequest.Id);

            if (restaurantManager != null)
            {
                return(SupplyResponse.Fail("Already a Manager",
                                           "This user is already a manager of restaurant: " + restaurantManager.Restaurant.RestaurantName));
            }

            restaurantManager = new RestaurantManager
            {
                RestaurantId = id,
                UserId       = idRequest.Id
            };
            _dbContext.RestaurantManager.Add(restaurantManager);
            _dbContext.SaveChanges();
            return(Get(id));
        }
        public SupplyResponse Put(int id, [FromBody] BlanketRelease orderStatus)
        {
            var order = _dbContext.BlanketRelease.SingleOrDefault(p => p.OrderId == id);

            if (order == null)
            {
                return(SupplyResponse.NotFound("Purchase Order", id + ""));
            }

            order.PurchaseOrderStatus = orderStatus.PurchaseOrderStatus;
            _dbContext.SaveChanges();

            return(Get(id));
        }
        public SupplyResponse AddStock(int id, [FromBody] ItemRequest itemRequest)
        {
            var stock = _dbContext.Stock.SingleOrDefault(p => p.StockId == id);
            var vItm  = _dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId == itemRequest.VirtualItemId);

            if (stock == null)
            {
                return(SupplyResponse.NotFound("stock", id + ""));
            }
            if (vItm == null)
            {
                return(SupplyResponse.NotFound("virtual item", itemRequest.VirtualItemId + ""));
            }

            var stockItem = _dbContext.StockItem.SingleOrDefault(p =>
                                                                 p.StockId == id && p.VirtualItem.VirtualItemId == vItm.VirtualItemId);

            if (stockItem != null)
            {
                stockItem.Quantity += itemRequest.Quantity;
            }
            else
            {
                stockItem = new StockItem
                {
                    StockId       = id,
                    VirtualItemId = vItm.Id,
                    Quantity      = itemRequest.Quantity
                };
                _dbContext.StockItem.Add(stockItem);
            }

            _dbContext.SaveChanges();

            return(Get(stock.StockId));
        }
Exemplo n.º 15
0
        public SupplyResponse Put(int id, DeliveryNote deliveryNote)
        {
            var order = _dbContext.DeliveryNote.Include(p => p.Request)
                        .ThenInclude(p => p.RequestItem).ThenInclude(p => p.VirtualItem).Include(p => p.Request)
                        .ThenInclude(p => p.Restaurant)
                        .SingleOrDefault(p => p.RequestId == id);

            if (order == null)
            {
                return(SupplyResponse.NotFound("Purchase Order", id + ""));
            }

            order.DeliveryStatus = deliveryNote.DeliveryStatus;

            _dbContext.SaveChanges();


            return(Get(id));
        }
Exemplo n.º 16
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 + ""));
        }
Exemplo n.º 17
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));
        }