public IHttpActionResult Get([FromUri] GridArgs args, int id, int printTypeId) { string path = String.Empty; var dataDirectory = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data"); var report = (PrintTypes)printTypeId; Stream stream = null; switch (report) { case PrintTypes.Order: path = Path.Combine(dataDirectory, Contracts.Configuration.OrderFileName); stream = printerManager.PrepareOrderPrintData(id, path, taxesManager, Manager); break; case PrintTypes.Offer: path = Path.Combine(dataDirectory, Contracts.Configuration.OfferFileName); stream = printerManager.PrepareOfferPrintData(id, path, taxesManager, Manager); break; case PrintTypes.Invoice: path = Path.Combine(dataDirectory, Contracts.Configuration.InvoiceFileName); stream = printerManager.PrepareInvoicePrintData(id, path, invoicesManager, Manager); break; case PrintTypes.ReminderMail: path = Path.Combine(dataDirectory, Contracts.Configuration.ReminderFileName); var invoices = invoicesManager.GetEntities(o => !o.PayDate.HasValue && o.ReminderCount < 3 && ((!o.LastReminderDate.HasValue && o.CreateDate.AddDays(o.PayInDays) < DateTime.Now) || (o.LastReminderDate.HasValue && o.LastReminderDate.Value.AddDays(8) < DateTime.Now) )).ToList(); foreach (var invoice in invoices) { invoice.LastReminderDate = DateTime.Now; invoice.ReminderCount++; } invoicesManager.SaveChanges(); var newIds = invoices.Select(o => o.Id).ToList(); var allInvoicesToReminder = new List <Contracts.Entities.Invoices>(invoices); allInvoicesToReminder.AddRange(invoicesManager.GetEntities(o => !o.PayDate.HasValue && o.ReminderCount != 0 && !newIds.Contains(o.Id)).ToList()); stream = printerManager.PrepareReminderPrintData(allInvoicesToReminder, path, invoicesManager, taxesManager, Manager); break; case PrintTypes.InvoiceStorno: path = Path.Combine(dataDirectory, Contracts.Configuration.InvoiceStornoFileName); stream = printerManager.PrepareInvoiceStornoPrintData(id, path, invoiceStornosManager, Manager); break; case PrintTypes.DeliveryNote: var term = termsManager.GetById(id); if (!String.IsNullOrEmpty(term.DeliveryNoteFileName)) { var directory = Path.Combine(HttpRuntime.AppDomainAppPath, "Lieferscheine"); var filePath = Path.Combine(directory, term.DeliveryNoteFileName); if (File.Exists(filePath)) { stream = File.OpenRead(filePath); } else { path = Path.Combine(dataDirectory, Contracts.Configuration.DeliveryNoteFileName); stream = printerManager.PrepareDeliveryNotePrintData(id, path, termsManager); } } else { path = Path.Combine(dataDirectory, Contracts.Configuration.DeliveryNoteFileName); stream = printerManager.PrepareDeliveryNotePrintData(id, path, termsManager); } break; default: throw new NotImplementedException(); } stream.Position = 0; var result = new StreamActionResult(stream); if (report == PrintTypes.DeliveryNote) { result.ContentType = "application/pdf"; path = "DeliveryNote.pdf"; } else if (report == PrintTypes.Invoice) { result.ContentType = "application/pdf"; path = "Invoice.pdf"; } else { result.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; } result.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = Path.GetFileName(path) }; return(result); }
public IHttpActionResult Post([FromBody] ChangeStateTermModel model) { var term = termManager.GetById(model.termId); ClientTermViewModel result = null; var user = userManager.GetByLogin(model.Login); if (user != null && user.Token == model.Token && term != null) { term.User = user; term.Status = model.status; switch ((TermStatusTypes)model.status) { case TermStatusTypes.BeginTrip: term.BeginTripFromOffice = model.BeginTripFromOffice; term.BeginTrip = DateTime.Now; break; case TermStatusTypes.EndTrip: term.EndTrip = DateTime.Now; break; case TermStatusTypes.BeginWork: term.BeginWork = DateTime.Now; break; case TermStatusTypes.CheckPositions: var termPositions = term.TermPositions.Where(o => !o.DeleteDate.HasValue).ToList(); foreach (var position in model.Positions) { var termPosition = termPositions.FirstOrDefault(o => o.Id == position.Id); termPosition.ProccessedAmount = position.ProccessedAmount; } termManager.SaveChanges(); break; case TermStatusTypes.CheckMaterials: var termMaterials = term.TermPositions.Where(o => !o.DeleteDate.HasValue).SelectMany(o => o.TermPositionMaterialRsps).ToList(); foreach (var material in model.Materials) { var termMaterial = termMaterials.FirstOrDefault(o => o.Id == material.Id); if (termMaterial != null) { termMaterial.Amount = material.Amount; } } var materialPositions = term.Positions.Where(o => !o.DeleteDate.HasValue).ToList(); foreach (var material in model.Materials) { var position = materialPositions.FirstOrDefault(o => o.Id == material.Id); if (position != null && material.Amount.HasValue) { position.Amount = material.Amount.Value; } } termManager.SaveChanges(); break; case TermStatusTypes.EndWork: term.EndWork = DateTime.Now; var dataDirectory = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data"); var path = Path.Combine(dataDirectory, Contracts.Configuration.DeliveryNoteFileName); if (!model.sendDeliveryNotePerEmail) { var signature = deliveryNoteSignaturesManager.GetEntities(o => o.TermId == model.termId).FirstOrDefault(); if (signature != null) { signature.Signature = model.signature; } else { deliveryNoteSignaturesManager.AddEntity(new DeliveryNoteSignatures() { TermId = model.termId, Signature = model.signature, }); } deliveryNoteSignaturesManager.SaveChanges(); } termMaterials = term.TermPositions.Where(o => !o.DeleteDate.HasValue). SelectMany(o => o.TermPositionMaterialRsps.Where(t => !t.DeleteDate.HasValue && t.Amount.HasValue)).ToList(); foreach (var termMaterial in termMaterials) { AutoMaterialHelper.CalculateUsedMaterial(termMaterial.Amount.Value, null, termMaterial); } termManager.SaveChanges(); var stream = printerManager.PrepareDeliveryNotePrintData(term.Id, path, termManager); var fileName = String.Format("Lieferscheine_{0}_{1}_{2}_{3}.pdf", term.Id, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var directory = Path.Combine(HttpRuntime.AppDomainAppPath, "Lieferscheine"); var filePath = Path.Combine(directory, fileName); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using (var fileStream = new FileStream(filePath, FileMode.Create)) { stream.WriteTo(fileStream); term.DeliveryNoteFileName = fileName; termManager.SaveChanges(); var attachments = new List <Attachment>(); stream.Position = 0; attachments.Add(new Attachment(stream, fileName)); if (model.sendDeliveryNotePerEmail) { SendMail(NotificationServerConfigSection.Instance.SmtpServer, NotificationServerConfigSection.Instance.SmtpServerAccountName, NotificationServerConfigSection.Instance.SmtpServerAccountPassword, term.Orders.Customers.Email, String.Format("Lieferschein {0}", DateTime.Now.ToShortDateString()), String.Format("<p>Sehr geehrte {0},</p><p>anbei der Lieferschein vom {1}.</p><p>Mit freundlichen Grüßen,</p><p>{2}</p>", term.Orders.Customers.Name, DateTime.Now.ToShortDateString(), "Firma Zierer Gebäude & Systemtechnik"), NotificationServerConfigSection.Instance.SmtpServerPort, NotificationServerConfigSection.Instance.EnableSsl, (SmtpDeliveryMethod)NotificationServerConfigSection.Instance.SmtpDeliveryMethod, true, attachments); } } break; case TermStatusTypes.BeginReturnTrip: term.BeginReturnTrip = DateTime.Now; break; case TermStatusTypes.EndReturnTrip: term.EndReturnTrip = DateTime.Now; break; } termManager.SaveChanges(); if (term != null) { result = TermViewModelHelper.ToModel(term, model.withPositions, model.withMaterials); } return(Ok(result)); } return(BadRequest()); }