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 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;
        private Label label7;
        private Label label4;
		/// <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
			//
		}
        Form1 f1 = new Form1();
		/// <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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(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.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.label7 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
            this.panel1.Controls.Add(this.label7);
            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.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(331, 238);
            this.panel1.TabIndex = 0;
            // 
            // textBox6
            // 
            this.textBox6.Location = new System.Drawing.Point(335, 218);
            this.textBox6.Name = "textBox6";
            this.textBox6.Size = new System.Drawing.Size(10, 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, ((byte)(0)));
            this.textBox5.Location = new System.Drawing.Point(12, 140);
            this.textBox5.Name = "textBox5";
            this.textBox5.ReadOnly = true;
            this.textBox5.Size = new System.Drawing.Size(100, 21);
            this.textBox5.TabIndex = 13;
            // 
            // label6
            // 
            this.label6.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label6.Image = ((System.Drawing.Image)(resources.GetObject("label6.Image")));
            this.label6.Location = new System.Drawing.Point(118, 140);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(201, 23);
            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, ((byte)(0)));
            this.button2.Location = new System.Drawing.Point(148, 204);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            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, ((byte)(0)));
            this.button1.Location = new System.Drawing.Point(67, 204);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            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, ((byte)(0)));
            this.label5.Image = ((System.Drawing.Image)(resources.GetObject("label5.Image")));
            this.label5.Location = new System.Drawing.Point(118, 172);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(201, 23);
            this.label5.TabIndex = 9;
            this.label5.Text = "کد مشتری";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label5.Visible = false;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Image = ((System.Drawing.Image)(resources.GetObject("label2.Image")));
            this.label2.Location = new System.Drawing.Point(118, 44);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(201, 23);
            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, ((byte)(0)));
            this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image")));
            this.label1.Location = new System.Drawing.Point(118, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(201, 23);
            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, ((byte)(0)));
            this.textBox4.Location = new System.Drawing.Point(12, 172);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(100, 21);
            this.textBox4.TabIndex = 4;
            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, ((byte)(0)));
            this.comboBox1.Items.AddRange(new object[] {
            "نقد",
            "نسیه"});
            this.comboBox1.Location = new System.Drawing.Point(12, 108);
            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, ((byte)(0)));
            this.textBox3.Location = new System.Drawing.Point(12, 76);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 21);
            this.textBox3.TabIndex = 2;
            // 
            // textBox2
            // 
            this.textBox2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox2.Location = new System.Drawing.Point(12, 44);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 21);
            this.textBox2.TabIndex = 1;
            this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
            // 
            // textBox1
            // 
            this.textBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // label7
            // 
            this.label7.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label7.Image = ((System.Drawing.Image)(resources.GetObject("label7.Image")));
            this.label7.Location = new System.Drawing.Point(116, 74);
            this.label7.Name = "label7";
            this.label7.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.label7.Size = new System.Drawing.Size(214, 23);
            this.label7.TabIndex = 15;
            this.label7.Text = "تاریخ فروش(روز/ماه/سال)";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label4
            // 
            this.label4.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Image = ((System.Drawing.Image)(resources.GetObject("label4.Image")));
            this.label4.Location = new System.Drawing.Point(118, 108);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(201, 23);
            this.label4.TabIndex = 8;
            this.label4.Text = "نوع فروش";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // Form3
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.ClientSize = new System.Drawing.Size(332, 236);
            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.panel1.PerformLayout();
            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)
		{
			
			
			SqlDataAdapter adp1=new SqlDataAdapter("select codekala from kala",f1.con);
            SqlDataAdapter adp2 = new SqlDataAdapter("select customerid from customer", f1.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, f1.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,"customer1");
			adp3.Fill(dst3,"number1");
			//adp4.Fill(dst4,"etebar");
			dv1=dst1.Tables["kala1"].DefaultView;
			dv2=dst2.Tables["customer1"].DefaultView;
			dv3=dst3.Tables["number1"].DefaultView;
			//dv4=dst4.Tables["etebar"].DefaultView;
			dv2.Sort="customerid";
			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)
					{
                        if ((textBox1.Text != "") && (textBox2.Text != "") && (textBox3.Text != "")  && (textBox5.Text != ""))
                        {
                            f1.con.Open();
                            SqlCommand com = new SqlCommand("insert into sell (codekala,numberkala,datesell,typesell,customerid,allsum) values (@a1,@a2,@a3,@a4,@a5,@a6)", f1.con);
                            com.Parameters.AddWithValue("@a1", textBox1.Text);
                            com.Parameters.AddWithValue("@a2", Convert.ToInt32(textBox2.Text));
                            com.Parameters.AddWithValue("@a3", Convert.ToDateTime(textBox3.Text));
                            com.Parameters.AddWithValue("@a4", Convert.ToString(comboBox1.SelectedItem));
                            textBox4.Text = "0";
                            com.Parameters.AddWithValue("@a5", textBox4.Text);
                            com.Parameters.AddWithValue("@a6", (Convert.ToInt32(textBox5.Text) * Convert.ToInt32(textBox2.Text)));
                            com.ExecuteNonQuery();
                            SqlCommand com1 = new SqlCommand("update kala set number=number-@a1 where codekala=@a2", f1.con);
                            com1.Parameters.AddWithValue("@a1", Convert.ToInt32(textBox2.Text));
                            com1.Parameters.AddWithValue("@a2", textBox1.Text);
                            com1.ExecuteNonQuery();
                            f1.con.Close();
                            textBox1.Text = "";
                            textBox2.Text = "";
                            textBox3.Text = "";
                            textBox4.Text = "";
                            textBox5.Text = "";
                            DialogResult msg3;
                            msg3 = MessageBox.Show(".فروش به درستی در بانک ثبت گردید", "ثبت موفق", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            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")
							{
                                f1.con.Open();
                                SqlCommand com = new SqlCommand("insert into sell (codekala,numberkala,datesell,typesell,customerid,allsum) values (@a1,@a2,@a3,@a4,@a5,@a6)", f1.con);
								com.Parameters.AddWithValue("@a1",textBox1.Text);
								com.Parameters.AddWithValue("@a2",Convert.ToInt32(textBox2.Text));
								com.Parameters.AddWithValue("@a3",Convert.ToDateTime(textBox3.Text));
								com.Parameters.AddWithValue("@a4",Convert.ToString(comboBox1.SelectedItem));
								com.Parameters.AddWithValue("@a5",textBox4.Text);
								com.Parameters.AddWithValue("@a6",(Convert.ToInt32(textBox5.Text)*Convert.ToInt32(textBox2.Text)));
								com.ExecuteNonQuery();
                                SqlCommand com1 = new SqlCommand("update kala set number=number-@a1 where codekala=@a2", f1.con);
								com1.Parameters.AddWithValue("@a1",Convert.ToInt32(textBox2.Text));
								com1.Parameters.AddWithValue("@a2",textBox1.Text);
								com1.ExecuteNonQuery();
                                SqlCommand com2 = new SqlCommand("update customer set regular=regular-@a1 where customerid=@a2", f1.con);
								com2.Parameters.AddWithValue("@a1",Convert.ToInt32(textBox5.Text));
								com2.Parameters.AddWithValue("@a2",textBox4.Text);
								com2.ExecuteNonQuery();
                                f1.con.Close();
                                textBox1.Text = "";
                                textBox2.Text = "";
                                textBox3.Text = "";
                                textBox4.Text = "";
                                textBox5.Text = "";
								DialogResult msg5;
								msg5=MessageBox.Show(".فروش به درستی در بانک ثبت گردید","ثبت موفق",MessageBoxButtons.OK,MessageBoxIcon.Error);
							}
							else
							{
								DialogResult msg7;
								msg7=MessageBox.Show(".مشتری اعتبار کافی ندارد","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
							}
						}
					}
				}
			}
		
		}

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{
			
			f1.con.Open();
            SqlCommand com2 = new SqlCommand("select * from kala where codekala='" + textBox1.Text + "'", f1.con);
			SqlDataReader dr;
			dr=com2.ExecuteReader();
            
            
			if (dr.Read())
			{
				textBox5.Text=Convert.ToString(dr["pricesell"]);
			}
			else
			{
				textBox5.Text="0";
			}
            f1.con.Close();			
		}

		private void textBox4_TextChanged(object sender, System.EventArgs e)
		{
			
			
            SqlDataAdapter adp = new SqlDataAdapter("select customerid from customer where regular >='" + textBox5.Text + "'", f1.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";
		}

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
        }		
	}
}
