public Int32 insertJournalVoucher(Models.TrnJournalVoucher journalVoucher) { try { var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault(); var lastJVNumber = from d in db.TrnJournalVouchers.OrderByDescending(d => d.Id) where d.BranchId == currentBranchId() select d; var JVNumberResult = "0000000001"; if (lastJVNumber.Any()) { var JVNumber = Convert.ToInt32(lastJVNumber.FirstOrDefault().JVNumber) + 0000000001; JVNumberResult = zeroFill(JVNumber, 10); } Data.TrnJournalVoucher newJournalVoucher = new Data.TrnJournalVoucher(); newJournalVoucher.BranchId = currentBranchId(); newJournalVoucher.JVNumber = JVNumberResult; newJournalVoucher.ManualJVNumber = "NA"; newJournalVoucher.JVDate = DateTime.Today; newJournalVoucher.Particulars = "NA"; newJournalVoucher.PreparedById = userId; newJournalVoucher.CheckedById = userId; newJournalVoucher.ApprovedById = userId; newJournalVoucher.IsLocked = false; newJournalVoucher.CreatedById = userId; newJournalVoucher.CreatedDateTime = DateTime.Now; newJournalVoucher.UpdatedById = userId; newJournalVoucher.UpdatedDateTime = DateTime.Now; db.TrnJournalVouchers.InsertOnSubmit(newJournalVoucher); db.SubmitChanges(); return(newJournalVoucher.Id); } catch { return(0); } }
public HttpResponseMessage AddFolderMonitoringJournalVoucher(List <Entities.FolderMonitoringTrnJournalVoucher> folderMonitoringTrnJournalVoucherObjects) { try { if (folderMonitoringTrnJournalVoucherObjects.Any()) { String returnMessage = ""; List <Entities.FolderMonitoringTrnJournalVoucherHeader> newFolderMonitoringTrnJournalVoucherHeaders = new List <Entities.FolderMonitoringTrnJournalVoucherHeader>(); List <Entities.FolderMonitoringTrnJournalVoucherLine> newFolderMonitoringTrnJournalVoucherLines = new List <Entities.FolderMonitoringTrnJournalVoucherLine>(); Boolean isDataValid = false; foreach (var folderMonitoringTrnJournalVoucherObject in folderMonitoringTrnJournalVoucherObjects) { Boolean isBranchExist = false, isUserExist = false, isEntryBranchCodeExist = false, isAccountExist = false, isArticleExist = false; var branch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnJournalVoucherObject.BranchCode) select d; if (branch.Any()) { isBranchExist = true; } var user = from d in db.MstUsers where d.UserName.Equals(folderMonitoringTrnJournalVoucherObject.UserCode) select d; if (user.Any()) { isUserExist = true; } var entryBranch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnJournalVoucherObject.EntryBranchCode) select d; if (entryBranch.Any()) { isEntryBranchCodeExist = true; } List <easyfis.Entities.MstArticle> listArticles = new List <easyfis.Entities.MstArticle>(); var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnJournalVoucherObject.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(folderMonitoringTrnJournalVoucherObject.ArticleCode) select d; if (article.Any()) { isArticleExist = true; } if (!isBranchExist) { returnMessage = "Invalid Branch Code: " + folderMonitoringTrnJournalVoucherObject.BranchCode; isDataValid = false; break; } else if (!isUserExist) { returnMessage = "Invalid User Code: " + folderMonitoringTrnJournalVoucherObject.UserCode; isDataValid = false; break; } else if (!isEntryBranchCodeExist) { returnMessage = "Invalid Entry Branch Code: " + folderMonitoringTrnJournalVoucherObject.EntryBranchCode; isDataValid = false; break; } else if (!isAccountExist) { returnMessage = "Invalid Account Code: " + folderMonitoringTrnJournalVoucherObject.AccountCode; isDataValid = false; break; } else if (!isArticleExist) { returnMessage = "Invalid Article Code: " + folderMonitoringTrnJournalVoucherObject.ArticleCode; isDataValid = false; break; } else { var journalVoucher = from d in db.TrnJournalVouchers where d.BranchId == branch.FirstOrDefault().Id && d.ManualJVNumber.Equals(folderMonitoringTrnJournalVoucherObject.ManualJVNumber) && d.IsLocked == true select d; if (journalVoucher.Any()) { returnMessage = "This Manual JV No. " + folderMonitoringTrnJournalVoucherObject.ManualJVNumber + " is already exist!"; isDataValid = false; break; } else { Int32?APRRId = null; if (!folderMonitoringTrnJournalVoucherObject.APManualRRNumber.Equals("") || !folderMonitoringTrnJournalVoucherObject.APManualRRNumber.Equals("NA")) { var receivingReceipt = from d in db.TrnReceivingReceipts where d.BranchId == branch.FirstOrDefault().Id && d.ManualRRNumber.Equals(folderMonitoringTrnJournalVoucherObject.APManualRRNumber) && d.IsLocked == true select d; if (receivingReceipt.Any()) { APRRId = receivingReceipt.FirstOrDefault().Id; } } Int32?ARSIId = null; if (!folderMonitoringTrnJournalVoucherObject.ARManualSINumber.Equals("") || !folderMonitoringTrnJournalVoucherObject.ARManualSINumber.Equals("NA")) { var salesInvoice = from d in db.TrnSalesInvoices where d.BranchId == branch.FirstOrDefault().Id && d.ManualSINumber.Equals(folderMonitoringTrnJournalVoucherObject.ARManualSINumber) && d.IsLocked == true select d; if (salesInvoice.Any()) { ARSIId = salesInvoice.FirstOrDefault().Id; } } var folderMonitoringTrnJournalVoucherHeader = from d in newFolderMonitoringTrnJournalVoucherHeaders where d.BranchId == branch.FirstOrDefault().Id && d.ManualJVNumber.Equals(folderMonitoringTrnJournalVoucherObject.ManualJVNumber) select d; if (folderMonitoringTrnJournalVoucherHeader.Any()) { var currentFolderMonitoringTrnJournalVoucherHeader = folderMonitoringTrnJournalVoucherHeader.FirstOrDefault(); currentFolderMonitoringTrnJournalVoucherHeader.ListFolderMonitoringTrnJournalVoucherLines.Add(new Entities.FolderMonitoringTrnJournalVoucherLine() { ManualJVNumber = folderMonitoringTrnJournalVoucherObject.ManualJVNumber, BranchId = entryBranch.FirstOrDefault().Id, AccountId = account.FirstOrDefault().Id, ArticleId = article.FirstOrDefault().Id, Particulars = folderMonitoringTrnJournalVoucherObject.Particulars, DebitAmount = folderMonitoringTrnJournalVoucherObject.DebitAmount, CreditAmount = folderMonitoringTrnJournalVoucherObject.CreditAmount, APRRId = APRRId, ARSIId = ARSIId, IsClear = folderMonitoringTrnJournalVoucherObject.IsClear }); } else { newFolderMonitoringTrnJournalVoucherLines = new List <Entities.FolderMonitoringTrnJournalVoucherLine> { new Entities.FolderMonitoringTrnJournalVoucherLine() { ManualJVNumber = folderMonitoringTrnJournalVoucherObject.ManualJVNumber, BranchId = entryBranch.FirstOrDefault().Id, AccountId = account.FirstOrDefault().Id, ArticleId = article.FirstOrDefault().Id, Particulars = folderMonitoringTrnJournalVoucherObject.Particulars, DebitAmount = folderMonitoringTrnJournalVoucherObject.DebitAmount, CreditAmount = folderMonitoringTrnJournalVoucherObject.CreditAmount, APRRId = APRRId, ARSIId = ARSIId, IsClear = folderMonitoringTrnJournalVoucherObject.IsClear } }; if (newFolderMonitoringTrnJournalVoucherLines.Any()) { newFolderMonitoringTrnJournalVoucherHeaders.Add(new Entities.FolderMonitoringTrnJournalVoucherHeader() { BranchId = branch.FirstOrDefault().Id, JVNumber = "0000000000", JVDate = Convert.ToDateTime(folderMonitoringTrnJournalVoucherObject.JVDate), ManualJVNumber = folderMonitoringTrnJournalVoucherObject.ManualJVNumber, Particulars = folderMonitoringTrnJournalVoucherObject.Remarks, 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(folderMonitoringTrnJournalVoucherObject.CreatedDateTime), UpdatedById = user.FirstOrDefault().Id, UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnJournalVoucherObject.CreatedDateTime), ListFolderMonitoringTrnJournalVoucherLines = newFolderMonitoringTrnJournalVoucherLines }); } } isDataValid = true; } } } if (isDataValid) { if (newFolderMonitoringTrnJournalVoucherHeaders.Any()) { foreach (var newFolderMonitoringTrnJournalVoucherHeader in newFolderMonitoringTrnJournalVoucherHeaders) { Int32 JVId = 0; var defaultJVNumber = "0000000001"; var lastJournalVoucher = from d in db.TrnJournalVouchers.OrderByDescending(d => d.Id) where d.BranchId == newFolderMonitoringTrnJournalVoucherHeader.BranchId select d; if (lastJournalVoucher.Any()) { var JVNumber = Convert.ToInt32(lastJournalVoucher.FirstOrDefault().JVNumber) + 0000000001; defaultJVNumber = FillLeadingZeroes(JVNumber, 10); } Data.TrnJournalVoucher newJournalVoucher = new Data.TrnJournalVoucher { BranchId = newFolderMonitoringTrnJournalVoucherHeader.BranchId, JVNumber = defaultJVNumber, JVDate = newFolderMonitoringTrnJournalVoucherHeader.JVDate, ManualJVNumber = newFolderMonitoringTrnJournalVoucherHeader.ManualJVNumber, Particulars = newFolderMonitoringTrnJournalVoucherHeader.Particulars, PreparedById = newFolderMonitoringTrnJournalVoucherHeader.PreparedById, CheckedById = newFolderMonitoringTrnJournalVoucherHeader.CheckedById, ApprovedById = newFolderMonitoringTrnJournalVoucherHeader.ApprovedById, Status = newFolderMonitoringTrnJournalVoucherHeader.Status, IsCancelled = newFolderMonitoringTrnJournalVoucherHeader.IsCancelled, IsPrinted = newFolderMonitoringTrnJournalVoucherHeader.IsPrinted, IsLocked = newFolderMonitoringTrnJournalVoucherHeader.IsLocked, CreatedById = newFolderMonitoringTrnJournalVoucherHeader.CreatedById, CreatedDateTime = newFolderMonitoringTrnJournalVoucherHeader.CreatedDateTime, UpdatedById = newFolderMonitoringTrnJournalVoucherHeader.UpdatedById, UpdatedDateTime = newFolderMonitoringTrnJournalVoucherHeader.UpdatedDateTime }; db.TrnJournalVouchers.InsertOnSubmit(newJournalVoucher); db.SubmitChanges(); JVId = newJournalVoucher.Id; if (newFolderMonitoringTrnJournalVoucherHeader.ListFolderMonitoringTrnJournalVoucherLines.Any()) { foreach (var listFolderMonitoringTrnJournalVoucherLine in newFolderMonitoringTrnJournalVoucherHeader.ListFolderMonitoringTrnJournalVoucherLines) { Data.TrnJournalVoucherLine newJournalVoucherLine = new Data.TrnJournalVoucherLine { JVId = JVId, BranchId = listFolderMonitoringTrnJournalVoucherLine.BranchId, AccountId = listFolderMonitoringTrnJournalVoucherLine.AccountId, ArticleId = listFolderMonitoringTrnJournalVoucherLine.ArticleId, Particulars = listFolderMonitoringTrnJournalVoucherLine.Particulars, DebitAmount = listFolderMonitoringTrnJournalVoucherLine.DebitAmount, CreditAmount = listFolderMonitoringTrnJournalVoucherLine.CreditAmount, APRRId = listFolderMonitoringTrnJournalVoucherLine.APRRId, ARSIId = listFolderMonitoringTrnJournalVoucherLine.ARSIId, IsClear = listFolderMonitoringTrnJournalVoucherLine.IsClear }; db.TrnJournalVoucherLines.InsertOnSubmit(newJournalVoucherLine); } db.SubmitChanges(); } } } } 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 AddJournalVoucher() { 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("JournalVoucherList") select d; if (userForms.Any()) { if (userForms.FirstOrDefault().CanAdd) { var defaultJVNumber = "0000000001"; var lastJournalVoucher = from d in db.TrnJournalVouchers.OrderByDescending(d => d.Id) where d.BranchId == currentBranchId select d; if (lastJournalVoucher.Any()) { var JVNumber = Convert.ToInt32(lastJournalVoucher.FirstOrDefault().JVNumber) + 0000000001; defaultJVNumber = FillLeadingZeroes(JVNumber, 10); } var users = from d in db.MstUsers.OrderBy(d => d.FullName) where d.IsLocked == true select d; if (users.Any()) { Data.TrnJournalVoucher newJournalVoucher = new Data.TrnJournalVoucher { BranchId = currentBranchId, JVNumber = defaultJVNumber, JVDate = DateTime.Today, ManualJVNumber = "NA", Particulars = "NA", PreparedById = currentUserId, CheckedById = currentUserId, ApprovedById = currentUserId, Status = null, IsCancelled = false, IsPrinted = false, IsLocked = false, CreatedById = currentUserId, CreatedDateTime = DateTime.Now, UpdatedById = currentUserId, UpdatedDateTime = DateTime.Now }; db.TrnJournalVouchers.InsertOnSubmit(newJournalVoucher); db.SubmitChanges(); String newObject = auditTrail.GetObjectString(newJournalVoucher); auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject); return(Request.CreateResponse(HttpStatusCode.OK, newJournalVoucher.Id)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "No user found. Please setup more users for all transactions.")); } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add journal voucher.")); } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this journal voucher 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 AddFolderMonitoringJournalVoucher(List <Entities.FolderMonitoringTrnJournalVoucher> folderMonitoringTrnJournalVoucherObjects) { try { if (folderMonitoringTrnJournalVoucherObjects.Any()) { foreach (var folderMonitoringTrnJournalVoucherObject in folderMonitoringTrnJournalVoucherObjects) { Boolean isBranchExist = false, isUserExist = false, isEntryBranchCodeExist = false, isAccountExist = false, isArticleExist = false, isReceivingReceiptExist = false, isSalesInvoiceExist = false; IQueryable <Data.TrnReceivingReceipt> receivingReceipt = null; IQueryable <Data.TrnSalesInvoice> salesInvoice = null; var branch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnJournalVoucherObject.BranchCode) select d; if (branch.Any()) { isBranchExist = true; if (!folderMonitoringTrnJournalVoucherObject.APRRNumber.Equals("") || !folderMonitoringTrnJournalVoucherObject.APRRNumber.Equals("NA")) { receivingReceipt = from d in db.TrnReceivingReceipts where d.BranchId == branch.FirstOrDefault().Id&& d.RRNumber.Equals(folderMonitoringTrnJournalVoucherObject.APRRNumber) && d.IsLocked == true select d; if (receivingReceipt.Any()) { isReceivingReceiptExist = true; } } if (!folderMonitoringTrnJournalVoucherObject.ARSINumber.Equals("") || !folderMonitoringTrnJournalVoucherObject.ARSINumber.Equals("NA")) { salesInvoice = from d in db.TrnSalesInvoices where d.BranchId == branch.FirstOrDefault().Id&& d.SINumber.Equals(folderMonitoringTrnJournalVoucherObject.ARSINumber) && d.IsLocked == true select d; if (salesInvoice.Any()) { isSalesInvoiceExist = true; } } } var user = from d in db.MstUsers where d.UserName.Equals(folderMonitoringTrnJournalVoucherObject.UserCode) select d; if (user.Any()) { isUserExist = true; } var entryBranch = from d in db.MstBranches where d.BranchCode.Equals(folderMonitoringTrnJournalVoucherObject.EntryBranchCode) select d; if (entryBranch.Any()) { isEntryBranchCodeExist = true; } List <easyfis.Entities.MstArticle> listArticles = new List <easyfis.Entities.MstArticle>(); var account = from d in db.MstAccounts where d.AccountCode.Equals(folderMonitoringTrnJournalVoucherObject.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(folderMonitoringTrnJournalVoucherObject.ArticleCode) select d; if (article.Any()) { isArticleExist = true; } if (isBranchExist && isUserExist && isEntryBranchCodeExist && isAccountExist && isArticleExist) { Int32 JVId = 0; var currentJournalVoucher = from d in db.TrnJournalVouchers where d.BranchId == branch.FirstOrDefault().Id&& d.ManualJVNumber.Equals(folderMonitoringTrnJournalVoucherObject.ManualJVNumber) && d.IsLocked == true select d; if (currentJournalVoucher.Any()) { JVId = currentJournalVoucher.FirstOrDefault().Id; var unlockJournalVoucher = currentJournalVoucher.FirstOrDefault(); unlockJournalVoucher.IsLocked = false; unlockJournalVoucher.UpdatedById = user.FirstOrDefault().Id; unlockJournalVoucher.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnJournalVoucherObject.CreatedDateTime); db.SubmitChanges(); journal.DeleteJournalVoucherJournal(JVId); } else { var defaultJVNumber = "0000000001"; var lastJournalVoucher = from d in db.TrnJournalVouchers.OrderByDescending(d => d.Id) where d.BranchId == branch.FirstOrDefault().Id select d; if (lastJournalVoucher.Any()) { var JVNumber = Convert.ToInt32(lastJournalVoucher.FirstOrDefault().JVNumber) + 0000000001; defaultJVNumber = FillLeadingZeroes(JVNumber, 10); } Data.TrnJournalVoucher newJournalVoucher = new Data.TrnJournalVoucher { BranchId = branch.FirstOrDefault().Id, JVNumber = defaultJVNumber, JVDate = Convert.ToDateTime(folderMonitoringTrnJournalVoucherObject.JVDate), ManualJVNumber = folderMonitoringTrnJournalVoucherObject.ManualJVNumber, Particulars = folderMonitoringTrnJournalVoucherObject.Remarks, 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(folderMonitoringTrnJournalVoucherObject.CreatedDateTime), UpdatedById = user.FirstOrDefault().Id, UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnJournalVoucherObject.CreatedDateTime) }; db.TrnJournalVouchers.InsertOnSubmit(newJournalVoucher); db.SubmitChanges(); JVId = newJournalVoucher.Id; } Int32?APRRId = null; if (isReceivingReceiptExist) { APRRId = receivingReceipt.FirstOrDefault().Id; } Int32?ARSIId = null; if (isSalesInvoiceExist) { ARSIId = salesInvoice.FirstOrDefault().Id; } Data.TrnJournalVoucherLine newJournalVoucherLine = new Data.TrnJournalVoucherLine { JVId = JVId, BranchId = entryBranch.FirstOrDefault().Id, AccountId = account.FirstOrDefault().Id, ArticleId = article.FirstOrDefault().Id, Particulars = folderMonitoringTrnJournalVoucherObject.Particulars, DebitAmount = folderMonitoringTrnJournalVoucherObject.DebitAmount, CreditAmount = folderMonitoringTrnJournalVoucherObject.CreditAmount, APRRId = APRRId, ARSIId = ARSIId, IsClear = folderMonitoringTrnJournalVoucherObject.IsClear }; db.TrnJournalVoucherLines.InsertOnSubmit(newJournalVoucherLine); db.SubmitChanges(); var journalVoucher = from d in db.TrnJournalVouchers where d.Id == JVId && d.IsLocked == false select d; if (journalVoucher.Any()) { var lockJournalVoucher = journalVoucher.FirstOrDefault(); lockJournalVoucher.IsLocked = true; lockJournalVoucher.UpdatedById = user.FirstOrDefault().Id; lockJournalVoucher.UpdatedDateTime = Convert.ToDateTime(folderMonitoringTrnJournalVoucherObject.CreatedDateTime); db.SubmitChanges(); var journalVoucherLines = from d in db.TrnJournalVoucherLines where d.JVId == JVId select d; if (journalVoucherLines.Any()) { foreach (var journalVoucherLine in journalVoucherLines) { if (journalVoucherLine.APRRId != null) { accountsPayable.UpdateAccountsPayable(Convert.ToInt32(journalVoucherLine.APRRId)); } if (journalVoucherLine.ARSIId != null) { accountsReceivable.UpdateAccountsReceivable(Convert.ToInt32(journalVoucherLine.ARSIId)); } } } journal.InsertJournalVoucherJournal(JVId); } } } 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 AdjustAPStockOut(Entities.TrnStockOutAdjustAP objStockOutAdjustAP) { 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("StockOutDetail") select d; if (userForms.Any()) { if (userForms.FirstOrDefault().CanAdd) { var defaultJVNumber = "0000000001"; var lastJournalVoucher = from d in db.TrnJournalVouchers.OrderByDescending(d => d.Id) where d.BranchId == currentBranchId select d; if (lastJournalVoucher.Any()) { var JVNumber = Convert.ToInt32(lastJournalVoucher.FirstOrDefault().JVNumber) + 0000000001; defaultJVNumber = FillLeadingZeroes(JVNumber, 10); } var stockOut = from d in db.TrnStockOuts where d.Id == objStockOutAdjustAP.Id select d; if (stockOut.Any()) { if (stockOut.FirstOrDefault().IsLocked) { var receivingReceipt = from d in db.TrnReceivingReceipts where d.Id == objStockOutAdjustAP.RRId && d.BranchId == currentBranchId && d.IsLocked == true select d; if (receivingReceipt.Any()) { Data.TrnJournalVoucher newJournalVoucher = new Data.TrnJournalVoucher { BranchId = currentBranchId, JVNumber = defaultJVNumber, JVDate = DateTime.Today, ManualJVNumber = receivingReceipt.FirstOrDefault().ManualRRNumber, Particulars = receivingReceipt.FirstOrDefault().DocumentReference, PreparedById = receivingReceipt.FirstOrDefault().PreparedById, CheckedById = receivingReceipt.FirstOrDefault().CheckedById, ApprovedById = receivingReceipt.FirstOrDefault().ApprovedById, Status = null, IsCancelled = false, IsPrinted = false, IsLocked = true, CreatedById = currentUserId, CreatedDateTime = DateTime.Now, UpdatedById = currentUserId, UpdatedDateTime = DateTime.Now }; db.TrnJournalVouchers.InsertOnSubmit(newJournalVoucher); db.SubmitChanges(); String newObject = "[" + auditTrail.GetObjectString(newJournalVoucher) + ", "; Data.TrnJournalVoucherLine newDebitJournalVoucherLine = new Data.TrnJournalVoucherLine { JVId = newJournalVoucher.Id, BranchId = currentBranchId, AccountId = receivingReceipt.FirstOrDefault().MstArticle.AccountId, ArticleId = receivingReceipt.FirstOrDefault().SupplierId, Particulars = stockOut.FirstOrDefault().Particulars, DebitAmount = objStockOutAdjustAP.Amount, CreditAmount = 0, APRRId = receivingReceipt.FirstOrDefault().Id, ARSIId = null, IsClear = false }; db.TrnJournalVoucherLines.InsertOnSubmit(newDebitJournalVoucherLine); db.SubmitChanges(); newObject += auditTrail.GetObjectString(newDebitJournalVoucherLine) + ", "; Data.TrnJournalVoucherLine newCreditJournalVoucherLine = new Data.TrnJournalVoucherLine { JVId = newJournalVoucher.Id, BranchId = currentBranchId, AccountId = stockOut.FirstOrDefault().AccountId, ArticleId = stockOut.FirstOrDefault().ArticleId, Particulars = stockOut.FirstOrDefault().Particulars, DebitAmount = 0, CreditAmount = objStockOutAdjustAP.Amount, APRRId = null, ARSIId = null, IsClear = false }; db.TrnJournalVoucherLines.InsertOnSubmit(newCreditJournalVoucherLine); db.SubmitChanges(); newObject += auditTrail.GetObjectString(newCreditJournalVoucherLine) + "]"; var journalVoucherLines = from d in db.TrnJournalVoucherLines where d.JVId == newJournalVoucher.Id select d; if (journalVoucherLines.Any()) { foreach (var journalVoucherLine in journalVoucherLines) { if (journalVoucherLine.APRRId != null) { accountsPayable.UpdateAccountsPayable(Convert.ToInt32(journalVoucherLine.APRRId)); } } } Business.Journal journal = new Business.Journal(); journal.InsertJournalVoucherJournal(newJournalVoucher.Id); auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject); return(Request.CreateResponse(HttpStatusCode.OK, newJournalVoucher.Id)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "Sorry. No receiving receipt found.")); } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. Cannot adjust AP if the stock out detail is locked.")); } } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "Invalid reference object.")); } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to adjust AP stock out.")); } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this stock out 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.")); } }