public ActionResult SetArticleAvailableForPickup(string packJson)
        {
            var res = new ResultResponse();

            try
            {
                var pack = JsonConvert.DeserializeObject <ArticleDelivered>(packJson);

                var eventId = _orderItemManager.GenerateEventId(EVENT_TYPE);
                _orderItemManager.AddLogItem(pack.nodeId, "LEVERERAD", "Leveranstyp: Avhämtas i lånedisken.", eventId, false, false);
                _orderItemManager.AddLogItem(pack.nodeId, "LOG", pack.logEntry, eventId, false, false);
                _orderItemManager.SetStatus(pack.nodeId, "05:Levererad", eventId, false, false);

                // We save everything here first so that we get the new values injected into the message by the template service.
                _orderItemManager.SetPatronEmail(pack.nodeId, pack.mail.recipientEmail, eventId);

                // Overwrite the message with message from template service so that we get the new values injected.
                pack.mail.message = _templateService.ReplaceMoustaches("ArticleAvailableInInfodiskMailTemplate",
                                                                       pack.mail.message, _orderItemManager.GetOrderItem(pack.nodeId));

                _mailService.SendMail(pack.mail);
                _orderItemManager.AddLogItem(pack.nodeId, "MAIL_NOTE", "Skickat mail till " + pack.mail.recipientEmail, eventId, false, false);
                _orderItemManager.AddLogItem(pack.nodeId, "MAIL", pack.mail.message, eventId);

                res.Success = true;
                res.Message = "Lyckades leverera.";
            }
            catch (Exception e)
            {
                res.Success = false;
                res.Message = "Fel vid leveransförsök: " + e.Message;
            }

            return(Json(res, JsonRequestBehavior.AllowGet));
        }
        public ActionResult SendMail(OutgoingMailPackageModel m)
        {
            var json = new ResultResponse();

            try
            {
                // Read current values that can be affected
                var orderItem          = _orderItemManager.GetOrderItem(m.nodeId);
                var currentPatronEmail = orderItem.PatronEmail;
                var currentStatus      = orderItem.Status;

                var eventId = _orderItemManager.GenerateEventId(EVENT_TYPE);

                // Send mail to recipient
                _mailService.SendMail(new OutgoingMailModel(orderItem.OrderId, m));

                _orderItemManager.AddLogItem(m.nodeId, "MAIL_NOTE", "Skickat mail till " + m.recipientEmail, eventId, false, false);
                _orderItemManager.AddLogItem(m.nodeId, "MAIL", m.message, eventId, false, false);

                // Set PatronEmail property if it differs from recipientEmail
                if (currentPatronEmail != m.recipientEmail)
                {
                    _orderItemManager.SetPatronEmail(m.nodeId, m.recipientEmail, eventId, false, false);
                }

                // Set FollowUpDate property if it differs from current
                DateTime currentFollowUpDate = orderItem.FollowUpDate;

                if (!String.IsNullOrEmpty(m.newFollowUpDate))
                {
                    DateTime parsedNewFollowUpDate = Convert.ToDateTime(m.newFollowUpDate);
                    if (currentFollowUpDate != parsedNewFollowUpDate)
                    {
                        _orderItemManager.SetFollowUpDate(m.nodeId, parsedNewFollowUpDate, eventId, false, false);
                    }
                }

                // Set status property if it differs from newStatus and if it is not -1 (no change)
                if (orderItem.StatusId != m.newStatusId && orderItem.StatusId != -1)
                {
                    _orderItemManager.SetStatus(m.nodeId, m.newStatusId, eventId, false, false);
                }

                // Update cancellation reason if we have a value that is not -1 (no change)
                if (orderItem.CancellationReasonId != m.newCancellationReasonId && m.newCancellationReasonId != -1)
                {
                    _orderItemManager.SetCancellationReason(m.nodeId, m.newCancellationReasonId, eventId, false, false);
                }

                // Update purchased material if we have a value that is not -1 (no change)
                if (orderItem.PurchasedMaterialId != m.newPurchasedMaterialId && m.newPurchasedMaterialId != -1)
                {
                    _orderItemManager.SetPurchasedMaterial(m.nodeId, m.newPurchasedMaterialId, eventId, false, false);
                }

                _orderItemManager.SaveWithoutEventsAndWithSynchronousReindexing(m.nodeId);

                // Construct JSON response for client (ie jQuery/getJSON)
                json.Success = true;
                json.Message = "Sent mail and eventually changed some properties.";
            }
            catch (Exception e)
            {
                json.Success = false;
                json.Message = "Error: " + e.Message;
            }

            return(Json(json, JsonRequestBehavior.AllowGet));
        }