/// <summary> /// cashierAndEventButton will store the user input into the global utilities file for future use, /// then create an excel file for this cashier or open/update an existing file, /// then will be redirected back to SaleDetails form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CashierAndEventButton_Click(object sender, EventArgs e) { // Creates the filepath for the cashier+event excel file string filePath = (GlobalUtilities.getMasterFilePath()).Remove((GlobalUtilities.getMasterFilePath()).LastIndexOf('\\') + 1); string[] dateTimeArray = (DateTime.Now.Date.ToString("dd/MM/yyyy")).Split('/'); GlobalUtilities.setCashierAndEventFilePath(filePath + cashierNameTextBox.Text + "_" + eventNameTextBox.Text + "_" + dateTimeArray[0] + "-" + dateTimeArray[1] + "-" + dateTimeArray[2] + ".xlsx"); // Checks if the file already exists, if so update, if not create a new file if (!System.IO.File.Exists(GlobalUtilities.getCashierAndEventFilePath())) // Create a new file { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(System.Reflection.Missing.Value); Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); xlWorksheet.Cells[1, 1] = "Customer Name"; xlWorksheet.Cells[1, 2] = "Barcode Number"; xlWorksheet.Cells[1, 3] = "Product Description"; xlWorksheet.Cells[1, 4] = "Price"; xlWorksheet.Cells[1, 5] = "Quantity"; xlWorksheet.Cells[1, 6] = "Total Amount"; xlWorksheet.Cells[1, 7] = "Customer Type"; xlWorksheet.Cells[1, 8] = "Cash"; xlWorksheet.Cells[1, 9] = "Debit"; xlWorksheet.Cells[1, 10] = "Credit"; xlWorksheet.Cells[1, 11] = "Check"; xlWorksheet.Cells[1, 12] = "Salary Deduction"; xlWorksheet.Cells[1, 13] = "Date"; xlWorksheet.Cells[1, 14] = "Time"; xlWorkbook.SaveAs(GlobalUtilities.getCashierAndEventFilePath()); xlWorkbook.Close(true); xlApp.Quit(); Marshal.ReleaseComObject(xlWorksheet); Marshal.ReleaseComObject(xlWorkbook); Marshal.ReleaseComObject(xlApp); } else { // Do nothing } this.Close(); GlobalUtilities.setCashierName(cashierNameTextBox.Text); GlobalUtilities.setEventName(eventNameTextBox.Text); }
/// <summary> /// Generates the user's Word document receipt /// </summary> private void createWordDocument() { try { string filePath = (GlobalUtilities.getMasterFilePath()).Remove((GlobalUtilities.getMasterFilePath()).LastIndexOf('\\') + 1); string[] dateArray = (DateTime.Now.Date.ToString("dd/MM/yyyy")).Split('/'); string[] timeArray = (DateTime.Now.ToString("h:mm:ss tt")).Split(':'); string wordFilePath = filePath + GlobalUtilities.getCustomerName() + "_" + dateArray[0] + "-" + dateArray[1] + "-" + dateArray[2] + "_" + timeArray[0] + "-" + timeArray[1] + "-" + timeArray[2] + ".docx"; Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.ShowAnimation = false; wordApp.Visible = false; object missing = System.Reflection.Missing.Value; Word.Document wordDocument = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); Word.Range rng = wordDocument.Paragraphs[1].Range; Word.Paragraph para = wordDocument.Paragraphs.Add(ref missing); // Sets the font format for the document to be monospace (easier to edit characters/words to print) wordDocument.Content.SetRange(0, 0); object styleName = "No Spacing"; rng.set_Style(ref styleName); rng.Font.Size = 11; rng.Font.Name = "Consolas"; rng.Select(); // There are a total of 77 possible characters per line // Indentation to align the Word doc text para.Range.Text = Environment.NewLine; // Cashier, Time and Customer Number info para.Range.Text = "Cashier: " + GlobalUtilities.getCashierName() + Environment.NewLine; para.Range.Text = "Cust. Add.: " + GlobalUtilities.getCustomerAddress() + Environment.NewLine; para.Range.Text = "Cust. Tel.: " + GlobalUtilities.getCashierName() + Environment.NewLine; para.Range.Text = "Time: " + timeArray[0] + ":" + timeArray[1] + ":" + timeArray[2] + Environment.NewLine; // Some indentation to align text para.Range.Text = Environment.NewLine; // Customer details: Customer Name, address, TIN, Business Style, Data, Terms, OSCA/PWD ID No., Cardholder's Signature para.Range.Text = buildCharacterParagraphArray(11, GlobalUtilities.getCustomerName(), 38, 10, DateTime.Now.Date.ToString("dd/MM/yyyy")) + Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(11, GlobalUtilities.getCustomerAddress(), 38, 11, GlobalUtilities.getCustomerTerms()) + Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(8, GlobalUtilities.getCustomerTIN(), 41, 21, GlobalUtilities.getCustomerOSCA()) + Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(17, GlobalUtilities.getCustomerBusinessStyle(), 33, 19, "") + Environment.NewLine; // Customer order details title para.Range.Text = Environment.NewLine; para.Range.Text = "Item Number Description Qty Price Amount" + Environment.NewLine; para.Range.Text = Environment.NewLine; // Customer order details breakdown List <string> keyList = new List <string>(GlobalUtilities.getCustomerTransactionDictionary().Keys); for (int i = 0; i < GlobalUtilities.getCustomerTransactionDictionary().Count; i++) { para.Range.Text = buildBodyParagraphArray(keyList[i]) + Environment.NewLine; } for (int j = 0; j < (20 - GlobalUtilities.getCustomerTransactionDictionary().Count); j++) { para.Range.Text = Environment.NewLine; } para.Range.Text = Environment.NewLine; para.Range.Text = buildTotalParagraphArray() + Environment.NewLine; // Sales data double vatSales = GlobalUtilities.getTotalCost() * 0.88; double vat = GlobalUtilities.getTotalCost() * 0.12; para.Range.Text = Environment.NewLine; para.Range.Text = Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(68, "", 0, 0, vatSales.ToString()) + Environment.NewLine; para.Range.Text = Environment.NewLine; para.Range.Text = Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(68, "", 0, 0, String.Format("{0:n}", GlobalUtilities.getTotalCost())) + Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(68, "", 0, 0, vat.ToString()) + Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(68, "", 0, 0, String.Format("{0:n}", GlobalUtilities.getTotalCost())) + Environment.NewLine; para.Range.Text = Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(62, "", 0, 0, String.Format("{0:n}", GlobalUtilities.getTotalCustomerPayment())) + Environment.NewLine; para.Range.Text = buildCharacterParagraphArray(62, "", 0, 0, String.Format("{0:n}", Double.Parse(GlobalUtilities.getTotalChange()))) + Environment.NewLine; //Save the document GlobalUtilities.setCustomerOrderFilePath(wordFilePath); object documentFilePath = GlobalUtilities.getCustomerOrderFilePath(); wordDocument.SaveAs2(ref documentFilePath); wordDocument.Close(ref missing, ref missing, ref missing); wordDocument = null; wordApp.Quit(ref missing, ref missing, ref missing); wordApp = null; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// masterFile button will open a file explorer for the user to use and search /// for the master file to fill the data hash set within the GlobalUtilities file, /// then redirects it to CashierAndEventInfo /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MasterFileButton_Click(object sender, EventArgs e) { try { // Opens file explorer and stores filename, and the contents of the excel file to global utilities OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Excel Worksheets|*.xls; *.xlsx"; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { GlobalUtilities.setMasterFilePath(openFileDialog.FileName); } // Stores excel data onto Dictionary Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(GlobalUtilities.getMasterFilePath(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); Excel.Range xlRange = xlWorksheet.UsedRange; int row = xlRange.Rows.Count; int col = xlRange.Columns.Count; string eanCodePlaceholder = ""; for (int rowCount = 1; rowCount <= row; rowCount++) { // Creating a list to contain the excel data List <string> productListInfo = new List <string>(); for (int colCount = 1; colCount <= col; colCount++) { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Legend: * * Material # | Material Description | Selling Price | Discount | EAN Code | Quantity * * --------------------------------------------------------------------------------------------------------------* * column 1 | colum 2 | column 3 | column 4 | column 5 | column6 * * type double | type string | type double | type double | type double | {null} no input* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // Stores data into dictionary Object obj = (xlRange.Cells[rowCount, colCount] as Excel.Range).Value; if (colCount == 5) // EAN Code, to be used in the dictionary { eanCodePlaceholder = obj.ToString(); } else // Material #, Material Description, Selling Price, Discount, and Quantity { if (colCount == 6) { productListInfo.Add("0"); } else if (obj != null) { productListInfo.Add(obj.ToString()); } } } GlobalUtilities.addToDictionary(GlobalUtilities.MASTER, eanCodePlaceholder, productListInfo); } } catch (Exception ex) { MessageBox.Show(ex.Message); } this.Close(); if (applicationType == GlobalUtilities.TRANSACTION_MODE) { CashierAndEventInfo cashierAndEventInfo = new CashierAndEventInfo(); cashierAndEventInfo.ShowDialog(); } else if (applicationType == GlobalUtilities.INVENTORY_MODE) { InventorySaleDetails inventorySaleDetails = new InventorySaleDetails(); inventorySaleDetails.ShowDialog(); } }
/// <summary> /// Updates Excel sheet and closes the application /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProcessButton_Click(object sender, EventArgs e) { addProductButton.PerformClick(); try { // Opens Excel Sheet xlApp = new Excel.Application(); xlApp.Visible = false; xlApp.DisplayAlerts = false; xlWorkbook = xlApp.Workbooks.Open(GlobalUtilities.getMasterFilePath(), 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); // Edits Excel Sheet Excel.Range xlRange = xlWorksheet.UsedRange; for (int i = 1; i <= xlRange.Rows.Count + 10; i++) { if (((Microsoft.Office.Interop.Excel.Range)xlWorksheet.Cells[i, 5]).Value != null) { object cellValue = ((Microsoft.Office.Interop.Excel.Range)xlWorksheet.Cells[i, 5]).Value; string cellValueString = cellValue.ToString(); if (GlobalUtilities.getMasterListDictionary().ContainsKey(cellValueString.TrimStart(new Char[] { '0' })) && !Regex.IsMatch(cellValueString.Replace(" ", ""), @"^[a-zA-Z]+$")) { List <string> tempMasterListDictionaryProduct = new List <string>(GlobalUtilities.getMasterListDictionary()[cellValueString.TrimStart(new Char[] { '0' })]); object quantityCellValue = ((Microsoft.Office.Interop.Excel.Range)xlWorksheet.Cells[i, 6]).Value; string quantityCellValueString; if (quantityCellValue != null) { quantityCellValueString = quantityCellValue.ToString(); } else { quantityCellValueString = "0"; } if (!string.IsNullOrWhiteSpace(quantityCellValueString)) { xlWorksheet.Cells[i, 6] = Double.Parse(quantityCellValueString) + Double.Parse(tempMasterListDictionaryProduct[4].ToString()); } else { xlWorksheet.Cells[i, 6] = Double.Parse(tempMasterListDictionaryProduct[4].ToString()); } } } } // Saves Excel sheet xlWorkbook.SaveAs(GlobalUtilities.getMasterFilePath()); xlWorkbook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); if (xlWorkbook != null) { Marshal.ReleaseComObject(xlWorkbook); } //release each workbook like this if (xlWorksheet != null) { Marshal.ReleaseComObject(xlWorksheet); } //release each worksheet like this if (xlApp != null) { Marshal.ReleaseComObject(xlApp); } //release the Excel application xlWorkbook = null; //set each memory reference to null. xlWorksheet = null; xlApp = null; GC.Collect(); } catch (Exception ex) { // Saves Excel sheet xlApp.Quit(); //release all memory - stop EXCEL.exe from hanging around. if (xlWorkbook != null) { Marshal.ReleaseComObject(xlWorkbook); } //release each workbook like this if (xlWorksheet != null) { Marshal.ReleaseComObject(xlWorksheet); } //release each worksheet like this if (xlApp != null) { Marshal.ReleaseComObject(xlApp); } //release the Excel application xlWorkbook = null; //set each memory reference to null. xlWorksheet = null; xlApp = null; GC.Collect(); MessageBox.Show(ex.Message); } this.Close(); Application.Exit(); }