Add Up Payments to Confirm Invoice as Paid MVC 3
In my MVC 3 project, I have an Invoice and Payments tables. I have a page where the user can confirm that payments have been made against the invoices. The invoice status must change to "Paid" or "Partly Paid" depending on the amount paid or the accumulated amount paid. The accumulated amount is where I'm having a problem. My code will only work and change the status to 'confirmed' if the user pays the full amount the first time round - it doesn't accumulate the amount.
I'm new to C# and am not quite sure how to do this. The amounts come from radio buttons, which works fine. So here is the code in my controller:
public ActionResult Confirm(int id, long InvoiceAmount, string PaymentType)
{
Invoices invoices = db.Invoice.Find(id);
开发者_开发百科 //now validate that if the logged in user is authorized to select and confirm this invoice or not.
var clientPayment = new ClientPayments();
if (InvoiceAmount + clientPayment.PaymentAmount != invoices.InvoiceAmount)
{
invoices.InvoiceStatus = "Partly Paid";
}
else
{
invoices.InvoiceStatus = "Confirmed";
}
db.Entry(invoices).State = EntityState.Modified;
clientPayment.InvoiceNumberID = id;
clientPayment.PaymentAmount = InvoiceAmount;
clientPayment.PaymentType = PaymentType;
clientPayment.PaymentDate = DateTime.Now;
// Set clientPayment properties
db.ClientPayments.Add(clientPayment);
db.SaveChanges();
InvoiceAmount above is the payment amount selected from the radio buttons (probably I should change it to something that makes more sense), clientPayment.PaymentAmount should be the column in the payments table and invoices.InvoiceAmount is the amount in the Invoice table.
Thanks
You are not accumulating the payments. Your code says
clientPayment.PaymentAmount = InvoiceAmount
So each time the payment is updated to the amount paid in that single action. It should be
clientPayment.PaymentAmount += invoiceAmount
(watch the little + sign)
BTW, off-topic, it is a widely used convention to use (lower) camel case for parameter names, like long invoiceAmount,
...
精彩评论