public bool AddPaidAccountDetail(PaidAccountDetail _paidAccountDetail) { using (var db = new KoontabiDBEntities()) { try { db.Database.BeginTransaction(); db.PaidAccountDetails.Add(_paidAccountDetail); var result = db.PaidAccounts.Find(_paidAccountDetail.PaidAccountID); result.AmountPending -= _paidAccountDetail.Payment; if (result.AmountPending == 0) { result.State = db.SP_GET_STATUS(2).FirstOrDefault().STATUS; result.DayDelay = 0; } db.SaveChanges(); db.Database.CurrentTransaction.Commit(); return(true); } catch (Exception) { db.Database.CurrentTransaction.Rollback(); throw; } } }
public Invoice Add(Invoice entity) { using (var db = new KoontabiDBEntities()) { try { db.Database.BeginTransaction(); db.Invoices.Add(entity); if (entity.TypeOfPayID == 2) { decimal subtotal = 0; decimal discount = 0; decimal itbis = 0; foreach (var item in entity.InvoiceDetails) { subtotal += item.Quantity * item.UnitPrice; } if (entity.DisCount.HasValue) { discount = ((decimal)entity.DisCount.Value * 100) * subtotal / 100; } if (entity.TaxID.HasValue) { Tax t = db.Taxes.Find(entity.TaxID.Value); itbis = (t.TaxValue * subtotal) / 100; } subtotal -= discount; subtotal += itbis; PaidAccount p = new PaidAccount(); p.InvoiceID = entity.InvoiceID; p.DateEmit = entity.InvoiceDate; p.ExpirationDate = entity.ExpirationInvoiceDate; p.Amount = subtotal; p.AmountPending = subtotal; p.DayDelay = Convert.ToInt32((entity.InvoiceDate - entity.ExpirationInvoiceDate).TotalDays); p.State = db.SP_GET_STATUS(1).FirstOrDefault().STATUS; db.PaidAccounts.Add(p); } db.SaveChanges(); db.Database.CurrentTransaction.Commit(); return(new Invoice() { InvoiceID = entity.InvoiceID }); } catch (Exception) { db.Database.CurrentTransaction.Rollback(); throw; } } }