/// <summary> /// Add Invoice based on given InvoiceViewModel. /// </summary> /// <param name="model">InvoiceViewModel</param> /// <param name="createdBy">ApplicationUser that creates Invoice</param> /// <returns>InvoiceReturnResult with id, invoice number and status</returns> public InvoiceReturnResult Add(InvoiceViewModel model, ApplicationUser createdBy) { if (model == null || createdBy == null) { throw new ArgumentNullException(nameof(model), "Nieprawidłowe parametry"); } var now = DateHelper.GetCurrentDatetime(); var documentNumber = DocumentNumberingManager.GetNextDocumentNumber(DocumentType.Invoice, now, createdBy); var invoice = Context.Invoices.SingleOrDefault(x => x.InvoiceNumber == documentNumber); if (invoice != null) { throw new ArgumentException($"Faktura o podanym numerze {documentNumber} już istnieje. Spróbuj ponownie a jeżeli problem będzie się powtarzał przestaw ręcznie numerację", nameof(invoice)); } var newInvoice = new Invoice() { Status = Status.Opened, CreatedById = createdBy.Id, CreatedDate = now, InvoiceNumber = documentNumber, PaymentMethod = model.PaymentMethod, PaymentDueDate = model.PaymentDueDate, IssueDate = model.IssueDate, ReceiveDate = model.ReceiveDate, CustomerId = model.CustomerId, Currency = model.Currency, BudgetId = model.BudgetId }; Context.Invoices.Add(newInvoice); var result = Context.SaveChanges(); if (result == 0) { throw new Exception("Nie zapisano żadnych danych."); } return(new InvoiceReturnResult() { Id = newInvoice.Id, InvoiceNumber = newInvoice.InvoiceNumber, Status = newInvoice.Status.ToString() }); }
/// <summary> /// Add Budget from given BudgetViewModel. /// </summary> /// <param name="model">BudgetViewModel</param> /// <param name="createdBy">ApplicationUser that creates Budget</param> /// <returns>BudgetReturnResult with id, budget number and status</returns> public BudgetReturnResult Add(BudgetViewModel model, ApplicationUser createdBy) { if (model == null || createdBy == null) { throw new ArgumentNullException(nameof(model), "Nieprawidłowe parametry"); } var now = DateHelper.GetCurrentDatetime(); var documentNumber = DocumentNumberingManager.GetNextDocumentNumber(DocumentType.Budget, now, createdBy); var budget = Context.Budgets.SingleOrDefault(x => x.BudgetNumber == documentNumber); if (budget != null) { throw new ArgumentException($"Budżet o podanym numerze {documentNumber} już istnieje. Spróbuj ponownie a jeżeli problem będzie się powtarzał przestaw ręcznie numerację", nameof(budget)); } var newBudget = new Budget() { Status = Status.Opened, BudgetNumber = documentNumber, Description = model.Description, CommitedAmount = model.CommitedAmount, InvoicedAmount = 0M, CreatedById = createdBy.Id, OwnerId = createdBy.Id, CreatedDate = now }; Context.Budgets.Add(newBudget); var result = Context.SaveChanges(); if (result == 0) { throw new Exception("Nie zapisano żadnych danych."); } return(new BudgetReturnResult() { Id = newBudget.Id, BudgetNumber = newBudget.BudgetNumber, Status = newBudget.Status.ToString() }); }