public static void DataInvoicesActionView(BasePage page, Invoice targetInvoice)
        {
            var invoiceItems     = Global.DaoFactory.GetInvoiceItemDao().GetAll();
            var invoiceItemsJson = JsonConvert.SerializeObject(invoiceItems.ConvertAll(item => new
            {
                id               = item.ID,
                title            = item.Title,
                stockKeepingUnit = item.StockKeepingUnit,
                description      = item.Description,
                price            = item.Price,
                quantity         = item.Quantity,
                stockQuantity    = item.StockQuantity,
                trackInventory   = item.TrackInventory,
                invoiceTax1ID    = item.InvoiceTax1ID,
                invoiceTax2ID    = item.InvoiceTax2ID
            }));

            var invoiceTaxes     = Global.DaoFactory.GetInvoiceTaxDao().GetAll();
            var invoiceTaxesJson = JsonConvert.SerializeObject(invoiceTaxes.ConvertAll(item => new
            {
                id          = item.ID,
                name        = item.Name,
                rate        = item.Rate,
                description = item.Description
            }));

            var invoiceSettings     = Global.TenantSettings.InvoiceSetting ?? InvoiceSetting.DefaultSettings;
            var invoiceSettingsJson = JsonConvert.SerializeObject(new
            {
                autogenerated = invoiceSettings.Autogenerated,
                prefix        = invoiceSettings.Prefix,
                number        = invoiceSettings.Number,
                terms         = invoiceSettings.Terms
            });

            var presetContactsJson = string.Empty;
            var presetContactID    = UrlParameters.ContactID;

            if (targetInvoice == null && presetContactID != 0)
            {
                var targetContact = Global.DaoFactory.GetContactDao().GetByID(presetContactID);
                if (targetContact != null)
                {
                    presetContactsJson = JsonConvert.SerializeObject(new
                    {
                        id                   = targetContact.ID,
                        displayName          = targetContact.GetTitle().HtmlEncode().ReplaceSingleQuote(),
                        smallFotoUrl         = ContactPhotoManager.GetSmallSizePhoto(targetContact.ID, targetContact is Company),
                        currencyAbbreviation = targetContact.Currency
                    });
                }
            }

            var currencyRates     = Global.DaoFactory.GetCurrencyRateDao().GetAll();
            var currencyRatesJson = JsonConvert.SerializeObject(currencyRates.ConvertAll(item => new
            {
                id           = item.ID,
                fromCurrency = item.FromCurrency,
                toCurrency   = item.ToCurrency,
                rate         = item.Rate
            }));

            var          apiServer    = new Api.ApiServer();
            const string apiUrlFormat = "{0}crm/contact/{1}/data.json";

            var contactInfoData   = string.Empty;
            var consigneeInfoData = string.Empty;

            if (targetInvoice != null)
            {
                if (targetInvoice.ContactID > 0)
                {
                    contactInfoData = apiServer.GetApiResponse(String.Format(apiUrlFormat, SetupInfo.WebApiBaseUrl, targetInvoice.ContactID), "GET");
                }
                if (targetInvoice.ConsigneeID > 0)
                {
                    consigneeInfoData = apiServer.GetApiResponse(String.Format(apiUrlFormat, SetupInfo.WebApiBaseUrl, targetInvoice.ConsigneeID), "GET");
                }
            }
            else if (presetContactID != 0)
            {
                contactInfoData = apiServer.GetApiResponse(String.Format(apiUrlFormat, SetupInfo.WebApiBaseUrl, presetContactID), "GET");
            }

            var apiUrl = String.Format("{0}crm/invoice/{1}.json",
                                       SetupInfo.WebApiBaseUrl,
                                       targetInvoice != null ? targetInvoice.ID.ToString(CultureInfo.InvariantCulture) : "sample");
            var invoiceData = apiServer.GetApiResponse(apiUrl, "GET");

            var script = String.Format(@"
                                        var invoiceItems = '{0}';
                                        var invoiceTaxes = '{1}';
                                        var invoiceSettings = '{2}';
                                        var invoicePresetContact = '{3}';
                                        var currencyRates = '{4}';
                                        var invoiceJsonData = '{5}';
                                        var countryListExt = {6};
                                        var currentCultureName = '{7}'; ",
                                       Global.EncodeTo64(invoiceItemsJson),
                                       Global.EncodeTo64(invoiceTaxesJson),
                                       Global.EncodeTo64(invoiceSettingsJson),
                                       Global.EncodeTo64(presetContactsJson),
                                       Global.EncodeTo64(currencyRatesJson),
                                       targetInvoice != null ? Global.EncodeTo64(targetInvoice.JsonData) : "",
                                       JsonConvert.SerializeObject(Global.GetCountryListExt()),
                                       new RegionInfo(CultureInfo.CurrentCulture.Name).EnglishName
                                       );

            page.RegisterInlineScript(script, onReady: false);
            page.JsonPublisher(contactInfoData, "invoiceContactInfo");
            page.JsonPublisher(consigneeInfoData, "invoiceConsigneeInfo");
            page.JsonPublisher(invoiceData, "invoice");
        }
        public static void DataContactActionView(BasePage page, Contact targetContact, List <CustomField> data, List <ContactInfo> networks)
        {
            var tags = targetContact != null?Global.DaoFactory.GetTagDao().GetEntityTags(EntityType.Contact, targetContact.ID) : new string[]
            {
            };
            var availableTags = Global.DaoFactory.GetTagDao().GetAllTags(EntityType.Contact).Where(item => !tags.Contains(item));

            String json;

            using (var stream = new MemoryStream())
            {
                var serializer = new DataContractJsonSerializer(data.GetType());
                serializer.WriteObject(stream, data);
                json = Encoding.UTF8.GetString(stream.ToArray());
            }

            var listItems = Global.DaoFactory.GetListItemDao().GetItems(ListType.ContactType);

            var presetCompanyForPersonJson = "";

            if (targetContact != null && targetContact is Person && ((Person)targetContact).CompanyID > 0)
            {
                var company = Global.DaoFactory.GetContactDao().GetByID(((Person)targetContact).CompanyID);
                if (company == null)
                {
                    log4net.LogManager.GetLogger("ASC.CRM").ErrorFormat("Can't find parent company (CompanyID = {0}) for person with ID = {1}", ((Person)targetContact).CompanyID, targetContact.ID);
                }
                else
                {
                    presetCompanyForPersonJson = JsonConvert.SerializeObject(new
                    {
                        id           = company.ID,
                        displayName  = company.GetTitle().HtmlEncode().ReplaceSingleQuote().Replace(@"\", @"\\"),
                        smallFotoUrl = ContactPhotoManager.GetSmallSizePhoto(company.ID, true)
                    });
                }
            }

            var presetPersonsForCompanyJson = "";

            if (targetContact != null && targetContact is Company)
            {
                var people = Global.DaoFactory.GetContactDao().GetMembers(targetContact.ID);
                if (people.Count != 0)
                {
                    presetPersonsForCompanyJson = JsonConvert.SerializeObject(people.ConvertAll(item => new
                    {
                        id           = item.ID,
                        displayName  = item.GetTitle().HtmlEncode().ReplaceSingleQuote().Replace(@"\", @"\\"),
                        smallFotoUrl = ContactPhotoManager.GetSmallSizePhoto(item.ID, false)
                    }));
                }
            }

            var script = String.Format(@"
                                var customFieldList = {0};
                                var contactNetworks = {1};
                                var contactActionTags = {2};
                                var contactActionAvailableTags = {3};
                                var contactAvailableTypes = {4};
                                var presetCompanyForPersonJson = '{5}';
                                var presetPersonsForCompanyJson = '{6}';
                                var facebokSearchEnabled = {7};
                                var twitterSearchEnabled = {8};
                                var contactActionCurrencies = {9};
                                var countryListExt = {10};
                                var currentCultureName = '{11}'; ",
                                       json,
                                       JsonConvert.SerializeObject(networks),
                                       JsonConvert.SerializeObject(tags.ToList().ConvertAll(t => t.HtmlEncode())),
                                       JsonConvert.SerializeObject(availableTags.ToList().ConvertAll(t => t.HtmlEncode())),
                                       JsonConvert.SerializeObject(
                                           listItems.ConvertAll(n => new
            {
                id    = n.ID,
                title = n.Title.HtmlEncode()
            })),
                                       presetCompanyForPersonJson,
                                       presetPersonsForCompanyJson,
                                       IsFacebookSearchEnabled.ToString().ToLower(),
                                       IsTwitterSearchEnabled.ToString().ToLower(),
                                       JsonConvert.SerializeObject(CurrencyProvider.GetAll()),
                                       JsonConvert.SerializeObject(Global.GetCountryListExt()),
                                       new RegionInfo(CultureInfo.CurrentCulture.Name).EnglishName
                                       );

            page.RegisterInlineScript(script, onReady: false);
        }