示例#1
0
        public void editInvoiceWordDocTemplate(string pathName)
        {
            string sourceFile      = Path.Combine(pathName, "InvoiceTemplate.docx");
            string destinationFile = Path.Combine(pathName, "Invoice " + invoiceID.ToString() + ".docx");

            File.Copy(sourceFile, destinationFile, true);
            using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))
            {
                MainDocumentPart mainPart = document.MainDocumentPart;

                Body body = mainPart.Document.Body;

                #region customer Info table
                TableProperties customerInfoTblprop = new TableProperties(
                    new TableBorders(
                        new TopBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new BottomBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new LeftBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new RightBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new InsideHorizontalBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new InsideVerticalBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                }),
                    new TableCellVerticalAlignment()
                {
                    Val = TableVerticalAlignmentValues.Center
                },
                    //new TableStyle() { Val = "TableGrid" },
                    new TableWidth()
                {
                    Width = "5000", Type = TableWidthUnitValues.Pct
                });

                Table         customerInfoTable     = new Table();
                List <string> customerInfoTableData = new List <string>();
                customerInfoTable.AppendChild <TableProperties>((TableProperties)customerInfoTblprop.Clone());
                customerInfoTableData.Add("Customer: " + this.customerName);
                customerInfoTableData.Add("COMMERCIAL INVOICE");
                customerInfoTable = AppendCustomerTableInfo(customerInfoTableData, customerInfoTable, false);


                customerInfoTableData = new List <string>();
                customerInfoTableData.Add("Email Address:" + this.emailAddress);
                customerInfoTableData.Add("Invoice P1SO-" + invoiceID);
                customerInfoTable = AppendCustomerTableInfo(customerInfoTableData, customerInfoTable, false);

                customerInfoTableData = new List <string>();
                customerInfoTableData.Add("Contact: " + this.phoneNum);
                customerInfoTableData.Add("Date: " + this.date);
                customerInfoTable = AppendCustomerTableInfo(customerInfoTableData, customerInfoTable, false);
                body.Append(customerInfoTable);
                #endregion

                body.Append(new Paragraph(new Run(new Text("\n"))));

                #region Invoice Item List Table
                TableProperties tblprop = new TableProperties(
                    new TableBorders(
                        new TopBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new BottomBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new LeftBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new RightBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new InsideHorizontalBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new InsideVerticalBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                }
                        ),
                    new TableCellVerticalAlignment()
                {
                    Val = TableVerticalAlignmentValues.Center
                },
                    //new TableStyle() { Val = "TableGrid"},
                    new TableWidth()
                {
                    Width = "5000", Type = TableWidthUnitValues.Pct
                }
                    );

                Table         invoiceTable = new Table();
                List <string> tableData    = new List <string>();
                invoiceTable.AppendChild <TableProperties>((TableProperties)tblprop.Clone());
                tableData.Add("Item");
                tableData.Add("Description");
                tableData.Add("Quantity");
                tableData.Add("Unit Price");
                tableData.Add("Item Total price");
                invoiceTable = AppendInvoiceTableInfo(tableData, invoiceTable, true);
                for (int i = 0; i < this._itemListViewModel.InvoiceList.Count(); i++)
                {
                    tableData = new List <string>();
                    tableData.Add(this._itemListViewModel.InvoiceList[i].item);
                    tableData.Add(this._itemListViewModel.InvoiceList[i].description);
                    tableData.Add(this._itemListViewModel.InvoiceList[i].quantity);
                    tableData.Add(this._itemListViewModel.InvoiceList[i].unitPrice);
                    tableData.Add(this._itemListViewModel.InvoiceList[i].totalPrice);
                    invoiceTable = AppendInvoiceTableInfo(tableData, invoiceTable, false);
                }
                body.Append(invoiceTable);

                #endregion

                body.Append(new Paragraph(new Run(new Text("\n"))));

                #region total price output


                double itemSubTotal = 0;
                for (int i = 0; i < this._itemListViewModel.InvoiceList.Count; i++)
                {
                    itemSubTotal += Math.Round(Convert.ToDouble(this._itemListViewModel.InvoiceList[i].totalPrice), 2);
                }

                double totalPrice = 0;
                double hstAmount  = 0;

                totalPrice = Math.Round(itemSubTotal * (1 + Convert.ToDouble(this.hST) / 100), 2);
                hstAmount  = Math.Round(itemSubTotal * Convert.ToDouble(this.hST) / 100, 2);


                TableProperties totalPriceTableProp = new TableProperties(
                    new TableBorders(
                        new TopBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new BottomBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new LeftBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new RightBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new InsideHorizontalBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                },
                        new InsideVerticalBorder()
                {
                    Val = new EnumValue <BorderValues>(BorderValues.None), Size = 0, Space = 0
                }),
                    new TableCellVerticalAlignment()
                {
                    Val = TableVerticalAlignmentValues.Center
                },
                    new TableWidth()
                {
                    Width = "5000", Type = TableWidthUnitValues.Pct
                });

                Table totalPriceTable = new Table();

                totalPriceTable.AppendChild <TableProperties>((TableProperties)totalPriceTableProp.Clone());

                List <string> totalPriceTableData = new List <string>();
                totalPriceTableData.Add("Sub Total:");
                totalPriceTableData.Add(itemSubTotal.ToString("N2"));
                totalPriceTable = AppendTotalPriceTableInfo(totalPriceTableData, totalPriceTable, false);


                totalPriceTableData = new List <string>();
                totalPriceTableData.Add("HST:");
                totalPriceTableData.Add(hstAmount.ToString("N2"));
                totalPriceTable = AppendTotalPriceTableInfo(totalPriceTableData, totalPriceTable, false);

                totalPriceTableData = new List <string>();
                totalPriceTableData.Add("Total:");
                totalPriceTableData.Add(totalPrice.ToString("N2"));
                totalPriceTable = AppendTotalPriceTableInfo(totalPriceTableData, totalPriceTable, false);

                totalPriceTableData = new List <string>();
                totalPriceTableData.Add("Paid:");
                totalPriceTableData.Add(this.paidAmount);
                totalPriceTable = AppendTotalPriceTableInfo(totalPriceTableData, totalPriceTable, false);

                totalPriceTableData = new List <string>();
                totalPriceTableData.Add("Balance:");
                totalPriceTableData.Add((totalPrice - Convert.ToDouble(this.paidAmount)).ToString("N2"));
                totalPriceTable = AppendTotalPriceTableInfo(totalPriceTableData, totalPriceTable, false);
                body.Append(totalPriceTable);



                //Paragraph totalPricePara = new Paragraph();
                //ParagraphProperties totalPriceParaP = new ParagraphProperties();
                //Justification pricePositionJustification = new Justification() { Val = JustificationValues.Right };
                //totalPriceParaP.Append(pricePositionJustification);


                //Run totalPriceRun = new Run();
                //RunProperties totalPriceRunP = new RunProperties();
                //FontSize totalPriceFontSize = new FontSize() { Val = "22" };
                //Color totalPriceFontColor = new Color() { Val = "365F91" };
                //MarginHeight totalPriceMargin = new MarginHeight() { Val = 0 };
                //Text subTotalPriceLine = new Text("Sub Toal: " + itemSubTotal.ToString("N2"));
                //Text HSTAmountLine = new Text("HST: " + hstAmount.ToString("N2"));
                //Text totalPriceLine = new Text("Total: " + totalPrice.ToString("N2"));
                //Text paidAmount = new Text("Paid: ");
                //Text balance = new Text("Balance: " );
                //totalPriceRunP.Append(totalPriceFontSize);
                //totalPriceRunP.Append(totalPriceFontColor);
                //totalPriceRunP.Append(totalPriceMargin);
                //totalPriceRun.Append(totalPriceRunP);
                //totalPriceRun.Append(subTotalPriceLine);
                //totalPriceRun.Append(new Break());
                //totalPriceRun.Append(HSTAmountLine);
                //totalPriceRun.Append(new Break());
                //totalPriceRun.Append(totalPriceLine);
                //totalPriceRun.Append(new Break());
                //totalPriceRun.Append(paidAmount);
                //totalPriceRun.Append(new Break());
                //totalPriceRun.Append(balance);


                //totalPricePara.Append(totalPriceParaP);
                //totalPricePara.Append(totalPriceRun);
                //body.Append(totalPricePara);


                #endregion

                document.Close();
            }
            Word.Application wordApp = new Word.Application();

            wordApp.Documents.Open(pathName + @"\" + "Invoice " + invoiceID.ToString() + ".docx");

            wordApp.Visible = true;
        }