예제 #1
0
 public int SaveLedgerTransaction(LedgerTransaction LedgerTransaction)
 {
     if (LedgerTransaction.LedgerTransactionId < 1)
     {
         Context.LedgerTransactions.Add(LedgerTransaction);
     }
     else
     {
         Context.Entry(LedgerTransaction).State = System.Data.Entity.EntityState.Modified;
     }
     return(SaveAll());
 }
예제 #2
0
 public async Task <int> SaveLedgerTransactionAsync(LedgerTransaction LedgerTransaction)
 {
     if (LedgerTransaction.LedgerTransactionId < 1)
     {
         Context.LedgerTransactions.Add(LedgerTransaction);
     }
     else
     {
         Context.Entry(LedgerTransaction).State = EntityState.Modified;
     }
     return(await SaveAllAsync());
 }
예제 #3
0
 public static MLedger_LedgerTransaction MLedger_LedgerTransaction(LedgerTransaction dbLedgerTransaction)
 {
     return(new MLedger_LedgerTransaction()
     {
         TransactionId = dbLedgerTransaction.LedgerTransactionId,
         TransactionDateTimeUtc = dbLedgerTransaction.TransactionDateTimeUtc,
         PostDateTime = dbLedgerTransaction.PostDateTime,
         Description = dbLedgerTransaction.Description,
         UnitOfWork = dbLedgerTransaction.UnitOfWork,
         Entries = dbLedgerTransaction.LedgerTransactionEntries.Select(r => MLedger_LedgerTransactionEntry(r)).ToList()
     });
 }
예제 #4
0
        public void ShouldMapBankAccount()
        {
            TestData = new BankBalanceAdjustmentTransaction(TransactionId)
            {
                BankAccount = new ChequeAccount("CHEQUE"),
                Amount      = -101,
                Narrative   = "TEsting 123"
            };
            TestInitialise(); // Re-initialise to use different test data.

            Assert.AreEqual(StatementModelTestData.ChequeAccount.Name, Result.Account);
        }
예제 #5
0
        private static void AddStockAdjustmentDecrementLedgerTransactionToDatabase(ERPContext context, StockAdjustmentTransaction stockAdjustmentTransaction, decimal totalCOGSAdjustment)
        {
            var ledgerTransaction = new LedgerTransaction();

            if (!LedgerTransactionHelper.AddTransactionToDatabase(context, ledgerTransaction, UtilityMethods.GetCurrentDate().Date,
                                                                  stockAdjustmentTransaction.StockAdjustmentTransactionID, "Stock Adjustment (Decrement)"))
            {
                return;
            }
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction, "Cost of Goods Sold", "Debit", totalCOGSAdjustment);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction, "Inventory", "Credit", totalCOGSAdjustment);
        }
        //[HttpGet] iterator will be by default
        public ActionResult AddorEdit(int id = 0)
        {
            LedgerTransaction emp = new LedgerTransaction();

            if (id != 0)
            {
                using (DBModel db = new DBModel())
                {
                    emp = db.LedgerTransactions.Where(x => x.LedgerTransactionID == id).FirstOrDefault <LedgerTransaction>();
                }
            }
            return(View(emp));
        }
예제 #7
0
        private static void RecordPurchaseLedgerTransactionInDatabaseContext(ERPContext context, PurchaseTransaction purchaseTransaction)
        {
            var purchaseLedgerTransaction = new LedgerTransaction();
            var accountsPayableName       = purchaseTransaction.Supplier.Name + " Accounts Payable";

            if (!LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseLedgerTransaction, UtilityMethods.GetCurrentDate().Date, purchaseTransaction.PurchaseID, "Purchase Transaction"))
            {
                return;
            }
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseLedgerTransaction, "Inventory", "Debit", purchaseTransaction.Total);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseLedgerTransaction, accountsPayableName, "Credit", purchaseTransaction.Total);
            context.SaveChanges();
        }
예제 #8
0
        public void Can_create_ledgerTransaction()
        {
            var postingDate = DateTime.Now;
            var transaction = new LedgerTransaction
            {
                Documentation = "Test",
                Description   = "Test",
                PostingDate   = postingDate
            };

            Assert.Equal("Test", transaction.Documentation);
            Assert.Equal("Test", transaction.Description);
            Assert.Equal(postingDate, transaction.PostingDate);
            Assert.NotNull(transaction.LedgerTransactionLines);
        }
예제 #9
0
        private static void RecordPaymentLedgerTransactionInDatabaseContext(ERPContext context, PurchaseTransaction purchaseTransaction, decimal paymentAmount, string paymentMode)
        {
            var accountsPayableName      = purchaseTransaction.Supplier.Name + " Accounts Payable";
            var paymentLedgerTransaction = new LedgerTransaction();

            if (!LedgerTransactionHelper.AddTransactionToDatabase(context, paymentLedgerTransaction, UtilityMethods.GetCurrentDate().Date, purchaseTransaction.PurchaseID, "Purchase Payment"))
            {
                return;
            }
            context.SaveChanges();

            LedgerTransactionHelper.AddTransactionLineToDatabase(context, paymentLedgerTransaction, accountsPayableName, "Debit", paymentAmount);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, paymentLedgerTransaction, paymentMode, "Credit", paymentAmount);
            context.SaveChanges();
        }
예제 #10
0
        public void Can_add_ledgerTransactionLine()
        {
            var transaction = new LedgerTransaction
            {
                Documentation = "Test",
                Description   = "Test",
                PostingDate   = DateTime.Now
            };
            var transactionLine1 = new LedgerTransactionLine {
                Id = 1
            };

            transaction.LedgerTransactionLines.Add(transactionLine1);
            Assert.Equal(1, transaction.LedgerTransactionLines.Count);
            Assert.Equal(1, transaction.LedgerTransactionLines.First().Id);
        }
예제 #11
0
 /// <summary>
 /// Get leget transaction event
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="ledgerTransaction"></param>
 /// <param name="cargo"></param>
 /// <param name="organizationDetailedDto"></param>
 /// <returns></returns>
 private LedgerTransactionAddedEvent GetLedgerTransactionEvent(AddTransactionCommand cmd,
                                                               LedgerTransaction ledgerTransaction, CargoTypeDetailedDTO cargo, OrganizationDetailedDTO organizationDetailedDto)
 {
     return(new LedgerTransactionAddedEvent
     {
         Id = ledgerTransaction.Id,
         Amount = ledgerTransaction.Amount,
         OrganizationId = ledgerTransaction.OrganizationId,
         TotalPrice = ledgerTransaction.TotalPrice,
         TransactionType = cmd.TransactionType,
         CargoId = cmd.CargoId,
         Comments = cmd.Comments,
         Timestamp = DateTime.Now,
         Ean = cargo.Ean,
         Kardex = organizationDetailedDto.Kardex
     });
 }
예제 #12
0
 private void AddEntryToDatabase()
 {
     using (var ts = new TransactionScope(TransactionScopeOption.Required))
     {
         var context     = new ERPContext();
         var transaction = new LedgerTransaction();
         if (!LedgerTransactionHelper.AddTransactionToDatabase(context, transaction, _newEntryDate, _newEntryDescription, _newEntryDescription))
         {
             return;
         }
         context.SaveChanges();
         LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, _newEntryAccount.Name, _newEntrySequence, _newEntryAmount);
         LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, _parentVM.SelectedBank.Name, _newEntrySequence == "Debit" ? "Credit" : "Debit", _newEntryAmount);
         context.SaveChanges();
         ts.Complete();
     }
 }
 public ActionResult Delete(int id)
 {
     try
     {
         using (DBModel db = new DBModel())
         {
             LedgerTransaction emp = db.LedgerTransactions.Where(x => x.LedgerTransactionID == id).FirstOrDefault <LedgerTransaction>();
             db.LedgerTransactions.Remove(emp);
             db.SaveChanges();
         }
         return(Json(new { success = true, html = GlobalClass.RenderRazorViewToString(this, "ViewAll", GetAllLedgetTransaction()), message = "Deleted Successfully" }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet));
     }
 }
