/// <summary> /// Updates the cashier's Excel sheet to include customer order /// </summary> private void updateCashierExcelSheet() { try { // Opens Excel Sheet Excel.Application xlApp = new Excel.Application(); xlApp.Visible = false; xlApp.DisplayAlerts = false; Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(GlobalUtilities.getCashierAndEventFilePath(), 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); // Edits Excel Sheet Excel.Range xlRange = xlWorksheet.UsedRange; int rowCount = xlRange.Rows.Count + 1; string date = DateTime.Now.Date.ToString("dd/MM/yyyy"); string time = DateTime.Now.ToString("h:mm:ss tt"); List <string> keyList = new List <string>(GlobalUtilities.getCustomerTransactionDictionary().Keys); double percentCash = Math.Round((Double.Parse(GlobalUtilities.getCashPayment()) / GlobalUtilities.getTotalCustomerPayment()), 2); double percentDebit = Math.Round((Double.Parse(GlobalUtilities.getDebitPayment()) / GlobalUtilities.getTotalCustomerPayment()), 2); double percentCredit = Math.Round((Double.Parse(GlobalUtilities.getCreditPayment()) / GlobalUtilities.getTotalCustomerPayment()), 2); double percentCheck = Math.Round((Double.Parse(GlobalUtilities.getCheckPayment()) / GlobalUtilities.getTotalCustomerPayment()), 2); double percentSalaryDeduction = Math.Round((Double.Parse(GlobalUtilities.getSalaryDeductionPayment()) / GlobalUtilities.getTotalCustomerPayment()), 2); for (int i = 0; i < GlobalUtilities.getCustomerTransactionDictionary().Count; i++) { List <string> customerOrder = GlobalUtilities.getProductInfoFromDictionary(GlobalUtilities.CUSTOMER, keyList[i]); xlWorksheet.Cells[rowCount + i, 1] = GlobalUtilities.getCustomerName(); xlWorksheet.Cells[rowCount + i, 2] = keyList[i]; xlWorksheet.Cells[rowCount + i, 3] = customerOrder[1]; xlWorksheet.Cells[rowCount + i, 4] = customerOrder[2]; xlWorksheet.Cells[rowCount + i, 5] = customerOrder[3]; xlWorksheet.Cells[rowCount + i, 6] = customerOrder[4]; xlWorksheet.Cells[rowCount + i, 7] = GlobalUtilities.getCustomerType(); xlWorksheet.Cells[rowCount + i, 8] = GlobalUtilities.getCashPayment(); xlWorksheet.Cells[rowCount + i, 9] = GlobalUtilities.getDebitPayment(); xlWorksheet.Cells[rowCount + i, 10] = GlobalUtilities.getCreditPayment(); xlWorksheet.Cells[rowCount + i, 11] = GlobalUtilities.getCheckPayment(); xlWorksheet.Cells[rowCount + i, 12] = GlobalUtilities.getSalaryDeductionPayment(); xlWorksheet.Cells[rowCount + i, 13] = date; xlWorksheet.Cells[rowCount + i, 14] = time; } // Saves Excel sheet xlWorkbook.SaveAs(GlobalUtilities.getCashierAndEventFilePath()); xlWorkbook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); Marshal.ReleaseComObject(xlWorksheet); Marshal.ReleaseComObject(xlWorkbook); Marshal.ReleaseComObject(xlApp); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <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); }