/
LeadsController.cs
374 lines (337 loc) · 15.9 KB
/
LeadsController.cs
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using PMS_HT.Areas.CRM.Manager;
using PMS_HT.Areas.CRM.Models;
using PMS_HT.Controllers;
using PMS_HT.Utility;
using PMS_HT.ViewModels;
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 AutoMapper;
using System.Net;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Net.Mail;
using System.Configuration;
using System.Net.Mime;
using PMS_HT.Models;
namespace PMS_HT.Areas.CRM.Controllers
{
public class LeadsController : CustomControllerBase
{
private LeadsManager leadsManager;
private CRM_Task_Manager taskManager;
public LeadsController()
{
leadsManager = new LeadsManager();
taskManager = new CRM_Task_Manager();
}
public async Task<ActionResult> CrmDashboard()
{
tbl_emp_info loggedInUserInfo = Utilities.GetLoggedInUserInfo(User.Identity.Name);
int? loggedInUser = loggedInUserInfo.Emp_ID;
int myTotalLeads = 0;
int myTotalLeadsToday = 0;
int TotalLeads = 0;
int TotalLeadsToday = 0;
int numberOfHighPrioritytask = 0;
int totalTask = 0;
List<tbl_crm_task> todaysCallList = new List<tbl_crm_task>();
List<tbl_crm_task> todaysAppointmentList = new List<tbl_crm_task>();
List<tbl_crm_task> todaysOtherTaskList = new List<tbl_crm_task>();
if (loggedInUser == null)
{
return new HttpUnauthorizedResult();
}
else
{
if (User.IsInRole(Permissions.CRM.VIEW_OWN_CRM_DASHBOARD))
{
myTotalLeads = await leadsManager.GetMyTotalLeadsAsync(loggedInUser).ConfigureAwait(false);
myTotalLeadsToday = await leadsManager.GetMyTotalLeadsTodayAsync(loggedInUser).ConfigureAwait(false);
}
else if (User.IsInRole(Permissions.CRM.VIEW_CRM_DASHBOARD))
{
TotalLeads = await leadsManager.GetTotalLeadsAsync().ConfigureAwait(false);
TotalLeadsToday = await leadsManager.GetTotalLeadsTodayAsync().ConfigureAwait(false);
}
if (User.IsInRole(Permissions.CRM.VIEW_OWN_TASKS))
{
numberOfHighPrioritytask = await taskManager.GetNumberOfInCompleteHighPriorityTaskAsync(loggedInUser).ConfigureAwait(false);
totalTask = await taskManager.GetTotalIncompleteTaskAsync(loggedInUser).ConfigureAwait(false);
todaysCallList = await taskManager.GetTodaysCallListAsync(loggedInUser).ConfigureAwait(false);
todaysAppointmentList = await taskManager.GetTodaysAppointmentListAsync(loggedInUser).ConfigureAwait(false);
todaysOtherTaskList = await taskManager.GetTodaysOtherTaskListAsync(loggedInUser).ConfigureAwait(false);
}
ViewBag.MyTotalLeads = myTotalLeads;
ViewBag.MyTotalLeadsToday = myTotalLeadsToday;
ViewBag.TotalLeads = TotalLeads;
ViewBag.TotalLeadsToday = TotalLeadsToday;
ViewBag.HighPriorityTask = numberOfHighPrioritytask;
ViewBag.TotalTask = totalTask;
ViewBag.TodaysCallList = todaysCallList;
ViewBag.TodaysAppointmentList = todaysAppointmentList;
ViewBag.TodaysOtherTaskList = todaysOtherTaskList;
return View();
}
}
public async Task<ActionResult> Index()
{
tbl_emp_info loggedInUserInfo = Utilities.GetLoggedInUserInfo(User.Identity.Name);
int? loggedInUser = loggedInUserInfo.Emp_ID;
string permission = "";
if (loggedInUser == null)
{
return new HttpUnauthorizedResult();
}
List<tbl_crm_leads> allLeadList = new List<tbl_crm_leads>();
if ((!User.IsInRole(Permissions.CRM.VIEW_ALL_LEADS)) && (User.IsInRole(Permissions.CRM.VIEW_OWN_LEADS)))
{
allLeadList = await db.tbl_crm_leads.Where(i => i.Generated_By == loggedInUser && i.ActionType != Constant.DELETE).ToListAsync().ConfigureAwait(false);
permission = Permissions.CRM.VIEW_OWN_LEADS;
}
else if(User.IsInRole(Permissions.CRM.VIEW_ALL_LEADS))
{
allLeadList = await db.tbl_crm_leads.Where(i => i.ActionType != Constant.DELETE).ToListAsync().ConfigureAwait(false);
permission = Permissions.CRM.VIEW_ALL_LEADS;
}
ViewBag.NumberOfContactedLeads = await leadsManager.GetNumberOfContactedLeadsAsync(loggedInUser, permission).ConfigureAwait(false);
ViewBag.NumberOfProcessingLeads = await leadsManager.GetNumberOfProcessingLeadsAsync(loggedInUser, permission).ConfigureAwait(false);
ViewBag.NumberOfWonLeads = await leadsManager.GetNumberOfWonLeadsAsync(loggedInUser, permission).ConfigureAwait(false);
ViewBag.NumberOfCustomer = await leadsManager.GetNumberOfCustomerAsync(loggedInUser, permission).ConfigureAwait(false);
return View(allLeadList);
}
[Authorize(Roles = Permissions.CRM.ADD_LEADS)]
public ActionResult Add()
{
ViewBag.Country_ID = new SelectList(db.tbl_country.ToList(), "Country_ID", "Country_Name");
ViewBag.Source_ID = new SelectList(db.tbl_crm_source.ToList(), "Source_ID", "Source_Name");
return PartialView();
}
[HttpPost]
public async Task<ActionResult> Add(LeadsViewModel Lead)
{
if (ModelState.IsValid)
{
tbl_crm_leads newLead = Mapper.Map<LeadsViewModel, tbl_crm_leads>(Lead);
tbl_emp_info loggedInUserInfo = Utilities.GetLoggedInUserInfo(User.Identity.Name);
int? loggedInUser = loggedInUserInfo.Emp_ID;
newLead.Generated_By = loggedInUser;
newLead.CreatedAt = DateTime.Now;
ResponseMessage responseMessage = await leadsManager.AddAsync(newLead).ConfigureAwait(false);
if(responseMessage.Type == Constant.RESPONSE_MESSAGE_TYPE_SUCCESS)
{
return Json(GeneralMessages.SAVE_SUCCESSFUL, JsonRequestBehavior.AllowGet);
}
else
{
return Json(GeneralMessages.SAVE_FAILED, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(GeneralMessages.SAVE_FAILED, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
[Authorize(Roles = Permissions.CRM.EDIT_LEADS)]
public async Task<ActionResult> Edit(int? id)
{
if(id == null)
{
return Json(false);
}
else
{
tbl_crm_leads findLead = await leadsManager.GetLeadByIdAsync(id).ConfigureAwait(false);
LeadsEditViewModel leadEditViewModel = Mapper.Map<tbl_crm_leads, LeadsEditViewModel>(findLead);
leadEditViewModel.CountryList = await leadsManager.GetAllCountryAsync().ConfigureAwait(false);
leadEditViewModel.SourceList = await leadsManager.GetAllSourceAsync().ConfigureAwait(false);
return PartialView(leadEditViewModel);
}
}
[HttpPost]
public async Task<ActionResult> Edit(LeadsEditViewModel updateLeads)
{
if (ModelState.IsValid)
{
tbl_crm_leads updatedLead = Mapper.Map<LeadsEditViewModel, tbl_crm_leads>(updateLeads);
tbl_emp_info loggedInUserInfo = Utilities.GetLoggedInUserInfo(User.Identity.Name);
int? loggedInUser = loggedInUserInfo.Emp_ID;
updatedLead.Generated_By = loggedInUser;
updatedLead.UpdatedAt = DateTime.Now;
ResponseMessage responseMessage = await leadsManager.EditAsync(updatedLead).ConfigureAwait(false);
if(responseMessage.Type == Constant.RESPONSE_MESSAGE_TYPE_SUCCESS)
{
return Json(GeneralMessages.SAVE_SUCCESSFUL, JsonRequestBehavior.AllowGet);
}
else
{
return Json(GeneralMessages.SAVE_FAILED, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(GeneralMessages.SAVE_FAILED, JsonRequestBehavior.AllowGet);
}
}
[Authorize(Roles = Permissions.CRM.DELETE_LEADS)]
public async Task<ActionResult> Delete(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ResponseMessage responseMessage = await leadsManager.DeleteByIdAsync(id).ConfigureAwait(false);
if(responseMessage.Type == Constant.RESPONSE_MESSAGE_TYPE_SUCCESS)
{
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Index");
}
}
public async Task<ActionResult> View(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tbl_crm_leads viewLead = await leadsManager.GetLeadByIdAsync(id).ConfigureAwait(false);
if (viewLead == null)
{
return RedirectToAction("Index");
}
else
{
return PartialView(viewLead);
}
}
[HttpGet]
public async Task<ActionResult> SendProposal(int? id)
{
if (id == null)
{
return RedirectToAction("Index");
}
else
{
string email = await leadsManager.GetLeadEmailAsync(id).ConfigureAwait(false);
SendProposalViewModel sendViewModel = new SendProposalViewModel();
sendViewModel.Lead_ID = id;
sendViewModel.To = email;
return PartialView(sendViewModel);
}
}
[HttpPost]
public async Task<JsonResult> SaveProposalInfo(SendProposalViewModel viewModel)
{
var postedFileWithoutExtension = Path.GetFileNameWithoutExtension(viewModel.Attachment.FileName);
viewModel.Proposal_File = Utilities.SaveImage(Server, viewModel.Attachment, postedFileWithoutExtension, Constant.FILE_PATH_FOR_LEADS_PROPOSAL);
tbl_crm_leads updateLead = Mapper.Map<SendProposalViewModel, tbl_crm_leads>(viewModel);
ResponseMessage responseMessage = await leadsManager.SaveProposalInfoAsync(updateLead).ConfigureAwait(false);
if (responseMessage.Type == Constant.RESPONSE_MESSAGE_TYPE_SUCCESS)
{
var root = Server.MapPath(Constant.FILE_PATH_FOR_LEADS_PROPOSAL);
var fileName = viewModel.Proposal_File;
var path = Path.Combine(root, fileName);
path = Path.GetFullPath(path);
var message = new MailMessage();
message.To.Add(new MailAddress(viewModel.To)); // replace with valid value
message.From = new MailAddress(ConfigurationManager.AppSettings["CRM_EMAIL_ADDRESS"]); // replace with valid value
message.Subject = viewModel.Subject;
message.Body = viewModel.Message;
Attachment attachment = new Attachment(path, MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
message.Attachments.Add(attachment);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = ConfigurationManager.AppSettings["CRM_EMAIL_ADDRESS"], // replace with valid value
Password = ConfigurationManager.AppSettings["CRM_EMAIL_PASSWORD"] // replace with valid value
};
smtp.Credentials = credential;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message);
}
return new JsonResult { Data = new { status = true } };
}
else
{
return new JsonResult { Data = new { status = false } };
}
}
//REPORT SECTION START
public JsonResult ExportLeadInfo(int? id)
{
if (id == null)
{
return Json(false);
}
//Step 1 - Get data according to your requirement for report
var leadInfo = db.tbl_crm_leads.Where(x => x.Lead_ID == id).Select(a => new
{
FirstName = a.First_Name,
MiddleNam = a.Middle_Name ?? "N/A",
LastName = a.Last_Name,
Country = a.tbl_country.Country_Name,
Address = a.Address,
Email = a.Email,
Comapany = a.Company ?? "N/A",
Designaion = a.Designation ?? "N/A",
Website = a.Website ?? "N/A",
Source = a.TblCrmSource.Source_Name,
ContactNo = a.Contact_No ?? "N/A",
ReferredBy = a.referred_by_emp_info.Emp_Name ?? "",
ExistingOffer = a.Existing_Offer ?? "N/A",
ExpectedOffer = a.Expected_Offer ?? "N/A",
Feedback = a.Feedback ?? "N/A",
Note = a.Note ?? "N/A",
Status = a.Status
});
string reportName = "LeadInfoReport";
string dataSetName = "LeadInfoDataSet";
String reportData = Utilities.CreateReport(this, reportName, dataSetName, leadInfo);
return Json(reportData, JsonRequestBehavior.AllowGet);
}
//REPORT SECTION END
[HttpGet]
public ActionResult SearchLeads()
{
return View();
}
[HttpPost]
public async Task<ActionResult> ViewDateRangeLeads(DateTime? from, DateTime? to)
{
if (from == null || to == null || from > to)
{
return Json("Invalid", JsonRequestBehavior.AllowGet);
}
to = to.Value.AddDays(1);
tbl_emp_info loggedInUserInfo = Utilities.GetLoggedInUserInfo(User.Identity.Name);
int? loggedInUser = loggedInUserInfo.Emp_ID;
if (loggedInUser == null)
{
return new HttpUnauthorizedResult();
}
List<tbl_crm_leads> leadsList = new List<tbl_crm_leads>();
if (User.IsInRole(Permissions.CRM.VIEW_ALL_LEADS))
{
leadsList = await leadsManager.GetDateRangeAllLeadsAsync(from, to).ConfigureAwait(false);
}
else if (User.IsInRole(Permissions.CRM.VIEW_OWN_LEADS))
{
leadsList = await leadsManager.GetDateRangeOwnLeadsAsync(from, to, loggedInUser).ConfigureAwait(false);
}
return PartialView(leadsList);
}
}
}