using System;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RezerveBeliteHavaPeimaCS
{
    public partial class frmRezerveSale : Form
    {
        int int_RezerveSale = 0;
        int int_QuanTity = 0;
        Decimal Des_SaleCount = 0;
        Decimal Dec_ZarfyatSale;
        Decimal Dec_ZarfyatDef;
        Decimal Dec_Mandeh;
        public frmRezerveSale()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع به روز رسانی رزرو بلیط
        /// </summary>
        private void UDf_RefreshRezerveSale()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblRezerveTickets
                        orderby row.RezerveID descending
                        select new
                        {
                            row.RezerveID,
                            row.RezerveDate,
                            row.Str_OperatorNameSname,
                            row.Str_CustomerNameSnameTTTT,
                            row.Str_ServiceName,
                            row.QuanTity,
                            row.BianehPardakhti,
                            row.Mandeh,
                            row.Notes
                        };
            Dgv_RezerveSale.DataSource = Query;
        }
        /// <summary>
        /// تابع درج رزرو بلیط
        /// </summary>
        private void UDF_InsertRezerveSale()
        {
            try
            {
                LSC_TADataContext db = new LSC_TADataContext();
                tblRezerveTicket tblRezervsale = new tblRezerveTicket();
                tblRezervsale.BianehPardakhti = Convert.ToDouble(Txt_RezerveSaleBeyaneh.Text.Trim());
                tblRezervsale.Mandeh = Convert.ToDouble(Txt_RezerveSaleMandeh.Text.Trim());
                tblRezervsale.QuanTity = Convert.ToInt32(Txt_RezerveSaleCount.Text.Trim());
                tblRezervsale.RezerveDate = Txt_RezerveSaleDate.Text.Trim();
                tblRezervsale.Notes = Txt_RezerveSaleMolahezat.Text.Trim();
                tblRezervsale.StaffID = int.Parse(Cmb_RezerveSaleOperator.SelectedValue.ToString().Trim());
                tblRezervsale.RezerverID = int.Parse(Cmb_RezerveSaleKharidar.SelectedValue.ToString().Trim());
                tblRezervsale.ServiceID = int.Parse(Cmb_RezerveSaleService.SelectedValue.ToString().Trim());
                db.tblRezerveTickets.InsertOnSubmit(tblRezervsale);
                db.SubmitChanges();
                UDF_TakmilZarfyat();
                Txt_RezerveSaleRezerveId.Text = "";
                Txt_RezerveSaleMolahezat.Text = "";
                Txt_RezerveSaleMandeh.Text = "";
                Txt_RezerveSaleDate.Text = "";
                Txt_RezerveSaleCount.Text = "";
                Txt_RezerveSaleBeyaneh.Text = "";
                Cmb_RezerveSaleKharidar.Text = "";
                Cmb_RezerveSaleOperator.Text = "";
                Cmb_RezerveSaleService.Text = "";
            }
            catch
            {
                MessageBox.Show("برای این کاربر قبلا ثبت انجام شده ، از ویرایش استفاده کنید");
            }
        }
        /// <summary>
        /// تابع حذف رزرو بلیط
        /// </summary>
        private void UDF_DeleteRezerveTicket()
        {
            int int_Quant = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tblRezT = db.tblRezerveTickets.Single(n => n.RezerveID == int.Parse(Txt_RezerveSaleRezerveId.Text.Trim()));
            tblServicesDef tblDef = db.tblServicesDefs.Single(n => n.ServiceDescript == Txt_RezerveSaleService.Text);
            int_Quant = tblRezT.QuanTity;
            Dec_Mandeh = UDF_MandehService4();
            Dec_Mandeh = Dec_Mandeh + Decimal.Parse(int_Quant.ToString());
            if (Dec_Mandeh == tblDef.Quantity)
            {
                tblDef.ExpireBit = Boolean.Parse("true");
            }
            db.tblRezerveTickets.DeleteOnSubmit(tblRezT);
            db.SubmitChanges();
            UDf_RefreshRezerveSale();
            UDF_FillComService();
            Txt_RezerveSaleRezerveId.Text = "";
            Txt_RezerveSaleRezerveId.Text = "";
            Txt_RezerveSaleMolahezat.Text = "";
            Txt_RezerveSaleMandeh.Text = "";
            Txt_RezerveSaleDate.Text = "";
            Txt_RezerveSaleCount.Text = "";
            Txt_RezerveSaleBeyaneh.Text = "";
            Cmb_RezerveSaleKharidar.Text = "";
            Cmb_RezerveSaleOperator.Text = "";
            Cmb_RezerveSaleService.Text = "";
        }
        /// <summary>
        /// تابع مربوط به ویرایش رزرو بلیط
        /// </summary>
        private void UDF_UpdateRezerve()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tblRezerve = db.tblRezerveTickets.Single(n => n.RezerveID == int.Parse(Txt_RezerveSaleRezerveId.Text.Trim()));
            tblRezerve.QuanTity = int.Parse(Txt_RezerveSaleCount.Text.Trim());
            Des_SaleCount = Decimal.Parse(Txt_RezerveSaleCount.Text.Trim());
            tblRezerve.RezerveDate = Txt_RezerveSaleDate.Text.Trim();
            tblRezerve.RezerveID = int.Parse(Txt_RezerveSaleSearch.Text.Trim());
            tblRezerve.Notes = Txt_RezerveSaleMolahezat.Text.Trim();
            tblRezerve.StaffID = int.Parse(Cmb_RezerveSaleOperator.SelectedValue.ToString().Trim());
            tblRezerve.RezerverID = int.Parse(Cmb_RezerveSaleKharidar.SelectedValue.ToString().Trim());
            tblRezerve.ServiceID = int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString().Trim());
            tblRezerve.BianehPardakhti = int.Parse(Txt_RezerveSaleBeyaneh.Text.Trim());
            tblRezerve.Mandeh = int.Parse(Txt_RezerveSaleMandeh.Text.Trim());
            db.SubmitChanges();
            UDF_TakmilZarfyat1();
            Cmb_RezerveSaleKharidar.Enabled = false;
            Cmb_RezerveSaleOperator.Enabled = false;
            Cmb_RezerveSaleService.Enabled = false;
            Txt_RezerveSaleRezerveId.Text = "";
            Txt_RezerveSaleMolahezat.Text = "";
            Txt_RezerveSaleMandeh.Text = "";
            Txt_RezerveSaleDate.Text = "";
            Txt_RezerveSaleCount.Text = "";
            Cmb_RezerveSaleKharidar.Text = "";
            Cmb_RezerveSaleOperator.Text = "";
            Txt_RezerveSaleBeyaneh.Text = "";
            Cmb_RezerveSaleService.Text = "";
            UDf_RefreshRezerveSale();
        }
        /// <summary>
        /// تابع پر کردن کامو باکس اپراتور
        /// </summary>
        private void UDF_FillComOperator()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from Row in db.tblStaffsTables
                        orderby Row.StaffID descending
                        select new
                        {
                            Row.StaffID,
                            Row.NameSname
                        };
            Cmb_RezerveSaleOperator.DisplayMember = "NameSname";
            Cmb_RezerveSaleOperator.ValueMember = "StaffID";
            Cmb_RezerveSaleOperator.DataSource = Query;
        }
        /// <summary>
        /// تابع پر کردن کامو باکس مشتری
        /// </summary>
        private void UDF_FillComCustomer()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblCustumers
                        orderby row.CustomerID descending
                        select new
                        {
                            row.CustomerID,
                            row.NameSname
                        };
            Cmb_RezerveSaleKharidar.DisplayMember = "NameSname";
            Cmb_RezerveSaleKharidar.ValueMember = "CustomerID";
            Cmb_RezerveSaleKharidar.DataSource = Query;
        }
        /// <summary>
        ///  تابع پر کردن کامو باکس سرویس
        /// </summary>
        private void UDF_FillComService()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblServicesDefs
                        where row.ExpireBit == bool.Parse("true")
                        orderby row.ServiceID descending
                        select new
                        {
                            row.ServiceID,
                            row.ServiceDescript,
                            row.ServiceDate
                        };
            Cmb_RezerveSaleService.DisplayMember = "ServiceDescript";
            Cmb_RezerveSaleService.ValueMember = "ServiceID";
            Cmb_RezerveSaleService.DataSource = Query;
        }
        /// <summary>
        /// تابع پر کردن کاموباکس برای مشتری
        /// </summary>
        private void UDF_FillUpdateComService()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblServicesDefs
                        orderby row.ServiceID descending
                        select new
                        {
                            row.ServiceID,
                            row.ServiceDescript,
                            row.ServiceDate
                        };
            Cmb_RezerveSaleUpdateService.DisplayMember = "ServiceDescript";
            Cmb_RezerveSaleUpdateService.ValueMember = "ServiceID";
            Cmb_RezerveSaleUpdateService.DataSource = Query;
        }
        /// <summary>
        /// تابع برگرداندن کد اپراتور برای جستجو
        /// </summary>
        private int UDf_ReturnCodeOperator()
        {
            int int_Operator = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblStaffsTables
                        where row.NameSname == Txt_RezerveSaleOperator.Text
                        select new
                        {
                            row.StaffID
                        };
            foreach (var Sabt1 in Query)
            {
                int_Operator = int.Parse(Sabt1.StaffID.ToString());
            }
            return int_Operator;
        }
        /// <summary>
        /// تابع برگرداندن کد خریدار برای جستجو
        /// </summary>
        private int UDf_ReturnCodeKharidar()
        {
            int int_Kharidar = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblCustumers
                        where row.NameSname == Txt_RezerveSaleKharidar.Text
                        select new
                        {
                            row.CustomerID
                        };
            foreach (var Sabt1 in Query)
            {
                int_Kharidar = int.Parse(Sabt1.CustomerID.ToString());
            }
            return int_Kharidar;
        }
        /// <summary>
        ///  تابع برگرداندن کد سرویس برای جستجو
        /// </summary>
        private int UDf_ReturnCodeService()
        {
            int int_Service = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblServicesDefs
                        where row.ServiceDescript == Txt_RezerveSaleService.Text
                        select new
                        {
                            row.ServiceID
                        };
            foreach (var Sabt1 in Query)
            {
                int_Service = int.Parse(Sabt1.ServiceID.ToString());
            }
            return int_Service;
        }
        /// <summary>
        /// تابع مربوط به جستجوی پیشرفته رزرو بلیط
        /// </summary>
        private void UDf_SearchAdvancedRezerveSale()
        {
            int int_SearchFlg = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from Row in db.tblRezerveTickets
                        select new
                        {   
                            Row.ServiceID,
                            Row.RezerverID,
                            Row.StaffID,
                            Row.RezerveID,
                            Row.RezerveDate,
                            Row.Str_OperatorNameSname,
                            Row.Str_CustomerNameSnameTTTT,
                            Row.Str_ServiceName,
                            Row.QuanTity,
                            Row.BianehPardakhti,
                            Row.Mandeh,
                            Row.Notes
                        };
            try
            {
                if (Chk_RezerveSaleRow.Checked == true)
                {
                    var Query1 = from Row in Query
                                 where Row.RezerveID == int.Parse(Txt_RezerveSaleSearch1.Text.Trim())
                                 select new
                                 {
                                     Row.ServiceID,
                                     Row.RezerverID,
                                     Row.StaffID,
                                     Row.RezerveID,
                                     Row.RezerveDate,
                                     Row.Str_OperatorNameSname,
                                     Row.Str_CustomerNameSnameTTTT,
                                     Row.Str_ServiceName,
                                     Row.QuanTity,
                                     Row.BianehPardakhti,
                                     Row.Mandeh,
                                     Row.Notes
                                 };
                    Query = Query1;
                    int_SearchFlg = 1;
                }
                if (Chk_RezerveSaleOperator.Checked == true)
                {
                    int int_Operator = 0;
                    int_Operator = UDf_ReturnCodeOperator();
                    var Query2 = from Row in Query
                                 where Row.StaffID == int_Operator
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (Chk_RezerveSaleKharidar.Checked == true)
                {
                    int int_Kharidar = 0;
                    int_Kharidar = UDf_ReturnCodeKharidar();
                    var Query3 = from Row in Query
                                 where Row.RezerverID == int_Kharidar
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query3;
                    int_SearchFlg = 1;
                }
                if (Chk__RezerveSaleService.Checked == true)
                {
                    int int_Service = 0;
                    int_Service = UDf_ReturnCodeService();
                    var Query4 = from Row in Query
                                 where Row.ServiceID == int_Service
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query4;
                    int_SearchFlg = 1;
                }
                if (Chk_RezerveSaleDate.Checked == true)
                {
                    var Query5 = from Row in Query
                                 where SqlMethods.Like(Row.RezerveDate, "%" + Txt_RezerveSaleDate.Text.Trim() + "%")
                                 orderby Row.RezerveID descending
                                 select new
                                 {
                                     Row.ServiceID,
                            Row.RezerverID,
                                     Row.StaffID,
                                     Row.RezerveID,
                                     Row.RezerveDate,
                                     Row.Str_OperatorNameSname,
                                     Row.Str_CustomerNameSnameTTTT,
                                     Row.Str_ServiceName,
                                     Row.QuanTity,
                                     Row.BianehPardakhti,
                                     Row.Mandeh,
                                     Row.Notes
                                 };
                    Query = Query5;
                    int_SearchFlg = 1;
                }
                if (Chk_RezerveSaleCount.Checked == true)
                {
                    var Query6 = from Row in Query
                                 where Row.QuanTity == int.Parse(Txt_RezerveSaleCount.Text.Trim())
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query6;
                    int_SearchFlg = 1;
                }
                if (Chk_RezerveSaleBeyaneh.Checked == true)
                {
                    var Query7 = from Row in Query
                                 where Row.BianehPardakhti == int.Parse(Txt_RezerveSaleBeyaneh.Text.Trim())
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query7;
                    int_SearchFlg = 1;
                }
                if (Chk_RezerveSaleMandeh.Checked == true)
                {
                    var Query8 = from Row in Query
                                 where Row.Mandeh == int.Parse(Txt_RezerveSaleMandeh.Text.Trim())
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query8;
                    int_SearchFlg = 1;
                }
                if (Chk__RezerveSaleMolahezat.Checked == true)
                {
                    var Query9 = from Row in Query
                                 where Row.Notes == Txt_RezerveSaleMolahezat.Text.Trim()
                                 orderby Row.RezerveID descending
                                 select Row;
                    Query = Query9;
                    int_SearchFlg = 1;
                }
                if (int_SearchFlg != 0)
                {
                    Dgv_RezerveSale.DataSource = Query;
                }
            }
            catch
            {
                MessageBox.Show("لطفا فیلد انتخاب شده را پر نمایید");
            }
        }
        public string dtShamsi()
        {
            string strMydate;
            PersianCalendar calFar1 = new PersianCalendar();
            strMydate = calFar1.GetYear(DateTime.Now).ToString() + "/" + calFar1.GetMonth(DateTime.Now).ToString("00") + "/" + calFar1.GetDayOfMonth(DateTime.Now).ToString("00");
            return strMydate;
        }
        private void frmRezerveSale_Load(object sender, EventArgs e)
        {
            Txt_RezerveSaleDate.Text = dtShamsi();
            Lbl_SaleMandeh.Text = "";
            UDF_FillComService();
            UDF_FillComCustomer();
            UDF_FillComOperator();
            UDf_RefreshRezerveSale();
            Pnl_RezerveSale2.Visible = false;
        }
        private void Btn_CustumersInsert_Click(object sender, EventArgs e)
        {
            Pnl_RezerveSale1.Visible = false;
            Pnl_RezerveSale2.Visible = true;
        }
        private void Btn_CustumersDelete_Click(object sender, EventArgs e)
        {
            Pnl_RezerveSale1.Visible = false;
            Pnl_RezerveSale2.Visible = true;
        }
        private void Btn_RezerveSaleUpdate_Click(object sender, EventArgs e)
        {
            int_RezerveSale = 2;
            Cmb_RezerveSaleUpdateService.BringToFront();
            Cmb_RezerveSaleOperator.BringToFront();
            Cmb_RezerveSaleKharidar.BringToFront();
            Cmb_RezerveSaleUpdateService.Enabled = false;
            Cmb_RezerveSaleOperator.Enabled = false;
            Cmb_RezerveSaleKharidar.Enabled = false;
            UDF_FillUpdateComService();
            UDf_RefreshRezerveSale();
            Pnl_RezerveSale1.Visible = false;
            Pnl_RezerveSale2.Visible = true;
        }
        private void Btn_RezerveSaleSearch_Click(object sender, EventArgs e)
        {
            UDf_RefreshRezerveSale();
            Btn_RezerveSaleRefresh1.SendToBack();
        }
        private void Btn_RezerveSaleInsert_Click(object sender, EventArgs e)
        {
            Txt_RezerveSaleService.SendToBack();
            Txt_RezerveSaleKharidar.SendToBack();
            Txt_RezerveSaleOperator.SendToBack();
            Pnl_RezerveSale1.Visible = false;
            Pnl_RezerveSale2.Visible = true;
            int_RezerveSale = 1;
        }
        private void Btn_RezerveSaleDelete_Click(object sender, EventArgs e)
        {
            Txt_RezerveSaleRezerveId.Enabled = true;
            if (Txt_RezerveSaleRezerveId.Text == "")
            {
                MessageBox.Show("لطفا کد رزرو را وارد نمایید");
            }
            else
            {
                Pnl_RezerveSale1.Visible = false;
                Pnl_RezerveSale2.Visible = true;
                int_RezerveSale = 3;
            }
        }
        private void Btn_RezerveSaleTaeed_Click(object sender, EventArgs e)
        {
            Pnl_RezerveSale1.Visible = true;
            Pnl_RezerveSale2.Visible = false;
            Txt_RezerveSaleService.BringToFront();
            Txt_RezerveSaleOperator.BringToFront();
            Txt_RezerveSaleKharidar.BringToFront();
            Cmb_RezerveSaleUpdateService.SendToBack();
            Cmb_RezerveSaleUpdateService.Enabled = true;
            Cmb_RezerveSaleOperator.Enabled = true;
            Cmb_RezerveSaleKharidar.Enabled = true;
            tblServicesDef tblSDef = new tblServicesDef();
          try
            {
                if (Txt_RezerveSaleBeyaneh.Text == "" && Txt_RezerveSaleCount.Text == "" && Txt_RezerveSaleDate.Text == "" && Txt_RezerveSaleMandeh.Text == "" && Txt_RezerveSaleMolahezat.Text == "" && Txt_RezerveSaleRezerveId.Text == "" && Cmb_RezerveSaleKharidar.Text == "" && Cmb_RezerveSaleOperator.Text == "" && Cmb_RezerveSaleService.Text == "")
                {
                    MessageBox.Show("لطفا فیلدهای خالی را پر نمایید");
                }
                else
                {
                    // شرط مربوط به درج رزرو
                    if (int_RezerveSale == 1)
                    {
                        Decimal Dec_Mandeh = UDF_MandehService1();
                        Decimal Dec_Sabt = Decimal.Parse(Txt_RezerveSaleCount.Text.Trim());
                        Txt_RezerveSaleService.BringToFront();
                        Txt_RezerveSaleOperator.BringToFront();
                        Txt_RezerveSaleKharidar.BringToFront();
                        //شرط مربوط به مقایسه تعداد ثبت نام باظرفیت موجود 
                        if (Dec_Sabt <= Dec_Mandeh)
                        {
                            UDF_InsertRezerveSale();
                        }
                        else
                        {
                            MessageBox.Show("خارج از محدوه");
                            Dec_Mandeh = Dec_Mandeh - Dec_Sabt;
                            Decimal Dec_Mandeh1 = -(Dec_Mandeh);
                            Lbl_SaleMandeh.Text = Dec_Mandeh1.ToString();
                        }
                        //شرط مربوط به ویرایش رزرو
                    }
                    else if (int_RezerveSale == 2)
                    {
                        if (Txt_RezerveSaleCount.Text == "0")
                        {
                            UDF_LaghvSafar();
                        }
                        else
                        {
                            UDF_TakmilZarfyat1();
                        }
                    }
                    //شرط مربوط به حذف رزرو
                    else if (int_RezerveSale == 3)
                    {
                        UDF_DeleteRezerveTicket();
                    }
                    else
                    {
                        Pnl_RezerveSale1.Visible = true;
                        Pnl_RezerveSale2.Visible = false;
                    }
                    UDf_RefreshRezerveSale();
                    Txt_RezerveSaleRezerveId.Text = "";
                    Txt_RezerveSaleMolahezat.Text = "";
                    Txt_RezerveSaleMandeh.Text = "";
                    Txt_RezerveSaleDate.Text = "";
                    Txt_RezerveSaleCount.Text = "";
                    Txt_RezerveSaleBeyaneh.Text = "";

                }
            }
           catch
           {
                MessageBox.Show("لطفا به هشدار بالا توجه فرمایید");
          }
        }
        private void Btn_RezerveSaleLaghv_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_RezerveSale1.Visible = true;
                Pnl_RezerveSale2.Visible = false;
                Cmb_RezerveSaleKharidar.Enabled = true;
                Cmb_RezerveSaleOperator.Enabled = true;
                Cmb_RezerveSaleService.Enabled = true;
                Txt_RezerveSaleService.BringToFront();
                Txt_RezerveSaleOperator.BringToFront();
                Txt_RezerveSaleKharidar.BringToFront();
                Cmb_RezerveSaleUpdateService.SendToBack();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را بر طرف نمایید");
            }
        }
        /// <summary>
        /// تابع پر نمودن فیلدها پس از کلیک بر دیتا گرید ویو
        /// </summary>
        int int_ReserveID1 = 0;
        private void Dgv_RezerveSale_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv_RezerveSale.GetCellCount(DataGridViewElementStates.Selected);
            int int_Counter = 0;
            if (selectedCellCount > 0)
            {
                if (Dgv_RezerveSale.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    try
                    {
                        for (int i = 0; i < selectedCellCount; i++)
                        {
                            if (int_Counter == 0)
                            {
                                Txt_RezerveSaleSearch.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                                int_ReserveID1 = int.Parse(Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString());
                                Cmb_RezerveSaleKharidar.SelectedValue = UDF_GetSharidarID(int_ReserveID1);
                                Cmb_RezerveSaleUpdateService.SelectedValue = UDF_GetServiceID(int_ReserveID1);
                                Cmb_RezerveSaleOperator.SelectedValue = UDF_GetOperatorID(int_ReserveID1);
                            }
                            int_Counter++;
                            Txt_RezerveSaleRezerveId.Text = int_ReserveID1.ToString();
                            Txt_RezerveSaleDate.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                            string Str_CmbOperator = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                            string Str_CmbCustomer = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                            string Str_CmbService = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                            Txt_RezerveSaleService.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                            Txt_RezerveSaleOperator.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                            Txt_RezerveSaleKharidar.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                            Txt_RezerveSaleCount.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                            Txt_RezerveSaleBeyaneh.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                            Txt_RezerveSaleMandeh.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                            Txt_RezerveSaleMolahezat.Text = Dgv_RezerveSale.Rows[int.Parse(Dgv_RezerveSale.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString();
                            GhablForoush();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("خطا");
                    }
                }
            }
        }
        /// <summary>
        /// تابع مربوط به برگرداندن کد پرسنل
        /// </summary>
        private int UDF_GetOperatorID(int int_ReserveID)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tbRes = db.tblRezerveTickets.Single(n => n.RezerveID == int_ReserveID);
            return tbRes.StaffID;
        }
        /// <summary>
        /// تابع مربوط به برگرداندن کد خریدار
        /// </summary>
        private int UDF_GetSharidarID(int int_ReserveID)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tbRes = db.tblRezerveTickets.Single(n => n.RezerveID == int_ReserveID);
            return tbRes.RezerverID;
        }
        /// <summary>
        /// تابع مربوط به برگرداندن کد سرویس
        /// </summary>
        private int UDF_GetServiceID(int int_ReserveID)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tbRes = db.tblRezerveTickets.Single(n => n.RezerveID == int_ReserveID);
            return tbRes.ServiceID;
        }
        /// <summary>
        ///تابع مربوط به تعداد نفرات موجود در یک سرویس در جدول سرویس برای درج
        /// </summary>
        private decimal UDF_ZarfyatServiceDef()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            decimal Dec_Sabt = 0;
            var Query = from row in db.tblServicesDefs
                        where decimal.Parse(Cmb_RezerveSaleService.SelectedValue.ToString()) == row.ServiceID
                        select new
                        {
                            row.Quantity
                        };
            foreach (var Sabt1 in Query)
            {
                Dec_Sabt = decimal.Parse(Sabt1.Quantity.ToString());
            }
            return Dec_Sabt;
        }
        /// <summary>
        ///تابع مربوط به تعداد نفرات موجود در یک سرویس در جدول سرویس برای ویرایش 
        /// </summary>
        private decimal UDF_ZarfyatServiceDef2()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            decimal Dec_Sabt = 0;
            var Query = from row in db.tblServicesDefs
                        where int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString()) == row.ServiceID
                        select new
                        {
                            row.Quantity
                        };
            foreach (var Sabt1 in Query)
            {
                Dec_Sabt = decimal.Parse(Sabt1.Quantity.ToString());
            }
            return Dec_Sabt;
        }
        /*      private decimal UDF_ZarfyatServiceDef4()
              {
                  LSC_TADataContext db = new LSC_TADataContext();
                  decimal Dec_Sabt = 0;
                  var Query = from row in db.tblServicesDefs
                              where Txt_RezerveSaleService.Text == row.ServiceDescript
                              select new
                              {
                                  row.Quantity
                              };
                  foreach (var Sabt1 in Query)
                  {
                      Dec_Sabt = decimal.Parse(Sabt1.Quantity.ToString());
                  }
                  return Dec_Sabt;
              }*/
        /// <summary>
        /// تابع مربوط به تعداد ثبت نامی در جدول رزرو
        /// </summary>
        private Decimal UDF_ZarfyatService3()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            // Decimal Dec_Sabt = 0;
            var Query = from row in db.tblRezerveTickets
                        where (int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString()) == row.ServiceID)
                        select new
                        {
                            row.QuanTity
                        };
            foreach (var Sabt1 in Query)
            {
                Des_SaleCount += Decimal.Parse(Sabt1.QuanTity.ToString());
            }
            return Des_SaleCount;
        }
        private Decimal UDF_ZarfyatService2()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Decimal Dec_Sabt = 0;
            var Query = from row in db.tblRezerveTickets
                        where (int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString()) == row.ServiceID)
                        select new
                        {
                            row.QuanTity
                        };
            foreach (var Sabt in Query)
            {
                Dec_Sabt += Decimal.Parse(Sabt.QuanTity.ToString());
            }
            return Dec_Sabt;
        }
        private Decimal UDF_ZarfyatService4()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Decimal Dec_Sabt = 0;
            var Query = from row in db.tblRezerveTickets
                        where (int.Parse(Txt_RezerveSaleSearch.Text) == row.RezerveID)
                        select new
                        {
                            row.QuanTity
                        };
            foreach (var Sabt in Query)
            {
                Dec_Sabt += Decimal.Parse(Sabt.QuanTity.ToString());
            }
            return Dec_Sabt;
        }
        private Decimal UDF_ZarfyatService()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Decimal Dec_Sabt = 0;
            var Query = from row in db.tblRezerveTickets
                        where (int.Parse(Cmb_RezerveSaleService.SelectedValue.ToString()) == row.ServiceID) || ((row.tblServicesDef.ServiceDescript).ToString() == Txt_RezerveSaleService.Text)
                        select new
                        {
                            row.QuanTity
                        };
            foreach (var Sabt in Query)
            {
                Dec_Sabt += Decimal.Parse(Sabt.QuanTity.ToString());
            }
            return Dec_Sabt;
        }
        /// <summary>
        /// تابع مربوط به ظرفیت مانده
        /// </summary>
        private Decimal UDF_MandehService1()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblDefs = new tblServicesDef();
            Dec_ZarfyatSale = UDF_ZarfyatService();
            Dec_ZarfyatDef = UDF_ZarfyatServiceDef();
            Dec_Mandeh = Dec_ZarfyatDef - Dec_ZarfyatSale;
            return Dec_Mandeh;
        }
        private Decimal UDF_MandehService2()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblDefs = new tblServicesDef();
            Decimal Dec_ZarfyatSale;
            Decimal Dec_ZarfyatDef;
            Decimal Dec_Mandeh;
            Dec_ZarfyatSale = UDF_ZarfyatService3();
            Dec_ZarfyatDef = UDF_ZarfyatServiceDef2();
            Dec_Mandeh = Dec_ZarfyatDef - Dec_ZarfyatSale;
            return Dec_Mandeh;
        }
        private Decimal UDF_MandehService3()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblDefs = new tblServicesDef();
            Dec_ZarfyatSale = UDF_ZarfyatService2();
            Dec_ZarfyatDef = UDF_ZarfyatServiceDef2();
            Dec_Mandeh = Dec_ZarfyatDef - Dec_ZarfyatSale;
            return Dec_Mandeh;
        }
        private Decimal UDF_MandehService4()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblDefs = new tblServicesDef();
            Dec_ZarfyatSale = UDF_ZarfyatService4();
            // Dec_ZarfyatDef = UDF_ZarfyatServiceDef4();
            Dec_Mandeh = Dec_ZarfyatDef - Dec_ZarfyatSale;
            return Dec_Mandeh;
        }
        private void Btnl_RezerveSaleMandeh_Click(object sender, EventArgs e)
        {
            GhablForoush();
        }

        private void GhablForoush()
        {
            Lbl_SaleMandeh.Text = "";
            if (int_RezerveSale == 2)
            {
                Dec_Mandeh = UDF_MandehService3();
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
            }
            else if (int_RezerveSale == 1)
            {
                Dec_Mandeh = UDF_MandehService1();
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
            }
            else
            {
                Dec_Mandeh = UDF_MandehService1();
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
            }
        }
        private void UDF_TakmilZarfyat()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblDef = db.tblServicesDefs.Single(n => n.ServiceID == int.Parse(Cmb_RezerveSaleService.SelectedValue.ToString().Trim()));
            Decimal Dec_Mandeh = UDF_MandehService1();
            //شرط مربوط به تعداد باقی مانده پس از ثبت نام
            if (Dec_Mandeh == 0)
            {
                MessageBox.Show("ظرفیت تکمیل شد");
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
                tblDef.ExpireBit = Boolean.Parse("false");
                db.SubmitChanges();
                UDF_FillComService();
            }
            else
            {
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
            }
        }
        private void UDF_TakmilZarfyat1()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tblRezerve = db.tblRezerveTickets.Single(n => n.RezerveID == int.Parse(Txt_RezerveSaleRezerveId.Text.Trim()));
            int_QuanTity = int.Parse(tblRezerve.QuanTity.ToString());
            tblRezerve.QuanTity = int.Parse(Txt_RezerveSaleCount.Text.Trim());
            Des_SaleCount = Decimal.Parse(Txt_RezerveSaleCount.Text.Trim());
            tblRezerve.RezerveDate = Txt_RezerveSaleDate.Text.Trim();
            tblRezerve.RezerveID = int.Parse(Txt_RezerveSaleSearch.Text.Trim());
            tblRezerve.Notes = Txt_RezerveSaleMolahezat.Text.Trim();
            tblRezerve.StaffID = int.Parse(Cmb_RezerveSaleOperator.SelectedValue.ToString().Trim());
            tblRezerve.RezerverID = int.Parse(Cmb_RezerveSaleKharidar.SelectedValue.ToString().Trim());
            tblRezerve.ServiceID = int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString().Trim());
            tblRezerve.BianehPardakhti = int.Parse(Txt_RezerveSaleBeyaneh.Text.Trim());
            tblRezerve.Mandeh = int.Parse(Txt_RezerveSaleMandeh.Text.Trim());
            tblServicesDef tblDef = db.tblServicesDefs.Single(n => n.ServiceID == int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString().Trim()));
            Decimal Dec_Mandeh = UDF_MandehService2();
            Dec_Mandeh = int_QuanTity + Dec_Mandeh;
            //شرط مربوط به تعداد باقی مانده پس از ثبت نام
            if (Dec_Mandeh == 0)
            {
                db.SubmitChanges();
                MessageBox.Show("ظرفیت تکمیل شد");
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
                tblDef.ExpireBit = Boolean.Parse("false");
                db.SubmitChanges();
                UDF_FillComService();
                Txt_RezerveSaleRezerveId.Text = "";
                Txt_RezerveSaleMolahezat.Text = "";
                Txt_RezerveSaleMandeh.Text = "";
                Txt_RezerveSaleDate.Text = "";
                Txt_RezerveSaleCount.Text = "";
                Cmb_RezerveSaleKharidar.Text = "";
                Cmb_RezerveSaleOperator.Text = "";
                Txt_RezerveSaleBeyaneh.Text = "";
                Cmb_RezerveSaleService.Text = "";
                Txt_RezerveSaleOperator.Text = "";
                Txt_RezerveSaleService.Text = "";
                Txt_RezerveSaleKharidar.Text = "";
            }
            else if (Dec_Mandeh < 0)
            {
                MessageBox.Show("تعداد افراد ثبت نامی بیش از ظرفیت است");
            }
            else
            {
                Lbl_SaleMandeh.Text = Dec_Mandeh.ToString();
                db.SubmitChanges();
                Txt_RezerveSaleRezerveId.Text = "";
                Txt_RezerveSaleMolahezat.Text = "";
                Txt_RezerveSaleMandeh.Text = "";
                Txt_RezerveSaleDate.Text = "";
                Txt_RezerveSaleCount.Text = "";
                Cmb_RezerveSaleKharidar.Text = "";
                Cmb_RezerveSaleOperator.Text = "";
                Txt_RezerveSaleBeyaneh.Text = "";
                Cmb_RezerveSaleService.Text = "";
                Txt_RezerveSaleOperator.Text = "";
                Txt_RezerveSaleService.Text = "";
                Txt_RezerveSaleKharidar.Text = "";
            }
        }
        /// <summary>
        /// تابع مربوط به صفر کردن فیلد اکسپایر سرویس پر شده
        /// </summary>
        /// <returns></returns>
        private Decimal UDF_Expire()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tblRezerve = new tblRezerveTicket();
            tblServicesDef tblDef = new tblServicesDef();
            Decimal Dec_Count = 0;
            var Query = from row in db.tblRezerveTickets
                        where int.Parse(Cmb_RezerveSaleUpdateService.SelectedValue.ToString()) == row.ServiceID
                        select new
                        {
                            row.QuanTity
                        };
            foreach (var Count in Query)
            {
                Dec_Count += Decimal.Parse(Count.QuanTity.ToString());
            }
            return Dec_Count;
        }
        private void UDF_LaghvSafar()
        {
            DialogResult Dialog_Dr;
            Dialog_Dr = MessageBox.Show("آیا مایل به لغو سفر هستید", "اخطار", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (Dialog_Dr == DialogResult.Yes)
            {
                UDF_DeleteRezerveTicket();
            }
        }
        /// <summary>
        /// تابع مربوط به هشدار ظرفیت تکمیل است
        /// </summary>
        private void UDF_TakmilZ()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblRezerveTicket tblRezerve = new tblRezerveTicket();
            tblServicesDef tblDef = db.tblServicesDefs.Single(n => n.ServiceID == int.Parse(Cmb_RezerveSaleService.SelectedValue.ToString().Trim()));
            if (tblDef.Quantity == tblRezerve.QuanTity)
            {
                MessageBox.Show("ظرفیت تکمیل است");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Cmb_RezerveSaleUpdateService.SendToBack();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            UDF_TakmilZ();
        }
        private void Txt_RezerveSaleDate_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 47 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Txt_RezerveSaleCount_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 48 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// تابع مربوط به مبلغ کل سرویس
        /// </summary>
        private Decimal UDF_MablaghColeService(int int_Service)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Decimal Dec_Sabt = 0;
            var Query = from row in db.tblServicesDefs
                        where int_Service == row.ServiceID
                        select new
                        {
                            row.DefPrice
                        };
            foreach (var Sabt in Query)
            {
                Dec_Sabt += Decimal.Parse(Sabt.DefPrice.ToString());
            }
            Dec_Sabt *= Decimal.Parse(Txt_RezerveSaleCount.Text.Trim().ToString());
            return Dec_Sabt;
        }
        private void Txt_RezerveSaleBeyaneh_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 48 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Txt_RezerveSaleMandeh_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 48 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Btn_RezerveSaleRefresh_Click(object sender, EventArgs e)
        {
            Btn_RezerveSaleRefresh1.BringToFront();
            UDf_SearchAdvancedRezerveSale();
        }
        private void Btn_RezerveSalePrice_Click(object sender, EventArgs e)
        {
            if (Txt_RezerveSaleCount.Text == "")
            {
                MessageBox.Show("لطفا فیلد تعداد را پر نمایید");
            }
            else
            {
                int int_Service = int.Parse(Cmb_RezerveSaleService.SelectedValue.ToString());
                Lbl_SalePrice.Text = UDF_MablaghColeService(int_Service).ToString();
            }
        }

        private void Cmb_RezerveSaleService_Click(object sender, EventArgs e)
        {

        }

        private void Cmb_RezerveSaleUpdateService_Click(object sender, EventArgs e)
        {

        }

        private void Cmb_RezerveSaleService_TextChanged(object sender, EventArgs e)
        {
            GhablForoush();
        }

        private void Cmb_RezerveSaleUpdateService_TextChanged(object sender, EventArgs e)
        {
            GhablForoush();
        }

        private void Cmb_RezerveSaleService_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Dgv_RezerveSale_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Cmb_RezerveSaleUpdateService_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}



