Example #1
0
        public HttpResponseMessage insertDisbursementLineAccountsPayable(Models.TrnReceivingReceipt receivingReceipt, String CVId)
        {
            try
            {
                Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                newDisbursementLine.CVId = Convert.ToInt32(CVId);
                newDisbursementLine.BranchId = receivingReceipt.BranchId;
                newDisbursementLine.AccountId = receivingReceipt.AccountId;
                newDisbursementLine.ArticleId = receivingReceipt.SupplierId;
                newDisbursementLine.RRId = receivingReceipt.Id;
                newDisbursementLine.Particulars = receivingReceipt.DocumentReference;
                newDisbursementLine.Amount = receivingReceipt.Amount;

                db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                db.SubmitChanges();

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Example #2
0
        public Int32 insertDisbursementLine(Models.TrnDisbursementLine disbursementLine)
        {
            try
            {
                Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();
                newDisbursementLine.CVId = disbursementLine.CVId;
                newDisbursementLine.BranchId = disbursementLine.BranchId;
                newDisbursementLine.AccountId = disbursementLine.AccountId;
                newDisbursementLine.ArticleId = disbursementLine.ArticleId;
                newDisbursementLine.RRId = disbursementLine.RRId;
                newDisbursementLine.Particulars = disbursementLine.Particulars;
                newDisbursementLine.Amount = disbursementLine.Amount;
                db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                db.SubmitChanges();

                var disbursement = from d in db.TrnDisbursements
                                   where d.Id == disbursementLine.CVId
                                   select d;

                if (disbursement.Any())
                {
                    var updateDisbursement = disbursement.FirstOrDefault();
                    updateDisbursement.Amount = disbursement.FirstOrDefault().TrnDisbursementLines.Sum(d => d.Amount);
                    db.SubmitChanges();
                }

                return newDisbursementLine.Id;
            }
            catch
            {
                return 0;
            }
        }
        public Int32 insertDisbursementLine(Models.TrnDisbursementLine disbursementLine)
        {
            try
            {
                Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                newDisbursementLine.CVId = disbursementLine.CVId;
                newDisbursementLine.BranchId = disbursementLine.BranchId;
                newDisbursementLine.AccountId = disbursementLine.AccountId;
                newDisbursementLine.ArticleId = disbursementLine.ArticleId;
                newDisbursementLine.RRId = disbursementLine.RRId;
                newDisbursementLine.Particulars = disbursementLine.Particulars;
                newDisbursementLine.Amount = disbursementLine.Amount;

                db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                db.SubmitChanges();

                return newDisbursementLine.Id;
            }
            catch
            {
                return 0;
            }
        }
Example #4
0
        public HttpResponseMessage insertDisbursementLineAllAccountsPayable(Models.TrnDisbursementLine disbursementLine, String supplierId, String CVId)
        {
            try
            {
                var receivingReceipts = from d in db.TrnReceivingReceipts
                                        where d.BranchId == currentBranchId()
                                        && d.SupplierId == Convert.ToInt32(supplierId)
                                        && d.BalanceAmount > 0
                                        && d.IsLocked == true
                                        select new Models.TrnReceivingReceipt
                                        {
                                            Id = d.Id,
                                            BranchId = d.BranchId,
                                            Branch = d.MstBranch.Branch,
                                            RRDate = d.RRDate.ToShortDateString(),
                                            RRNumber = d.RRNumber,
                                            SupplierId = d.SupplierId,
                                            Supplier = d.MstArticle.Article,
                                            AccountId = d.MstArticle.AccountId,
                                            TermId = d.TermId,
                                            Term = d.MstTerm.Term,
                                            DocumentReference = d.DocumentReference,
                                            ManualRRNumber = d.ManualRRNumber,
                                            Remarks = d.Remarks,
                                            Amount = d.Amount,
                                            WTaxAmount = d.WTaxAmount,
                                            PaidAmount = d.PaidAmount,
                                            AdjustmentAmount = d.AdjustmentAmount,
                                            BalanceAmount = d.BalanceAmount,
                                            ReceivedById = d.ReceivedById,
                                            ReceivedBy = d.MstUser4.FullName,
                                            PreparedById = d.PreparedById,
                                            PreparedBy = d.MstUser3.FullName,
                                            CheckedById = d.CheckedById,
                                            CheckedBy = d.MstUser1.FullName,
                                            ApprovedById = d.ApprovedById,
                                            ApprovedBy = d.MstUser.FullName
                                        };

                if (receivingReceipts.Any())
                {
                    foreach (var receivingReceipt in receivingReceipts)
                    {
                        Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                        newDisbursementLine.CVId = Convert.ToInt32(CVId);
                        newDisbursementLine.BranchId = receivingReceipt.BranchId;
                        newDisbursementLine.AccountId = receivingReceipt.AccountId;
                        newDisbursementLine.ArticleId = receivingReceipt.SupplierId;
                        newDisbursementLine.RRId = receivingReceipt.Id;
                        newDisbursementLine.Particulars = receivingReceipt.DocumentReference;
                        newDisbursementLine.Amount = receivingReceipt.Amount;

                        db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                        db.SubmitChanges();
                    }

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Example #5
0
        public HttpResponseMessage insertDisbursementLineAdvances(Models.TrnDisbursementLine disbursementLine, String articleId, String CVId)
        {
            try
            {
                var SupplierAdvancesAccountId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.SupplierAdvancesAccountId).SingleOrDefault();

                var journals = from d in db.TrnJournals
                               where d.ArticleId == Convert.ToInt32(articleId)
                               && d.AccountId == SupplierAdvancesAccountId
                               && d.BranchId == currentBranchId()
                               group d by new
                               {
                                   BranchId = d.BranchId,
                                   Branch = d.MstBranch.Branch,
                                   AccountId = d.AccountId,
                                   Account = d.MstAccount.Account,
                                   AccountCode = d.MstAccount.AccountCode,
                                   ArticleId = d.ArticleId,
                                   Article = d.MstArticle.Article,
                                   RRId = d.RRId
                               } into g
                               select new Models.TrnJournal
                               {
                                   BranchId = g.Key.BranchId,
                                   Branch = g.Key.Branch,
                                   AccountId = g.Key.AccountId,
                                   Account = g.Key.Account,
                                   AccountCode = g.Key.AccountCode,
                                   ArticleId = g.Key.ArticleId,
                                   Article = g.Key.Article,
                                   RRId = g.Key.RRId,
                                   DebitAmount = g.Sum(d => d.DebitAmount),
                                   CreditAmount = g.Sum(d => d.CreditAmount),
                                   Balance = g.Sum(d => d.DebitAmount) - g.Sum(d => d.CreditAmount)
                               };

                if (journals.Any())
                {
                    Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                    foreach (var journal in journals)
                    {
                        newDisbursementLine.CVId = Convert.ToInt32(CVId);
                        newDisbursementLine.BranchId = journal.BranchId;
                        newDisbursementLine.AccountId = journal.AccountId;
                        newDisbursementLine.ArticleId = journal.ArticleId;
                        newDisbursementLine.RRId = journal.RRId;
                        newDisbursementLine.Particulars = "Supplier Advances";
                        newDisbursementLine.Amount = journal.Balance * -1;
                    }

                    db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                    db.SubmitChanges();

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        public HttpResponseMessage AddFolderMonitoringDisbursement(List <Entities.FolderMonitoringTrnDisbursement> folderMonitoringTrnDisbursementObjects)
        {
            try
            {
                if (folderMonitoringTrnDisbursementObjects.Any())
                {
                    foreach (var folderMonitoringTrnDisbursementObject in folderMonitoringTrnDisbursementObjects)
                    {
                        Boolean isBranchExist            = false,
                                 isSupplierExist         = false,
                                 isPayTypeExist          = false,
                                 isBankExist             = false,
                                 isUserExist             = false,
                                 isAccountExist          = false,
                                 isArticleExist          = false,
                                 isReceivingReceiptExist = false;

                        IQueryable <Data.TrnReceivingReceipt> receivingReceipt = null;

                        var branch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnDisbursementObject.BranchCode) select d;
                        if (branch.Any())
                        {
                            isBranchExist = true;

                            if (!folderMonitoringTrnDisbursementObject.RRNumber.Equals("") || !folderMonitoringTrnDisbursementObject.RRNumber.Equals("NA"))
                            {
                                receivingReceipt = from d in db.TrnReceivingReceipts where d.BranchId == branch.FirstOrDefault().Id&& d.RRNumber.Equals(folderMonitoringTrnDisbursementObject.RRNumber) && d.IsLocked == true select d;
                                if (receivingReceipt.Any())
                                {
                                    isReceivingReceiptExist = true;
                                }
                            }
                        }

                        var supplier = from d in db.MstArticles where d.ArticleTypeId == 3 && d.ManualArticleCode.Equals(folderMonitoringTrnDisbursementObject.SupplierCode) && d.IsLocked == true select d;
                        if (supplier.Any())
                        {
                            isSupplierExist = true;
                        }

                        var payType = from d in db.MstPayTypes where d.PayType.Equals(folderMonitoringTrnDisbursementObject.PayType) select d;
                        if (payType.Any())
                        {
                            isPayTypeExist = true;
                        }

                        var bank = from d in db.MstArticles where d.ArticleTypeId == 5 && d.ManualArticleCode.Equals(folderMonitoringTrnDisbursementObject.BankCode) select d;
                        if (bank.Any())
                        {
                            isBankExist = true;
                        }

                        var user = from d in db.MstUsers where d.UserName.Equals(folderMonitoringTrnDisbursementObject.UserCode) select d;
                        if (user.Any())
                        {
                            isUserExist = true;
                        }

                        List <easyfis.Entities.MstArticle> listArticles = new List <easyfis.Entities.MstArticle>();

                        var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnDisbursementObject.AccountCode) select d;
                        if (account.Any())
                        {
                            isAccountExist = true;

                            var accountArticleTypes = from d in db.MstAccountArticleTypes where d.AccountId == Convert.ToInt32(account.FirstOrDefault().Id) select d;
                            if (accountArticleTypes.Any())
                            {
                                foreach (var accountArticleType in accountArticleTypes)
                                {
                                    var articles = from d in db.MstArticles where d.ArticleTypeId == accountArticleType.ArticleTypeId && d.IsLocked == true select d;
                                    if (articles.Any())
                                    {
                                        foreach (var articleObject in articles)
                                        {
                                            listArticles.Add(new easyfis.Entities.MstArticle()
                                            {
                                                Id = articleObject.Id,
                                                ManualArticleCode = articleObject.ManualArticleCode,
                                                Article           = articleObject.Article
                                            });
                                        }
                                    }
                                }
                            }
                        }

                        var article = from d in listArticles where d.ManualArticleCode.Equals(folderMonitoringTrnDisbursementObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

                        if (isBranchExist && isSupplierExist && isPayTypeExist && isBankExist && isUserExist && isAccountExist && isArticleExist)
                        {
                            Int32 CVId = 0;

                            var currentDisbursement = from d in db.TrnDisbursements where d.BranchId == branch.FirstOrDefault().Id&& d.ManualCVNumber.Equals(folderMonitoringTrnDisbursementObject.ManualCVNumber) && d.IsLocked == true select d;
                            if (currentDisbursement.Any())
                            {
                                CVId = currentDisbursement.FirstOrDefault().Id;

                                var unlockDisbursement = currentDisbursement.FirstOrDefault();
                                unlockDisbursement.IsLocked        = false;
                                unlockDisbursement.UpdatedById     = user.FirstOrDefault().Id;
                                unlockDisbursement.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CreatedDateTime);
                                db.SubmitChanges();

                                journal.DeleteCashVoucherJournal(CVId);
                            }
                            else
                            {
                                var defaultCVNumber  = "0000000001";
                                var lastDisbursement = from d in db.TrnDisbursements.OrderByDescending(d => d.Id) where d.BranchId == branch.FirstOrDefault().Id select d;
                                if (lastDisbursement.Any())
                                {
                                    var CVNumber = Convert.ToInt32(lastDisbursement.FirstOrDefault().CVNumber) + 0000000001;
                                    defaultCVNumber = FillLeadingZeroes(CVNumber, 10);
                                }

                                Data.TrnDisbursement newDisbursement = new Data.TrnDisbursement
                                {
                                    BranchId        = branch.FirstOrDefault().Id,
                                    CVNumber        = defaultCVNumber,
                                    CVDate          = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CVDate),
                                    ManualCVNumber  = folderMonitoringTrnDisbursementObject.ManualCVNumber,
                                    Payee           = folderMonitoringTrnDisbursementObject.Payee,
                                    SupplierId      = supplier.FirstOrDefault().Id,
                                    PayTypeId       = payType.FirstOrDefault().Id,
                                    Particulars     = folderMonitoringTrnDisbursementObject.Remarks,
                                    BankId          = bank.FirstOrDefault().Id,
                                    CheckNumber     = folderMonitoringTrnDisbursementObject.CheckNumber,
                                    CheckDate       = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CheckDate),
                                    Amount          = 0,
                                    IsCrossCheck    = folderMonitoringTrnDisbursementObject.IsCrossCheck,
                                    PreparedById    = user.FirstOrDefault().Id,
                                    CheckedById     = user.FirstOrDefault().Id,
                                    ApprovedById    = user.FirstOrDefault().Id,
                                    IsClear         = folderMonitoringTrnDisbursementObject.IsClear,
                                    Status          = null,
                                    IsCancelled     = false,
                                    IsPrinted       = false,
                                    IsLocked        = false,
                                    CreatedById     = user.FirstOrDefault().Id,
                                    CreatedDateTime = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CreatedDateTime),
                                    UpdatedById     = user.FirstOrDefault().Id,
                                    UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CreatedDateTime)
                                };

                                db.TrnDisbursements.InsertOnSubmit(newDisbursement);
                                db.SubmitChanges();

                                CVId = newDisbursement.Id;
                            }

                            Int32?RRId = null;
                            if (isReceivingReceiptExist)
                            {
                                RRId = receivingReceipt.FirstOrDefault().Id;
                            }

                            Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine
                            {
                                CVId        = CVId,
                                BranchId    = branch.FirstOrDefault().Id,
                                AccountId   = account.FirstOrDefault().Id,
                                ArticleId   = article.FirstOrDefault().Id,
                                RRId        = RRId,
                                Particulars = folderMonitoringTrnDisbursementObject.Particulars,
                                Amount      = folderMonitoringTrnDisbursementObject.Amount
                            };

                            db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                            db.SubmitChanges();

                            var disbursement = from d in db.TrnDisbursements where d.Id == CVId && d.IsLocked == false select d;
                            if (disbursement.Any())
                            {
                                Decimal amount            = 0;
                                var     disbursementLines = from d in db.TrnDisbursementLines where d.CVId == CVId select d;
                                if (disbursementLines.Any())
                                {
                                    amount = disbursementLines.Sum(d => d.Amount);
                                }

                                var lockDisbursement = disbursement.FirstOrDefault();
                                lockDisbursement.Amount          = amount;
                                lockDisbursement.IsLocked        = true;
                                lockDisbursement.UpdatedById     = user.FirstOrDefault().Id;
                                lockDisbursement.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CreatedDateTime);
                                db.SubmitChanges();

                                if (disbursementLines.Any())
                                {
                                    foreach (var disbursementLine in disbursementLines)
                                    {
                                        if (disbursementLine.RRId != null)
                                        {
                                            accountsPayable.UpdateAccountsPayable(Convert.ToInt32(disbursementLine.RRId));
                                        }
                                    }
                                }

                                journal.InsertCashVoucherJournal(CVId);
                            }
                        }
                    }

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "No data found."));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
        public HttpResponseMessage AddDisbursementLine(Entities.TrnDisbursementLine objDisbursementLine, String CVId)
        {
            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("DisbursementDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanAdd)
                        {
                            var disbursement = from d in db.TrnDisbursements
                                               where d.Id == Convert.ToInt32(CVId)
                                               select d;

                            if (disbursement.Any())
                            {
                                if (!disbursement.FirstOrDefault().IsLocked)
                                {
                                    var accounts = from d in db.MstAccounts.OrderBy(d => d.Account)
                                                   where d.Id == objDisbursementLine.AccountId &&
                                                   d.IsLocked == true
                                                   select d;

                                    if (accounts.Any())
                                    {
                                        var articles = from d in db.MstArticles
                                                       where d.Id == objDisbursementLine.ArticleId &&
                                                       d.IsLocked == true
                                                       select d;

                                        if (articles.Any())
                                        {
                                            Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine
                                            {
                                                CVId        = Convert.ToInt32(CVId),
                                                BranchId    = objDisbursementLine.BranchId,
                                                AccountId   = objDisbursementLine.AccountId,
                                                ArticleId   = objDisbursementLine.ArticleId,
                                                RRId        = objDisbursementLine.RRId,
                                                Particulars = objDisbursementLine.Particulars,
                                                Amount      = objDisbursementLine.Amount,
                                            };

                                            db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                                            db.SubmitChanges();

                                            Decimal disbursementItemTotalAmount = 0;

                                            if (disbursement.FirstOrDefault().TrnDisbursementLines.Any())
                                            {
                                                disbursementItemTotalAmount = disbursement.FirstOrDefault().TrnDisbursementLines.Sum(d => d.Amount);
                                            }

                                            var updateDisbursement = disbursement.FirstOrDefault();
                                            updateDisbursement.Amount = disbursementItemTotalAmount;
                                            db.SubmitChanges();

                                            String newObject = at.GetObjectString(newDisbursementLine);
                                            at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject);

                                            return(Request.CreateResponse(HttpStatusCode.OK));
                                        }
                                        else
                                        {
                                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "No Article."));
                                        }
                                    }
                                    else
                                    {
                                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "No Account."));
                                    }
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "You cannot add new disbursement line if the current disbursement detail is locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "These current disbursement details are not found in the server. Please add new disbursement first before proceeding."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add new disbursement line in this disbursement detail page."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access in this disbursement detail 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."));
            }
        }
        public HttpResponseMessage insertDisbursementLineAccountsPayable(Models.TrnReceivingReceipt receivingReceipt, String CVId)
        {
            try
            {
                Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                newDisbursementLine.CVId = Convert.ToInt32(CVId);
                newDisbursementLine.BranchId = receivingReceipt.BranchId;
                newDisbursementLine.AccountId = receivingReceipt.AccountId;
                newDisbursementLine.ArticleId = receivingReceipt.SupplierId;
                newDisbursementLine.RRId = receivingReceipt.Id;
                newDisbursementLine.Particulars = receivingReceipt.DocumentReference;
                newDisbursementLine.Amount = receivingReceipt.Amount;

                db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                db.SubmitChanges();

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch(Exception e)
            {
                Debug.WriteLine(e);
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        public HttpResponseMessage insertDisbursementLineAllAccountsPayable(Models.TrnDisbursementLine disbursementLine, String supplierId, String CVId)
        {
            try
            {
                var receivingReceipts = from d in db.TrnReceivingReceipts
                                        where d.BranchId == currentBranchId()
                                        && d.SupplierId == Convert.ToInt32(supplierId)
                                        && d.BalanceAmount > 0
                                        && d.IsLocked == true
                                        select new Models.TrnReceivingReceipt
                                        {
                                            Id = d.Id,
                                            BranchId = d.BranchId,
                                            Branch = d.MstBranch.Branch,
                                            RRDate = d.RRDate.ToShortDateString(),
                                            RRNumber = d.RRNumber,
                                            SupplierId = d.SupplierId,
                                            Supplier = d.MstArticle.Article,
                                            AccountId = d.MstArticle.AccountId,
                                            TermId = d.TermId,
                                            Term = d.MstTerm.Term,
                                            DocumentReference = d.DocumentReference,
                                            ManualRRNumber = d.ManualRRNumber,
                                            Remarks = d.Remarks,
                                            Amount = d.Amount,
                                            WTaxAmount = d.WTaxAmount,
                                            PaidAmount = d.PaidAmount,
                                            AdjustmentAmount = d.AdjustmentAmount,
                                            BalanceAmount = d.BalanceAmount,
                                            ReceivedById = d.ReceivedById,
                                            ReceivedBy = d.MstUser4.FullName,
                                            PreparedById = d.PreparedById,
                                            PreparedBy = d.MstUser3.FullName,
                                            CheckedById = d.CheckedById,
                                            CheckedBy = d.MstUser1.FullName,
                                            ApprovedById = d.ApprovedById,
                                            ApprovedBy = d.MstUser.FullName
                                        };

                if (receivingReceipts.Any())
                {
                    foreach (var receivingReceipt in receivingReceipts)
                    {
                        Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                        newDisbursementLine.CVId = Convert.ToInt32(CVId);
                        newDisbursementLine.BranchId = receivingReceipt.BranchId;
                        newDisbursementLine.AccountId = receivingReceipt.AccountId;
                        newDisbursementLine.ArticleId = receivingReceipt.SupplierId;
                        newDisbursementLine.RRId = receivingReceipt.Id;
                        newDisbursementLine.Particulars = receivingReceipt.DocumentReference;
                        newDisbursementLine.Amount = receivingReceipt.Amount;

                        db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                        db.SubmitChanges();
                    }

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        public HttpResponseMessage insertDisbursementLineAdvances(Models.TrnDisbursementLine disbursementLine, String articleId, String CVId)
        {
            try
            {
                var SupplierAdvancesAccountId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.SupplierAdvancesAccountId).SingleOrDefault();

                var journals = from d in db.TrnJournals
                               where d.ArticleId == Convert.ToInt32(articleId)
                               && d.AccountId == SupplierAdvancesAccountId
                               && d.BranchId == currentBranchId()
                               group d by new
                               {
                                   BranchId = d.BranchId,
                                   Branch = d.MstBranch.Branch,
                                   AccountId = d.AccountId,
                                   Account = d.MstAccount.Account,
                                   AccountCode = d.MstAccount.AccountCode,
                                   ArticleId = d.ArticleId,
                                   Article = d.MstArticle.Article,
                                   RRId = d.RRId
                               } into g
                               select new Models.TrnJournal
                               {
                                   BranchId = g.Key.BranchId,
                                   Branch = g.Key.Branch,
                                   AccountId = g.Key.AccountId,
                                   Account = g.Key.Account,
                                   AccountCode = g.Key.AccountCode,
                                   ArticleId = g.Key.ArticleId,
                                   Article = g.Key.Article,
                                   RRId = g.Key.RRId,
                                   DebitAmount = g.Sum(d => d.DebitAmount),
                                   CreditAmount = g.Sum(d => d.CreditAmount),
                                   Balance = g.Sum(d => d.DebitAmount) - g.Sum(d => d.CreditAmount)
                               };

                if (journals.Any())
                {
                    Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine();

                    foreach (var journal in journals)
                    {
                        newDisbursementLine.CVId = Convert.ToInt32(CVId);
                        newDisbursementLine.BranchId = journal.BranchId;
                        newDisbursementLine.AccountId = journal.AccountId;
                        newDisbursementLine.ArticleId = journal.ArticleId;
                        newDisbursementLine.RRId = journal.RRId;
                        newDisbursementLine.Particulars = "Supplier Advances";
                        newDisbursementLine.Amount = journal.Balance;
                    }

                    db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                    db.SubmitChanges();

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Example #11
0
        public HttpResponseMessage AddFolderMonitoringDisbursement(List <Entities.FolderMonitoringTrnDisbursement> folderMonitoringTrnDisbursementObjects)
        {
            try
            {
                if (folderMonitoringTrnDisbursementObjects.Any())
                {
                    String returnMessage = "";

                    List <Entities.FolderMonitoringTrnDisbursementHeader> newFolderMonitoringTrnDisbursementHeaders = new List <Entities.FolderMonitoringTrnDisbursementHeader>();
                    List <Entities.FolderMonitoringTrnDisbursementLine>   newFolderMonitoringTrnDisbursementLines   = new List <Entities.FolderMonitoringTrnDisbursementLine>();

                    Boolean isDataValid = false;

                    foreach (var folderMonitoringTrnDisbursementObject in folderMonitoringTrnDisbursementObjects)
                    {
                        Boolean isBranchExist      = false,
                                 isSupplierExist   = false,
                                 isPayTypeExist    = false,
                                 isBankExist       = false,
                                 isUserExist       = false,
                                 isLineBranchExist = false,
                                 isAccountExist    = false,
                                 isArticleExist    = false;

                        var branch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnDisbursementObject.BranchCode) select d;
                        if (branch.Any())
                        {
                            isBranchExist = true;
                        }

                        var supplier = from d in db.MstArticles where d.ArticleTypeId == 3 && d.ManualArticleCode.Equals(folderMonitoringTrnDisbursementObject.SupplierCode) && d.IsLocked == true select d;
                        if (supplier.Any())
                        {
                            isSupplierExist = true;
                        }

                        var payType = from d in db.MstPayTypes where d.PayType.Equals(folderMonitoringTrnDisbursementObject.PayType) select d;
                        if (payType.Any())
                        {
                            isPayTypeExist = true;
                        }

                        var bank = from d in db.MstArticles where d.ArticleTypeId == 5 && d.ManualArticleCode.Equals(folderMonitoringTrnDisbursementObject.BankCode) select d;
                        if (bank.Any())
                        {
                            isBankExist = true;
                        }

                        var user = from d in db.MstUsers where d.UserName.Equals(folderMonitoringTrnDisbursementObject.UserCode) select d;
                        if (user.Any())
                        {
                            isUserExist = true;
                        }

                        var lineBranch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnDisbursementObject.LineBranchCode) select d;
                        if (lineBranch.Any())
                        {
                            isLineBranchExist = true;
                        }

                        List <easyfis.Entities.MstArticle> listArticles = new List <easyfis.Entities.MstArticle>();
                        var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnDisbursementObject.AccountCode) select d;
                        if (account.Any())
                        {
                            isAccountExist = true;

                            var accountArticleTypes = from d in db.MstAccountArticleTypes
                                                      where d.AccountId == Convert.ToInt32(account.FirstOrDefault().Id)
                                                      group d by d.ArticleTypeId into g
                                                      select g;

                            if (accountArticleTypes.Any())
                            {
                                foreach (var accountArticleType in accountArticleTypes)
                                {
                                    var articles = from d in db.MstArticles
                                                   where d.ArticleTypeId == accountArticleType.Key &&
                                                   d.IsLocked == true
                                                   select d;

                                    if (articles.Any())
                                    {
                                        foreach (var articleObject in articles)
                                        {
                                            listArticles.Add(new easyfis.Entities.MstArticle()
                                            {
                                                Id = articleObject.Id,
                                                ManualArticleCode = articleObject.ManualArticleCode,
                                                Article           = articleObject.Article
                                            });
                                        }
                                    }
                                }
                            }
                        }

                        var article = from d in listArticles where d.ManualArticleCode.Equals(folderMonitoringTrnDisbursementObject.ArticleCode) select d;
                        if (article.Any())
                        {
                            isArticleExist = true;
                        }

                        if (!isBranchExist)
                        {
                            returnMessage = "Invalid Branch Code: " + folderMonitoringTrnDisbursementObject.BranchCode; isDataValid = false; break;
                        }
                        else if (!isSupplierExist)
                        {
                            returnMessage = "Invalid Supplier Code: " + folderMonitoringTrnDisbursementObject.SupplierCode; isDataValid = false; break;
                        }
                        else if (!isPayTypeExist)
                        {
                            returnMessage = "Invalid Pay Type: " + folderMonitoringTrnDisbursementObject.PayType; isDataValid = false; break;
                        }
                        else if (!isBankExist)
                        {
                            returnMessage = "Invalid Bank Code: " + folderMonitoringTrnDisbursementObject.BankCode; isDataValid = false; break;
                        }
                        else if (!isUserExist)
                        {
                            returnMessage = "Invalid User Code: " + folderMonitoringTrnDisbursementObject.UserCode; isDataValid = false; break;
                        }
                        else if (!isLineBranchExist)
                        {
                            returnMessage = "Invalid Line Branch Code: " + folderMonitoringTrnDisbursementObject.LineBranchCode; isDataValid = false; break;
                        }
                        else if (!isAccountExist)
                        {
                            returnMessage = "Invalid Account Code: " + folderMonitoringTrnDisbursementObject.AccountCode; isDataValid = false; break;
                        }
                        else if (!isArticleExist)
                        {
                            returnMessage = "Invalid Article Code: " + folderMonitoringTrnDisbursementObject.ArticleCode; isDataValid = false; break;
                        }
                        else
                        {
                            var currentDisbursement = from d in db.TrnDisbursements
                                                      where d.BranchId == branch.FirstOrDefault().Id &&
                                                      d.ManualCVNumber.Equals(folderMonitoringTrnDisbursementObject.ManualCVNumber)
                                                      select d;

                            if (currentDisbursement.Any())
                            {
                                returnMessage = "This Manual CV No. " + folderMonitoringTrnDisbursementObject.ManualCVNumber + " is already exist!";
                                isDataValid   = false;

                                break;
                            }
                            else
                            {
                                Int32?RRId = null;
                                if (!folderMonitoringTrnDisbursementObject.ManualRRNumber.Equals("") || !folderMonitoringTrnDisbursementObject.ManualRRNumber.Equals("NA"))
                                {
                                    var receivingReceipt = from d in db.TrnReceivingReceipts
                                                           where d.BranchId == branch.FirstOrDefault().Id &&
                                                           d.ManualRRNumber.Equals(folderMonitoringTrnDisbursementObject.ManualRRNumber) &&
                                                           d.IsLocked == true
                                                           select d;

                                    if (receivingReceipt.Any())
                                    {
                                        RRId = receivingReceipt.FirstOrDefault().Id;
                                    }
                                }

                                var folderMonitoringTrnDisbursementHeader = from d in newFolderMonitoringTrnDisbursementHeaders
                                                                            where d.BranchId == branch.FirstOrDefault().Id &&
                                                                            d.ManualCVNumber.Equals(folderMonitoringTrnDisbursementObject.ManualCVNumber)
                                                                            select d;

                                if (folderMonitoringTrnDisbursementHeader.Any())
                                {
                                    var currentFolderMonitoringTrnDisbursementHeader = folderMonitoringTrnDisbursementHeader.FirstOrDefault();
                                    currentFolderMonitoringTrnDisbursementHeader.ListFolderMonitoringTrnDisbursementLines.Add(new Entities.FolderMonitoringTrnDisbursementLine()
                                    {
                                        ManualCVNumber = folderMonitoringTrnDisbursementObject.ManualCVNumber,
                                        BranchId       = lineBranch.FirstOrDefault().Id,
                                        AccountId      = account.FirstOrDefault().Id,
                                        ArticleId      = article.FirstOrDefault().Id,
                                        RRId           = RRId,
                                        Particulars    = folderMonitoringTrnDisbursementObject.Particulars,
                                        Amount         = folderMonitoringTrnDisbursementObject.Amount
                                    });
                                }
                                else
                                {
                                    newFolderMonitoringTrnDisbursementLines = new List <Entities.FolderMonitoringTrnDisbursementLine>
                                    {
                                        new Entities.FolderMonitoringTrnDisbursementLine()
                                        {
                                            ManualCVNumber = folderMonitoringTrnDisbursementObject.ManualCVNumber,
                                            BranchId       = lineBranch.FirstOrDefault().Id,
                                            AccountId      = account.FirstOrDefault().Id,
                                            ArticleId      = article.FirstOrDefault().Id,
                                            RRId           = RRId,
                                            Particulars    = folderMonitoringTrnDisbursementObject.Particulars,
                                            Amount         = folderMonitoringTrnDisbursementObject.Amount
                                        }
                                    };

                                    if (newFolderMonitoringTrnDisbursementLines.Any())
                                    {
                                        newFolderMonitoringTrnDisbursementHeaders.Add(new Entities.FolderMonitoringTrnDisbursementHeader()
                                        {
                                            BranchId        = branch.FirstOrDefault().Id,
                                            CVNumber        = "0000000000",
                                            CVDate          = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CVDate),
                                            SupplierId      = supplier.FirstOrDefault().Id,
                                            Payee           = folderMonitoringTrnDisbursementObject.Payee,
                                            PayTypeId       = payType.FirstOrDefault().Id,
                                            BankId          = bank.FirstOrDefault().Id,
                                            ManualCVNumber  = folderMonitoringTrnDisbursementObject.ManualCVNumber,
                                            Particulars     = folderMonitoringTrnDisbursementObject.Remarks,
                                            CheckNumber     = folderMonitoringTrnDisbursementObject.CheckNumber,
                                            CheckDate       = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CheckDate),
                                            Amount          = 0,
                                            IsCrossCheck    = folderMonitoringTrnDisbursementObject.IsCrossCheck,
                                            IsClear         = folderMonitoringTrnDisbursementObject.IsClear,
                                            PreparedById    = user.FirstOrDefault().Id,
                                            CheckedById     = user.FirstOrDefault().Id,
                                            ApprovedById    = user.FirstOrDefault().Id,
                                            Status          = null,
                                            IsCancelled     = false,
                                            IsPrinted       = false,
                                            IsLocked        = false,
                                            CreatedById     = user.FirstOrDefault().Id,
                                            CreatedDateTime = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CreatedDateTime),
                                            UpdatedById     = user.FirstOrDefault().Id,
                                            UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnDisbursementObject.CreatedDateTime),
                                            ListFolderMonitoringTrnDisbursementLines = newFolderMonitoringTrnDisbursementLines
                                        });
                                    }
                                }

                                isDataValid = true;
                            }
                        }
                    }

                    if (isDataValid)
                    {
                        if (newFolderMonitoringTrnDisbursementHeaders.Any())
                        {
                            foreach (var newFolderMonitoringTrnDisbursementHeader in newFolderMonitoringTrnDisbursementHeaders)
                            {
                                Int32 CVId = 0;

                                var defaultCVNumber  = "0000000001";
                                var lastDisbursement = from d in db.TrnDisbursements.OrderByDescending(d => d.Id) where d.BranchId == newFolderMonitoringTrnDisbursementHeader.BranchId select d;
                                if (lastDisbursement.Any())
                                {
                                    var CVNumber = Convert.ToInt32(lastDisbursement.FirstOrDefault().CVNumber) + 0000000001;
                                    defaultCVNumber = FillLeadingZeroes(CVNumber, 10);
                                }

                                Data.TrnDisbursement newDisbursement = new Data.TrnDisbursement
                                {
                                    BranchId        = newFolderMonitoringTrnDisbursementHeader.BranchId,
                                    CVNumber        = defaultCVNumber,
                                    CVDate          = newFolderMonitoringTrnDisbursementHeader.CVDate,
                                    ManualCVNumber  = newFolderMonitoringTrnDisbursementHeader.ManualCVNumber,
                                    Payee           = newFolderMonitoringTrnDisbursementHeader.Payee,
                                    SupplierId      = newFolderMonitoringTrnDisbursementHeader.SupplierId,
                                    PayTypeId       = newFolderMonitoringTrnDisbursementHeader.PayTypeId,
                                    Particulars     = newFolderMonitoringTrnDisbursementHeader.Particulars,
                                    BankId          = newFolderMonitoringTrnDisbursementHeader.BankId,
                                    CheckNumber     = newFolderMonitoringTrnDisbursementHeader.CheckNumber,
                                    CheckDate       = newFolderMonitoringTrnDisbursementHeader.CheckDate,
                                    Amount          = newFolderMonitoringTrnDisbursementHeader.Amount,
                                    IsCrossCheck    = newFolderMonitoringTrnDisbursementHeader.IsCrossCheck,
                                    PreparedById    = newFolderMonitoringTrnDisbursementHeader.PreparedById,
                                    CheckedById     = newFolderMonitoringTrnDisbursementHeader.CheckedById,
                                    ApprovedById    = newFolderMonitoringTrnDisbursementHeader.ApprovedById,
                                    IsClear         = newFolderMonitoringTrnDisbursementHeader.IsClear,
                                    Status          = newFolderMonitoringTrnDisbursementHeader.Status,
                                    IsCancelled     = newFolderMonitoringTrnDisbursementHeader.IsCancelled,
                                    IsPrinted       = newFolderMonitoringTrnDisbursementHeader.IsPrinted,
                                    IsLocked        = newFolderMonitoringTrnDisbursementHeader.IsLocked,
                                    CreatedById     = newFolderMonitoringTrnDisbursementHeader.CreatedById,
                                    CreatedDateTime = newFolderMonitoringTrnDisbursementHeader.CreatedDateTime,
                                    UpdatedById     = newFolderMonitoringTrnDisbursementHeader.UpdatedById,
                                    UpdatedDateTime = newFolderMonitoringTrnDisbursementHeader.UpdatedDateTime
                                };

                                db.TrnDisbursements.InsertOnSubmit(newDisbursement);
                                db.SubmitChanges();

                                CVId = newDisbursement.Id;

                                if (newFolderMonitoringTrnDisbursementHeader.ListFolderMonitoringTrnDisbursementLines.Any())
                                {
                                    foreach (var listFolderMonitoringTrnDisbursementLine in newFolderMonitoringTrnDisbursementHeader.ListFolderMonitoringTrnDisbursementLines)
                                    {
                                        Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine
                                        {
                                            CVId        = CVId,
                                            BranchId    = listFolderMonitoringTrnDisbursementLine.BranchId,
                                            AccountId   = listFolderMonitoringTrnDisbursementLine.AccountId,
                                            ArticleId   = listFolderMonitoringTrnDisbursementLine.ArticleId,
                                            RRId        = listFolderMonitoringTrnDisbursementLine.RRId,
                                            Particulars = listFolderMonitoringTrnDisbursementLine.Particulars,
                                            Amount      = listFolderMonitoringTrnDisbursementLine.Amount
                                        };

                                        db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                                    }

                                    db.SubmitChanges();
                                }

                                var disbursement = from d in db.TrnDisbursements
                                                   where d.Id == CVId
                                                   select d;

                                if (disbursement.Any())
                                {
                                    Decimal amount = 0;
                                    var     lines  = from d in db.TrnDisbursementLines where d.CVId == CVId select d;
                                    if (lines.Any())
                                    {
                                        amount = lines.Sum(d => d.Amount);
                                    }

                                    var lockDisbursement = disbursement.FirstOrDefault();
                                    lockDisbursement.Amount   = amount;
                                    lockDisbursement.IsLocked = true;
                                    db.SubmitChanges();

                                    journal.InsertCashVoucherJournal(CVId);
                                }

                                var disbursementLines = from d in db.TrnDisbursementLines
                                                        where d.CVId == CVId &&
                                                        d.RRId != null &&
                                                        d.TrnDisbursement.IsLocked == true
                                                        select d;

                                if (disbursementLines.Any())
                                {
                                    foreach (var disbursementLine in disbursementLines)
                                    {
                                        accountsPayable.UpdateAccountsPayable(Convert.ToInt32(disbursementLine.RRId));
                                    }
                                }
                            }
                        }
                    }

                    return(Request.CreateResponse(HttpStatusCode.OK, returnMessage));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "Data Source is Invalid or Empty."));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Backend Error: " + ex.Message));
            }
        }
        public HttpResponseMessage ApplySupplierAdvancesDisbursementLine(Entities.TrnDisbursementLine objDisbursementLine, String CVId)
        {
            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 currentBranchId = currentUser.FirstOrDefault().BranchId;

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

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanAdd)
                        {
                            var disbursement = from d in db.TrnDisbursements
                                               where d.Id == Convert.ToInt32(CVId)
                                               select d;

                            if (disbursement.Any())
                            {
                                if (!disbursement.FirstOrDefault().IsLocked)
                                {
                                    var journals = from d in db.TrnJournals
                                                   where d.ArticleId == objDisbursementLine.ArticleId &&
                                                   d.AccountId == objDisbursementLine.AccountId &&
                                                   d.BranchId == currentBranchId
                                                   group d by new
                                    {
                                        BranchId  = d.BranchId,
                                        AccountId = d.AccountId,
                                        ArticleId = d.ArticleId,
                                    } into g
                                        select new
                                    {
                                        BranchId      = g.Key.BranchId,
                                        AccountId     = g.Key.AccountId,
                                        ArticleId     = g.Key.ArticleId,
                                        DebitAmount   = g.Sum(d => d.DebitAmount),
                                        CreditAmount  = g.Sum(d => d.CreditAmount),
                                        BalanceAmount = g.Sum(d => d.DebitAmount) - g.Sum(d => d.CreditAmount)
                                    };

                                    if (journals.Any())
                                    {
                                        var advances = from d in journals.ToList()
                                                       select new
                                        {
                                            BranchId      = d.BranchId,
                                            AccountId     = d.AccountId,
                                            ArticleId     = d.ArticleId,
                                            DebitAmount   = d.DebitAmount,
                                            CreditAmount  = d.CreditAmount,
                                            BalanceAmount = d.BalanceAmount
                                        };

                                        if (advances.Any())
                                        {
                                            Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine
                                            {
                                                CVId        = Convert.ToInt32(CVId),
                                                BranchId    = advances.FirstOrDefault().BranchId,
                                                AccountId   = advances.FirstOrDefault().AccountId,
                                                ArticleId   = advances.FirstOrDefault().ArticleId,
                                                RRId        = null,
                                                Particulars = "Supplier Advances",
                                                Amount      = advances.FirstOrDefault().BalanceAmount * -1,
                                            };

                                            db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                                            db.SubmitChanges();

                                            Decimal disbursementItemTotalAmount = 0;

                                            if (disbursement.FirstOrDefault().TrnDisbursementLines.Any())
                                            {
                                                disbursementItemTotalAmount = disbursement.FirstOrDefault().TrnDisbursementLines.Sum(d => d.Amount);
                                            }

                                            var updateDisbursement = disbursement.FirstOrDefault();
                                            updateDisbursement.Amount = disbursementItemTotalAmount;
                                            db.SubmitChanges();

                                            return(Request.CreateResponse(HttpStatusCode.OK));
                                        }
                                        else
                                        {
                                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "No advances found."));
                                        }
                                    }
                                    else
                                    {
                                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "No journal data found."));
                                    }
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "You cannot add new disbursement line if the current disbursement detail is locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "These current disbursement details are not found in the server. Please add new disbursement first before proceeding."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add new disbursement line in this disbursement detail page."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access in this disbursement detail 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."));
            }
        }
        public HttpResponseMessage ApplyReceivingReceiptStatusDisbursementLine(List <Entities.TrnDisbursementLine> objDisbursementLines, String CVId)
        {
            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 currentBranchId = currentUser.FirstOrDefault().BranchId;

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

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanAdd)
                        {
                            var disbursement = from d in db.TrnDisbursements
                                               where d.Id == Convert.ToInt32(CVId)
                                               select d;

                            if (disbursement.Any())
                            {
                                if (!disbursement.FirstOrDefault().IsLocked)
                                {
                                    foreach (var objDisbursementLine in objDisbursementLines)
                                    {
                                        var receivingReceipt = from d in db.TrnReceivingReceipts
                                                               where d.Id == objDisbursementLine.RRId &&
                                                               d.BranchId == currentBranchId &&
                                                               d.IsLocked == true
                                                               select d;

                                        if (receivingReceipt.Any())
                                        {
                                            Data.TrnDisbursementLine newDisbursementLine = new Data.TrnDisbursementLine
                                            {
                                                CVId        = Convert.ToInt32(CVId),
                                                BranchId    = receivingReceipt.FirstOrDefault().BranchId,
                                                AccountId   = receivingReceipt.FirstOrDefault().MstArticle.AccountId,
                                                ArticleId   = receivingReceipt.FirstOrDefault().SupplierId,
                                                RRId        = receivingReceipt.FirstOrDefault().Id,
                                                Particulars = receivingReceipt.FirstOrDefault().Remarks,
                                                Amount      = objDisbursementLine.Amount,
                                            };

                                            db.TrnDisbursementLines.InsertOnSubmit(newDisbursementLine);
                                            db.SubmitChanges();
                                        }
                                    }

                                    Decimal disbursementItemTotalAmount = 0;

                                    if (disbursement.FirstOrDefault().TrnDisbursementLines.Any())
                                    {
                                        disbursementItemTotalAmount = disbursement.FirstOrDefault().TrnDisbursementLines.Sum(d => d.Amount);
                                    }

                                    var updateDisbursement = disbursement.FirstOrDefault();
                                    updateDisbursement.Amount = disbursementItemTotalAmount;
                                    db.SubmitChanges();

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "You cannot add new disbursement line if the current disbursement detail is locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "These current disbursement details are not found in the server. Please add new disbursement first before proceeding."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add new disbursement line in this disbursement detail page."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access in this disbursement detail 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."));
            }
        }