// POST api/RequisitionDeliveryDescription public HttpResponseMessage PostRequisitionDeliveryDescription(RequisitionDeliveryDescription requisitiondeliverydescription) { // long RequisitionID=(long)db.RequisitionDeliveries.Where(r=>r.RequisitionDeliveryID==requisitiondeliverydescription.RequisitionDeliveryID).Select(s=>s.PurchaseRequisitionID).SingleOrDefault(); PurchaseRequisitionDescription purchaseRequisitionDescription = db.PurchaseRequisitionDescriptions.Where(r => (r.PurchaseRequisitionID == requisitiondeliverydescription.PurchaseRequisitionID) && (r.ProductID == requisitiondeliverydescription.ProductID)).SingleOrDefault(); if (ModelState.IsValid) { db.Entry(requisitiondeliverydescription).State = requisitiondeliverydescription.RequisitionDeliveryDescriptionID == 0 ? EntityState.Added : EntityState.Modified; //db.RequisitionDeliveryDescriptions.Add(requisitiondeliverydescription); requisitiondeliverydescription.InsertBy = loginUser.UserID; db.SaveChanges(); var DeliveredQty = (db.RequisitionDeliveryDescriptions.Where(r => (r.ProductID == requisitiondeliverydescription.ProductID) && (r.PurchaseRequisitionID == requisitiondeliverydescription.PurchaseRequisitionID)).Select(r => r.Quantity)).ToList().Sum(); var ReceivedQuantity = (db.RequisitionDeliveryDescriptions.Where(r => (r.ProductID == requisitiondeliverydescription.ProductID) && (r.PurchaseRequisitionID == requisitiondeliverydescription.PurchaseRequisitionID)).Select(r => r.ReceivedQuantity)).ToList().Sum(); purchaseRequisitionDescription.DeliveredQuantity = (float)DeliveredQty; purchaseRequisitionDescription.ReceivedQuantity = (float)ReceivedQuantity; db.Entry(purchaseRequisitionDescription).State = EntityState.Modified; db.SaveChanges(); if (requisitiondeliverydescription.ReceivedQuantity > 0) { Product product = db.Products.Where(p => p.ProductID == requisitiondeliverydescription.ProductID).SingleOrDefault(); product.CurrentStock -= requisitiondeliverydescription.ReceivedQuantity; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, requisitiondeliverydescription); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = requisitiondeliverydescription.RequisitionDeliveryDescriptionID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/RequisitionDeliveryDescription/5 public HttpResponseMessage PutRequisitionDeliveryDescription(long id, RequisitionDeliveryDescription requisitiondeliverydescription) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != requisitiondeliverydescription.RequisitionDeliveryDescriptionID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } requisitiondeliverydescription.UpdateBy = loginUser.UserID; db.Entry(requisitiondeliverydescription).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }