public Boolean delete(DonHang obj)
        {
            try
            {
                //get entity
                obj = this.get_by_id(obj.id);

                if (obj.dagiaohang) return false;
                //remove relations
                while (obj.ds_chitiet_donhang.Count > 0)
                {
                    ChiTiet_DonHang ctdh = obj.ds_chitiet_donhang[0];
                    ctdh._set_context(_db);
                    // set lai so luong va ton kho
                    ctdh.sanpham_chitiet.update_tonkho(ctdh.sanpham_chitiet.tonkho + ctdh.soluong, DateTime.Now);

                    ctdh.delete();
                }

                //remove
                this._db.ds_donhang.Remove(obj);
                //commit
                return this.save();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                return false;
            }
        }
 public int add(DonHang obj)
 {
     //tinh toán tong tiền
         int sum = 0;
         foreach (var item in obj.ds_chitiet_donhang)
         {
             item.dongia = item.sanpham_chitiet.sanpham.gia;//for sure
             sum += item.dongia * item.soluong;
             //cập nhật tồn kho trên sản phẩm
                 item.sanpham_chitiet.tonkho -= item.soluong;
                 if (item.sanpham_chitiet.tonkho < 0)
                 {
                     item.sanpham_chitiet.tonkho = 0;
                 }
                 //thêm log tồn kho
                     TonKho tk = new TonKho();
                     tk.ngay = obj.ngay;
                     tk.soluong = item.sanpham_chitiet.tonkho;
                     item.sanpham_chitiet.ds_tonkho.Add(tk);
         }
         obj.tongtien = sum;
     //call add
     this._db.ds_donhang.Add(obj);
     //commit
     this.save();
     //return ma moi nhat
     return this._db.ds_donhang.Max(x => x.id);
 }
        public FmThongTinDonHang(DonHang dh = null)
        {
            InitializeComponent();
            dtgvChiTietNH.AutoGenerateColumns = false;

            if (dh == null) this.dh = new DonHang();
            else
            {
                this.dh = dh;
                ThongTinFormDonHang = this.dh;
                LoadDTGV_ChiTietDonHang();
            }
        }
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     base.OnActionExecuting(filterContext);
     ViewBag.Title = "Cửa hàng DTDD";
     //tim kiem
     //build timkiem_nhanvien
     if (Request.Cookies.Get("timkiem_sanpham") == null)
     {
         //chưa set cookies trước => tiến hành set cookies
         this._khoitao_timkiem_cookie();
         Response.Cookies.Add(CookieLibrary.Base64Encode(this._timkiem_sanpham));
     }
     else
     {
         try
         {
             this._timkiem_sanpham = CookieLibrary.Base64Decode(Request.Cookies.Get("timkiem_sanpham"));
         }
         catch (Exception)
         {
             this._khoitao_timkiem_cookie();
             Response.Cookies.Add(CookieLibrary.Base64Encode(this._timkiem_sanpham));
         }
     }
     ViewBag.timkiem_sanpham = this._timkiem_sanpham;
     HangSXController ctr = new HangSXController();
     ViewBag.HangSX_List = ctr.timkiem("", "", "1");
     //CART section
         try
         {
             if (Session["giohang"] != null)
             {
                 this._giohang = (DonHang)Session["giohang"];
             }
             else
             {
                 this._giohang = new DonHang();
             }
         }
         catch (Exception)
         {
             this._giohang = new DonHang();
         }
         //set session obj
         this._save_cart_to_session();
         ViewBag.giohang = this._giohang;
 }
Esempio n. 5
0
        private DonHang _Clone()
        {
            DonHang obj = new DonHang();
            obj.id = this.id;
            obj.kh_diachi = this.kh_diachi;
            obj.kh_email = this.kh_email;
            obj.kh_sdt = this.kh_sdt;
            obj.kh_ten = this.kh_ten;
            obj.ngay = this.ngay;
            obj.tongtien = this.tongtien;
            obj.active = this.active;
            obj.dagiaohang = this.dagiaohang;

            ChiTiet_DonHang tmp;
            SanPham_ChiTietController ctr_ = new SanPham_ChiTietController(this._ctr._db);
            foreach (var item in this.ds_chitiet_donhang)
            {
                tmp = new ChiTiet_DonHang();
                tmp.dongia = item.dongia;
                tmp.id = item.id;
                tmp.soluong = item.soluong;
                tmp.sanpham_chitiet = ctr_.get_by_id(item.sanpham_chitiet.id);
                obj.ds_chitiet_donhang.Add(tmp);
            }
            return obj;
        }
 public FrontController()
 {
     this._giohang = new DonHang();
     this._khoitao_timkiem_cookie();
 }
 public List<string> validate(DonHang obj)
 {
     List<string> re = new List<string>();
     if (obj.ds_chitiet_donhang.Count == 0)
     {
         re.Add("rong_fail");
     }
     if (obj.kh_diachi.Equals(""))
     {
         re.Add("kh_diachi_fail");
     }
     if (obj.kh_ten.Equals(""))
     {
         re.Add("kh_ten_fail");
     }
     if (obj.kh_email.Equals("") || !ValidateLibrary.is_valid_email(obj.kh_email))
     {
         re.Add("kh_email_fail");
     }
     if (obj.kh_sdt.Equals(""))
     {
         re.Add("kh_sdt_fail");
     }
     //xét tồn kho
     foreach (var item in obj.ds_chitiet_donhang)
     {
         if (item.sanpham_chitiet.tonkho < item.soluong || item.soluong<=0)
         {
             re.Add(item.sanpham_chitiet.id+"_soluong_fail");
         }
     }
     return re;
 }
 public void reload(DonHang dh)
 {
     foreach (ChiTiet_DonHang ctdh in dh.ds_chitiet_donhang)
     {
         try
         {
             _db.Entry<SanPham_ChiTiet>(ctdh.sanpham_chitiet).Reload();
         }
         catch (Exception) { continue; }
     }
 }