using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Foresight.Data; using System.Web; using Foresight.Fleet.Services.AssetHealth; using Foresight.Fleet.Services.Asset; namespace IronIntel.Contractor.Maintenance { public class WorkOrderManager : BusinessBase { public WorkOrderManager(string dbstr) : base(dbstr) { } public long[] GetOpenWorkOrderID(string machineid) { const string SQL = "select WORKORDERID from WORKORDER where MACHINEID={0} and STATUS<>'Completed' and and isnull(DELETED,0)=0"; DataTable tb = GetDataTableBySQL(SQL, machineid); List result = new List(); foreach (DataRow dr in tb.Rows) { result.Add(FIDbAccess.GetFieldInt(dr[0], 0)); } return result.ToArray(); } public bool CanAccessWorkOrder(string sessionid, string woid, string mid, string useriid) { const string SQL = "select count(1) from WORKORDER where WORKORDERID={0} and ASSIGNEDTO={1}"; bool result = true; var user = FleetServiceClientHelper.CreateClient(sessionid).GetUserByIID(useriid); if ((!user.IsForesightUser) && (user.ContactType == Foresight.Fleet.Services.User.ContactTypes.Technician)) { result = FIDbAccess.GetFieldInt(GetRC1BySQL(SQL, woid, useriid), 0) > 0; } if (result && user.UserType < Foresight.Fleet.Services.User.UserTypes.Admin) { long assetid = -1; if (!string.IsNullOrEmpty(mid)) Int64.TryParse(mid, out assetid); else { var client = FleetServiceClientHelper.CreateClient(sessionid); var workorder = client.GetWorkOrderDetail(SystemParams.CompanyID, Convert.ToInt64(woid)); assetid = workorder.AssetID; } long[] availableAssetsids = FleetServiceClientHelper.CreateClient(sessionid).GetAvailableAssetsForUsers(SystemParams.CompanyID, useriid); if (availableAssetsids != null && !availableAssetsids.Contains(assetid)) result = false; } return result; } public string[] GetInvoiceNumber(string woid) { const string SQL = @"select a.INVOICENUMBER from WORKORDER_ALERTS t left join MAINTENANCELOG a on a.ALERTID=t.ALERTID,MACHINES b where t.WORKORDERID={0} and a.MACHINEID = b.MACHINEID and ISNULL(b.HIDE,0)<>1 and ISNULL(a.INVOICENUMBER,'')<>'' ORDER BY a.ADDEDON DESC"; DataTable dt = GetDataTableBySQL(SQL, woid); if (dt.Rows.Count == 0) return new string[0]; List list = new List(); foreach (DataRow dr in dt.Rows) { string num = FIDbAccess.GetFieldString(dr["INVOICENUMBER"], string.Empty); if (!string.IsNullOrEmpty(num)) list.Add(num); } return list.ToArray(); } public static string GenerateWorkOrderPrintHtml(string sessionid, string companyid, long woid) { StringBuilder str = new StringBuilder(); str.AppendLine("

Work Order

"); str.AppendFormat("
Details for work order <{0}> are listed below:
", woid); str.AppendLine(""); //str.AppendLine("
Work Order Information:
"); str.AppendLine("
"); var client = FleetServiceClientHelper.CreateClient(companyid, sessionid); WorkOrderDetail wo = client.GetWorkOrderDetail(companyid, woid); str.Append(GenerateWorkOrderInfoHtml(wo)); str.AppendLine("
"); //str.AppendLine("
Segments:
"); WorkOrderSegmentItem[] segments = client.GetSegments(companyid, woid); if (segments != null && segments.Length > 0) { for (int i = 0; i < segments.Length; i++) { var se = segments[i]; str.Append(GenerateSegmentHtml(se, i + 1)); } } return str.ToString(); } private static string GenerateWorkOrderInfoHtml(WorkOrderDetail wo) { StringBuilder str = new StringBuilder(); str.Append(""); str.AppendFormat("", wo.WorkOrderType); str.AppendLine(""); str.AppendFormat("", HttpUtility.HtmlEncode(wo.AssignedToName)); str.AppendLine(""); str.AppendFormat("", wo.Status); str.AppendLine(""); str.AppendFormat("", wo.DueDate == null ? "" : wo.DueDate.Value.ToShortDateString()); str.AppendLine(""); str.AppendFormat("", HttpUtility.HtmlEncode(wo.Description).Replace("\n", "
")); str.AppendLine(""); str.AppendFormat("", wo.MeterType); str.AppendLine(""); if (string.Compare(wo.MeterType, "HourMeter", true) == 0 || string.Compare(wo.MeterType, "Both", true) == 0) str.AppendFormat("", wo.HourMeter); if (string.Compare(wo.MeterType, "Odometer", true) == 0 || string.Compare(wo.MeterType, "Both", true) == 0) str.AppendFormat("", wo.Odometer, wo.OdometerUnits); str.AppendFormat("", wo.WorkOrderTotalCost); str.AppendLine(""); str.AppendFormat("", wo.OtherCost); str.AppendLine(""); str.AppendFormat("", wo.PartsCost); str.AppendLine(""); str.AppendFormat("", wo.TravelTimeCost); str.AppendLine(""); str.AppendFormat("", wo.LaborCost); str.AppendLine(""); str.AppendFormat("", wo.HourlyRate); str.AppendLine(""); str.AppendFormat("", wo.HoursToComplete); str.AppendLine(""); str.AppendFormat("", wo.CompleteDate == null ? "" : wo.CompleteDate.Value.ToShortDateString()); str.AppendLine(""); str.AppendFormat("", wo.InternalID); str.AppendLine(""); str.AppendFormat("", HttpUtility.HtmlEncode(wo.InvoiceNumber)); str.AppendLine(""); str.AppendFormat("", HttpUtility.HtmlEncode(wo.Notes).Replace("\n", "
")); str.AppendLine(""); str.AppendFormat("
Work Order Type{0}
Assigned To{0}
Status{0}
Due Date{0}
Description{0}
Meter Type{0}
Hour Meter{0}
Odometer{0} {1}
Work Order Total Costs ($){0}
Other Cost ($){0}
Parts Cost ($){0}
Travel Time Cost ($){0}
Labor Cost ($){0}
Hourly Rate{0}
Time To Complete(Hrs){0}
Completed Date{0}
Internal ID{0}
Invoice Number{0}
Notes{0}
"); return str.ToString(); } private static string GenerateSegmentHtml(WorkOrderSegmentItem se, int index) { StringBuilder str = new StringBuilder(); //str.AppendFormat("
", (index - 2) % 4 == 0 ? "page-break-after: always;" : ""); //str.AppendLine(""); str.AppendLine("
"); str.AppendLine(""); str.AppendFormat("", index); str.AppendLine(""); str.AppendFormat("", HttpUtility.HtmlEncode(se.UserName)); str.AppendLine(""); str.AppendFormat("", se.Hours); str.AppendLine(""); str.AppendFormat("", se.JobsiteName); str.AppendLine(""); str.AppendFormat("", se.Cost); str.AppendLine(""); str.AppendFormat("", se.Component); str.AppendLine(""); str.AppendFormat("", se.Completed ? "Yes" : "No"); str.AppendLine(""); str.AppendFormat("", se.CompletedDate == null ? "" : se.CompletedDate.Value.ToShortDateString()); str.AppendFormat("", HttpUtility.HtmlEncode(se.Description)); str.AppendLine(""); str.AppendFormat("", HttpUtility.HtmlEncode(se.Notes).Replace("\n", "
")); str.AppendLine(""); str.AppendLine("
Segment {0}
User{0}
Hours{0}
Job Site{0}
Cost{0}
Component{0}
Completed{0}
Completed Date{0}
Description{0}
Notes{0}
"); str.AppendLine("
"); return str.ToString(); } } }