using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using FileHelpers; using GraphQL; using Newtonsoft; using Newtonsoft.Json.Linq; namespace BodyshopPartner.Utils { public static class ARMSRoData { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static string ArmsRoDataQuery = @" query ENTEGRAL_EXPORT($bodyshopid: uuid!, $start: timestamptz!, $end: timestamptz!) { jobs(where: {_and: [{_or: [{inproduction: {_eq: true}}, {_and: [{actual_completion: {_gte: $start}}, {actual_completion: {_lte: $end}}]}]}, {shopid: {_eq: $bodyshopid}}]}) { joblines { id line_ind } id ro_number status asgn_date date_open kmin scheduled_completion actual_completion actual_delivery date_exported actual_in ins_co_nm ins_addr1 ins_addr2 ins_city ins_st ins_zip ins_ctry ins_ph1 ins_ph2 est_ct_ln est_ct_fn insd_fn insd_ln ownr_fn ownr_ln ownr_co_nm ownr_addr1 ownr_addr2 ownr_city ownr_st ownr_zip ownr_ctry ownr_ph1 ownr_ph2 ownr_ea clm_ct_fn clm_ct_ln v_vin kmin plate_no v_model_yr v_make_desc v_model_desc v_color driveable clm_no policy_no loss_date area_of_damage tlos_ind parts_tax_rates federal_tax_rate state_tax_rate rate_la1 rate_la2 rate_la3 rate_la4 rate_laa rate_lab rate_lad rate_lae rate_laf rate_lag rate_lam rate_lar rate_las rate_lau rate_ma2s rate_ma2t rate_ma3s rate_mabl rate_macs rate_mahw rate_mapa rate_mash rate_matd job_totals ded_amt cieca_ttl adjustment_bottom_line employee_body_rel { employee_number first_name last_name } employee_refinish_rel { employee_number first_name last_name } } bodyshops_by_pk(id: $bodyshopid) { id entegral_id shopname md_ro_statuses features } } "; public static async Task GenerateArmsROData() { logger.Debug("Generating ARMS Info."); try { FileHelperEngine engine = new FileHelperEngine(); var r = new GraphQLRequest { Query = ArmsRoDataQuery, Variables = new { start = DateTime.Now.Date.AddDays(-30), end = DateTime.Now.Date, bodyshopid = AppMetaData.ActiveShopId } }; var data = await Utils.GraphQL.ExecuteQuery(r); var RepairOrders = new List(); Regex ROStripper = new Regex(@"\d"); foreach (dynamic job in data.jobs) { logger.Debug(job.ro_number); var RoNumber = string.Join("", Regex.Matches((string)(job.ro_number?.Value), @"\d").OfType().Select(m => m.Value)); var VehicleYr = string.Join("", Regex.Matches((string)(job.v_model_yr?.Value ?? ""), @"\d").OfType().Select(m => m.Value)); RepairOrders.Add(new ARMSRoDataModel() { ShopShortName = data.bodyshops_by_pk.entegral_id?.Value, RO = int.Parse(RoNumber), TransType = job.actual_completion?.Value == null ? "Z" : "C", //TODO*** Finish this status mapping. ShopLongName = data.bodyshops_by_pk.shopname?.Value, EstimatorID = job.est_ct_ln, EstimatorName = $"{job.est_ct_fn} {job.est_ct_ln}", BodymanID = job.employee_body_rel?.Value == null ? "" : job.employee_body_rel?.employee_number?.Value, CustomerFirstName = job.ownr_fn?.Value, CustomerLastName = job.ownr_ln?.Value, CustomerStreet = job.ownr_addr1?.Value, CustomerCity = job.ownr_city?.Value, CustomerState = job.ownr_st?.Value, CustomerZip = job.ownr_zip?.Value, CustomerPhone1 = job.ownr_ph1?.Value, CustomerPhone2 = job.ownr_ph2?.Value, //RetWhslCustomer = "R", Year = VehicleYr != "" ? int.Parse(VehicleYr) : 0, //TODO STRIP TO 2 NUMBERS Make = job.v_make_desc?.Value, Model = job.v_model_desc?.Value, VIN = job.v_vin?.Value, License = job.plate_no?.Value, MileageIn = job.kim ?? 0, CompanyName = job.ins_co_nm?.Value, InsuranceAddress = job.ins_addr1?.Value, InsuranceCity = job.ins_city?.Value, InsuranceState = job.ins_st?.Value, InsuranceZip = job.ins_zip?.Value, ClaimType = "N/A", Claim = job.clm_no?.Value, DateOpened = job.date_open?.Value,// ? job.date_open : null, EstComplete = job.date_open?.Value, //DateofLoss = job.loss_date?.Value,// ? job.loss_date : null, PromiseDate = job.scheduled_completion, CarComplete = job.actual_completion, CarinShop = job.actual_in, CustPickup = job.actual_delivery, DateClosed = job.date_invoiced, BodyRate = job.rate_lab, RefinishRate = job.rate_lar, MechanicalRate = job.rate_lam, StructuralRate = job.rate_las, PMRate = job.rate_mapa, BMRate = job.rate_mash, RevisedTotalsBodyHours = (Decimal?)job.job_totals?.rates?.lab?.hours?.Value, RevisedTotalsRefinishHours = (Decimal?)job.job_totals?.rates?.lar?.hours?.Value, RevisedTotalsMechanicalHours = (Decimal?)job.job_totals?.rates?.lam?.hours?.Value, RevisedTotalsStructuralHours = (Decimal?)job.job_totals?.rates?.las?.hours?.Value, RevisedTotalsPartsTotal = ((Decimal?)job.job_totals?.parts?.parts?.total?.amount?.Value) / 100, RevisedTotalsSubletTotal = ((Decimal?)job.job_totals?.parts?.sublets?.total?.amount?.Value) / 100, RevisedTotalsBodyLaborTotal = ((Decimal?)job.job_totals?.rates?.lab?.total?.amount?.Value) / 100, RevisedTotalsRefinishLaborTotal = ((Decimal?)job.job_totals?.rates?.lar?.total?.amount?.Value) / 100, RevisedTotalsMechanicalLaborTotal = ((Decimal?)job.job_totals?.rates?.lam?.total?.amount?.Value) / 100, RevisedTotalsStructuralLaborTotal = ((Decimal?)job.job_totals?.rates?.las?.total?.amount?.Value) / 100, RevisedTotalsPMTotal = ((Decimal?)job.job_totals?.rates?.mapa?.total?.amount?.Value) / 100, RevisedTotalsBMTotal = ((Decimal?)job.job_totals?.rates?.mash?.total?.amount?.Value) / 100, RevisedTotalsSalesTaxTotal = ((Decimal?)(job.job_totals?.totals?.federal_tax?.amount?.Value + job.job_totals?.totals?.state_tax?.amount?.Value)) / 100, RevisedTotalsGrossTotal = ((Decimal?)job.job_totals?.totals?.total_repairs?.amount?.Value) / 100, RevisedTotalsDeductibleTotal = (Decimal?)job.ded_amt?.Value, // RevisedTotalsDepreciationTotal = job.job_totals?.rates?.lab?.hours?.Value, TotalLossYN = job.tlos_ind, BodyTechName = job.employee_body_rel?.Value == null ? "" : $"{job.employee_body_rel?.first_name?.Value} {job.employee_body_rel?.last_name?.Value}", Vehiclecolor = job.v_color?.Value, PaintTechID = job.employee_refinish_rel?.Value == null ? "" : job.employee_refinish_rel?.employee_number?.Value, PaintTechName = job.employee_refinish_rel?.Value == null ? "" : $"{job.employee_refinish_rel?.first_name?.Value} {job.employee_refinish_rel?.last_name?.Value}", ProductionStageCode = StatusMapper(job, data.bodyshops_by_pk.features.entegral), Vehiclescheduledindate = job.scheduled_in?.Value }); } Directory.CreateDirectory(Properties.Settings.Default.ArmsExportPath); engine.WriteFile($"{Properties.Settings.Default.ArmsExportPath}\\{DateTime.Now.ToString("MMdd")}{data.bodyshops_by_pk.entegral_id?.Value}.txt", RepairOrders); } catch (Exception ex) { logger.Error(ex, "Exception while creating ARMS RO Data Extract: "); } } public static string StatusMapper(dynamic job,dynamic features) { JArray j = (JArray)(features); var x = j.Where(v => v["status"] == job.status).FirstOrDefault(); if (x == null) return "8D"; return x["code"].ToString() ; } } }