/// <summary> /// Builds the total paragraph to display total quantity and amount /// </summary> /// <returns></returns> private string buildTotalParagraphArray() { List <string> paraList = new List <string>(); paraList.Add(" Total "); char[] totalQuantity = GlobalUtilities.getTotalQuantity().ToCharArray(); char[] totalCost = String.Format("{0:n}", GlobalUtilities.getTotalCost()).ToCharArray(); for (int i = 0; i < 15; i++) { if (i < GlobalUtilities.getTotalQuantity().Length) { paraList.Add(totalQuantity[i].ToString()); } else { paraList.Add(" "); } } for (int j = 0; j < 9; j++) { if (j < GlobalUtilities.getTotalCost().ToString().Length) { paraList.Add(totalCost[j].ToString()); } else { paraList.Add(" "); } } return(string.Join("", paraList)); }
/// <summary> /// enables or disables the text boxes based on whether or not /// the preceeding identifiers were toggled on the PaymentAndCustomerType form, /// also disables the proceedButton to force the user to calculate customer's change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PaymentCalculator_Load(object sender, EventArgs e) { proceedButton.Enabled = false; if (GlobalUtilities.getCashPaymentIdentifier() != true) { cashTextBox.ReadOnly = true; } if (GlobalUtilities.getCreditPaymentIdentifier() != true) { creditTextBox.ReadOnly = true; } if (GlobalUtilities.getDebitPaymentIdentifier() != true) { debitTextBox.ReadOnly = true; } if (GlobalUtilities.getCheckPaymentIdentifier() != true) { checkTextBox.ReadOnly = true; } if (GlobalUtilities.getSalaryDeductionPaymentIdentifier() != true) { salaryDeductionTextBox.ReadOnly = true; } totalAmountTextBox.Text = String.Format("{0:n}", GlobalUtilities.getTotalCost()); changeTextBox.Text = "0"; }
/// <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> /// saves the data input on SaleDetails form into GlobalUtilities, /// then redirected to CodeDetails form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProcessButton_Click(object sender, EventArgs e) { addProductButton.PerformClick(); GlobalUtilities.setCustomerName(nameTextBox.Text); GlobalUtilities.setCustomerAddress(addressTextBox.Text); GlobalUtilities.setCustomerContact(phoneTextBox.Text); CodeDetails codeDetails = new CodeDetails(); codeDetails.ShowDialog(); }
/// <summary> /// proceed button takes the user input on the given fields, /// stores it into global utilities, to be used later, /// and moves to PaymentType form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProceedButton_Click(object sender, EventArgs e) { this.Close(); GlobalUtilities.setCustomerTIN(tinTextBox.Text); GlobalUtilities.setCustomerBusinessStyle(businessStyleTextBox.Text); GlobalUtilities.setCustomerTerms(termsTextBox.Text); GlobalUtilities.setCustomerOSCA(oscaTextBox.Text); PaymentAndCustomerType paymentAndCustomerType = new PaymentAndCustomerType(); paymentAndCustomerType.ShowDialog(); }
/// <summary> /// Loads masterListDictionary on Form load /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void InventorySaleDetails_Load(object sender, EventArgs e) { for (int i = 0; i < keyList.Count; i++) { if (!Regex.IsMatch(keyList[i].Replace(" ", ""), @"^[a-zA-Z]+$")) { List <string> customerProductOrder = new List <string>(GlobalUtilities.getProductInfoFromDictionary(GlobalUtilities.MASTER, keyList[i])); // Bar Code Product Description Price Quantity Discount productOrderDataGridView.Rows.Add(keyList[i], customerProductOrder[1], customerProductOrder[2], customerProductOrder[3], customerProductOrder[4]); } } }
/// <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> /// Increases/Decreases the quantity value of the product in the masterListDictionary /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddProductButton_Click(object sender, EventArgs e) { if (String.IsNullOrWhiteSpace(productBarCodeTextBox.Text) || !GlobalUtilities.isInDictionary(GlobalUtilities.MASTER, (productBarCodeTextBox.Text).TrimStart(new Char[] { '0' }))) { productBarCodeTextBox.Focus(); //quantityNumericUpDown.Value = 1; //productBarCodeTextBox.Text = ""; return; } List <string> customerProductOrder = new List <string>(); List <string> masterListDictionaryProduct = new List <string>(GlobalUtilities.getMasterListDictionary()[(productBarCodeTextBox.Text).TrimStart(new Char[] { '0' })]); DataGridViewRow updateDataGridViewRow = productOrderDataGridView.Rows[rowSearchWithMatchingBarCodes((productBarCodeTextBox.Text).TrimStart(new Char[] { '0' }))]; int totalQuantityCheck = Int32.Parse(updateDataGridViewRow.Cells[3].Value.ToString()) + (int)quantityNumericUpDown.Value; if (totalQuantityCheck <= 0) { GlobalUtilities.setTotalQuantity((Int32.Parse(GlobalUtilities.getTotalQuantity()) - Int32.Parse(updateDataGridViewRow.Cells[3].Value.ToString())).ToString()); totalQuantityTextBox.Text = GlobalUtilities.getTotalQuantity(); updateDataGridViewRow.Cells[3].Value = "0"; productBarCodeTextBox.Focus(); quantityNumericUpDown.Value = 1; productBarCodeTextBox.Text = ""; return; } GlobalUtilities.setTotalQuantity((Int32.Parse(GlobalUtilities.getTotalQuantity()) + Int32.Parse(quantityNumericUpDown.Value.ToString())).ToString()); updateDataGridViewRow.Cells[3].Value = totalQuantityCheck; // Material #, Material Description, Selling Price, Discount, and Quantity customerProductOrder.Add(masterListDictionaryProduct[0].ToString()); customerProductOrder.Add(masterListDictionaryProduct[1].ToString()); customerProductOrder.Add(masterListDictionaryProduct[2].ToString()); customerProductOrder.Add(masterListDictionaryProduct[3].ToString()); customerProductOrder.Add(updateDataGridViewRow.Cells[3].Value.ToString()); GlobalUtilities.addToDictionary(GlobalUtilities.MASTER, productBarCodeTextBox.Text, customerProductOrder); totalQuantityTextBox.Text = GlobalUtilities.getTotalQuantity(); productBarCodeTextBox.Focus(); productBarCodeTextBox.Text = String.Empty; quantityNumericUpDown.Value = 1; }
/// <summary> /// Clears the SaleDetails controls and deletes current customer information /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NextCustomerButton_Click(object sender, EventArgs e) { GlobalUtilities.cleanCustomerOrder(); foreach (Control c in this.Controls) { var textBox = c as TextBox; var dataGridView = c as DataGridView; if (textBox != null) { textBox.Clear(); } } productOrderDataGridView.Rows.Clear(); productOrderDataGridView.Refresh(); processButton.Enabled = false; productBarCodeTextBox.Focus(); }
/// <summary> /// allows the user to do a quick print without opening the desired word document /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PrintButton_Click(object sender, EventArgs e) { try { object missing = System.Reflection.Missing.Value; Word.Application wordApp = new Word.Application { Visible = false }; Word.Document wordDocument = wordApp.Documents.Open(GlobalUtilities.getCustomerOrderFilePath(), ReadOnly: false, Visible: false); wordDocument.Activate(); wordDocument.PrintOut(); 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> /// calculates the customer's change (total amount - customer payment), /// this will determine whether or not the cashier can proceed to the next form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CalculateButton_Click(object sender, EventArgs e) { GlobalUtilities.setCashPayment(cashTextBox.Text); GlobalUtilities.setCreditPayment(creditTextBox.Text); GlobalUtilities.setDebitPayment(debitTextBox.Text); GlobalUtilities.setCheckPayment(checkTextBox.Text); GlobalUtilities.setSalaryDeductionPayment(salaryDeductionTextBox.Text); GlobalUtilities.setTotalCustomerPayment(); GlobalUtilities.setTotalChange(); changeTextBox.Text = String.Format("{0:n}", GlobalUtilities.getTotalChange()); totalCustomerPaymentTextBox.Text = String.Format("{0:n}", (Double.Parse(GlobalUtilities.getCashPayment()) + Double.Parse(GlobalUtilities.getCreditPayment()) + Double.Parse(GlobalUtilities.getDebitPayment()) + Double.Parse(GlobalUtilities.getCheckPayment()) + Double.Parse(GlobalUtilities.getSalaryDeductionPayment())).ToString()); if (Double.Parse(GlobalUtilities.getTotalChange()) >= 0) { proceedButton.Enabled = true; } else { proceedButton.Enabled = false; } }
/// <summary> /// payment and customer type button stores the user data input check box, /// then redirects the user to the calculator form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PaymentAndCustomerTypeButton_Click(object sender, EventArgs e) { // Stores payment type in GlobalUtilities for (int i = 0; i < paymentTypeCheckBox.CheckedIndices.Count; i++) { switch (paymentTypeCheckBox.CheckedIndices[i]) { case 0: // Cash GlobalUtilities.setCashPaymentIdentifier(true); break; case 1: // Credit GlobalUtilities.setCreditPaymentIdentifier(true); break; case 2: // Debit GlobalUtilities.setDebitPaymentIdentifier(true); break; case 3: // Check GlobalUtilities.setCheckPaymentIdentifier(true); break; case 4: // Salary Deduction GlobalUtilities.setSalaryDeductionPaymentIdentifier(true); break; } } // Stores customer type in GlobalUtilities GlobalUtilities.setCustomerType(customerTypeGroupBox.Controls.OfType <RadioButton>().SingleOrDefault(n => n.Checked == true).Text); this.Close(); PaymentCalculator paymentCalculator = new PaymentCalculator(); paymentCalculator.ShowDialog(); }
/// <summary> /// Builds the body that can host up to 20 customer orders. /// Displays Item number, Description, Quantity, Price, and Amount /// </summary> /// <param name="customerKey"></param> /// <returns></returns> private string buildBodyParagraphArray(string customerKey) { List <string> paraList = new List <string>(); List <string> customerOrder = GlobalUtilities.getProductInfoFromDictionary(GlobalUtilities.CUSTOMER, customerKey); char[] barCodeCharArray = customerKey.ToCharArray(); char[] productDescriptionCharArray = customerOrder[1].ToCharArray(); char[] priceCharArray = customerOrder[2].ToCharArray(); char[] quantityCharArray = customerOrder[3].ToCharArray(); char[] amountCharArray = customerOrder[4].ToCharArray(); // Item Number for (int i = 0; i < 16; i++) { if (i < customerKey.Length) { paraList.Add(barCodeCharArray[i].ToString()); } else { paraList.Add(" "); } } for (int numSpace = 0; numSpace < 3; numSpace++) { paraList.Add(" "); } // Description for (int j = 0; j < 32; j++) { if (j < customerOrder[1].Length) { paraList.Add(productDescriptionCharArray[j].ToString()); } else { paraList.Add(" "); } } for (int numSpace = 0; numSpace < 3; numSpace++) { paraList.Add(" "); } // Quantity for (int k = 0; k < 3; k++) { if (k < customerOrder[3].Length) { paraList.Add(quantityCharArray[k].ToString()); } else { paraList.Add(" "); } } for (int numSpace = 0; numSpace < 3; numSpace++) { paraList.Add(" "); } // Price for (int l = 0; l < 5; l++) { if (l < customerOrder[2].Length) { paraList.Add(priceCharArray[l].ToString()); } else { paraList.Add(" "); } } for (int numSpace = 0; numSpace < 3; numSpace++) { paraList.Add(" "); } // Amount for (int m = 0; m < 9; m++) { if (m < customerOrder[4].Length) { paraList.Add(amountCharArray[m].ToString()); } else { paraList.Add(" "); } } return(string.Join("", paraList)); }
/// <summary> /// button searches takes the userInput text from the text box and searches the dictionary for the product. /// If the product exists, add it to the DataGridView, /// else neglect the input. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddProductButton_Click(object sender, EventArgs e) { List <string> customerProductOrder = new List <string>(); // Check customerTransactionDictionary. if available find the list and update, else search masterListDictionary and add it into customerTransactionDictionary // then update the DataGridView as necessary if (GlobalUtilities.isInDictionary(GlobalUtilities.CUSTOMER, productBarCodeTextBox.Text)) { DataGridViewRow updateDataGridViewRow = productOrderDataGridView.Rows[rowSearchWithMatchingBarCodes(productBarCodeTextBox.Text)]; int totalQuantityCheck = Int32.Parse(updateDataGridViewRow.Cells[3].Value.ToString()) + (int)quantityNumericUpDown.Value; if (totalQuantityCheck <= 0) { productOrderDataGridView.Rows.RemoveAt(rowSearchWithMatchingBarCodes(productBarCodeTextBox.Text)); GlobalUtilities.setTotalQuantity((Int32.Parse(GlobalUtilities.getTotalQuantity()) - Int32.Parse(updateDataGridViewRow.Cells[3].Value.ToString())).ToString()); GlobalUtilities.setTotalCost(GlobalUtilities.getTotalCost() - Double.Parse(GlobalUtilities.calculatePrice(updateDataGridViewRow.Cells[2].Value.ToString(), updateDataGridViewRow.Cells[3].Value.ToString(), updateDataGridViewRow.Cells[4].Value.ToString()))); (GlobalUtilities.getCustomerTransactionDictionary()).Remove(productBarCodeTextBox.Text); totalQuantityTextBox.Text = GlobalUtilities.getTotalQuantity(); totalCostTextBox.Text = (GlobalUtilities.getTotalCost()).ToString(); productBarCodeTextBox.Focus(); quantityNumericUpDown.Value = 1; productBarCodeTextBox.Text = ""; return; } GlobalUtilities.setTotalQuantity((Int32.Parse(GlobalUtilities.getTotalQuantity()) + Int32.Parse(quantityNumericUpDown.Value.ToString())).ToString()); GlobalUtilities.setTotalCost(GlobalUtilities.getTotalCost() + Double.Parse(GlobalUtilities.calculatePrice(updateDataGridViewRow.Cells[2].Value.ToString(), quantityNumericUpDown.Value.ToString(), updateDataGridViewRow.Cells[4].Value.ToString()))); updateDataGridViewRow.Cells[3].Value = totalQuantityCheck; updateDataGridViewRow.Cells[5].Value = GlobalUtilities.calculatePrice(updateDataGridViewRow.Cells[2].Value.ToString(), updateDataGridViewRow.Cells[3].Value.ToString(), updateDataGridViewRow.Cells[4].Value.ToString()); customerProductOrder.Add(updateDataGridViewRow.Cells[1].Value.ToString()); customerProductOrder.Add(updateDataGridViewRow.Cells[2].Value.ToString()); customerProductOrder.Add(updateDataGridViewRow.Cells[3].Value.ToString()); customerProductOrder.Add(updateDataGridViewRow.Cells[4].Value.ToString()); customerProductOrder.Add(updateDataGridViewRow.Cells[5].Value.ToString()); GlobalUtilities.addToDictionary(GlobalUtilities.CUSTOMER, productBarCodeTextBox.Text, customerProductOrder); } else { if (GlobalUtilities.getCustomerTransactionDictionary().Count == 20) { // Do not add it to the customer transaction list since it can only have a max of 20 productBarCodeTextBox.Text = ""; return; } if ((int)quantityNumericUpDown.Value <= 0 || String.IsNullOrWhiteSpace(productBarCodeTextBox.Text) || !GlobalUtilities.isInDictionary(GlobalUtilities.MASTER, (productBarCodeTextBox.Text).TrimStart(new Char[] { '0' }))) { return; } customerProductOrder = GlobalUtilities.getProductInfoFromDictionary(GlobalUtilities.MASTER, (productBarCodeTextBox.Text).TrimStart(new Char[] { '0' })); customerProductOrder[3] = (Int32.Parse(customerProductOrder[3]) + (int)quantityNumericUpDown.Value).ToString(); customerProductOrder[5] = GlobalUtilities.calculatePrice(customerProductOrder[2], customerProductOrder[3], customerProductOrder[4]); // Bar Code Product Description Price Quantity Discount Amount productOrderDataGridView.Rows.Add(productBarCodeTextBox.Text, customerProductOrder[1], customerProductOrder[2], customerProductOrder[3], customerProductOrder[4], customerProductOrder[5]); customerProductOrder.RemoveAt(0); GlobalUtilities.addToDictionary(GlobalUtilities.CUSTOMER, productBarCodeTextBox.Text, customerProductOrder); GlobalUtilities.setTotalQuantity((Int32.Parse(GlobalUtilities.getTotalQuantity()) + Int32.Parse(customerProductOrder[2])).ToString()); GlobalUtilities.setTotalCost(GlobalUtilities.getTotalCost() + Double.Parse(GlobalUtilities.calculatePrice(customerProductOrder[1], customerProductOrder[2], customerProductOrder[3]))); } totalQuantityTextBox.Text = GlobalUtilities.getTotalQuantity(); totalCostTextBox.Text = (GlobalUtilities.getTotalCost()).ToString(); productBarCodeTextBox.Text = ""; quantityNumericUpDown.Value = 1; if (canProcessOrder()) { processButton.Enabled = true; } else { processButton.Enabled = false; } productBarCodeTextBox.Focus(); }
/// <summary> /// resets payment identifiers used in PaymentCalculator form upon form closing /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CodeDetails_FormClosing(object sender, FormClosingEventArgs e) { GlobalUtilities.resetPaymentIdentifiers(); }
/// <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> /// allows the user to open up the desired word document to take a closer look /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OpenButton_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start(GlobalUtilities.getCustomerOrderFilePath()); }
/// <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(); }
/// <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(); } }