private static void FillInvoice(bool isWarranty, InvoicePrint invoicePrint, string invoiceNumber, InvoiceEx invoiceEx = null)
        {
            string[] currencyType = new string[2];

            word = new WordService(fileName, false);

            try {
                word.FindReplace("[brokerName]", order.Auction.SupplierOrders[0].BrokerName);
                word.FindReplace("[brokerKbe]", order.Auction.SupplierOrders[0].BrokerKbe);
                word.FindReplace("[brokerBIN]", order.Auction.SupplierOrders[0].BrokerBIN);
                word.FindReplace("[brokerBankName]", order.Auction.SupplierOrders[0].BrokerBankName);
                word.FindReplace("[brokerBIK]", order.Auction.SupplierOrders[0].BrokerBIK);
                word.FindReplace("[payCode]", isWarranty ? "171" : "859");
                word.FindReplace("[invoiceNumber]", invoiceNumber);
                word.FindReplace("[invoiceDate]", isWarranty ? DateTime.Now.ToShortDateString() : order.Auction.Date.ToShortDateString());
                word.FindReplace("[supplierName]", order.Auction.SupplierOrders[0].BrokerName);
                word.FindReplace("[supplierBIN]", order.Auction.SupplierOrders[0].BrokerBIN);
                word.FindReplace("[supplierAddress]", order.Auction.SupplierOrders[0].BrokerAddress);
                word.FindReplace("[buyerName]", order.Auction.SupplierOrders[0].Name);
                word.FindReplace("[buyerBIN]", order.Auction.SupplierOrders[0].BIN);
                word.FindReplace("[buyerAddress]", order.Auction.SupplierOrders[0].Address);
                word.FindReplace("[contractNum]", "№" + order.Auction.SupplierOrders[0].ContractNum);
                word.FindReplace("[contractDate]", order.Auction.SupplierOrders[0].ContractDate.ToShortDateString());
                word.SetCell(3, 2, 1, "1");

                if (isWarranty)
                {
                    word.SetCell(3, 2, 3, "Гарантийное обеспечение за участие в аукционе №" + order.Auction.Number + " от " + order.Auction.Date.ToShortDateString());
                }
                else
                {
                    word.SetCell(3, 2, 3, "Брокерские услуги за участие в аукционе №" + order.Auction.Number + " от " + order.Auction.Date.ToShortDateString());
                }

                if (order.Auction.SiteId == 4)
                {
                    word.AddTableRow(3);
                    word.SetCell(3, 3, 1, "2");
                    word.SetCell(3, 3, 3, "Открытие и ведение счета на товарной бирже");
                    word.SetCell(3, 3, 4, "1");
                    word.SetCell(3, 3, 5, "шт.");
                    word.SetCell(3, 3, 6, invoiceEx.Services[1].Price.ToString());
                    word.SetCell(3, 3, 7, invoiceEx.Services[1].Аmount.ToString());
                }

                word.SetCell(3, 2, 6, invoicePrint.Price);
                word.SetCell(3, 2, 7, invoicePrint.Аmount);
                word.FindReplace("[count]", order.Auction.SiteId == 4 ? "2" : "1");

                if (order.Auction.SiteId == 4)
                {
                    if (order.Auction.SupplierOrders[0].BIN == "141040012412")
                    {
                        invoicePrint.Total = (invoiceEx.Services[0].Аmount + invoiceEx.Services[1].Аmount).ToString();
                    }
                    else
                    {
                        invoicePrint.Total = (invoiceEx.Services[0].Аmount + invoiceEx.Services[1].Аmount).ToString() + ",00";
                    }

                    if (order.Auction.SupplierOrders[0].BrokerName.ToUpper().Contains("АЛТЫН"))
                    {
                        invoicePrint.TotalTax = "";
                    }
                    else
                    {
                        invoicePrint.TotalTax = ((invoiceEx.Services[0].Аmount + invoiceEx.Services[1].Аmount) * 12 / 112).ToString();
                    }
                }

                word.FindReplace("[invoiceSum]", invoicePrint.Total);
                word.FindReplace("[ndsSum]", invoicePrint.TotalTax);
                word.FindReplace("[invoiceSum]", invoicePrint.Total);

                if (invoicePrint.АmountInWords.Contains("тиын"))
                {
                    word.FindReplace("[currency]", "KZT");
                    currencyType[0] = " тенге";
                    currencyType[1] = " тиын";

                    if (order.Auction.SupplierOrders[0].BrokerName.ToUpper().Contains("КОРУНД"))
                    {
                        word.FindReplace("[brokerIIK]", order.Auction.SupplierOrders[0].BrokerIIK);
                    }
                    else
                    {
                        word.FindReplace("[brokerIIK]", order.Auction.SupplierOrders[0].BrokerIIK);
                    }
                }
                else if (invoicePrint.АmountInWords.Contains("копеек"))
                {
                    word.FindReplace("[currency]", "RUB");
                    currencyType[0] = " рублей";
                    currencyType[1] = " копеек";

                    if (order.Auction.SupplierOrders[0].BrokerName.ToUpper().Contains("КОРУНД"))
                    {
                        word.FindReplace("[brokerIIK]", "KZ739261802171874002");
                    }
                    else
                    {
                        word.FindReplace("[brokerIIK]", order.Auction.SupplierOrders[0].BrokerIIK);
                    }
                }
                else if (invoicePrint.АmountInWords.Contains("центов"))
                {
                    word.FindReplace("[currency]", "USD");
                    currencyType[0] = " долларов";
                    currencyType[1] = " центов";

                    if (order.Auction.SupplierOrders[0].BrokerName.ToUpper().Contains("КОРУНД"))
                    {
                        word.FindReplace("[brokerIIK]", "KZ039261802171874001");
                    }
                    else
                    {
                        word.FindReplace("[brokerIIK]", order.Auction.SupplierOrders[0].BrokerIIK);
                    }
                }

                if (order.Auction.SiteId == 4)
                {
                    invoicePrint.АmountInWords = "Всего к оплате: " + ConvertNumberToWord(Convert.ToInt32(invoiceEx.Services[0].Аmount + invoiceEx.Services[1].Аmount)) + currencyType[0] + ",00" + currencyType[1];
                }

                word.FindReplace("[amountInWords]", invoicePrint.АmountInWords);
            } catch (Exception ex) { Debug.WriteLine("Err:" + ex); }

            word.SaveAsPDF(fileName);

            word.CloseDocument(true);
            word.CloseWord(true);
        }
        public static String CreateInvoice(int brokerType, bool isWarranty, Order order, bool onlyPlay = false)
        {
            AppJournal.Write("1C Transport", "Create invoice", true);

            ChooseBaseType(brokerType);

            string companyBin     = order.Auction.SupplierOrders[0].BIN;
            string contractNumber = order.Auction.SupplierOrders[0].ContractNum;

            if (contractNumber.ToLower().Contains("с") || contractNumber.ToLower().Contains("c"))
            {
                // Check for contract exist (russian & english letter conflict)
                contractNumber = contractNumber.Replace("с", "c"); // Eng->rus

                if (SearchContract(companyBin, contractNumber, brokerType) == null)
                {
                    contractNumber = contractNumber.Replace("c", "с"); // Rus->eng

                    if (SearchContract(companyBin, contractNumber, brokerType) == null)
                    {
                        companyBin     = companyBin.PadLeft(12, '0');
                        contractNumber = contractNumber.Replace("с", "c"); // Eng->rus

                        if (SearchContract(companyBin, contractNumber, brokerType) == null)
                        {
                            contractNumber = contractNumber.Replace("c", "с"); // Rus->eng

                            if (SearchContract(companyBin, contractNumber, brokerType) == null)
                            {
                                return("Err: Договора не существует");
                            }
                        }
                    }
                }
            }
            else
            {
                if (SearchContract(companyBin, contractNumber, brokerType) == null)
                {
                    companyBin = companyBin.PadLeft(12, '0');

                    if (SearchContract(companyBin, contractNumber, brokerType) == null)
                    {
                        return("Err: Договора не существует");
                    }
                }
            }

            var invoiceEx = new InvoiceEx();

            invoiceEx.IncludeVAT = true;

            if (brokerType == 3)
            {
                invoiceEx.IncludeVAT = false;
            }

            invoiceEx.DocNum  = "";
            invoiceEx.DocDate = isWarranty ? DateTime.Now.Date : order.Auction.Date.Date;

            invoiceEx.Comments = ".NET Автоматизация (" + order.Auction.Trader + ")";

            invoiceEx.Client     = new Clients();
            invoiceEx.Client.IIN = companyBin;

            invoiceEx.Contract = new Contracts();
            invoiceEx.Contract.ContractNumber = order.Auction.SupplierOrders[0].ContractNum;
            invoiceEx.Contract.ClientIIN      = companyBin;
            invoiceEx.Contract.CtlgCode       = "";

            invoiceEx.OrganizationBankAccount                  = new BankAccounts();
            invoiceEx.OrganizationBankAccount.CtlgCode         = "";
            invoiceEx.OrganizationBankAccount.BankCode         = "";
            invoiceEx.OrganizationBankAccount.OrganizationtIIN = order.Auction.SupplierOrders[0].BrokerBIN;

            switch (order.Auction.SupplierOrders[0].CurrencyCode)
            {
            case "KZT":
                invoiceEx.OrganizationBankAccount.AccountNumber = order.Auction.SupplierOrders[0].BrokerIIK;
                break;

            case "RUB":
                invoiceEx.OrganizationBankAccount.AccountNumber = brokerType == 1 ? order.Auction.SupplierOrders[0].BrokerIIK : brokerType == 2 ? "KZ739261802171874002" : order.Auction.SupplierOrders[0].BrokerIIK;
                break;

            case "USD":
                invoiceEx.OrganizationBankAccount.AccountNumber = brokerType == 1 ? order.Auction.SupplierOrders[0].BrokerIIK : brokerType == 2 ? "KZ039261802171874001" : "KZ889470840991144845";
                break;

            case "EUR":
                invoiceEx.OrganizationBankAccount.AccountNumber = brokerType == 1 ? order.Auction.SupplierOrders[0].BrokerIIK : brokerType == 2 ? order.Auction.SupplierOrders[0].BrokerIIK : "KZ309470978907126878";
                break;
            }

            var percent = order.Auction.InvoicePercent;

            if (isWarranty)
            {
                invoiceEx.PaymentCode = "171";
                invoiceEx.Services    = new ServicesRow[order.Auction.SiteId == 4 ? 2 : 1];

                invoiceEx.Services[0]                = new ServicesRow();
                invoiceEx.Services[0].Service        = "Гарантийное обеспечение";
                invoiceEx.Services[0].ServiceContent = "Гарантийное обеспечение по аукциону №" + order.Auction.Number;
                invoiceEx.Services[0].Quantity       = 1;

                if (order.Auction.Lots[0].Name.ToUpper().Contains("БЕНЗИН") || order.Auction.Lots[0].Name.ToUpper().Contains("ДИЗЕЛЬ"))
                {
                    invoiceEx.Services[0].Price = (float)Math.Round(order.Auction.SupplierOrders[0].MinimalPrice / 100 * Convert.ToDecimal(0.4), 0);
                }
                else
                {
                    if (order.Auction.SupplierOrders[0].BIN == "141040012412")
                    {
                        invoiceEx.Services[0].Price = (float)Math.Round(order.Auction.SupplierOrders[0].MinimalPrice / 100 * Convert.ToDecimal(percent), 2);
                    }
                    else
                    {
                        invoiceEx.Services[0].Price = (float)Math.Round(order.Auction.SupplierOrders[0].MinimalPrice / 100 * Convert.ToDecimal(percent), 0);
                    }
                }

                if (order.Auction.SupplierOrders[0].BIN == "141040012412")
                {
                    invoiceEx.Services[0].Аmount = (float)Math.Round((decimal)(invoiceEx.Services[0].Price * invoiceEx.Services[0].Quantity), 2);
                }
                else
                {
                    invoiceEx.Services[0].Аmount = (float)Math.Round((decimal)(invoiceEx.Services[0].Price * invoiceEx.Services[0].Quantity), 0);
                }

                if (order.Auction.SiteId == 4)
                {
                    invoiceEx.Services[1]                = new ServicesRow();
                    invoiceEx.Services[1].Service        = "Открытие и введение счета на товарной бирже";
                    invoiceEx.Services[1].ServiceContent = "Открытие и ведение счета на товарной бирже";
                    invoiceEx.Services[1].Quantity       = 1;
                    invoiceEx.Services[1].Price          = order.Auction.SupplierOrders[0].MinimalPrice < 1000000 ? 500 : 5000;
                    invoiceEx.Services[1].Аmount         = order.Auction.SupplierOrders[0].MinimalPrice < 1000000 ? 500 : 5000;
                }
            }
            else
            {
                invoiceEx.PaymentCode = "859";
                invoiceEx.Services    = new ServicesRow[order.Auction.SiteId == 4 ? 2 : 1];

                invoiceEx.Services[0]                = new ServicesRow();
                invoiceEx.Services[0].Service        = "Брокерские услуги";
                invoiceEx.Services[0].ServiceContent = "Брокерские услуги по аукциону №" + order.Auction.Number;
                invoiceEx.Services[0].Quantity       = 1;

                if (order.Auction.Lots[0].Name.ToUpper().Contains("БЕНЗИН") || order.Auction.Lots[0].Name.ToUpper().Contains("ДИЗЕЛЬ"))
                {
                    invoiceEx.Services[0].Price = onlyPlay?0: (float)Math.Round(order.Auction.SupplierOrders[0].MinimalPrice / 100 * Convert.ToDecimal(0.4), 0);
                }
                else
                {
                    if (order.Auction.SupplierOrders[0].BIN == "141040012412")
                    {
                        invoiceEx.Services[0].Price = onlyPlay ? 0 : (float)Math.Round(order.Auction.SupplierOrders[0].MinimalPrice / 100 * Convert.ToDecimal(percent), 2);
                    }
                    else
                    {
                        invoiceEx.Services[0].Price = onlyPlay ? 0 : (float)Math.Round(order.Auction.SupplierOrders[0].MinimalPrice / 100 * Convert.ToDecimal(percent), 0);
                    }
                }

                if (order.Auction.SupplierOrders[0].BIN == "141040012412")
                {
                    invoiceEx.Services[0].Аmount = onlyPlay ? 0 : (float)Math.Round((decimal)(invoiceEx.Services[0].Price * invoiceEx.Services[0].Quantity), 2);
                }
                else
                {
                    invoiceEx.Services[0].Аmount = onlyPlay ? 0 : (float)Math.Round((decimal)(invoiceEx.Services[0].Price * invoiceEx.Services[0].Quantity), 0);
                }

                if (order.Auction.SiteId == 4)
                {
                    invoiceEx.Services[1]                = new ServicesRow();
                    invoiceEx.Services[1].Service        = "Открытие и введение счета на товарной бирже";
                    invoiceEx.Services[1].ServiceContent = "Открытие и ведение счета на товарной бирже";
                    invoiceEx.Services[1].Quantity       = 1;
                    invoiceEx.Services[1].Price          = order.Auction.SupplierOrders[0].MinimalPrice < 1000000 ? 500 : 5000;
                    invoiceEx.Services[1].Аmount         = order.Auction.SupplierOrders[0].MinimalPrice < 1000000 ? 500 : 5000;
                }
            }

            responseData = baseConn.writeInvoiceEx(invoiceEx, false, isWarranty);

            if (responseData.RequestSuccess)
            {
                return((responseData.ResponseObject as InvoiceSearch).DocNum);
            }
            else
            {
                AppJournal.Write("1C Transport", "Create invoice error :" + responseData.ErrorMsg, true);
                return("Err: " + responseData.ErrorMsg);
            }
        }