예제 #14
0
        public static void AddTransactionLineToDatabase(ERPContext context, LedgerTransaction transaction, string accountName, string seq, decimal amount)
        {
            var transactionLine = new LedgerTransactionLine();
            var account = context.Ledger_Accounts.First(e => e.Name.Equals(accountName));
            var ledgerAccount = context.Ledger_General.First(e => e.ID.Equals(account.ID)); 

            transactionLine.LedgerTransaction = transaction;
            transactionLine.LedgerAccount = account;
            transactionLine.Seq = seq;
            transactionLine.Amount = amount;

            // Update the amount of the account
            if (seq == "Debit") ledgerAccount.Debit += amount;
            else if (seq == "Credit") ledgerAccount.Credit += amount;

            context.Ledger_Transaction_Lines.Add(transactionLine);
        }
        private static void AddPurchaseReturnTransactionLedgerTransactionToDatabaseContext(ERPContext context, PurchaseReturnTransaction purchaseReturnTransaction, decimal totalCOGS)
        {
            var purchaseReturnLedgerTransaction = new LedgerTransaction();

            if (!LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseReturnLedgerTransaction, UtilityMethods.GetCurrentDate(), purchaseReturnTransaction.PurchaseReturnTransactionID, "Purchase Return"))
            {
                return;
            }
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseReturnLedgerTransaction,
                                                                 $"{purchaseReturnTransaction.PurchaseTransaction.Supplier.Name} Accounts Payable", "Debit", purchaseReturnTransaction.NetTotal);
            if (totalCOGS - purchaseReturnTransaction.NetTotal > 0)
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseReturnLedgerTransaction, "Cost of Goods Sold", "Debit", totalCOGS - purchaseReturnTransaction.NetTotal);
            }
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseReturnLedgerTransaction, "Inventory", "Credit", totalCOGS);
            context.SaveChanges();
        }
예제 #16
0
        /// <summary>
        /// Insert a ledger transaction
        /// </summary>
        /// <param name="ledgerTransaction">Ledger transaction</param>
        public virtual void InsertLedgerTransaction(LedgerTransaction ledgerTransaction)
        {
            if (ledgerTransaction.LedgerTransactionLines.Count <= 1)
            {
                throw new ArgumentException("There are no ledger transaction lines.");
            }

            // Attach properties
            ledgerTransaction.ChartOfAccounts = _repository.GetById <ChartOfAccounts>(ledgerTransaction.ChartOfAccountsId);

            foreach (var line in ledgerTransaction.LedgerTransactionLines)
            {
                line.LedgerAccount = _repository.GetById <LedgerAccount>(line.LedgerAccountId);
            }

            if (ledgerTransaction.AreThereAccountsFromDifferentCOAs())
            {
                throw new ArgumentException(
                          "Ledger transaction lines contains ledger accounts from different chart of accounts.");
            }

            var currentLedgerPeriodStartingDate = ledgerTransaction.ChartOfAccounts.CurrentLedgerPeriodStartDate;

            if (ledgerTransaction.PostingDate.Year != currentLedgerPeriodStartingDate.Year ||
                ledgerTransaction.PostingDate.Month != currentLedgerPeriodStartingDate.Month ||
                ledgerTransaction.PostingDate > DateTime.Now)
            {
                throw new ArgumentException("Invalid posting period.");
            }

            if (ledgerTransaction.GetTotalDebitAmount() != ledgerTransaction.GetTotalCreditAmount())
            {
                throw new ArgumentException("Total debit amount differs from total credit amount.");
            }

            if (ledgerTransaction.AreThereDuplicateAccounts())
            {
                throw new ArgumentException("Ledger transaction lines contain duplicate ledger accounts.");
            }

            _repository.Create(ledgerTransaction);

            _repository.Save();
        }
        public IActionResult Add([FromBody] LedgerTransactionViewModel ltvm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                // TODO: get the user creating the ledger account
                // get the admin creating the student
                //                var adminId = GetCurrentUserId();
                //                if (adminId == null) return NotFound(new { error = "User is not authenticated." });
                //                var admin = _dbContext.Admins.SingleOrDefault(i => i.Id == adminId);
                //                if (admin == null) return NotFound(new { error = $"User ID {adminId} has not been found" });

                var ledgerTransaction = new LedgerTransaction
                {
                    Documentation          = ltvm.Documentation,
                    Description            = ltvm.Description,
                    PostingDate            = DateTime.ParseExact(ltvm.PostingDate, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    IsEditable             = ltvm.IsEditable,
                    ChartOfAccountsId      = ltvm.ChartOfAccountsId,
                    LedgerTransactionLines = ltvm.LedgerTransactionLines.Select(line => new LedgerTransactionLine
                    {
                        Amount          = line.Amount,
                        IsDebit         = line.IsDebit,
                        LedgerAccountId = line.LedgerAccountId
                    }).ToList()
                };

                _ledgerTransactionService.InsertLedgerTransaction(ledgerTransaction);

                // return the newly-created ledger transaction to the client.
                return(new JsonResult(Mapper.Map <LedgerTransaction, LedgerTransactionViewModel>(ledgerTransaction),
                                      DefaultJsonSettings));
            }
            catch (Exception e)
            {
                // return the error.
                return(BadRequest(new { Error = e.Message }));
            }
        }
예제 #18
0
        private static void RecordEditedPurchaseTransactionTotalCOGSChangedLedgerTransactionInDatabaseContext(
            ERPContext context, PurchaseTransaction editedPurchaseTransaction, decimal totalCOGSChanged)
        {
            var purchaseTransactionTotalCOGSChangedLedgerTransaction = new LedgerTransaction();

            LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, UtilityMethods.GetCurrentDate().Date, editedPurchaseTransaction.PurchaseID, "Purchase Transaction Adjustment (COGS)");
            context.SaveChanges();

            if (totalCOGSChanged > 0)
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Cost of Goods Sold", "Debit", totalCOGSChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Inventory", "Credit", totalCOGSChanged);
            }
            else
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Inventory", "Debit", -totalCOGSChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Cost of Goods Sold", "Credit", -totalCOGSChanged);
            }
            context.SaveChanges();
        }
예제 #19
0
        public LedgerAccountTransactionBuilder Begin(string description, DateTime postDateTime, DateTime utcNow)
        {
            if (m_ledgerTransaction != null)
            {
                throw new InvalidOperationException("Begin has already been called.");
            }

            m_postDateTime = postDateTime;
            m_utcNow       = utcNow;

            m_ledgerTransaction = new LedgerTransaction()
            {
                TransactionDateTimeUtc = m_utcNow,
                PostDateTime           = m_postDateTime,
                Description            = description
            };
            _ = m_ctx.LedgerTransactions.Add(m_ledgerTransaction);

            return(this);
        }
