public async Task <IActionResult> CreateBulkPayments(BulkPaymentViewModel model) { if (!ModelState.IsValid) { return(BadRequest(model)); } var invoiceList = _mapper.Map <List <PaymentDto> >(model.Payments); var result = await _businessManager.CreatePayment(invoiceList); if (result == null || result.Count == 0) { return(BadRequest(model)); } return(Ok(result)); }
public async Task <ActionResult> Create(PaymentViewModel model) { try { if (ModelState.IsValid) { var item = await _businessManager.CreatePayment(_mapper.Map <PaymentDto>(model)); if (item == null) { return(BadRequest()); } return(RedirectToAction(nameof(Index))); } } catch (Exception er) { _logger.LogError(er, er.Message); } var customers = await _businessManager.GetCustomers(); ViewBag.Customers = customers.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList(); return(View(model)); }
public async Task <IActionResult> CreateUploadInvoices(InvoiceImportCsvViewModel model) { try { if (!ModelState.IsValid) { throw new Exception("Form is not valid!"); } var cacheModel = _memoryCache.Get <InvoiceImportCsvViewModel>("_InvoiceUploadCache"); model.Rows = cacheModel?.Rows; var invoiceList = new List <InvoiceViewModel>(); for (var i = 0; i < model.Rows?.Count(); i++) { var row = model.Rows[i]; var rnd = new Random(); var invoiceModel = new InvoiceViewModel() { No = $"{DateTime.Now.ToString("mmyy")}_{rnd.Next(100000, 999999)}" }; for (var j = 0; j < row.Count(); j++) { var column = model.Columns[j]; if (column != null && !string.IsNullOrEmpty(column.Name) && row[j].Index == column.Index) { if (column.Name == "CustomerNo") { var customer = await _customerBusinessManager.GetCustomer(row[j].Value, model.CompanyId); if (customer != null) { var propertyCustomerId = invoiceModel.GetType().GetProperty("CustomerId"); propertyCustomerId.SetValue(invoiceModel, customer.Id); } continue; } var property = invoiceModel.GetType().GetProperty(column.Name); if (property != null && property.CanWrite) { if (property.PropertyType == typeof(double)) { if (double.TryParse(row[j].Value, out double doubleVal)) { property.SetValue(invoiceModel, doubleVal); } } else if (property.PropertyType == typeof(decimal) || property.PropertyType == typeof(decimal?)) { if (decimal.TryParse(row[j].Value, out decimal decimalVal)) { property.SetValue(invoiceModel, decimalVal); } } else if (property.PropertyType == typeof(int)) { if (int.TryParse(row[j].Value, out int intVal)) { property.SetValue(invoiceModel, intVal); } } else if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?)) { if (DateTime.TryParse(row[j].Value, out DateTime dateVal)) { property.SetValue(invoiceModel, dateVal); } } else { property.SetValue(invoiceModel, row[j].Value); } } } } if (TryValidateModel(invoiceModel)) { var invoice = await _businessManager.CreateInvoice(_mapper.Map <InvoiceDto>(invoiceModel)); if (invoice != null) { var paColumn = model.Columns.Where(x => x.Name == "PaymentAmount").FirstOrDefault(); var pdColumn = model.Columns.Where(x => x.Name == "PaymentDate").FirstOrDefault(); var paValue = row[paColumn.Index].Value; var pdValue = row[pdColumn.Index].Value; if (decimal.TryParse(paValue, out decimal paymentValue) && DateTime.TryParse(pdValue, out DateTime paymentDate)) { var paymentModel = new PaymentViewModel() { No = $"{DateTime.Now.ToString("mmyy")}_{rnd.Next(100000, 999999)}", Amount = paymentValue, Date = paymentDate, InvoiceId = invoice.Id }; if (TryValidateModel(paymentModel)) { var payment = await _businessManager.CreatePayment(_mapper.Map <PaymentDto>(paymentModel)); } } invoiceList.Add(_mapper.Map <InvoiceViewModel>(invoice)); } } } if (invoiceList.Count == 0) { throw new Exception("No records have been created! Please, fill the required fields!"); } return(Ok(new { Message = $"{invoiceList.Count}/{model.Rows?.Count} invoices are created!" })); } catch (Exception er) { return(BadRequest(er.Message ?? er.StackTrace)); } }