private void button5_Click(object sender, EventArgs e)
        {
            flag = true;
            compoOfClients.Enabled = true;
            int        numberOfBill;
            ClientBill clientB = context.ClientBills.OrderByDescending(c => c.ID).FirstOrDefault();

            if (clientB == null)
            {
                numberOfBill         = 1;
                labelNumberBill.Text = numberOfBill.ToString();
            }
            else
            {
                numberOfBill = int.Parse(clientB.ID.ToString());
                numberOfBill++;
                labelNumberBill.Text = numberOfBill.ToString();
            }
        }
        private void formofclient_Load(object sender, EventArgs e)
        {
            List <Client> ListOfClient = context.Clients.ToList();

            compoOfClients.DataSource    = ListOfClient;
            compoOfClients.DisplayMember = "Name";
            //------------------------------------------------------

            int        numberOfBill;
            ClientBill clientB = context.ClientBills.OrderByDescending(c => c.ID).FirstOrDefault();

            if (clientB == null)
            {
                SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-9HH57Q0\MSSQLSEVEREX;Initial Catalog=project;Integrated Security=True");

                SqlCommand command = new SqlCommand();
                connection.Open();
                command.Connection  = connection;
                command.CommandText = "DBCC CHECKIDENT ('clientBills',RESEED,0)";
                command.ExecuteNonQuery();
                connection.Close();

                numberOfBill         = 1;
                labelNumberBill.Text = numberOfBill.ToString();
            }
            else
            {
                numberOfBill = int.Parse(clientB.ID.ToString());
                numberOfBill++;
                labelNumberBill.Text = numberOfBill.ToString();
            }
            List <Representitive> rep = context.Representitives.ToList();

            rpresentiveCombo.DataSource    = rep;
            rpresentiveCombo.DisplayMember = "Name";

            //------------------------fill name of product------------
            List <Product> ProductList = context.Products.ToList();

            productComboFromRepresentive.DataSource    = ProductList;
            productComboFromRepresentive.DisplayMember = "Name";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //compoOfClients.Enabled = false;

            //RepresentitiveBillDetail newRepresentiveBd = new RepresentitiveBillDetail();



            //ClientBill CLientBillJustAdded = context.ClientBills.OrderByDescending(r => r.ID).FirstOrDefault();
            //ClientBillDetail clientBDetails = new ClientBillDetail();
            //double totalPrice = 0;
            //string trypeClientBill = null;
            //foreach (var item in supperList)
            //{
            //    clientBDetails.Amount = item.Amount;
            //    clientBDetails.Date = item.Date;
            //    clientBDetails.Type = item.Type;
            //    trypeClientBill = item.Type;
            //    clientBDetails.AmountPrice = item.AmountPrice;
            //    clientBDetails.Paid = 0;
            //    clientBDetails.Debt = 0;
            //    clientBDetails.ClientBillObj_ID = CLientBillJustAdded.ID;
            //    clientBDetails.ProductOpj_ID = item.ProductOpj_ID;
            //    clientBDetails.AdeyMortagaType = item.AdeyMortagaType;

            //    context.ClientBillDetails.Add(clientBDetails);

            //    totalPrice = totalPrice + clientBDetails.AmountPrice;
            //    ClientBillDetail ClientBillJustAdded = context.ClientBillDetails.OrderByDescending(cB => cB.ID).FirstOrDefault();

            //    x = (Representitive)rpresentiveCombo.SelectedItem;
            //    var prdN = productComboFromRepresentive.Text.ToString();
            //    var prd = context.Products.Where(n => n.Name == prdN).FirstOrDefault();
            //    //amount of representitveBillDetails
            //    var y = context.RepresentitiveBillDetails
            //          .Where(p => p.RepresentitiveBill.Representitive_NationalID == x.NationalID && p.ProductObj_ID == prd.ID).FirstOrDefault();
            //    clientBDetails.RepresentitiveBillDetailsObj_ID = y.ID;

            //    ClientBillDetail cUpdated = context.ClientBillDetails.OrderByDescending(c => c.ID).FirstOrDefault();

            //    //  cUpdated.RepresentitiveBillDetail.GivenAmount = cUpdated.RepresentitiveBillDetail.GivenAmount - item.Amount;
            //    //context.SaveChanges();


            //    RepresentitiveBillDetail rebDeta = context.RepresentitiveBillDetails.FirstOrDefault(r => r.ID == cUpdated.RepresentitiveBillDetailsObj_ID);

            //    if (rebDeta.GivenAmount > 0 && MortagaAdeyCombo.SelectedItem.ToString() == "عادى")
            //    {
            //        rebDeta.GivenAmount = rebDeta.GivenAmount + item.Amount;


            //    }
            //    else
            //    {
            //        prd.AmountInStock = prd.AmountInStock + item.Amount;

            //    }
            //    context.SaveChanges();
            //}
            //Client ClientObj = (Client)compoOfClients.SelectedItem;
            ////Representitive rep = context.Clients.FirstOrDefault(r => r.Name == NameRep.Name);
            //double paid = int.Parse(PaidNumericUpdown.Value.ToString());
            //double deptPrice = totalPrice - paid;
            //ClientBill NewClientBill = new ClientBill()
            //{
            //    ClientObj_NationalID = ClientObj.NationalID,
            //    TotalPrice = totalPrice,
            //    type = trypeClientBill,
            //    PaidPrice = paid,
            //    DeptPrice = deptPrice
            //};

            ////lapelTotalPrice.Text = totalPrice.ToString();
            ////lapelTotalPrice.Visible = true;
            //if (flag == true)
            //{
            //    context.ClientBills.Add(NewClientBill);
            //    context.SaveChanges();
            //    flag = false;
            //}
            compoOfClients.Enabled = false;

            RepresentitiveBillDetail newRepresentiveBd = new RepresentitiveBillDetail();

            Client     ClientSelected = context.Clients.FirstOrDefault(c => c.Name == compoOfClients.Text);
            ClientBill clientBilld    = new ClientBill()
            {
                ClientObj_NationalID = ClientSelected.NationalID,
                type = TypeBillCombo.Text,
            };

            context.ClientBills.Add(clientBilld);
            context.SaveChanges();


            ClientBill       CLientBillJustAdded = context.ClientBills.OrderByDescending(r => r.ID).FirstOrDefault();
            ClientBillDetail clientBDetails      = new ClientBillDetail();
            double           totalPrice          = 0;
            string           trypeClientBill     = null;

            foreach (var item in supperList)
            {
                clientBDetails.Amount           = item.Amount;
                clientBDetails.Date             = item.Date;
                clientBDetails.Type             = item.Type;
                trypeClientBill                 = item.Type;
                clientBDetails.AmountPrice      = item.AmountPrice;
                clientBDetails.Paid             = 0;
                clientBDetails.Debt             = 0;
                clientBDetails.ClientBillObj_ID = CLientBillJustAdded.ID;
                clientBDetails.ProductOpj_ID    = item.ProductOpj_ID;
                clientBDetails.AdeyMortagaType  = item.AdeyMortagaType;

                context.ClientBillDetails.Add(clientBDetails);

                totalPrice = totalPrice + clientBDetails.AmountPrice;
                ClientBillDetail ClientBillJustAdded = context.ClientBillDetails.OrderByDescending(cB => cB.ID).FirstOrDefault();

                x = (Representitive)rpresentiveCombo.SelectedItem;
                var prdN = productComboFromRepresentive.Text.ToString();
                var prd  = context.Products.Where(n => n.Name == prdN).FirstOrDefault();
                //amount of representitveBillDetails
                var y = context.RepresentitiveBillDetails
                        .Where(p => p.RepresentitiveBill.Representitive_NationalID == x.NationalID && p.ProductObj_ID == prd.ID).FirstOrDefault();
                clientBDetails.RepresentitiveBillDetailsObj_ID = y.ID;

                ClientBillDetail cUpdated = context.ClientBillDetails.OrderByDescending(c => c.ID).FirstOrDefault();

                //  cUpdated.RepresentitiveBillDetail.GivenAmount = cUpdated.RepresentitiveBillDetail.GivenAmount - item.Amount;
                //context.SaveChanges();


                RepresentitiveBillDetail rebDeta = context.RepresentitiveBillDetails.FirstOrDefault(r => r.ID == cUpdated.RepresentitiveBillDetailsObj_ID);

                if (rebDeta.GivenAmount > 0 && MortagaAdeyCombo.SelectedItem.ToString() == "عادى")
                {
                    rebDeta.GivenAmount = rebDeta.GivenAmount - item.Amount;
                }
                else
                {
                    prd.AmountInStock = prd.AmountInStock + item.Amount;
                }
                context.SaveChanges();
            }
            Client ClientObj = (Client)compoOfClients.SelectedItem;
            //Representitive rep = context.Clients.FirstOrDefault(r => r.Name == NameRep.Name);
            double     paid          = int.Parse(PaidNumericUpdown.Value.ToString());
            double     deptPrice     = totalPrice - paid;
            ClientBill NewClientBill = new ClientBill()
            {
                ClientObj_NationalID = ClientObj.NationalID,
                TotalPrice           = totalPrice,
                type      = trypeClientBill,
                PaidPrice = paid,
                DeptPrice = deptPrice
            };

            //lapelTotalPrice.Text = totalPrice.ToString();
            //lapelTotalPrice.Visible = true;
            if (flag == true)
            {
                context.ClientBills.Add(NewClientBill);
                context.SaveChanges();
                flag = false;
            }
        }