public HttpResponseMessage LockReceivingReceipt(Entities.TrnReceivingReceipt objReceivingReceipt, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("ReceivingReceiptDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var receivingReceipt = from d in db.TrnReceivingReceipts
                                                   where d.Id == Convert.ToInt32(id)
                                                   select d;

                            if (receivingReceipt.Any())
                            {
                                int countInvalidPO = 0;
                                var invalidPOs     = from d in db.TrnReceivingReceiptItems
                                                     where d.RRId == receivingReceipt.FirstOrDefault().Id &&
                                                     d.TrnPurchaseOrder.IsLocked == false &&
                                                     d.TrnPurchaseOrder.IsCancelled == true
                                                     select d;

                                if (invalidPOs.Any())
                                {
                                    countInvalidPO = invalidPOs.Count();
                                }

                                if (!receivingReceipt.FirstOrDefault().IsLocked&& countInvalidPO == 0)
                                {
                                    String oldObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());

                                    Decimal amount = 0, WTAXAmount = 0;
                                    var     receivingReceiptItems = from d in db.TrnReceivingReceiptItems where d.RRId == Convert.ToInt32(id) select d;
                                    if (receivingReceiptItems.Any())
                                    {
                                        amount = receivingReceiptItems.Sum(d => d.Amount); WTAXAmount = receivingReceiptItems.Sum(d => d.WTAXAmount);
                                    }

                                    var lockReceivingReceipt      = receivingReceipt.FirstOrDefault();
                                    lockReceivingReceipt.RRDate            = Convert.ToDateTime(objReceivingReceipt.RRDate);
                                    lockReceivingReceipt.DocumentReference = objReceivingReceipt.DocumentReference;
                                    lockReceivingReceipt.SupplierId        = objReceivingReceipt.SupplierId;
                                    lockReceivingReceipt.TermId            = objReceivingReceipt.TermId;
                                    lockReceivingReceipt.Remarks           = objReceivingReceipt.Remarks;
                                    lockReceivingReceipt.ManualRRNumber    = objReceivingReceipt.ManualRRNumber;
                                    lockReceivingReceipt.Amount            = amount;
                                    lockReceivingReceipt.WTaxAmount        = WTAXAmount;
                                    lockReceivingReceipt.ReceivedById      = objReceivingReceipt.ReceivedById;
                                    lockReceivingReceipt.CheckedById       = objReceivingReceipt.CheckedById;
                                    lockReceivingReceipt.ApprovedById      = objReceivingReceipt.ApprovedById;
                                    lockReceivingReceipt.Status            = objReceivingReceipt.Status;
                                    lockReceivingReceipt.IsLocked          = true;
                                    lockReceivingReceipt.UpdatedById       = currentUserId;
                                    lockReceivingReceipt.UpdatedDateTime   = DateTime.Now;
                                    db.SubmitChanges();

                                    if (lockReceivingReceipt.IsLocked)
                                    {
                                        UpdatePurchaseOrderStatus(Convert.ToInt32(id));

                                        accountsPayable.UpdateAccountsPayable(Convert.ToInt32(id));
                                        inventory.InsertReceivingReceiptInventory(Convert.ToInt32(id));
                                        journal.InsertReceivingReceiptJournal(Convert.ToInt32(id));
                                    }

                                    if (!currentUser.FirstOrDefault().MstCompany.IsRRCanReceiveOverPOQuantity)
                                    {
                                        Decimal pquantity = 0, rquantity = 0;

                                        Boolean isExceedQuantity = false;
                                        var     receivedItems    = from d in db.TrnReceivingReceiptItems
                                                                   where d.RRId == receivingReceipt.FirstOrDefault().Id
                                                                   select d;

                                        if (receivedItems.Any())
                                        {
                                            foreach (var receivedItem in receivedItems)
                                            {
                                                Decimal totalQuantityPurchasedItem = 0, totalQuantityReceivedItem = 0;

                                                var purchasedQuantityItem = from d in db.TrnPurchaseOrderItems
                                                                            where d.POId == receivedItem.POId &&
                                                                            d.ItemId == receivedItem.ItemId &&
                                                                            d.TrnPurchaseOrder.IsLocked == true
                                                                            select d;

                                                if (purchasedQuantityItem.Any())
                                                {
                                                    totalQuantityPurchasedItem = purchasedQuantityItem.Sum(d => d.Quantity);

                                                    var receivedQuantityItem = from d in db.TrnReceivingReceiptItems
                                                                               where d.POId == receivedItem.POId &&
                                                                               d.ItemId == receivedItem.ItemId &&
                                                                               d.TrnReceivingReceipt.IsLocked == true
                                                                               select d;

                                                    if (receivedQuantityItem.Any())
                                                    {
                                                        totalQuantityReceivedItem = receivedQuantityItem.Sum(d => d.Quantity);
                                                    }
                                                }

                                                if (totalQuantityReceivedItem > totalQuantityPurchasedItem)
                                                {
                                                    isExceedQuantity = true;

                                                    pquantity = totalQuantityPurchasedItem;
                                                    rquantity = totalQuantityReceivedItem;

                                                    break;
                                                }
                                            }
                                        }

                                        if (isExceedQuantity)
                                        {
                                            db.Refresh(RefreshMode.OverwriteCurrentValues, receivingReceipt);

                                            var unlockReceivingReceipt = receivingReceipt.FirstOrDefault();
                                            unlockReceivingReceipt.IsLocked        = false;
                                            unlockReceivingReceipt.UpdatedById     = currentUserId;
                                            unlockReceivingReceipt.UpdatedDateTime = DateTime.Now;
                                            db.SubmitChanges();

                                            if (!unlockReceivingReceipt.IsLocked)
                                            {
                                                UpdatePurchaseOrderStatus(Convert.ToInt32(id));

                                                accountsPayable.UpdateAccountsPayable(Convert.ToInt32(id));
                                                inventory.DeleteReceivingReceiptInventory(Convert.ToInt32(id));
                                                journal.DeleteReceivingReceiptJournal(Convert.ToInt32(id));
                                            }

                                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. Cannot receive more than purchased item quantity." + " Purchased Qty: " + pquantity + ", Received Qty: " + rquantity));
                                        }
                                        else
                                        {
                                            String newObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());
                                            auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                                            return(Request.CreateResponse(HttpStatusCode.OK));
                                        }
                                    }
                                    else
                                    {
                                        String newObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());
                                        auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                                        return(Request.CreateResponse(HttpStatusCode.OK));
                                    }
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These receiving receipt details are already locked or PO is invalid.."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These receiving receipt details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock receiving receipt."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this receiving receipt page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }