public VMs() { DataClasses1DataContext dc = new DataClasses1DataContext(Properties.Settings.Default.ManagementProjectConnectionString); List <DiscountDb> modelsDb = dc.DiscountDbs.ToList(); models = new List <ViewModel>(); int n = modelsDb.Count; for (int i = 0; i < n; i++) { DiscountDb model = modelsDb[i]; CategoryDb product = (from p in dc.CategoryDbs where p.id == model.idProduct select p).Single(); String nameProduct = product.nameType; Score_TypeCustomer typeCustomer = (from p in dc.Score_TypeCustomers where p.id == model.idTypeCustomer select p).Single(); string nameTypeProduct = typeCustomer.nameTypeCustomer; if (DateTime.Compare(model.endDate, DateTime.Now) < 0) //discount is out of date { modelsDb[i].statusDiscount = 0; dc.SubmitChanges(); } string status = modelsDb[i].statusDiscount == 1 ? "Đang áp dụng" : "Hết hạn"; int order = i + 1; if (nameProduct != null && nameProduct != null) { ViewModel newItem = new ViewModel(model, nameProduct, nameTypeProduct, status, order); models.Add(newItem); } } }
private void btnAddDiscount_Click(object sender, RoutedEventArgs e) { if (checkInput()) { string nameDiscountInput = nameDiscountTxt.Text; DateTime startDateInput = (DateTime)startDate.SelectedDate; DateTime endDateInput = (DateTime)endDate.SelectedDate; CategoryDb productInput = comboBoxTypeProduct.SelectedValue as CategoryDb; Score_TypeCustomer customerInput = comboBoxTypeCustomer.SelectedValue as Score_TypeCustomer; int percentageInput = Convert.ToInt32(percentDiscountTxt.Text); DiscountDb newDiscount = new DiscountDb(nameDiscountInput, productInput.id, customerInput.id, 1, startDateInput, endDateInput, percentageInput); dc.DiscountDbs.InsertOnSubmit(newDiscount); try { dc.SubmitChanges(); reloadData(); } catch (Exception ex) { MessageBox.Show("Lỗi", ex.Message); } dialogDiscount.IsOpen = false; } }
public ViewModel(DiscountDb model, string product, string typeCustomer, string status, int order) : base(model) { this.nameProduct = product; this.nameTypeCustomer = typeCustomer; this.status = status; this.order = order; this.discount = model.percentageDiscount.ToString() + "%"; startShortDate = model.startDate.ToShortDateString(); endShortDate = model.endDate.ToShortDateString(); }
public DiscountDb(DiscountDb model) { this.nameDiscount = model.nameDiscount; this.idProduct = model.idProduct; this.idTypeCustomer = model.idTypeCustomer; this.statusDiscount = model.statusDiscount; this.startDate = model.startDate; this.endDate = model.endDate; this.percentageDiscount = model.percentageDiscount; this.id = model.id; }