public Domain.Request FindSingle(int requestID)
        {
            var request = new Domain.Request();
            var order   = new Order();

            order.LoadByPrimaryKey(requestID);

            request.RequestID     = order.ID;
            request.OrderNumber   = order.RefNo;
            request.LetterNumber  = !order.IsColumnNull("LetterNo") ? order.LetterNo : "";
            request.RequestedDate = order.EurDate;

            request.Client      = _clientRepository.FindSingle(order.RequestedBy);
            request.Mode        = _modeService.GetEnum(order.FromStore);
            request.PaymentTerm = _paymentTermService.FindSingle(order.PaymentTypeID);
            request.OrderStatus = _orderStatusService.GetEnum(order.OrderStatusID);

            var orderDetail = new OrderDetail();

            orderDetail.LoadAllByOrderID(requestID);
            orderDetail.Rewind();
            request.RequestDetails = new Collection <Domain.RequestDetail>();
            while (!orderDetail.EOF)
            {
                var item          = _itemRepository.FindSingle(orderDetail.ItemID);
                var unitOfIssue   = _unitOfIssueRepository.FindSingle(orderDetail.UnitID);
                var requestDetail = new Domain.RequestDetail()
                {
                    RequestDetailId   = orderDetail.ID,
                    Item              = item,
                    Unit              = unitOfIssue,
                    RequestedQuantity = orderDetail.Pack,
                    ApprovedQuantity  = !orderDetail.IsColumnNull("ApprovedQuantity") ? orderDetail.ApprovedQuantity : orderDetail.Pack,
                    IsFirstLoad       = orderDetail.IsColumnNull("ApprovedQuantity"),
                    ActivityGroup     = orderDetail.IsColumnNull(OrderDetail.ColumnNames.StoreID)?null:_activityRepository.FindSingle(orderDetail.StoreID, orderDetail.DeliveryNote),
                    ExpiryDate        =
                        !orderDetail.IsColumnNull("PreferredExpiryDate")
                                                    ? orderDetail.PreferredExpiryDate
                                                    : (DateTime?)null,
                    Manufacturer =
                        !orderDetail.IsColumnNull("PreferredManufacturerID")
                                                    ? _manufacturerRepository.FindSingle(
                            orderDetail.PreferredManufacturerID)
                                                    : null,
                    physicalStore =
                        !orderDetail.IsColumnNull("PreferredPhysicalStoreID")
                                                    ? _physicalStoreRepository.FindSingle(
                            orderDetail.PreferredPhysicalStoreID)
                                                    : null
                };
                request.RequestDetails.Add(requestDetail);
                orderDetail.MoveNext();
            }



            return(request);
        }
        public Domain.Request FindSingle(int requestID)
        {
            var request = new Domain.Request();
            var order = new Order();
            order.LoadByPrimaryKey(requestID);

            request.RequestID = order.ID;
            request.OrderNumber = order.RefNo;
            request.LetterNumber = !order.IsColumnNull("LetterNo") ? order.LetterNo : "";
            request.RequestedDate = order.EurDate;

            request.Client = _clientRepository.FindSingle(order.RequestedBy);
            request.Mode = _modeService.GetEnum(order.FromStore);
            request.PaymentTerm = _paymentTermService.FindSingle(order.PaymentTypeID);
            request.OrderStatus = _orderStatusService.GetEnum(order.OrderStatusID);

            var orderDetail = new OrderDetail();
            orderDetail.LoadAllByOrderID(requestID);
            orderDetail.Rewind();
            request.RequestDetails = new Collection<Domain.RequestDetail>();
            while (!orderDetail.EOF)
            {
                var item = _itemRepository.FindSingle(orderDetail.ItemID);
                var unitOfIssue = _unitOfIssueRepository.FindSingle(orderDetail.UnitID);
                var requestDetail = new Domain.RequestDetail()
                                        {

                                            RequestDetailId = orderDetail.ID,
                                            Item = item,
                                            Unit = unitOfIssue,
                                            RequestedQuantity = orderDetail.Pack,
                                            ApprovedQuantity = !orderDetail.IsColumnNull("ApprovedQuantity") ? orderDetail.ApprovedQuantity : orderDetail.Pack,
                                            IsFirstLoad = orderDetail.IsColumnNull("ApprovedQuantity"),
                                            ActivityGroup = orderDetail.IsColumnNull(OrderDetail.ColumnNames.StoreID)?null:_activityRepository.FindSingle(orderDetail.StoreID,orderDetail.DeliveryNote),
                                            ExpiryDate =
                                                !orderDetail.IsColumnNull("PreferredExpiryDate")
                                                    ? orderDetail.PreferredExpiryDate
                                                    : (DateTime?)null,
                                            Manufacturer =
                                                !orderDetail.IsColumnNull("PreferredManufacturerID")
                                                    ? _manufacturerRepository.FindSingle(
                                                        orderDetail.PreferredManufacturerID)
                                                    : null,
                                            physicalStore =
                                                !orderDetail.IsColumnNull("PreferredPhysicalStoreID")
                                                    ? _physicalStoreRepository.FindSingle(
                                                        orderDetail.PreferredPhysicalStoreID)
                                                    : null

                                        };
                request.RequestDetails.Add(requestDetail);
            orderDetail.MoveNext();
            }

            return request;
        }
