public partial class MainWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } public MainWindow() { InitializeComponent(); QModernMessageBox.MainLang = new QMetroMessageLang() { Ok = "Ok", Cancel = "Cancel", Abort = "Abort", Ignore = "Ignore", No = "No", Yes = "Yes", Retry = "Retry" }; QModernMessageBox.GlobalBackground = new SolidColorBrush(Colors.Black) { Opacity = 0.6 }; QModernMessageBox.GlobalForeground = System.Windows.Media.Brushes.White; this.DataContext = new BillsViewModel(); } public void Maximize() { if (WindowState == WindowState.Maximized) { WindowState = WindowState.Normal; } else WindowState = WindowState.Maximized; } public void Minimize() { WindowState = WindowState.Minimized; } public void DoSelectedRow(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null && !cell.IsEditing) { DataGridRow row = FindVisualParent(cell); if (row != null) { row.IsSelected = !row.IsSelected; e.Handled = true; } } } private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e) { var comboboxSelectedItem = ((ComboBoxItem)cb.SelectedValue).Content; switch (comboboxSelectedItem) { case "2020": DatabaseLayer.dbloc = @"Data Source=2020.db"; ((BillsViewModel)this.DataContext).SetProperties(); OnPropertyChanged(string.Empty); break; case "2021": DatabaseLayer.dbloc = @"Data Source=2021.db"; ((BillsViewModel)this.DataContext).SetProperties(); OnPropertyChanged(string.Empty); break; default: DatabaseLayer.dbloc = @"Data Source=2022.db"; ((BillsViewModel)this.DataContext).SetProperties(); OnPropertyChanged(string.Empty); break; } } public static Parent FindVisualParent(DependencyObject child) where Parent : DependencyObject { DependencyObject parentObject = child; while (!((parentObject is System.Windows.Media.Visual) || (parentObject is System.Windows.Media.Media3D.Visual3D))) { if (parentObject is Parent || parentObject == null) { return parentObject as Parent; } else { parentObject = (parentObject as FrameworkContentElement).Parent; } } parentObject = VisualTreeHelper.GetParent(parentObject); if (parentObject is Parent || parentObject == null) { return parentObject as Parent; } else { return FindVisualParent(parentObject); } } void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DragMove(); } } public class BillsViewModel : ViewModelBase { public string dtm { get; set; } public ValidInput SelectedValidInput { get; private set; } public ICommand SelectedValidInputCommand { get; private set; } private string _SelecedItme; public string SelecedItme { get { return _SelecedItme; } set { SetValue(ref _SelecedItme, value); } } public int Cnt { get { return AllBills.OfType().ToList().Count(x => String.IsNullOrEmpty(x.Remarks)); } } public int Cnt2 { get { return AllBills.OfType().ToList().Where(a => !String.IsNullOrEmpty(a.Remarks)).Count(x => String.IsNullOrEmpty(x.PaidOn)); } } public int Cnt3 { get { return AllCNDNs.OfType().ToList().Count(x => String.IsNullOrEmpty(x.UsedOn)); } } private ObservableCollection _allBills; public ObservableCollection AllBillss { get { return _allBills; } set { _allBills = value; SetValue(ref _allBills, value); } } private ObservableCollection _adjBillss; public ObservableCollection AdjBillss { get { return _adjBillss; } set { _adjBillss = value; SetValue(ref _adjBillss, value); } } private ICollectionView pendingBills; public ICollectionView PendingBills { get { return pendingBills; } set { pendingBills = value; SetValue(ref pendingBills, value); } } private ICollectionView _allBillsCollection; public ICollectionView AllBills { get { return _allBillsCollection; } set { _allBillsCollection = value; OnPropertyChanged("AllBills"); } } private ICollectionView _allCNDNs; public ICollectionView AllCNDNs { get { return _allCNDNs; } set { _allCNDNs = value; OnPropertyChanged("AllCNDNs"); } } private ObservableCollection comboItems; public ObservableCollection ComboItems { get { return comboItems; } set { comboItems = value; OnPropertyChanged("ComboItems"); } } private ObservableCollection comboItems2; public ObservableCollection ComboItems2 { get { return comboItems2; } set { comboItems2 = value; OnPropertyChanged("ComboItems2"); } } private ObservableCollection comboItems3; public ObservableCollection ComboItems3 { get { return comboItems3; } set { comboItems3 = value; OnPropertyChanged("ComboItems3"); } } private string _SelectedCBItem; public string SelectedCBItem { get { return _SelectedCBItem; } set { SetValue(ref _SelectedCBItem, value); } } private string _SelectedCBItem2; public string SelectedCBItem2 { get { return _SelectedCBItem2; } set { FilterString = null; SetValue(ref _SelectedCBItem2, value); AllBills.Refresh(); OnPropertyChanged("Cnt"); OnPropertyChanged("Cnt2"); PendingBills.Refresh(); } } private string _SelectedCBItem3; public string SelectedCBItem3 { get { return _SelectedCBItem3; } set { FilterString = null; SetValue(ref _SelectedCBItem3, value); AllCNDNs.Refresh(); OnPropertyChanged("Cnt"); OnPropertyChanged("Cnt2"); OnPropertyChanged("Cnt3"); } } private string _filterString; public string FilterString { get { return _filterString; } set { _filterString = value; SetValue(ref _filterString, value); } } BillsBusinessObject bills; private ObservableCollection _Bill; public ObservableCollection Bill { get { _Bill = new ObservableCollection(bills.GetBills()); return _Bill; } } private ObservableCollection _AdjustmentBill; public ObservableCollection AdjustmentBill { get { _AdjustmentBill = new ObservableCollection(bills.GetAdjustmentBills()); return _AdjustmentBill; } } public int SelectedIndex { get; set; } object _SelectedInv; public object SelectedInv { get { return _SelectedInv; } set { if (_SelectedInv != value) { _SelectedInv = value; OnPropertyChanged("SelectedInv"); } } } public int SelectedIndex2 { get; set; } object _SelectedAdj; public object SelectedAdj { get { return _SelectedAdj; } set { if (_SelectedAdj != value) { _SelectedAdj = value; OnPropertyChanged("SelectedAdj"); } } } object _SelectedFilter; public object SelectedFilter { get { return _SelectedFilter; } set { if (_SelectedFilter != value) { _SelectedFilter = value; OnPropertyChanged("SelectedFilter"); } } } private BindingGroup _UpdateBindingGroup; public BindingGroup UpdateBindingGroup { get { return _UpdateBindingGroup; } set { if (_UpdateBindingGroup != value) { _UpdateBindingGroup = value; OnPropertyChanged("UpdateBindingGroup"); } } } private string _myInfo; public string myInfo { get { return _myInfo; } set { SetValue(ref _myInfo, value); } } private string _CityName; public string CityName { get { return _CityName; } set { SetValue(ref _CityName, value); } } public RelayCommand GoButtonClicked { get; set; } public RelayCommand SearchButtonClicked { get; set; } public RelayCommand PreviewButtonClicked { get; set; } public RelayCommand DocxButtonClicked { get; set; } public RelayCommand SelButtonClicked { get; set; } public RelayCommand SelectedButtonClicked { get; set; } public ICollectionView FilteredBills { get; private set; } public ICollectionView AdjBills { get; private set; } public SelectOptions SOptions { get; set; } MainWindow window2 = Application.Current.Windows .Cast() .FirstOrDefault(window => window is MainWindow) as MainWindow; public BillsViewModel() { SetProperties(); } internal void SetProperties() { this.SelectedValidInputCommand = new RelayCommand(ExecuteSelectCurrencyCommand, commandParameter => true); CultureInfo culture = new CultureInfo("en-IN"); SOptions = new SelectOptions(); AllBillss = DatabaseLayer.GetAllBillsFromDB(); AdjBillss = DatabaseLayer.GetAllAdjBillsFromDB(); comboItems = new ObservableCollection(AllBillss.Select(b => b.Party).Distinct().OrderBy(b => b).ToList()); comboItems2 = new ObservableCollection(AllBillss.Select(b => b.Party).Distinct().OrderBy(b => b).ToList()); PendingBills = new ListCollectionView(AllBillss) { Filter = o => String.IsNullOrEmpty(((Bills)o).Remarks) && (DateTime.ParseExact(((Bills)o).DueDt, "yyyy-MM-dd", culture) >= DateTime.Today.AddDays(-10) && DateTime.ParseExact(((Bills)o).DueDt, "yyyy-MM-dd", culture) < DateTime.Today.AddDays(11) ) }; AllBills = new ListCollectionView(AllBillss) { Filter = o => ((Bills)o).Party == SelectedCBItem2 }; AllCNDNs = new ListCollectionView(AdjBillss) { Filter = o => ((Bills)o).Vendor == SelectedCBItem3 }; GoButtonClicked = new RelayCommand(GoFilterData, CanGoFilterData); bills = new BillsBusinessObject(); bills.BillChanged += new EventHandler(bills_BillChanged); UpdateBindingGroup = new BindingGroup { Name = "Group1" }; CancelCommand = new RelayCommand(DoCancel); SaveCommand = new RelayCommand(DoSave); AddCommand = new RelayCommand(AddUser); DeleteUserCommand = new RelayCommand(DeleteUser); SearchButtonClicked = new RelayCommand(ComboFilterData); DocxButtonClicked = new RelayCommand(CreateDoc, CanCreateDoc); SelButtonClicked = new RelayCommand(SelUnsel); SelectedButtonClicked = new RelayCommand(SelUnselAdj); FilteredBills = new ListCollectionView(AllBillss) { Filter = o => ((Bills)o).Party == SelectedCBItem && String.IsNullOrEmpty(((Bills)o).Remarks) }; AdjBills = new ListCollectionView(AdjBillss) { Filter = o => ((Bills)o).Vendor == SelectedCBItem && String.IsNullOrEmpty(((Bills)o).UsedOn) }; } public void SelUnsel(object param) { IList list = (IList)param; if (list.Count > 0) { list.Clear(); } else { foreach (var item in FilteredBills) { list.Add(item); } } } public void SelUnselAdj(object param) { IList list = (IList)param; if (list.Count > 0) { list.Clear(); } else { foreach (var item in AdjBills) list.Add(item); } } public void ComboFilterData(object param) { FilteredBills.Refresh(); AdjBills.Refresh(); Dictionary dict = new Dictionary(); using (var connection = new SQLiteConnection(DatabaseLayer.dbloc)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = "SELECT * FROM supps"; using (var reader = command.ExecuteReader()) { while (reader.Read()) { var Name = reader.GetString(1); var City = reader.GetString(2); if (!dict.ContainsKey(Name)) { dict.Add(Name, City); } } } } bool keyExists = dict.ContainsKey(SelectedCBItem); if (keyExists) { CityName = dict[SelectedCBItem]; } else { CityName = ""; } if (FilteredBills.IsEmpty) { myInfo = @"No unprocessed """ + SelectedCBItem + @""" bills found!"; } else { myInfo = SelectedCBItem; } SelectedCBItem = null; } public void GoFilterData(object param) { AllBills.Filter = FilterTask; OnPropertyChanged("Cnt"); OnPropertyChanged("Cnt2"); } private void ExecuteSelectCurrencyCommand(object commandParameter) { this.SelectedValidInput = (ValidInput)commandParameter; } private bool CanGoFilterData(object commandParameter) { DateTime validDate; Decimal k; switch (this.SelectedValidInput) { case ValidInput.ValidParty: return !string.IsNullOrEmpty(this.FilterString) && !string.IsNullOrEmpty(this.SelectedCBItem2); case ValidInput.ValidDueDt: return DateTime.TryParseExact(FilterString, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out validDate) && !string.IsNullOrEmpty(this.SelectedCBItem2); case ValidInput.ValidAmt: return Decimal.TryParse(FilterString, out k) && !string.IsNullOrEmpty(this.SelectedCBItem2); default: return false; } } public bool CanCreateDoc(object param) { if (!string.IsNullOrEmpty(this.SelecedItme)) { return true; } return false; } public void CreateDoc(object param) { string fileName = @"E:\WPF.docx"; if (File.Exists(fileName)) { if (IsFileLocked(new FileInfo(fileName))) { QModernMessageBox.Error("Please close the following file :\n" + fileName, "File Already Open"); return; } } if (string.IsNullOrEmpty(CityName)) { QModernMessageBox.Error("City name of vendor not available, please update it in the database file", "City Name Missing"); return; } var billsGrid = window2.billsGrid; var adjGrid = window2.adjGrid; if (adjGrid.SelectedItems.Count > 0) { if (adjGrid.SelectedItems.Count > 3) { QModernMessageBox.Error("Please select 3 or less Credit/Debit notes", "Invalid Selection"); return; } if (billsGrid.SelectedItems.Count > 0 && billsGrid.SelectedItems.Count > 8) { QModernMessageBox.Error("Please select atleast 1 bill and/or maximum 8 bills", "Invalid Selection"); return; } else { if (billsGrid.SelectedItems.Count < 1) { QModernMessageBox.Error("Please select atleast 1 bill", "Invalid Selection"); return; } if (adjGrid.SumSelectedAdjustments() > billsGrid.SumSelectedBills()) { QModernMessageBox.Error("Credit/Debit note total value cannot be higher than total bill value", "Invalid Payment Amount"); return; } //DocXCreator.Modify_DocX(fileName, myInfo, billsGrid, adjGrid); List selBills = billsGrid.SelectedItems.OfType().ToList(); foreach (var pair in selBills) { Bills b = pair as Bills; b.Id = pair.Id; b.Remarks = @"Sent to A/c. on " + DateTime.Today.ToString(@"dd.MM.yyyy"); bills.UpdateBillDataGrid(b); } List selAdjs = adjGrid.SelectedItems.OfType().ToList(); foreach (var pair in selAdjs) { Bills b = pair as Bills; b.AId = pair.AId; b.UsedOn = DateTime.Today.ToString("dd-MM-yyyy"); bills.UpdateBillAdg(b); } OnPropertyChanged("Bill"); OnPropertyChanged("AllBillss"); OnPropertyChanged("AdjBillss"); OnPropertyChanged("Cnt"); OnPropertyChanged("Cnt2"); OnPropertyChanged("Cnt3"); QModernMessageBox.Done("Done", "Process Completed"); AllBills.Refresh(); PendingBills.Refresh(); FilteredBills.Refresh(); AdjBills.Refresh(); myInfo = ""; } } else { if (billsGrid.SelectedItems.Count < 1 || billsGrid.SelectedItems.Count > 15) { QModernMessageBox.Error("Please select minimum 1 bill and/or maximum 15 bills", "Invalid Selection"); } else { // DocXCreator.Modify_DocX(fileName, myInfo, billsGrid, adjGrid); List selBills = billsGrid.SelectedItems.OfType().ToList(); foreach (var pair in selBills) { Bills b = pair as Bills; b.Id = pair.Id; b.Remarks = @"Sent to A/c. on " + DateTime.Today.ToString(@"dd.MM.yyyy"); bills.UpdateBillDataGrid(b); } OnPropertyChanged("Bill"); OnPropertyChanged("AllBillss"); OnPropertyChanged("AdjBillss"); OnPropertyChanged("Cnt"); OnPropertyChanged("Cnt2"); OnPropertyChanged("Cnt3"); QModernMessageBox.Done("Done", "Process Completed"); AllBills.Refresh(); PendingBills.Refresh(); FilteredBills.Refresh(); AdjBills.Refresh(); myInfo = ""; } } } public bool FilterTask(object value) { bool f; var entry = value as Bills; if (entry != null) { if (!string.IsNullOrEmpty(FilterString)) { switch (SOptions.EnumProperty) { case Options.BillNo: f = entry.Party == SelectedCBItem2 && entry.BillNo.Contains(FilterString); break; case Options.BillDt: f = entry.Party == SelectedCBItem2 && entry.BillDt == ConvDate(FilterString); break; case Options.Amt: double temp = Double.Parse(FilterString, CultureInfo.InvariantCulture); string result = temp.ToString("0.00", CultureInfo.InvariantCulture); f = entry.Party == SelectedCBItem2 && entry.Amt == result; break; default: f = entry.Party == SelectedCBItem2; break; } return f; } else { f = entry.Party == SelectedCBItem2; return f; } } return false; } public static string ConvDate(string s) { string[] authorsList = s.Split('-'); string x = authorsList[2] + "-" + authorsList[1] + "-" + authorsList[0]; return x; } void bills_BillChanged(object sender, EventArgs e) { Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { OnPropertyChanged("Bill"); OnPropertyChanged("AllBillss"); OnPropertyChanged("AdjBillss"); OnPropertyChanged("PendingBills"); })); } public RelayCommand CancelCommand { get; set; } public RelayCommand SaveCommand { get; set; } public RelayCommand AddCommand { get; set; } public RelayCommand DeleteUserCommand { get; set; } void DoCancel(object param) { UpdateBindingGroup.CancelEdit(); if (SelectedIndex == -1) SelectedInv = null; } void DoSave(object param) { if (QModernMessageBox.Show("Are you sure you want to save this ?", "Confirmation", QModernMessageBox.QModernMessageBoxButtons.YesNo) == ModernMessageboxResult.Button1) { UpdateBindingGroup.CommitEdit(); var bill = SelectedInv as Bills; if (SelectedIndex == -1) { bills.AddBill(bill); AllBillss.Add(bill); if (!ComboItems2.Select(a => a).Distinct().ToList().Contains(bill.Party)) { ComboItems.Add(bill.Party.ToString()); ComboItems = new ObservableCollection(ComboItems.OrderBy(i => i)); ComboItems2.Add(bill.Party.ToString()); ComboItems2 = new ObservableCollection(ComboItems2.OrderBy(i => i)); OnPropertyChanged("ComboItems"); OnPropertyChanged("ComboItems2"); } QModernMessageBox.Done("Data saved!", "Process Completed"); } else { bills.UpdateBill(bill); QModernMessageBox.Done("Data edited!", "Process Completed"); } SelectedInv = null; OnPropertyChanged("Bill"); OnPropertyChanged("AllBillss"); OnPropertyChanged("Cnt"); OnPropertyChanged("Cnt2"); OnPropertyChanged("Cnt3"); AllBills.Refresh(); PendingBills.Refresh(); FilteredBills.Refresh(); } } void AddUser(object param) { SelectedInv = null; var bill = new Bills(); bill.BillDt = DateTime.Today.ToString("dd-MM-yyyy"); bill.DueDt = DateTime.Today.ToString("dd-MM-yyyy"); SelectedInv = bill; } void DeleteUser(object parameter) { if (QModernMessageBox.Show("Are you sure you want to delete this record ?", "Confirmation", QModernMessageBox.QModernMessageBoxButtons.YesNo) == ModernMessageboxResult.Button1) { var bill = SelectedInv as Bills; if (SelectedIndex != -1) { bills.DeleteBill(bill); AllBillss.Remove(bill); if (!AllBillss.Select(a => a.Party).Distinct().ToList().Contains(bill.Party)) { ComboItems.Remove(bill.Party.ToString()); ComboItems2.Remove(bill.Party.ToString()); OnPropertyChanged("ComboItems"); OnPropertyChanged("ComboItems2"); OnPropertyChanged("myInfo"); } QModernMessageBox.Done("Data deleted successfully!", "Process Completed"); } else { SelectedInv = null; } OnPropertyChanged("Bill"); OnPropertyChanged("AllBillss"); AllBills.Refresh(); PendingBills.Refresh(); FilteredBills.Refresh(); } } protected virtual bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } }