private void outputDocGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { Cursor = Cursors.WaitCursor; using (var db = new PastryShopDbContext()) { var senderGrid = (DataGridView)sender; if (senderGrid.Columns[e.ColumnIndex].Name == "show" && senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && !senderGrid.Rows[e.RowIndex].IsNewRow) { var docId = int.Parse(senderGrid.Rows[e.RowIndex].Cells["id"].Value.ToString()); var outputDoc = db.OutputDocuments .Include("Lines.Dessert") .Include("Lines.OutputDocLineProducts.Product.ProductDetail") .FirstOrDefault(d => d.Id == docId); if (outputDoc != null) { PdfMaker pdfMaker = new PdfMaker(); DocumentModel docModel = DocumentModelMapper.CreateDocumentModel(outputDoc); pdfMaker.CreatePdfDocumet(docModel); } } } Cursor = Cursors.Default; }
public static ProductDetail ReadValues(TextBox name, TextBox unit, bool saveInDB) { string productName = (name.Text != null) ? name.Text.Trim() : String.Empty; string productUnit = (unit.Text != null) ? unit.Text.Trim() : String.Empty; bool areAllRequiredFieldsFilled = productName != String.Empty && productUnit != String.Empty; if (areAllRequiredFieldsFilled) { ProductDetail productDetail = new ProductDetail(); productDetail.Name = productName; productDetail.Unit = productUnit; if (saveInDB) { using (var db = new PastryShopDbContext()) { db.ProductDetails.Add(productDetail); db.SaveChanges(); } } return(productDetail); } return(null); }
public static Provider ReadValues(TextBox name, bool saveInDB) { string providerName = (name.Text != null) ? name.Text.Trim() : String.Empty; bool areAllRequiredFieldsFilled = providerName != String.Empty; if (areAllRequiredFieldsFilled) { Provider provider = new Provider(); provider.Name = providerName; if (saveInDB) { using (var db = new PastryShopDbContext()) { db.Providers.Add(provider); db.SaveChanges(); } } return(provider); } return(null); }
private void LoadProviders() { using (var db = new PastryShopDbContext()) { List <string> providerNames = db.Providers.Select(p => p.Name).ToList <string>(); foreach (var providerName in providerNames) { int newRowIndex = this.dgwProviders.Rows.Add(); DataGridViewRow row = this.dgwProviders.Rows[newRowIndex]; row.Cells["ProviderName"].Value = providerName; } } }
private void ReloadDataGrid() { DataGridViewComboBoxColumn productColumn = (DataGridViewComboBoxColumn)this.dgwRecipe.Columns["Product"]; Cursor = Cursors.WaitCursor; using (var db = new PastryShopDbContext()) { productColumn.DataSource = db.ProductDetails.OrderBy(pd => pd.Name).ToList(); productColumn.DisplayMember = "Name"; productColumn.ValueMember = "Id"; } Cursor = Cursors.Default; this.UpdateDataGrid(); }
private void LoadProductsBySelectedOption(int option) { var dateNow = DateTime.Now.Date; if (option == 0) { if (this.availableAndNotExpiredProducts == null) { using (var db = new PastryShopDbContext()) { this.availableAndNotExpiredProducts = db.Products.Include("ProductDetail") .Where(p => p.AvailableQuantity > 0 && p.ExpiryDate > dateNow) .ToList(); } } LoadProductsIntoDataGridView(this.availableAndNotExpiredProducts); } else if (option == 1) { if (notAvailableAndNotExpiredProducts == null) { using (var db = new PastryShopDbContext()) { this.notAvailableAndNotExpiredProducts = db.Products.Include("ProductDetail") .Where(p => p.AvailableQuantity <= 0 && p.ExpiryDate > dateNow) .ToList(); } } LoadProductsIntoDataGridView(this.notAvailableAndNotExpiredProducts); } else if (option == 2) { if (productsWithExpiredDate == null) { using (var db = new PastryShopDbContext()) { this.productsWithExpiredDate = db.Products.Include("ProductDetail") .Where(p => p.ExpiryDate < dateNow) .ToList(); } } LoadProductsIntoDataGridView(this.productsWithExpiredDate); } }
public CreateDocumentFrame() { InitializeComponent(); DataGridViewComboBoxColumn dessertColumn = (DataGridViewComboBoxColumn)this.dessertDataGridView.Columns["Product"]; DataGridViewButtonColumn deleteColumn = (DataGridViewButtonColumn)this.dessertDataGridView.Columns["Delete"]; Cursor = Cursors.WaitCursor; using (var db = new PastryShopDbContext()) { dessertColumn.DataSource = db.Desserts.OrderBy(d => d.Name).ToList(); dessertColumn.DisplayMember = "Name"; dessertColumn.ValueMember = "Id"; } Cursor = Cursors.Default; }
public static Dessert ReadValues(DataGridView gridView, TextBox tbDessetName, bool saveInDB) { if (gridView == null || gridView.RowCount - 1 <= 0) { return(null); } Dessert dessert = new Dessert(); using (var db = new PastryShopDbContext()) { string dessertName = tbDessetName.Text; if (dessertName == null || dessertName.Trim().Length == 0) { return(null); } dessert.Name = dessertName.Trim(); int rowCount = gridView.RowCount - 1; for (int i = 0; i < rowCount; i++) { var productDetailId = gridView.Rows[i].Cells["Product"].Value; var quantity = gridView.Rows[i].Cells["Quantity"].Value; bool areAllRequiredFieldsFilled = productDetailId != null && quantity != null; if (areAllRequiredFieldsFilled) { RecipeLine line = new RecipeLine(); line.Dessert = dessert; line.Quantity = Convert.ToDouble(quantity); line.ProductDetail = db.ProductDetails.First(p => p.Id == (int)productDetailId); dessert.RecipeLines.Add(line); } else { return(null); } } if (saveInDB) { db.Desserts.Add(dessert); db.SaveChanges(); } } return(dessert); }
public InputDocumentCreationForm() { InitializeComponent(); DataGridViewComboBoxColumn providerColumn = (DataGridViewComboBoxColumn)this.dgwInputDocument.Columns["Provider"]; DataGridViewComboBoxColumn productColumn = (DataGridViewComboBoxColumn)this.dgwInputDocument.Columns["Product"]; using (var db = new PastryShopDbContext()) { providerColumn.DataSource = db.Providers.ToArray(); providerColumn.DisplayMember = "Name"; providerColumn.ValueMember = "Id"; productColumn.DataSource = db.ProductDetails.ToArray(); productColumn.DisplayMember = "Name"; productColumn.ValueMember = "Id"; } }
public InputDocumentCreationFrame() { InitializeComponent(); DataGridViewComboBoxColumn providerColumn = (DataGridViewComboBoxColumn)this.dgwInputDocument.Columns["Provider"]; DataGridViewComboBoxColumn productColumn = (DataGridViewComboBoxColumn)this.dgwInputDocument.Columns["Product"]; Cursor = Cursors.WaitCursor; using (var db = new PastryShopDbContext()) { providerColumn.DataSource = db.Providers.OrderBy(pr => pr.Name).ToList(); providerColumn.DisplayMember = "Name"; providerColumn.ValueMember = "Id"; productColumn.DataSource = db.ProductDetails.OrderBy(pd => pd.Name).ToList(); productColumn.DisplayMember = "Name"; productColumn.ValueMember = "Id"; } Cursor = Cursors.Default; }
private void showBtn_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; var fromDate = fromDateTimePicker.Value.Date; var toDate = toDateTimePicker.Value.Date; List <OutputDocument> filtredDocuments = new List <OutputDocument>(); using (var db = new PastryShopDbContext()) { filtredDocuments = db.OutputDocuments.Where(d => d.IssueDate >= fromDate && d.IssueDate <= toDate).ToList(); } this.outputDocGridView.Rows.Clear(); for (int i = 0; i < filtredDocuments.Count; i++) { DataGridViewRow row = new DataGridViewRow(); int rowIndex = this.outputDocGridView.Rows.Add(); row = this.outputDocGridView.Rows[rowIndex]; row.Cells["id"].Value = filtredDocuments[i].Id; row.Cells["issueDate"].Value = filtredDocuments[i].IssueDate.ToShortDateString(); } Cursor = Cursors.Default; }
private void LoadDesserts() { using (var db = new PastryShopDbContext()) { List <Dessert> desserts = db.Desserts.OrderBy(d => d.Name).ToList(); foreach (var dessert in desserts) { int newRowIndex = this.dgwDesserts.Rows.Add(); DataGridViewRow row = this.dgwDesserts.Rows[newRowIndex]; row.Cells["DessertName"].Value = dessert.Name; var recipeLines = (DataGridViewComboBoxCell)row.Cells["DessertRecipeLines"]; List <string> productNameQuantityList = dessert.RecipeLines .Select( rl => rl.ProductDetail.Name + " - " + rl.Quantity.ToString("0.#######") + " " + rl.ProductDetail.Unit ).ToList <string>(); productNameQuantityList.ForEach(x => recipeLines.Items.Add(x)); } } }
private DocumentModel CreateDocumentModel(Dictionary <int, int> idsToQuantities) { Cursor = Cursors.WaitCursor; DocumentModel docModel = new DocumentModel(); docModel.IssueDate = DateTime.Now; //Saving OutputDocument OutputDocument outputDoc = new OutputDocument(); outputDoc.IssueDate = DateTime.Now.Date; using (var db = new PastryShopDbContext()) { using (var tr = db.Database.BeginTransaction()) { foreach (var item in idsToQuantities) { Dessert dessert = db.Desserts.Include("RecipeLines.ProductDetail").FirstOrDefault(d => d.Id == item.Key); DessertInfo dessertInfo = new DessertInfo(); dessertInfo.Name = dessert.Name; dessertInfo.Quantity = item.Value; //Saving OutputDocument OutputDocumentLine outputDocLine = new OutputDocumentLine(); outputDocLine.Quantity = item.Value; outputDocLine.Dessert = dessert; outputDocLine.OutputDocument = outputDoc; foreach (var recipeLine in dessert.RecipeLines) { var dateNow = DateTime.Now.Date; var productsInStore = db.Products .Where(p => p.ProductDetail.Id == recipeLine.ProductDetail.Id && p.AvailableQuantity > 0 && p.ExpiryDate >= dateNow ); int productsInStoreCount = productsInStore.Count(); double neededQuantity = item.Value * recipeLine.Quantity; var productInDbSum = productsInStore.Sum(p => (double?)(p.AvailableQuantity)) ?? 0; if (productsInStoreCount == 0 || productInDbSum < neededQuantity) { throw new Exception("Няма достатъчно количество " + recipeLine.ProductDetail.Name + " за " + dessert.Name); } while (neededQuantity > 0) { var product = productsInStore.FirstOrDefault(p => p.AvailableQuantity > 0 && p.ExpiryDate >= dateNow); // product can be null if at the time of execution // the query expiry date become < DateTime.Now.Date if (product == null) { throw new Exception("Няма достатъчно количество " + recipeLine.ProductDetail.Name + " за " + dessert.Name); } Ingredient ingredient = new Ingredient(); ingredient.Name = recipeLine.ProductDetail.Name; ingredient.Unit = recipeLine.ProductDetail.Unit; ingredient.BatchNumber = product.BatchNumber; //Saving OutputDocument OutputDocLineProduct outDocLine = new OutputDocLineProduct(); outDocLine.OutputDocLine = outputDocLine; outDocLine.Product = product; if (product.AvailableQuantity >= neededQuantity) { product.AvailableQuantity -= neededQuantity; ingredient.Quantity = neededQuantity; outDocLine.CalcualtedQuantity = neededQuantity; neededQuantity = 0; } else { neededQuantity -= product.AvailableQuantity; ingredient.Quantity = product.AvailableQuantity; product.AvailableQuantity = 0; } //Saving OutputDocument outDocLine.CalcualtedQuantity = ingredient.Quantity; outputDocLine.OutputDocLineProducts.Add(outDocLine); dessertInfo.Ingredients.Add(ingredient); db.SaveChanges(); } } outputDocLine.OutputDocument = outputDoc; outputDoc.Lines.Add(outputDocLine); docModel.Desserts.Add(dessertInfo); } db.OutputDocuments.Add(outputDoc); db.SaveChanges(); tr.Commit(); } } Cursor = Cursors.Default; return(docModel); }
public static InputDocument ReadValues(DataGridView gridView, bool saveInDB) { if (gridView == null || gridView.RowCount - 1 <= 0) { return(null); } InputDocument inputDocument = new InputDocument(); inputDocument.CreatedOn = DateTime.Now; using (var db = new PastryShopDbContext()) { int rowCount = gridView.RowCount - 1; for (int i = 0; i < rowCount; i++) { var receivingDate = gridView.Rows[i].Cells["ReceivingDate"].Value; var providerId = gridView.Rows[i].Cells["Provider"].Value; var productDetailId = gridView.Rows[i].Cells["Product"].Value; var receivedQuantity = gridView.Rows[i].Cells["ReceivedQuantity"].Value; var batchNumber = gridView.Rows[i].Cells["BatchNumber"].Value; var expiryDate = gridView.Rows[i].Cells["ExpiryDate"].Value; var storageCondition = gridView.Rows[i].Cells["StorageCondition"].Value; var vehicle = gridView.Rows[i].Cells["Vehicle"].Value; var document = gridView.Rows[i].Cells["Document"].Value; bool areAllRequiredFieldsFilled = batchNumber != null && receivingDate != null && productDetailId != null && expiryDate != null && receivedQuantity != null && providerId != null; if (areAllRequiredFieldsFilled) { InputDocumentLine line = new InputDocumentLine(); line.InputDocument = inputDocument; line.ReceivingDate = Convert.ToDateTime(receivingDate.ToString()); line.Provider = db.Providers.Single(pr => pr.Id == (int)providerId); line.ReceivedQuantity = Convert.ToDouble(receivedQuantity.ToString()); line.Vehicle = (vehicle != null) ? vehicle.ToString() : String.Empty; line.Document = (document != null) ? document.ToString() : String.Empty; var retrievedProductFromDB = db.Products .FirstOrDefault(p => p.ProductDetail.Id == (int)productDetailId && p.BatchNumber == batchNumber.ToString() ); if (retrievedProductFromDB == null) { Product product = new Product(); product.AvailableQuantity = line.ReceivedQuantity; product.BatchNumber = batchNumber.ToString(); product.ExpiryDate = Convert.ToDateTime(expiryDate).Date; product.StorageCondition = (storageCondition != null) ? storageCondition.ToString() : String.Empty; product.ProductDetail = db.ProductDetails.Single(p => p.Id == (int)productDetailId); line.Product = product; } else { line.Product = retrievedProductFromDB; line.Product.AvailableQuantity += line.ReceivedQuantity; } inputDocument.Lines.Add(line); } else { return(null); } } if (saveInDB) { db.InputDocuments.Add(inputDocument); db.SaveChanges(); } } return(inputDocument); }