private async Task <bool> LoadTickets(DateTime startDate, DateTime endDate)
        {
            return(await Task.Factory.StartNew(() =>
            {
                if (_checkFoCancellation())
                {
                    ReportProgressEvent?.Invoke(_index, 100, true);
                    return false;
                }

                var message = string.Format("Loading Tickets from {0:y} to {1:y}. Loaded {2} tickets.", startDate, endDate, _ticketCount);
                ReportStatusEvent?.Invoke(message);
                ReportProgressEvent?.Invoke(_index, 0, false);

                var tickets = new CommitCRM.ObjectQuery <CommitCRM.Ticket>(CommitCRM.LinkEnum.linkAND, ITEMS_COUNT_TO_LOAD);
                tickets.AddCriteria(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.OperatorEnum.opGreaterThan, startDate);
                tickets.AddCriteria(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.OperatorEnum.opLessThan, endDate);
                tickets.AddSortExpression(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.SortDirectionEnum.sortASC);
                foreach (var item in _ticketGroups.SelectMany(x => x))
                {
                    tickets.AddCriteria(CommitCRM.Ticket.Fields.TicketREC_ID, CommitCRM.OperatorEnum.opNotLike, item.TicketREC_ID.ToString());
                }

                var ticketResult = tickets.FetchObjects();
                if (ticketResult != null && ticketResult.Any())
                {
                    _ticketCount += ticketResult.Count;
                    _ticketGroups.Add(ticketResult);
                }

                return ticketResult.Count == ITEMS_COUNT_TO_LOAD;
            }));
        }
Beispiel #2
0
        private DateTime GetCreationDate()
        {
            var defaultAccounts = new CommitCRM.ObjectQuery <CommitCRM.Account>(CommitCRM.LinkEnum.linkAND, 1);

            defaultAccounts.AddSortExpression(CommitCRM.Account.Fields.CreationDate, CommitCRM.SortDirectionEnum.sortASC);
            var defaultAccountResult = defaultAccounts.FetchObjects();

            var date = Directory.GetCreationTime(Properties.Settings.Default.InstalledLocation);

//            if (defaultAccountResult != null && defaultAccountResult.Count > 0)
//                date = defaultAccountResult[0].CreationDate;

            return(date);
        }
