229 lines
8.3 KiB
C#

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;
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;
}
}
}
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<FuelManagementClient>().GetFuelRecords(SystemParams.CompanyID, Convert.ToInt64(type), beginDate, endDate, searchtxt);
List<FuelRecordInfo> list = new List<FuelRecordInfo>();
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<FuelManagementClient>().GetFuelRecordAuditItems(SystemParams.CompanyID, fuleid);
List<FuelRecordAuditItem> list = new List<FuelRecordAuditItem>();
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<FuelRecordInfo>(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<FuelManagementClient>().AddNewFuelRecord(SystemParams.CompanyID, record, session.User.UID);
fuleid = fr.FuelID;
}
else
{
CreateClient<FuelManagementClient>().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<FuelManagementClient>().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<KeyValuePair<string, string>> 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;
}
}
}
}