Subversion Repository Public Repository

ChrisCompleteCodeTrunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using ActionTireCo.Crm.Model.Database;
using ActionTireCo.Crm.Model.View;
using Microsoft.Exchange.WebServices.Data;
using ActionTireCo.Crm.Model.Session;
using System.Data.Linq.SqlClient;
using System.Data.SqlClient;
using System.Configuration;

namespace ActionTireCo.Crm.Controllers
{
    public class CustomerController : Controller
    {
        public async Task<ActionResult> Index(int totalItems, int page, int itemsPerPage, string customerName = null, string customerPhone = null, string customerAddress1 = null, string customerAddress2 = null, string customerCity = null, string customerState = null, int customerType = 0, int customerAssigned = 0)
        {
            var context = new ActionTireCoCrmContext();

            if (customerName == null & customerPhone == null & customerAddress1 == null & customerAddress2 == null & customerCity == null & customerState == null)
            {
                return View(
                   await context.Customer.OrderBy(o => o.Name).Skip(itemsPerPage * page).Take(itemsPerPage).ToListAsync<Customer>()
                );
            }
            else
            {
                IQueryable<Customer> query = context.Customer;

                if (customerName != String.Empty)
                {
                    query = query.Where(e => e.Name.ToUpper().Contains(customerName.ToUpper()));
                }
                if (customerPhone != String.Empty)
                {
                    query = query.Where(e => e.Phone.ToUpper().Contains(customerPhone.ToUpper()));
                }
                if (customerAddress1 != String.Empty)
                {
                    query = query.Where(e => e.AddressLine1.ToUpper().Contains(customerAddress1.ToUpper()));
                }
                if (customerAddress2 != String.Empty)
                {
                    query = query.Where(e => e.AddressLine2.ToUpper().Contains(customerAddress2.ToUpper()));
                }
                if (customerCity != String.Empty)
                {
                    query = query.Where(e => e.City.ToUpper().Contains(customerCity.ToUpper()));
                }
                if (customerState != String.Empty)
                {
                    query = query.Where(e => e.State.ToUpper().Contains(customerState.ToUpper()));
                }
                if (customerType != 0)
                {
                    // 0 = Both
                    // 1 = Customer
                    // 2 = Prospect
                    if (customerType == 1)
                    {
                        query = query.Where(e => e.CustomerTypeId == 1);
                    }
                    if (customerType == 2)
                    {
                        query = query.Where(e => e.CustomerTypeId == 2);
                    }

                }
                if (customerAssigned != 0)
                {
                    // 0 = All
                    // 1 = Assigned To Me
                    if (customerAssigned == 1)
                    {
                        int userId = ((User)Session["User"]).Id;
                        query = query.Where(e => e.UserId == userId);
                    }
                }

                query = query.OrderBy(o => o.Name).Skip(itemsPerPage * page).Take(itemsPerPage);

                return View(
                    await query.ToListAsync<Customer>()
                );
            }
        }

        public async Task<ActionResult> Edit(int customerId)
        {

            System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

            UserObject userObject = (UserObject)Session["UserObject"];
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

            service.Credentials = new WebCredentials(userObject.UserName, userObject.Password);
            service.Url = new Uri("https://actionex1.atc.local/ews/exchange.asmx");


            DateTime startDate = DateTime.Now.AddDays(-30);
            DateTime endDate = DateTime.Now.AddDays(30);

            // load the default calendar
            CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

            // load events
            CalendarView cView = new CalendarView(startDate, endDate, 50);
            cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Id);


            var context = new ActionTireCoCrmContext();
            return View(
                new EditCustomerModel()
                {
                    Customer = await (from c in context.Customer where c.Id == customerId select c).SingleOrDefaultAsync<Customer>(),
                    Genders = await context.Gender.ToListAsync<Gender>(),
                    Notes = await context.Note.Where(n => n.CustomerId == customerId).ToListAsync<Note>(),
                    PhoneNumberTypes = await context.PhoneNumberType.ToListAsync<PhoneNumberType>(),
                    EmailAddressTypes = await context.EmailAddressType.ToListAsync<EmailAddressType>(),
                    PostalAddressTypes = await context.PostalAddressType.ToListAsync<PostalAddressType>(),
                    Appointments = calendar.FindAppointments(cView).ToList<Appointment>(),
                    Calls = await context.Call.Where(n => n.CustomerId == customerId).ToListAsync<Call>()
                }
            );
        }

