private Error createPurchaseOrderDataFile(PurchaseOrderHeader poh, XElement file, ref string tempFile) { var error = new Error(); Dictionary <string, string> data = new Dictionary <string, string>(); // Create the CSV writer List <CSVFormat> formats = new List <CSVFormat>(); // Read the formatting configuration from the XML file foreach (var formatElement in file.Elements("Formats").Elements()) { // Read the formatting options CSVFormat fileFormat = new CSVFormat { DataFieldDelimiter = doReplacements(formatElement.Attribute("DataFieldDelimiter").Value), DataFieldDelimiterUsage = (CSVDelimiterUsage)Enum.Parse(typeof(CSVDelimiterUsage), formatElement.Attribute("DataFieldDelimiterUsage").Value), DataFieldSeparator = doReplacements(formatElement.Attribute("DataFieldSeparator").Value), HeaderFieldDelimiter = doReplacements(formatElement.Attribute("HeaderFieldDelimiter").Value), HeaderFieldSeparator = doReplacements(formatElement.Attribute("HeaderFieldSeparator").Value) }; // Now read the fields foreach (var fieldElement in formatElement.Elements("Fields").Elements()) { CSVField fieldFormat = new CSVField { FieldName = fieldElement.Attribute("Name").Value, FieldType = (CSVFieldType)Enum.Parse(typeof(CSVFieldType), fieldElement.Attribute("Type").Value) }; fileFormat.Fields.Add(fieldFormat); } formats.Add(fileFormat); } // Create a temp file tempFile = Path.GetTempFileName(); // Create the CSV writer CSVWriter sw = new CSVWriter(); sw.CreateFile(tempFile, formats); // Write the header lines for (int i = 0; i < sw.Formats.Count(); i++) { sw.CurrentFormat = i; sw.WriteHeaderLine(); } int idx = 0; if (sw.Formats.Count > 1) { // Now write the first data line sw.CurrentFormat = idx++; data.AddProperty("PurchaseID", poh.Id.ToString()); data.AddProperty("HeaderLabel", "H"); data.AddProperty("SupplierName", poh.Supplier.Name); data.AddProperty("SupplierInvoice", poh.SupplierInv); data.AddProperty("SupplierID", poh.SupplierId); string expDate = (poh.RequiredDate == null ? "" : poh.RequiredDate.Value.ToString("dd/MM/yyyy")); data.AddProperty("ExpectedDate", expDate); double totalCbms = 0; foreach (var pod in poh.PurchaseOrderDetails) { if (pod.OrderQty != null && pod.Product.UnitCBM != null) { totalCbms += pod.OrderQty.Value * pod.Product.UnitCBM.Value; } } data.AddProperty("CBMs", totalCbms); data.AddProperty("SKUs", poh.PurchaseOrderDetails.Count); sw.WriteLine(data); } // Now write the second and subsequent data lines sw.CurrentFormat = idx; foreach (var pohd in poh.PurchaseOrderDetails) { var prodPrice = pohd.Product .ProductPrices .Where(pp => pp.PriceLevel == "A") .FirstOrDefault(); data = new Dictionary <string, string>(); // Properties for Warehouse and common data.AddProperty("PurchaseID", poh.OrderNumber.ToString()); data.AddProperty("DetailLabel", "I"); data.AddProperty("PurchaseQty", pohd.OrderQty); data.AddProperty("ItemNumber", pohd.Product.ItemNumber); data.AddProperty("ItemDescription", pohd.Product.ItemName); data.AddProperty("PurchaseLineID", pohd.Id); data.AddProperty("Barcode", pohd.Product.BarCode); data.AddProperty("PackSize", prodPrice.QuantityBreakAmount); data.AddProperty("ProductDimensions", "0"); // In ACCESS this comes from tblItemAdditional, but the table is empty data.AddProperty("ProductWeight", "0"); // In ACCESS this comes from tblItemAdditional, but the table is empty // Properties for freight forwarder data.AddProperty("OrderNumber", poh.OrderNumber.ToString()); data.AddProperty("OrderQty", pohd.OrderQty); data.AddProperty("SupplierID", poh.SupplierId.ToString()); data.AddProperty("SupplierName", poh.Supplier.Name); data.AddProperty("ItemUnitPrice", (pohd.UnitPriceExTax == null ? "" : pohd.UnitPriceExTax.Value.ToString())); data.AddProperty("CurrencyCode", (poh.Currency == null ? "" : poh.Currency.CurrencyCode)); data.AddProperty("OrderDate", (poh.OrderDate == null ? "" : poh.OrderDate.Value.ToString("dd/MM/yyyy"))); data.AddProperty("RequiredShipDate", (poh.RequiredShipDate == null ? "" : poh.RequiredShipDate.Value.ToString("dd/MM/yyyy"))); data.AddProperty("ShipMethod", (poh.ShipMethodId == null ? "" : db.FindLOVItem(poh.ShipMethodId.Value).ItemText)); data.AddProperty("PortName", (poh.PortId == null ? "" : db.FindPort(poh.PortId.Value).PortName)); data.AddProperty("Destimation", (poh.PortArrivalId == null ? "" : db.FindPort(poh.PortArrivalId.Value).PortName)); data.AddProperty("ExchangeRate", poh.ExchangeRate); sw.WriteLine(data); } sw.Close(); return(error); }
private Error createPickDataFile(PickHeaderModel pickH, XElement file, ref string tempFile) { var error = new Error(); try { Dictionary <string, string> data = new Dictionary <string, string>(); // Create the CSV writer List <CSVFormat> formats = new List <CSVFormat>(); // Read the formatting configuration from the XML file foreach (var formatElement in file.Elements("Formats").Elements()) { // Read the formatting options CSVFormat fileFormat = new CSVFormat { DataFieldDelimiter = doReplacements(formatElement.Attribute("DataFieldDelimiter").Value), DataFieldDelimiterUsage = (CSVDelimiterUsage)Enum.Parse(typeof(CSVDelimiterUsage), formatElement.Attribute("DataFieldDelimiterUsage").Value), DataFieldSeparator = doReplacements(formatElement.Attribute("DataFieldSeparator").Value), HeaderFieldDelimiter = doReplacements(formatElement.Attribute("HeaderFieldDelimiter").Value), HeaderFieldSeparator = doReplacements(formatElement.Attribute("HeaderFieldSeparator").Value) }; // Now read the fields foreach (var fieldElement in formatElement.Elements("Fields").Elements()) { CSVField fieldFormat = new CSVField { FieldName = fieldElement.Attribute("Name").Value, FieldType = (CSVFieldType)Enum.Parse(typeof(CSVFieldType), fieldElement.Attribute("Type").Value) }; fileFormat.Fields.Add(fieldFormat); } formats.Add(fileFormat); } // Create a temp file var extn = file.Attribute("DataFileExtension").Value; tempFile = Path.GetTempPath() + pickH.Id.ToString() + ".CSV"; // extn; // Create the CSV writer CSVWriter sw = new CSVWriter(); sw.CreateFile(tempFile, formats); // Write the header lines for (int i = 0; i < sw.Formats.Count(); i++) { sw.CurrentFormat = i; sw.WriteHeaderLine(); } var customer = db.FindCustomer(pickH.CustomerId.Value); var country = db.FindCountry(pickH.ShipCountryId.Value); int idx = 0; if (sw.Formats.Count > 1) { // Now write the first data line sw.CurrentFormat = idx++; data.AddProperty("PickID", pickH.Id.ToString()); data.AddProperty("HeaderLabel", "H"); data.AddProperty("CustomerName", customer.Name); data.AddProperty("CustomerOrderNumber", pickH.CustPO); data.AddProperty("ShipAddress1", pickH.ShipAddress1); data.AddProperty("ShipAddress2", pickH.ShipAddress2); data.AddProperty("ShipAddress3", pickH.ShipAddress3); data.AddProperty("ShipSuburb", pickH.ShipSuburb); data.AddProperty("ShipState", pickH.ShipState); data.AddProperty("ShipPostcode", pickH.ShipPostcode); data.AddProperty("ShipCountry", country.CountryName); data.AddProperty("OnforwarderName", ""); data.AddProperty("OnforwarderAddress", ""); data.AddProperty("OnForwarderSuburb", ""); data.AddProperty("OnForwarderPostcode", ""); data.AddProperty("OnForwarderState", ""); data.AddProperty("OnForwarderCode", ""); data.AddProperty("CarrierCode", ""); data.AddProperty("CustomerID", pickH.CustomerId); data.AddProperty("DeliveryInstructions", pickH.DeliveryInstructions); data.AddProperty("CustomerContact", ""); data.AddProperty("PartnerEDI", ""); sw.WriteLine(data); } // Now write the second and subsequent data lines sw.CurrentFormat = idx; foreach (var pickD in pickH.PickDetails) { data = new Dictionary <string, string>(); // Properties for Warehouse and common var product = db.FindProduct(pickD.ProductId.Value); data.AddProperty("PickID", pickH.Id); data.AddProperty("DetailLabel", "I"); data.AddProperty("PickQty", pickD.QtyToPick); data.AddProperty("ItemNumber", product.ItemNumber); data.AddProperty("PickLineID", pickD.Id); sw.WriteLine(data); } sw.Close(); } catch (Exception ex) { error.SetError(ex); } return(error); }