public ActionResult UpdatePickup(Pickup pickup) { // get the current user // string currentUserId = User.Identity.GetUserId(); string fullName = null; try { // Get info for Current user // ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId); // Get user's full name to notify others // fullName = currentUser.FirstName + " " + currentUser.LastName; } catch (Exception ex) { Debug.Write("Error autheticating user: "******"none"; // Update Comnpany Data // newItem.Company = "ZFM"; db2.Items.Add(newItem); } } try { db2.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); // Provide for exceptions. } #endregion pickup.LastEditBy = fullName; //Azure time conversion // DateTime timeUtc = DateTime.UtcNow; TimeZoneInfo pstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); DateTime pstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, pstZone); pickup.LastModified = pstTime; //BMK Pickup Completed // if (pickup.Status.ToLower().Contains("completed") || pickup.Status.ToLower().Contains("closed")) { pickup.Closed = pstNow(); pickup.ClosedBy = fullName; // then delete all the pictures if any foreach (var item in pickup.Items) { // Get file and erase if (!String.IsNullOrEmpty(item.Picture1) && item.Picture1 != "none") { // Get file name var file = Path.GetFileName(item.Picture1); // Create Path var path = Path.Combine(Server.MapPath("/Content/Photos/"), file); // Erase File FileInfo fi1 = new FileInfo(path); if (fi1.Exists) // check if its there and delete fi1.Delete(); } } } //BMK Pickup Completed with SMS // if (pickup.Status.ToLower().Contains("completed sms") || pickup.Status.ToLower().Contains("closed sms")) { // Update the Status // pickup.Closed = pstNow(); pickup.ClosedBy = fullName; // Get the Sales Rep Number // ApplicationUser salesRep4PU = db.Users.FirstOrDefault(x => x.FirstName + " " + x.LastName == pickup.Rep); // Get user's full name to notify others // string repSMS = salesRep4PU.CellNumber; // Get the Courier Number // ApplicationUser courierSMS = db.Users.FirstOrDefault(x => x.FirstName + " " + x.LastName == pickup.TransporterName); // Get user's full name to notify others // string trasportSMS = courierSMS.CellNumber; // Cleanup // salesRep4PU = null; courierSMS = null; // Add the Prefix ( +1 ) if the numbers don't have them // if(repSMS.Substring(0, 2) != "+1") repSMS = "+1" + repSMS; if (trasportSMS.Substring(0, 2) != "+1") trasportSMS = "+1" + trasportSMS; // Send the SMS alert // // Find your Account Sid and Auth Token at twilio.com/user/account // string AccountSid = "AC1ac0db79f475dc98b90b50a4f245217f"; string AuthToken = "b6c641704a0678be31dc089173aae90a"; var twilio = new TwilioRestClient(AccountSid, AuthToken); //var message = twilio.SendMessage(repSMS, trasportSMS, "Pickup #: " + pickup.PickupNumber + " - Closed by: " + pickup.ClosedBy , " -> NOTES: " + pickup.Notes); //var message = twilio.SendMessage( "+15107882884", repSMS + "," + trasportSMS, "Hello World"); // Send Message to Sales Rep // var messageRep = twilio.SendMessage("+15107882884", repSMS, "Pickup #: " + pickup.PickupNumber + "\n\nClosed by: " + pickup.ClosedBy + "\n\n" + pickup.Notes); // Send Message to Courier // var messageCourier = twilio.SendMessage("+15107882884", trasportSMS, "Pickup #: " + pickup.PickupNumber + "\n\nClosed by: " + pickup.ClosedBy + "\n\n" + pickup.Notes); if (messageRep.RestException != null || messageCourier.RestException != null) { // handle the error ... // Debug.Print(messageRep.RestException.Message); Debug.Print(messageCourier.RestException.Message); } // then delete all the pictures if any foreach (var item in pickup.Items) { // Get file and erase if (!String.IsNullOrEmpty(item.Picture1) && item.Picture1 != "none") { // Get file name var file = Path.GetFileName(item.Picture1); // Create Path var path = Path.Combine(Server.MapPath("/Content/Photos/"), file); // Erase File FileInfo fi1 = new FileInfo(path); if (fi1.Exists) // check if its there and delete fi1.Delete(); } } } db2.Entry(pickup).State = EntityState.Modified; db2.SaveChanges(); // Check if the Location Already Exists // var locationCheck = (from l in db2.Locations where l.Name.Contains(pickup.LocationName) select l); if (locationCheck.Count() == 0) { // Add it to the Locations database Location newLocatioEntry = new Location { Name = pickup.LocationName }; db2.Locations.Add(newLocatioEntry); db2.SaveChanges(); } // Send Message To All Users To Update Pickup List // string updatedPickup = pickup.PickupNumber.ToString(); // Get Pickup Number var hub = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<ZTRAK.Hubs.PickupHub>(); hub.Clients.All.newDataUpdate(fullName, updatedPickup); return Json(pickup.ID); #endregion } else { #region Create a new Pickup // Determine if this is a new pickup // if (pickup.ID == 0) { // Convert the pickup to a list so we can remove items if needed // foreach (var item in pickup.Items.ToList()) { // Remove Items marked if (item.Qty == 0) { pickup.Items.Remove(item); } } // Update the Company Name for new pickup // pickup.Company = "ZFM"; // Save new pickup - fix ID and proceed to save // Calculate the new PickupID and assign it to the new pickup in order to // avoid drastic number changes var pUcount = (from r in db2.Pickups where r.ID != null select r).Count() + 1; // Increase by One for new Pickup pickup.PickupNumber = pUcount; // Get the Team name if the value was not given // if(String.IsNullOrEmpty(pickup.Team)) { // Get the Team Name string teamName = (from t in db2.AspNetUsers where (t.FirstName.ToLower().Trim() + " " + t.LastName.ToLower().Trim() == pickup.Rep.ToLower().Trim()) select t.TeamName).FirstOrDefault(); // Try to Find the Team for the new Pickup // Assign the Team if the Team is found // if(!String.IsNullOrEmpty(teamName)) pickup.Team = teamName.ToString(); } // db2.Entry(pickup).State = EntityState.Added; db2.SaveChanges(); string NewPickup = pickup.PickupNumber.ToString(); // Get Pickup Number // Let the others know // var hub = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<Hubs.PickupHub>(); hub.Clients.All.newDataUpdate(fullName, NewPickup); return Json(NewPickup); } #endregion } return View(); }
public JsonResult SaveLocation(string locInfo) { // Break apart the address // string[] loc; string status=""; // Extract The fields loc = locInfo.Split(','); // Check if a duplicate exists // // Limit return to the first five matches string locName = loc[0]; string locOverwrite = loc[7].Trim(); var result = db2.Locations .Where(r => r.Name.ToLower() == locName.ToLower() && r.Company == "ZFM") .Select(r => new { label = r.Name }).Distinct(); // Deyect duplicates // if (result.Count() >= 1) { // Duplicate Detected status = "duplicate"; } if (locOverwrite == "yes" && result.Count() >= 1) { try { // delete existing // using (var ctx = new ZtrakEntities()) { var listToRemove = (from i in ctx.Locations where i.Name.ToLower() == locName.ToLower() && i.Company == "ZFM" select i); if (listToRemove.Count() >= 1) { ctx.Locations.RemoveRange(listToRemove); ctx.SaveChanges(); } } //Save the Location // Location newLocation = new Location { Name = loc[0].ToLower(), Address = loc[1], City = loc[2], State = loc[3], Zip = loc[4], Latitude = Convert.ToDouble(loc[5]), Longitude = Convert.ToDouble(loc[6]), Company = "ZFM" }; // Save it db2.Locations.Add(newLocation); db2.SaveChanges(); status = "saved!"; } catch (Exception e) { Console.WriteLine(e.Message); // Provide for exceptions. } } if (locOverwrite == "no" && result.Count() == 0) { try { //Save the Location // Location newLocation = new Location { Name = loc[0].ToLower(), Address = loc[1], City = loc[2], State = loc[3], Zip = loc[4], Latitude = Convert.ToDouble(loc[5]), Longitude = Convert.ToDouble(loc[6]), Company = "ZFM" }; // Save it db2.Locations.Add(newLocation); db2.SaveChanges(); status = "saved!"; } catch (Exception e) { Console.WriteLine(e.Message); // Provide for exceptions. } } // return Json(new { answer = status }, JsonRequestBehavior.AllowGet); }