// GET: api/Batches
        /// Send a collection of active BatchItemDTO's
        /// Currently purchase Price does not exist in domain



        public IQueryable <BatchItemDTO> GetAllBatches()
        {
            var all = db.Batches.Where(b => b.Active == true);
            List <BatchItemDTO> DTO = new List <BatchItemDTO>();

            foreach (var item in all)
            {
                var b = new BatchItemDTO {
                    Id             = item.Id,
                    Sku            = item.Sku,
                    Name           = item.Name,
                    Location       = item.Location,
                    FormSize       = item.FormSize,
                    FormSizeCode   = item.FormSizeCode,
                    PurchasePrice  = ConvertMe(item.BuyPrice),
                    WholesalePrice = item.WholesalePrice,
                    Comment        = item.Comment
                };
                DTO.Add(b);
            }
            ;
            return(DTO.AsQueryable());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Edit(BatchEditVM batch)
        {
            var locations = ServiceLayer.BatchService.GetLocations();

            if (ModelState.IsValid)
            {
                var mainLocation = batch.MainLocation;
                var subLocation  = batch.SubLocation;
                var mainexists   = locations.Any(l => l.MainLocation == mainLocation);
                if (mainexists)
                {
                    var subexists = locations.Any(l => l.SubLocation == subLocation);
                    if (subexists)
                    {
                        // var subexists = locations.Any(l => l.SubLocation == subLocation);
                        var sublocations = locations.Where(l => l.MainLocation == mainLocation);
                        //  var sublocations = ServiceLayer.BatchService.GetSubLocations(mainLocation);
                        var subvalid = sublocations.Any(l => l.SubLocation == subLocation);
                        if (!(subvalid))
                        {
                            ModelState.AddModelError(string.Empty, "Sub Location Does NOT exist for this Location");
                            return(View(batch));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Sub Location Does NOT exist");
                        return(View(batch));
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Main Location Does NOT exist");
                    return(View(batch));
                }


                /// This starts to build the DTO
                /// it collates the Location using a space seperator
                /// if its for sale quantity is quantity if not quantity is 0
                /// if its not for sale growing quantity is quantity if not growing quantity is 0
                /// so either quantity has a value or growing quantity has a value > 0 never both
                BatchService.Models.BatchItemDTO batchDTO = new BatchItemDTO
                {
                    Id              = batch.BatchId,
                    Sku             = batch.Sku,
                    FormSize        = batch.FormSize,
                    FormSizeCode    = batch.FormSizeCode,
                    Name            = batch.Name,
                    Location        = mainLocation + " " + subLocation,
                    Quantity        = (batch.forSale) ? batch.Quantity : 0,
                    GrowingQuantity = (!(batch.forSale)) ? batch.Quantity : 0,
                };


                /// Two possible senarios
                /// One the current item is a PB item : we add the new local stock item to the DB
                /// and keep the origional PB un touched
                /// Two the current item is Local : we update the existing local stock item
                using (var client = new HttpClient())
                {
                    //  https://ahillsbatchservice.azurewebsites.net/
                    client.BaseAddress = new Uri("https://ahillsbatchservice.azurewebsites.net/");
                    //client.BaseAddress = new Uri("http://localhost:52009/");

                    if (batch.wasPB == true)
                    {
                        /// I am a PB so POST and ADD
                        var response = await client.PostAsJsonAsync("api/Batches/New", batchDTO);

                        if (response.IsSuccessStatusCode)
                        {
                            // this ensures the UI presents the last used location
                            Session["LastLocation"] = mainLocation;
                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, "Server error try after some time.");
                        }
                        ///
                    }
                    else
                    {
                        /// I am local so PUT and UPDATE
                        BatchService.Models.BatchLocationDTO updateDTO = new BatchService.Models.BatchLocationDTO();
                        updateDTO.BatchId  = batch.BatchId;
                        updateDTO.Location = batch.MainLocation + " " + batch.SubLocation;

                        updateDTO.Quantity        = (batch.forSale) ? batch.Quantity : 0;
                        updateDTO.GrowingQuantity = (!(batch.forSale)) ? batch.Quantity : 0;

                        var response = await client.PutAsJsonAsync("api/Batches/location", updateDTO);

                        if (response.IsSuccessStatusCode)
                        {
                            // this ensures the UI presents the last used location
                            Session["LastLocation"] = mainLocation;
                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, "Server error try after some time.");
                        }
                        ///
                    }
                }
                Session["LastLocation"] = mainLocation;
                return(RedirectToAction("Index"));
            }
            return(View(batch));
        }