public static string OrderSelect(dynamic data, int type = 0) { //Dictionary<int, Zmapping> orders = AttacheFetchData.getBody(); string query = ""; using (var db = new ZudelloContext()) { var body = db.Zmapping.Where(i => i.DocType == "PURCHASING::RECEIVE:QUERY").FirstOrDefault(); query = body.Body; } dynamic myObj = JsonConvert.DeserializeObject <ExpandoObject>(query); string myQuery = ""; string myLineQuery = ""; if (type == 0) { myQuery = myObj.HDR_QUERY.ToString(); myLineQuery = myObj.LINE_QUERY.ToString(); } else { //Invoice line type required different query myQuery = myObj.HDR_QUERY_INVOICE.ToString(); myLineQuery = myObj.LINE_QUERY_INVOICE.ToString(); } // Loop through and get the data.Receive IDs. Dictionary <int, LineReceipting> lineReceipting = new Dictionary <int, LineReceipting>(); List <string> internalDocNbr = new List <string>(); try { int i = 0; foreach (var id in data.document.lines) { foreach (var nbr in id.receive) { LineReceipting LineData = new LineReceipting(); if (!internalDocNbr.Contains(nbr.remote_order_id.ToString())) { internalDocNbr.Add(nbr.remote_order_id.ToString()); LineData.internalNbr = nbr.remote_order_id.ToString(); LineData.lineNbr = nbr.line.ToString(); #warning Taken out for general testing // LineData.poNbr = nbr.number.ToString(); LineData.receiptQty = nbr.qty.ToString(); } i = i + 1; lineReceipting.Add(i + 1, LineData); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } string accountCode = data.document.supplier.code.ToUpper(); string poNbr = String.Join(", ", internalDocNbr.ToArray()); //Hdr myQuery = myQuery.Replace("{po_number}", poNbr); myQuery = myQuery.Replace("{account_code}", accountCode); //Lines myLineQuery = myLineQuery.Replace("{po_number}", poNbr); //Query to get HDR DataSet orderData = AttacheFetchData.GetDataSet(myQuery); List <string> Sequence = new List <string>(); foreach (DataTable dt in orderData.Tables) { foreach (DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) { Sequence.Add(dr[dc.ColumnName].ToString()); } } } //Query to get lines DataSet lineOrderData = AttacheFetchData.GetDataSet(myLineQuery); // List<string> LineSequence = new List<string>(); //Might need to put this as its own Function where uesr can specific the sequence ie ,,,,,,,,,20,204, or extra. List <object[]> MyLineSequence = new List <object[]>(); foreach (DataTable dt in lineOrderData.Tables) { foreach (DataRow dr in dt.Rows) { //Add to array MyLineSequence.Add(dr.ItemArray); } } StringBuilder lineSequence = new StringBuilder(); foreach (var obj in MyLineSequence) { //Get the sequence to return, looking at dictionary to comapre. foreach (var dic in lineReceipting) { if (dic.Value.internalNbr == obj[1].ToString() && dic.Value.lineNbr == obj[2].ToString()) { lineSequence.Append(dic.Value.receiptSeq.Replace("0", dic.Value.receiptQty.ToString())); //not sure if this is needed, but still works? lineReceipting.Remove(dic.Key); break; } else { lineSequence.Append(dic.Value.receiptSeq); break; } } } // Console.ReadLine(); StringBuilder upSequence = new StringBuilder(); int lineUp = MyLineSequence.Count(); for (int i = 0; i < lineUp; i++) { upSequence.Append("<UP>"); } // string lineValues //Maybe walk object in scribian //or here need to now use ,,,0<F9> if not part of receipt. //Maybe put in query. string returnSequence = String.Join("", Sequence.ToArray()); Console.WriteLine(returnSequence); Console.ReadLine(); returnSequence = returnSequence + "<F9>" + upSequence.ToString() + lineSequence.ToString(); Console.WriteLine(returnSequence); return(returnSequence); }
public static async Task PushDataToZudello() { string Token = ZudelloLogin.Login(); foreach (var Mappings in getBody()) { string myReplacement = ""; string myObjType = Mappings.Value.DocType; dynamic myObj = JsonConvert.DeserializeObject <ExpandoObject>(Mappings.Value.Body); string myLinkValue = myObj.LINK.ToString(); string myQuery = myObj.HDR_QUERY.ToString(); if (myQuery.Contains("{LAST_ID}")) { try { using (var db = new ZudelloContext()) { var lastSyncValue = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); myQuery = myQuery.Replace("{LAST_ID}", lastSyncValue.lastID.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (myQuery.Contains("{LAST_SYNC}")) { try { using (var db = new ZudelloContext()) { var lastSyncValue = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); DateTime MyQueryDate = DateTime.Parse(lastSyncValue.LastSync); myQuery = myQuery.Replace("{LAST_SYNC}", MyQueryDate.ToString("yyyy-MM-dd HHmmss")); } } catch { } } var headerDs = AttacheFetchData.GetDataSet(myQuery); try { myReplacement = myObj.REPLACE_ME.ToString(); } catch { } bool LineQuery = false; if (myLinkValue != "") { LineQuery = true; } foreach (DataTable dt in headerDs.Tables) { foreach (DataRow dr in dt.Rows) { Data QData = new Data(); Output output = new Output(); QData.object_type = myObjType; //dynamic obj = new DynamicClass(fields); Dictionary <string, object> temp = new Dictionary <string, object>(); foreach (DataColumn dc in dt.Columns) { dynamic Colvalue; if (dr[dc.ColumnName] is String) { Colvalue = dr[dc.ColumnName].ToString(); Colvalue = Colvalue.Trim(); } else { Colvalue = dr[dc.ColumnName]; } temp.Add(dc.ColumnName, Colvalue); } QData.data = temp; output.data = QData; if (LineQuery == true) { var query = temp[myLinkValue]; // output.data.data = temp; string myQuery2 = myObj.LINE_QUERY.ToString().Replace("{0}", query.ToString()); var lineDs = GetDataSet(myQuery2); List <object> mArray = new List <object>(); foreach (DataTable lt in lineDs.Tables) { foreach (DataRow lr in lt.Rows) { Dictionary <string, object> lstemp = new Dictionary <string, object>(); foreach (DataColumn lc in lt.Columns) { dynamic lineValue; if (lr[lc.ColumnName] is String) { lineValue = lr[lc.ColumnName].ToString(); lineValue = lineValue.Trim(); } else { lineValue = lr[lc.ColumnName]; } lstemp.Add(lc.ColumnName, lineValue); } mArray.Add(lstemp); } } Dictionary <int, object> myDic = new Dictionary <int, object>(); object REPLACEME = new object(); string data = JsonConvert.SerializeObject(output); dynamic obj = JsonConvert.DeserializeObject <ExpandoObject>(data); obj.data.data.REPLACEME = mArray; string lineData = JsonConvert.SerializeObject(obj); lineData = lineData.Replace("REPLACEME", myReplacement); Console.WriteLine(lineData); //If this record already exisits in the hashing table then continue with the loop. if (HashingRecords.Hash(lineData, Mappings.Value.Id) == true) { continue; } Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, lineData)); using (var db = new ZudelloContext()) { var updateLastSync = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); updateLastSync.LastSync = DateTime.UtcNow.ToString(); try { int myInternalId = Convert.ToInt32(temp[myLinkValue]); updateLastSync.lastID = myInternalId; } catch { } db.SaveChanges(); } } else { string data = JsonConvert.SerializeObject(output); Console.WriteLine(data); //If this record already exisits in the hashing table then continue with the loop. if (HashingRecords.Hash(data, Mappings.Value.Id) == true) { continue; } Console.WriteLine(ZudelloLogin.SendToZudelloQueue(Token, data)); using (var db = new ZudelloContext()) { var updateLastSync = (from a in db.Zlastsync join c in db.Zmapping on a.MappingId equals c.Id where c.Id == Mappings.Value.Id select a).FirstOrDefault(); updateLastSync.LastSync = DateTime.UtcNow.ToString(); db.SaveChanges(); } } } } } }