public JsonResult Create(Payment payment, string type, CreditCardCharge cardCharge, TPCheck tPCheck) { payment.PaymentDate = DateTime.Now; payment.PaymentType = (PaymentType)Enum.Parse(typeof(PaymentType), type); db.Payments.Add(payment); db.SaveChanges(); if (payment.PaymentType == PaymentType.CreditCard) { cardCharge.PaymentId = payment.Id; db.CreditCardCharges.Add(cardCharge); db.SaveChanges(); } else if (payment.PaymentType == PaymentType.Check) { tPCheck.PaymentId = payment.Id; db.TPChecks.Add(tPCheck); db.SaveChanges(); } var clientId = payment.ClientId; var amount = payment.Amount; var clientBalance = db.ClientBalances.Find(clientId); clientBalance.Subtract(amount); Invoice.SubtractIB(amount, clientId); ClientWeek.SubtractBD(amount, clientId); db.SaveChanges(); return(Json(new { Amount = payment.Amount })); }
public JsonResult Edit(Payment payment, string type, CreditCardCharge cardCharge, TPCheck tPCheck) { var clientId = payment.ClientId; var oldPayment = db.Payments.Find(payment.Id); if (payment.Amount > oldPayment.Amount) { db.ClientBalances.Find(clientId).Subtract(payment.Amount - oldPayment.Amount); ClientWeek.SubtractBD(payment.Amount - oldPayment.Amount, clientId); } else if (payment.Amount < oldPayment.Amount) { db.ClientBalances.Find(clientId).Add(oldPayment.Amount - payment.Amount); ClientWeek.AddBD(oldPayment.Amount - payment.Amount, clientId); } payment.PaymentType = (PaymentType)Enum.Parse(typeof(PaymentType), type); db.Set <Payment>().AddOrUpdate(payment); db.SaveChanges(); if (payment.PaymentType == PaymentType.CreditCard) { cardCharge.PaymentId = payment.Id; db.Set <CreditCardCharge>().AddOrUpdate(cardCharge); db.SaveChanges(); var charge = db.CreditCardCharges.Include(c => c.Card).SingleOrDefault(c => c.PaymentId == payment.Id); var card = db.CreditCards.Find(charge.CardId); return(Json(new { Id = payment.Id, PaymentDate = payment.PaymentDate, Amount = payment.Amount, PaymentType = payment.PaymentType, Name = card.Name, CardEnding = card.CardNumber.Substring(card.CardNumber.Length - 4), Location = charge.Location })); } else if (payment.PaymentType == PaymentType.Check) { tPCheck.PaymentId = payment.Id; db.Set <TPCheck>().AddOrUpdate(tPCheck); db.SaveChanges(); var check = db.TPChecks.Find(payment.Id); return(Json(new { Id = payment.Id, PaymentDate = payment.PaymentDate, Amount = payment.Amount, PaymentType = payment.PaymentType, Memo = check.Memo })); } return(Json(payment)); }