예제 #20
0
        public static bool AddTransactionToDatabase(ERPContext context, LedgerTransaction transaction, DateTime date, string documentation, string description)
        {
            using (var c = new ERPContext())
            {
                var period = c.Ledger_General.First().Period;
                if (period != date.Month)
                {
                    MessageBox.Show("Wrong period.", "Invalid Transaction", MessageBoxButton.OK);
                    return(false);
                }

                if (date.Date.Day != UtilityMethods.GetCurrentDate().Day)
                {
                    if (!(date.AddDays(2).Day <= UtilityMethods.GetCurrentDate().Day&& date.AddDays(2).Day >= date.Day))
                    {
                        MessageBox.Show("Wrong period.", "Invalid Transaction", MessageBoxButton.OK);
                        return(false);
                    }
                }
            }

            transaction.Documentation = documentation;
            transaction.Date          = date;
            transaction.Description   = description;

            if (Application.Current != null)
            {
                var user = Application.Current.TryFindResource("CurrentUser") as User;
                if (user != null)
                {
                    transaction.User = context.Users.FirstOrDefault(e => e.Username.Equals(user.Username));
                }
            }
            else
            {
                transaction.User = context.Users.FirstOrDefault(e => e.Username.Equals("EMPTY"));
            }

            context.Ledger_Transactions.Add(transaction);
            return(true);
        }
        public async Task <LedgerTransactionDto> AddLedgerTransactionAsync(LedgerTransactionDto transactionDto)
        {
            var account = await _db.Accounts.SingleOrDefaultAsync(x => x.AccountId == transactionDto.AccountId);

            var transaction = new LedgerTransaction()
            {
                Amount             = transactionDto.Amount,
                DateTimeCreatedUTC = transactionDto.DateTimeCreatedUTC,
                TransactionType    = transactionDto.TransactionType
            };

            account.Transactions.Add(transaction);
            account.UpdateBalance(transaction);

            _db.Entry(account).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            transactionDto.TransactionId = transaction.TransactionId;

            return(transactionDto);
        }
예제 #22
0
        private void UpdateDisplayedLines()
        {
            DisplayedLines.Clear();
            SetBeginningBalance();
            _endingBalance = _beginningBalance;
            using (var context = new ERPContext())
            {
                var lines = context.Ledger_Transaction_Lines
                    .Include("LedgerAccount")
                    .Include("LedgerTransaction")
                    .Where(line => line.LedgerAccount.Name.Equals("Cash") &&
                    line.LedgerTransaction.Date.Equals(_date))
                    .ToList();

                var account = new LedgerAccount { ID = -1, Name = "Sales Receipt", Class = "Asset" };
                var transaction = new LedgerTransaction { ID = -1, Date = _date, Description = "Sales Receipt", Documentation = "Sales Receipt" };
                var salesreceiptLine = new LedgerTransactionLine { LedgerTransaction = transaction, LedgerAccount = account, Seq = "Debit" };
                foreach (var line in lines)
                {
                    var lineVM = new LedgerTransactionLineVM { Model = line };
                    foreach (var oppositeLine in lineVM.OpposingLines)
                    {
                        if (!oppositeLine.Description.Equals("Sales Transaction Receipt"))
                        {
                            if (line.Seq == "Credit") oppositeLine.Amount = -oppositeLine.Amount;
                            DisplayedLines.Add(new LedgerTransactionLineVM { Model = oppositeLine.Model, Balance = _beginningBalance + oppositeLine.Amount });
                        }

                        else
                            salesreceiptLine.Amount += oppositeLine.Amount;
                        
                        _endingBalance += oppositeLine.Amount;
                        oppositeLine.Balance = _endingBalance;
                    }
                }
                if (salesreceiptLine.Amount > 0)
                    DisplayedLines.Add(new LedgerTransactionLineVM { Model = salesreceiptLine, Balance = _endingBalance });
            }
            OnPropertyChanged("EndingBalance");
        }
        private static void AddSalesReturnTransactionLedgerTransactionsToDatabaseContext(ERPContext context,
                                                                                         SalesReturnTransaction salesReturnTransaction)
        {
            var totalCOGS =
                salesReturnTransaction.SalesReturnTransactionLines.ToList()
                .Sum(salesReturnTransactionLine => salesReturnTransactionLine.CostOfGoodsSold);

            // Record the corresponding ledger transactions in the database
            var ledgerTransaction1 = new LedgerTransaction();
            var ledgerTransaction2 = new LedgerTransaction();

            if (
                !LedgerTransactionHelper.AddTransactionToDatabase(context, ledgerTransaction1,
                                                                  UtilityMethods.GetCurrentDate().Date, salesReturnTransaction.SalesReturnTransactionID,
                                                                  "Sales Return"))
            {
                return;
            }
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction1,
                                                                 "Sales Returns and Allowances", "Debit", salesReturnTransaction.NetTotal);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction1,
                                                                 "Cash", "Credit", salesReturnTransaction.NetTotal);

            if (
                !LedgerTransactionHelper.AddTransactionToDatabase(context, ledgerTransaction2,
                                                                  UtilityMethods.GetCurrentDate().Date, salesReturnTransaction.SalesReturnTransactionID,
                                                                  "Sales Return"))
            {
                return;
            }
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction2, "Inventory", "Debit",
                                                                 totalCOGS);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction2, "Cost of Goods Sold",
                                                                 "Credit", totalCOGS);

            context.SaveChanges();
        }
        private void OnDeleteTransactionCommandExecuted(LedgerTransaction transaction)
        {
            if (IsReadOnly)
            {
                return;
            }

            if (InLedgerEntryMode)
            {
                this.wasChanged = true;
                this.reconService.RemoveTransaction(this.ledgerService.LedgerBook, LedgerEntry, transaction.Id);
                ShownTransactions.Remove(transaction);
            }
            else if (InBalanceAdjustmentMode)
            {
                this.wasChanged = true;
                this.reconService.CancelBalanceAdjustment(this.entryLine, transaction.Id);
                ShownTransactions.Remove(transaction);
            }

            RaisePropertyChanged(() => TransactionsTotal);
            RaisePropertyChanged(() => LedgerEntry);
        }
예제 #25
0
        public static void AddTransactionLineToDatabase(ERPContext context, LedgerTransaction transaction, string accountName, string seq, decimal amount)
        {
            var transactionLine = new LedgerTransactionLine();
            var account         = context.Ledger_Accounts.First(e => e.Name.Equals(accountName));
            var ledgerAccount   = context.Ledger_General.First(e => e.ID.Equals(account.ID));

            transactionLine.LedgerTransaction = transaction;
            transactionLine.LedgerAccount     = account;
            transactionLine.Seq    = seq;
            transactionLine.Amount = amount;

            // Update the amount of the account
            if (seq == "Debit")
            {
                ledgerAccount.Debit += amount;
            }
            else if (seq == "Credit")
            {
                ledgerAccount.Credit += amount;
            }

            context.Ledger_Transaction_Lines.Add(transactionLine);
        }
예제 #26
0
        public void Can_check_whether_there_are_duplicate_ledgerAccounts()
        {
            var transaction = new LedgerTransaction
            {
                Documentation = "Test",
                Description   = "Test",
                PostingDate   = DateTime.Now
            };
            var transactionLine1 = new LedgerTransactionLine
            {
                Id = 1,
                LedgerAccountId = 1,
                Amount          = 2000,
                IsDebit         = true
            };
            var transactionLine2 = new LedgerTransactionLine
            {
                Id = 2,
                LedgerAccountId = 1,
                Amount          = 2000,
                IsDebit         = false
            };
            var transactionLine3 = new LedgerTransactionLine
            {
                Id = 3,
                LedgerAccountId = 3,
                Amount          = 1000,
                IsDebit         = true
            };

            transaction.LedgerTransactionLines.Add(transactionLine1);
            transaction.LedgerTransactionLines.Add(transactionLine2);
            transaction.LedgerTransactionLines.Add(transactionLine3);
            var areThereDuplicateLedgerAccounts = transaction.AreThereDuplicateAccounts();

            Assert.True(areThereDuplicateLedgerAccounts);
        }
