public HttpResponseMessage Post(HttpRequestMessage request, [FromBody] SelectedPickListDTO picklist)
        {
            if (!ModelState.IsValid)
            {
                return(request.CreateResponse(HttpStatusCode.BadRequest, picklist));
            }

            try
            {
                var newPickList = new Picklist
                {
                    QuoteId           = picklist.QuoteId,
                    DispatchDate      = picklist.DipatchDate,
                    DeliveryAddress   = picklist.DeliveryAddress,
                    DeliveryNeeded    = picklist.DeliveryNeeded,
                    IsPicked          = picklist.IsPicked,
                    IsPacked          = picklist.IsPacked,
                    IsDelivered       = picklist.IsPacked,
                    Comment           = picklist.Comment,
                    EstimatedDelivery = picklist.EsimatedDelivery,
                    Active            = true //Default to true as initial save
                };

                db.Picklists.Add(newPickList);
                db.SaveChanges();
                db.Entry(newPickList).Reload();

                var pickListId = newPickList.PicklistId;
                foreach (var plant in picklist.PickListPlants)
                {
                    var thisPlant = new PlantsForPicklist
                    {
                        PicklistId       = pickListId,
                        PlantForQuoteId  = plant.PlantForQuoteId,
                        PlantName        = plant.PlantName,
                        FormSize         = plant.FormSize,
                        QuantityToPick   = plant.QuantityToPick,
                        SubbedFor        = plant.SubbedFor,
                        isSubbed         = plant.IsSubbed,
                        DispatchLocation = plant.DispatchLocation,
                        Active           = true
                    };
                    db.PlantsForPicklists.Add(thisPlant);
                }
                db.SaveChanges();


                return(request.CreateResponse(HttpStatusCode.OK, picklist));
            }
            catch (Exception ex)
            {
                return(request.CreateResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemple #2
0
        public IHttpActionResult PutPickItems(HttpRequestMessage request, SelectedPickListDTO picklistItems)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!PickListExists(picklistItems.PicklistId))
            {
                return(NotFound());
            }

            var currentPicklist = db.Picklists.Where(x => x.PicklistId == picklistItems.PicklistId).FirstOrDefault();
            //Will always default to true, meaning that all the plants on the picklist have been picked
            //If not it will be set to false in foreach statement if a plant has only been partially picked
            bool pickedState = true;

            foreach (var item in picklistItems.PickListPlants)
            {
                //Saving the amount that has been picked to each plant on the current picklist
                var plantToChange = db.PlantsForPicklists.Where(x => x.PlantForPickListId == item.PlantForPicklistId).FirstOrDefault();
                if (plantToChange != null)
                {
                    //int dbQuantityPicked = plantToChange.QuantityPicked
                    //int calcQuantityPicked = plantToChange.QuantityPicked += item.QuantityPicked;
                    plantToChange.QuantityPicked += item.QuantityPicked; //Need to change this so it doesnt add
                    //plantToChange.CurrentQuantityPicked = item.QuantityPicked;
                    if (plantToChange.QuantityPicked < plantToChange.QuantityToPick)
                    {
                        pickedState = false;
                    }
                }
                else
                {
                    return(StatusCode(HttpStatusCode.BadRequest));
                }

                db.Entry(plantToChange).State = EntityState.Modified;
            }
            currentPicklist.IsAllocated = false;
            currentPicklist.IsPicked    = pickedState;

            db.Entry(currentPicklist).State = EntityState.Modified;


            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PickListExists(picklistItems.PicklistId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.OK));
        }
Exemple #3
0
        public HttpResponseMessage Post(HttpRequestMessage request, [FromBody] SelectedPickListDTO picklist)
        {
            if (!ModelState.IsValid)
            {
                return(request.CreateResponse(HttpStatusCode.BadRequest, picklist));
            }

            try
            {
                var newPickList = new Picklist
                {
                    QuoteId           = picklist.QuoteId,
                    DispatchDate      = DateTime.ParseExact(picklist.DispatchDate, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    DeliveryAddress   = picklist.DeliveryAddress,
                    DeliveryNeeded    = picklist.DeliveryNeeded,
                    IsPicked          = picklist.IsPicked,
                    IsAllocated       = picklist.IsAllocated,
                    IsDelivered       = picklist.IsDelivered,
                    Comment           = picklist.Comment,
                    EstimatedDelivery = picklist.EstimatedDelivery,
                    Active            = true //Default to true as initial save
                };

                db.Picklists.Add(newPickList);
                db.SaveChanges();
                db.Entry(newPickList).Reload();

                var pickListId = newPickList.PicklistId;
                foreach (var plant in picklist.PickListPlants)
                {
                    var needsPurchasing = false;
                    if (plant.Location == "PB")
                    {
                        needsPurchasing = true;
                    }

                    var thisPlant = new PlantsForPicklist
                    {
                        PicklistId       = pickListId,
                        PlantForQuoteId  = plant.PlantForQuoteId,
                        BatchId          = plant.BatchId,
                        PlantName        = plant.PlantName,
                        FormSize         = plant.FormSize,
                        QuantityToPick   = plant.QuantityToPick,
                        IsSubbed         = plant.IsSubbed,
                        DispatchLocation = plant.DispatchLocation,
                        QuantityPicked   = 0, //Default this to 0 because none will have been picked on creation??
                        NeedsPurchasing  = needsPurchasing,
                        Active           = true
                    };
                    db.PlantsForPicklists.Add(thisPlant);
                }
                db.SaveChanges();


                return(request.CreateResponse(HttpStatusCode.OK, picklist));
            }
            catch (Exception ex)
            {
                return(request.CreateResponse(HttpStatusCode.BadRequest, ex));
            }
        }