/// <summary> /// set customer name as shipment customer name /// </summary> /// <param name="jsonObject"></param> /// <param name="customerName"></param> /// <returns></returns> public string SetCustomerName(SiteflowOrder.RootObject jsonObject, string customerName) { if (jsonObject.orderData.shipments.Count > 0) { customerName = jsonObject.orderData.shipments[0].shipTo.name; jsonObject.orderData.customerName = customerName; } return(customerName); }
/// <summary> /// check media clip item exists /// </summary> /// <param name="jsonObject"></param> /// <returns></returns> public bool ContainsMediaClipItem(SiteflowOrder.RootObject jsonObject) { bool hasMediaClipItem = false; foreach (var item in jsonObject.orderData.items) { if (!string.IsNullOrEmpty(item.supplierPartAuxiliaryId)) { hasMediaClipItem = true; break; } } return(hasMediaClipItem); }
public DateTime SetOrderDatetime(SiteflowOrder.RootObject jsonObject) { var orderDatetime = Convert.ToDateTime(jsonObject.orderData.slaTimestamp); if (orderDatetime < DateTime.Now.AddMonths(-3)) { orderDatetime = DateTime.Now; } jsonObject.orderData.slaTimestamp = orderDatetime; foreach (var orderDataShipment in jsonObject.orderData.shipments) { orderDataShipment.canShipEarly = true; orderDataShipment.slaDays = 3; } return(orderDatetime); }
/// <summary> /// Based on supplierPartAuxiliaryId download the files /// </summary> /// <param name="hasMediaClipItem"></param> /// <param name="jsonObject"></param> /// <param name="pdfCount"></param> public void MediaClipFilesDownload(bool hasMediaClipItem, SiteflowOrder.RootObject jsonObject, int pdfCount) { if (hasMediaClipItem) { foreach (var item in jsonObject.orderData.items) { if (!string.IsNullOrEmpty(item.supplierPartAuxiliaryId)) { var mediaClipNumber = Convert.ToInt32(item.mediaclipLineNumber); var orderDetails = _mediaClipEntities.tMediaClipOrderDetails.FirstOrDefault(m => m.SupplierPartAuxilliaryId == item.supplierPartAuxiliaryId && m.LineNumber == mediaClipNumber); var extrinsicDetails = _mediaClipEntities.tMediaClipOrderExtrinsic .Where(e => e.MediaClipOrderDetailsId == orderDetails.OrderDetailsId).ToList(); if (item.components.Count == 1) { var extrinsic = extrinsicDetails.FirstOrDefault(); DownloadPdf(extrinsic.ExtrinsicValue, _localProcessingPath + "/PDFS/" + jsonObject.orderData.sourceOrderId + "-" + (pdfCount) + ".PDF"); } else { foreach (var component in item.components) { var path = component.path; var coverOrText = component.code; if (coverOrText == "Cover") { var coverExtrinsic = extrinsicDetails.FirstOrDefault(x => x.ExtrinsicName.Contains("cover")); DownloadPdf(coverExtrinsic.ExtrinsicValue, _localProcessingPath + "/PDFS/" + jsonObject.orderData.sourceOrderId + "-" + (1) + ".PDF"); } else { if (coverOrText == "Text") { var pageExtrinsic = extrinsicDetails.FirstOrDefault(x => x.ExtrinsicName.Contains("pages")); DownloadPdf(pageExtrinsic.ExtrinsicValue, _localProcessingPath + "/PDFS/" + jsonObject.orderData.sourceOrderId + "-" + (2) + ".PDF"); } else { var extrinsic = extrinsicDetails.FirstOrDefault(); DownloadPdf(extrinsic.ExtrinsicValue, _localProcessingPath + "/PDFS/" + jsonObject.orderData.sourceOrderId + "-" + (pdfCount) + ".PDF"); } } } } } } } }
public Dictionary <string, string> CreateOrder() { //get each order json pdf from FTP location var jsonFiles = new DirectoryInfo(_localProcessingPath + "\\Input\\").GetFiles("*.json"); if (!jsonFiles.Any()) { return(new Dictionary <string, string>()); } Dictionary <string, string> processingSummary = new Dictionary <string, string>(); foreach (var jsonFile in jsonFiles) { string json = ""; SiteflowOrder.RootObject jsonObject = new SiteflowOrder.RootObject(); bool exceptionJsonRead = false; try { jsonObject = ReadJsonFile(jsonFile, ref json); } catch (Exception e) { exceptionJsonRead = true; } if (exceptionJsonRead) { processingSummary.Add(Path.GetFileName(jsonFile.FullName), "JSON structure issue- Order failed"); File.Delete(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); continue; } var customerName = ""; customerName = SetCustomerName(jsonObject, customerName); var sourceOrderId = ""; try { sourceOrderId = jsonObject.orderData.sourceOrderId; } catch { processingSummary.Add(Path.GetFileNameWithoutExtension(jsonFile.FullName), "Error- Json structure issue"); if (File.Exists(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName))) { File.Delete(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); } File.Move(jsonFile.FullName, _localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); continue; } var itemFound = _orderHelper.DoesOrderExists(sourceOrderId); if (itemFound) { if (File.Exists(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName))) { File.Delete(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); } File.Move(jsonFile.FullName, _localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); processingSummary.Add(sourceOrderId, "Order exists in database and order has already been pushed to siteflow"); continue; } if (jsonObject.orderData.shipments.Count > 0 && jsonObject.orderData.shipments[0].shipTo != null) { if (string.IsNullOrEmpty(jsonObject.orderData.shipments[0].shipTo.address1) || string.IsNullOrEmpty(jsonObject.orderData.shipments[0].shipTo.town) || string.IsNullOrEmpty(jsonObject.orderData.shipments[0].shipTo.postcode)) { if (File.Exists(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName))) { File.Delete(_localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); } File.Move(jsonFile.FullName, _localProcessingPath + "\\ProcessedInput\\" + Path.GetFileName(jsonFile.FullName)); processingSummary.Add(sourceOrderId, "Error - Incomplete Address"); continue; } } var orderDatetime = SetOrderDatetime(jsonObject); decimal orderTotal = 0M; decimal deliveryCost = 0M; string email = ""; string telephone = ""; string originalJson = json; ////create order and order details, address entry in database var orderId = _orderHelper.CreateOrder(sourceOrderId, orderDatetime, orderTotal, deliveryCost, email, telephone, originalJson); var itemCount = jsonObject.orderData.items.Count; foreach (var item in jsonObject.orderData.items) { var sourceItemId = item.sourceItemId; var sku = item.sku; if (string.IsNullOrEmpty(sku)) { if (processingSummary.ContainsKey(sourceOrderId)) { processingSummary[sourceOrderId] += "NULL SKU - Order failed"; } else { processingSummary.Add(sourceOrderId, "NULL SKU - Order failed"); } break; } var qty = item.quantity; var pdfUri = item.components[0].path; bool staticOrder = pdfUri != null && pdfUri.ToLower().Contains("static"); var pdfName = ""; if (staticOrder) { pdfName = pdfUri.Split('/').Last(); } var partArray = pdfUri.Split(new char[] { '-' }); var pdfCount = 1; if (itemCount > 1) { if (partArray.Length == 2) { partArray[1] = partArray[1].Replace(".pdf", ""); try { pdfCount = Convert.ToInt32(partArray[1]); } catch (Exception e) { pdfCount = 1; } } } bool hasMediaClipItem = !string.IsNullOrEmpty(item.supplierPartAuxiliaryId); if (hasMediaClipItem) { staticOrder = false; } MediaClipFilesDownload(hasMediaClipItem, jsonObject, pdfCount); var substrate = item.components[0].attributes.Substrate; var pdfPath = _localProcessingPath + "/PDFS/"; var staticPdfPath = ConfigurationManager.AppSettings["StaticPDFPath"]; if (!sku.ToLower().Contains("photobook")) { if (staticOrder) { if (!File.Exists(staticPdfPath + pdfName)) { //send email processingSummary.Add(sourceOrderId + "-" + sourceItemId, staticPdfPath + pdfName + " not found in static folder"); if (processingSummary.ContainsKey(sourceOrderId)) { processingSummary[sourceOrderId] += "Order failed"; } else { processingSummary.Add(sourceOrderId, "Order failed"); } continue; } File.Copy(staticPdfPath + pdfName, pdfPath + sourceItemId + ".PDF", true); } else { if (!File.Exists(_localProcessingPath + "/PDFS/" + sourceOrderId + "-" + (pdfCount) + ".PDF")) { processingSummary.Add(sourceOrderId + "-" + sourceItemId, sourceOrderId + "-" + (pdfCount) + ".PDF" + " PDF not found"); if (processingSummary.ContainsKey(sourceOrderId)) { processingSummary[sourceOrderId] += "Order failed"; } else { processingSummary.Add(sourceOrderId, "Order failed"); } continue; } File.Copy(_localProcessingPath + "/PDFS/" + sourceOrderId + "-" + (pdfCount) + ".PDF", pdfPath + sourceItemId + ".PDF", true); } } string orderfileName = pdfPath + sourceItemId + ".PDF"; string ordersubstrateName = substrate; string orderbarcode = sourceItemId; string orderorderId = sourceOrderId; string orderQty = Convert.ToString(qty); var originalOrderInputPath = ConfigurationManager.AppSettings["OriginalOrderInputPath"]; var finalPdfPath = originalOrderInputPath + "/Processed/" + orderorderId + "_" + orderbarcode + ".PDF"; if (sku.ToLower().Contains("photobook")) { PhotobookProcessing(sourceOrderId, originalOrderInputPath, orderorderId, orderbarcode, item); } else { File.Copy(_localProcessingPath + "/PDFS/" + sourceOrderId + "-" + (pdfCount) + ".PDF", finalPdfPath, true); item.components[0].path = "https://smilepdf.espsmile.co.uk/pdfs/Processed/" + orderorderId + "_" + orderbarcode + ".PDF"; } _orderHelper.AddOrderItem(orderId, sku, sourceItemId, qty, substrate, finalPdfPath); } var serializedResultJson = JsonConvert.SerializeObject( jsonObject, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var goodOrder = IsGoodOrder(processingSummary, sourceOrderId); if (goodOrder) { _orderHelper.SubmitModifiedSiteflowJson(orderId, serializedResultJson); } var fileName = Path.GetFileName(jsonFile.FullName); if (File.Exists(_localProcessingPath + "\\ProcessedInput\\" + fileName)) { File.Delete(_localProcessingPath + "\\ProcessedInput\\" + fileName); } File.Move(jsonFile.FullName.ToString(), _localProcessingPath + "\\ProcessedInput\\" + fileName); if (!processingSummary.ContainsKey(sourceOrderId)) { processingSummary.Add(sourceOrderId, "OK"); } } return(processingSummary); }