public async Task <Invoice> AddAsync(Invoice newInvoice, CancellationToken ct = default(CancellationToken)) { var invoice = new DataModels.Invoice { CustomerId = newInvoice.CustomerId, InvoiceDate = newInvoice.InvoiceDate, BillingAddress = newInvoice.BillingAddress, BillingCity = newInvoice.BillingCity, BillingState = newInvoice.BillingState, BillingCountry = newInvoice.BillingCountry, BillingPostalCode = newInvoice.BillingPostalCode, Total = newInvoice.Total }; _context.Invoice.Add(invoice); await _context.SaveChangesAsync(ct); newInvoice.InvoiceId = invoice.InvoiceId; return(newInvoice); }
public static DataModels.Invoice CreateNewInvoice(List <DataModels.Visit> visits, out string message) { if (visits.Any(x => x.Invoiced)) { message = "Una delle visite selezionate è già stata fatturata!"; return(null); } if (visits.Any(x => x.Payed) && !visits.All(x => x.Payed)) { message = "Non è possibile fatturare un insieme di visite dove solo alcune sono state già pagate"; return(null); } var invoiceTile = ""; Therapist therapist = null; using (var db = new Db.PhisioDB()) { var invoices = db.Invoices.Where(x => x.Date >= new NpgsqlTypes.NpgsqlDate(DateTime.Now.Year, 1, 1)).ToList(); invoiceTile = $"{(invoices.Count + 1).ToString("0000")}/{DateTime.Now.Year}"; therapist = db.Therapists.FirstOrDefault(); } var newInvoice = new DataModels.Invoice { Date = new NpgsqlTypes.NpgsqlDate(DateTime.Now), Discount = 0, Payed = false, Text = "", Visitsinvoiceidfkeys = visits, Title = invoiceTile, TaxStamp = false, TherapistId = therapist.Id, Contanti = false }; message = ""; return(newInvoice); }
private void PrintInvoice(DataModels.Invoice invoice) { if (invoice.Id == null || invoice.Id == Guid.Empty) { invoice.TaxStamp = checkBox1.Checked; if (double.TryParse(textBoxDiscount.Text, out double disc)) { invoice.Discount = disc; } MessageBox.Show("Si sta visualizzando un anteprima, ricordarsi di salvare la fattura!", "Salvataggio", MessageBoxButtons.OK, MessageBoxIcon.Information); } DataModels.Therapist therapist = null; using (var db = new Db.PhisioDB()) { therapist = db.Therapists.FirstOrDefault(); } if (therapist == null) { MessageBox.Show("Qualcosa è andato storto non trovo i parametri generali", "Stampa", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(therapist.InvoicesFolder)) { MessageBox.Show("Prima di proseguire bisogna impostare la cartella di salvataggio delle fatture", "Salvataggio", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string html = ""; var groupVisits = checkBoxGroup.Checked; if (_customer.Language == "german") { html = File.ReadAllText("Template/templateInvoice_de.html"); } else { html = File.ReadAllText("Template/templateInvoice_it.html"); } html = Helper.Helper.ReplaceInvoicePlaceHolder(html, _customer, invoice, groupVisits); var basePath = therapist.InvoicesFolder; var date = $"{invoice.Date.Year}{invoice.Date.Month}"; basePath = Path.Combine(basePath, date); Directory.CreateDirectory(basePath); var pdfPath = $@"{basePath}\{invoice.Title.Replace(@"/", "_")}_{_customer.FullName.Replace(" ", "_")}.pdf"; var htmlPath = $@"{basePath}\{invoice.Title.Replace("/", "_")}_{_customer.FullName.Replace(" ", "_")}.html"; pdfPath = pdfPath.Replace("'", "_"); htmlPath = htmlPath.Replace("'", "_"); try { File.WriteAllText(htmlPath, html); Helper.PdfManager.CreatePdf(pdfPath, htmlPath); } catch (Exception ee) { MessageBox.Show("Il file è già aperto, chiuderlo", "Stampa", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Helper.DriveManagement.InsertFilePdf(pdfPath, new List<string> { "Invoice", date }); File.Delete(htmlPath); if (File.Exists(pdfPath)) { System.Diagnostics.Process.Start(pdfPath); } else { MessageBox.Show("Il pdf non è stato creato!", "Stampa", MessageBoxButtons.OK, MessageBoxIcon.Error); } }