예제 #27
0
        private static void CloseRevenueOrExpenseAccountToRetainedEarnings(LedgerAccount account, ERPContext context)
        {
            var retainedEarnings = context.Ledger_Accounts
                                   .Include("LedgerGeneral")
                                   .Include("LedgerAccountBalances")
                                   .Include("LedgerTransactionLines")
                                   .Single(e => e.Name.Equals("Retained Earnings"));

            if (account.Class.Equals("Expense"))
            {
                var amount      = account.LedgerGeneral.Debit - account.LedgerGeneral.Credit;
                var transaction = new LedgerTransaction();

                LedgerTransactionHelper.AddTransactionToDatabase(context, transaction, UtilityMethods.GetCurrentDate().Date, "Closing Entry", account.Name);
                context.SaveChanges();
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, account.Name, "Credit", amount);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, retainedEarnings.Name, "Debit", amount);

                account.LedgerGeneral.Credit -= amount;
            }

            else if (account.Class.Equals("Revenue"))
            {
                var amount      = account.LedgerGeneral.Credit - account.LedgerGeneral.Debit;
                var transaction = new LedgerTransaction();

                if (!LedgerTransactionHelper.AddTransactionToDatabase(context, transaction, UtilityMethods.GetCurrentDate().Date, "Closing Entry", account.Name))
                {
                    return;
                }
                context.SaveChanges();
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, account.Name, "Debit", amount);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, retainedEarnings.Name, "Credit", amount);

                account.LedgerGeneral.Debit -= amount;
            }
        }
예제 #28
0
        public static bool AddTransactionToDatabase(ERPContext context, LedgerTransaction transaction, DateTime date, string documentation, string description)
        {
            using (var c = new ERPContext())
            {
                var period = c.Ledger_General.First().Period;
                if (period != date.Month)
                {
                    MessageBox.Show("Wrong period.", "Invalid Transaction", MessageBoxButton.OK);
                    return false;
                }

                if (date.Date.Day != UtilityMethods.GetCurrentDate().Day)
                {
                    if (!(date.AddDays(2).Day <= UtilityMethods.GetCurrentDate().Day && date.AddDays(2).Day >= date.Day))
                    {
                        MessageBox.Show("Wrong period.", "Invalid Transaction", MessageBoxButton.OK);
                        return false;
                    }
                }
            }

            transaction.Documentation = documentation;
            transaction.Date = date;
            transaction.Description = description;

            if (Application.Current != null)
            {
                var user = Application.Current.TryFindResource("CurrentUser") as User;
                if (user != null)
                    transaction.User = context.Users.FirstOrDefault(e => e.Username.Equals(user.Username));
            }
            else transaction.User = context.Users.FirstOrDefault(e => e.Username.Equals("EMPTY"));

            context.Ledger_Transactions.Add(transaction);
            return true;
        }
예제 #29
0
        private static void RecordEditedPurchaseTransactionNetTotalChangedLedgerTransactionInDatabaseContext(ERPContext context, PurchaseTransaction editedPurchaseTransaction, decimal valueChanged)
        {
            var purchaseTransactionNetTotalChangedLedgerTransaction = new LedgerTransaction();

            LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction,
                                                             UtilityMethods.GetCurrentDate().Date, editedPurchaseTransaction.PurchaseID, "Purchase Transaction Adjustment");
            context.SaveChanges();

            if (valueChanged > 0)
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction, "Inventory",
                                                                     "Debit", valueChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction,
                                                                     $"{editedPurchaseTransaction.Supplier.Name} Accounts Payable", "Credit", valueChanged);
            }
            else
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction,
                                                                     $"{editedPurchaseTransaction.Supplier.Name} Accounts Payable", "Debit", -valueChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction, "Inventory",
                                                                     "Credit", -valueChanged);
            }
            context.SaveChanges();
        }
 public ActionResult AddorEdit(LedgerTransaction emp)
 {
     try
     {
         using (DBModel db = new DBModel())
         {
             if (emp.LedgerTransactionID == 0)
             {
                 db.LedgerTransactions.Add(emp);
                 db.SaveChanges();
             }
             else
             {
                 db.Entry(emp).State = EntityState.Modified;
                 db.SaveChanges();
             }
         }
         return(Json(new { success = true, html = GlobalClass.RenderRazorViewToString(this, "ViewAll", GetAllLedgetTransaction()), message = "Submitted Successfully" }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet));
     }
 }
예제 #31
0
        private static void RecordPaymentLedgerTransactionInDatabaseContext(ERPContext context, PurchaseTransaction purchaseTransaction, decimal paymentAmount, string paymentMode)
        {
            var accountsPayableName = purchaseTransaction.Supplier.Name + " Accounts Payable";
            var paymentLedgerTransaction = new LedgerTransaction();

            if (!LedgerTransactionHelper.AddTransactionToDatabase(context, paymentLedgerTransaction, UtilityMethods.GetCurrentDate().Date, purchaseTransaction.PurchaseID, "Purchase Payment")) return;
            context.SaveChanges();

            LedgerTransactionHelper.AddTransactionLineToDatabase(context, paymentLedgerTransaction, accountsPayableName, "Debit", paymentAmount);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, paymentLedgerTransaction, paymentMode, "Credit", paymentAmount);
            context.SaveChanges();
        }
