Beispiel #1
0
        /// <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);
            }
        }