public static void AddSalesReturnTransactionToDatabase(SalesReturnTransaction salesReturnTransaction) { IsLastSaveSuccessful = false; using (var ts = new TransactionScope()) { var context = new ERPContext(); AttachSalesReturnTransactionPropertiesToDatabaseContext(context, ref salesReturnTransaction); var lines = salesReturnTransaction.SalesReturnTransactionLines.ToList(); salesReturnTransaction.SalesReturnTransactionLines.Clear(); foreach (var salesReturnTransactionLine in lines) { salesReturnTransactionLine.SalesReturnTransaction = salesReturnTransaction; AddSalesReturnTransactionLineToDatabaseContext(context, salesReturnTransactionLine); DecreaseSalesReturnTransactionLineItemSoldOrReturnedInDatabaseContext(context, salesReturnTransactionLine); InceaseSalesReturnTransactionLineItemStockInDatabaseContext(context, salesReturnTransactionLine); context.SaveChanges(); } AddSalesReturnTransactionLedgerTransactionsToDatabaseContext(context, salesReturnTransaction); // Deduct member's points ts.Complete(); } IsLastSaveSuccessful = true; }
public static void AddSalesReturnTransactionToDatabase(SalesReturnTransaction salesReturnTransaction) { IsLastSaveSuccessful = false; using (var ts = new TransactionScope()) { var context = new ERPContext(); AttachSalesReturnTransactionPropertiesToDatabaseContext(context, ref salesReturnTransaction); var lines = salesReturnTransaction.SalesReturnTransactionLines.ToList(); salesReturnTransaction.SalesReturnTransactionLines.Clear(); foreach (var salesReturnTransactionLine in lines) { salesReturnTransactionLine.SalesReturnTransaction = salesReturnTransaction; AddSalesReturnTransactionLineToDatabaseContext(context, salesReturnTransactionLine); DecreaseSalesReturnTransactionLineItemSoldOrReturnedInDatabaseContext(context, salesReturnTransactionLine); InceaseSalesReturnTransactionLineItemStockInDatabaseContext(context, salesReturnTransactionLine); context.SaveChanges(); } AddSalesReturnTransactionLedgerTransactionsToDatabaseContext(context, salesReturnTransaction); // Deduct member's points ts.Complete(); } IsLastSaveSuccessful = true; }
private static void AttachSalesReturnTransactionPropertiesToDatabaseContext(ERPContext context, ref SalesReturnTransaction salesReturnTransaction) { //var user = Application.Current.FindResource("CurrentUser") as User; //salesReturnTransaction.User = context.Users.FirstOrDefault(e => e.Username.Equals(user.Username)); context.SalesTransactions.Attach(salesReturnTransaction.SalesTransaction); if (salesReturnTransaction.SalesTransaction.Customer != null) context.Customers.Attach(salesReturnTransaction.SalesTransaction.Customer); }
private static void AttachSalesReturnTransactionPropertiesToDatabaseContext(ERPContext context, ref SalesReturnTransaction salesReturnTransaction) { //var user = Application.Current.FindResource("CurrentUser") as User; //salesReturnTransaction.User = context.Users.FirstOrDefault(e => e.Username.Equals(user.Username)); context.SalesTransactions.Attach(salesReturnTransaction.SalesTransaction); if (salesReturnTransaction.SalesTransaction.Customer != null) { context.Customers.Attach(salesReturnTransaction.SalesTransaction.Customer); } }
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 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(); }