using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using BLL;
using mgh;
using CommonLayer;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;

namespace Grocery
{
    public partial class FrmFaktor : Form
    {
        List<Kala> KalasList = new List<Kala>();
        List<Customer> CustomersList = new List<Customer>();
        int mojodi = 0;
        bool sabtshode = false;
        FaktorMod mod = new FaktorMod();
        public static bool kalachanged = false;
        public static string selectedkalaid = "";
        public static bool CustomerChanged = false;
        public static string SelectedCid = "";
        FrmReport report = new FrmReport();

        public FrmFaktor(FaktorMod md)
        {
            InitializeComponent();
            mod = md;
        }

        private void Bind()
        {
            try
            {
                KalasList = Kala.GetAllKalas();
                //if (mod == FaktorMod.BazgashtFrosh || mod == FaktorMod.Frosh) CustomersList = Customer.GetAllCustomers();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void FrmKharid_Load(object sender, EventArgs e)
        {
            try
            {
                switch (mod)
                {
                    case FaktorMod.BazgashtFrosh:
                        panelEx1.Text = "فاکتور بازگشت از فروش";
                        pictureBox1.Image = imageList1.Images[2];
                        txtkharid.WatermarkText = "قیمت فروش";
                        BazgashtFrosh bf = new BazgashtFrosh();
                        txtsanadid.Text = bf.GetNewBazgashtFroshSanadId().ToString();
                        break;
                    case FaktorMod.BazgashtKharid:
                        pictureBox1.Image = imageList1.Images[3];
                        panelEx1.Text = "فاکتور بازگشت از خرید";
                        grpcustomer.Visible = false;
                        this.Height -= grpcustomer.Height;
                        BazgashtKharid bk = new BazgashtKharid();
                        txtsanadid.Text = bk.GetNewBazgashtKharidSanadId().ToString();
                        break;
                    case FaktorMod.Frosh:
                        pictureBox1.Image = imageList1.Images[0];
                        txtkharid.WatermarkText = "قیمت فروش";
                        panelEx1.Text = "فاکتور فروش";
                        Frosh f = new Frosh();
                        txtsanadid.Text = f.GetNewFroshSanadId().ToString();
                        break;
                    case FaktorMod.Kharid:
                        pictureBox1.Image = imageList1.Images[1];
                        panelEx1.Text = "فاکتور خرید";
                        grpcustomer.Visible = false;
                        this.Height -= grpcustomer.Height;
                        Kharid kh = new Kharid();
                        txtsanadid.Text = kh.GetNewKharidSanadId().ToString();
                        break;
                }
                Bind();
                dataGridViewX1.Rows.Clear();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void GetCustomerData()
        {
            try
            {
                CustomersList = Customer.GetCustomer(Convert.ToInt64(txtcid.Text));
                if (CustomersList.Count > 0)
                {
                    lblghablibedon.Text = CustomersList[0].bedehkar;
                    lblghabliba.Text = (Convert.ToDecimal(CustomersList[0].bedehkar) + Convert.ToDecimal(lbljamkol.Text)).ToString();
                    txtname.Text = CustomersList[0].fname.Trim() + " " + CustomersList[0].lname.Trim();
                }
                else FarsiMessagbox.Show("پیدا نشد");
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (sabtshode) { this.Close(); return; }
                switch (mod)
                {
                    case FaktorMod.BazgashtFrosh:
                        BazgashtFrosh bf = new BazgashtFrosh();
                        bf.Delete(txtsanadid.Text);
                        break;
                    case FaktorMod.BazgashtKharid:
                        BazgashtKharid bk = new BazgashtKharid();
                        bk.Delete(txtsanadid.Text);
                        break;
                    case FaktorMod.Frosh:
                        Frosh f = new Frosh();
                        f.Delete(txtsanadid.Text);
                        break;
                    case FaktorMod.Kharid:
                        Kharid kh = new Kharid();
                        kh.Delete(txtsanadid.Text);
                        break;
                }
                sabtshode = true;
                this.Close();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void txtkalaid_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (kalachanged)
                {
                    Bind();
                    kalachanged = false;
                }
                if (e.KeyData == Keys.Enter)
                {
                    if (txtkalaid.Text.Trim() != string.Empty)
                    {
                        int i;
                        for (i = 0; i < KalasList.Count; i++)
                        {
                            if (KalasList[i].kalaid == txtkalaid.Text)
                            {
                                switch (mod)
                                {
                                    case FaktorMod.BazgashtFrosh:
                                        BazgashtFrosh bf = new BazgashtFrosh();
                                        mojodi = Convert.ToInt32(bf.GetMojodiKala(txtkalaid.Text));
                                        break;
                                    case FaktorMod.BazgashtKharid:
                                        BazgashtKharid bk = new BazgashtKharid();
                                        mojodi = Convert.ToInt32(bk.GetMojodiKala(txtkalaid.Text, "0"));
                                        break;
                                    case FaktorMod.Frosh:
                                        Frosh f = new Frosh();
                                        mojodi = Convert.ToInt32(f.GetMojodiKala(txtkalaid.Text));
                                        txtkharid.Text = KalasList[i].price;
                                        break;
                                    case FaktorMod.Kharid:
                                        Kharid kh = new Kharid();
                                        mojodi = Convert.ToInt32(kh.GetMojodiKala(txtkalaid.Text));
                                        break;
                                }

                                txtkalaname.Text = KalasList[i].kalaname;
                                lblmojodi.Text = "موجودی :" + mojodi.ToString();
                                lblvahed.Text = KalasList[i].Vahed;
                                if (mojodi > 0)
                                {
                                    lblmojodi.ForeColor = Color.Green;
                                }
                                else
                                {
                                    lblmojodi.ForeColor = Color.Red;
                                }
                                break;
                            }
                        }
                        if (i == KalasList.Count) FarsiMessagbox.Show("پبدا نشد");
                    }
                    else
                    {
                        try
                        {
                            FrmListKala list = new FrmListKala();
                            list.ShowDialog();
                            if (selectedkalaid != "")
                            {
                                txtkalaid.Text = selectedkalaid;
                                txtkalaid_KeyDown(null, new KeyEventArgs(Keys.Enter));
                            }
                        }
                        catch (Exception ex)
                        {
                            FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void txtkalaid_TextChanged(object sender, EventArgs e)
        {

        }

        private void FrmKharid_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (sabtshode == false) btncancel.PerformClick();
        }

        private void btnaddtolist_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                if (txtfakid.Text.Trim() == string.Empty) { errorProvider1.SetError(txtfakid, "شماره فاکتور را وارد نمایید"); txtfakid.Focus(); return; }
                if (!FarsiDate.IsValidDate(txtdate.Text)) { errorProvider1.SetError(txtdate, "تاریخ را به شکل صحیح وارد نمایید"); txtdate.Focus(); return; }
                if (grpcustomer.Visible && txtname.Text == string.Empty) { errorProvider1.SetError(txtcid, "کد مشتری را وارد نمایید"); txtdate.Focus(); return; }
                if (txtkalaname.Text == string.Empty) { errorProvider1.SetError(txtkalaid, "کالای مورد نظر را انتخاب نمایید"); txtkalaid.Focus(); return; }
                if (Convert.ToDecimal(txtnumber.Text) == 0) { errorProvider1.SetError(txtnumber, "تعداد را بزرگتر از صفر وارد نمایید"); txtnumber.Focus(); return; }
                switch (mod)
                {
                    case FaktorMod.BazgashtKharid:
                    case FaktorMod.Frosh:
                        if (txtkharid.Text.Trim() == string.Empty) { errorProvider1.SetError(txtkharid, "قیمت فروش را وارد نمایید"); txtkharid.Focus(); return; }
                        if (txtnumber.Text.Trim() == string.Empty || int.Parse(txtnumber.Text) > mojodi) { errorProvider1.SetError(txtnumber, "تعداد را یک عدد صحیح مثبت و کوچکتر یا مساوی موجودی وارد نمایید"); txtnumber.Focus(); return; }
                        break;
                    case FaktorMod.BazgashtFrosh:
                    case FaktorMod.Kharid:
                        if (txtkharid.Text.Trim() == string.Empty) { errorProvider1.SetError(txtkharid, "قیمت خرید را وارد نمایید"); txtkharid.Focus(); return; }
                        if (txtnumber.Text.Trim() == string.Empty || int.Parse(txtnumber.Text) <= 0) { errorProvider1.SetError(txtnumber, "تعداد را یک عدد صحیح مثبت وارد نمایید"); txtnumber.Focus(); return; }
                        break;
                }

                //if(mojodi==0){FarsiMessagbox.Show("موجودی کالای "+txtkalaname.Text+" صفر می باشد");
                object[] newrow = new object[7];
                newrow[0] = dataGridViewX1.Rows.Count + 1;
                newrow[1] = txtkalaid.Text;
                newrow[2] = txtkalaname.Text;
                newrow[3] = txtnumber.Text;
                newrow[4] = lblvahed.Text;
                newrow[5] = txtkharid.Text;
                newrow[6] = int.Parse(txtnumber.Text) * int.Parse(txtkharid.Text);
                dataGridViewX1.Rows.Add(newrow);

                RowsChanged();
                init();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void init()
        {
            txtkalaid.Text = txtkalaname.Text = txtkharid.Text = txtnumber.Text = lblmojodi.Text = lblvahed.Text = string.Empty;
            mojodi = 0;
            txtkalaid.Focus();
        }

        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == dataGridViewX1.Columns["edit"].Index)
                {
                    txtkalaid.Text = dataGridViewX1.Rows[e.RowIndex].Cells["kalaid"].Value.ToString();
                    txtkalaid_KeyDown(null, new KeyEventArgs(Keys.Enter));
                    txtkharid.Text = dataGridViewX1.Rows[e.RowIndex].Cells["kharid"].Value.ToString();
                    txtnumber.Text = dataGridViewX1.Rows[e.RowIndex].Cells["number"].Value.ToString();
                }
                else if (e.ColumnIndex == dataGridViewX1.Columns["delete"].Index) dataGridViewX1.Rows.RemoveAt(e.RowIndex);
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void FrmKharid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
                btnaddtolist.PerformClick();
            if (e.KeyCode == Keys.F1)
                init();
            if (e.KeyCode == Keys.F8)
                btnok.PerformClick();
            if (e.KeyCode == Keys.P && e.Control && btnprint.Enabled)
                btnprint.PerformClick();
            if (e.KeyCode == Keys.Escape)
                this.Close();
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void RowsChanged()
        {
            try
            {
                decimal sum = 0;
                for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
                {
                    sum += Convert.ToDecimal(dataGridViewX1.Rows[i].Cells["kol"].Value);
                    dataGridViewX1.Rows[i].Cells["row"].Value = i + 1;
                    dataGridViewX1.Rows[i].Cells["edit"].Value = "ویرایش";
                    dataGridViewX1.Rows[i].Cells["delete"].Value = "حذف";
                }
                lbljamkol.Text = sum.ToString("###,###,###,###");
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void RunReportForm()
        {
            Application.Run(report);
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            try
            {
                report.listView1.Items.Clear();
                switch (mod)
                {
                    case FaktorMod.BazgashtFrosh:
                        BazgashtFrosh bf = new BazgashtFrosh();
                        for (int i = 0; i < dataGridViewX1.RowCount; i++)
                        {
                            bf.Date = txtdate.Text;
                            bf.FakId = txtfakid.Text;
                            bf.kalaid = dataGridViewX1.Rows[i].Cells["kalaid"].Value.ToString();
                            bf.Kharid = dataGridViewX1.Rows[i].Cells["kharid"].Value.ToString();
                            bf.Number = dataGridViewX1.Rows[i].Cells["number"].Value.ToString();
                            bf.SanadId = txtsanadid.Text;
                            bf.CID = txtcid.Text;
                            try
                            {
                                bf.Insert();
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت شد", 0);
                                //dataGridViewX1.Rows.RemoveAt(i);
                                //i--;
                            }
                            catch (Exception ex)
                            {
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت نشد", 1);
                                report.listView1.Items.Add(ex.Message, 1);
                                dataGridViewX1.Rows.RemoveAt(i);
                                i--;
                                //FarsiMessagbox.Show("خطا در ثبت کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString());
                            }
                        }
                        break;
                    case FaktorMod.BazgashtKharid:
                        BazgashtKharid bk = new BazgashtKharid();
                        for (int i = 0; i < dataGridViewX1.RowCount; i++)
                        {
                            bk.Date = txtdate.Text;
                            bk.FakId = txtfakid.Text;
                            bk.kalaid = dataGridViewX1.Rows[i].Cells["kalaid"].Value.ToString();
                            bk.kharid = dataGridViewX1.Rows[i].Cells["kharid"].Value.ToString();
                            bk.Number = dataGridViewX1.Rows[i].Cells["number"].Value.ToString();
                            bk.SanadId = txtsanadid.Text;
                            try
                            {
                                bk.Insert();
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت شد", 0);
                                //dataGridViewX1.Rows.RemoveAt(i);
                                //i--;
                            }
                            catch (Exception ex)
                            {
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت نشد", 1);
                                report.listView1.Items.Add(ex.Message, 1);
                                dataGridViewX1.Rows.RemoveAt(i);
                                i--;
                                //FarsiMessagbox.Show("خطا در ثبت کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString());
                            }
                        }
                        break;
                    case FaktorMod.Frosh:
                        Frosh f = new Frosh();
                        for (int i = 0; i < dataGridViewX1.RowCount; i++)
                        {
                            f.Date = txtdate.Text;
                            f.FakId = txtfakid.Text;
                            f.kalaid = dataGridViewX1.Rows[i].Cells["kalaid"].Value.ToString();
                            f.Price = dataGridViewX1.Rows[i].Cells["kharid"].Value.ToString();
                            f.Number = dataGridViewX1.Rows[i].Cells["number"].Value.ToString();
                            f.SanadId = txtsanadid.Text;
                            f.CID = txtcid.Text;
                            try
                            {
                                f.Insert();
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت شد", 0);
                                //dataGridViewX1.Rows.RemoveAt(i);
                                //i--;
                            }
                            catch (Exception ex)
                            {
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت نشد", 1);
                                report.listView1.Items.Add(ex.Message, 1);
                                dataGridViewX1.Rows.RemoveAt(i);
                                i--;
                                //FarsiMessagbox.Show("خطا در ثبت کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString());
                            }
                        }
                        break;
                    case FaktorMod.Kharid:
                        Kharid kh = new Kharid();
                        for (int i = 0; i < dataGridViewX1.RowCount; i++)
                        {
                            kh.Date = txtdate.Text;
                            kh.FakId = txtfakid.Text;
                            kh.kalaid = dataGridViewX1.Rows[i].Cells["kalaid"].Value.ToString();
                            kh.kharid = dataGridViewX1.Rows[i].Cells["kharid"].Value.ToString();
                            kh.Number = dataGridViewX1.Rows[i].Cells["number"].Value.ToString();
                            kh.SanadId = txtsanadid.Text;
                            try
                            {
                                kh.Insert();
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت شد", 0);
                                //dataGridViewX1.Rows.RemoveAt(i);
                                //i--;
                            }
                            catch (Exception ex)
                            {
                                report.listView1.Items.Add("کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString().Trim() + " ثبت نشد", 1);
                                report.listView1.Items.Add(ex.Message, 1);
                                dataGridViewX1.Rows.RemoveAt(i);
                                i--;
                                //FarsiMessagbox.Show("خطا در ثبت کالای " + dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString());
                            }
                        }
                        break;
                }
                if (txtcid.Text == "0")
                {
                    Frosh.InsertUnkhownName(txtsanadid.Text, (int)mod, txtname.Text);
                }
                sabtshode = true;
                btnprint.Enabled = true;
                FarsiMessagbox.Show("ثبت شد");
                DisableAllControls();
                if (chkpardakht.Checked)
                {
                    Pardakht.Mablagh = Convert.ToDecimal(lbljamkol.Text);
                    //Pardakht.cid = 101;
                    switch (mod)
                    {
                        case FaktorMod.BazgashtKharid:
                            Pardakht.Opt = op.Daryafti;
                            break;
                        case FaktorMod.Frosh:
                            Pardakht.Opt = op.Daryafti;
                            Pardakht.cid = Convert.ToInt64(txtcid.Text);
                            break;
                        case FaktorMod.BazgashtFrosh:
                            Pardakht.Opt = op.Pardakhti;
                            Pardakht.cid = Convert.ToInt64(txtcid.Text);
                            break;
                        case FaktorMod.Kharid:
                            Pardakht.Opt = op.Pardakhti;
                            break;
                    }

                    Frmpardakht pp = new Frmpardakht();
                    pp.Show();
                }
                //Thread run = new Thread(new ThreadStart(RunReportForm));
                //run.Start();
                report.ShowDialog();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void DisableAllControls()
        {
            if (sabtshode)
            {
                txtdate.Enabled = txtfakid.Enabled = txtkalaid.Enabled = txtkalaname.Enabled = lbljamkol.Enabled = txtnumber.Enabled = txtsanadid.Enabled = btnaddtolist.Enabled = btnok.Enabled = dataGridViewX1.Enabled = false;
            }
            else btnprint.Enabled = false;
        }

        private void txtkharid_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtkharid.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtkharid.Text = txtkharid.Text.Replace(ch.ToString(), "");
            }
        }

        private void txtnumber_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtnumber.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtnumber.Text = txtnumber.Text.Replace(ch.ToString(), "");
            }
        }

        private void txtcid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (txtcid.Text.Trim() == string.Empty)
                {
                    FrmListCustomer listcustomer = new FrmListCustomer();
                    listcustomer.ShowDialog();
                    if (SelectedCid != "")
                    {
                        txtcid.Text = SelectedCid;
                        GetCustomerData();
                    }
                }
                else if (txtcid.Text == "0")
                {
                    txtname.ReadOnly = false;
                }
                else
                {
                    GetCustomerData();
                    txtname.ReadOnly = true;
                }
            }
        }

        private void txtkharid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && mod == FaktorMod.BazgashtKharid)
            {
                BazgashtKharid bk = new BazgashtKharid();
                mojodi = Convert.ToInt32(bk.GetMojodiKala(txtkalaid.Text, txtkharid.Text));
                lblmojodi.Text = "موجودی :" + mojodi.ToString();
                if (mojodi > 0)
                {
                    lblmojodi.ForeColor = Color.Green;
                }
                else
                {
                    lblmojodi.ForeColor = Color.Red;
                }

            }
        }

        private void btntoday_Click(object sender, EventArgs e)
        {
            System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();
            txtdate.Text = p.GetYear(DateTime.Now).ToString() + p.GetMonth(DateTime.Now).ToString("0#") + p.GetDayOfMonth(DateTime.Now).ToString("0#");
            SendKeys.Send("{Tab}");
        }

        private void txtcid_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtcid.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtcid.Text = txtcid.Text.Replace(ch.ToString(), "");
            }
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            try
            {
                DSFaktor ds = new DSFaktor();
                object[] row = new object[5];
                string reportpatch="";
                switch (mod)
                {
                    case FaktorMod.BazgashtFrosh:
                        reportpatch = @"\rptbazgashtfrosh.rpt";
                        break;
                    case FaktorMod.BazgashtKharid:
                        reportpatch = @"\rptbazgashtkharid.rpt";
                        break;
                    case FaktorMod.Frosh:
                        reportpatch = @"\rptfrosh.rpt";
                        break;
                    case FaktorMod.Kharid:
                        reportpatch = @"\rptkharid.rpt";
                        break;
                }
                for (int i = 0; i < dataGridViewX1.RowCount; i++)
                {
                    row[0] = dataGridViewX1.Rows[i].Cells["kalaname"].Value.ToString();
                    row[1] = dataGridViewX1.Rows[i].Cells["number"].Value.ToString();
                    row[2] = dataGridViewX1.Rows[i].Cells["vahed"].Value.ToString();
                    row[3] = dataGridViewX1.Rows[i].Cells["kharid"].Value.ToString();
                    row[4] = dataGridViewX1.Rows[i].Cells["kol"].Value.ToString();
                    ds.Tables["faktor"].Rows.Add(row);
                }

                FrmPrintFaktor objform = new FrmPrintFaktor();
                ReportDocument RepDoc = new ReportDocument();
                string RepPath = Application.StartupPath + reportpatch;
                if (File.Exists(RepPath) == false) { MessageBox.Show("یکی از فایل های برنامه پاک شده است. لطفا برنامه را ترمیم نموده یا دوباره نصب نمایید", "چاپ", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
                RepDoc.Load(RepPath);

                RepDoc.SetDataSource(ds);

                RepDoc.SetParameterValue("date", txtdate.Text);
                RepDoc.SetParameterValue("sherkat", Settings.Name);
                if (txtname.Visible) RepDoc.SetParameterValue("name", txtname.Text);
                else RepDoc.SetParameterValue("name", "");
                RepDoc.SetParameterValue("sanadid", txtsanadid.Text);
                RepDoc.SetParameterValue("fakid", txtfakid.Text);
                RepDoc.SetParameterValue("user", Environment.UserName);
                objform.crystalReportViewer1.ReportSource = RepDoc;
                objform.ShowDialog();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }
    }
}
