// POST api/PurchaseDeliveryReceiveDescription
        public HttpResponseMessage PostPurchaseDeliveryReceiveDescription(PurchaseDeliveryReceiveDescription purchasedeliveryreceivedescription)
        {
            if (ModelState.IsValid)
            {

                PurchaseOrderDescription purchaseOrderDescription = db.PurchaseOrderDescriptions.Where(r => (r.PurchaseOrderID == purchasedeliveryreceivedescription.PurchaseOrderID) && (r.ProductID == purchasedeliveryreceivedescription.ProductID)).SingleOrDefault();
                Product procuct = db.Products.Where(p => p.ProductID == purchasedeliveryreceivedescription.ProductID).SingleOrDefault();
                purchasedeliveryreceivedescription.UOM = null;
                purchasedeliveryreceivedescription.InsertBy = loginUser.UserID;

                db.Entry(purchasedeliveryreceivedescription).State = purchasedeliveryreceivedescription.PurchaseDeliveryReceiveDescriptionID == 0 ?
                  EntityState.Added : EntityState.Modified;
                db.SaveChanges();
                var ReceivedQuantity = (db.PurchaseDeliveryReceiveDescriptions.Where(r => (r.ProductID == purchasedeliveryreceivedescription.ProductID) && (r.PurchaseOrderID == purchasedeliveryreceivedescription.PurchaseOrderID)).Select(r => r.Quantity)).ToList().Sum();
                purchaseOrderDescription.ReceivedQuantity = ReceivedQuantity;

                db.Entry(purchaseOrderDescription).State = EntityState.Modified;
                db.SaveChanges();
                if (procuct.CurrentStock==null)
                {
                    procuct.CurrentStock = 0;
                }
                procuct.CurrentStock += purchasedeliveryreceivedescription.Quantity;
                db.Entry(procuct).State = EntityState.Modified;
                db.SaveChanges();

                var PandingReceive = (db.PurchaseOrderDescriptions.Where(r => (r.PurchaseOrderID == purchasedeliveryreceivedescription.PurchaseOrderID)).Select(r => r.Quantity)).ToList().Sum();
                var ReceivedQTY= (db.PurchaseDeliveryReceiveDescriptions.Where(r =>  (r.PurchaseOrderID == purchasedeliveryreceivedescription.PurchaseOrderID)).Select(r => r.Quantity)).ToList().Sum();

                if(PandingReceive==ReceivedQTY)
                {
                    PurchaseOrder purchaseOrder = db.PurchaseOrders.Find(purchasedeliveryreceivedescription.PurchaseOrderID);
                    purchaseOrder.IsReceived = true;
                    db.Entry(purchaseOrder).State = EntityState.Modified;
                    db.SaveChanges();

                }
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, purchasedeliveryreceivedescription);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = purchasedeliveryreceivedescription.PurchaseDeliveryReceiveDescriptionID }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        // PUT api/PurchaseDeliveryReceiveDescription/5
        public HttpResponseMessage PutPurchaseDeliveryReceiveDescription(long id, PurchaseDeliveryReceiveDescription purchasedeliveryreceivedescription)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != purchasedeliveryreceivedescription.PurchaseDeliveryReceiveDescriptionID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            purchasedeliveryreceivedescription.UpdateBy = loginUser.UserID;
            db.Entry(purchasedeliveryreceivedescription).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }