private void PrintMethod() { try { using (var unitOfWork = new UnitOfWork(new GeneralDBContext())) { unitOfWork.Bills.Edit(_selectedBill); unitOfWork.Safes.Remove(d => d.RegistrationDate == _selectedBill.RegistrationDate); Safe safe = new Safe { Amount = _selectedBill.Total, CanDelete = false, RegistrationDate = _selectedBill.RegistrationDate, Type = true, Statement = $"فاتورة {_selectedBill.ID}", UserID = (int)_selectedBill.UserID }; unitOfWork.Safes.Add(safe); unitOfWork.BillsItems.Remove(s => s.BillID == BillID); foreach (var item in BillItems) { unitOfWork.BillsItems.Add(new BillItem { BillID = BillID, ItemID = item.ItemID, Price = item.Item.Price, Qty = item.Qty, Total = item.Qty * item.Item.Price }); } unitOfWork.Complete(); Mouse.OverrideCursor = Cursors.Wait; int rnd = new Random().Next(1000, 9999); List <int?> categoriesId = _billItems.Select(s => s.Item.CategoryID).Distinct().ToList(); foreach (var categoryId in categoriesId) { DS ds = new DS(); ds.Bill.Rows.Clear(); int i = 0; foreach (var item in BillItems.Where(w => w.Item.CategoryID == categoryId)) { ds.Bill.Rows.Add(); ds.Bill[i]["BillID"] = $"#{rnd}#{_selectedBill.ID}#"; ds.Bill[i]["Date"] = DateTime.Now.ToShortDateString(); ds.Bill[i]["Time"] = DateTime.Now.ToString(" h:mm tt"); ds.Bill[i]["Type"] = _selectedBill.Type; ds.Bill[i]["Details"] = _selectedBill.Details; ds.Bill[i]["ItemQty"] = item.Qty; ds.Bill[i]["ItemName"] = item.Item.Name; ds.Bill[i]["ItemPrice"] = string.Format("{0:0.00}", item.Item.Price);; ds.Bill[i]["BillTotal"] = string.Format("{0:0.00}", Math.Round(Convert.ToDecimal(BillItems.Where(w => w.Item.CategoryID == categoryId).Sum(s => s.Total)), 0)); i++; } BillItemsReport billItemsReport = new BillItemsReport(); billItemsReport.SetDataSource(ds.Tables["Bill"]); Mouse.OverrideCursor = null; billItemsReport.PrintToPrinter(1, false, 0, 15); } currentWindow.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { Mouse.OverrideCursor = null; } }
private void PrintMethod() { try { if (BillItems.Count == 0) { return; } using (var unitOfWork = new UnitOfWork(new GeneralDBContext())) { DateTime dt = DateTime.Now; _newBill.Date = dt; _newBill.RegistrationDate = dt; _newBill.UserID = UserData.ID; _newBill = unitOfWork.Bills.Add(_newBill); foreach (var item in BillItems) { unitOfWork.BillsItems.Add(new BillItem { BillID = _newBill.ID, ItemID = item.ItemID, Price = item.Item.Price, Qty = item.Qty, Total = item.Qty * item.Item.Price }); } unitOfWork.Complete(); Safe safe = new Safe { Amount = _newBill.Total, CanDelete = false, RegistrationDate = dt, Type = true, UserID = UserData.ID, Statement = $"فاتورة {_newBill.ID}" }; unitOfWork.Safes.Add(safe); unitOfWork.Complete(); Mouse.OverrideCursor = Cursors.Wait; int rnd = new Random().Next(1000, 9999); List <int?> categoriesId = _billItems.Select(s => s.Item.CategoryID).Distinct().ToList(); foreach (var categoryId in categoriesId) { DS ds = new DS(); ds.Bill.Rows.Clear(); int i = 0; foreach (var item in BillItems.Where(w => w.Item.CategoryID == categoryId)) { ds.Bill.Rows.Add(); ds.Bill[i]["BillID"] = $"#{rnd}#{_newBill.ID}#"; ds.Bill[i]["Date"] = DateTime.Now.ToShortDateString(); ds.Bill[i]["Time"] = DateTime.Now.ToString(" h:mm tt"); ds.Bill[i]["Type"] = _newBill.Type; ds.Bill[i]["Details"] = _newBill.Details; ds.Bill[i]["ItemQty"] = item.Qty; ds.Bill[i]["ItemName"] = item.Item.Name; ds.Bill[i]["ItemPrice"] = string.Format("{0:0.00}", item.Item.Price);; ds.Bill[i]["BillTotal"] = string.Format("{0:0.00}", Math.Round(Convert.ToDecimal(BillItems.Where(w => w.Item.CategoryID == categoryId).Sum(s => s.Total)), 0)); i++; } //ReportWindow rpt = new ReportWindow(); BillItemsReport billItemsReport = new BillItemsReport(); billItemsReport.SetDataSource(ds.Tables["Bill"]); Mouse.OverrideCursor = null; billItemsReport.PrintToPrinter(1, false, 0, 15); //rpt.crv.ViewerCore.ReportSource = billItemsReport; //rpt.ShowDialog(); } BillItems = new ObservableCollection <BillItemDisplayDataModel>(); NewBill = new Bill { Type = "تيك اواى" }; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { Mouse.OverrideCursor = null; } }