예제 #32
0
        private void SaveJournalEntry()
        {
            try
            {
                if (App.IsLicenseValid == false)
                {
                    MessageBox.Show("Your License Has Expired");
                    return;
                }



                if (((FinanceViewModel)DataContext).FakeUser == null)
                {
                    throw new Exception("Please Login");
                }
                ((App)Application.Current).CurrentUser = ((FinanceViewModel)DataContext).FakeUser;
                //if(FakeUser.UserRole.UserInRole!=UserRoles.Accountant && FakeUser.UserRole.UserInRole != UserRoles.Administrator)
                //{
                //    throw new Exception("You have no authority to enter journals");
                //}
                int TransactionId = 0;
                try
                {
                    var v = ((FinanceViewModel)DataContext).LedgerTransactions.DefaultIfEmpty().Max(a => a.LedgerTransactionId);
                    TransactionId = v + 1;
                }
                catch
                {
                    TransactionId = 1;
                }
                jdsd = new List <JournalData>();
                jdsc = new List <JournalData>();
                foreach (var item in xt1.Children)
                {
                    JournalData jd = new JournalData();
                    if (item is StackPanel)
                    {
                        StackPanel s = item as StackPanel;
                        foreach (var cnt in s.Children)
                        {
                            if (cnt is TextBox)
                            {
                                TextBox t1 = cnt as TextBox;
                                if (!string.IsNullOrEmpty(t1.Text))
                                {
                                    decimal d = 0;
                                    if (decimal.TryParse(t1.Text, out d))
                                    {
                                        jd.amount  = d;
                                        jd.EntryId = TransactionId;
                                    }
                                }
                            }
                            else if (cnt is ComboBox)
                            {
                                ComboBox c1 = cnt as ComboBox;
                                if (c1.SelectedIndex > -1)
                                {
                                    var la = c1.SelectedItem as LedgerAccount;
                                    // var logic =.Where(a => a.parentLedgerAccount.LedgerAccountId == la.LedgerAccountId).FirstOrDefault();
                                    foreach (var account in ((FinanceViewModel)DataContext).LedgerAccounts)
                                    {
                                        if (account.parentLedgerAccount != null)
                                        {
                                            if (la.LedgerAccountId == account.parentLedgerAccount.LedgerAccountId)
                                            {
                                                throw new Exception(la.AccountName + " Cannot be Selected");
                                            }
                                        }
                                    }
                                    jd.LedgerAccountId = la.LedgerAccountId;
                                }
                            }
                        }
                        if (jd.LedgerAccountId > 0 && jd.amount > 0)
                        {
                            jdsd.Add(jd);
                        }
                    }
                }
                foreach (var item in cdt1.Children)
                {
                    JournalData jd = new JournalData();
                    if (item is StackPanel)
                    {
                        StackPanel s = item as StackPanel;
                        foreach (var cnt in s.Children)
                        {
                            if (cnt is TextBox)
                            {
                                TextBox t1 = cnt as TextBox;
                                if (!string.IsNullOrEmpty(t1.Text))
                                {
                                    decimal d = 0;
                                    if (decimal.TryParse(t1.Text, out d))
                                    {
                                        jd.amount  = d;
                                        jd.EntryId = TransactionId;
                                    }
                                }
                            }
                            else if (cnt is ComboBox)
                            {
                                ComboBox c1 = cnt as ComboBox;
                                if (c1.SelectedIndex > -1)
                                {
                                    var la = c1.SelectedItem as LedgerAccount;
                                    // var logic =.Where(a => a.parentLedgerAccount.LedgerAccountId == la.LedgerAccountId).FirstOrDefault();
                                    foreach (var account in ((FinanceViewModel)DataContext).LedgerAccounts)
                                    {
                                        if (account.parentLedgerAccount != null)
                                        {
                                            if (la.LedgerAccountId == account.parentLedgerAccount.LedgerAccountId)
                                            {
                                                throw new Exception(la.AccountName + " Cannot be Selected");
                                            }
                                        }
                                    }
                                    jd.LedgerAccountId = (c1.SelectedItem as LedgerAccount).LedgerAccountId;
                                }
                            }
                        }
                        if (jd.LedgerAccountId > 0 && jd.amount > 0)
                        {
                            jdsc.Add(jd);
                        }
                    }
                }
                if (!IsJournalEntryValid())
                {
                    return;
                }
                if (jdsd.Count < 1 || jdsc.Count < 1)
                {
                    return;
                }
                else
                {
                    var totaldebits  = jdsd.Sum(a => a.amount);
                    var totalcredits = jdsc.Sum(a => a.amount);
                    if (totaldebits != totalcredits)
                    {
                        throw new Exception("Debit and Credit Do Not Balance \nOr You May have Some Error in Assigning Accounts");
                    }
                }
                //if (((App)Application.Current).V)
                //{
                //    MessageBox.Show("trial version");
                //    return;
                //}
                List <string>     debitaccounts = new List <string>();
                List <string>     creditamounts = new List <string>();
                LedgerTransaction lt            = new LedgerTransaction();
                //lt.LedgerTransactionId = TransactionId;
                lt.Date        = ((FinanceViewModel)DataContext).JournalDate;
                lt.Description = textBox1.Text;

                lt.UserId = ((FinanceViewModel)DataContext).FakeUser.UserId;
                ((FinanceViewModel)DataContext).Service.InsertLedgerTransaction(lt);
                foreach (var item in jdsd)
                {
                    LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                    ltd.Amount              = item.amount;
                    ltd.LedgerAccountId     = item.LedgerAccountId;
                    ltd.LedgerTransactionId = lt.LedgerTransactionId;
                    ((FinanceViewModel)DataContext).Service.InsertLedgerTransactionDetail(ltd);
                    LedgerGeneral lg = new LedgerGeneral();
                    lg.LedgerAccountId     = item.LedgerAccountId;
                    lg.LedgerTransactionId = lt.LedgerTransactionId;
                    lg.Debit            = item.amount;
                    lg.JournalEntryDate = lt.Date;
                    ((FinanceViewModel)DataContext).Service.InsertLedgerGeneral(lg);
                    debitaccounts.Add(lg.LedgerAccount.AccountName);
                }
                foreach (var item in jdsc)
                {
                    LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                    ltd.Amount              = item.amount;
                    ltd.LedgerAccountId     = item.LedgerAccountId;
                    ltd.LedgerTransactionId = lt.LedgerTransactionId;
                    ((FinanceViewModel)DataContext).Service.InsertLedgerTransactionDetail(ltd);
                    LedgerGeneral lg = new LedgerGeneral();
                    lg.LedgerAccountId     = item.LedgerAccountId;
                    lg.LedgerTransactionId = lt.LedgerTransactionId;
                    lg.Credit           = item.amount;
                    lg.JournalEntryDate = lt.Date;
                    ((FinanceViewModel)DataContext).Service.InsertLedgerGeneral(lg);
                    creditamounts.Add(lg.LedgerAccount.AccountName);
                }
                if (lt.Description.Length < 1)
                {
                    StringBuilder sb = new StringBuilder();
                    // sb.Append("Debit Account(s)");
                    foreach (var item in debitaccounts)
                    {
                        sb.AppendLine(item);
                    }
                    // sb.Append("Credit Account(s)");
                    foreach (var item in creditamounts)
                    {
                        sb.AppendLine(item);
                    }
                    lt.Description = sb.ToString();
                }


                ((FinanceViewModel)DataContext).Service.SaveAll();
                //  Messenger.Default.Send<UpdateView>(new Utility.UpdateView());
                ((FinanceViewModel)DataContext).Init2();
                // ((FinanceViewModel)DataContext).getJournalsBydate();
                ResetControls();
            }
            catch (Exception ex)
            {
                if (ex.Message == "Debit and Credit Do Not Balance")
                {
                    throw ex;
                }
                else
                {
                    ResetControls();
                    throw ex;
                }
            }
        }
 private void AddEntryToDatabase()
 {
     using (var ts = new TransactionScope(TransactionScopeOption.Required))
     {
         var context = new ERPContext();
         var transaction = new LedgerTransaction();
         if (!LedgerTransactionHelper.AddTransactionToDatabase(context, transaction, _newEntryDate, _newEntryDescription, _newEntryDescription)) return;
         context.SaveChanges();
         LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, _newEntryAccount.Name, _newEntrySequence, _newEntryAmount);
         LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, _parentVM.SelectedBank.Name, _newEntrySequence == "Debit" ? "Credit" : "Debit", _newEntryAmount);
         context.SaveChanges();
         ts.Complete();
     }
 }
예제 #34
0
        private void SaveInLedger(SaleOrder SaleOrder, string Mode, LedgerAccount SelectedCustomer, LedgerAccount SelectedBankAccount, string ChequeNumber)
        {
            LedgerTransaction lt = new LedgerTransaction();

            lt.Date        = DateTime.Now;
            lt.UserId      = 1;
            lt.Description = "sales  ";
            Context.LedgerTransactions.Add(lt);
            var result = Context.SaveChanges();

            if (result > 0)
            {
                if (SaleOrder.Due <= 0)
                {
                    if (Mode == "IsCash")
                    {
                        LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                        ltd.Amount = SaleOrder.AmountAfterDiscount;
                        ltd.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd.LedgerAccountId     = 1;
                        Context.LedgerTransactionDetails.Add(ltd);
                        Context.SaveChanges();

                        LedgerTransactionDetail ltd1 = new LedgerTransactionDetail();
                        ltd1.Amount = SaleOrder.AmountAfterDiscount;
                        ltd1.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd1.LedgerAccountId     = 2;
                        Context.LedgerTransactionDetails.Add(ltd1);
                        Context.SaveChanges();

                        LedgerGeneral lg = new LedgerGeneral();
                        lg.LedgerAccountId     = 1;
                        lg.LedgerTransactionId = lt.LedgerTransactionId;
                        lg.Debit            = SaleOrder.AmountAfterDiscount;
                        lg.Credit           = 0;
                        lg.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg);
                        Context.SaveChanges();

                        LedgerGeneral lg1 = new LedgerGeneral();
                        lg1.LedgerAccountId     = 2;
                        lg1.LedgerTransactionId = lt.LedgerTransactionId;
                        lg1.Credit           = SaleOrder.AmountAfterDiscount;
                        lg1.Debit            = 0;
                        lg1.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg1);
                        Context.SaveChanges();
                    }
                    else if (Mode == "IsCredit")
                    {
                        LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                        ltd.Amount = SaleOrder.AmountAfterDiscount;
                        ltd.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd.LedgerAccountId     = 2;
                        Context.LedgerTransactionDetails.Add(ltd);
                        Context.SaveChanges();

                        LedgerTransactionDetail ltd1 = new LedgerTransactionDetail();
                        ltd1.Amount = SaleOrder.AmountAfterDiscount;
                        ltd1.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd1.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        Context.LedgerTransactionDetails.Add(ltd1);
                        Context.SaveChanges();

                        LedgerGeneral lg = new LedgerGeneral();
                        lg.LedgerAccountId     = 2;
                        lg.LedgerTransactionId = lt.LedgerTransactionId;
                        lg.Debit            = 0;
                        lg.Credit           = SaleOrder.AmountAfterDiscount;
                        lg.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg);
                        Context.SaveChanges();

                        LedgerGeneral lg1 = new LedgerGeneral();
                        lg1.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        lg1.LedgerTransactionId = lt.LedgerTransactionId;
                        lg1.Credit           = 0;
                        lg1.Debit            = SaleOrder.AmountAfterDiscount;
                        lg1.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg1);
                        Context.SaveChanges();
                    }
                    else if (Mode == "IsBank")
                    {
                        LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                        ltd.Amount = SaleOrder.AmountAfterDiscount;
                        ltd.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd.LedgerAccountId     = 2;
                        Context.LedgerTransactionDetails.Add(ltd);
                        Context.SaveChanges();

                        LedgerTransactionDetail ltd1 = new LedgerTransactionDetail();
                        ltd1.Amount = SaleOrder.AmountPaid;
                        ltd1.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd1.LedgerAccountId     = SelectedBankAccount.LedgerAccountId;
                        Context.LedgerTransactionDetails.Add(ltd1);
                        Context.SaveChanges();

                        LedgerGeneral lg = new LedgerGeneral();
                        lg.LedgerAccountId     = 2;
                        lg.LedgerTransactionId = lt.LedgerTransactionId;
                        lg.Debit            = 0;
                        lg.Credit           = SaleOrder.AmountAfterDiscount;
                        lg.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg);
                        Context.SaveChanges();

                        LedgerGeneral lg1 = new LedgerGeneral();
                        lg1.LedgerAccountId     = SelectedBankAccount.LedgerAccountId;
                        lg1.LedgerTransactionId = lt.LedgerTransactionId;
                        lg1.Credit           = 0;
                        lg1.Debit            = SaleOrder.AmountAfterDiscount;
                        lg1.ChequeNumber     = ChequeNumber;
                        lg1.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg1);
                        Context.SaveChanges();
                    }
                }
                else
                {
                    if (Mode == "IsCash")
                    {
                        LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                        ltd.Amount = SaleOrder.AmountPaid;
                        ltd.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd.LedgerAccountId     = 1;
                        Context.LedgerTransactionDetails.Add(ltd);
                        Context.SaveChanges();

                        LedgerTransactionDetail ltd1 = new LedgerTransactionDetail();
                        ltd1.Amount = SaleOrder.AmountAfterDiscount;
                        ltd1.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd1.LedgerAccountId     = 2;
                        Context.LedgerTransactionDetails.Add(ltd1);
                        Context.SaveChanges();

                        LedgerTransactionDetail ltd2 = new LedgerTransactionDetail();
                        ltd2.Amount = SaleOrder.Due;
                        ltd2.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd2.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        Context.LedgerTransactionDetails.Add(ltd2);
                        Context.SaveChanges();

                        LedgerGeneral lg = new LedgerGeneral();
                        lg.LedgerAccountId     = 1;
                        lg.LedgerTransactionId = lt.LedgerTransactionId;
                        lg.Credit           = 0;
                        lg.Debit            = SaleOrder.AmountPaid;
                        lg.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg);
                        Context.SaveChanges();

                        LedgerGeneral lg1 = new LedgerGeneral();
                        lg1.LedgerAccountId     = 2;
                        lg1.LedgerTransactionId = lt.LedgerTransactionId;
                        lg1.Debit            = 0;
                        lg1.Credit           = SaleOrder.AmountAfterDiscount;
                        lg1.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg1);
                        Context.SaveChanges();

                        LedgerGeneral lg2 = new LedgerGeneral();
                        lg2.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        lg2.LedgerTransactionId = lt.LedgerTransactionId;
                        lg2.Credit           = 0;
                        lg2.Debit            = SaleOrder.Due;
                        lg2.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg2);
                        Context.SaveChanges();
                    }
                    else if (Mode == "IsCredit")
                    {
                        LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                        ltd.Amount = SaleOrder.AmountAfterDiscount;
                        ltd.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd.LedgerAccountId     = 2;
                        Context.LedgerTransactionDetails.Add(ltd);
                        Context.SaveChanges();

                        if (SaleOrder.AmountPaid > 0)
                        {
                            LedgerTransactionDetail ltd1 = new LedgerTransactionDetail();
                            ltd1.Amount = SaleOrder.AmountPaid;
                            ltd1.LedgerTransactionId = lt.LedgerTransactionId;
                            ltd1.LedgerAccountId     = 1;
                            Context.LedgerTransactionDetails.Add(ltd1);
                            Context.SaveChanges();
                        }

                        LedgerTransactionDetail ltd2 = new LedgerTransactionDetail();
                        ltd2.Amount = SaleOrder.Due;
                        ltd2.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd2.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        Context.LedgerTransactionDetails.Add(ltd2);
                        Context.SaveChanges();

                        LedgerGeneral lg = new LedgerGeneral();
                        lg.LedgerAccountId     = 2;
                        lg.LedgerTransactionId = lt.LedgerTransactionId;
                        lg.Debit            = 0;
                        lg.Credit           = SaleOrder.AmountAfterDiscount;
                        lg.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg);
                        Context.SaveChanges();

                        if (SaleOrder.AmountPaid > 0)
                        {
                            LedgerGeneral lg1 = new LedgerGeneral();
                            lg1.LedgerAccountId     = 1;
                            lg1.LedgerTransactionId = lt.LedgerTransactionId;
                            lg1.Credit           = 0;
                            lg1.Debit            = SaleOrder.AmountPaid;
                            lg1.JournalEntryDate = lt.Date;
                            Context.Ledgergenerals.Add(lg1);
                            Context.SaveChanges();
                        }

                        LedgerGeneral lg2 = new LedgerGeneral();
                        lg2.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        lg2.LedgerTransactionId = lt.LedgerTransactionId;
                        lg2.Credit           = 0;
                        lg2.Debit            = SaleOrder.Due;
                        lg2.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg2);
                        Context.SaveChanges();
                    }
                    else if (Mode == "IsBank")
                    {
                        LedgerTransactionDetail ltd = new LedgerTransactionDetail();
                        ltd.Amount = SaleOrder.AmountAfterDiscount;
                        ltd.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd.LedgerAccountId     = 2;
                        Context.LedgerTransactionDetails.Add(ltd);
                        Context.SaveChanges();


                        LedgerTransactionDetail ltd1 = new LedgerTransactionDetail();
                        ltd1.Amount = SaleOrder.AmountPaid;
                        ltd1.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd1.LedgerAccountId     = SelectedBankAccount.LedgerAccountId;
                        Context.LedgerTransactionDetails.Add(ltd1);
                        Context.SaveChanges();

                        LedgerTransactionDetail ltd2 = new LedgerTransactionDetail();
                        ltd2.Amount = SaleOrder.Due;
                        ltd2.LedgerTransactionId = lt.LedgerTransactionId;
                        ltd2.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        Context.LedgerTransactionDetails.Add(ltd2);
                        Context.SaveChanges();



                        LedgerGeneral lg = new LedgerGeneral();
                        lg.LedgerAccountId     = 2;
                        lg.LedgerTransactionId = lt.LedgerTransactionId;
                        lg.Credit           = SaleOrder.AmountAfterDiscount;
                        lg.Debit            = 0;
                        lg.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg);
                        Context.SaveChanges();

                        LedgerGeneral lg1 = new LedgerGeneral();
                        lg1.LedgerAccountId     = SelectedBankAccount.LedgerAccountId;
                        lg1.LedgerTransactionId = lt.LedgerTransactionId;
                        lg1.Credit           = 0;
                        lg1.Debit            = SaleOrder.AmountPaid;
                        lg1.ChequeNumber     = ChequeNumber;
                        lg1.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg1);
                        Context.SaveChanges();

                        LedgerGeneral lg2 = new LedgerGeneral();
                        lg2.LedgerAccountId     = SelectedCustomer.LedgerAccountId;
                        lg2.LedgerTransactionId = lt.LedgerTransactionId;
                        lg2.Debit            = SaleOrder.Due;
                        lg2.Credit           = 0;
                        lg2.JournalEntryDate = lt.Date;
                        Context.Ledgergenerals.Add(lg2);
                        Context.SaveChanges();
                    }
                }
            }
        }
