WIP PPG Data Pump

This commit is contained in:
Patrick Fic
2021-07-13 21:42:41 -07:00
parent 25b0b8ff4f
commit dc88d2baa3
11 changed files with 222 additions and 3 deletions

View File

@@ -0,0 +1,148 @@
using GraphQL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BodyshopPartner.Utils
{
public static class PPGMixData
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static string PpgDataQuery = @"query PPGDataPump($today: timestamptz!, $todayplus5: timestamptz!, $shopid: uuid!) {
bodyshops_by_pk(id:$shopid) {
id
shopname
}
jobs(where: {_or: [{_and: [{scheduled_in: {_lte: $todayplus5}}, {scheduled_in: {_gte: $today}}]}, {inproduction: {_eq: true}}]}) {
id
ro_number
status
ownr_fn
ownr_ln
ownr_co_nm
v_vin
v_model_yr
v_make_desc
v_model_desc
v_color
plate_no
ins_co_nm
est_ct_fn
est_ct_ln
rate_mapa
rate_lab
job_totals
labhrs: joblines_aggregate(where: {mod_lbr_ty: {_neq: ""LAR""}, removed: {_eq: false}}) {
aggregate {
sum {
mod_lb_hrs
}
}
}
larhrs: joblines_aggregate(where: { mod_lbr_ty: { _eq: ""LAR""}, removed: { _eq: false} }) {
aggregate {
sum {
mod_lb_hrs
}
}
}
}
}
";
public static async Task PushDataToPPG()
{
try
{
//Get the path for the XML file that should be written. Ensure that we can read and write to that directory.
//Query the database based on the active shop for all work that's coming within the next 5 days + all active work.
var r = new GraphQLRequest
{
Query = PpgDataQuery,
Variables = new
{
today = DateTime.Now.Date,
todayplus5 = DateTime.Now.Date.AddDays(5),
shopid = AppMetaData.ActiveShopId
}
};
var data = await Utils.GraphQL.ExecuteQuery(r);
//Create an XML document node
XDocument doc = new XDocument(
new XElement("PPG",
new XElement("Header",
new XElement("Protocol", new XElement("Message", "PaintShopInterface"), new XElement("Name", "PPG"), new XElement("Version", "1.5.0")),
new XElement("Transaction", new XElement("TransactionDate", new DateTime().ToString("yyyy-MM-DDTHH:MM:SS"))),
new XElement("Product", new XElement("Name", data?.bodyshop_by_pk?.paintshopid))
),
new XElement("DataInterface",
new XElement("ROData",
new XElement("ShopInfo",
new XElement("ShopID", data?.bodyshop_by_pk?.shopname),
new XElement("ShopName", data?.bodyshop_by_pk?.shopname)
),
new XElement("RepairOrders",
new XElement("ROCount", data?.jobs?.Count)
)
)
)
)
);
foreach(dynamic job in data.jobs)
{
doc.Element("PPG").Element("DataInterface").Element("ROData").Element("RepairOrders").Add(new XElement("RO",
new XElement("RONumber", job.ro_number?.Value),
new XElement("ROStatus", job.status?.Value),
new XElement("Customer", $"{job.ownr_ln}, {job.ownr_fn}"),
new XElement("ROPainterNotes", ""),
new XElement("LicensePlateNum", job.plate_no?.Value),
new XElement("VIN", job.v_vin?.Value),
new XElement("ModelYear", job.v_model_yr?.Value),
new XElement("MakeDesc", job.v_make_desc?.Value),
new XElement("ModelName", job.v_model_desc?.Value),
// new XElement("OEMColorCode", job.vehicle?.colorcode),
new XElement("RefinishLaborHours", job.larhrs?.aggregate?.sum.mod_lb_hrs?.Value),
new XElement("InsuranceCompanyName", job.ins_co_nm?.Value),
new XElement("EstimatorName", $"{job.est_ct_ln}, {job.est_ct_fn}"),
new XElement("PaintMaterialsRevenue", 0),
new XElement("PaintMaterialsRate", job.rate_mapa?.Value),
new XElement("BodyHours", job.labhrs?.aggregate.sum?.mod_lb_hrs?.Value),
new XElement("BodyLaborRate", job.rate_lab?.Value)
//new XElement("TotalCostOfRepairs", job.job_total.totals.subtotal.amount?.Value / 100)
));
}
doc.Save(@"C:\\VPS\\doc.xml");
// doc.Save(Properties.Settings.Default.PaintScalePath);
//Ensure all values for strings are escaped.
//Write the file to the disk and start the timer again.
}
catch (Exception ex)
{
logger.Error(ex);
}
}
}
}