protected override ActionResult Display(GridCommand command, string id, bool isNew)
        {
            int totalRows = 0;
            IEnumerable <PaymentReceiptDetails> resultList;
            PaymentReceiptDetailsLibrary        lib = new PaymentReceiptDetailsLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
            ScaleLibrary libScale = new ScaleLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());

            if (isNew || id == "0")
            {
                resultList = lib.GetAllByPaging(TempEntityList, out totalRows,
                                                command.Page,
                                                command.PageSize,
                                                command.SortDescriptors.Count == 0 ? "" : command.SortDescriptors[0].Member,
                                                command.SortDescriptors.Count == 0 ? "" : command.SortDescriptors[0].SortDirection == System.ComponentModel.ListSortDirection.Descending ? "Desc" : "Asc",
                                                IncludePredicates,
                                                (command.FilterDescriptors.Count == 0 ? null : command.FilterDescriptors)
                                                );
            }
            else
            {
                resultList = new PaymentReceiptDetailsLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString())
                             .GetAllByPagingByParentID(out totalRows,
                                                       int.Parse(id.ToString()),
                                                       command.Page,
                                                       command.PageSize == 0 ? 20 : command.PageSize,
                                                       command.SortDescriptors.Count == 0 ? "" : command.SortDescriptors[0].Member,
                                                       command.SortDescriptors.Count == 0 ? "" : command.SortDescriptors[0].SortDirection == System.ComponentModel.ListSortDirection.Descending ? "Desc" : "Asc",
                                                       new string[] { "PaymentReceipt", "Settlement.Scale.Party_ID", "Settlement.Scale.Purchase_Order", "ExpenseRequest.Paid_Party_To", "ExpenseRequest.Scale_Ref", "ExpenseRequest.Dispatcher_Request_Ref.Booking_Ref_No", "ExpenseRequest.Dispatcher_Request_Ref.Container" },
                                                       (command.FilterDescriptors.Count == 0 ? null : command.FilterDescriptors)
                                                       );
                foreach (var item in resultList)
                {
                    if (item.ExpenseRequest != null && item.ExpenseRequest.Dispatcher_Request_Ref != null && item.ExpenseRequest.Dispatcher_Request_Ref.Container != null)
                    {
                        item.ExpenseRequest.Scale_Ref = libScale.GetScalesByContainerId(item.ExpenseRequest.Dispatcher_Request_Ref.Container.ID);
                    }
                }
            }

            return(View(new GridModel {
                Data = resultList, Total = totalRows
            }));
        }
        public ActionResult SendEmail(string id)
        {
            string      destinationFilePath = string.Empty;
            FilelHelper fileHelper          = new FilelHelper();

            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    throw new Exception("Email send failed due to Container# not found.");
                }

                ContainerLibrary containerLib = new ContainerLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                Container        container    = containerLib.GetByID(id, new string[] { "Booking.Sales_Order_No.Party" });

                if (container == null || container.Booking == null || container.Booking.Sales_Order_No == null || container.Booking.Sales_Order_No.Party == null)
                {
                    throw new Exception("Email send failed.");
                }

                IEnumerable <Contact> contacts = Helpers.ContactHelper.GetEmailContactsByPartyId(container.Booking.Sales_Order_No.Party.ID);
                if (contacts.Count() <= 0)
                {
                    throw new Exception("There is no email contact exists.");
                }

                NotificationDefinition notDef = new NotificationDefinition();
                notDef.ToRecipients = new System.Net.Mail.MailAddressCollection();
                foreach (var item in contacts)
                {
                    notDef.ToRecipients.Add(new System.Net.Mail.MailAddress(item.Email, item.ListText));
                }

                EmployeeHelper employeeHelper = new EmployeeHelper();
                Employee       employee       = employeeHelper.GetEmployeeByUsername(System.Web.HttpContext.Current.User.Identity.Name);
                if (employee == null || string.IsNullOrEmpty(employee.Email) || string.IsNullOrEmpty(employee.Email_Password))
                {
                    throw new Exception("Sender email and password is required.");
                }

                ScaleLibrary scaleLib = new ScaleLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                Scale        scale    = scaleLib.GetScalesByContainerId(container.ID);

                ScaleAttachmentsLibrary        scaleAttachLib   = new ScaleAttachmentsLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                IEnumerable <ScaleAttachments> scaleAttachments = scaleAttachLib.GetAllByParentID(scale.ID);
                if (scaleAttachments == null || scaleAttachments.Count() <= 0)
                {
                    throw new Exception("There is no attachment found.");
                }

                destinationFilePath = Path.Combine(Path.GetTempPath(), container.ID.ToString());
                fileHelper.CreateDirectory(destinationFilePath);
                fileHelper.DeleteFiles(destinationFilePath);
                string imageFilePath = Path.Combine(destinationFilePath, "Images");
                fileHelper.CreateDirectory(imageFilePath);

                string zipFilePath = Path.Combine(destinationFilePath, container.ID.ToString() + ".zip");

                foreach (var item in scaleAttachments)
                {
                    string sourceFilePath    = fileHelper.GetFilePathByFileRefId(item.Document_RefId.ToString());
                    string imageFileFullPath = Path.Combine(imageFilePath, item.Document_Name);
                    System.IO.File.Copy(sourceFilePath, imageFileFullPath);
                }
                new smART.Common.FilelHelper().CreateZip(imageFilePath, zipFilePath);

                if (!fileHelper.FileExits(zipFilePath))
                {
                    throw new Exception("There is no attachment found.");
                }

                string xslPath     = Path.Combine(ConfigurationHelper.GetsmARTXslPath(), "ContainerEmailBody.xslt");
                string smtpAddress = ConfigurationHelper.GetsmARTSMTPServer();

                notDef.Attachments = new List <System.Net.Mail.Attachment>();
                notDef.Attachments.Add(new System.Net.Mail.Attachment(zipFilePath));
                notDef.DeliveryType            = EnumNotificationDeliveryType.Email;
                notDef.FormatType              = EnumFormatType.HTML;
                notDef.Sender                  = new System.Net.Mail.MailAddress(employee.Email, employee.Emp_Name);
                notDef.SMTPServer              = smtpAddress;
                notDef.SMTPServerCredentialID  = employee.Email;
                notDef.SMTPServerCredentialPwd = employee.Email_Password;
                notDef.Subject                 = "Booking#/Container#: " + container.Booking.Booking_Ref_No + "/" + container.Container_No;
                NotificationHelper.StartNotificationWF(id, container.Booking.Sales_Order_No.Party.Party_Name, PartyHelper.GetOrganizationName(), employee.Emp_Name, notDef, xslPath, NotificationWFCompleted, container.Booking.Booking_Ref_No, container.Booking.Sales_Order_No.ID.ToString());
                //return Display(command, container);
                if (_sendMail == true)
                {
                    return(Json(new { Sucess = "Email send sucessfully." }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { Sucess = "Email send failed." }, JsonRequestBehavior.AllowGet));
                }
                //return Json(new { Sucess = "Email send sucessfully." }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                //var script = @"ShowAlertMessage(""Send mail failed"");";
                //return JavaScript(script);
                return(Json(new { Sucess = ex.Message }, JsonRequestBehavior.AllowGet));
            }
            finally
            {
                //fileHelper.DeleteDirectory(destinationFilePath);
            }
        }