Пример #1
0
    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>");
    }
Пример #2
0
    protected void GrdOrgInvoicesOutstanding_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Email")
        {
            int          organisationID = Convert.ToInt32(e.CommandArgument);
            Organisation org            = OrganisationDB.GetByID(organisationID);
            string[]     emails         = ContactDB.GetEmailsByEntityID(org.EntityID);

            if (emails.Length == 0)
            {
                SetErrorMessage("No email address set for '" + org.Name + "'. Please set one to email invoices to them.");
                return;
            }
            else
            {
                DataTable dt  = Session["orginvoicesoutstanding_data"] as DataTable;
                DataRow   row = dt.Select("organisation_id = " + organisationID)[0];

                string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"];
                int[]  invoiceIDs         = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32);


                string tmpLettersDirectory = Letter.GetTempLettersDirectory();
                string originalFile        = Letter.GetLettersDirectory() + (!UserView.GetInstance().IsAgedCareView ? @"OverdueInvoiceTemplate.docx" : @"OverdueInvoiceTemplateAC.docx");
                string tmpDir = FileHelper.GetTempDirectoryName(tmpLettersDirectory);
                System.IO.Directory.CreateDirectory(tmpDir);
                string outputFile = tmpDir + "OverdueInvoices.pdf";


                try
                {
                    Letter.GenerateOutstandingInvoices(originalFile, outputFile, invoiceIDs, -1, organisationID);

                    EmailerNew.SimpleEmail(
                        ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromEmail"].Value,
                        ((SystemVariables)System.Web.HttpContext.Current.Session["SystemVariables"])["Email_FromName"].Value,
                        string.Join(",", emails),
                        "Overdue Invoices",
                        "Pease find your Invoices attached. <br/>Please call us if you do not agree with the Invoice amount stated.<br /><br />Thank you.",
                        true,
                        new string[] { outputFile },
                        false,
                        null
                        );

                    SetErrorMessage("Invoices Emailed to " + org.Name + " (" + string.Join(",", emails) + ")");
                }
                catch (CustomMessageException cmEx)
                {
                    SetErrorMessage(cmEx.Message);
                }
                catch (Exception ex)
                {
                    SetErrorMessage("", ex.ToString());
                }
                finally
                {
                    try { if (System.IO.File.Exists(outputFile))
                          {
                              System.IO.File.Delete(outputFile);
                          }
                    }
                    catch (Exception) { }

                    // delete temp dir
                    if (tmpDir != null)
                    {
                        try { System.IO.Directory.Delete(tmpDir, true); }
                        catch (Exception) { }
                    }
                }
            }
        }

        if (e.CommandName == "Print")
        {
            int organisationID = Convert.ToInt32(e.CommandArgument);

            DataTable dt  = Session["orginvoicesoutstanding_data"] as DataTable;
            DataRow   row = dt.Select("organisation_id = " + organisationID)[0];

            SetErrorMessage("Org ID: " + row["organisation_id"] + "<br />Invoices: " + row["invoice_ids_comma_sep"]);

            string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"];
            int[]  invoiceIDs         = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32);

            Letter.GenerateOutstandingInvoicesToPrint(Response, invoiceIDs, -1, organisationID, !UserView.GetInstance().IsAgedCareView);
        }

        if (e.CommandName == "SetAllPaid" || e.CommandName == "SetAllWiped")
        {
            try
            {
                int organisationID = Convert.ToInt32(e.CommandArgument);

                DataTable dt  = Session["orginvoicesoutstanding_data"] as DataTable;
                DataRow   row = dt.Select("organisation_id = " + organisationID)[0];

                string invoiceIDsCommaSep = (string)row["invoice_ids_comma_sep"];
                int[]  invoiceIDs         = Array.ConvertAll <string, int>(invoiceIDsCommaSep.Split(','), Convert.ToInt32);


                foreach (int invoiceID in invoiceIDs)
                {
                    Invoice invoice = InvoiceDB.GetByID(invoiceID);
                    if (invoice == null || invoice.IsPaID)
                    {
                        continue;
                    }

                    if (e.CommandName.Equals("SetAllPaid"))
                    {
                        ReceiptDB.Insert(null, 362, invoice.InvoiceID, invoice.TotalDue, Convert.ToDecimal(0.00), false, false, DateTime.MinValue, Convert.ToInt32(Session["StaffID"]));
                        InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true);
                    }
                    else if (e.CommandName.Equals("SetAllWiped"))
                    {
                        CreditNoteDB.Insert(invoice.InvoiceID, invoice.TotalDue, string.Empty, Convert.ToInt32(Session["StaffID"]));
                        InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true);
                    }
                }

                SetErrorMessage("Invoices Set As " + (e.CommandName.Equals("SetAllPaid") ? "Paid" : "Wiped") + " : " + row["invoice_ids_comma_sep"]);

                GrdOrgInvoicesOutstanding_FillGrid();
            }
            catch (CustomMessageException cmEx)
            {
                SetErrorMessage(cmEx.Message);
            }
            catch (Exception ex)
            {
                SetErrorMessage("", ex.ToString());
            }
        }
    }
Пример #3
0
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (GetUrlParamType() == UrlParamType.View)
        {
            Response.Redirect(UrlParamModifier.AddEdit(Request.RawUrl, "type", "edit"));
        }
        //else if (GetUrlParamType() == UrlParamType.Edit)
        //{
        //    if (!IsValidFormID())
        //    {
        //        HideTableAndSetErrorMessage();
        //        return;
        //    }
        //    Receipt receipt = ReceiptDB.GetByID(GetFormID());
        //    if (receipt == null)
        //    {
        //        HideTableAndSetErrorMessage("Invalid receipt ID");
        //        return;
        //    }

        //    ReceiptDB.Update(receipt.ReceiptID, Convert.ToInt32(ddlPaymentType.SelectedValue), Convert.ToDecimal(txtTotal.Text), Convert.ToDecimal(txtAmountReconciled.Text), chkFailedToClear.Checked, receipt.IsOverpaid, GetBankProcessedDateFromForm());

        //    Response.Redirect(UrlParamModifier.AddEdit(Request.RawUrl, "type", "view_only"));


        //    // close this window
        //    //Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.returnValue=false;self.close();</script>");
        //}
        else if (GetUrlParamType() == UrlParamType.Add)
        {
            if (!IsValidFormID())
            {
                HideTableAndSetErrorMessage();
                return;
            }
            Invoice invoice = InvoiceDB.GetByID(GetFormID());
            if (invoice == null)
            {
                HideTableAndSetErrorMessage("Invalid invoice ID");
                return;
            }

            decimal thisRefundAmount = Convert.ToDecimal(txtTotal.Text);
            if (thisRefundAmount > invoice.ReceiptsTotal)
            {
                SetErrorMessage("Can not be more than the amount receipted.");
                return;
            }


            if (!invoice.IsPaID)
            {
                int credit_note_id = CreditNoteDB.Insert(invoice.InvoiceID, invoice.TotalDue, "Auto credit noted to set inv as paid so can refund.", Convert.ToInt32(Session["StaffID"]));
                InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true);
            }

            int refund_id = RefundDB.Insert(invoice.InvoiceID, thisRefundAmount, Convert.ToInt32(ddlRefundReason.SelectedValue), txtComment.Text, Convert.ToInt32(Session["StaffID"]));
            InvoiceDB.UpdateIsRefund(invoice.InvoiceID, true);


            // close this window
            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.returnValue=false;self.close();</script>");
        }
        else
        {
            HideTableAndSetErrorMessage("", "Invalid URL Parameters");
        }
    }