예제 #35
0
 private static void AddStockAdjustmentDecrementLedgerTransactionToDatabase(ERPContext context, StockAdjustmentTransaction stockAdjustmentTransaction, decimal totalCOGSAdjustment)
 {
     var ledgerTransaction = new LedgerTransaction();
     if (!LedgerTransactionHelper.AddTransactionToDatabase(context, ledgerTransaction, UtilityMethods.GetCurrentDate().Date,
         stockAdjustmentTransaction.StockAdjustmentTransactionID, "Stock Adjustment (Decrement)")) return;
     context.SaveChanges();
     LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction, "Cost of Goods Sold", "Debit", totalCOGSAdjustment);
     LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction, "Inventory", "Credit", totalCOGSAdjustment);
 }
예제 #36
0
        private static void CloseRevenueOrExpenseAccountToRetainedEarnings(LedgerAccount account, ERPContext context)
        {
            var retainedEarnings = context.Ledger_Accounts
                .Include("LedgerGeneral")
                .Include("LedgerAccountBalances")
                .Include("LedgerTransactionLines")
                .Single(e => e.Name.Equals("Retained Earnings"));

            if (account.Class.Equals("Expense"))
            {
                var amount = account.LedgerGeneral.Debit - account.LedgerGeneral.Credit;
                var transaction = new LedgerTransaction();

                LedgerTransactionHelper.AddTransactionToDatabase(context, transaction, UtilityMethods.GetCurrentDate().Date, "Closing Entry", account.Name);
                context.SaveChanges();
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, account.Name, "Credit", amount);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, retainedEarnings.Name, "Debit", amount);

                account.LedgerGeneral.Credit -= amount;
            }

            else if (account.Class.Equals("Revenue"))
            {
                var amount = account.LedgerGeneral.Credit - account.LedgerGeneral.Debit;
                var transaction = new LedgerTransaction();

                if (!LedgerTransactionHelper.AddTransactionToDatabase(context, transaction, UtilityMethods.GetCurrentDate().Date, "Closing Entry", account.Name)) return;
                context.SaveChanges();
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, account.Name, "Debit", amount);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, transaction, retainedEarnings.Name, "Credit", amount);

                account.LedgerGeneral.Debit -= amount;
            }
        }
        private static void AddSalesReturnTransactionLedgerTransactionsToDatabaseContext(ERPContext context,
            SalesReturnTransaction salesReturnTransaction)
        {
            var totalCOGS =
                salesReturnTransaction.SalesReturnTransactionLines.ToList()
                    .Sum(salesReturnTransactionLine => salesReturnTransactionLine.CostOfGoodsSold);

            // Record the corresponding ledger transactions in the database
            var ledgerTransaction1 = new LedgerTransaction();
            var ledgerTransaction2 = new LedgerTransaction();

            if (
                !LedgerTransactionHelper.AddTransactionToDatabase(context, ledgerTransaction1,
                    UtilityMethods.GetCurrentDate().Date, salesReturnTransaction.SalesReturnTransactionID,
                    "Sales Return")) return;
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction1,
                "Sales Returns and Allowances", "Debit", salesReturnTransaction.NetTotal);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction1,
                "Cash", "Credit", salesReturnTransaction.NetTotal);

            if (
                !LedgerTransactionHelper.AddTransactionToDatabase(context, ledgerTransaction2,
                    UtilityMethods.GetCurrentDate().Date, salesReturnTransaction.SalesReturnTransactionID,
                    "Sales Return")) return;
            context.SaveChanges();
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction2, "Inventory", "Debit",
                totalCOGS);
            LedgerTransactionHelper.AddTransactionLineToDatabase(context, ledgerTransaction2, "Cost of Goods Sold",
                "Credit", totalCOGS);

            context.SaveChanges();
        }
