using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WareHouse.dtsAnbarJadidTableAdapters;

namespace WareHouse.frmParvandeh
{
    public partial class khariDarishodeh : Form
    {
        public void myDbcon101()
        {
            string sql;
            sql = @"SELECT     TOP (100) PERCENT Purchased.PurAssetID AS [کد کالا], Sefareshat.DateShOfSefaresh1 AS [تاریخ سفارش], Purchased.SefarshID1 AS [کد سفارش], 
                      Purchased.PurchShDate AS [تاریخ خرید], Sefareshat.Tedad AS [تعداد درخواست], Categ.Categ AS [طبقه بندی], Asset.AssetName AS [عنوان کالا], 
                      Units.UnitName AS [نام واحد], AssetUnits.AssetUnit AS [واحد شمارش], Purchased.WareHouse AS [کد انبار], WareHouse.WareHouseName AS [نام انبار], 
                      Purchased.TahvilID AS [وضعیت تحویل] FROM         Purchased INNER JOIN     Sefareshat ON Purchased.SefarshID1 = Sefareshat.SefareshID INNER JOIN
                      WareHouse ON Purchased.WareHouse = WareHouse.WarHouseID INNER JOIN        Units ON Sefareshat.UnitID = Units.UnitID INNER JOIN
                      Asset ON Sefareshat.AssetID = Asset.AssetID INNER JOIN     Categ ON Asset.Categu = Categ.CatID INNER JOIN
                      AssetUnits ON Asset.AssetUnit = AssetUnits.AssetUnitID  ORDER BY PurchShDate DESC";
            dbconnClass1 con101 = new dbconnClass1();
            con101.dbCon1(sql);
            dbgPurch.DataSource = con101.dt;
            try
            {
                dbgPurch.Columns[4].Width = 150;
            }
            catch { }
        }
        public khariDarishodeh()
        {
            InitializeComponent();
        }

        private void khariDarishodeh_Load(object sender, EventArgs e)
        {
            GrdFill();
        }

        private void GrdFill()
        {
            string sql;
            myDbcon101();

            /////////////////////////////////////////////////////////////////////////////////

            sql = "SELECT WarHouseID, WareHouseName FROM WareHouse";
            dbconnClass1 cmBo102 = new dbconnClass1();
            cmBo102.dbCon1(sql);
            cmbPlace1.DataSource = cmBo102.dt;
            cmbPlace1.ValueMember = "WarHouseID";
            cmbPlace1.DisplayMember = "WareHouseName";
            cmBo102.dt.Rows.Add(new Object[] { 0 , "لطفا انتخاب فرمائید . . . "});
            cmbPlace1.SelectedValue = 0;

            /////////////////////////////////////////////////////////////////////////////////

            sql = "SELECT     SefareshID, Purch FROM   Sefareshat WHERE  (Purch = 0)";
            dbconnClass1 cmBo103 = new dbconnClass1();
            cmBo103.dbCon1(sql);
            cmbSefareshnumber.DataSource = cmBo103.dt;
            cmbSefareshnumber.ValueMember = "SefareshID";
            cmbSefareshnumber.DisplayMember = "SefareshID";

            /////////////////////////////////////////////////////////////////////////////////
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtKalacode.Text == "" || MTxt_Kharid.Text == "" || cmbSefareshnumber.Text == "" || cmbPlace1.Text == "")
            {
                MessageBox.Show("ورود تمامي مقادر الزامي مي باشد");
                return;
            }
            try
            {
                DialogResult dr = MessageBox.Show("آیا نسبت به عمل ثبت نهایی اطمینان دارید", "اخطار ثبت سند", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    string sql = "insert into Purchased (PurAssetID, PurchShDate, SefarshID1, WareHouse ,TahvilID)values('" + txtKalacode.Text + "' , '" + MTxt_Kharid.Text + "' , '" + cmbSefareshnumber.SelectedValue + "' , '" + cmbPlace1.SelectedValue + "' , '" + 0 + "')";
                    dbconnClass1 dbInsert1 = new dbconnClass1();
                    dbInsert1.dbIns1(sql);
                    SefareshatTableAdapter tbl = new SefareshatTableAdapter();
                    tbl.UpdatePurchBit(true, int.Parse(cmbSefareshnumber.SelectedValue.ToString()));
                    groupBox1.Enabled = false;
                    Btn_GetSefareshInfo.Enabled = true;
                    cmbSefareshnumber.Enabled = true;
                    myDbcon101();
                    GrdFill();
                }
            }
            catch
            {
                MessageBox.Show("لطفا تمامی مقادیر را انتخاب فرمائید.");
            }

        }

        private void Btn_GetSefareshInfo_Click(object sender, EventArgs e)
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut("SELECT  SefareshID, UnitID, AssetID, Tedad, DateSef, Purch, DateShOfSefaresh, DateShOfSefaresh1 FROM   Sefareshat WHERE     (SefareshID = '" + int.Parse(cmbSefareshnumber.SelectedValue.ToString()) + "')");

            txtKalacode.Text = dt.Rows[0][2].ToString();
            MTxt_SefareshDate.Text = dt.Rows[0][7].ToString();
            Txt_TedadeDarkhast.Text = dt.Rows[0][3].ToString();
            Txt_AssetName.Text = GetAssetInfo(int.Parse(dt.Rows[0][2].ToString()));
            Txt_UnitName.Text = GetUnitInfo(int.Parse(dt.Rows[0][1].ToString()));
            groupBox1.Enabled = true;
            Btn_GetSefareshInfo.Enabled = false;
            cmbSefareshnumber.Enabled = false;
            MTxt_Kharid.Text = db.dtShamsi();
        }

        private String GetAssetInfo(int int_AssetID)
        {
            String Str_AssetName = String.Empty;
            Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
            Asset tblAsset = db.Assets.Single(n => n.AssetID == int_AssetID);

            Str_AssetName = tblAsset.AssetName;
            return Str_AssetName;
        }

        private String GetUnitInfo(int int_UnitID)
        {
            String Str_UnitName = String.Empty;
            Lsc_AnbarDataContext db = new Lsc_AnbarDataContext();
            Unit tblUnit = db.Units.Single(n => n.UnitID == int_UnitID);

            Str_UnitName = tblUnit.UnitName;
            return Str_UnitName;
        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
            Btn_GetSefareshInfo.Enabled = true;
            cmbSefareshnumber.Enabled = true;
        }
    }
}