public HttpResponseMessage unlockCollection(String id, Models.TrnCollection collection)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var collections = from d in db.TrnCollections where d.Id == Convert.ToInt32(id) select d;
                if (collections.Any())
                {
                    var updateCollection = collections.FirstOrDefault();
                    updateCollection.IsLocked        = false;
                    updateCollection.UpdatedById     = userId;
                    updateCollection.UpdatedDateTime = DateTime.Now;

                    db.SubmitChanges();

                    journal.DeleteOfficialReceiptJournal(Convert.ToInt32(id));
                    UpdateARCollection(Convert.ToInt32(id));

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public HttpResponseMessage updateCollection(String id, Models.TrnCollection collection)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var collections = from d in db.TrnCollections where d.Id == Convert.ToInt32(id) select d;
                if (collections.Any())
                {
                    var updateCollection = collections.FirstOrDefault();
                    updateCollection.BranchId        = collection.BranchId;
                    updateCollection.ORNumber        = collection.ORNumber;
                    updateCollection.ORDate          = Convert.ToDateTime(collection.ORDate);
                    updateCollection.CustomerId      = collection.CustomerId;
                    updateCollection.Particulars     = collection.Particulars;
                    updateCollection.ManualORNumber  = collection.ManualORNumber;
                    updateCollection.PreparedById    = collection.PreparedById;
                    updateCollection.CheckedById     = collection.CheckedById;
                    updateCollection.ApprovedById    = collection.ApprovedById;
                    updateCollection.IsLocked        = true;
                    updateCollection.UpdatedById     = userId;
                    updateCollection.UpdatedDateTime = DateTime.Now;

                    db.SubmitChanges();

                    journal.InsertOfficialReceiptJournal(Convert.ToInt32(id));
                    UpdateARCollection(Convert.ToInt32(id));

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public Int32 insertCollection(Models.TrnCollection collection)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var lastORNumber   = from d in db.TrnCollections.OrderByDescending(d => d.Id) where d.BranchId == currentBranchId() select d;
                var ORNumberResult = "0000000001";

                if (lastORNumber.Any())
                {
                    var ORNumber = Convert.ToInt32(lastORNumber.FirstOrDefault().ORNumber) + 0000000001;
                    ORNumberResult = zeroFill(ORNumber, 10);
                }

                Data.TrnCollection newCollection = new Data.TrnCollection();
                newCollection.BranchId        = currentBranchId();
                newCollection.ORNumber        = ORNumberResult;
                newCollection.ORDate          = DateTime.Today;
                newCollection.CustomerId      = (from d in db.MstArticles where d.ArticleTypeId == 2 select d.Id).FirstOrDefault();
                newCollection.Particulars     = "NA";
                newCollection.ManualORNumber  = "NA";
                newCollection.PreparedById    = userId;
                newCollection.CheckedById     = userId;
                newCollection.ApprovedById    = userId;
                newCollection.IsLocked        = false;
                newCollection.CreatedById     = userId;
                newCollection.CreatedDateTime = DateTime.Now;
                newCollection.UpdatedById     = userId;
                newCollection.UpdatedDateTime = DateTime.Now;

                db.TrnCollections.InsertOnSubmit(newCollection);
                db.SubmitChanges();

                return(newCollection.Id);
            }
            catch
            {
                return(0);
            }
        }