using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using IronIntel.Contractor.Machines; using IronIntel.Contractor.Maintenance; using IronIntel.Services.Business.Admin; using System.Web; using IronIntel.Contractor.Users; using Foresight.ServiceModel; using Foresight.Fleet.Services.AssetHealth; using Foresight.Fleet.Services.Device; using Foresight.Fleet.Services; using Foresight.Fleet.Services.Attachment; using IronIntel.Contractor.Attachment; namespace IronIntel.Contractor.Site.Maintenance { public class FuelRecordBasePage : ContractorBasePage { protected void ProcessRequest(string methodName) { object result = null; try { if (methodName != null) { switch (methodName.ToUpper()) { case "GETFUELRECORDS": result = GetFuelRecords(); break; case "GETFUELRECORDCHANGEHISTORY": result = GetFuelRecordChangeHistory(); break; case "SAVEFUELRECORD": result = SaveFuelRecord(); break; case "DELETEFUELRECORD": result = DeleteFuelRecord(); break; case "GETFUELTYPES": result = GetFuelTypes(); break; case "GETFUELRECORDCOMMENTS": result = GetFuelRecordComments(); break; case "ADDFUELRECORDCOMMENT": result = AddFuelRecordComment(); break; case "GETATTACHMENTS": result = GetAttachments(); break; case "ADDATTACHMENT": result = AddAttachment(); break; case "DELETEATTACHMENT": result = DeleteAttachment(); break; } } } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception ex) { SystemParams.WriteLog("error", "FuelRecordsBasePage" + methodName, ex.Message, ex.ToString()); } string json = JsonConvert.SerializeObject(result); Response.Write(json); Response.End(); } private object GetFuelRecords() { try { var session = GetCurrentLoginSession(); if (session != null) { var clientdata = Request.Form["ClientData"].Split((char)170); var type = HttpUtility.HtmlDecode(clientdata[0]); var sdatestr = HttpUtility.HtmlDecode(clientdata[1]); var edatestr = HttpUtility.HtmlDecode(clientdata[2]); var searchtxt = HttpUtility.HtmlDecode(clientdata[3]); DateTime beginDate = DateTime.MinValue; DateTime endDate = DateTime.MaxValue; if (!DateTime.TryParse(sdatestr, out beginDate)) beginDate = DateTime.MinValue; if (!DateTime.TryParse(edatestr, out endDate)) endDate = DateTime.MaxValue; FuelRecord[] fuels = CreateClient().GetFuelRecords(SystemParams.CompanyID, Convert.ToInt64(type), beginDate, endDate, searchtxt); List list = new List(); foreach (FuelRecord fuel in fuels) { FuelRecordInfo fi = new FuelRecordInfo(); Helper.CloneProperty(fi, fuel); fi.TransactionDate = fi.TransactionDate.ToLocalTime(); list.Add(fi); } return list.ToArray(); } else return new FuelRecordInfo[0]; } catch (Exception ex) { AddLog("ERROR", "FuelRecordBasePage.GetFuelRecords", ex.Message, ex.ToString()); return ex.Message; } } private object GetFuelRecordChangeHistory() { try { var session = GetCurrentLoginSession(); if (session != null) { string clientdata = Request.Form["ClientData"]; long fuleid = Convert.ToInt64(HttpUtility.HtmlDecode(clientdata)); FuelRecordAuditInfo[] fuels = CreateClient().GetFuelRecordAuditItems(SystemParams.CompanyID, fuleid); List list = new List(); foreach (FuelRecordAuditInfo fuel in fuels) { FuelRecordAuditItem fi = new FuelRecordAuditItem(); Helper.CloneProperty(fi, fuel); fi.TransactionDate = fi.TransactionDate.ToLocalTime(); fi.AddedOn = fi.AddedOn.ToLocalTime(); fi.LastUpdatedOn = fi.LastUpdatedOn.ToLocalTime(); list.Add(fi); } return list.ToArray(); } else return new FuelRecordInfo[0]; } catch (Exception ex) { AddLog("ERROR", "FuelRecordBasePage.GetFuelRecords", ex.Message, ex.ToString()); return ex.Message; } } private object SaveFuelRecord() { try { var session = GetCurrentLoginSession(); if (session != null) { string clientdata = HttpUtility.HtmlDecode(Request.Params["ClientData"]); FuelRecordInfo fuelrecord = JsonConvert.DeserializeObject(clientdata); FuelRecord record = new FuelRecord(); Helper.CloneProperty(record, fuelrecord); record.TransactionDate = record.TransactionDate.ToUniversalTime(); long fuleid = record.FuelID; if (record.FuelID == -1) { FuelRecord fr = CreateClient().AddNewFuelRecord(SystemParams.CompanyID, record, session.User.UID); fuleid = fr.FuelID; } else { CreateClient().UpdateFuelRecord(SystemParams.CompanyID, record, session.User.UID); } return fuleid; } return "Failed"; } catch (Exception ex) { return ex.Message; } } private object DeleteFuelRecord() { try { var session = GetCurrentLoginSession(); if (session != null) { string clientdata = Request.Form["ClientData"]; long fuleid = Convert.ToInt64(HttpUtility.HtmlDecode(clientdata)); CreateClient().DeleteFuelRecord(SystemParams.CompanyID, fuleid, session.User.UID); return "OK"; } return "Failed"; } catch (Exception ex) { return ex.Message; } } private object GetFuelTypes() { try { var session = GetCurrentLoginSession(); if (session != null) { List> list = FuelManagementClient.FuelTypes.OrderBy(m => m.Value).ToList(); var type1 = list.FirstOrDefault(m => m.Value == "Unleaded Regular"); var type2 = list.FirstOrDefault(m => m.Value == "Unleaded Plus"); var type3 = list.FirstOrDefault(m => m.Value == "Diesel #1"); list.Remove(type1); list.Remove(type2); list.Remove(type3); list = list.OrderBy(m => m.Value).ToList(); list.Insert(0, type1); list.Insert(1, type2); list.Insert(2, type3); return list.ToArray(); } else return new StringKeyValue[0]; } catch (Exception ex) { AddLog("ERROR", "FuelRecordBasePage.GetFuelTypes", ex.Message, ex.ToString()); return ex.Message; } } private object GetFuelRecordComments() { try { if (GetCurrentLoginSession() != null) { string fuleid = Request.Form["ClientData"]; CommentInfo[] comments = CreateClient().GetComments(SystemParams.CompanyID, "FuelRecord", fuleid); List list = new List(); foreach (var c in comments) { CommentItem citem = new CommentItem(); Helper.CloneProperty(citem, c); list.Add(citem); } return list.ToArray(); } else { return new CommentItem[0]; } } catch (Exception ex) { return ex.Message; } } private object AddFuelRecordComment() { try { var user = GetCurrentUser(); if (user != null) { string clientdata = HttpUtility.HtmlDecode(Request.Params["ClientData"]); string[] ps = JsonConvert.DeserializeObject(clientdata); string fuleid = ps[0]; string comment = ps[1]; CreateClient().SubmitComment(SystemParams.CompanyID, user.IID, "FuelRecord", fuleid.ToString(), comment, false); } return ""; } catch (Exception ex) { return ex.Message; } } #region Attachment private object GetAttachments() { try { if (GetCurrentLoginSession() != null) { string fuleid = HttpUtility.HtmlDecode(Request.Form["ClientData"]); AttachmentInfo[] atts = CreateClient().GetAttachments(SystemParams.CompanyID, "FuelRecord", fuleid); if (atts == null || atts.Length <= 0) return new AttachmentItem[0]; List list = new List(); foreach (AttachmentInfo att in atts) { AttachmentItem item = new AttachmentItem(); Helper.CloneProperty(item, att); item.AddedOn = item.AddedOn.AddHours(SystemParams.GetHoursOffset()); list.Add(item); } return list.OrderBy(m => m.AddedOn).ToArray(); } else return new AttachmentItem[0]; } catch (Exception ex) { return ex.Message; } } private object AddAttachment() { try { var session = GetCurrentLoginSession(); if (session != null) { string fuleid = HttpUtility.HtmlDecode(Request.Form["ClientData"]); HttpPostedFile uploadFile = null; byte[] iconfilebyte = null; if (Request.Files.Count > 0) { uploadFile = Request.Files[0]; iconfilebyte = ConvertFile2bytes(uploadFile); } AttachmentInfo attachment = new AttachmentInfo(); attachment.StringID = Guid.NewGuid().ToString().ToUpper(); attachment.FileName = uploadFile == null ? "" : uploadFile.FileName; attachment.Source = "FuelRecord"; attachment.SourceID = fuleid; attachment.FileData = iconfilebyte; attachment.AddedByUserIID = session.User.UID; string attid = CreateClient().AddAttachmentLegacy(SystemParams.CompanyID, attachment); return "OK"; } return "Failed"; } catch (Exception ex) { return ex.Message; } } private object DeleteAttachment() { try { var session = GetCurrentLoginSession(); if (session != null) { string attachid = HttpUtility.HtmlDecode(Request.Form["ClientData"]); CreateClient().DeleteAttachmentLegacy(SystemParams.CompanyID, attachid, session.User.UID); return "OK"; } return "Failed"; } catch (Exception ex) { return ex.Message; } } #endregion } }