public static int PerformInvoiceRun(string lastInvoiceDate, string invoiceDate) { DateTime startDate; var startRunTime = DateTime.Now; if (DateTime.TryParse(lastInvoiceDate, out startDate)) { startDate = startDate.AddDays(1); } else { throw new Exception("Invalid Last Invoiced Date"); } DateTime endDate; if (!DateTime.TryParse(invoiceDate, out endDate)) { throw new Exception("Invalid Invoice Date"); } if (endDate < startDate) { throw new Exception("Invoice Run Date must be after Last Invoice Date"); } if (endDate > DateTime.Today.AddDays(1)) { throw new Exception("Invoice Date cannot be in the future"); } try { var service = new InvoicingService(); var invoiceRunNumber = service.InvoiceRun(endDate); // Pause for at least 3 seconds. var runDuration = DateTime.Now - startRunTime; if (runDuration.TotalMilliseconds < 3000) { var sleepTime = 3000 - (int)runDuration.TotalMilliseconds; System.Threading.Thread.Sleep(sleepTime); } return invoiceRunNumber; } catch (InvoiceRunFrequencyException) { throw; } catch (Exception) { throw new Exception("Internal Invoicing Error. Contact System Support."); } }
public AccountPaymentResponse MakePayment(int accountId, decimal amount) { var response = new AccountPaymentResponse(); var account = _accountRepository.GetAccount(accountId); if (account != null) { response.Account = account; if (!account.IsInvoiceRoot) { response.IsSuccessful = false; response.Message = "Payments can only be made against Invoice Root accounts"; return response; } using (var ts = new TransactionScope()) { account.MakePayment(amount); // Get Last Invoice for this account - this will be inefficient!! var invoices = _invoiceRepository.GetAllInvoiceForAccount(account); var failedPaymentInvoice = invoices.Where(i => i.HasFailedPayment).OrderByDescending(i => i.InvoiceDate).FirstOrDefault(); if (failedPaymentInvoice != null) { failedPaymentInvoice.MakeCatchupPayment(amount); var invoicingService = new InvoicingService(); invoicingService.SaveInvoiceHardcopy(failedPaymentInvoice); _invoiceRepository.UpdateInvoice(failedPaymentInvoice); } _accountRepository.UpdateAccount(account); response.IsSuccessful = true; ts.Complete(); } } else { response.IsSuccessful = false; response.Message = "Could not find Account"; } return response; }