Beispiel #3
0
        protected async Task <Customer> CreateCustomerForTicket(CommitCRM.Ticket ticket, SQLiteConnection connection, string remoteHost)
        {
            var accounts = new CommitCRM.ObjectQuery <CommitCRM.Account>(CommitCRM.LinkEnum.linkAND, 1);

            accounts.AddCriteria(CommitCRM.Account.Fields.AccountREC_ID, CommitCRM.OperatorEnum.opEqual, ticket.AccountREC_ID);

            var accountsResult = accounts.FetchObjects();
            var account        = accountsResult.SingleOrDefault();

            if (account != null)
            {
                return(await ExportSingleCustomer(account, connection, remoteHost));
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        private void resetConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int result = -1;
            int rsult  = -1;

            RepairShoprUtils.LogWriteLineinHTML("Reseting Configuration including Database.. ", MessageSource.Initialization, "", messageType.Information);
            using (SQLiteConnection conn = new SQLiteConnection("data source=" + _path + ";PRAGMA journal_mode=WAL;Password=shyam;"))
            {
                conn.Open();
                using (SQLiteCommand cmdAccountDelete = new SQLiteCommand("DELETE From Account", conn))
                {
                    result = cmdAccountDelete.ExecuteNonQuery();
                }
                using (SQLiteCommand cmdTicketDelete = new SQLiteCommand("DELETE From Ticket", conn))
                {
                    rsult = cmdTicketDelete.ExecuteNonQuery();
                }

                var defaultTickets = new CommitCRM.ObjectQuery <CommitCRM.Ticket>(CommitCRM.LinkEnum.linkAND, 1);
                defaultTickets.AddSortExpression(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.SortDirectionEnum.sortASC);

                var    defaultTicketResult = defaultTickets.FetchObjects();
                string ticketNumber        = string.Empty;
                if (defaultTicketResult != null && defaultTicketResult.Count > 0)
                {
                    ticketNumber = defaultTicketResult[0].TicketNumber;
                }
                Properties.Settings.Default.TicketNumber = ticketNumber;

                var creationDate = GetCreationDate();
                Properties.Settings.Default.CustomerExport = creationDate;
                Properties.Settings.Default.TicketExport   = creationDate;
                Properties.Settings.Default.Save();

                if (result != -1 && rsult != -1)
                {
                    RepairShoprUtils.LogWriteLineinHTML("Reset Configuration including Database successfull.", MessageSource.Initialization, "", messageType.Information);

                    MessageBox.Show("Configuration is reset successfull", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                conn.Close();
            }
        }
Beispiel #5
0
        private string GetComment(CommitCRM.Ticket ticket)
        {
            var sb = new StringBuilder();

            sb.Append(string.Format("Ticket Number: {0}", ticket.TicketNumber.Replace("-", string.Empty)));
            sb.AppendFormat(" , Cause: {0}", ticket.GetFieldValue("FLDTKTCAUSE"));
            sb.AppendFormat(" , Source: {0}", ticket.Source);
            sb.AppendFormat(" , Category: {0}", ticket.GetFieldValue("FLDTKTCATEGORY"));
            sb.AppendFormat(" , Note: {0}", ticket.Notes);
            sb.AppendFormat(" , Resolution : {0}", ticket.Resolution);

            var charges = new CommitCRM.ObjectQuery <CommitCRM.Charge>();

            charges.AddCriteria(CommitCRM.Charge.Fields.TicketREC_ID, CommitCRM.OperatorEnum.opEqual, ticket.TicketREC_ID);

            var chargesResult = charges.FetchObjects();

            foreach (CommitCRM.Charge charge in chargesResult)
            {
                sb.AppendFormat(", Charge :{0}", charge.Description);
                sb.AppendFormat(", Amount : {0}", charge.GetFieldValue("FLDSLPBILLTOTAL"));
                sb.AppendFormat(", Quantity: {0}", charge.GetFieldValue("FLDSLPQUANTITY"));
                sb.AppendFormat(", Date : {0}", charge.Date);
            }

            var history = new CommitCRM.ObjectQuery <CommitCRM.HistoryNote>();

            history.AddCriteria(CommitCRM.HistoryNote.Fields.RelLinkREC_ID, CommitCRM.OperatorEnum.opEqual, ticket.TicketREC_ID);

            var historyResult = history.FetchObjects();

            foreach (CommitCRM.HistoryNote historyNote in historyResult)
            {
                sb.AppendFormat(" History Note :{0}", historyNote.Description);
                sb.AppendFormat(", Date : {0}", historyNote.Date);
            }

            return(sb.ToString());
        }
        private void resetConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int result = -1;
            int rsult = -1;
            RepairShoprUtils.LogWriteLineinHTML("Reseting Configuration including Database.. ", MessageSource.Initialization, "", messageType.Information);
            using (SQLiteConnection conn = new SQLiteConnection("data source=" + _path + ";PRAGMA journal_mode=WAL;Password=shyam;"))
            {
                conn.Open();
                using (SQLiteCommand cmdAccountDelete = new SQLiteCommand("DELETE From Account", conn))
                {
                    result = cmdAccountDelete.ExecuteNonQuery();
                }
                using (SQLiteCommand cmdTicketDelete = new SQLiteCommand("DELETE From Ticket", conn))
                {
                    rsult = cmdTicketDelete.ExecuteNonQuery();
                }

                CommitCRM.ObjectQuery<CommitCRM.Ticket> DefaultTickets = new CommitCRM.ObjectQuery<CommitCRM.Ticket>(CommitCRM.LinkEnum.linkAND, 1);
                DefaultTickets.AddSortExpression(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.SortDirectionEnum.sortASC);
                List<CommitCRM.Ticket> DefaultTicketResult = DefaultTickets.FetchObjects();
                string ticketNumber = string.Empty;
                if (DefaultTicketResult != null && DefaultTicketResult.Count > 0)
                    ticketNumber = DefaultTicketResult[0].TicketNumber;
                Properties.Settings.Default.TicketNumber = ticketNumber;

                CommitCRM.ObjectQuery<CommitCRM.Account> DefaultAccounts = new CommitCRM.ObjectQuery<CommitCRM.Account>(CommitCRM.LinkEnum.linkAND, 1);
                DefaultAccounts.AddSortExpression(CommitCRM.Account.Fields.CreationDate, CommitCRM.SortDirectionEnum.sortASC);
                List<CommitCRM.Account> DefaultAccountResult = DefaultAccounts.FetchObjects();
                DateTime customerExport = Directory.GetCreationTime(installedLocation);
                if (DefaultAccountResult != null && DefaultAccountResult.Count > 0)
                    customerExport = DefaultAccountResult[0].CreationDate;
                Properties.Settings.Default.CustomerExport = customerExport;
                Properties.Settings.Default.Save();

                if (result != -1 && rsult != -1)
                {
                    RepairShoprUtils.LogWriteLineinHTML("Reset Configuration including Database successfull.. ", MessageSource.Initialization, "", messageType.Information);

                    MessageBox.Show("Configuration is reset successfull", "RepairShoprApps", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private string GetCommentValue(CommitCRM.Ticket ticket)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("Ticket Number: {0}", ticket.TicketNumber.Replace("-", string.Empty)));
            try
            {
                sb.AppendFormat(" , Cause: {0}", ticket.GetFieldValue("FLDTKTCAUSE"));
                sb.AppendFormat(" , Source: {0}", ticket.Source);
                sb.AppendFormat(" , Category: {0}", ticket.GetFieldValue("FLDTKTCATEGORY"));
                sb.AppendFormat(" , Note: {0}", ticket.Notes);
                sb.AppendFormat(" , Resolution : {0}", ticket.Resolution);

                CommitCRM.ObjectQuery<CommitCRM.Charge> ChargeSearch = new CommitCRM.ObjectQuery<CommitCRM.Charge>();
                ChargeSearch.AddCriteria(CommitCRM.Charge.Fields.TicketREC_ID, CommitCRM.OperatorEnum.opEqual, ticket.TicketREC_ID);
                List<CommitCRM.Charge> Charges = ChargeSearch.FetchObjects();
                foreach (CommitCRM.Charge Charge in Charges)
                {
                    sb.AppendFormat(", Charge :{0}", Charge.Description);
                    sb.AppendFormat(", Amount : {0}", Charge.GetFieldValue("FLDSLPBILLTOTAL"));
                    sb.AppendFormat(", Quantity: {0}", Charge.GetFieldValue("FLDSLPQUANTITY"));
                    sb.AppendFormat(", Date : {0}", Charge.Date);
                }

                CommitCRM.ObjectQuery<CommitCRM.HistoryNote> HistoryNoteSearch = new CommitCRM.ObjectQuery<CommitCRM.HistoryNote>();
                HistoryNoteSearch.AddCriteria(CommitCRM.HistoryNote.Fields.RelLinkREC_ID, CommitCRM.OperatorEnum.opEqual, ticket.TicketREC_ID);
                List<CommitCRM.HistoryNote> HistoryNotes = HistoryNoteSearch.FetchObjects();
                foreach (CommitCRM.HistoryNote HistoryNote in HistoryNotes)
                {
                    sb.AppendFormat(" History Note :{0}", HistoryNote.Description);
                    sb.AppendFormat(", Date : {0}", HistoryNote.Date);
                }
            }
            catch (Exception ex)
            {
                RepairShoprUtils.LogWriteLineinHTML(string.Format("Failed to get Charge, HistoryNote of Ticket : {0} . Due to {1}", ticket.Description, ex.Message), MessageSource.Ticket, ex.StackTrace, messageType.Warning);

            }
            return sb.ToString();
        }
        public string  CreateContactForMissingTicket(CommitCRM.Ticket ticket,SQLiteConnection conn)
        {
            string customerID = string.Empty;
            CommitCRM.ObjectQuery<CommitCRM.Account> AccountSearch = new CommitCRM.ObjectQuery<CommitCRM.Account>();
            AccountSearch.AddCriteria(CommitCRM.Account.Fields.AccountREC_ID, CommitCRM.OperatorEnum.opEqual, ticket.AccountREC_ID);
            List<CommitCRM.Account> Accounts = AccountSearch.FetchObjects();
            foreach (CommitCRM.Account account in Accounts)
            {
                try
                {
                    RepairShoprUtils.LogWriteLineinHTML(string.Format("Creating Account with last Name : {0}", account.LastName), MessageSource.Customer, "", messageType.Information);
                    NameValueCollection myNameValueCollection = new NameValueCollection();
                    string fullname = account.GetFieldValue("FLDCRDCONTACT");
                    myNameValueCollection.Add("business_name", account.CompanyName);
                    if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrEmpty(account.LastName))
                        myNameValueCollection.Add("firstname", fullname.Replace(account.LastName, string.Empty));
                    else
                        myNameValueCollection.Add("firstname", fullname);
                    myNameValueCollection.Add("lastname", account.LastName);
                    if (account.EmailAddress1.Contains("@"))
                        myNameValueCollection.Add("email", account.EmailAddress1);
                    myNameValueCollection.Add("phone", account.Phone1);
                    myNameValueCollection.Add("mobile", account.Phone2);
                    myNameValueCollection.Add("address", account.AddressLine1);
                    myNameValueCollection.Add("address_2", account.AddressLine2);
                    myNameValueCollection.Add("city", account.City);
                    myNameValueCollection.Add("state", account.State);
                    myNameValueCollection.Add("zip", account.Zip);
                    myNameValueCollection.Add("notes", account.Notes);
                    var newCustomer = RepairShoprUtils.ExportCustomer(myNameValueCollection);
                    if (newCustomer != null)
                    {
                        using (SQLiteCommand cmdINewItem = new SQLiteCommand(string.Format("INSERT INTO  Account (AccountId,CustomerId) VALUES('{0}','{1}')", account.AccountREC_ID, newCustomer.Id), conn))
                            cmdINewItem.ExecuteNonQuery();
                        customerID = newCustomer.Id;
                        break;
                    }
                    else
                    {
                        RepairShoprUtils.LogWriteLineinHTML(string.Format("Faile to create account : {0} for Ticket : {1}", account.LastName,ticket.Description), MessageSource.Customer, "", messageType.Warning);
                    }
                }
                catch (Exception ex)
                {
                    RepairShoprUtils.LogWriteLineinHTML(string.Format("Faile to create account : {0} for Ticket : {1} dues to {3}", account.LastName, ticket.Description,ex.Message), MessageSource.Customer, ex.StackTrace, messageType.Error);
                }              

            }
            return customerID;
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {   
            int totalNumer = 0;
            int index = 1;
            int percentage = 1;
            int ticketCount = 0;
            int ticketIndex = 1;
            int customerCount = 0;
            int customerIndex = 1;
            int totalcountData = 0;

            lock (item_handle)
            {

                using (SQLiteConnection conn = new SQLiteConnection("data source=" + _path + ";PRAGMA journal_mode=WAL;Password=shyam;"))
                {
                    conn.Open();
                    if (_exportCustomer)
                    {
                        CommitCRM.ObjectQuery<CommitCRM.Account> DefaultAccounts = new CommitCRM.ObjectQuery<CommitCRM.Account>(CommitCRM.LinkEnum.linkAND, 1);
                        DefaultAccounts.AddSortExpression(CommitCRM.Account.Fields.CreationDate, CommitCRM.SortDirectionEnum.sortASC);
                        List<CommitCRM.Account> DefaultAccountResult = DefaultAccounts.FetchObjects();
                        DateTime customerExport = Directory.GetCreationTime(installedLocation);
                        if (DefaultAccountResult != null && DefaultAccountResult.Count > 0)
                            customerExport = DefaultAccountResult[0].CreationDate;
                        if (Properties.Settings.Default.CustomerExport != null && Properties.Settings.Default.CustomerExport > customerExport)
                            customerExport = Properties.Settings.Default.CustomerExport;
                       
                        while (customerExport < DateTime.Today)
                        {
                            if (bgw.CancellationPending)
                            {
                                RepairShoprUtils.LogWriteLineinHTML("Contact Exporting Process is Stoped or Cancelled by User", MessageSource.Customer, "", messageType.Warning);
                                bgw.ReportProgress(100, index);
                                return;
                            }
                            RepairShoprUtils.LogWriteLineinHTML("Loading Account from  " + customerExport.ToString() + "to " + customerExport.AddMonths(1).ToString(), MessageSource.Ticket, "", messageType.Information);
                            customerIndex = 1;
                            index = 1;
                            totalcountData = 0;
                            percentage = 1;
                            CommitCRM.ObjectQuery<CommitCRM.Account> Accounts = new CommitCRM.ObjectQuery<CommitCRM.Account>(CommitCRM.LinkEnum.linkAND, 1000);
                            Accounts.AddCriteria(CommitCRM.Account.Fields.CreationDate, CommitCRM.OperatorEnum.opGreaterThan, customerExport);
                            Accounts.AddCriteria(CommitCRM.Account.Fields.CreationDate, CommitCRM.OperatorEnum.opLessThan, customerExport.AddMonths(1));
                            Accounts.AddSortExpression(CommitCRM.Account.Fields.CreationDate, CommitCRM.SortDirectionEnum.sortASC);
                            _statusMessage = string.Format("Loading Account from {0:y}.., it will take 2-3 mintues", customerExport);
                            bgw.ReportProgress(percentage, index);
                            List<CommitCRM.Account> CommitCRMAccountLists = Accounts.FetchObjects();                           
                            if (CommitCRMAccountLists != null)
                            {
                                // totalNumer += CommitCRMAccountLists.Count;
                                customerCount = CommitCRMAccountLists.Count;
                                totalcountData = CommitCRMAccountLists.Count;

                            }
                            _statusMessage = "Sending to RepairShopr..";
                            bgw.ReportProgress(percentage, index);
                            foreach (CommitCRM.Account account in CommitCRMAccountLists)
                            {
                                try
                                {

                                    if (bgw.CancellationPending)
                                    {
                                        RepairShoprUtils.LogWriteLineinHTML("Contact Exporting Process is Stoped or Cancelled by User", MessageSource.Customer, "", messageType.Warning);
                                        bgw.ReportProgress(100, index);                                        
                                        break;
                                    }
                                    //if (string.IsNullOrEmpty(account.LastName))
                                    //{
                                    //    RepairShoprUtils.LogWriteLineinHTML("Some Contacts missing Last Name and other Field.So, it is skipping ", MessageSource.Customer, "", messageType.Warning);
                                    //    _statusMessage = string.Format("Customer : {0} has empty other value so, it is skipping", account.Contact);
                                    //    percentage = (100 * index) / totalNumer;
                                    //    bgw.ReportProgress(percentage, index);
                                    //    index++;
                                    //    // customerIndex++;
                                    //    continue;
                                    //}
                                    //if (account.AccountType_Text == "Account")
                                    //{
                                    string csutomerId = string.Empty;
                                    using (SQLiteCommand cmdItemAlready = new SQLiteCommand(string.Format("SELECT CustomerId FROM Account WHERE AccountId='{0}'", account.AccountREC_ID), conn))
                                    {
                                        using (SQLiteDataReader reader = cmdItemAlready.ExecuteReader())
                                        {
                                            while (reader.Read())
                                            {
                                                csutomerId = reader[0].ToString();
                                            }
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(csutomerId))
                                    {
                                        RepairShoprUtils.LogWriteLineinHTML(string.Format("Account With Last Name : {0}  is already exported", account.LastName), MessageSource.Customer, "", messageType.Warning);

                                        percentage = (100 * index) / totalcountData; //totalNumer;
                                        _statusMessage = string.Format("Customer : {0} is already Exported so, it is skipping", account.LastName);
                                        bgw.ReportProgress(percentage, index);
                                        index++;
                                        //customerIndex++;
                                        continue;
                                    }
                                    percentage = (100 * index) / totalcountData;//totalNumer;
                                    _statusMessage = string.Format("Exporting ( {0}/{1} ) of Account", customerIndex, customerCount);
                                    bgw.ReportProgress(percentage, index);
                                    RepairShoprUtils.LogWriteLineinHTML(string.Format("Creating Account with last Name : {0}", account.LastName), MessageSource.Customer, "", messageType.Information);
                                    NameValueCollection myNameValueCollection = new NameValueCollection();
                                    string fullname = account.GetFieldValue("FLDCRDCONTACT");
                                    myNameValueCollection.Add("business_name", account.CompanyName);
                                    if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrEmpty(account.LastName))
                                        myNameValueCollection.Add("firstname", fullname.Replace(account.LastName, string.Empty));
                                    else
                                        myNameValueCollection.Add("firstname", fullname);
                                    myNameValueCollection.Add("lastname", account.LastName);
                                    myNameValueCollection.Add("email", account.EmailAddress1);
                                    myNameValueCollection.Add("phone", account.Phone1);
                                    myNameValueCollection.Add("mobile", account.Phone2);
                                    myNameValueCollection.Add("address", account.AddressLine1);
                                    myNameValueCollection.Add("address_2", account.AddressLine2);
                                    myNameValueCollection.Add("city", account.City);
                                    myNameValueCollection.Add("state", account.State);
                                    myNameValueCollection.Add("zip", account.Zip);
                                    myNameValueCollection.Add("notes", account.Notes);
                                    var newCustomer = RepairShoprUtils.ExportCustomer(myNameValueCollection);
                                    if (newCustomer != null)
                                    {                                        
                                        using (SQLiteCommand cmdINewItem = new SQLiteCommand(string.Format("INSERT INTO  Account (AccountId,CustomerId) VALUES('{0}','{1}')", account.AccountREC_ID, newCustomer.Id), conn))
                                            cmdINewItem.ExecuteNonQuery();
                                        CommitCRM.ObjectQuery<CommitCRM.Contact> contactSearch = new CommitCRM.ObjectQuery<CommitCRM.Contact>();
                                        contactSearch.AddCriteria(CommitCRM.Contact.Fields.ParentAccountREC_ID, CommitCRM.OperatorEnum.opEqual,account.AccountREC_ID);
                                         contactSearch.AddCriteria(CommitCRM.Contact.Fields.AccountType, CommitCRM.OperatorEnum.opEqual,5);
                                        List<CommitCRM.Contact> contacts = contactSearch.FetchObjects();
                                        RepairShoprUtils.LogWriteLineinHTML(string.Format("There are {0} Contact with Account : {1} ", contacts.Count, fullname), MessageSource.Contact, "", messageType.Information);
                                        foreach (CommitCRM.Contact contact in contacts)
                                        {                                            
                                            _statusMessage = string.Format("Exporting Contact :{0} of Account {1}", contact.LastName, fullname);
                                            RepairShoprUtils.LogWriteLineinHTML(string.Format("Exported New Contact : {0} in RepairShopr ", contact.LastName), MessageSource.Contact, "", messageType.Information);
                                            percentage = (100 * index) / totalcountData;
                                            bgw.ReportProgress(percentage, index);
                                            string contactname = contact.GetFieldValue("FLDCRDCONTACT");
                                            NameValueCollection contactNameCollection = new NameValueCollection();
                                            if (account.EmailAddress1.Contains("@"))
                                                myNameValueCollection.Add("email", contact.EmailAddress1);
                                            contactNameCollection.Add("phone", contact.Phone1);
                                            contactNameCollection.Add("mobile", contact.Phone2);
                                            contactNameCollection.Add("address", contact.AddressLine1);
                                            contactNameCollection.Add("address_2", contact.AddressLine2);
                                            contactNameCollection.Add("city", contact.City);
                                            contactNameCollection.Add("state", contact.State);
                                            contactNameCollection.Add("zip", contact.Zip);
                                            contactNameCollection.Add("customer_id", newCustomer.Id);
                                            contactNameCollection.Add("name", contactname);
                                            var result = RepairShoprUtils.ExportContact(contactNameCollection);
                                            if (result != null)
                                            {
                                                _statusMessage = string.Format("Exported Contact :{0} of Account {1}", contact.LastName, fullname);
                                            }
                                            percentage = (100 * index) / totalcountData;///totalNumer;
                                            bgw.ReportProgress(percentage, index);
                                        }
                                        percentage = (100 * index) / totalcountData;//totalNumer;
                                        _statusMessage = string.Format("Exported ( {0}/{1} ) of Account", customerIndex, customerCount);
                                        customerIndex++;
                                        bgw.ReportProgress(percentage, index);

                                    }
                                    else
                                    {
                                        RepairShoprUtils.LogWriteLineinHTML(string.Format("Unable to create Account with last Name : {0}", account.LastName), MessageSource.Customer, "", messageType.Warning);
                                    }
                                    percentage = (100 * index) / totalcountData;
                                    bgw.ReportProgress(percentage, index);
                                   
                                }
                                catch (Exception ex)
                                {
                                    RepairShoprUtils.LogWriteLineinHTML("Failed to Export New Contact. Due to " + ex.Message, MessageSource.Customer, ex.StackTrace, messageType.Error);
                                }
                                index++;
                            }
                            RepairShoprUtils.LogWriteLineinHTML("Sucessfull Loaded contact up to   " + customerExport.AddMinutes(1).ToString(), MessageSource.Ticket, "", messageType.Information);
                            Properties.Settings.Default.CustomerExport = customerExport;
                            Properties.Settings.Default.Save();
                            customerExport = customerExport.AddMonths(1); //Add month by 1
                            
                        }
                        isCompleteCustomer = true;
                        bgw.ReportProgress(100, index);
                    }
                    if (_exportTicket)
                    {                     
                       
                        string startTicket = string.Empty;
                        int ticketNumber = 0;
                        CommitCRM.ObjectQuery<CommitCRM.Ticket> DefaultTickets = new CommitCRM.ObjectQuery<CommitCRM.Ticket>(CommitCRM.LinkEnum.linkAND, 1);
                        DefaultTickets.AddSortExpression(CommitCRM.Ticket.Fields.TicketNumber, CommitCRM.SortDirectionEnum.sortASC);
                        List<CommitCRM.Ticket> DefaultTicketResult = DefaultTickets.FetchObjects();
                        
                        if (DefaultTicketResult != null && DefaultTicketResult.Count > 0)
                            startTicket = DefaultTicketResult[0].TicketNumber;

                        var partTicketByPart = startTicket.Split('-');

                        if (!string.IsNullOrEmpty(Properties.Settings.Default.TicketNumber))
                            ticketNumber = int.Parse(Properties.Settings.Default.TicketNumber);
                        else
                        {
                            if (partTicketByPart.Length == 2)
                            {
                                ticketNumber = int.Parse(partTicketByPart[1]);
                            }
                        }
//                        if (Properties.Settings.Default.TicketExport != null && Properties.Settings.Default.TicketExport > exportTicket)
//                            exportTicket = Properties.Settings.Default.TicketExport;                       
                        while (ticketNumber <globalTicketNumber)
                        {
                            if (bgw.CancellationPending)
                            {
                                RepairShoprUtils.LogWriteLineinHTML("Ticket Exporting Process is Stoped or Cancelled by User", MessageSource.Ticket, "", messageType.Warning);
                                bgw.ReportProgress(100, index);
                                return;
                            }                           
                            index = 1;                           
                            totalNumer = 0;
                            percentage = 1;
                            ticketIndex = 1;                           
                            CommitCRM.ObjectQuery<CommitCRM.Ticket> Tickets = new CommitCRM.ObjectQuery<CommitCRM.Ticket>(CommitCRM.LinkEnum.linkAND, 510);
                            string startIndex=partTicketByPart[0]+"-"+ticketNumber;
                            Tickets.AddCriteria(CommitCRM.Ticket.Fields.TicketNumber, CommitCRM.OperatorEnum.opGreaterThanOrEqual, startIndex);
                            string final=partTicketByPart[0]+"-"+(ticketNumber+300);
                            Tickets.AddCriteria(CommitCRM.Ticket.Fields.TicketNumber, CommitCRM.OperatorEnum.opLessThan, final);
                            RepairShoprUtils.LogWriteLineinHTML(" Loading Ticket Number from :" + startIndex + " To : " + final, MessageSource.Ticket, "", messageType.Information);
                            _statusMessage = string.Format("Loading Ticket from Ticket Number: {0}, it will take 2-3 mintues",startIndex);
                            bgw.ReportProgress(percentage, index);
                            List<CommitCRM.Ticket> CommitCRMTicketLists = Tickets.FetchObjects();

                            if (CommitCRMTicketLists != null)
                            {
                                totalNumer = CommitCRMTicketLists.Count;
                                ticketCount = CommitCRMTicketLists.Count;
                            }
                            else
                                continue;
                            _statusMessage = "Sending to RepairShopr..";
                            bgw.ReportProgress(percentage, index);                            
                            foreach (CommitCRM.Ticket ticket in CommitCRMTicketLists)
                            {
                               
                                try
                                {
                                    if (bgw.CancellationPending)
                                    {
                                        RepairShoprUtils.LogWriteLineinHTML("Ticket Exporting Process is Stoped or Cancelled by User", MessageSource.Ticket, "", messageType.Warning);
                                        bgw.ReportProgress(100, index);                                       
                                        break;
                                    }

                                    string ticketId = string.Empty;
                                    using (SQLiteCommand cmdItemAlready = new SQLiteCommand(string.Format("SELECT RTicketId FROM Ticket WHERE ticketId='{0}'", ticket.TicketREC_ID), conn))
                                    {
                                        using (SQLiteDataReader reader = cmdItemAlready.ExecuteReader())
                                        {
                                            while (reader.Read())
                                            {
                                                ticketId = reader[0].ToString();
                                            }
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(ticketId))
                                    {
                                        RepairShoprUtils.LogWriteLineinHTML(string.Format("Ticket With Ticket Number:{0} and Description: {1} and  is already exported",ticket.TicketNumber, ticket.Description), MessageSource.Ticket, "", messageType.Warning);

                                        percentage = (100 * index) / totalNumer;
                                        _statusMessage = string.Format("Ticket : {0} is already Exported so, it is skipping", ticket.TicketNumber);
                                        bgw.ReportProgress(percentage, index);
                                        index++;
                                        //customerIndex++;
                                        continue;
                                    }
                                    string customerId = string.Empty;
                                    using (SQLiteCommand cmdItemAlready = new SQLiteCommand(string.Format("SELECT CustomerId FROM Account WHERE AccountId='{0}'", ticket.AccountREC_ID), conn))
                                    {
                                        using (SQLiteDataReader reader = cmdItemAlready.ExecuteReader())
                                        {
                                            while (reader.Read())
                                            {
                                                customerId = reader[0].ToString();
                                            }
                                        }
                                    }
                                    if (string.IsNullOrEmpty(customerId))
                                    {
                                        customerId = CreateContactForMissingTicket(ticket, conn);
                                        if (string.IsNullOrEmpty(customerId))
                                        {
                                            RepairShoprUtils.LogWriteLineinHTML("Unable to locate Account with Ticket : " + ticket.Description, MessageSource.Ticket, "", messageType.Warning);
                                            percentage = (100 * index) / totalNumer;
                                            _statusMessage = string.Format("Unable to locate customer Id for Ticket : {0}, it is skipping", ticket.Description);
                                            bgw.ReportProgress(percentage, index);
                                            index++;
                                            //ticketIndex++;
                                            continue;
                                        }
                                    }
                                    RepairShoprUtils.LogWriteLineinHTML("Creating ticket with Ticket Number  : " + ticket.TicketNumber, MessageSource.Ticket, "", messageType.Information);

                                    RepairShoprUtils.LogWriteLineinHTML(string.Format("Ticket has following Information :  Subject : {0}, Customer Id: {1},Problem Type :{2},comment_subject:{3}", ticket.Description, customerId, ticket.TicketType, ticket.Status_Text), MessageSource.Ticket, "", messageType.Information);

                                    NameValueCollection myNameValueCollection = new NameValueCollection();
                                    string subject = string.Empty;
                                    if (!string.IsNullOrEmpty(ticket.Description))
                                    {
                                        if (ticket.Description.Length > 255)
                                        {
                                            subject = ticket.Description.Substring(0, 255);
                                            RepairShoprUtils.LogWriteLineinHTML(string.Format("Ticket subject is truncate to length 255, Origional Ticket description is {0} and after Truncation is {1}",ticket.Description,subject), MessageSource.Ticket, "", messageType.Warning);
                                        }
                                        else
                                            subject = ticket.Description;
                                    }
                                    myNameValueCollection.Add("subject", subject);
                                    myNameValueCollection.Add("customer_id", customerId);
                                    if (!string.IsNullOrEmpty(ticket.TicketType))
                                        myNameValueCollection.Add("problem_type", ticket.TicketType);
                                    myNameValueCollection.Add("status", "Resolved");
                                    myNameValueCollection.Add("comment_body", GetCommentValue(ticket));
                                    if (!string.IsNullOrEmpty(ticket.Status_Text))
                                        myNameValueCollection.Add("comment_subject", ticket.Status_Text);
                                    myNameValueCollection.Add("comment_hidden", "1");
                                    myNameValueCollection.Add("comment_do_not_email", "1");                                   
                                    string createDate=HttpUtility.UrlEncode(ticket.UpdateDate.ToString("yyyy-MM-dd H:mm:ss"));
                                    myNameValueCollection.Add("created_at", createDate);
                                    var newTicket = RepairShoprUtils.ExportTicket(myNameValueCollection);
                                    if (newTicket != null)
                                    {
                                        percentage = (100 * index) / totalNumer;
                                        _statusMessage = string.Format("Exported ( {0}/{1} ) of Ticket", ticketIndex, ticketCount);
                                        bgw.ReportProgress(percentage, index);
                                        using (SQLiteCommand cmdINewItem = new SQLiteCommand(string.Format("INSERT INTO  Ticket (TicketId,RTicketId) VALUES('{0}','{1}')", ticket.TicketREC_ID, newTicket.Id), conn))
                                            cmdINewItem.ExecuteNonQuery();
                                        RepairShoprUtils.LogWriteLineinHTML("Successfully Exported New Ticket in RepairShopr ", MessageSource.Ticket, "", messageType.Information);
                                        ticketIndex++;
                                    }
                                    else
                                    {
                                        RepairShoprUtils.LogWriteLineinHTML("Failed Exported New Ticket in RepairShopr "+ticket.Description, MessageSource.Ticket, "", messageType.Warning);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    RepairShoprUtils.LogWriteLineinHTML("Failed to Export New Ticket. Due to " + ex.Message, MessageSource.Ticket, ex.StackTrace, messageType.Error);
                                }
                                index++;

                            }
                            ticketNumber += 300;
                            RepairShoprUtils.LogWriteLineinHTML("Sucessfull Loaded Ticket up to   " +final, MessageSource.Ticket, "", messageType.Information);
                            Properties.Settings.Default.TicketNumber = ticketNumber.ToString();
                            Properties.Settings.Default.Save();                            
                        }
                        isCompleteTicket = true;
                        bgw.ReportProgress(100, index);
                    }
                }
            }

        }
Beispiel #10
0
        protected async Task <Customer> ExportSingleCustomer(CommitCRM.Account account, SQLiteConnection connection, string remoteHost)
        {
            return(await Task.Factory.StartNew(() =>
            {
                var myNameValueCollection = new NameValueCollection();
                string fullname = account.GetFieldValue("FLDCRDCONTACT");
                myNameValueCollection.Add("business_name", account.CompanyName);
                if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrEmpty(account.LastName))
                {
                    myNameValueCollection.Add("firstname", fullname.Replace(account.LastName, string.Empty));
                }
                else
                {
                    myNameValueCollection.Add("firstname", fullname);
                }
                myNameValueCollection.Add("lastname", account.LastName);
                myNameValueCollection.Add("email", account.EmailAddress1);
                myNameValueCollection.Add("phone", Regex.Replace(account.Phone1, @"[^.0-9\s]", ""));
                myNameValueCollection.Add("mobile", Regex.Replace(account.Phone2, @"[^.0-9\s]", ""));
                myNameValueCollection.Add("address", account.AddressLine1);
                myNameValueCollection.Add("address_2", account.AddressLine2);
                myNameValueCollection.Add("city", account.City);
                myNameValueCollection.Add("state", account.State);
                myNameValueCollection.Add("zip", account.Zip);
                myNameValueCollection.Add("notes", account.Notes);

                var newCustomer = RepairShoprUtils.ExportCustomer(myNameValueCollection, remoteHost);
                if (newCustomer != null)
                {
                    using (SQLiteCommand cmdINewItem = new SQLiteCommand(string.Format("INSERT INTO  Account (AccountId,CustomerId) VALUES('{0}','{1}')", account.AccountREC_ID, newCustomer.Id), connection))
                        cmdINewItem.ExecuteNonQuery();

                    var contacts = new CommitCRM.ObjectQuery <CommitCRM.Contact>();
                    contacts.AddCriteria(CommitCRM.Contact.Fields.ParentAccountREC_ID, CommitCRM.OperatorEnum.opEqual, account.AccountREC_ID);
                    contacts.AddCriteria(CommitCRM.Contact.Fields.AccountType, CommitCRM.OperatorEnum.opEqual, 5);

                    var contactsResult = contacts.FetchObjects();
                    foreach (CommitCRM.Contact contact in contactsResult)
                    {
                        string contactname = contact.GetFieldValue("FLDCRDCONTACT");
                        NameValueCollection contactNameCollection = new NameValueCollection();
                        if (account.EmailAddress1.Contains("@"))
                        {
                            myNameValueCollection.Add("email", contact.EmailAddress1);
                        }
                        contactNameCollection.Add("phone", contact.Phone1);
                        contactNameCollection.Add("mobile", contact.Phone2);
                        contactNameCollection.Add("address", contact.AddressLine1);
                        contactNameCollection.Add("address_2", contact.AddressLine2);
                        contactNameCollection.Add("city", contact.City);
                        contactNameCollection.Add("state", contact.State);
                        contactNameCollection.Add("zip", contact.Zip);
                        contactNameCollection.Add("customer_id", newCustomer.Id);
                        contactNameCollection.Add("name", contactname);

                        RepairShoprUtils.ExportContact(contactNameCollection, remoteHost);
                    }
                }

                return newCustomer;
            }));
        }