private void Late_paymentOrderMony_Load(object sender, EventArgs e)
        {
            onematch = false;
            if (this.clientId > 0)
            {
                ClientLaterPaymentinfo clientalredyexist2 = null;
                ClientLaterPaymentinfo clientalredyexist3 = null;
                var paclient          = context.clients.FirstOrDefault(c => c.Id == this.clientId);
                var clientalredyexist = context.clientLaterPaymentinfos.FirstOrDefault(c => c.Name == paclient.Name);
                if (paclient.FirstPhoneNumner != "")
                {
                    clientalredyexist2 = context.clientLaterPaymentinfos.FirstOrDefault(c => c.FirstPhoneNumner == paclient.FirstPhoneNumner ||
                                                                                        c.FirstPhoneNumner == paclient.SecondPhoneNumner);
                }
                if (paclient.SecondPhoneNumner != "")
                {
                    clientalredyexist3 = context.clientLaterPaymentinfos.FirstOrDefault(c => c.SecondPhoneNumner == paclient.FirstPhoneNumner ||
                                                                                        c.SecondPhoneNumner == paclient.SecondPhoneNumner);
                }
                if (clientalredyexist != null)
                {
                    onematch       = true;
                    this.donevalue = 0;
                    DialogResult dialog = MessageBox.Show("هذا العميل موجود بالفعل هل تريض اضافة طلب جديد له", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (dialog == DialogResult.Yes)
                    {
                        using (var payednew = new payIfUserExists())
                        {
                            payednew.AllMony = this.allmony;
                            var result = payednew.ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                if (payednew.payeddonevalue == 0)
                                {
                                    this.closethisform();
                                    return;
                                }
                                OrderMoneAndDate pay = new OrderMoneAndDate()
                                {
                                    clientid          = clientalredyexist.Id,
                                    DateAdded         = DateTime.Now,
                                    allMony           = this.allmony,
                                    paidOrderMony     = payednew.Paiedmony,
                                    RemaningOrderMony = payednew.RemaningMony,
                                    orderid           = this.ordeid
                                };
                                context.OrderMoneAndDates.Add(pay);
                                context.SaveChanges();
                            }
                        }

                        MessageBox.Show("تمت الاضافة بنجاح", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.donevalue    = 1;
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    if (dialog == DialogResult.No)
                    {
                        this.Close();
                    }
                    this.Close();
                }
                if (clientalredyexist2 != null)
                {
                    if (onematch == false)
                    {
                        onematch       = true;
                        this.donevalue = 0;
                        DialogResult dialog = MessageBox.Show("هذا العميل موجود بالفعل هل تريض اضافة طلب جديد له", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (dialog == DialogResult.Yes)
                        {
                            using (var payednew = new payIfUserExists())
                            {
                                var result = payednew.ShowDialog();
                                if (result == DialogResult.OK)
                                {
                                    if (payednew.payeddonevalue == 0)
                                    {
                                        this.closethisform();
                                        return;
                                    }

                                    OrderMoneAndDate pay = new OrderMoneAndDate()
                                    {
                                        clientid          = clientalredyexist.Id,
                                        DateAdded         = DateTime.Now,
                                        allMony           = this.allmony,
                                        paidOrderMony     = payednew.Paiedmony,
                                        RemaningOrderMony = payednew.RemaningMony,
                                        orderid           = this.ordeid
                                    };
                                    context.OrderMoneAndDates.Add(pay);
                                    context.SaveChanges();
                                }
                            }
                            MessageBox.Show("تمت الاضافة بنجاح", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            this.donevalue    = 1;
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        if (dialog == DialogResult.No)
                        {
                            this.Close();
                        }
                        this.Close();
                    }
                }
                if (clientalredyexist3 != null)
                {
                    if (onematch == false)
                    {
                        onematch       = true;
                        this.donevalue = 0;
                        DialogResult dialog = MessageBox.Show("هذا العميل موجود بالفعل هل تريض اضافة طلب جديد له", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (dialog == DialogResult.Yes)
                        {
                            using (var payednew = new payIfUserExists())
                            {
                                var result = payednew.ShowDialog();
                                if (result == DialogResult.OK)
                                {
                                    if (payednew.payeddonevalue == 0)
                                    {
                                        this.closethisform();
                                        return;
                                    }
                                    OrderMoneAndDate pay = new OrderMoneAndDate()
                                    {
                                        clientid          = clientalredyexist.Id,
                                        DateAdded         = DateTime.Now,
                                        allMony           = this.allmony,
                                        paidOrderMony     = payednew.Paiedmony,
                                        RemaningOrderMony = payednew.RemaningMony,
                                        orderid           = this.ordeid
                                    };
                                    context.OrderMoneAndDates.Add(pay);
                                    context.SaveChanges();
                                }
                            }
                            MessageBox.Show("تمت الاضافة بنجاح", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.donevalue    = 1;
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        if (dialog == DialogResult.No)
                        {
                            this.Close();
                        }
                        this.Close();
                    }
                }

                clientNametextbox.Text    = paclient.Name;
                clientAddresstextbox.Text = paclient.Address;
                firstPhonetextbox.Text    = paclient.FirstPhoneNumner;
                seceondPhoneTextbox.Text  = paclient.SecondPhoneNumner;
                serviceNumeric.Value      = Convert.ToInt32(paclient.DelevaryService);
                AllMonytextBox.Text       = allmony.ToString();
            }
            if (allmony > 0)
            {
                AllMonytextBox.Text = allmony.ToString();
            }
        }
        private void Save_Click(object sender, EventArgs e)
        {
            this.donevalue = 0;
            try
            {
                if (clientNametextbox.Text == "")
                {
                    MessageBox.Show("من فضلك ادخل اسم العميل", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (Convert.ToDouble(RemainingMonytextbox.Text) != (Convert.ToDouble(AllMonytextBox.Text) - Convert.ToDouble(PaiedMonytextpox.Text)))
                {
                    MessageBox.Show("القيمة المتبقية من الاموال غير مستحقة", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    PaiedMonytextpox.Text     = "0";
                    RemainingMonytextbox.Text = "0";
                    return;
                }
                var clientifexist = context.clientLaterPaymentinfos.FirstOrDefault(c => c.Name == clientNametextbox.Text);
                var clientexistinmainclienttable = context.clients.FirstOrDefault(c => c.Name == clientNametextbox.Text);
                if (firstPhonetextbox.Text != "")
                {
                    clientifexistusingphone1 = context.clientLaterPaymentinfos.FirstOrDefault(c => (c.FirstPhoneNumner == firstPhonetextbox.Text) || (c.SecondPhoneNumner == firstPhonetextbox.Text));
                    clientifexistusingphone1inclienttable = context.clients.FirstOrDefault(c => (c.FirstPhoneNumner == firstPhonetextbox.Text) || (c.SecondPhoneNumner == firstPhonetextbox.Text));
                }
                if (seceondPhoneTextbox.Text != "")
                {
                    clientifexistusingphone2 = context.clientLaterPaymentinfos.FirstOrDefault(c => (c.FirstPhoneNumner == seceondPhoneTextbox.Text) || (c.SecondPhoneNumner == seceondPhoneTextbox.Text));
                    clientifexistusingphone2inclienttable = context.clients.FirstOrDefault(c => (c.FirstPhoneNumner == seceondPhoneTextbox.Text) || (c.SecondPhoneNumner == seceondPhoneTextbox.Text));
                }
                if (clientifexist != null)
                {
                    DialogResult dialog = MessageBox.Show("هذا الاسم موجود بافعل لا يمكنك اضافتة مرة اخرى", "المريض موجود", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (dialog == DialogResult.OK)
                    {
                        FormClearName();
                        clientifexist = null;
                        return;
                    }
                }
                if (clientexistinmainclienttable != null)
                {
                    DialogResult dialog = MessageBox.Show("هذا الاسم موجود بافعل فى قائمة العملاء يمكنك اضافة دفع اجل له من هناك", "المريض موجود", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (dialog == DialogResult.OK)
                    {
                        FormClearName();
                        clientifexist = null;
                        return;
                    }
                }

                if (clientifexistusingphone1 != null || clientifexistusingphone2 != null)
                {
                    DialogResult dialog = MessageBox.Show("رقم الهاتف  موجود بافعل لا يمكنك اضافتة مرة اخرى ", "المريض موجود", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (dialog == DialogResult.OK)
                    {
                        FormClearphone();
                        clientifexistusingphone1 = null;
                        clientifexistusingphone2 = null;
                        return;
                    }
                }

                if (clientifexistusingphone1inclienttable != null || clientifexistusingphone2inclienttable != null)
                {
                    DialogResult dialog = MessageBox.Show("رقم الهاتف لهذا العميل موجود بافعل فى قائمة العملاء يمكنك اضافة دفع اجل له من هناك", "المريض موجود", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (dialog == DialogResult.OK)
                    {
                        FormClearphone();
                        clientifexistusingphone1inclienttable = null;
                        clientifexistusingphone2inclienttable = null;
                        return;
                    }
                }
                ClientLaterPaymentinfo newclient = new ClientLaterPaymentinfo()
                {
                    Name              = clientNametextbox.Text,
                    Address           = clientAddresstextbox.Text,
                    FirstPhoneNumner  = firstPhonetextbox.Text,
                    SecondPhoneNumner = seceondPhoneTextbox.Text,
                    Nots              = NotstextBox.Text,
                    DelevaryService   = Convert.ToDouble(serviceNumeric.Value),
                };

                context.clientLaterPaymentinfos.Add(newclient);
                context.SaveChanges();

                Clientss newclientt = new Clientss()
                {
                    Name              = clientNametextbox.Text,
                    Address           = clientAddresstextbox.Text,
                    FirstPhoneNumner  = firstPhonetextbox.Text,
                    SecondPhoneNumner = seceondPhoneTextbox.Text,
                    Nots              = NotstextBox.Text,
                    DelevaryService   = Convert.ToDouble(serviceNumeric.Value),
                    DateAdded         = AddeddateTimePicker.Value,
                    LaterClientID     = newclient.Id
                };

                context.clients.Add(newclientt);
                context.SaveChanges();

                var curentclientid = context.clientLaterPaymentinfos.FirstOrDefault(
                    c => c.Name == clientNametextbox.Text &&
                    c.Address == clientAddresstextbox.Text &&
                    c.FirstPhoneNumner == firstPhonetextbox.Text &&
                    c.SecondPhoneNumner == seceondPhoneTextbox.Text &&
                    c.Nots == NotstextBox.Text
                    );

                OrderMoneAndDate newduration = new OrderMoneAndDate
                {
                    DateAdded         = AddeddateTimePicker.Value,
                    allMony           = Convert.ToDouble(AllMonytextBox.Text),
                    paidOrderMony     = Convert.ToDouble(PaiedMonytextpox.Text),
                    RemaningOrderMony = Convert.ToDouble(RemainingMonytextbox.Text),
                    clientid          = curentclientid.Id,
                    orderid           = this.ordeid
                };
                context.OrderMoneAndDates.Add(newduration);
                context.SaveChanges();
                MessageBox.Show("تم الاضافة بنجاح", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                FormClear();

                this.donevalue    = 1;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch {
                DialogResult dialog = MessageBox.Show("من فضلك اكمل بيانات الدفع", "المريض موجود", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }