public IHttpActionResult FinalizeProject(int id)
{
var result = this.data.Projects
.Find(x => x.Id == id).FirstOrDefault();
if (result == null)
{
return this.BadRequest("No project with that id is present.");
}
result.IsComplete = true;
result.TimeFinished = DateTime.Now;
var invoice = new Invoice
{
ProjectId = result.Id,
ProjectTitle = result.Name,
IssuedOn = DateTime.Now,
EmployeeEmail = result.Employee.Email,
EmployeeName = result.Employee.FirstName + " " + result.Employee.LastName,
ClientEmail = result.Client.Email,
ClientName = result.Client.FirstName + " " + result.Client.LastName,
PricePerHour = result.PricePerHour,
CategoryName = result.Category.Name,
WorkLogs = result.WorkLogs
};
var fileName = invoice.ClientEmail + "__" + invoice.IssuedOn.Ticks;
var pdf = PdfInvoiceWriter.GeneratePdf(invoice);
var link = dropbox.UploadFileEntry(pdf, string.Format("/Invoices/{0}{1}", fileName, ".pdf"));
invoice.Url = link;
result.Invoice = invoice;
result.Client.Invoices.Add(invoice);
data.Projects.Update(result);
data.SaveChanges();
var message = string.Format("Project activity: User {0} finalized {1}'s project | {2}", invoice.EmployeeEmail, invoice.ClientEmail, "/projects");
pubnubClient.Broadcast(Constants.PubnubChannelActivityFeed, message, str => { }, s => { });
return this.Ok();
}