internal static string BuildHtml(WorkOrder order, string handtekening)
    {
      var builder = new StringBuilder();

      builder.AppendLine("<html>");
      builder.AppendLine("<head>");
      builder.AppendLine("<title></title>");
      builder.AppendLine("</head>");
      builder.AppendLine("<body>");
      builder.AppendLine("<table align=\"left\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"100%\" width=\"700\">");
      builder.AppendLine("<tbody>");
      builder.AppendLine("<tr><td valign=\"top\" ><table border=\"0\"><tbody><tr>");
      builder.AppendLine("<td colspan=\"2\" style=\"font -weight: normal;\" >");
      builder.AppendLine("Hierbij ontvangt u een digitale werkbon van Overeem Telecom.De werkbon heeft betrekking op de door Overeem Telecom aan u, of aan uw relaties geleverde diensten en / of services.<br/>");
      builder.AppendLine("</td></tr>");
      builder.AppendLine("<tr><td>Opdrachtgever:</td><td><b>");
      builder.AppendLine(order.Bedrijf);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td>Referentie:</td><td><b>");
      builder.AppendLine(order.Referentie);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td>Adres:</td><td><b>");
      builder.AppendLine(order.Adres);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td>&nbsp;</td><td><b>");
      builder.AppendLine(order.Postcode);
      builder.AppendLine(" ");
      builder.AppendLine(order.Plaats);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td>Telefoon:</td><td><b>");
      builder.AppendLine(order.Telefoonnummer);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td>Contactpersoon:</td><td><b>");
      builder.AppendLine(order.Contactpersoon);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td>Email adres:</td><td><b>");
      builder.AppendLine(order.Email);
      builder.AppendLine("</b></td></tr>");

      builder.AppendLine("<tr><td colspan=\"2\"><Hr/></td></tr>");
      builder.AppendLine("<tr><td>Opdracht:</td><td><b>");
      builder.AppendLine(order.Opdracht);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\"><Hr/></td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\">Aankomst en - vertrektijd:</td><td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\">");
      builder.AppendLine(BuildTijden(order.Momenten));
      builder.AppendLine("</td><td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\"><Hr/></td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\">Gebruikte materialen:</td><td></tr><tr><td colspan=\"2\">");
      builder.AppendLine(BuildMaterialen(order.Onderdelen));
      builder.AppendLine("</td><td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\"><Hr/></td></tr>");
      builder.AppendLine("<tr><td>Oplossing:</td><td><b>");
      builder.AppendLine(order.Oplossing);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\"><Hr/></td></tr>");
      builder.AppendLine("<tr><td>Naam:</td><td><b>");
      builder.AppendLine(order.NaamHandtekening);
      builder.AppendLine("</b></td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\">Handtekening voor akkoord:</td><td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\"><img style=\"width: 300px;\" src=\"data:image/png;base64,");
      builder.AppendLine(handtekening);
      builder.AppendLine("\"/></td><td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\"><Hr/></td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"color: rgb(255, 128, 0);\">Belangrijke mededeling:</td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"font-weight: normal;\">");
      builder.AppendLine("U dient deze digitale werkbon als originele werkbon te beschouwen en deze conform de reglementen van de Belastingdienst zelf uit te printen en toe te voegen aan uw eigen administratie. Mocht u hier niet toe in staat zijn of liever de originele werkbon per post te ontvangen neemt u dan even contact met ons op via het e-mailadres: <a href=\"mailto: [email protected]\">[email protected]</a>.</td> ");
      builder.AppendLine("</tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"color: rgb(255, 128, 0);\">Reclamaties:</td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"font-weight: normal;\">");
      builder.AppendLine("Indien u fouten constateert in de werkbon, dan dient u deze(binnen 5 dagen) bij Overeem Telecom te reclameren.Reclamaties kunt u melden via het e-mailadres: <a href=\"mailto: [email protected]\">[email protected] </a> . De verichtte werkzaamheden overeenkomstig de algemene voorwaarden van Overeem Telecom blijven bestaan.Eventuele verschillen zullen op een nieuwe werkbon worden gecorrigeerd.</td>");
      builder.AppendLine("</tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"color: rgb(255, 128, 0);\">Vragen?</td></tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"font-weight: normal;\">");
      builder.AppendLine("Indien u vragen heeft over deze digitale werkbon kunt u contact opnemen met Overeem Telecom, telefoon:");
      builder.AppendLine("088 - 6837336 of stuur een e-mail naar <a href=\"mailto: [email protected]\">[email protected]</a></td>");
      builder.AppendLine("</tr>");
      builder.AppendLine("<tr><td colspan=\"2\" style=\"line-height: 11px; font-family: arial,helvetica,sans-serif; color: rgb(170, 170, 170); font-size: 9px; font-weight: normal; text-decoration: none;\"><br />");
      builder.AppendLine("De informatie verzonden met dit e-mailbericht is uitsluitend bestemd voor de geadresseerde. Gebruik van deze informatie door anderen dan de geadresseerde is verboden.Openbaarmaking, vermenigvuldiging, verspreiding en/ of verstrekking van deze informatie aan derden is niet toegestaan.< br />< br />");
      builder.AppendLine("The information contained in this communication is confidential and may be legally privileged.It is intended solely for the use of the individual or entity to whom it is addressed and others authorized to receive it.If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or taking any action in reliance of the contents of this information is strictly prohibited and may be unlawful.Overeem Telecom is neither liable for the proper and & nbsp; complete transmission of the information contained in this communication nor for any delay in its receipt.< br />");
      builder.AppendLine("&nbsp;</td> ");
      builder.AppendLine("</tr>");
      builder.AppendLine("<tr><td align=\"center\" colspan=\"2\" style=\"font-style: italic; font-family: helvetica; color: rgb(255, 128, 0); font-size: 10px;\" valign=\"top\">");
      builder.AppendLine("Overeem Telecom | Eemweg 31 - 24 | &nbsp;");
      builder.AppendLine("3755 LC & nbsp;");
      builder.AppendLine("Eemnes | T. 088 - 6837336 | F. 088 - 6837337 | E. <a href=\"mailto:[email protected]\">[email protected]</a></td>");
      builder.AppendLine("</tr>");
      builder.AppendLine("</tbody>");
      builder.AppendLine("</table>");
      builder.AppendLine("</td>");
      builder.AppendLine("</tr>");
      builder.AppendLine("</tbody>");
      builder.AppendLine("</table>");
      builder.AppendLine("<br />");
      builder.AppendLine("</body>");
      builder.AppendLine("</html>");

      return builder.ToString();
    }
 public async Task<ActionResult> SaveIpad(string from_name, string from_email, WorkOrder order)
 {
   return await SaveInternal(from_name, from_email, order);
 }
    private async Task<ActionResult> SaveInternal(string from_name, string from_email, WorkOrder order)
    {
      var cookies = new CookieManager(HttpContext);
      if(cookies.GetUser() == null)
      {
        return RedirectToAction("Index", "Index", new { goed = true });
      }

      PerfectView.PerfectViewSoapClient client = new PerfectView.PerfectViewSoapClient();
      try
      {
        var response = await client.ActivityCreateAsync(Settings.Credentials, new PerfectView.PvActivitySettingsData
        {
          WorkflowId = Settings.SupportWorkflowId,
          WorkflowStepId = Settings.NieuwWorkFlowStepId,
          ParentRelationshipId = Guid.Parse(order.BedrijfsId),
          ChildRelationshipId = !string.IsNullOrEmpty(order.ContactpersoonId) ? Guid.Parse(order.ContactpersoonId) : (Guid?)null,

        }, new PerfectView.PvActivityFollowUpData
        {
          Start = ParseDateTime(order.Momenten.FirstOrDefault(), m => m.Datum, m => m.Begintijd),
          End = ParseDateTime(order.Momenten.FirstOrDefault(), m => m.Datum, m => m.Eindtijd)
        }, new PerfectView.PvFieldValueData[] {
          ActivityRequestBuilder.Build(ActivityFields.Onderwerp, GetOnderwerp(from_name, order.Opdracht) ),
          ActivityRequestBuilder.Build(ActivityFields.Omschrijving, order.Opdracht),
          ActivityRequestBuilder.Build(ActivityFields.Oplossing, BuildOplossing(order)),
        }, null);

        if(response.Body.ActivityCreateResult.Activity == null)
        {
          throw new Exception(response.Body.ActivityCreateResult.ErrorInformation);
        }
        var activityId = response.Body.ActivityCreateResult.Activity.Id;
        await client.ActivityFlowAsync(Settings.Credentials, activityId, Guid.Empty, Guid.Empty, Guid.Parse(order.Wachtrij));

        var sigToImg = new SignatureToImage()
        {
          BackgroundColor = Color.White,
          PenColor = Color.FromArgb(20, 83, 148),
          CanvasWidth = 290,
          CanvasHeight = 330,
          PenWidth = 2,
          FontSize = 24,
          FontName = "Journal",
        };

        using(var signatureImage = sigToImg.SigJsonToImage(order.output))
        using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
        {
          signatureImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
          byte[] imageBytes = stream.ToArray();
          string imageBase64String = Convert.ToBase64String(imageBytes);
          await client.ActivityAddAttachmentAsync(Settings.Credentials, activityId, "handtekening.png", imageBase64String);

          var htmlBytes = System.Text.Encoding.UTF8.GetBytes(ActivityRequestBuilder.BuildHtml(order, imageBase64String));
          string htmlBase64String = Convert.ToBase64String(htmlBytes);
          await client.ActivityAddAttachmentAsync(Settings.Credentials, activityId, "werkbon.html", htmlBase64String);
        }

        return RedirectToAction("Index", "Index", new { goed = true });
      }
      catch(Exception exc)
      {
        return RedirectToAction("Index", "Index", new { goed = false, error = exc.Message });
      }
    }
 private string BuildOplossing(WorkOrder order)
 {
   var builder = new StringBuilder();
   if(order.Momenten.Any())
   {
     foreach(var datum in order.Momenten.Where(m => !string.IsNullOrEmpty(m.Datum)).Select(m => string.Format("{0} {1}-{2}", m.Datum, m.Begintijd, m.Eindtijd)))
     {
       builder.AppendLine(datum);
     }
     builder.AppendLine();
   }
   if(order.Onderdelen.Any())
   {
     foreach(var datum in order.Onderdelen.Select(o => string.Format("{0} {1}", o.Aantal, o.Omschrijving)))
     {
       builder.AppendLine(datum);
     }
     builder.AppendLine();
   }
   builder.AppendLine(order.Oplossing);
   return builder.ToString();
 }