Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
6.5 kB
0
Indexable
Never

        QlbanHangContext db = new QlbanHangContext();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            HienThiDuLieu();
            var query = from l in db.LoaiSps
                        select l;
            cboLoaiSp.ItemsSource = query.ToList();
            cboLoaiSp.SelectedValuePath = "MaLoai";
            cboLoaiSp.DisplayMemberPath = "TenLoai";
            cboLoaiSp.SelectedIndex = 0;
        }

        public bool valid()
        {
            int soLuong;
            int donGia;
            if(!int.TryParse(txtSoLuong.Text, out soLuong) || !(soLuong > 0) )
            {
                MessageBox.Show("Số lượng có phải là số nguyên lớn hơn 0", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            if (!int.TryParse(txtDonGia.Text, out donGia) || !(donGia > 0))
            {
                MessageBox.Show("Đơn giá phải là số nguyên lớn hơn 0", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        private void dtgList_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            var dongchon = dtgList.SelectedItem;
            if (dongchon != null)
            {
                Type type = dongchon.GetType();
                PropertyInfo[] properties = type.GetProperties();
                txtMaSp.Text = properties[0].GetValue(dongchon).ToString();
                txtTenSp.Text = properties[1].GetValue(dongchon).ToString();
                cboLoaiSp.Text = properties[2].GetValue(dongchon).ToString();
                txtSoLuong.Text = properties[3].GetValue(dongchon).ToString();
                txtDonGia.Text = properties[4].GetValue(dongchon).ToString();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var query = from sp in db.SanPhams
                        where sp.MaSp == txtMaSp.Text
                        select sp;
            if(query.Count() > 0)
            {
                SanPham spSua = query.SingleOrDefault();
                if(valid())
                {
                    spSua.TenSp = txtTenSp.Text;
                    spSua.MaLoai =  cboLoaiSp.SelectedValue.ToString();

                    spSua.SoLuongCo = int.Parse(txtSoLuong.Text);
                    spSua.DonGia = int.Parse(txtDonGia.Text);
                }
                db.SaveChanges();
                HienThiDuLieu();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var maSpToDelete = txtMaSp.Text;

            var query = from sp in db.SanPhams
                        where sp.MaSp == maSpToDelete
                        select sp;

            var query1 = from hd in db.ChiTietHoaDons
                         where hd.MaSp == maSpToDelete
                         select hd;

            var spXoa = query.FirstOrDefault();

            if (spXoa != null)
            {

                db.ChiTietHoaDons.RemoveRange(query1);

                db.SanPhams.Remove(spXoa);

                db.SaveChanges();
 
                HienThiDuLieu();
            }
            else
            {
                MessageBox.Show("Product not found", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

    }
}
<Application x:Class="THPro.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:THPro"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="textBoxStyle" TargetType="TextBox">
            <Setter Property="FontFamily" Value="Times New Roman"/>
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="FontStyle" Value="Italic"/>
        </Style>

        <ControlTemplate x:Key="btnTemp" TargetType="Button">
            <Grid>
                <Border Background="Cyan" CornerRadius="50"/>
                <ContentPresenter  HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>

        <Style x:Key="header" TargetType="DataGridColumnHeader">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>

        <Style x:Key="cell" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </Application.Resources> 
</Application>

 private void Button_Click_3(object sender, RoutedEventArgs e)
 {
     var query1 = from lsp in db.LoaiSps
                  select new
                  {
                      lsp.TenLoai,
                      lsp.MaLoai,
                      soSp = lsp.SanPhams.Count()

                  };
     var query2 = from sp in db.SanPhams
                  select new
                  {
                      sp.MaSp,
                      sp.TenSp,
                      sp.MaLoaiNavigation.TenLoai,
                      soluongmua = sp.ChiTietHoaDons.Sum(x => x.SoLuongMua),
                      tongsotien = sp.ChiTietHoaDons.Sum(x => x.SoLuongMua * x.MaSpNavigation.DonGia)

                  };
     var queryx = from hdct in db.ChiTietHoaDons
                  group hdct by hdct.MaSp into spGr
                  select new
                  {
                      MaSp = spGr.Key,
                      SoluongDaBan = spGr.Sum(x => x.SoLuongMua),
                      tongtien = spGr.Sum(x => x.SoLuongMua * x.MaSpNavigation.DonGia)
                  };


     var query3 = from spDaBan in queryx
                  join sp in db.SanPhams on spDaBan.MaSp equals sp.MaSp
                  select new
                  {
                      spDaBan.MaSp,
                      sp.TenSp,
                      sp.MaLoaiNavigation.TenLoai,
                      spDaBan.SoluongDaBan,
                      spDaBan.tongtien

                  };


     
     Window1 window = new Window1();
     window.dtgResult.ItemsSource = query3.ToList();
     window.Show();
 }

Leave a Comment