Beispiel #3
0
        public static RequestBuilder Create(ModuleBuilder module, string name)
        {
            if (module is null)
            {
                throw new ArgumentNullException(nameof(module));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(nameof(name));
            }

            var request = new Domain.Request(name);

            return(new RequestBuilder(module, request));
        }
Beispiel #4
0
 public RequestBuilder(ModuleBuilder module, Domain.Request request)
 {
     Build    = module ?? throw new ArgumentNullException(nameof(module));
     Instance = request ?? throw new ArgumentNullException(nameof(request));
 }
Beispiel #5
0
        public void TestRequestValidation()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser      = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));

            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // when incorrect instance request
            var result = browser.Put("notmock/routing/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not found
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);

            // when empty request
            result = browser.Get("mock/routing/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1;1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "a,1");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect vehicle.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "novehiclehere");
                with.Query("loc", "1,1");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with 0 locations.
            var request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] { 1, 1 }
                },
                profile = new Domain.Profile()
                {
                    vehicle = "car"
                }
            };

            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with invalid vehicle.
            request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] { 1, 1 },
                    new double[] { 2, 2 }
                },
                profile = new Domain.Profile()
                {
                    vehicle = "novehiclehere"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with unsupported vehicle.
            request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] { 1, 1 },
                    new double[] { 2, 2 }
                },
                profile = new Domain.Profile()
                {
                    vehicle = "pedestrian"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
        }
        public void Save(Domain.Request _request, int userID, Domain.OrderStatus orderStatus = Domain.OrderStatus.Submitted)
        {
            var order = new Order();

            order.LoadByPrimaryKey(_request.RequestID);

            var dborderStatus = new BLL.OrderStatus();

            dborderStatus.LoadByCode(EnumService <Domain.OrderStatus> .GetCode(orderStatus));

            order.ChangeStatus(dborderStatus.ID, CurrentContext.UserId);
            order.ApprovedBy   = userID;
            order.ApprovedDate = DateTimeHelper.ServerDateTime;

            var orderDetail = new OrderDetail();

            orderDetail.LoadAllByOrderID(_request.RequestID);
            while (!orderDetail.EOF)
            {
                var requestDetail = _request.RequestDetails.SingleOrDefault(r => r.RequestDetailId == orderDetail.ID);
                if (requestDetail == null)
                {
                    orderDetail.MarkAsDeleted();
                }
                else
                {
                    //Set Activity
                    if (requestDetail.ActivityGroup != null)
                    {
                        orderDetail.StoreID      = requestDetail.ActivityGroup.Activity.ActivityID;
                        orderDetail.DeliveryNote = requestDetail.ActivityGroup.IsDeliveryNote;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("StoreID");
                    }
                    //Set Manufacturer
                    if (requestDetail.Manufacturer != null)
                    {
                        orderDetail.PreferredManufacturerID = requestDetail.Manufacturer.ManufacturerID;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("PreferredManufacturerID");
                    }

                    //Set PhysicalStore
                    if (requestDetail.physicalStore != null)
                    {
                        orderDetail.PreferredPhysicalStoreID = requestDetail.physicalStore.PhysicalStoreID;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("PreferredPhysicalStoreID");
                    }

                    //Set ExpiryDate
                    if (requestDetail.ExpiryDate.HasValue)
                    {
                        orderDetail.PreferredExpiryDate = requestDetail.ExpiryDate.Value;
                    }
                    else
                    {
                        orderDetail.SetColumnNull("PreferredExpiryDate");
                    }
                    orderDetail.ApprovedQuantity = requestDetail.ApprovedQuantity;
                    orderDetail.StockedOut       = requestDetail.StockedOut;
                }

                orderDetail.MoveNext();
            }
            foreach (var requestDetail in _request.RequestDetails.Where(r => r.RequestDetailId == 0).ToList())
            {
                orderDetail.AddNew();
                orderDetail.ItemID           = requestDetail.Item.ItemID;
                orderDetail.UnitID           = requestDetail.Unit.UnitID;
                orderDetail.Pack             = requestDetail.RequestedQuantity;
                orderDetail.OrderID          = order.ID;
                orderDetail.QtyPerPack       = 1;
                orderDetail.Quantity         = requestDetail.RequestedQuantity;
                orderDetail.ApprovedQuantity = requestDetail.ApprovedQuantity;
                orderDetail.StockedOut       = requestDetail.StockedOut;
                if (requestDetail.ActivityGroup != null)
                {
                    orderDetail.StoreID      = requestDetail.ActivityGroup.Activity.ActivityID;
                    orderDetail.DeliveryNote = requestDetail.ActivityGroup.IsDeliveryNote;
                }
                else
                {
                    orderDetail.SetColumnNull("StoreID");
                }
                //Set Manufacturer
                if (requestDetail.Manufacturer != null)
                {
                    orderDetail.PreferredManufacturerID = requestDetail.Manufacturer.ManufacturerID;
                }
                else
                {
                    orderDetail.SetColumnNull("PreferredManufacturerID");
                }

                //Set PhysicalStore
                if (requestDetail.physicalStore != null)
                {
                    orderDetail.PreferredPhysicalStoreID = requestDetail.physicalStore.PhysicalStoreID;
                }
                else
                {
                    orderDetail.SetColumnNull("PreferredPhysicalStoreID");
                }

                //Set ExpiryDate
                if (requestDetail.ExpiryDate.HasValue)
                {
                    orderDetail.PreferredExpiryDate = requestDetail.ExpiryDate.Value;
                }
                else
                {
                    orderDetail.SetColumnNull("PreferredExpiryDate");
                }
            }


            order.Save();
            orderDetail.Save();
        }
        public async Task <IActionResult> Create([FromBody] CreateRequest createRequest)
        {
            var user = await this.GetUserAuthenticated(db);

            if (user is null)
            {
                return(Forbid());
            }
            //
            var invalid = false;

            invalid |= string.IsNullOrWhiteSpace(createRequest.DeliveryAddress);
            invalid |= createRequest.DeliveryDate != null && createRequest.DeliveryDate <= DateTime.Now;
            invalid |= createRequest.Items.Any(x => x.ProductId == Guid.Empty || x.Amount < 1);
            if (invalid)
            {
                return(BadRequest());
            }
            //
            var store = await db.Stores
                        .Include(x => x.Users)
                        .FirstOrDefaultAsync(x => x.Id == createRequest.StoreId);

            var products = await db.Products.ToArrayAsync();

            // TODO Validate if that product is in that store
            //
            var request = new Domain.Request
            {
                CreatedAt       = DateTime.Now,
                User            = user,
                Store           = store,
                DeliveryAddress = createRequest.DeliveryAddress,
                DeliveryDate    = createRequest.DeliveryDate,
                Status          = Domain.RequestStatus.Sent,
                Items           = createRequest.Items.Select(x =>
                {
                    var product = products.First(y => y.Id == x.ProductId);
                    return(new Domain.RequestItem
                    {
                        Amount = x.Amount,
                        Product = product,
                        Price = product.Price,
                    });
                })
                                  .ToArray(),
            };
            //
            await db.Requests.AddAsync(request);

            await db.SaveChangesAsync();

            //
            var storeUserIds = store.Users.Select(x => x.UserId);
            var tasks        = new List <Task>();

            foreach (var storeUserId in storeUserIds)
            {
                tasks.Add(this.NotifyUser(db, configuration, storeUserId, $"Request from {user.Name} worth R$ {request.Items.Sum(x => x.Price * x.Amount)}", "Access Orders Page for further details"));
            }
            await Task.WhenAll(tasks);

            //
            return(Ok());
        }
 public void Update(Domain.Request updatedDomain)
 {
     base.Save <Data.Models.Request>(updatedDomain.ToEntity(), updatedDomain.Id);
 }
 public Domain.Request Add(Domain.Request domain)
 {
     return(base.Add <Data.Models.Request>(domain.ToEntity()).ToDomain());
 }
        public void TestRequestValidation()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));
            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // when incorrect instance request
            var result = browser.Put("notmock/routing/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not found
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);

            // when empty request
            result = browser.Get("mock/routing/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1;1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "a,1");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect vehicle.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "novehiclehere");
                with.Query("loc", "1,1");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with 0 locations.
            var request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] {1, 1}
                },
                profile = new Domain.Profile()
                {
                    vehicle = "car"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with invalid vehicle.
            request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] {1, 1},
                    new double[] {2, 2}
                },
                profile = new Domain.Profile()
                {
                    vehicle = "novehiclehere"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with unsupported vehicle.
            request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] {1, 1},
                    new double[] {2, 2}
                },
                profile = new Domain.Profile()
                {
                    vehicle = "pedestrian"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
        }