public bool Remove(QuoteOffer model)
        {
            SqlConnection oConn = null;

            try
            {
                oConn = ConnManager.OpenConn();
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            };

            bool result;

            try
            {
                result = Remove(model, oConn);
            }
            catch (Exception ex)
            {
                ConnManager.CloseConn(oConn);
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            }

            ConnManager.CloseConn(oConn);

            return(result);
        }
        public void SendNotifications(QuoteOffer model)
        {
            var lista = GetListInterestedByQuote(model.QHeaderKey);

            foreach (var item in lista.Where(w => w.VendorKey != model.VendorKey).ToList())
            {
                SendMailToVendors(item);
                Thread.Sleep(3000);
            }

            SendMailToWinner(lista.Where(w => w.VendorKey == model.VendorKey).FirstOrDefault(), model);
        }
        private bool Remove(QuoteOffer model, SqlConnection oConn)
        {
            string sql = "DELETE FROM QuoteOffers WHERE (QOfferKey = @key)";

            SqlCommand cmd = new SqlCommand(sql, oConn);

            cmd.Parameters.Add("@key", SqlDbType.Int).Value = model.QOfferKey;

            int number = Convert.ToInt32(cmd.ExecuteNonQuery());

            if (number > 0)
            {
                return(true);
            }

            return(false);
        }
        public QuoteOffer Update(QuoteOffer model)
        {
            SqlConnection oConn = null;

            try
            {
                oConn = ConnManager.OpenConn();
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            };

            var oldData = Get(model.QOfferKey, oConn);

            string sql = "UPDATE QuoteOffers SET {0} WHERE QOfferKey = @key";

            EnumExtension.setUpdateValues(model, "QOfferKey", ref sql);

            SqlCommand cmd = new SqlCommand(sql, oConn);

            cmd.Parameters.Add("@key", SqlDbType.Int).Value = model.QOfferKey;

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ConnManager.CloseConn(oConn);
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            }

            var returnData = Get(model.QOfferKey, oConn);

            ConnManager.CloseConn(oConn);

            return(returnData);
        }
        public QuoteOffer Add(QuoteOffer model)
        {
            SqlConnection oConn = null;

            try
            {
                oConn = ConnManager.OpenConn();
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            };

            model.QOfferCreatedDate = DateTime.Now;
            string sql = "INSERT INTO QuoteOffers ({0}) VALUES ({1}) " +
                         "SELECT SCOPE_IDENTITY()";

            EnumExtension.setListValues(model, "QOfferKey", ref sql);

            SqlCommand cmd = new SqlCommand(sql, oConn);

            int keyGenerated = 0;

            try
            {
                keyGenerated = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                ConnManager.CloseConn(oConn);
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            }

            var returnData = Get(keyGenerated, oConn);

            ConnManager.CloseConn(oConn);

            return(returnData);
        }
        private void SendMailToWinner(QuoteInterested model, QuoteOffer offer)
        {
            try
            {
                string errMsg = String.Empty;

                NuvemMailMessage msg    = new NuvemMailMessage();
                List <string>    Emails = new List <string>();

                string subject = "Nuvem B2B";

                if (!string.IsNullOrEmpty(model.VendorEmail))
                {
                    Emails.Add(model.VendorEmail);
                }

                if (!string.IsNullOrEmpty(model.UserEmail))
                {
                    Emails.Add(model.UserEmail);
                }

                if (string.IsNullOrEmpty(model.VendorEmail) && string.IsNullOrEmpty(model.UserEmail))
                {
                    return;
                }

                string from = "Nuvem Notification Services";

                /* Si deseamos Adjuntar algún archivo*/
                //mnsj.Attachments.Add(new Attachment(pdfFile));
                string msj = "";

                using (WebClient client = new WebClient())
                {
                    var protocol = HttpContext.Current.Request.IsSecureConnection ? "https:" : "http:";
                    var path     = HttpContext.Current.Request.Url.AbsolutePath;
                    var port     = HttpContext.Current.Request.Url.Port;
                    var host     = HttpContext.Current.Request.Url.Host + ((port == 80) ? "" : ":" + port.ToString());

                    path = host + @"/" + path.Substring(0, path.IndexOf(@"api/")) + @"/EmailAcceptedOffer";
                    path = path.Replace(@"//", @"/");
                    path = protocol + @"//" + path;
                    var url = path + string.Format("?q={0}&v={1}&w=1", model.QHeaderKey, model.VendorKey);

                    msj = client.DownloadString(url);
                }

                MailHelper.SendToEnqueue(subject, Emails, msj, from);

                var bc = new BroadcastMessage();
                bc.CommandType = "Notification";
                bc.CommandText = JsonConvert.SerializeObject(offer);
                SignalRHelper.SendData(bc);
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                    ex = ex.InnerException;
                }
            }
        }
        public QuoteOffer AcceptOffer(int QOfferKey)
        {
            var model      = Get(QOfferKey);
            var QHeaderKey = model.QHeaderKey;

            SqlConnection oConn = null;

            try
            {
                oConn = ConnManager.OpenConn();
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            };

            string sql = "sp_AcceptOffer";

            SqlDataAdapter da = new SqlDataAdapter(sql, oConn);

            da.SelectCommand.Parameters.Add("QOfferKey", SqlDbType.Int).Value  = QOfferKey;
            da.SelectCommand.Parameters.Add("QHeaderKey", SqlDbType.Int).Value = QHeaderKey;
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataSet ds = new DataSet();

            try
            {
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                ConnManager.CloseConn(oConn);
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                throw;
            }

            ConnManager.CloseConn(oConn);

            DataTable dt;

            dt = ds.Tables[0];
            var data = new QuoteOffer();

            if (dt.Rows.Count > 0)
            {
                data = dt.ToList <QuoteOffer>().FirstOrDefault();
            }
            else
            {
                throw new Exception("No records found");
            }

            try
            {
                SendNotifications(data);
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);
                //throw;
            }

            return(data);
        }