public int ImportInvioce(Stream csvFile) { StreamReader textReader = new StreamReader(csvFile); CsvReader CSV = new CsvReader(textReader); int count = 0; while (CSV.Read()) { string invoiceNo = CSV.GetField<string>("invoice#").Trim(); string customerName = CSV.GetField<string>("customer"); string date = CSV.GetField<string>("date"); string description = CSV.GetField<string>("description"); string price = CSV.GetField<string>("price"); string qty = CSV.GetField<string>("qty"); DateTime orderDate = DateTime.Parse(date); decimal unitPrice = decimal.Parse(price); decimal Qty = decimal.Parse(qty); Customer customer = custSvr.GetByName(customerName); if (customer != null) { Invoice invoice = invSvr.GetByInvoiceNumber(invoiceNo); if (invoice != null) { //update record? } else { OrderDetail orderDetail = new OrderDetail { Description = description, UnitPrice = unitPrice, Qty = Qty }; Invoice newInvoice = new Invoice { OrderDate = orderDate, OrderNumber = invoiceNo, CustomerId = customer.Id }; newInvoice.OrderDetail.Add(orderDetail); try { invSvr.Save(newInvoice); count++; } catch (Exception e) { throw new Exception("Save Invoice Error", e); } } } } return count; }
public int ImportInvioceDetail(Stream csvFile) { StreamReader textReader = new StreamReader(csvFile); CsvReader CSV = new CsvReader(textReader); CSV.Configuration.IsHeaderCaseSensitive = false; CSV.Configuration.TrimFields = true; CSV.Configuration.WillThrowOnMissingField = false; int count = 0; List<OrderDetail> list = new List<OrderDetail>(); try { while (CSV.Read()) { string invoiceNo = CSV.GetField<string>("invoice#").Trim(); string description = CSV.GetField<string>("description"); string price = CSV.GetField<string>("UnitPrice"); string qty = CSV.GetField<string>("qty"); string strDiscount = CSV.GetField<string>("Discount"); decimal unitPrice = decimal.Parse(price); decimal Qty = decimal.Parse(qty); decimal discount = decimal.Parse(strDiscount); //Get GST by Code Invoice invoice = invSvr.GetByInvoiceNumber(invoiceNo); if (invoice != null) { OrderDetail orderDetail = new OrderDetail { Description = description, UnitPrice = unitPrice, Discount=discount, Qty = Qty, OrderId = invoice.Id }; list.Add(orderDetail); } } orderDetailSvr.Save(list); } catch (Exception e) { throw new Exception("Save Invoice details Error", e); } return count; }