using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for Form3.
	/// </summary>
	public class Form3 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox6;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form3()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form3));
			this.panel1 = new System.Windows.Forms.Panel();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
			this.panel1.Controls.Add(this.textBox6);
			this.panel1.Controls.Add(this.textBox5);
			this.panel1.Controls.Add(this.label6);
			this.panel1.Controls.Add(this.button2);
			this.panel1.Controls.Add(this.button1);
			this.panel1.Controls.Add(this.label5);
			this.panel1.Controls.Add(this.label4);
			this.panel1.Controls.Add(this.label3);
			this.panel1.Controls.Add(this.label2);
			this.panel1.Controls.Add(this.label1);
			this.panel1.Controls.Add(this.textBox4);
			this.panel1.Controls.Add(this.comboBox1);
			this.panel1.Controls.Add(this.textBox3);
			this.panel1.Controls.Add(this.textBox2);
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(280, 240);
			this.panel1.TabIndex = 0;
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(264, 216);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(8, 20);
			this.textBox6.TabIndex = 14;
			this.textBox6.Text = "textBox6";
			this.textBox6.Visible = false;
			// 
			// textBox5
			// 
			this.textBox5.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox5.Location = new System.Drawing.Point(24, 144);
			this.textBox5.Name = "textBox5";
			this.textBox5.ReadOnly = true;
			this.textBox5.TabIndex = 13;
			this.textBox5.Text = "";
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Image = ((System.Drawing.Image)(resources.GetObject("label6.Image")));
			this.label6.Location = new System.Drawing.Point(160, 144);
			this.label6.Name = "label6";
			this.label6.TabIndex = 12;
			this.label6.Text = "جمع کل";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(160, 208);
			this.button2.Name = "button2";
			this.button2.TabIndex = 11;
			this.button2.Text = "خروج";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(32, 208);
			this.button1.Name = "button1";
			this.button1.TabIndex = 10;
			this.button1.Text = "درج";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Image = ((System.Drawing.Image)(resources.GetObject("label5.Image")));
			this.label5.Location = new System.Drawing.Point(160, 176);
			this.label5.Name = "label5";
			this.label5.TabIndex = 9;
			this.label5.Text = "کد مشتری";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.label5.Visible = false;
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Image = ((System.Drawing.Image)(resources.GetObject("label4.Image")));
			this.label4.Location = new System.Drawing.Point(160, 112);
			this.label4.Name = "label4";
			this.label4.TabIndex = 8;
			this.label4.Text = "نوع فروش";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Image = ((System.Drawing.Image)(resources.GetObject("label3.Image")));
			this.label3.Location = new System.Drawing.Point(160, 80);
			this.label3.Name = "label3";
			this.label3.TabIndex = 7;
			this.label3.Text = "تاریخ فروش";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Image = ((System.Drawing.Image)(resources.GetObject("label2.Image")));
			this.label2.Location = new System.Drawing.Point(160, 48);
			this.label2.Name = "label2";
			this.label2.TabIndex = 6;
			this.label2.Text = "تعداد کالا";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image")));
			this.label1.Location = new System.Drawing.Point(160, 16);
			this.label1.Name = "label1";
			this.label1.TabIndex = 5;
			this.label1.Text = "کد کالا";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// textBox4
			// 
			this.textBox4.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox4.Location = new System.Drawing.Point(24, 176);
			this.textBox4.Name = "textBox4";
			this.textBox4.TabIndex = 4;
			this.textBox4.Text = "";
			this.textBox4.Visible = false;
			this.textBox4.TextChanged += new System.EventHandler(this.textBox4_TextChanged);
			// 
			// comboBox1
			// 
			this.comboBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.comboBox1.Items.AddRange(new object[] {
														   "نقد",
														   "نسیه"});
			this.comboBox1.Location = new System.Drawing.Point(24, 112);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(96, 21);
			this.comboBox1.TabIndex = 3;
			this.comboBox1.Text = "نقد";
			this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
			// 
			// textBox3
			// 
			this.textBox3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox3.Location = new System.Drawing.Point(24, 80);
			this.textBox3.Name = "textBox3";
			this.textBox3.TabIndex = 2;
			this.textBox3.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox2.Location = new System.Drawing.Point(24, 48);
			this.textBox2.Name = "textBox2";
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox1.Location = new System.Drawing.Point(24, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
			// 
			// Form3
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(272, 238);
			this.Controls.Add(this.panel1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "Form3";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = " ثبت فروش";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void button2_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (comboBox1.SelectedIndex==1)
			{
				textBox4.Visible=true;
				label5.Visible=true;
			}
			else
			{
			textBox4.Visible=false;
			label5.Visible=false;
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			SqlConnection con=new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=abzar;Data Source=MAHDI-59CA33515\\WINCC");
			con.Open();
			SqlDataAdapter adp1=new SqlDataAdapter("select codekala from kala",con);
			SqlDataAdapter adp2=new SqlDataAdapter("select workerid from worker",con);
			string s;
			s="select codekala,number from kala where number >= '"+textBox2.Text+"'";
			//s1="select customerid,regular from customer where regular >= '";
			SqlDataAdapter adp3=new SqlDataAdapter(s,con);
			//SqlDataAdapter adp4=new SqlDataAdapter(s1,con);
			DataSet dst1=new DataSet();
			DataSet dst2=new DataSet();
			DataSet dst3=new DataSet();
			//DataSet dst4=new DataSet();
            DataView dv1=new DataView();
			DataView dv2=new DataView();
			DataView dv3=new DataView();
			//DataView dv4=new DataView();
			adp1.Fill(dst1,"kala1");
			adp2.Fill(dst2,"worker1");
			adp3.Fill(dst3,"number1");
			//adp4.Fill(dst4,"etebar");
			dv1=dst1.Tables["kala1"].DefaultView;
			dv2=dst2.Tables["worker1"].DefaultView;
			dv3=dst3.Tables["number1"].DefaultView;
			//dv4=dst4.Tables["etebar"].DefaultView;
			dv2.Sort="workerid";
			dv1.Sort="codekala";
		    dv3.Sort="codekala";
			// در صورت نقد بودن
			if (dv1.Find(textBox1.Text)<0)
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".کد کالای وارد شده وجود ندارد","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
			else
			{
				if(dv3.Find(textBox1.Text)<0)
				{
					DialogResult msg2;
					msg2=MessageBox.Show(".تعداد کالای درخواستی بیشتر از کالای موجود در انبار می باشد","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
				}
				else
				{
					if (textBox4.Visible==false)
					{
						SqlCommand com=new SqlCommand("insert into sell (codekala,numberkala,datesell,typesell,customerid,allsum) values (@a1,@a2,@a3,@a4,@a5,@a6)",con);
						com.Parameters.Add("@a1",textBox1.Text);
						com.Parameters.Add("@a2",Convert.ToInt32(textBox2.Text));
						com.Parameters.Add("@a3",Convert.ToDateTime(textBox3.Text));
						com.Parameters.Add("@a4",Convert.ToString(comboBox1.SelectedItem));
						textBox4.Text="0";
						com.Parameters.Add("@a5",textBox4.Text);
						com.Parameters.Add("@a6",(Convert.ToInt32(textBox5.Text)*Convert.ToInt32(textBox2.Text)));
						com.ExecuteNonQuery();
						SqlCommand com1=new SqlCommand("update kala set number=number-@a1 where codekala=@a2",con);
						com1.Parameters.Add("@a1",Convert.ToInt32(textBox2.Text));
						com1.Parameters.Add("@a2",textBox1.Text);
						com1.ExecuteNonQuery();
						DialogResult msg3;
						msg3=MessageBox.Show(".فروش به درستی در بانک ثبت گردید","ثبت موفق",MessageBoxButtons.OK,MessageBoxIcon.Error);
					}
					else
					{
						if (dv2.Find(textBox4.Text)<0)
						{
							DialogResult msg4;
							msg4=MessageBox.Show(".شماره کارمند وارد شده اشتباه می باشد","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
						}
						else
						{
							if (textBox6.Text=="1")
							{
								SqlCommand com=new SqlCommand("insert into sell (codekala,numberkala,datesell,typesell,customerid,allsum) values (@a1,@a2,@a3,@a4,@a5,@a6)",con);
								com.Parameters.Add("@a1",textBox1.Text);
								com.Parameters.Add("@a2",Convert.ToInt32(textBox2.Text));
								com.Parameters.Add("@a3",Convert.ToDateTime(textBox3.Text));
								com.Parameters.Add("@a4",Convert.ToString(comboBox1.SelectedItem));
								com.Parameters.Add("@a5",textBox4.Text);
								com.Parameters.Add("@a6",(Convert.ToInt32(textBox5.Text)*Convert.ToInt32(textBox2.Text)));
								com.ExecuteNonQuery();
								SqlCommand com1=new SqlCommand("update kala set number=number-@a1 where codekala=@a2",con);
								com1.Parameters.Add("@a1",Convert.ToInt32(textBox2.Text));
								com1.Parameters.Add("@a2",textBox1.Text);
								com1.ExecuteNonQuery();
								SqlCommand com2=new SqlCommand("update customer set regular=regular-@a1,pricebarrow=pricebarrow+@a1 where customerid=@a2",con);
								com2.Parameters.Add("@a1",Convert.ToInt32(textBox5.Text));
								com2.Parameters.Add("@a2",textBox4.Text);
								com2.ExecuteNonQuery();
								DialogResult msg5;
								msg5=MessageBox.Show(".فروش به درستی در بانک ثبت گردید","ثبت موفق",MessageBoxButtons.OK,MessageBoxIcon.Error);
							}
							else
							{
								DialogResult msg6;
								msg6=MessageBox.Show(".مشتری دیگر اعتباری ندارد","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
							}
						}
					}
				}
			}
		con.Close();
		}

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{
			SqlConnection con=new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=abzar;Data Source=MAHDI-59CA33515\\WINCC");
			con.Open();
			SqlCommand com2=new SqlCommand("select * from kala where codekala='"+textBox1.Text+"'",con);
			SqlDataReader dr;
			dr=com2.ExecuteReader();
			if (dr.Read())
			{
				textBox5.Text=Convert.ToString(dr["pricesell"]);
			}
			else
			{
				textBox5.Text="0";
			}
			con.Close();			
		}

		private void textBox4_TextChanged(object sender, System.EventArgs e)
		{
			SqlConnection con=new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=abzar;Data Source=MAHDI-59CA33515\\WINCC");
			con.Open();
			SqlDataAdapter adp=new SqlDataAdapter("select customerid from customer where regular >='"+textBox5.Text+"'",con);
     		DataSet dst=new DataSet();
            DataView dv=new DataView();
			adp.Fill(dst,"t1");
            dv=dst.Tables["t1"].DefaultView;
		    dv.Sort="customerid";
			if (dv.Find(textBox4.Text)>=0)
        	textBox6.Text="1";
            else
            textBox6.Text="0";
		}		
	}
}
