private async Task LoadUser(InvoiceAndContentApi item) { var user = await DbContext.Users.Where(u => u.Id == CurrentUserId).Select(u => new { u.FullName, u.Email }).FirstAsync(); item.FromFullName = user.FullName; item.FromEmail = user.Email; }
private async Task <Invoice> CreateCreditNote(Translate translate, InvoiceAndContentApi item, long invoiceId) { var invoice = await DbContext.Invoices.Where(i => i.PartitionId == CurrentPartitionId && i.Id == invoiceId).FirstOrDefaultAsync(); if (invoice != null) { var creditNote = Invoice.CreateNew(CurrentPartitionId, CurrentUserId, await NextInvoiceNumber()); creditNote.CreditNote = true; creditNote.CustomerShort = invoice.CustomerShort; creditNote.SubTotalPrice = invoice.SubTotalPrice; await MapDeltaValuesAndCleanWhiteSpace(item, creditNote); creditNote.InvoiceDate = DateTime.Now.Date; var creditNoteInvoiceData = await invoice.InvoiceData.FromJson <InvoiceDataApi>(); creditNoteInvoiceData.InvoiceTitle = translate.Get("INVOICE.CREDIT_NOTE"); creditNoteInvoiceData.PaymentDetails = null; creditNoteInvoiceData.InvoicePaymentTerms = null; creditNote.InvoiceData = await creditNoteInvoiceData.ToJson(); DbContext.Invoices.Add(creditNote); await DbContext.SaveChangesAsync(); return(creditNote); } return(invoice); }
private async Task <Invoice> CreateAndSendInvoice(InvoiceAndContentApi item) { var invoice = Invoice.CreateNew(CurrentPartitionId, CurrentUserId, await NextInvoiceNumber()); invoice.CustomerShort = item.InvoiceCustomer?.CleanWhiteSpace()?.FirstLine(); invoice.SubTotalPrice = item.Invoice.SubTotalPrice; await MapDeltaValuesAndCleanWhiteSpace(item, invoice); invoice.InvoiceData = await new InvoiceDataApi { ShowGroupColl = item.showGroupColl, InvoiceTitle = item.InvoiceTitle?.CleanWhiteSpace(), InvoiceCustomer = item.InvoiceCustomer?.CleanWhiteSpace(), VatNumber = item.Vat ? item.VatNumber?.CleanWhiteSpace() : null, PaymentDetails = item.PaymentDetails?.CleanWhiteSpace(), InvoicePaymentTerms = item.InvoicePaymentTerms?.CleanWhiteSpace(), InvoiceReference = item.InvoiceReference?.CleanWhiteSpace(), InvoiceText = item.InvoiceText?.CleanWhiteSpace(), Tax = item.Tax, Vat = item.Vat, TaxPercentage = item.Tax ? item.TaxPercentage : null, VatPercentage = item.Vat ? item.VatPercentage : null, TaxPrice = item.Tax ? item.TaxPrice : null, VatPrice = item.Vat ? item.VatPrice : null, TotalPrice = item.TotalPrice, Invoice = item.Invoice, }.ToJson(); DbContext.Invoices.Add(invoice); await DbContext.SaveChangesAsync(); return(invoice); }
private async Task <Invoice> ResendInvoice(InvoiceAndContentApi item, long invoiceId) { var invoice = await DbContext.Invoices.Where(i => i.PartitionId == CurrentPartitionId && i.Id == invoiceId).FirstOrDefaultAsync(); if (invoice != null) { invoice.Status = InvoiceStatus.Resending; invoice.UpdateTimestamp = DateTime.UtcNow; invoice.ToEmail = item.ToEmail; invoice.FromFullName = item.FromFullName; invoice.FromEmail = item.FromEmail; invoice.EmailSubject = item.EmailSubject; invoice.EmailBody = item.EmailBody; await DbContext.SaveChangesAsync(); } return(invoice); }
public async Task <IActionResult> Post([FromBody] InvoiceAndContentApi item) { if (!ModelState.IsValidUpdated()) { return(new BadRequestObjectResult(ModelState)); } var translate = new Translate(); await CleanWhiteSpace(item); if (!item.ToEmail.EmailsIsValid()) { ModelState.AddModelError("item.ToEmail", translate.Get("INVOICE.EMAIL_COMMA_ERROR")); return(new BadRequestObjectResult(ModelState)); } await LoadUser(item); Invoice invoice = null; if (item.RelatedId.HasValue) { if (item.CreditNote.HasValue && item.CreditNote.Value) { invoice = await CreateCreditNote(translate, item, item.RelatedId.Value); } else { invoice = await ResendInvoice(item, item.RelatedId.Value); } if (invoice == null) { return(new NotFoundResult()); } } else { var maxInvoices = await DbContext.Partitions.Where(p => p.Id == CurrentPartitionId).Select(p => p.MaxInvoicesPerMonth).FirstAsync(); // Max Invoices count if (await DbContext.Invoices.Where(i => i.PartitionId == CurrentPartitionId && i.InvoiceDate >= item.InvoiceDate.FirstDayOfMonth() && i.InvoiceDate < item.InvoiceDate.FirstDayOfNextMonth()).CountAsync() >= maxInvoices) { ModelState.AddModelError("", translate.Get("INVOICE.MAXIMUM_INVOICES_ERROR")); return(new BadRequestObjectResult(ModelState)); } invoice = await CreateAndSendInvoice(item); } QueueInvoice(invoice); telemetryClient.TrackEvent("InvoiceQueued", new Dictionary <string, string> { { "UserId", Convert.ToString(CurrentUserId) }, { "PartitionId", CurrentPartitionIdAsString } }); return(Json(new InvoiceItemApi { Id = invoice.Id, Status = invoice.Status, Number = invoice.Number, CustomerShort = invoice.CustomerShort, InvoiceDate = invoice.InvoiceDate, SubTotalPrice = invoice.SubTotalPrice, ToEmail = invoice.ToEmail, CreditNote = invoice.CreditNote ? true as bool? : null })); }