private void btnKaydet_Click(object sender, EventArgs e)
        {
            try
            {
                supermarketEntities db = new supermarketEntities();
                var seciliKategori     = cmbKategori.SelectedItem as Category;
                var seciliTedarikci    = cmbFirmaAdi.SelectedItem as Supplier;

                if (seciliKategori == null || seciliTedarikci == null)
                {
                    MessageBox.Show("Lütfen tüm alanları doldurunuz.");
                    return;
                }
                Product yeniurun = new Product
                {
                    ProductName  = txtUrunAdi.Text,
                    Barcode      = txtUrunKodu.Text,
                    CompanyId    = seciliTedarikci.CompanyId,
                    UnitPrice    = nAlisFiyati.Value,
                    SalesPrice   = nSatisFiyati.Value,
                    CategoryId   = seciliKategori.CategoryId,
                    Stock        = Convert.ToInt32(nStok.Value),
                    ProductImage = resimDosyası
                };
                db.Products.Add(yeniurun);
                db.SaveChanges();
                MessageBox.Show($"{yeniurun.ProductName} ürünü başarıyla eklendi");
                VerileriGetir();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 void VerileriGetir(string arama = "")
 {
     try
     {
         var ara = arama.ToLower();
         supermarketEntities db = new supermarketEntities();
         var sonuc = from prod in db.Products
                     where (prod.Barcode.Contains(ara))
                     orderby prod.Barcode ascending
                     select new UrunViewModel
         {
             UrunId      = prod.ProductId,
             UrunAdi     = prod.ProductName,
             SatisFiyati = prod.SalesPrice ?? 0,
             Stok        = prod.Stock,
             Kategori    = prod.Category.CategoryName
         };
         lstUrun.DataSource = sonuc.ToList();
         urunList           = sonuc.ToList();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Veritabanına bağlanılamadı: " + ex.Message);
     }
 }
        private void btnGuncelle_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstUrunler.SelectedItems == null)
                {
                    return;
                }
                supermarketEntities db = new supermarketEntities();
                int seciliUrun         = int.Parse(lstUrunler.SelectedItems[0].Text);
                var sonuc = db.Products.Where(x => x.ProductId == seciliUrun).ToList();

                sonuc.ForEach(x =>
                {
                    x.ProductName           = txtUrunAdi.Text;
                    x.Barcode               = txtUrunKodu.Text;
                    x.Supplier.CompanyName  = cmbFirmaAdi.Text;
                    x.UnitPrice             = nAlisFiyati.Value;
                    x.SalesPrice            = nSatisFiyati.Value;
                    x.Category.CategoryName = cmbKategori.Text;
                    x.Stock        = Convert.ToInt32(nStok.Value);
                    x.ProductImage = resimDosyası;
                });

                db.SaveChanges();
                VerileriGetir();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void detayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstOrders.SelectedItems == null || lstOrders.SelectedItems.Count == 0)
            {
                MessageBox.Show("Bir şey seçmediniz");
                return;
            }
            try
            {
                var siparisId          = int.Parse(lstOrders.SelectedItems[0].Text);
                supermarketEntities db = new supermarketEntities();
                var sonuc = db.OrderDetails.Where(x => x.OrderId == siparisId).ToList();
                List <ListViewItem> viewItemList = new List <ListViewItem>();
                sonuc.ForEach(x =>
                {
                    ListViewItem viewItem = new ListViewItem(x.Product.ProductName);
                    viewItem.SubItems.Add($"{x.SalesPrice:c2}");
                    viewItem.SubItems.Add($"{x.Quantity}");
                    //viewItem.SubItems.Add($"% {/*x.Discount **/ 100}");
                    viewItemList.Add(viewItem);
                });

                FormMusteriSiparisDetay musteriSiparisDetay = new FormMusteriSiparisDetay(viewItemList);
                musteriSiparisDetay.Text          = $"{siparisId} nolu siparişin detayı";
                musteriSiparisDetay.StartPosition = FormStartPosition.CenterParent;
                musteriSiparisDetay.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void VerileriGetir()
        {
            supermarketEntities db = new supermarketEntities();

            lstCustomer.DataSource    = db.Customers.OrderBy(x => x.CustomerName).ToList();
            lstCustomer.DisplayMember = "CustomerName";
            lstCustomer.ValueMember   = "CustomerId";
        }
        private void lstCustomer_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (lstCustomer.SelectedItem == null)
            {
                return;
            }

            var seciliMusteri = lstCustomer.SelectedItem as Customer;

            try
            {
                supermarketEntities db = new supermarketEntities();

                var sonuc = db.Orders
                            .Where(x => x.CustomerId == seciliMusteri.CustomerId)
                            .OrderByDescending(x => x.OrderDate)
                            .Select(x => new SiparisViewModel()
                {
                    IstenenTarih        = x.RequiredDate,
                    KargoFirmasi        = x.CargoCompany,
                    KargoTutari         = x.Freight ?? 0,
                    SiparisNo           = x.OrderId,
                    SiparisTarihi       = x.OrderDate,
                    Ulke                = x.Address,
                    ToplamSiparisTutari = 0
                })
                            .ToList();
                lstOrders.Items.Clear();
                sonuc.ForEach(x =>
                {
                    x.ToplamSiparisTutari = db.OrderDetails
                                            .Where(y => y.OrderId == x.SiparisNo)
                                            .Sum(y => y.SalesPrice * y.Quantity);
                    ListViewItem viewItem = new ListViewItem(x.SiparisNo.ToString());
                    viewItem.SubItems.Add($"{x.SiparisTarihi:dd MMMM yyyy}");
                    viewItem.SubItems.Add($"{x.IstenenTarih:dd MMMM yyyy}");
                    viewItem.SubItems.Add(x.Ulke);
                    viewItem.SubItems.Add($"{x.KargoTutari:c2}");
                    viewItem.SubItems.Add($"{x.ToplamSiparisTutari:c2}");
                    viewItem.SubItems.Add(x.KargoFirmasi);
                    lstOrders.Items.Add(viewItem);
                }
                              );
                lstOrders.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sipariş bulunamadı");
                MessageBox.Show(ex.Message);
            }
        }
        private void btnSil_Click(object sender, EventArgs e)
        {
            if (lstUrunler.SelectedItems == null)
            {
                return;
            }
            supermarketEntities db = new supermarketEntities();
            int seciliUrun         = int.Parse(lstUrunler.SelectedItems[0].Text);
            var sonuc = db.Products.Where(x => x.ProductId == seciliUrun).FirstOrDefault();

            db.Products.Remove(sonuc);
            db.SaveChanges();
            VerileriGetir();
        }
        private void VerileriGetir()
        {
            try
            {
                supermarketEntities db = new supermarketEntities();

                cmbKategori.DataSource    = db.Categories.ToList();
                cmbKategori.DisplayMember = "CategoryName";
                cmbKategori.ValueMember   = "CategoryId";

                cmbFirmaAdi.DataSource    = db.Suppliers.ToList();
                cmbFirmaAdi.DisplayMember = "CompanyName";
                cmbFirmaAdi.ValueMember   = "CompanyId";

                var urunler = db.Products
                              .Select(x => new UrunViewModel()
                {
                    UrunId         = x.ProductId,
                    UrunAdi        = x.ProductName,
                    Barkod         = x.Barcode,
                    TedarikciFirma = x.Supplier.CompanyName,
                    AlisFiyati     = x.UnitPrice ?? 0,
                    SatisFiyati    = x.SalesPrice ?? 0,
                    Kategori       = x.Category.CategoryName,
                    Stok           = x.Stock ?? 0
                }).ToList();
                lstUrunler.Items.Clear();

                urunler.ForEach(x =>
                {
                    ListViewItem viewItem = new ListViewItem(x.UrunId.ToString());
                    viewItem.SubItems.Add(x.UrunAdi);
                    viewItem.SubItems.Add(x.Barkod);
                    viewItem.SubItems.Add(x.TedarikciFirma);
                    viewItem.SubItems.Add($"{x.AlisFiyati:c2}");

                    viewItem.SubItems.Add(x.Kategori);
                    viewItem.SubItems.Add(x.Stok.ToString());

                    lstUrunler.Items.Add(viewItem);
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void FormSatis_Load(object sender, EventArgs e)
        {
            VerileriGetir();

            try
            {
                supermarketEntities db = new supermarketEntities();

                cmbMusteri.DataSource = db.Customers.ToList();

                cmbMusteri.DisplayMember = "CompanyName";
                cmbMusteri.ValueMember   = "CustomerName";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Veritabanına bağlanılamadı: " + ex.Message);
            }
        }
 private void lstUrunler_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (lstUrunler.SelectedItems == null)
     {
         return;
     }
     if (lstUrunler.SelectedItems.Count > 0)
     {
         supermarketEntities db = new supermarketEntities();
         int seciliurun         = int.Parse(lstUrunler.SelectedItems[0].Text);
         var sonuc = db.Products.Where(x => x.ProductId == seciliurun).ToList();
         sonuc.ForEach(x =>
         {
             txtUrunAdi.Text    = x.ProductName;
             txtUrunKodu.Text   = x.Barcode;
             cmbFirmaAdi.Text   = x.Supplier.CompanyName;
             nAlisFiyati.Value  = Convert.ToDecimal(x.UnitPrice);
             nSatisFiyati.Value = Convert.ToDecimal(x.SalesPrice);
             cmbKategori.Text   = x.Category.CategoryName;
             nStok.Value        = Convert.ToDecimal(x.Stock);
             ///pbResim.Image = new Bitmap(new MemoryStream(x.ProductImage));
             if (x.ProductImage != null)
             {
                 try
                 {
                     pbResim.Image = new Bitmap(new MemoryStream(x.ProductImage));
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
             }
             else
             {
                 pbResim.Image = null;
             }
         });
     }
 }
        private void btnSiparisOlustur_Click(object sender, EventArgs e)
        {
            if (sepetList.Count == 0)
            {
                MessageBox.Show("Önce Sepete Ürün Eklemelisiniz");
                return;
            }

            var musteri = cmbMusteri.SelectedItem as Customer;
            //var kargo = cmbKargo.SelectedItem as Shipper;
            string mesaj = $" siparişi onaylıyor musunuz?\nMüşteri: {musteri.CustomerName}";

            label11.Text = satisfiyat.ToString() + " TL";

            var cevap = MessageBox.Show(mesaj, "Sipariş Onayı", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (cevap != DialogResult.Yes)
            {
                return;
            }

            supermarketEntities db = new supermarketEntities();

            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    var yeniSiparis = new Order()
                    {
                        CustomerId   = musteri.CustomerId,
                        OrderDate    = DateTime.Now,
                        RequiredDate = DateTime.Now.AddDays(7),
                        Freight      = nUlasUcret.Value,
                    };
                    db.Orders.Add(yeniSiparis);
                    db.SaveChanges();

                    foreach (var item in sepetList)
                    {
                        var siparisDetay = new OrderDetail()
                        {
                            OrderId    = yeniSiparis.OrderId,
                            ProductId  = item.ProductID,
                            SalesPrice = item.SalesPrice,
                            Quantity   = item.Quantity,
                            //Discount = item.Discount,
                        };
                        //if (item.ProductID == 2) // bilerek hata verdirmek istiyoruz rollbacki görmek için
                        //    throw new Exception("Manuel hata");
                        db.OrderDetails.Add(siparisDetay);

                        var urun = db.Products.Find(item.ProductID);
                        urun.Stock -= item.Quantity;
                    }
                    db.SaveChanges();

                    tran.Commit();
                    decimal toplam = 0, kdv = 0;
                    toplam = sepetList.Sum(x => x.Total);
                    if (rbKrediKarti.Checked)
                    {
                        toplam = nToplam.Value;
                        MessageBox.Show("ödeme türü :kredi \n ücret  kartınızdan  çekildi" + satisfiyat + " TL");
                    }

                    else if (radioButton1.Checked)
                    {
                        toplam = nToplam.Value;
                        decimal nakit    = Convert.ToDecimal(txtOdeme.Text);
                        decimal paraUstu = 0.0m;
                        paraUstu         = nakit - satisfiyat;
                        txtParaUstu.Text = paraUstu.ToString();
                        MessageBox.Show("ödeme türü:nakit  \n paraüstünüz:" + paraUstu);
                    }
                    MessageBox.Show($"{yeniSiparis.OrderId}'nolu siparişiniz tarafımıza ulaşmıştır");
                    Temizle();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    MessageBox.Show("Sipariş Oluşturma işleminde hata oluştu.\n" + ex.Message);
                }
                label11.Text = "";
            }
        }
    public static void XmlToMongoDB()
    {
        if (!mongoDB.CollectionExists("ExpensesReports"))
        {
            mongoDB.CreateCollection("ExpensesReports");
        }

        MongoCollection expensesReports = mongoDB.GetCollection("ExpensesReports");
        expensesReports.RemoveAll();

        supermarketEntities db = new supermarketEntities();
        var expenses = db.Expenses
            .Include("Expenses")
            .Select(e => new
            {
                vendorName = e.Vendors.VendorName,
                month = e.Mounth,
                expenses = e.Expenses1
            });

        foreach (var e in expenses)
        {
            var bson = e.ToBsonDocument();
            expensesReports.Insert(bson);
        }
    }
    private static IQueryable<object> GenerateJSON()
    {
        supermarketEntities db = new supermarketEntities();

        var products = db.SellsReports
            .Include("Product")
            .Select(r => new
            {
                productId = r.Product.ID,
                productName = r.Product.Product_Name,
                vendorName = r.Product.Vendor.VendorName,
                totalQuantitySold =
                    db.SellsReports
                    .Where(x => x.ProductID == r.ProductID)
                    .Sum(x => x.Quantity),
                totalIncomes =
                    db.SellsReports
                    .Where(x => x.ProductID == r.ProductID)
                    .Sum(x => x.Quantity * x.UnitPrice)
            })
            .Distinct();

        return products;
    }