private Part ConstructPart(bool isNew, int existingId = 0) { // Usually the DB would auto-increment var maxId = MainForm.Inventory.Parts.OrderByDescending(p => p.Id).FirstOrDefault().Id; if (Source == PartSource.Inhouse) { var part = new InhousePart(isNew, maxId, existingId) { Name = txtName.Text, Cost = Convert.ToDouble(numCost.Value), InStock = Convert.ToInt32(numInventory.Value), Min = Convert.ToInt32(numMinimum.Value), Max = Convert.ToInt32(numMaximum.Value), MachineId = Convert.ToInt32(numMachineId.Value) }; return(part); } else if (Source == PartSource.Outsourced) { var part = new OutsourcedPart(isNew, maxId, existingId) { Name = txtName.Text, Cost = Convert.ToDouble(numCost.Value), InStock = Convert.ToInt32(numInventory.Value), Min = Convert.ToInt32(numMinimum.Value), Max = Convert.ToInt32(numMaximum.Value), CompanyName = txtCompanyName.Text }; return(part); } return(null); }
/// <summary> /// Confirm part modifications as filled out in the form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnModifyPartSave_Click(object sender, RoutedEventArgs e) { // Make sure number fields are numbers... if (!MainWindow.IsNumericValue(txtModifyPartInventory.Text) || !MainWindow.IsDecimalValue(txtModifyPartPrice.Text) || !MainWindow.IsNumericValue(txtModifyPartMin.Text) || !MainWindow.IsNumericValue(txtModifyPartMax.Text)) { MessageBox.Show("Please ensure Inventory, Price, Min & Max values are numbers."); return; } if (Convert.ToInt32(txtModifyPartMax.Text) <= Convert.ToInt32(txtModifyPartMin.Text)) { MessageBox.Show("Max value must be larger than Min value"); return; } if (Convert.ToInt32(txtModifyPartMin.Text) > Convert.ToInt32(txtModifyPartInventory.Text) || Convert.ToInt32(txtModifyPartMax.Text) < Convert.ToInt32(txtModifyPartInventory.Text)) { MessageBox.Show("Inventory value must be between Min & Max values."); return; } if (radModifyPartInhouse.IsChecked == true) { if (!MainWindow.IsNumericValue(txtModifyPartMachine_Company.Text)) { MessageBox.Show("Machine ID should be a number."); return; } } if (radModifyPartInhouse.IsChecked == true) { InhousePart newPart = new InhousePart(MainWindow.partSelected.PartID, txtModifyPartName.Text, Convert.ToDecimal(txtModifyPartPrice.Text), Convert.ToInt32(txtModifyPartInventory.Text), Convert.ToInt32(txtModifyPartMin.Text), Convert.ToInt32(txtModifyPartMax.Text), Convert.ToInt32(txtModifyPartMachine_Company.Text)); MainWindow.viewModel.UpdatePart(MainWindow.partSelected.PartID, newPart); } else if (radModifyPartOutsourced.IsChecked == true) { OutsourcedPart newPart = new OutsourcedPart(MainWindow.partSelected.PartID, txtModifyPartName.Text, Convert.ToDecimal(txtModifyPartPrice.Text), Convert.ToInt32(txtModifyPartInventory.Text), Convert.ToInt32(txtModifyPartMin.Text), Convert.ToInt32(txtModifyPartMax.Text), txtModifyPartMachine_Company.Text); MainWindow.viewModel.UpdatePart(MainWindow.partSelected.PartID, newPart); } this.Close(); }
private void SaveButton_Click(object sender, RoutedEventArgs e) { if (IsValid) { if (IsOutsourced) { Part = new OutsourcedPart { PartId = PartId, Name = PartName, Price = decimal.Parse(PartCost), InStock = int.Parse(PartCount), Max = int.Parse(PartMax), Min = int.Parse(PartMin), CompanyName = PartVariableValue }; } else { Part = new InhousePart { PartId = PartId, Name = PartName, Price = decimal.Parse(PartCost), InStock = int.Parse(PartCount), Max = int.Parse(PartMax), Min = int.Parse(PartMin), MachineId = int.Parse(PartVariableValue) }; } if (IsNewPart) { mainWindow.Model.AddPart(Part); } else { mainWindow.Model.UpdatePart(PartId, Part); } this.Close(); mainWindow.Parts = mainWindow.GetUpdatedParts(); mainWindow.Opacity = 1; } else { MessageBox.Show( "You must correct the part errors before saving.", "Save Error", MessageBoxButton.OK, MessageBoxImage.Error ); } }
private void btnModifyPart_Click(object sender, EventArgs e) { if (dataGridParts.CurrentRow.DataBoundItem.GetType() == typeof(InHousePart)) { InHousePart inPart = (InHousePart)dataGridParts.CurrentRow.DataBoundItem; new ModifyPart(inPart).ShowDialog(); } else { OutsourcedPart outPart = (OutsourcedPart)dataGridParts.CurrentRow.DataBoundItem; new ModifyPart(outPart).ShowDialog(); } }