void newchitieu()
        {
            context = new Quan_Ly_Tieu_DungEntities3();
            LanChiTieu lct = new LanChiTieu
            {
                Money   = Convert.ToInt32(textBox1.Text),
                Reasion = textBox2.Text,
                Ngay    = DateTime.Now,
                UserID  = USER
            };

            context.LanChiTieux.Add(lct);
            context.SaveChanges();

            var sst = context.TienTrongVis.Where(p => p.UserID == USER);

            if (sst.ToList().Count != 0)
            {
                int sstv = Convert.ToInt32(sst.ToList().Last().SoTien.ToString());

                TienTrongVi ttv = new TienTrongVi
                {
                    Ngay   = DateTime.Today,
                    SoTien = sstv - Convert.ToInt32(textBox1.Text),
                    UserID = USER
                };

                context.TienTrongVis.Add(ttv);
                context.SaveChanges();
            }
            MessageBox.Show("Đã lưu");
            this.Close();
        }
Exemple #2
0
        private void btnThanhToanXau_Click(object sender, EventArgs e)
        {
            context = new Quan_Ly_Tieu_DungEntities3();
            var ttv = context.TienTrongVis.Where(p => p.UserID == USER);

            int r = dgvNoXau.CurrentCell.RowIndex;


            // lấy số tiền trong ví
            int temp = 0;

            if (ttv.ToList().Count != 0)
            {
                temp = Convert.ToInt32(ttv.ToList().Last().SoTien.ToString());
            }
            // lấy ghi chú
            string ghiChu = dgvNoXau.Rows[r].Cells[1].Value.ToString();

            // lấy số tiền cần thanh toán
            int st = Convert.ToInt32(dgvNoXau.Rows[r].Cells[0].Value.ToString());

            if (temp > st)
            {
                // chuyển true / false

                NoXau c = context.NoXaus.Where(p => p.UserID == USER && p.Ghichu == ghiChu && p.ThanhToan == false).ToList().First();
                c.ThanhToan = true;
                context.SaveChanges();


                //
                if (ttv.ToList().Count != 0)
                {
                    int stt = st;
                    st = Convert.ToInt32(ttv.ToList().Last().SoTien.ToString()) - st;

                    TienTrongVi ttvs = new TienTrongVi
                    {
                        UserID = USER,
                        SoTien = st,
                        Ngay   = DateTime.Today,
                    };
                    context.TienTrongVis.Add(ttvs);
                    context.SaveChanges();

                    // tạo thêm chi tiêu

                    LanChiTieu lcts = new LanChiTieu
                    {
                        Money   = stt,
                        Ngay    = DateTime.Today,
                        UserID  = USER,
                        Reasion = "Thanh toán cho " + ghiChu,
                    };

                    context.LanChiTieux.Add(lcts);
                    context.SaveChanges();
                }


                loadNoXau();
                frmTrangChu_Load(null, null);
                MessageBox.Show("Đã thanh toán");
            }
        }