-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadController.cs
609 lines (504 loc) · 23 KB
/
LoadController.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ATGV3.Business;
using ATGV3.Business.Model;
using ATGV3.Business.Model.Custom;
using ATGV3.Web.Helpers;
using Kendo.Mvc.UI;
using ATGV3.Business.ValueObjects;
using ATGV3.Business.Services;
using ATGV3.Web.Models;
using ATGV3.Business.Workflow;
using ATGV3.Common;
using System.Data.Entity.Validation;
using System.Text;
using Newtonsoft.Json;
namespace ATGV3.Web.Controllers
{
public class LoadController : BaseController
{
private readonly LoadBusiness _loadBusiness;
public LoadController()
{
if (_user == null) _user = GetSystemUser();
_loadBusiness = new LoadBusiness(_applicationServicesSetup, _user);
}
// GET: Load
public ActionResult Index()
{
var Loadboard = new LoadboardViewModel();
Loadboard.StatusSelector = SelectListHelper.Generic(LoadBusiness.GetLoadStatuses(_user));
//retrieve save searches
Loadboard.SavedSearches = _user.SavedSearches.Where(i => i.Category == Globals.SearchCategories.Loadboard).ToList();
//retrieve save loadboard settings
var LoadboardSettings = _user.Settings.FirstOrDefault(i => i.SettingName == "Loadboard");
if (LoadboardSettings != null)
Loadboard.Settings = LoadboardSettings.SettingValue;
using (var OfficeBusiness = new OfficeBusiness(_applicationServicesSetup, _user))
{
Loadboard.OfficeGroups = OfficeBusiness.GetUserGroupsByOffice(_user.OfficeId, new[] { Globals.Related.OfficeGroup.Members });
//if just an agent, only show the groups you are apart of.
if (_user.IsAgent() && !_user.IsEmployee())
{
Loadboard.OfficeGroups = Loadboard.OfficeGroups.Where(i => i.Members.Select(u => u.Id == _user.Id).Any()).ToList();
}
}
return View(Loadboard);
}
public ActionResult Claim(int LoadId)
{
var Load = _loadBusiness.Get(LoadId, new[] {
Globals.Related.Load.Order,
Globals.Related.Load.OrderCustomer,
Globals.Related.Load.AssignedToUser,
Globals.Related.Load.OrderUserAssigned,
Globals.Related.Load.InternalNotes,
});
Claim claim = new Claim(Load);
return View(claim);
}
public ActionResult Claims()
{
var Claims = _loadBusiness.GetActiveClaims().Select(claim => new ClaimSummary()
{
OrderId = claim.OrderId,
LoadNumber = claim.LoadNumber,
ClaimDescription = claim.ClaimDescription,
ClaimValue = claim.ClaimValue.Value,
Date = claim.ClaimDate.Value.ToShortDateString(),
Status = claim.ClaimStatus,
Office = claim.Order.UserAssigned.Office.Name,
Agent = claim.Order.UserAssigned.Name,
Customer = claim.Customer.Name,
Carrier = claim.Carrier.Name
}).ToList();
return View(Claims);
}
public ActionResult Holds()
{
using (OrderBusiness orderBusiness = new OrderBusiness(_applicationServicesSetup, _user))
{
var Holds = orderBusiness.GetBillingHolds();
return View(Holds);
}
}
[HttpPost]
public ActionResult SaveClaim(Claim claim)
{
var Load = _loadBusiness.Get(claim.LoadId);
claim.FillClaim(Load);
_loadBusiness.Update(Load);
return SuccessMessage();
}
[HttpGet]
public JsonResult GetLoadBoard()
{
var result = LoadBoardCentral.Instance.GetLoadBoard(_user);
return Json(result.ToList(), JsonRequestBehavior.AllowGet);
}
public ActionResult Search()
{
ViewBag.States = SelectListHelper.States("", "State");
return View();
}
#region AjaxActions
[HttpPost]
public ActionResult LaneSearchFilter(Address origin, int? originRadius, Address dest, int? destRadius)
{
List<vLoadLane> results = _loadBusiness.LaneSearch(origin, originRadius, dest, destRadius);
if (results == null)
{
return ValidationErrorResponse("Search failed, one of your inputs could have been misspelled. Please correct, and try again.");
}
return Json(results);
}
public ActionResult Lookup(string query)
{
var parts = query.Split('-');
int orderId;
if (!Int32.TryParse(parts[0], out orderId))
return ErrorMessage("Invalid order id. Input was not a number");
using (OrderBusiness _orderBusiness = new OrderBusiness(_applicationServicesSetup, _user))
{
var order = _orderBusiness.Get(orderId, new[] { Globals.Related.Order.UserAssigned });
if (order == null)
return NotFound();
else
return SuccessMessage(new { Id = orderId });
}
}
public ActionResult AddComment(string id /* Load Id */, CheckCallViewModel comment)
{
comment.ObjectId = int.Parse(id);
if (_loadBusiness.AddComment(int.Parse(id), comment.ToLoadComment()))
{
return SuccessMessage(new { User = _user.Name });
}
else
return ErrorMessage("Error occured Saving your comment");
}
[HttpPost]
public ActionResult RequestCheck(int id, decimal Amount, string Comment = "")
{
try
{
if (_user.IsAdmin() || _user.HasPermission(Globals.Permission.ApproveEChecks))
{
var Payments = _loadBusiness.GetECheck(id, Amount);
return SuccessMessage(new { Payments = Payments.Select(i => new PaymentViewModel(i)) });
}
else
{
using (ECheckQueue queueBusiness = new ECheckQueue(_applicationServicesSetup, _user))
{
queueBusiness.Add(Globals.RequestTypes.Check, Amount, Comment, id);
return SuccessMessage();
}
}
}
catch (Exception e)
{
return ErrorMessage(e.Message);
}
}
public ActionResult CheckDetails(string id)
{
using (var ECheck = new EFS(_applicationServicesSetup))
{
return Json(ECheck.getMoneyCodeDetails(id), JsonRequestBehavior.AllowGet);
}
}
[HttpPost]
public ActionResult VoidCheck(string TxnId, int PaymentId)
{
if (_loadBusiness.VoidCheck(PaymentId, TxnId))
{
return SuccessMessage();
}
else
return ErrorMessage("Failed to void check");
}
public ActionResult GetCarrierSummary(int id /*Carrier Id */)
{
using (CarrierBusiness carrierBusiness = new CarrierBusiness(_applicationServicesSetup, _user))
{
Carrier carrier = carrierBusiness.Get(id, new string[] {
Globals.Related.Carrier.CarrierContacts,
Globals.Related.Carrier.CarrierRatings,
Globals.Related.Carrier.Blacklists
});
List<string> Warnings = new List<string>();
foreach (CarrierContact c in carrier.CarrierContacts)
c.Carrier = null;
return Json(new
{
Id = carrier.Id,
Name = carrier.Name,
Contacts = carrier.CarrierContacts,
DOT = carrier.DOT,
MC = carrier.MC,
Address = new Address(carrier.PhysicalAddress1, carrier.PhysicalAddress2, carrier.PhysicalCity, carrier.PhysicalState, carrier.PhysicalZip),
Rating = carrier.AverageRating,
OfficeRating = carrier.OfficeRating,
IsActive = carrier.IsActive(),
HasExpiredCargoInsurance = carrier.HasExpiredCargoInsurance,
HasExpiredLiabilityInsurance = carrier.HasExpiredLiabilityInsurance,
Warnings = _loadBusiness.GetCarrierWarnings(carrier),
AdvancesAllowed = !carrier.AllowAdvances,
Mode = carrier.Mode,
Blacklisted = carrier.CarrierBlacklists.Where(i => i.OfficeId == _user.OfficeId).Any()
}, JsonRequestBehavior.AllowGet);
}
}
[HttpPost]
public ActionResult FlagLoad(FlagViewModel Flag)
{
OrderFlag _flag = Flag.ToFlag();
using (OrderFlagBusiness flagBusiness = new OrderFlagBusiness(_applicationServicesSetup, _user))
{
if (_flag.Id > 0)
flagBusiness.Modify(_flag);
else
{
int Id = flagBusiness.Create(_flag);
var NewFlag = flagBusiness.GetById(Id, new[]{
Globals.Related.OrderFlag.FlaggedByUser
});
Flag = new FlagViewModel(NewFlag);
}
if (flagBusiness.ValidationErrors.Any())
{
ValidationErrorResponse(flagBusiness.ValidationErrors);
}
}
return Json(Flag);
}
public ActionResult RemoveFlag(int id /* Flag Id*/ )
{
using (OrderFlagBusiness flagBusiness = new OrderFlagBusiness(_applicationServicesSetup, _user))
{
flagBusiness.ClearFlag(id);
if (flagBusiness.ValidationErrors.Any())
{
return ValidationErrorResponse(flagBusiness.ValidationErrors);
}
else
return SuccessMessage();
}
}
public ActionResult ChangeStatus(int id, string Status)
{
_loadBusiness.ChangeStatus(id, Status);
return SuccessMessage();
}
public ActionResult AddFlagComment(int id /* Flag Id*/, string comment)
{
using (OrderFlagBusiness flagBusiness = new OrderFlagBusiness(_applicationServicesSetup, _user))
{
var _Comment = flagBusiness.AddComment(id, comment);
if (_Comment.Id > 0)
return SuccessMessage(new CommentViewModel(_Comment));
else
return ValidationErrorResponse(flagBusiness.ValidationErrors);
}
}
public ActionResult FlagComments(int id /* Flag Id*/)
{
using (OrderFlagBusiness flagBusiness = new OrderFlagBusiness(_applicationServicesSetup, _user))
{
return Json(
flagBusiness.GetCommentsForFlag(id).Select(i => new CommentViewModel(i)),
JsonRequestBehavior.AllowGet
);
}
}
public ActionResult CalculateMileage(string[] zips)
{
var Mileage = _loadBusiness.CalculateMileage(zips);
if (Mileage != null)
return SuccessMessage(Mileage);
else
return (_loadBusiness.ValidationErrors.Any())
? ErrorMessage(_loadBusiness.ValidationErrors)
: ErrorMessage("Could Not calculate Mileage. Third party service returned an error");
}
public ActionResult EditHistory(int id)
{
var Load = _loadBusiness.Get(id, new string[] {
Globals.Related.Load.Histories,
Globals.Related.Load.HistoriesUser
});
return Json(Load.LoadHistories.Select(i => new
{
Action = i.Action,
TimeStamp = i.TimeStamp.ToShortDateString() + " " + i.TimeStamp.ToLongTimeString(),
Item = i.Description,
OldValue = i.OldValue,
NewValue = i.NewValue,
EditedBy = i.AspNetUser.Name
}), JsonRequestBehavior.AllowGet);
}
public ActionResult SearchTrackingNumber(string query)
{
return Json(_loadBusiness.FindByTrackingNumber(query), JsonRequestBehavior.AllowGet);
}
public ActionResult ReferenceNumberSearch(string term)
{
return Json(
_loadBusiness.GetRepository().Select<Order>(i => i.ReferenceNumber.StartsWith(term)).ToList(),
JsonRequestBehavior.AllowGet
);
}
public ActionResult GetEquipmentOptions(string mode)
{
return Json(_loadBusiness.GetEquipmentOptions(mode), JsonRequestBehavior.AllowGet);
}
public ActionResult LoadboardLoadDetails(int id)
{
var Load = _loadBusiness.Get(id, new[] {
Globals.Related.Load.Stops,
Globals.Related.Load.Carrier,
Globals.Related.Load.CarrierContacts,
Globals.Related.Load.CarrierContact,
Globals.Related.Load.Customer,
Globals.Related.Load.CustomerContacts
//Globals.Related.Load.Comments,
//Globals.Related.Load.CommentUser,
});
foreach (var stop in Load.Stops)
{
stop.Load = null;
}
var ret = new
{
Carrier = new
{
Id = Load.CarrierId,
Name = Load.Carrier.Name,
MC = Load.Carrier.MC,
DOT = Load.Carrier.DOT,
Driver = Load.Driver,
DriverPhone = Load.DriverPhone,
Contacts = Load.Carrier.CarrierContacts
.Where(i => i.Status == Globals.ContactStatus.Active)
.Select(i => new Contact(i)).FirstOrDefault(),
MainContact = new Contact(Load.CarrierContact),
},
Customer = new
{
Name = Load.Customer.Name,
Id = Load.CustomerId,
Contacts = Load.Customer.CustomerContacts
.Where(i => i.Type == Globals.CustomerContactType.Freight && i.Status == Globals.ContactStatus.Active)
.Select(i => new Contact(i))
},
Stops = Load.Stops.OrderBy(i => i.StopDateTime),
//Comments = Load.LoadComments.Select(comment=> new CheckCallViewModel(comment))
};
return Json(ret, JsonRequestBehavior.AllowGet);
}
public ActionResult MarkStopCompleted(int id, List<Stop> Stops)
{
var Load = _loadBusiness.Get(id, new[] { Globals.Related.Load.Stops });
foreach (var Stop in Load.Stops)
{
var theStop = Stops.FirstOrDefault(stop => stop.Id == Stop.Id);
Stop.DriverInTime = theStop.DriverInTime;
Stop.DriverOutTime = theStop.DriverOutTime;
Stop.Arrived = theStop.Arrived;
Stop.ModifiedDate = DateTime.UtcNow;
}
_loadBusiness.Update(Load);
return SuccessMessage();
}
#region MacroPoint Actions
public ActionResult MacroPointBeginTracking(int LoadId, string DriverPhone, string TimeZone, DateTime TrackingStartDate, List<MacroPointStopViewModel> ArrivalTimes)
{
var Arrivals = ArrivalTimes.Select(stop => new MacroPointStop
{
Id = (int)stop.Id,
ETA = stop.ETA,
EDT = stop.EDT
});
bool isSuccess = _loadBusiness.MacroPointBeginTracking(LoadId, DriverPhone, TrackingStartDate, TimeZone, Arrivals); ;
if (isSuccess)
return SuccessMessage();
else
return ErrorMessage("MacroPoint service experienced an error");
}
//Used to Ping MacroPoint service for an immediate LocationUpdate.
public ActionResult UpdateLocation(int loadId)
{
Load load = _loadBusiness.Get(loadId);
string message = null;
if (load != null && load.MacroPointOrderId != null)
{
message = _loadBusiness.UpdateLocation(load.MacroPointOrderId);
}
if (message == "SUCCESS") return SuccessMessage(null, "Location Update Request - Received. Please allow 5 minutes to see your update.");
return ErrorMessage(message);
}
//*** NOTE: This URL is setup on MacroPoint's website (www.macropoint-lite.com) on the Company Settings tab. Set it to point to the production site (currently: www.atgfr8.com; the full URL will be www.atgfr8.com/Load/LocationUpdate)
[AllowAnonymous]
public HttpStatusCode LocationUpdate(string ID, decimal Latitude, decimal Longitude, decimal Uncertainty, string Street1,
string Street2, string Neighborhood, string City, string State, string Postal, string Country, DateTime LocationDateTimeUTC, DateTime ApproxLocationDateTimeInLocalTime, string MPOrderID)
{
try
{
var logText = string.Format("MacroPoint LocationUpdate -- ID: {0}, City: {1}, State: {2}, ApproxLocationDateTimeInLocalTime: {3}, MPOrderId: {4}", ID, City, State, ApproxLocationDateTimeInLocalTime, MPOrderID);
_applicationServices.LogWriteFull(logText, LoggingSeverity.Information, LoggingCategory.Controller);
Load load = _loadBusiness.GetLoadByMacroPointId(MPOrderID);
if (load != null)
{
LoadComment comment = new LoadComment()
{
LoadId = load.Id,
UserId = _user.Id,
Comment = "MacroPoint Location Update",
City = City,
State = State,
Latitude = Latitude,
Longitude = Longitude,
CreatedDate = ApproxLocationDateTimeInLocalTime,
LocationTime = ApproxLocationDateTimeInLocalTime
};
_loadBusiness.AddComment(load.Id, comment);
}
}
catch (Exception e)
{
_applicationServices.HandleException(e, ExceptionCategory.Controller);
return HttpStatusCode.InternalServerError;
}
return HttpStatusCode.OK;
}
[AllowAnonymous]
public HttpStatusCode ScheduleAlert(string MPOrderID, string ID, string StopType, string StopID, string StopName, string AddressLine1, string City, string State, string PostalCode, int ScheduleAlertCode,
string ScheduledStartTimeInLocalTimeForStop, string ScheduledEndTimeInLocalTimeForStop, string ScheduleAlertText)
{
try
{
var logText = string.Format("MacroPoint Schedule Alert -- ID: {0}, ScheduleCode: {1}, ScheduleAlertText: {2}", ID, MacroPointService.ScheduleAlertMessage(ScheduleAlertCode), ScheduleAlertText);
_applicationServices.LogWriteFull(logText, LoggingSeverity.Information, LoggingCategory.Controller);
Load load = _loadBusiness.GetLoadByMacroPointId(MPOrderID);
LoadComment comment = new LoadComment()
{
LoadId = load.Id,
UserId = _user.Id,
Comment = "MacroPoint Schedule Alert -- " + MacroPointService.ScheduleAlertMessage(ScheduleAlertCode) + " | " + ScheduleAlertText,
CreatedDate = DateTime.Now.ToUniversalTime(),
LocationTime = DateTime.Now.ToUniversalTime()
};
_loadBusiness.AddComment(load.Id, comment);
}
catch (Exception e)
{
_applicationServices.HandleException(e, ExceptionCategory.Controller);
return HttpStatusCode.BadRequest;
}
return HttpStatusCode.OK;
}
[AllowAnonymous]
public HttpStatusCode TrackingStatusChange(string ID, string Code, string Message, string MPOrderID)
{
try
{
var logText = string.Format("MacroPoint TrackingStatusChange -- ID: {0}, Code: {1}, Message: {2}, MPOrderID: {3}", ID, Code, Message, MPOrderID);
_applicationServices.LogWriteFull(logText, LoggingSeverity.Information, LoggingCategory.Controller);
Load load = _loadBusiness.GetLoadByMacroPointId(MPOrderID);
if (load != null)
{
LoadComment comment = new LoadComment()
{
LoadId = load.Id,
UserId = _user.Id,
Comment = "MacroPoint Tracking Status Change -- " + MacroPointService.TrackingStatus(Code),
CreatedDate = DateTime.Now.ToUniversalTime(),
LocationTime = DateTime.Now.ToUniversalTime()
};
_loadBusiness.AddComment(load.Id, comment);
string userMessage = string.Format("Tracking Status Change (Order #: {0}) - {1}", load.OrderId, MacroPointService.TrackingStatus(Code));
List<int> usersToMessage = new List<int> { load.Order.AssignedId };
if (load.AssignedTo != null) usersToMessage.Add(load.AssignedTo.Value);
if (load.CoveredBy != null) usersToMessage.Add(load.CoveredBy.Value);
_loadBusiness.NotifyUsers(userMessage, usersToMessage, "Order:" + load.OrderId);
}
}
catch (Exception e)
{
return HttpStatusCode.BadRequest;
}
return HttpStatusCode.OK;
}
#endregion
#endregion
public void Dispose()
{
_loadBusiness.Dispose();
base.Dispose();
}
}
}