protected void btnPay_Click(object sender, EventArgs e) { try { int invoiceID = 336368; int entityID = 224413; Invoice invoice = InvoiceDB.GetByID(invoiceID); if (invoice == null) { HideTableAndSetErrorMessage("Invalid invoice ID"); return; } ArrayList useVouchers = new ArrayList(); decimal total = 0; for (int i = 0; i < lstPayments.Items.Count; i++) { HiddenField hiddenCreditID = (HiddenField)lstPayments.Items[i].FindControl("hiddenCreditID"); TextBox txtAmount = (TextBox)lstPayments.Items[i].FindControl("txtAmount"); txtAmount.Text = txtAmount.Text.Trim(); if (txtAmount.Text.Length > 0) { useVouchers.Add(new Tuple <int, decimal>(Convert.ToInt32(hiddenCreditID.Value), Convert.ToDecimal(txtAmount.Text))); total += Convert.ToDecimal(txtAmount.Text); } } decimal totalOwed = invoice.TotalDue - total; bool isOverPaid = totalOwed < 0; bool isPaid = totalOwed <= 0; if (isOverPaid) { SetErrorMessage("Total can not be more than the amount owing."); return; } ArrayList creditIDsAdded = new ArrayList(); try { foreach (Tuple <int, decimal> item in useVouchers) { int creditID = CreditDB.Insert_UseVoucher(entityID, item.Item2, item.Item1, invoiceID, Convert.ToInt32(Session["StaffID"])); creditIDsAdded.Add(creditID); } } catch (Exception ex) { // roll back foreach (int creditID in creditIDsAdded) { CreditDB.Delete(creditID); } throw; } if (isPaid) { InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true); } FillCreditGrid(); FillPayments(); } catch (Exception ex) { lblErrorMessage.Text = ex.Message; } }
protected void btnSubmit_Click(object sender, EventArgs e) { if (!IsValidFormID()) { HideTableAndSetErrorMessage(); return; } Invoice invoice = InvoiceDB.GetByID(GetFormID()); if (invoice == null) { HideTableAndSetErrorMessage("Invalid invoice ID"); return; } decimal total = 0; ArrayList receipts = new ArrayList(); for (int i = 0; i < lstPayments.Items.Count; i++) { Label lblTypeID = (Label)lstPayments.Items[i].FindControl("lblTypeID"); TextBox txtAmount = (TextBox)lstPayments.Items[i].FindControl("txtAmount"); txtAmount.Text = txtAmount.Text.Trim(); if (txtAmount.Text.Length > 0 && lblTypeID != null) { receipts.Add(new Tuple <int, decimal>(Convert.ToInt32(lblTypeID.Text), Convert.ToDecimal(txtAmount.Text))); total += Convert.ToDecimal(txtAmount.Text); } } ArrayList vouchers = new ArrayList(); for (int i = 0; i < lstVouchers.Items.Count; i++) { HiddenField hiddenCreditID = (HiddenField)lstVouchers.Items[i].FindControl("hiddenCreditID"); HiddenField hiddenEntityID = (HiddenField)lstVouchers.Items[i].FindControl("hiddenEntityID"); TextBox txtAmount = (TextBox)lstVouchers.Items[i].FindControl("txtAmount"); txtAmount.Text = txtAmount.Text.Trim(); if (txtAmount.Text.Length > 0) { vouchers.Add(new Tuple <int, int, decimal>(Convert.ToInt32(hiddenCreditID.Value), Convert.ToInt32(hiddenEntityID.Value), Convert.ToDecimal(txtAmount.Text))); total += Convert.ToDecimal(txtAmount.Text); } } if (txtCreditNoteTotal.Text == string.Empty) { txtCreditNoteTotal.Text = "0"; } total += Convert.ToDecimal(txtCreditNoteTotal.Text); decimal totalOwed = invoice.TotalDue - total; bool isOverPaid = totalOwed < 0; bool isPaid = totalOwed <= 0; if (isOverPaid) { SetErrorMessage("Total can not be more than the amount owing."); return; } // put in try/catch block in case someone just used the vouchers and there is more being used than is remaining in the voucher ArrayList creditIDsAdded = new ArrayList(); try { foreach (Tuple <int, int, decimal> item in vouchers) { int creditID = CreditDB.Insert_UseVoucher(item.Item2, item.Item3, item.Item1, invoice.InvoiceID, Convert.ToInt32(Session["StaffID"])); creditIDsAdded.Add(creditID); } } catch (Exception ex) { // roll back foreach (int creditID in creditIDsAdded) { CreditDB.Delete(creditID); } SetErrorMessage(ex.Message); return; } foreach (Tuple <int, decimal> item in receipts) { ReceiptDB.Insert(null, item.Item1, invoice.InvoiceID, item.Item2, Convert.ToDecimal(0.00), false, isOverPaid, DateTime.MinValue, Convert.ToInt32(Session["StaffID"])); } if (Convert.ToDecimal(txtCreditNoteTotal.Text) > 0) { CreditNoteDB.Insert(invoice.InvoiceID, Convert.ToDecimal(txtCreditNoteTotal.Text), txtCreditCardReason.Text, Convert.ToInt32(Session["StaffID"])); } if (isPaid) { InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true); } FillEmptyAddForm(); // close this window string returnValue = Request.QueryString["returnValue"] != null ? Request.QueryString["returnValue"] : "false"; Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.returnValue=" + returnValue + ";window.opener.location.href = window.opener.location.href;self.close();</script>"); }