Exemplo n.º 1
0
        // typeCode = 0 - generate invoice
        // typeCode = 1 - generate invoice duplicate eith current date
        public string generate(int typeCode)
        {
            string    path = "templates/invoice.xml";
            XDocument doc  = XDocument.Load(path);
            XElement  root = doc.Element("Template");

            string dateTimeFormat = "yyyy-MM-dd";

            string header = root.Element("Header").Value;

            header = string.Format(header, Company.Name, Company.FullAddress, Company.Phone, Company.Email, Company.Website, Company.BankName, Company.BankAccountNumber);

            string title       = root.Element("Title").Value;
            string dateOfIssue = DateOfIssue.ToString(dateTimeFormat);

            if (typeCode == 1)
            {
                string duplicateTitle = " - DUPLIKAT";
                Number      = Number + duplicateTitle;
                dateOfIssue = DateTime.Now.ToString(dateTimeFormat);
            }

            title = string.Format(title, Number);

            string cityOfIssue    = root.Element("DatePlace").Value;
            string dateOfDelivery = DateOfDelivery.ToString(dateTimeFormat);

            cityOfIssue = string.Format(cityOfIssue, dateOfIssue, Company.InvoiceIssueCity, dateOfDelivery);



            string sellerBuyer = root.Element("SellerBuyer").Value;

            sellerBuyer = string.Format(sellerBuyer, Company.FullName, Company.FullAddress, Company.NIP, Contractor.FullName, Contractor.FullAddress, Contractor.NIP);

            header += cityOfIssue += title += sellerBuyer;

            string invoiceItemsTable   = root.Element("InvoiceItemsTableHeader").Value;
            string invoiceItemsSummary = root.Element("InvoiceItemTableSummary").Value;
            string invoiceItem         = root.Element("InvoiceItem").Value;

            int i = 1;

            foreach (InvoiceItem item in InvoiceItems)
            {
                string newItem = string.Format(invoiceItem, i, item.Name, item.UnitOfMeasureShortName, item.Quantity, item.Price.ToString("0.00"), item.TotalPrice.ToString("0.00"), item.VATValue.ToString("0.00"), item.TotalVATValue.ToString("0.00"), item.TotalPriceBrutto.ToString("0.00"));
                invoiceItemsTable += newItem;
                i++;
            }
            invoiceItemsSummary = string.Format(invoiceItemsSummary, TotalValue.ToString("0.00"), TotalVATValue.ToString("0.00"), TotalValueInclVat.ToString("0.00"));
            invoiceItemsTable  += invoiceItemsSummary;

            string taxTableHeader = root.Element("TaxTableHeader").Value;
            string tax            = root.Element("Tax").Value;

            tax = string.Format(tax, "23", TotalValue.ToString("0.00"), TotalVATValue.ToString("0.00"), TotalValueInclVat.ToString("0.00"));
            string taxTableSummary = root.Element("TaxTableSummary").Value;

            taxTableSummary = string.Format(taxTableSummary, TotalValue.ToString("0.00"), TotalVATValue.ToString("0.00"), TotalValueInclVat.ToString("0.00"));
            string taxTable = taxTableHeader + tax + taxTableSummary;

            string priceSummary = root.Element("PriceSummary").Value;

            priceSummary = string.Format(priceSummary, TotalValueInclVat.ToString("0.00"), getValueInWords(TotalValueInclVat));
            string paymentMethod = root.Element("PaymentMethod").Value;

            paymentMethod = string.Format(paymentMethod, PaymentMethod.Name, DateOfIssue.AddDays(PaymentMethod.DueTerm).ToString(dateTimeFormat));
            string issuer = root.Element("Issuer").Value;

            issuer = string.Format(issuer, Company.InvoiceIssuerName);
            string footer = paymentMethod + issuer;


            //string output = header + invoiceItemsTable + priceSummary + footer;
            string output = header + invoiceItemsTable + taxTable + priceSummary + footer;

            output = output.Replace("~^~^", "{{");
            output = output.Replace("^~^~", "}}");
            output = output.Replace("~^", "{");
            output = output.Replace("^~", "}");


            string time = DateTime.Now.ToFileTime().ToString();

            string outputFile = Tools.getHash(sellerBuyer + time);

            File.WriteAllText("tmp/" + outputFile + ".tex", output);

            Process process = new Process();

            process.StartInfo.WorkingDirectory = "tmp";
            process.StartInfo.FileName         = "pdflatex";
            process.StartInfo.Arguments        = outputFile + ".tex";
            process.Start();

            // wait to avoid FileNotFoundException
            //Task.Delay(5000);
            process.Dispose();
            return(outputFile + ".pdf");
        }