        [HttpPost]
        public ActionResult Add(Customer customer)
        {
            var context = new ActionTireCoCrmContext();
            try
            {
                customer.CustomerTypeId = 2;
                customer.UserId = ((User)Session["User"]).Id;
                customer.DateCreated = DateTime.Now;
                customer.Active = true;
                context.Customer.Add(customer);
                context.SaveChanges();

                var contact = new Model.Database.Contact();
                contact.GenderId = 1;
                contact.FirstName = customer.Name;
                contact.DateCreated = DateTime.Now;
                contact.Active = true;
                context.Contact.Add(contact);
                context.SaveChanges();

                var customerContact = new Model.Database.CustomerContact();
                customerContact.ContactId = contact.Id;
                customerContact.CustomerId = customer.Id;
                customerContact.DateCreated = DateTime.Now;
                customerContact.Active = true;
                context.CustomerContact.Add(customerContact);
                context.SaveChanges();

                var postalAddress = new PostalAddress();
                postalAddress.DateCreated = DateTime.Now;
                postalAddress.Active = true;
                postalAddress.PostalAddressTypeId = 1;
                postalAddress.AddressLine1 = customer.AddressLine1;
                postalAddress.AddressLine2 = customer.AddressLine2;
                postalAddress.City = customer.City;
                postalAddress.State = customer.State;
                postalAddress.ZipCode = customer.Zip;
                context.PostalAddress.Add(postalAddress);
                context.SaveChanges();

                var phoneNumber = new PhoneNumber();
                phoneNumber.DateCreated = DateTime.Now;
                phoneNumber.Active = true;
                phoneNumber.PhoneNumberTypeId = 1;
                phoneNumber.Value = customer.Phone;
                context.PhoneNumber.Add(phoneNumber);
                context.SaveChanges();

                var contactPostalAddress = new ContactPostalAddress();
                contactPostalAddress.DateCreated = DateTime.Now;
                contactPostalAddress.Active = true;
                contactPostalAddress.ContactId = contact.Id;
                contactPostalAddress.PostalAddressId = postalAddress.Id;
                context.ContactPostalAddress.Add(contactPostalAddress);
                context.SaveChanges();

                var contactPhoneNumber = new ContactPhoneNumber();
                contactPhoneNumber.DateCreated = DateTime.Now;
                contactPhoneNumber.Active = true;
                contactPhoneNumber.ContactId = contact.Id;
                contactPhoneNumber.PhoneNumberId = phoneNumber.Id;
                context.ContactPhoneNumber.Add(contactPhoneNumber);
                context.SaveChanges();

                return RedirectToAction("Edit", "Customer", new { @customerId = customer.Id });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index", "Customer",
                    new
                    {
                        @errorMessage = ex.InnerException.Message,
                        @totalItems = 2147483647,
                        @page = 0,
                        @itemsPerPage = 50,
                        @customerType = 0,
                        @customerAssigned = 0
                    }
                );
            }
        }

        public static int GetNextProspectId()
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ActionTireCoCrmContext"].ToString());
            cn.Open();
            SqlCommand cmd = new SqlCommand("SELECT CASE WHEN MAX([Id]) < 5000000 THEN 5000000 ELSE MAX([Id]) + 1 END AS [NextId]  FROM [dbo].[Customer] WITHOUT (NOLOCK);", cn);
            int id = Int32.Parse(cmd.ExecuteScalar().ToString());
            cmd.Dispose();
            cn.Close();
            cn.Dispose();
            return id;
        }
    }
}

Commits for ChrisCompleteCodeTrunk/ActionTireCo/ActionTireCo.Crm/Controllers/CustomerController.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000