private string GenerateHtmlForEmail(SpotterViewModel model, string SpotterRefNumber, string UserName)
        {
            string        html        = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/EmailTemplate/Spotter_Email.html"));
            StringBuilder tablerows   = new StringBuilder();
            int           count       = 0;
            decimal       sumOfAmount = (decimal)0.00;

            foreach (var item in model.SpotterContractDetails)
            {
                if (item.Valid == "Y")
                {
                    count++;
                    tablerows   = tablerows.Append("<TR>");
                    tablerows   = tablerows.Append("<TD ALIGN=CENTER WIDTH= 4% style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;font-weight: bold;'>" + count + "</TD>");
                    tablerows   = tablerows.Append("<TD ALIGN=LEFT WIDTH= 20% style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;font-weight: bold;'>" + item.ReferralName + "</TD>");
                    tablerows   = tablerows.Append("<TD ALIGN=LEFT WIDTH= 6% style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;font-weight: bold;'>" + item.ContractNumber + "</TD>");
                    tablerows   = tablerows.Append("<TD ALIGN=RIGHT WIDTH= 8%  style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;font-weight: bold;'>" + item.SpotterAmt + "</TD>");
                    tablerows   = tablerows.Append("</TR>");
                    sumOfAmount = sumOfAmount + item.SpotterAmt;
                }
            }
            #region bottom row
            tablerows = tablerows.Append("<TR>");
            tablerows = tablerows.Append("<TD COLSPAN=3 ALIGN=RIGHT WIDTH=58% style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;font-weight: bold;'><FONT COLOR=BLUE>TOTAL CLAIMS</FONT></TD>");
            tablerows = tablerows.Append("<TD ALIGN=RIGHT WIDTH=10% style='font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;font-weight: bold;'><FONT COLOR=BLUE>" + sumOfAmount + "</FONT></TD>");
            tablerows = tablerows.Append("</TR>");
            #endregion
            StringBuilder sb = new StringBuilder(html);
            sb = sb.Replace("@preparationDate", model.PreparationDate.ToString());
            sb = sb.Replace("@CreatedBy", UserName);
            sb = sb.Replace("@RefNo", SpotterRefNumber);
            sb = sb.Replace("@ApprovingOfficer", UserName);
            sb = sb.Replace("@TableRows", tablerows.ToString());
            return(sb.ToString());
        }
        public bool SendEmailNotificationAPI(SpotterViewModel model, string SpotterRefNumber, string UserName, string UserId)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(ConfigurationManager.AppSettings["SitePathAPI"]);

                string html = GenerateHtmlForEmail(model, SpotterRefNumber, UserName);

                EmailViewModel emailModel = new EmailViewModel()
                {
                    MailType  = clsVariables.SpotterMailType,
                    EmailTo   = ConfigurationManager.AppSettings["EmailTo"],
                    EmailFrom = UserId,
                    CcEmail   = "",
                    Subject   = clsVariables.SpotterMailSubject,
                    body      = html,
                    UserId    = UserId,
                };
                var responseTask = client.PostAsJsonAsync <EmailViewModel>("Email", emailModel);

                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public ResultViewModel InsertSpotterData(SpotterViewModel model, string UserName)
        {
            string userMail = "";

            glog.Debug("InsertSpotterData: Entry");
            var result = new ResultViewModel();

            using (var db = new MainDbContext())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        userMail = db.Sys_Users.FirstOrDefault(x => x.Name == UserName).Email;
                        var            NewId         = clsGlobal.GetSystemID("Payment", "SFP", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString());
                        Spotter_Master spotterMaster = new Spotter_Master()
                        {
                            SpotterID        = model.SpotterId,
                            SpotterRefNumber = model.SpotterId == 0 ? NewId.NewId : model.SpotterRefNumber,
                            PreparationDate  = DateTime.ParseExact(model.PreparationDate, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                            ApprovedAmount   = (decimal)0.00,
                            Amount           = model.Amount,
                            Status           = "P",
                            CreatedBy        = model.SpotterId == 0 ? userMail : model.CreatedBy,
                            CreatedDate      = model.SpotterId == 0 ? DateTime.Now : Convert.ToDateTime(model.CreatedDate),
                            UpdatedBy        = model.SpotterId == 0 ? null : userMail,
                            UpdatedDate      = model.SpotterId == 0 ? (DateTime?)null : DateTime.Now,
                        };
                        db.Spotter_Master.Add(spotterMaster);
                        if (model.SpotterId == 0)
                        {
                            db.Entry(spotterMaster).State = System.Data.Entity.EntityState.Added;
                        }
                        else
                        {
                            db.Entry(spotterMaster).State = System.Data.Entity.EntityState.Modified;
                        }


                        var Id = db.SaveChanges();

                        #region Spotter Details
                        var spotterId = 0;
                        foreach (var data in model.SpotterContractDetails)
                        {
                            Spotter_Detail spotter = new Spotter_Detail()
                            {
                                SpotterDetailId = data.SpotterDetailId,
                                ApprovedInd     = "P",
                                SpotterId       = model.SpotterId == 0 ? spotterMaster.SpotterID : model.SpotterId,
                                ContractNumber  = data.ContractNumber,
                                RolloverNumber  = data.RolloverNumber,
                                ItemNumber      = data.ItemNumber,
                                Valid           = data.Valid,
                                CreatedBy       = model.SpotterId == 0 ? userMail : model.CreatedBy,
                                CreatedDate     = model.SpotterId == 0 ? DateTime.Now : Convert.ToDateTime(model.CreatedDate),
                                UpdatedBy       = model.SpotterId == 0 ? null : userMail,
                                UpdatedDate     = model.SpotterId == 0 ? (DateTime?)null : DateTime.Now,
                            };
                            db.Spotter_Detail.Add(spotter);
                            if (model.SpotterId == 0)
                            {
                                db.Entry(spotter).State = EntityState.Added;
                            }
                            else
                            {
                                db.Entry(spotter).State = EntityState.Modified;
                            }
                        }
                        spotterId = db.SaveChanges();
                        #endregion

                        #region Contract Spotter
                        foreach (var data in model.SpotterContractDetails)
                        {
                            Contract_Spotter contractSpotter = new Contract_Spotter()
                            {
                                ContractNumber = data.ContractNumber,
                                RolloverNumber = data.RolloverNumber,
                                ItemNumber     = data.ItemNumber,
                                SpotterAmt     = data.SpotterAmt,
                                Status         = data.Status,
                                ApprovedInd    = data.ApprovedInd,
                                Referral       = data.ReferralID,
                                CreatedBy      = data.CreatedBy,
                                CreatedDate    = data.CreatedDate,
                                UpdatedBy      = userMail,
                                UpdatedDate    = DateTime.Now,
                            };
                            db.Contract_Spotter.Add(contractSpotter);
                            db.Entry(contractSpotter).State = System.Data.Entity.EntityState.Modified;
                        }
                        var contractId = db.SaveChanges();
                        #endregion

                        if (model.SpotterId == 0)
                        {
                            clsGlobal.UpdateSystemIDLastNum(NewId, UserName, db);
                        }
                        if (model.SpotterId != 0)
                        {
                            _clsGlobal.RemoveLockRecord("Spotter", spotterMaster.SpotterRefNumber, UserName);
                        }
                        if (spotterId > 0)
                        {
                            transaction.Commit();
                            var isEmailSend = SendEmailNotificationAPI(model, NewId.NewId, UserName, userMail);
                            if (model.SpotterId == 0)
                            {
                                result.Status  = 1;
                                result.Message = "Data had been submitted for approval!";
                            }
                            else
                            {
                                result.Status  = 1;
                                result.Message = String.Format("{0}  updated successfully!", spotterMaster.SpotterRefNumber);
                            }
                        }
                        else
                        {
                            transaction.Rollback();
                            if (model.SpotterId == 0)
                            {
                                result.Status  = 0;
                                result.Message = "Error occurred when submitting for approval.";
                            }
                            else
                            {
                                result.Status  = 1;
                                result.Message = String.Format("Error occurred when updating {0}!", spotterMaster.SpotterRefNumber);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        glog.Error("InsertSpotterData Exception: " + ex.Message + ex.InnerException);
                        result.Status  = 0;
                        result.Message = "Please contact MIS, error: " + ex.Message;
                        transaction.Rollback();
                    }
                    finally
                    {
                        transaction.Dispose();
                    }
                    glog.Debug("InsertSpotterData: Exit");
                    return(result);
                }
            }
        }