using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Web.Mail;
public partial class FrmCreateInvoice : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
//SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Freeda"].ConnectionString);
int TotalBoxNo = 0;
string storedatedc;
string bx;
double total = 0;
// ON PAGE LOAD
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialGridBind();
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
DataTable dtdata = (DataTable)Session["Data"];
TextBox4.Text = dtdata.Rows[0]["IV"].ToString();
Session["DCUC"] = TextBox4.Text;
TextBox5.Text = dtdata.Rows[0]["Date"].ToString();
TextBox6.Text = dtdata.Rows[0]["Client"].ToString();
//txtLRNO.Text = dtdata.Rows[0]["LrNo"].ToString();
txtDC.Text = dtdata.Rows[0]["DC"].ToString();
txtDCDate.Text = dtdata.Rows[0]["DCDate"].ToString();
//txtdcdate1.Text = dtdata.Rows[0]["LRdate"].ToString();
btnpreview.Visible = false;
string lr = "Select LRno,LRdate from Deliverychalan where Dcno='" + txtDC.Text + "'";
SqlDataAdapter dalr = new SqlDataAdapter(lr, conn);
DataSet dslr = new DataSet();
DataTable dtlr = new DataTable();
dalr.Fill(dslr, "lr");
dtlr = dslr.Tables[0];
string lrno = Convert.ToString(dtlr.Rows[0]["LRno"]);
string lrdt = Convert.ToString(dtlr.Rows[0]["LRdate"]);
txtLRNO.Text = lrno;
txtdcdate1.Text = lrdt;
lbltotal.Text="0.00";
txtcst.Text = "0";
txtvat.Text = "0";
lblNettotal.Text = "0.00";
btnnet.Visible = false;
}
btnsubmit.Visible = false;
Label14.Visible = false;
Label21.Visible = false;
}
protected void SetInitialGridBind()
{
double totamt = 0;
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
DataTable dtdc = (DataTable)Session["Invoice1"];
if (dtdc.Rows.Count == 0)
{
Label14.Visible = true;
Label14.Text = "No Records Present";
}
else
{
string d1 = Convert.ToString(dtdc.Rows[0][1]);
string d2 = Convert.ToString(dtdc.Rows[0][3]);
for (int i = 0; i < dtdc.Rows.Count; i++)
{
string Amt = Convert.ToString(dtdc.Rows[i][7]);
totamt = totamt + Convert.ToDouble (Amt);
double tot = Math.Round(totamt, 2);
lbltotal.Text = Convert.ToString(tot);
string t = Convert.ToString(totamt);
if (!t.Contains('.'))
{
lbltotal.Text = t + ".00";
}
}
Session["SOUC"] = d2;
Session["PidUC"] = d1;
Gridview1.DataSource = dtdc;
Gridview1.DataBind();
}
}
protected void OnText_ChangedRate(object sender, EventArgs e)
{
Calculation();
int rowIndex = Gridview1.Rows.Count - 1;
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
SetFocus(box5);
}
protected void OnText_ChangedPer(object sender, EventArgs e)
{
Calculation();
}
public void Calculation()
{
try
{
int qty, per;
Double rate;
Double amount;
int rowIndex = 0;
for (int i = 1; i <= Gridview1.Rows.Count; i++)
{
//extract the TextBox values
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
qty = Convert.ToInt32(box3.Text);
if (box4.Text == "")
{
rate = 0;
}
else
{
rate = Convert.ToDouble(box4.Text);
}
if (box5.Text == "")
{
per = 0;
}
else
{
per = Convert.ToInt32(box5.Text);
}
if (per == 0)
{
amount = 0;
}
else
{
Double Cal = (qty / per);
amount = Convert.ToDouble(Cal * rate);
}
//TextBox lblamount = new TextBox();
TextBox lblamount = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtAmount");
//lblamount.Text = amount.ToString();
double tot = Math.Round(amount, 2);
lblamount.Text = tot.ToString();
string a = Convert.ToString(amount);
if (!a.Contains('.'))
{
lblamount.Text = a + ".00";
}
rowIndex++;
}
}
catch (Exception exx)
{
Label14.Visible = true;
Label14.Text = exx.Message;
}
}
protected void calctotal_Click(object sender, EventArgs e)
{
btnnet.Visible = true;
//calctotalamt = "1";
double totpc = 0;
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
GridViewRow row = Gridview1.Rows[i];
TextBox lbltotpc = (TextBox)row.Cells[5].FindControl("txtAmount");
totpc = totpc + Convert.ToDouble(lbltotpc.Text);
//Label lbltotpc = (Label)GV.Rows[i].Cells[4].FindControl("lblTotal");
lbltotal.Text = totpc.ToString();
//TotalBoxNo = TotalBoxNo + Convert.ToInt32(GVbox1.Text);
}
}
protected void Nettotal_Click(object sender, EventArgs e)
{
btnsubmit.Visible = true;
double vatcal, cstcal;
double calamt = 0;
Double price = Convert.ToDouble(lbltotal.Text);
//Double vat = Convert.ToDouble(txtvat.Text);
//Double cst = Convert.ToDouble(txtcst.Text);
Double nettotal = 0;
if (txtvat.Text == "" && txtcst.Text == "")
{
vatcal = 0;
cstcal = 0;
lblvatcal.Text = Convert.ToString(vatcal);
lblcstcal.Text = Convert.ToString(cstcal);
nettotal = Convert.ToDouble(lbltotal.Text);
}
else if (txtvat.Text == "" && txtcst.Text != "")
{
cstcal = (price * (Convert.ToDouble(txtcst.Text) / 100));
vatcal = 0;
lblvatcal.Text = Convert.ToString(vatcal);
lblcstcal.Text = Convert.ToString(cstcal);
// nettotal = lbltotal.Text;
nettotal = Convert.ToDouble(lbltotal.Text) + vatcal + cstcal;
}
else if (txtvat.Text != "" && txtcst.Text == "")
{
vatcal = (price * (Convert.ToDouble(txtvat.Text) / 100));
cstcal = 0;
lblvatcal.Text = Convert.ToString(vatcal);
lblcstcal.Text = Convert.ToString(cstcal);
// nettotal = lbltotal.Text;
nettotal = Convert.ToDouble(lbltotal.Text) + vatcal + cstcal;
}
else if (txtvat.Text != "" && txtcst.Text != "")
{
vatcal = (price * (Convert.ToDouble(txtvat.Text) / 100));
cstcal = (price * (Convert.ToDouble(txtcst.Text) / 100));
lblvatcal.Text = Convert.ToString(vatcal);
lblcstcal.Text = Convert.ToString(cstcal);
// nettotal = lbltotal.Text;
nettotal = Convert.ToDouble(lbltotal.Text) + vatcal + cstcal;
}
double ash = Math.Round(nettotal, 2);
long test = Convert.ToInt64(ash);
string b = Convert.ToString(ash);
if (!b.Contains('.'))
{
lblNettotal.Text = b + ".00";
}
else
{
lblNettotal.Text = Convert.ToString(ash);
}
string NumCalc = lblNettotal.Text;
if (!NumCalc.Contains('.'))
{
NumCalc = NumCalc + ".00";
}
string[] dtSplitCalc = NumCalc.Split('.');
string NumCalc1 = dtSplitCalc[0];
string NumCalc2 = dtSplitCalc[1];
if (Convert.ToInt32(NumCalc2) == 00)
{
txtRsinwords.Text = ConvertNumberToWord(Convert.ToInt32(NumCalc1)) + " Rupees Only ";
}
else
{
txtRsinwords.Text = ConvertNumberToWord(Convert.ToInt32(NumCalc1)) + " Rupees " + ConvertNumberToWord(Convert.ToInt32(NumCalc2)) + " Paise" + " Only ";
}
}
private string ConvertNumberToWord(long nNumber)
{
long CurrentNumber = nNumber;
string sReturn = "";
if (CurrentNumber >= 10000000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber / 10000000, "Crores");
CurrentNumber = CurrentNumber % 10000000;
}
if (CurrentNumber >= 100000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber / 100000, "Lakhs");
CurrentNumber = CurrentNumber % 100000;
}
if (CurrentNumber >= 1000)
{
sReturn = sReturn + " " + GetWord(CurrentNumber / 1000, "Thousand");
CurrentNumber = CurrentNumber % 1000;
}
if (CurrentNumber >= 100)
{
sReturn = sReturn + " " + GetWord(CurrentNumber / 100, "Hundred");
CurrentNumber = CurrentNumber % 100;
}
if (CurrentNumber >= 20)
{
sReturn = sReturn + " " + GetWord(CurrentNumber, "");
CurrentNumber = CurrentNumber % 10;
}
else if (CurrentNumber > 0)
{
sReturn = sReturn + " " + GetWord(CurrentNumber, "");
CurrentNumber = 0;
}
return sReturn.Replace(" ", " ").Trim();
}
private string GetWord(long nNumber, string sPrefix)
{
long nCurrentNumber = nNumber;
string sReturn = "";
while (nCurrentNumber > 0)
{
if (nCurrentNumber > 100)
{
sReturn = sReturn + " " + GetWord(nCurrentNumber / 100, "Hundred");
nCurrentNumber = nCurrentNumber % 100;
}
else if (nCurrentNumber >= 20)
{
sReturn = sReturn + " " + GetTwentyWord(nCurrentNumber / 10);
nCurrentNumber = nCurrentNumber % 10;
}
else
{
sReturn = sReturn + " " + GetLessThanTwentyWord(nCurrentNumber);
nCurrentNumber = 0;
}
}
sReturn = sReturn + " " + sPrefix;
return sReturn;
}
private string GetTwentyWord(long nNumber)
{
string sReturn = "";
switch (nNumber)
{
case 2:
sReturn = "Twenty";
break;
case 3:
sReturn = "Thirty";
break;
case 4:
sReturn = "Forty";
break;
case 5:
sReturn = "Fifty";
break;
case 6:
sReturn = "Sixty";
break;
case 7:
sReturn = "Seventy";
break;
case 8:
sReturn = "Eighty";
break;
case 9:
sReturn = "Ninety";
break;
}
return sReturn;
}
private string GetLessThanTwentyWord(long nNumber)
{
string sReturn = "";
switch (nNumber)
{
case 1:
sReturn = "One";
break;
case 2:
sReturn = "Two";
break;
case 3:
sReturn = "Three";
break;
case 4:
sReturn = "Four";
break;
case 5:
sReturn = "Five";
break;
case 6:
sReturn = "Six";
break;
case 7:
sReturn = "Seven";
break;
case 8:
sReturn = "Eight";
break;
case 9:
sReturn = "Nine";
break;
case 10:
sReturn = "Ten";
break;
case 11:
sReturn = "Eleven";
break;
case 12:
sReturn = "Twelve";
break;
case 13:
sReturn = "Thirteen";
break;
case 14:
sReturn = "Forteen";
break;
case 15:
sReturn = "Fifteen";
break;
case 16:
sReturn = "Sixteen";
break;
case 17:
sReturn = "Seventeen";
break;
case 18:
sReturn = "Eighteen";
break;
case 19:
sReturn = "Nineteen";
break;
}
return sReturn;
}
//ON SUBMIT BUTTON
protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
int a = 0;
StringCollection sc = new StringCollection();
StringCollection sc1 = new StringCollection();
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
GridViewRow row = Gridview1.Rows[i];
// bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
//if (isChecked)
//{
Label lblpid = (Label)row.FindControl("Label1");
Label lblDescription = (Label)row.FindControl("Label2");
Label lblSO = (Label)row.FindControl("Label3");
Label lblDC = (Label)row.FindControl("Label5");
TextBox box1 = (TextBox)row.FindControl("TextBox2");
TextBox box2 = (TextBox)row.FindControl("TextBox3");
TextBox box3 = (TextBox)row.FindControl("TextBox4");
TextBox box4 = (TextBox)row.FindControl("txtAmount");
sc1.Add(lblpid.Text + "," + lblDescription.Text + "," + lblSO.Text + "," + lblDC.Text + "," + box1.Text + "," + box2.Text + "," + box3.Text + "," + box4.Text + "," + lbltotal.Text + "," + txtvat.Text + "," + lblNettotal.Text + "," + txtcst.Text );
//Label finalTotal = (Label)GV.FindControl("lblTotal");
if (txtvat.Text == "")
{
lblNettotal.Text = lbltotal.Text;
}
////TextBox lbltotal = (TextBox)GV.Rows[0].Cells[4].FindControl("lbltotal");
//sc.Add(lblpid.Text + "," + lblDescription.Text + "," + lblSO.Text + "," + box1.Text + "," + bx + "," + lbltotal.Text + "," + box2.Text + "," + BoxT.Text);
// }
//Call the method for executing inserts
//InsertRecords(sc);
//sc.Clear();
}
InsertRecords1(sc1);
sc1.Clear();
//string storedatedc5;
//if (txtdcdate1.Text != "")
//{
// string str5 = txtdcdate1.Text;
// string[] dtSplit5 = str5.Split('/');
// storedatedc5 = dtSplit5[1] + "/" + dtSplit5[0] + "/" + dtSplit5[2];
//}
//else
//{
// storedatedc5 = " ";
//}
conn.Open();
string insqry = "Insert into MstInvoice(IncNo,Date,ClientName,TotalAmt,VAT,NetTotal,Rsinwords,DC,DCDate,vatcal,LRdate,CST,cstcal,LRNO) values ('" + TextBox4.Text + "','" + txtdcdate1.Text + "','" + TextBox6.Text + "','" + lbltotal.Text + "','" + txtvat.Text + "' ,'" + lblNettotal.Text + "','" + txtRsinwords.Text + "','" + txtDC.Text + "','" + txtDCDate.Text + "','" + lblvatcal.Text + "','" + txtdcdate1.Text + "','" + txtcst.Text + "','" + lblcstcal.Text + "','" + txtLRNO.Text + "')";
SqlCommand cmd2 = new SqlCommand(insqry, conn);
cmd2.ExecuteNonQuery();
conn.Close();
btnsubmit.Visible = false;
btnpreview.Visible = true;
Label21.Visible = true;
conn.Close();
conn.Open();
string Status = "update Deliverychalan set IncCreat='Yes' where Dcno='" + txtDC.Text + "'";
SqlCommand cmdst = new SqlCommand(Status, conn);
cmdst.ExecuteNonQuery();
conn.Close();
Sendemail();
btnpreview.Attributes.Add("onclick", "javascript:OpenfSecondOrd('" + TextBox4.Text + "')");
//Response.Redirect("invoiceform.aspx?ID=" + TextBox4.Text);
}
catch (Exception exx)
{
Label14.Visible = true;
Label14.Text = exx.Message;
}
}
//TO INSERT RECORDS IN DATABASE
private void InsertRecords1(StringCollection sc1)
{
//StringBuilder sb = new StringBuilder(string.Empty);
StringBuilder sb1 = new StringBuilder(string.Empty);
string[] splitItems = null;
string[] splitItems1 = null;
//string str1 = TextBox5.Text;
//string[] dtSplit = str1.Split('-');
//storedatedc = dtSplit[1] + "-" + dtSplit[0] + "-" + dtSplit[2];
foreach (string item in sc1)
{
const string sqlStatement1 = "INSERT INTO Invoice(IncNo,Date,ClientName,ProductId,PDescription,SO,Dcno,Quantity,Rate,Per,Amount,TotalBox,VAT,NetTotal,CST) VALUES";
if (item.Contains(","))
{
splitItems1 = item.Split(",".ToCharArray());
sb1.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}'); ", sqlStatement1, TextBox4.Text, storedatedc, TextBox6.Text, splitItems1[0], splitItems1[1], splitItems1[2], splitItems1[3], splitItems1[4], splitItems1[5], splitItems1[6], splitItems1[7], splitItems1[8], splitItems1[9], splitItems1[10], splitItems1[11]);
}
}
conn.Open();
SqlCommand cmd1 = new SqlCommand(sb1.ToString(), conn);
cmd1.CommandType = CommandType.Text;
cmd1.ExecuteNonQuery();
conn.Close();
}
//protected void txtcst_TextChanged(object sender, EventArgs e)
//{
// Nettotal();
//}
protected void Sendemail()
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
StringBuilder sb = new StringBuilder();
sb.Append("
");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("Information");
sb.Append("");
sb.Append(" | ");
sb.Append("
");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append(" ");
sb.Append(" | ");
sb.Append(" ");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("Dear Ma'am");
sb.Append(" ");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append(" | ");
sb.Append(" ");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append(" | ");
sb.Append(" ");
sb.Append("");
sb.Append(" ");
sb.Append(" | ");
sb.Append(" ");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("Invoice Created for " + TextBox6.Text + " Dated on " + TextBox5.Text);
sb.Append(" ");
sb.Append("Having Invoice No(Inc No):" + TextBox4.Text);
sb.Append(" ");
sb.Append(" ");
sb.Append("");
sb.Append(" | ");
sb.Append(" ");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append(" ");
sb.Append("Thanks & Regards");
sb.Append(" ");
sb.Append(" Freeda Enterprises LTD.");
sb.Append("");
sb.Append("");
sb.Append(" ");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append(" | ");
sb.Append(" ");
sb.Append("");
sb.Append(" ");
sb.Append(" | ");
sb.Append("
");
sb.Append("");
sb.Append("
");
mail.To = "freeda@freedaenterprises.com";
mail.Bcc = "";
mail.From = "info@freedapress.com";
mail.Subject = "Generation Of Invoice";
mail.Body = sb.ToString();
mail.BodyFormat = MailFormat.Html;
mail.Priority = System.Web.Mail.MailPriority.High;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "info@freedapress.com");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456");
SmtpMail.SmtpServer = "mail.freedapress.com";
try
{
//Sending Mail.
SmtpMail.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}