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 ImportInvioceHeader(Stream csvFile) { StreamReader textReader = new StreamReader(csvFile); CsvReader CSV = new CsvReader(textReader); CSV.Configuration.IsHeaderCaseSensitive = false; CSV.Configuration.TrimFields = true; CSV.Configuration.WillThrowOnMissingField = false; List<Invoice> list = new List<Invoice>(); int count = 0; try { while (CSV.Read()) { string invoiceNo = CSV.GetField<string>("invoice#").Trim(); string customerName = CSV.GetField<string>("customer"); string salesmanName = CSV.GetField<string>("salesman"); string date = CSV.GetField<string>("date"); string remark = CSV.GetField<string>("remark"); string GSTRate = CSV.GetField<string>("SalesTaxRate"); decimal _GSTRate = 0; decimal.TryParse(GSTRate, out _GSTRate); //Get GST by Code if (!string.IsNullOrEmpty(customerName) && !string.IsNullOrEmpty(invoiceNo) && !string.IsNullOrEmpty(date)) { DateTime orderDate = DateTime.Parse(date); Customer customer = custSvr.GetByName(customerName); if (customer != null) { Salesman salesman = salesmanSvc.GetByName(salesmanName); Invoice invoice = invSvr.GetByInvoiceNumber(invoiceNo); if (invoice != null) { //update salesman only invoice.SalesmanId = salesman.Id; } else { Invoice newInvoice = new Invoice { OrderNumber = invoiceNo, OrderDate = orderDate, CustomerId = customer.Id, SalesmanId = salesman.Id, Remark = remark, GSTRate = _GSTRate }; list.Add(newInvoice); } } } } invSvr.Save(list); count = list.Count(); } catch (Exception e) { throw new Exception("Save Invoice Error", e); } return count; }
public ActionResult Create(Invoice entity, FormCollection collection) { if (ModelState.IsValid) { entity.OrderDetail = entity.OrderDetail. Where(s => !string.IsNullOrWhiteSpace(s.Description)).ToList(); if (orderSvc.IsExist(entity.OrderNumber)) { ModelState.AddModelError("OrderNumber", "Duplicate Number"); ViewBag.CustomerId = cHelper.GetCustomerDropDown(); return View(entity); } svc.Save(entity); optionsSvc.SetNextInvoiceNumber(entity.OrderNumber); return RedirectToAction("Details", new { id = entity.Id }); } ViewBag.CustomerId = cHelper.GetCustomerDropDown(); return View(entity); }
public ActionResult Edit(int id, Invoice entity, FormCollection collection) { Invoice _invoice = svc.GetById(id); if (ModelState.IsValid) { //update Order Header //Disable below as no compile time error when change fields name //UpdateModel(_entity, new[] { "OrderDate", "OrderNumber", "Remark", "CustomerId" }); _invoice.OrderDate = entity.OrderDate; _invoice.OrderNumber = entity.OrderNumber; _invoice.Remark = entity.Remark; _invoice.GSTRate = entity.GSTRate; _invoice.CustomerId = entity.CustomerId; //update Order detail foreach (var detail in _invoice.OrderDetail) { UpdateModel(detail, "OrderDetails[" + detail.Id + "]"); } //new details entity.OrderDetail = entity.OrderDetail. Where(s => !string.IsNullOrWhiteSpace(s.Description)).ToList(); if (entity.OrderDetail != null && entity.OrderDetail.Count > 0) { foreach (var item in entity.OrderDetail) { _invoice.OrderDetail.Add(item); } } try { svc.Update(_invoice); } catch (Exception e) { } return RedirectToAction("Details", new { id = _invoice.Id }); } ViewBag.CustomerId = cHelper.GetCustomerDropDown(); return View(_invoice); }
public ActionResult Create(int? id) { Invoice _item = new Invoice(); ViewBag.idmas_GST = new SelectList(gstSvc.GetAll(), "Id", "Code"); ViewBag.CustomerId = cHelper.GetCustomerDropDown(id); ViewBag.NewDetail = new OrderDetail(); _item.OrderNumber = optionsSvc.GetNextInvoiceNumber(); _item.OrderDate = DateTime.Today; return View(_item); }