public static bool edit(HoaDonDaiLy hoadon)
        {
            try
            {
                using (EntitiesDataContext db = new EntitiesDataContext())
                {
                    HOADONDAILY hd;
                    hd = (from p in db.HOADONDAILies
                          where p.masohoadon.Equals(hoadon.MaSoHoaDon)
                          select p).SingleOrDefault();
                    if (hd == null) return false;
                    hd.masodaily = hoadon.MaSoDaiLy;
                    hd.ngaylap = hoadon.NgayLap;
                    hd.trangthai = hoadon.TrangThai;
                    hd.tongtien = hoadon.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia); // tính tổng tiền các chi tiết
                    db.CHITIETHOADONDAILies.DeleteAllOnSubmit(hd.CHITIETHOADONDAILies);
                    db.SubmitChanges();
                    foreach (ChiTietHoaDonDaiLy ct in hoadon.ChiTiet)
                    {
                        ChiTiet.add(ct, hoadon.MaSoHoaDon);
                    }

                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
 public static int add(HoaDonDaiLy hoadon)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var hd = new HOADONDAILY
             {
                 masodaily = hoadon.MaSoDaiLy,
                 ngaylap = hoadon.NgayLap,
                 tongtien = hoadon.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia),
                 trangthai = 0
             };
             db.HOADONDAILies.InsertOnSubmit(hd);
             db.SubmitChanges();
             foreach (ChiTietHoaDonDaiLy ct in hoadon.ChiTiet)
             {
                 ChiTiet.add(ct, hd.masohoadon);
             }
             return hd.masohoadon;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
示例#3
0
        public List <ChiTietHoaDonDaiLy> getHoaDonDaiLyTheoThang(int startMonth, int startYear, int endMonth, int endYear)
        {
            DateTime startDate = new DateTime(startYear, startMonth, 1);
            DateTime endDate   = new DateTime(endYear, endMonth, 1);

            endDate = endDate.AddMonths(1).AddDays(-1);
            return(HoaDonDaiLy.Where(ct =>
                                     ct.HoaDon.NgayLap >= startDate &&
                                     ct.HoaDon.NgayLap <= endDate).ToList());
        }
        //khi Lưu lại hóa đơn
        private void btnLuu_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Bạn có muốn lưu", "Thông báo", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                List<ChiTietHoaDonDaiLy> ListChiTiet = new List<ChiTietHoaDonDaiLy>();
                HoaDonDaiLy hd = new HoaDonDaiLy();
                hd.MaSoDaiLy = int.Parse(txbMaSoDaiLy.Text.ToString());
                hd.NgayLap = DateTime.Parse(dtpNgayLap.Value.ToString("yyyy-MM-dd"));

                for (int i = 0; i < gdvChiTiet.RowCount - 1; i++)
                {
                    if (!String.IsNullOrEmpty(Convert.ToString(gdvChiTiet.Rows[i].Cells[0].Value)) && !String.IsNullOrEmpty(Convert.ToString(gdvChiTiet.Rows[i].Cells[1].Value)) && !String.IsNullOrEmpty(Convert.ToString(gdvChiTiet.Rows[i].Cells[2].Value)))
                    {
                        ChiTietHoaDonDaiLy ct = new ChiTietHoaDonDaiLy();
                        ct.MaSoSach = int.Parse(gdvChiTiet.Rows[i].Cells[0].Value.ToString());
                        ct.SoLuong = int.Parse(gdvChiTiet.Rows[i].Cells[1].Value.ToString());
                        ct.DonGia = int.Parse(gdvChiTiet.Rows[i].Cells[2].Value.ToString());
                        ListChiTiet.Add(ct);
                    }
                }
                if (ListChiTiet.Count > 1)
                {
                    for (int i = 0; i < ListChiTiet.Count - 1; i++)
                    {
                        for (int j = i + 1; j < ListChiTiet.Count; j++)
                            if (ListChiTiet[i].MaSoSach.Equals(ListChiTiet[j].MaSoSach))
                            {
                                MessageBox.Show("Không nhập trùng sách");
                                return;
                            }
                    }
                }

                hd.ChiTiet = ListChiTiet;

                int x = HoaDonDaiLyManager.add(hd);
                if (x != 0)
                {
                    MessageBox.Show("Đã thêm thành công hóa đơn đại lý");
                    txbMaHoaDon.Text = x + "";
                }
            }
            else if (dialogResult == DialogResult.No)
            {
                return;
            }
        }
        public void select(HoaDonDaiLy hd)
        {
            if (hd != null)
            {
                txbMaHD.Text = hd.MaSoHoaDon + "";
                cmbDaiLy.SelectedValue = hd.MaSoDaiLy;
                string day = hd.NgayLap.ToString();
                DateTime thedate = DateTime.Parse(day);
                String dateString = thedate.ToString("yyyy/MM/dd");
                day = dateString;

                char[] cut = day.ToCharArray();
                string nam = "";
                for (int i = 0; i < 4; i++)
                {
                    nam += cut[i];
                }
                string thang = "";
                for (int i = 5; i < 7; i++)
                {
                    thang += cut[i];
                }
                string ngay = "";
                for (int i = 8; i < 10; i++)
                {
                    ngay += cut[i];
                }
                dtpNgay.Value = new DateTime(int.Parse(nam), int.Parse(thang), int.Parse(ngay));
                int trangthai = (int)hd.TrangThai;
                if (trangthai == 1)
                {
                    btDuyet.Text = "Đã duyệt";
                    btDuyet.Enabled = false;
                    btXoa.Enabled = false;

                }
                if (trangthai == 0)
                {
                    btDuyet.Text = "Duyệt hóa đơn";
                    btDuyet.Enabled = true;
                    btXoa.Enabled = true;
                }
            }
        }
 public frmChiTietHoaDonDaiLy(Form parent, HoaDonDaiLy hd)
     : this(parent)
 {
     _CurrentHD = hd;
 }
        public ActionResult ThanhToan(HoaDonDaiLy model, FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    var result = HoaDonDaiLyManager.add(model);
                    if (result != 0)
                    {
                        _hoadon = null;
                        putSuccessMessage("Thanh toán thành công");
                        return RedirectToAction("Details", new { id = result });
                    }
                    else
                    {
                        putErrorMessage("Thanh toán không thành công");
                    }
                }
                else
                {
                    putModelStateFailErrors(ModelState);
                }
                //ViewBag.cultureInfo = CultureInfo;
                //ViewBag.currentDaiLy = _hoadon.DaiLy;
                //ViewBag.DMSach = new SelectList(_hoadon.DaiLy.getSachNo(),
                //                        nameof(SachManager.Properties.MaSoSach),
                //                        nameof(SachManager.Properties.TenSach), "");
                //_hoadon.NgayLap = DateTime.Now;

                //return View(_hoadon);
                return RedirectToAction("ThanhToan", new { masodaily = _hoadon.MaSoDaiLy });
            }
            catch(Exception ex)
            {
                putErrorMessage(ex.Message);
                return RedirectToAction("ThanhToan", new { masodaily = _hoadon.MaSoDaiLy });
            }
        }
 // GET: PhieuNhap/ThanhToan
 public ActionResult ThanhToan(int? masodaily)
 {
     if (masodaily != null)
     {
         var dl = DaiLyManager.find((int)masodaily);
         if (dl == null || dl.TrangThai == 0)
         {
             putErrorMessage("Không tìm thấy đại lý");
             return RedirectToAction("ThanhToan");
         }
         ViewBag.cultureInfo = CultureInfo;
         ViewBag.currentDaiLy = dl;
         ViewBag.DMSach = new SelectList(dl.getSachNo(),
                                 nameof(SachManager.Properties.MaSoSach),
                                 nameof(SachManager.Properties.TenSach), "");
         if (_hoadon == null)
         {
             _hoadon = new HoaDonDaiLy();
         }
         _hoadon.MaSoDaiLy = dl.MaSoDaiLy;
         _hoadon.DaiLy = dl;
         _hoadon.NgayLap = DateTime.Now;
         setAlertMessage();
         return View(_hoadon);
     }
     else
     {
         ViewBag.DMDaiLy = new SelectList(DaiLyManager.getAllAlive()
                                         .Where(dl => dl.TongTienNo > 0).ToList(),
                                 nameof(DaiLyManager.Properties.MaSoDaiLy),
                                 nameof(DaiLyManager.Properties.TenDaiLy), "");
         _hoadon = new HoaDonDaiLy();
         setAlertMessage();
         return View();
     }
 }
        public ActionResult Edit(HoaDonDaiLy model, FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (HoaDonDaiLyManager.edit(model))
                    {
                        _currentHoaDon = null;
                        putSuccessMessage("Cập nhật thành công");
                        return RedirectToAction("Details", new { id = model.MaSoHoaDon });
                    }
                    else {
                        putErrorMessage("Cập nhật thất bại");
                    }

                }
                else
                {
                    putModelStateFailErrors(ModelState);
                }
                // TODO: Add update logic here
                //_hoadon = model;
                //ViewBag.currentDaiLy = _hoadon.DaiLy;
                //ViewBag.DMSach = new SelectList(_hoadon.DaiLy.getSachNo(),
                //                        nameof(SachManager.Properties.MaSoSach),
                //                        nameof(SachManager.Properties.TenSach), "");
                //return View(_hoadon);
                return RedirectToAction("Edit", new { id = model.MaSoHoaDon });
            }
            catch(Exception ex)
            {
                putErrorMessage(ex.Message);
                return RedirectToAction("Edit", new { id = model.MaSoHoaDon });
            }
        }
 // GET: PhieuNhap/Edit/5
 public ActionResult Edit(int? id)
 {
     if (id == null)
     {
         putErrorMessage("Đường dẫn không chính xác");
         return RedirectToAction("All");
     }
     if (_currentHoaDon == null || _currentHoaDon != id)
     {
         _currentHoaDon = id;
         _hoadon = HoaDonDaiLyManager.find((int)id);
         if (_hoadon == null)
         {
             putErrorMessage("Không tìm thấy");
             return RedirectToAction("All");
         }
         if (_hoadon.TrangThai == 1)
         {
             //Nếu đã duyệt thì không cho sửa, chuyển sang trang chi tiết
             _currentHoaDon = null;
             putErrorMessage("Hóa đơn đã duyệt");
             return RedirectToAction("Details", new { id = id });
         }
     }
     ViewBag.cultureInfo = CultureInfo;
     ViewBag.currentDaiLy = _hoadon.DaiLy;
     ViewBag.DMSach = new SelectList(_hoadon.DaiLy.getSachNo(),
                                 nameof(SachManager.Properties.MaSoSach),
                                 nameof(SachManager.Properties.TenSach), "");
     setAlertMessage();
     return View(_hoadon);
 }
 private void gdvHoaDon_SelectionChanged(object sender, EventArgs e)
 {
     int index = ((DataGridView)sender).CurrentRow.Index;
     _CurrentHD = (((DataGridView)sender).DataSource as List<HoaDonDaiLy>)[index];
     select(_CurrentHD);
 }