public ActionResult AddSales(string request) { //using (var transaction = _context.Database.BeginTransaction()) //{ TblSale tbl_Sale = JsonConvert.DeserializeObject <TblSale>(request); if (SalesItemStock.Count > 0) { var saved = false; try { var t = JsonConvert.SerializeObject(tbl_Sale); _context.TblSale.Add(tbl_Sale); _context.SaveChanges(); SalesItemStock.ForEach(q => q.Item = null); _context.TblItemStock.AddRange(SalesItemStock); _context.SaveChanges(); TblSequence NewSequenceValue = GenericHelper.GetNextUpdatedData("tbl_Sale"); _context.Entry(NewSequenceValue).State = EntityState.Modified; //_context.SaveChanges(); TblSequence NewSequenceValuetbl_SalesInvoice = GenericHelper.GetNextUpdatedData("tbl_SalesInvoice"); _context.Entry(NewSequenceValuetbl_SalesInvoice).State = EntityState.Modified; //_context.SaveChanges(); TblSequence NewSequenceValueItemStock = GenericHelper.GetNextUpdatedData("tbl_ItemStock"); _context.Entry(NewSequenceValueItemStock).State = EntityState.Modified; //_context.SaveChanges(); //_context.SaveChanges(); // 1 hold -- only save Sales & Item with status hold // 2 CREDIT PURCHASE -- one transaction entry with Debit (In) with unpaid status // 3 CASH PAY -- one transaction entry with Debit (In)/ Paid // one payment Entry with full amount // one transaction entry with credit (out) with payment ID // 4 MULTI MODE PAY -- one transaction entry with Debit (In) // one payment Entry // one transaction entry with credit (out) if (tbl_Sale.PaymentMode != null && tbl_Sale.PaymentMode.Value != PaymentMode.HOLD ) // not hold { var amount = tbl_Sale.Amount; var creditTID = GenericHelper.GetMaxValue(SequenceTable.tbl_TransactionCredit); var tbl_Transaction_Credit = new TblTransaction { Id = creditTID, CreatedDatetime = tbl_Sale.CreatedDatetime, EntryDate = tbl_Sale.InvoiceDate.ToString(), EntryType = TransactionEntryType.Sales .ToString(), //"Purchase", // Purchase/Sales/Recipt/Payment Status = TransactionStatus .Unpaid, //Paid/Unpaid/Pending/Paid Against entries : // Recive Against entries TransactionType = TransactionType.Debit .ToString(), //Debit/ credit -- For Real: Debit what comes in, credit what goes out. //TransactionRef = null, EntryId = tbl_Sale.SaleId, //PurchaseId = tbl_Sale.Id, SalesId = tbl_Sale.SaleId, Amount = tbl_Sale.GrandTotal, VendorId = tbl_Sale.CustomerId }; _context.TblTransaction.Add(tbl_Transaction_Credit); //_context.SaveChanges(); var NewSequenceValueTbl_Transaction = GenericHelper.GetNextUpdatedData("tbl_TransactionCredit"); _context.Entry(NewSequenceValueTbl_Transaction).State = EntityState.Modified; //_context.SaveChanges(); if (tbl_Sale.PaymentMode == 2) //CREDIT PURCHASE { } else if (tbl_Sale.PaymentMode == 3) //CASH PAY { var tbl_PaymentID = GenericHelper.GetMaxValue("tbl_Payment"); var tbl_Payment = new TblPayment { Id = tbl_PaymentID, Amount = tbl_Sale.GrandTotal, CreatedDatetime = tbl_Sale.CreatedDatetime, PartyName = tbl_Sale.SaleId, PaymentDate = tbl_Sale.InvoiceDate.ToString(), PaymentMode = "CASH" //Remark = "", //TransactionID = creditTID, }; _context.TblPayment.Add(tbl_Payment); var NewSequenceValueTbl_Payment = GenericHelper.GetNextUpdatedData("tbl_Payment"); _context.Entry(NewSequenceValueTbl_Payment).State = EntityState.Modified; //_context.SaveChanges(); var tbl_TransactionDebit = new TblTransaction { Id = GenericHelper.GetMaxValue("tbl_TransactionDebit"), CreatedDatetime = tbl_Sale.CreatedDatetime, EntryDate = tbl_Sale.InvoiceDate.ToString(), EntryType = TransactionEntryType.Sales .ToString(), // "Purchase", // Purchase/Sales/Recipt/Payment Status = TransactionStatus.PaidAgainstEntries + tbl_Sale .SaleId, //Paid/Unpaid/Pending/Paid Against entries : // Recive Against entries TransactionType = TransactionType.Credit .ToString(), //Debit/ credit -- For Real: Debit what comes in, credit what goes out. TransactionRef = tbl_Sale.SaleId, EntryId = tbl_PaymentID, //PurchaseID = null, //SalesID = null, PaymentId = tbl_PaymentID, Amount = tbl_Sale.GrandTotal, VendorId = tbl_Sale.CustomerId }; _context.TblTransaction.Add(tbl_TransactionDebit); var NewSequenceValuetbl_Transaction2 = GenericHelper.GetNextUpdatedData("tbl_TransactionDebit"); _context.Entry(NewSequenceValuetbl_Transaction2).State = EntityState.Modified; //_context.SaveChanges(); //var tbl_Transaction result = _context.tbl_Transaction.Where(q => q.ID == creditTID).FirstOrDefault(); tbl_Transaction_Credit.Status = TransactionStatus.Paid; //_context.Entry(tbl_Transactionresult).State = EntityState.Modified; //_context.SaveChanges(); } else if (tbl_Sale.PaymentMode == 4) //MULTI MODE PAY { var tbl_PaymentID = GenericHelper.GetMaxValue("tbl_Payment"); var tbl_Payment = new TblPayment { Id = tbl_PaymentID, Amount = amount, CreatedDatetime = tbl_Sale.CreatedDatetime, PartyName = tbl_Sale.CustomerId, PaymentDate = tbl_Sale.InvoiceDate.ToString(), PaymentMode = "CASH" //Remark = "", //TransactionID = creditTID, }; _context.TblPayment.Add(tbl_Payment); var NewSequenceValueTbl_Payment = GenericHelper.GetNextUpdatedData("tbl_Payment"); _context.Entry(NewSequenceValueTbl_Payment).State = EntityState.Modified; //_context.SaveChanges(); var tbl_TransactionDebit = new TblTransaction { Id = GenericHelper.GetMaxValue("tbl_TransactionDebit"), CreatedDatetime = tbl_Sale.CreatedDatetime, EntryDate = tbl_Sale.InvoiceDate.ToString(), EntryType = TransactionEntryType.Purchase .ToString(), // "Purchase", // Purchase/Sales/Recipt/Payment Status = TransactionStatus.PaidAgainstEntries + tbl_Sale .SaleId, //Paid/Unpaid/Pending/Paid Against entries : // Recive Against entries TransactionType = TransactionType.Debit .ToString(), //Debit/ credit -- For Real: Debit what comes in, credit what goes out. TransactionRef = tbl_Sale.SaleId, EntryId = tbl_PaymentID, //PurchaseID = null, //SalesID = null, PaymentId = tbl_PaymentID, Amount = amount, VendorId = tbl_Sale.CustomerId }; _context.TblTransaction.Add(tbl_TransactionDebit); var NewSequenceValueTbl_Transaction2 = GenericHelper.GetNextUpdatedData("tbl_TransactionDebit"); _context.Entry(NewSequenceValueTbl_Transaction2).State = EntityState.Modified; } } _context.SaveChanges(); saved = true; } catch (Exception e) { throw new InvalidOperationException(e.Message); } finally { //if (saved) transaction.Commit(); } //if (tbl_Sale.PaymentMode == 4) //MULTI MODE PAY // return JavaScript("window.location = '/Purchases/index'"); //else //return RedirectToAction("Index"); return(Content("window.location = '/Sale/index'")); } else { ViewBag.CustomerID = new SelectList(_context.TblVendor, "ID", "Name", tbl_Sale.CustomerId); throw new InvalidOperationException("Please add item to continue!!"); } //return RedirectToAction("Index"); //} }