예제 #38
0
        /// <summary>
        /// Closes a chart of accounts current ledger period
        /// </summary>
        /// <param name="coaId">Chart of accounts identifier</param>
        public virtual void CloseLedgerPeriod(int coaId)
        {
            var coa = GetChartOfAccountsById(coaId);

            if (coa == null)
            {
                throw new ArgumentException("Chart of accounts does not exist.");
            }

            var year  = coa.CurrentLedgerPeriodStartDate.Year;
            var month = coa.CurrentLedgerPeriodStartDate.Month;

            // Close expense and revenue accounts to retained earnings
            var expenseRevenueAccounts         = GetExpenseRevenueAccounts(coa.LedgerAccounts);
            var retainedEarningsAccount        = GetRetainedEarningsAccount(coa.LedgerAccounts);
            var amountClosedToRetainedEarnings = 0m;

            foreach (var account in expenseRevenueAccounts)
            {
                var closingAmount = CalculatePeriodLedgerAccountBalanceChange(account, year, month);

                if (closingAmount == 0)
                {
                    continue;
                }

                var closingEntry = new LedgerTransaction
                {
                    Documentation     = "Closing Entry",
                    Description       = "Closing Entry",
                    PostingDate       = coa.CurrentLedgerPeriodStartDate.AddMonths(1).AddDays(-1), // last day of the month
                    IsEditable        = false,
                    IsClosing         = true,
                    ChartOfAccountsId = coaId
                };

                var closingEntryLine = new LedgerTransactionLine
                {
                    LedgerAccountId = account.Id,
                    Amount          = closingAmount,
                    IsDebit         =
                        account.Type.Equals(LedgerAccountType.Revenue) ||
                        account.Type.Equals(LedgerAccountType.ContraExpense)
                };

                // take into account if the amount is negative
                closingEntryLine.IsDebit = closingAmount < 0 ? !closingEntryLine.IsDebit : closingEntryLine.IsDebit;

                var closingEntryRetainedEarningsLine = new LedgerTransactionLine
                {
                    LedgerAccountId = retainedEarningsAccount.Id,
                    Amount          = closingAmount,
                    IsDebit         = !closingEntryLine.IsDebit
                };

                amountClosedToRetainedEarnings += closingEntryRetainedEarningsLine.IsDebit
                    ? -closingEntryRetainedEarningsLine.Amount
                    : closingEntryRetainedEarningsLine.Amount;

                closingEntry.LedgerTransactionLines.Add(closingEntryLine);
                closingEntry.LedgerTransactionLines.Add(closingEntryRetainedEarningsLine);

                _repository.Create(closingEntry);
            }

            var balanceSheetAccounts = GetBalanceSheetLedgerAccounts(coa.LedgerAccounts);

            foreach (var account in balanceSheetAccounts)
            {
                bool isCreate;
                var  accountBalance = GetPeriodLedgerAccountBalance(account.Id, year, month, out isCreate);

                // Get the period starting balance
                var accountPeriodStartingBalance = accountBalance.GetMonthBalance(month - 1);

                // Calculate the period ending balance
                decimal accountPeriodEndingBalance;

                if (!account.Name.Equals("Retained Earnings"))
                {
                    accountPeriodEndingBalance = accountPeriodStartingBalance +
                                                 CalculatePeriodLedgerAccountBalanceChange(account, year, month);
                }
                else
                {
                    accountPeriodEndingBalance = accountPeriodStartingBalance + amountClosedToRetainedEarnings;
                }

                // Update the ending balance
                accountBalance.SetMonthBalance(month, accountPeriodEndingBalance);

                if (isCreate)
                {
                    _repository.Create(accountBalance);
                }
                else
                {
                    _repository.Update(accountBalance);
                }
            }

            // Advance and update the current ledger period
            coa.AdvanceLedgerPeriod();
            _repository.Update(coa);

            // Persist changes
            _repository.Save();
        }
예제 #39
0
        private static void RecordEditedPurchaseTransactionNetTotalChangedLedgerTransactionInDatabaseContext(ERPContext context, PurchaseTransaction editedPurchaseTransaction, decimal valueChanged)
        {
            var purchaseTransactionNetTotalChangedLedgerTransaction = new LedgerTransaction();

            LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction,
                UtilityMethods.GetCurrentDate().Date, editedPurchaseTransaction.PurchaseID, "Purchase Transaction Adjustment");
            context.SaveChanges();

            if (valueChanged > 0)
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction, "Inventory",
                    "Debit", valueChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction,
                    $"{editedPurchaseTransaction.Supplier.Name} Accounts Payable", "Credit", valueChanged);
            }
            else
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction,
                    $"{editedPurchaseTransaction.Supplier.Name} Accounts Payable", "Debit", -valueChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionNetTotalChangedLedgerTransaction, "Inventory",
                    "Credit", -valueChanged);
            }
            context.SaveChanges();
        }
예제 #40
0
        private static void RecordEditedPurchaseTransactionTotalCOGSChangedLedgerTransactionInDatabaseContext(
            ERPContext context, PurchaseTransaction editedPurchaseTransaction, decimal totalCOGSChanged)
        {
            var purchaseTransactionTotalCOGSChangedLedgerTransaction = new LedgerTransaction();
            LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, UtilityMethods.GetCurrentDate().Date, editedPurchaseTransaction.PurchaseID, "Purchase Transaction Adjustment (COGS)");
            context.SaveChanges();

            if (totalCOGSChanged > 0)
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Cost of Goods Sold", "Debit", totalCOGSChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Inventory", "Credit", totalCOGSChanged);
            }
            else
            {
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Inventory", "Debit", -totalCOGSChanged);
                LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseTransactionTotalCOGSChangedLedgerTransaction, "Cost of Goods Sold", "Credit", -totalCOGSChanged);
            }
            context.SaveChanges();
        }
예제 #41
0
 private static void RecordPurchaseLedgerTransactionInDatabaseContext(ERPContext context, PurchaseTransaction purchaseTransaction)
 {
     var purchaseLedgerTransaction = new LedgerTransaction();
     var accountsPayableName = purchaseTransaction.Supplier.Name + " Accounts Payable";
     if (!LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseLedgerTransaction, UtilityMethods.GetCurrentDate().Date, purchaseTransaction.PurchaseID, "Purchase Transaction")) return;
     context.SaveChanges();
     LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseLedgerTransaction, "Inventory", "Debit", purchaseTransaction.Total);
     LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseLedgerTransaction, accountsPayableName, "Credit", purchaseTransaction.Total);
     context.SaveChanges();
 }
 private static void AddPurchaseReturnTransactionLedgerTransactionToDatabaseContext(ERPContext context, PurchaseReturnTransaction purchaseReturnTransaction, decimal totalCOGS)
 {
     var purchaseReturnLedgerTransaction = new LedgerTransaction();
     if (!LedgerTransactionHelper.AddTransactionToDatabase(context, purchaseReturnLedgerTransaction, UtilityMethods.GetCurrentDate(), purchaseReturnTransaction.PurchaseReturnTransactionID, "Purchase Return")) return;
     context.SaveChanges();
     LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseReturnLedgerTransaction,
         $"{purchaseReturnTransaction.PurchaseTransaction.Supplier.Name} Accounts Payable", "Debit", purchaseReturnTransaction.NetTotal);
     if (totalCOGS - purchaseReturnTransaction.NetTotal > 0)
         LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseReturnLedgerTransaction, "Cost of Goods Sold", "Debit", totalCOGS - purchaseReturnTransaction.NetTotal);
     LedgerTransactionHelper.AddTransactionLineToDatabase(context, purchaseReturnLedgerTransaction, "Inventory", "Credit", totalCOGS);
     context.SaveChanges();
 }
예제 #43
0
 // ReSharper disable once RedundantAssignment
 partial void ModelFactory(LedgerTransactionDto dto, ref LedgerTransaction model)
 {
     model = this.transactionFactory.Build(dto.TransactionType, dto.Id);
 }
예제 #44
0
 partial void ToDtoPostprocessing(ref LedgerTransactionDto dto, LedgerTransaction model)
 {
     dto.TransactionType = model.GetType().FullName;
     // Inheritance could be better handled.
     var bankBalanceTransaction = model as BankBalanceAdjustmentTransaction;
     if (bankBalanceTransaction != null)
     {
         dto.Account = bankBalanceTransaction.BankAccount.Name;
     }
 }
예제 #45
0
 partial void ToModelPostprocessing(LedgerTransactionDto dto, ref LedgerTransaction model)
 {
     // Inheritance could be better handled.
     var balanaceTransaction = model as BankBalanceAdjustmentTransaction;
     if (balanaceTransaction != null)
     {
         balanaceTransaction.BankAccount = this.accountTypeRepo.GetByKey(dto.Account);
     }
 }