Exemplo n.º 1
0
        private void Rezervbtn_Click(object sender, EventArgs e)
        {
            cmbCustomer.Items.AddRange(db.Customers.Select(c => c.Firstname).ToArray());
            string   cusname    = cmbCustomer.Text;
            string   cussurname = cmbSurname.Text;
            string   cusphone   = cmbCustomPhone.Text;
            string   usname     = cmbUser.Text;
            string   stadium    = cmbStadium.Text;
            DateTime rDate      = dtRezervDate.Value;
            decimal  price      = nmPrice.Value;

            string[] ar = { cusname, cussurname, cusphone, usname, stadium };
            if (Utilities.isEmpty(ar))
            {
                lblError.Visible = false;
                int          customID = FindCustomer(cusname, cussurname, cusphone);
                Rezervations rez      = db.Rezervations.Add(new Rezervations
                {
                    CostumerID = customID,
                    UserID     = selectedUser.U_ID,
                    StadiumID  = selectedStadium.S_ID,
                    RezervDate = rDate,
                    Price      = price
                });
                db.SaveChanges();
                MessageBox.Show("Recerved successfully!");
                ClearFormData();

                RoomPanel.Visible = true;
            }
            else
            {
                lblError.Text    = "Please,all the fiel!";
                lblError.Visible = true;
            }
        }
        public IHttpActionResult cancel(int id)
        {
            int user_id = Users.GetUserId(User);

            Rezervations rezervation = db.rezervations.Find(id);

            if (rezervation == null)
            {
                return(NotFound());
            }
            if (rezervation.is_cancel)
            {
                ExceptionThrow.Throw("Rezervasyon daha önce iptal edilmiş.", HttpStatusCode.Forbidden);
            }

            RezervationAdverts advert = db.rezervation_adverts.Where(ra => ra.advert_id == rezervation.advert_id).FirstOrDefault();

            if (advert == null)
            {
                return(NotFound());
            }

            bool is_cancel = rezervation.is_cancel;

            rezervation.state        = false;
            rezervation.is_cancel    = true;
            rezervation.updated_date = DateTime.Now;


            if (advert.user_id == user_id)
            {
                Users user = db.users.Find(rezervation.user_id);
                if (user == null)
                {
                    return(NotFound());
                }
                db.SaveChanges();

                // Add notify
                Notifications notify = new Notifications();
                notify.add(user.id, "#" + rezervation.id + " nolu " + rezervation.days + " günlük rezervasyon talebi iptal edildi!", rezervation.id);

                // Send sms
                NetGsm.Send(user.gsm, "#" + rezervation.id + " nolu " + "(" + rezervation.days + " gün - " + rezervation.total_price + " TL) rezervasyonunuz iptal edildi. - Menkule.com.tr");

                // Send email
                Mailgun.Send("cancel", new Dictionary <string, object>()
                {
                    { "fullname", System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.name) + " " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.lastname) }, { "rezervation_id", rezervation.id }, { "checkin", Convert.ToDateTime(rezervation.checkin).ToShortDateString() }, { "checkout", Convert.ToDateTime(rezervation.checkout).ToShortDateString() }, { "days", rezervation.days }, { "price", rezervation.total_price + " TL." }
                }, user.email, "Rezervasyon talebi iptal edildi.");
            }


            // Delete unavaiable dates
            db.advert_unavaiable_dates.RemoveRange(db.advert_unavaiable_dates.Where(uad => uad.advert_id == rezervation.advert_id && uad.rezervation_id == id));

            if (user_id == rezervation.user_id)
            {
                DateTime lastCanceleableDate = rezervation.checkin.AddDays(-advert.cancel_time);
                DateTime EndDate             = DateTime.Now;
                int      dateDiff            = Convert.ToInt32(lastCanceleableDate.Subtract(EndDate).TotalDays) + 1;
                if (!(dateDiff <= 0 || is_cancel ? false : true))
                {
                    ExceptionThrow.Throw("Bu rezervasyon iptal süresi dışındadır.", HttpStatusCode.Forbidden);
                }
                db.SaveChanges();

                Users advert_owner = db.users.Find(rezervation.owner);
                if (advert_owner != null)
                {
                    // Send sms
                    NetGsm.Send(advert_owner.gsm, "#" + rezervation.id + " nolu " + "(" + rezervation.days + " gün - " + rezervation.total_price + " TL) rezervasyon talebi iptal edildi. - Menkule.com.tr");

                    // Add Notify
                    Notifications notify = new Notifications();
                    notify.add(advert_owner.id, "#" + rezervation.id + " nolu " + rezervation.days + " günlük rezervasyon talebi iptal edildi!", rezervation.id);

                    // Send email
                    Mailgun.Send("cancel", new Dictionary <string, object>()
                    {
                        { "fullname", System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(advert_owner.name) + " " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(advert_owner.lastname) }, { "rezervation_id", rezervation.id }, { "checkin", Convert.ToDateTime(rezervation.checkin).ToShortDateString() }, { "checkout", Convert.ToDateTime(rezervation.checkout).ToShortDateString() }, { "days", rezervation.days }, { "price", rezervation.total_price + " TL." }
                    }, advert_owner.email, "Rezervasyon talebi iptal edildi.");
                }
            }

            return(Ok());
        }
        public object forceApprove([FromBody] _ExistRezervation rezervations, int id)
        {
            int user_id = Users.GetUserId(User);

            // get exist rezervation id
            List <int> rezervations_id = new List <int>();

            rezervations.rezervations.ToList().ForEach(r =>
            {
                if (!db.rezervations.Any(rr => rr.owner == user_id))
                {
                    ExceptionThrow.Throw("Yetkisiz işlem gerçekleştirildi!", HttpStatusCode.Forbidden);
                }
                rezervations_id.Add(r.id);
            });

            // rezervation validation
            Rezervations rezervation = db.rezervations.Find(id);

            if (rezervation == null)
            {
                return(NotFound());
            }

            // rezervation validation
            if (rezervation.updated_date != null)
            {
                ExceptionThrow.Throw("Yetkisiz işlem gerçekleştirildi!", HttpStatusCode.Forbidden);
            }

            // rezervation advert validation
            RezervationAdverts advert = db.rezervation_adverts.Where(ra => ra.advert_id == rezervation.advert_id).FirstOrDefault();

            if (advert == null)
            {
                return(NotFound());
            }

            // rezervation owner validation
            if (advert.user_id != user_id)
            {
                ExceptionThrow.Throw("Yetkisiz işlem gerçekleştirildi!", HttpStatusCode.Forbidden);
            }

            // rezervation user validation
            Users user = db.users.Find(rezervation.user_id);

            if (user == null)
            {
                return(NotFound());
            }

            rezervation.state        = true;
            rezervation.is_cancel    = false;
            rezervation.updated_date = DateTime.Now;

            // available date validation
            var dateList = new List <DateTime>();

            for (DateTime date = rezervation.checkin; date.Date < rezervation.checkout.Date; date = date.AddDays(1))
            {
                dateList.Add(date);
            }

            // set unavaiable date
            dateList.ForEach(date =>
            {
                AdvertUnavailableDate advertUnavaiableDate = new AdvertUnavailableDate
                {
                    advert_id      = rezervation.advert_id,
                    day            = date.Day,
                    month          = date.Month,
                    year           = date.Year,
                    fulldate       = date,
                    created_date   = DateTime.Now,
                    rezervation_id = rezervation.id
                };
                db.advert_unavaiable_dates.Add(advertUnavaiableDate);
            });


            db.rezervations.Where(r => r.owner == user_id && rezervations_id.Contains(r.id)).ToList().ForEach(rez =>
            {
                rez.state        = false;
                rez.is_cancel    = true;
                rez.updated_date = DateTime.Now;
            });

            db.SaveChanges();

            rezervations.rezervations.ForEach(r =>
            {
                Rezervations rez_ = db.rezervations.Where(r_ => r_.id == r.id && r_.state == false & r_.is_cancel == true).FirstOrDefault();
                if (rez_ != null)
                {
                    Users user_ = db.users.Where(u => u.id == rez_.user_id).FirstOrDefault();
                    if (user_ != null)
                    {
                        // Add notify
                        Notifications notify_ = new Notifications();
                        notify_.add(user_.id, "#" + rez_.id + " nolu " + rez_.days + " günlük rezervasyon talebi iptal edildi!", rez_.id);

                        // Send sms
                        NetGsm.Send(user_.gsm, "#" + rez_.id + " nolu " + "(" + rez_.days + " gün - " + rez_.total_price + " TL) rezervasyonunuz iptal edildi. - Menkule.com.tr");

                        // Send email
                        Mailgun.Send("cancel", new Dictionary <string, object>()
                        {
                            { "fullname", System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user_.name) + " " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user_.lastname) }, { "rezervation_id", rez_.id }, { "checkin", Convert.ToDateTime(rez_.checkin).ToShortDateString() }, { "checkout", Convert.ToDateTime(rez_.checkout).ToShortDateString() }, { "days", rez_.days }, { "price", rez_.total_price + " TL." }
                        }, user_.email, "Rezervasyon talebi iptal edildi.");
                    }
                }
            });

            // send sms
            NetGsm.Send(user.gsm, "#" + rezervation.id + " nolu " + "(" + rezervation.days + " gün - " + rezervation.total_price + " TL) rezervasyonunuz onaylandı. - Menkule.com.tr");

            // send notifications
            Notifications notify = new Notifications();

            notify.add(user.id, "#" + rezervation.id + " nolu rezervasyon talebiniz onaylandı.", rezervation.id);

            // Send email
            Mailgun.Send("approve", new Dictionary <string, object>()
            {
                { "fullname", System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.name) + " " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.lastname) }, { "rezervation_id", rezervation.id }, { "checkin", Convert.ToDateTime(rezervation.checkin).ToShortDateString() }, { "checkout", Convert.ToDateTime(rezervation.checkout).ToShortDateString() }, { "days", rezervation.days }, { "price", rezervation.total_price + " TL." }
            }, user.email, "Rezervasyon talebi onaylandı.");

            return(Ok());
        }
        public object approve(int id)
        {
            int user_id = Users.GetUserId(User);

            Rezervations rezervation = db.rezervations.Find(id);

            if (rezervation == null)
            {
                return(NotFound());
            }


            if (rezervation.updated_date != null)
            {
                ExceptionThrow.Throw("Yetkisiz işlem gerçekleştirildi!", HttpStatusCode.Forbidden);
            }

            RezervationAdverts advert = db.rezervation_adverts.Where(ra => ra.advert_id == rezervation.advert_id).FirstOrDefault();

            if (advert == null)
            {
                return(NotFound());
            }

            if (advert.user_id != user_id)
            {
                ExceptionThrow.Throw("Yetkisiz işlem gerçekleştirildi!", HttpStatusCode.Forbidden);
            }

            Users user = db.users.Find(rezervation.user_id);

            if (user == null)
            {
                return(NotFound());
            }

            // exist rezervation validations
            DateTime checkoutDate       = rezervation.checkout.AddDays(-1);
            var      exist_rezervations = db.rezervations.Where(r => ((r.checkin >= rezervation.checkin && r.checkin <= checkoutDate) || (r.checkin <= rezervation.checkin && r.checkout <= checkoutDate)) && r.advert_id == advert.advert_id && r.id != id && r.state == false && r.is_cancel == false && r.checkout > rezervation.checkin).ToList();

            if (exist_rezervations.Count > 0)
            {
                ExceptionThrow.Throw(exist_rezervations, HttpStatusCode.NotImplemented);
            }

            rezervation.state        = true;
            rezervation.is_cancel    = false;
            rezervation.updated_date = DateTime.Now;

            // available date validation
            var dateList = new List <DateTime>();

            for (DateTime date = rezervation.checkin; date.Date < rezervation.checkout.Date; date = date.AddDays(1))
            {
                dateList.Add(date);
            }

            // set unavaiable date
            dateList.ForEach(date =>
            {
                AdvertUnavailableDate advertUnavaiableDate = new AdvertUnavailableDate
                {
                    advert_id      = rezervation.advert_id,
                    day            = date.Day,
                    month          = date.Month,
                    year           = date.Year,
                    fulldate       = date,
                    created_date   = DateTime.Now,
                    rezervation_id = rezervation.id
                };
                db.advert_unavaiable_dates.Add(advertUnavaiableDate);
            });

            db.SaveChanges();

            // send sms
            NetGsm.Send(user.gsm, "#" + rezervation.id + " nolu " + "(" + rezervation.days + " gün - " + rezervation.total_price + " TL) rezervasyonunuz onaylandı. - Menkule.com.tr");

            // send notifications
            Notifications notify = new Notifications();

            notify.add(user.id, "#" + rezervation.id + " nolu rezervasyon talebiniz onaylandı.", rezervation.id);

            // Send email
            Mailgun.Send("approve", new Dictionary <string, object>()
            {
                { "fullname", System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.name) + " " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.lastname) }, { "rezervation_id", rezervation.id }, { "checkin", Convert.ToDateTime(rezervation.checkin).ToShortDateString() }, { "checkout", Convert.ToDateTime(rezervation.checkout).ToShortDateString() }, { "days", rezervation.days }, { "price", rezervation.total_price + " TL." }
            }, user.email, "Rezervasyon talebi onaylandı.");

            return(Ok());
        }
        public IHttpActionResult add([FromBody] _Rezervation _rezervation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // get user
            int   user_id = Users.GetUserId(User);
            Users user    = db.users.Where(u => u.id == user_id && u.state == true).FirstOrDefault();

            if (user == null)
            {
                ExceptionThrow.Throw("Lütfen hesabınızı doğrulayın.", HttpStatusCode.Forbidden);
            }

            // existence
            Advert advert = db.advert.Where(a => a.state == true && a.id == _rezervation.advert_id).FirstOrDefault();

            if (advert == null)
            {
                return(NotFound());
            }
            if (db.rezervations.Any(rez => rez.user_id == user_id && rez.advert_id == _rezervation.advert_id && rez.checkin == _rezervation.checkin && rez.checkout == _rezervation.checkout))
            {
                ExceptionThrow.Throw("Zaten aynı tarih için bir rezervasyon talebiniz bulunmakta.", HttpStatusCode.Forbidden);
            }

            if (user_id == advert.user_id)
            {
                ExceptionThrow.Throw("Lütfen farklı bir hesap ile deneyin.", HttpStatusCode.Forbidden);
            }

            // visitor validation
            AdvertProperties properties = db.advert_properties.Where(ap => ap.advert_id == advert.id).FirstOrDefault();

            if (properties == null)
            {
                return(NotFound());
            }

            if (properties.visitor < _rezervation.visitors.Count)
            {
                ExceptionThrow.Throw("Bu ilan için en fazla. " + properties.visitor + " misafir kabul edilebilmektedir.", HttpStatusCode.Forbidden);
            }

            // get owner
            Users owner = db.users.Where(u => u.id == advert.user_id).FirstOrDefault();

            if (owner == null)
            {
                return(NotFound());
            }

            // rezervation dates list
            var RezervationDates = new List <DateTime>();

            for (DateTime date = _rezervation.checkin; date.Date < _rezervation.checkout.Date; date = date.AddDays(1))
            {
                RezervationDates.Add(date);
            }

            // available dates validation
            List <AdvertAvailableDate> avaiableDates = db.advert_avaiable_dates.Where(aad => aad.advert_id == _rezervation.advert_id).ToList();

            if (avaiableDates.Count > 0)
            {
                RezervationDates.ForEach(rd =>
                {
                    if (avaiableDates.Find(a => a.fulldate == rd) == null)
                    {
                        ExceptionThrow.Throw("İlan belirtilen tarih için müsait değil.", HttpStatusCode.Forbidden);
                    }
                });
            }

            // unavailable dates validation
            if (db.advert_unavaiable_dates.Where(i => i.advert_id == _rezervation.advert_id && RezervationDates.Contains(i.fulldate)).Count() > 0)
            {
                ExceptionThrow.Throw("İlan belirtilen tarih için müsait değil.", HttpStatusCode.Forbidden);
            }

            // min layover date validation
            if ((_rezervation.checkout - _rezervation.checkin).TotalDays < advert.min_layover)
            {
                ExceptionThrow.Throw("Bu ilan için en az " + advert.min_layover + " günlük rezervasyon oluşturulabilir.", HttpStatusCode.Forbidden);
            }

            // create rezervation
            Rezervations rezervation = new Rezervations
            {
                advert_id    = _rezervation.advert_id,
                checkin      = _rezervation.checkin,
                checkout     = _rezervation.checkout,
                created_date = DateTime.Now,
                gsm          = user.gsm,
                name         = user.name,
                lastname     = user.lastname,
                visitor      = _rezervation.visitors.Count,
                user_id      = user.id,
                day_price    = advert.price,
                owner        = advert.user_id,
                note         = _rezervation.note
            };

            db.rezervations.Add(rezervation);
            db.SaveChanges();

            // create rezervations advert
            RezervationAdverts rezervation_advert = new RezervationAdverts
            {
                adress         = advert.adress,
                advert_id      = advert.id,
                cancel_time    = advert.cancel_time,
                city_id        = advert.city_id,
                town_id        = advert.town_id,
                title          = advert.title,
                description    = advert.description,
                entry_time     = advert.entry_time,
                exit_time      = advert.exit_time,
                latitude       = advert.latitude,
                longitude      = advert.longitude,
                rezervation_id = rezervation.id,
                created_date   = advert.created_date,
                user_id        = advert.user_id,
                advert_type_id = advert.advert_type_id
            };

            db.rezervation_adverts.Add(rezervation_advert);

            // create visitors
            _rezervation.visitors.ToList().ForEach(v =>
            {
                RezervationVisitors visitor = new RezervationVisitors
                {
                    created_date   = DateTime.Now,
                    fullname       = v.fullname,
                    gender         = v.gender,
                    rezervation_id = rezervation.id,
                    tc             = v.tc
                };
                db.rezervation_visitors.Add(visitor);
            });

            db.SaveChanges();

            // send notifications
            Notifications notify = new Notifications();

            notify.add(advert.user_id, "#" + advert.id + " nolu ilanınız için " + rezervation.days + " günlük rezervasyon talebi!", rezervation.id);

            // send sms
            NetGsm.Send(owner.gsm, "#" + advert.id + " nolu ilaniniz icin toplam " + rezervation.days + " günlük (" + rezervation.total_price + " TL) rezervasyon talebi oluşturuldu. - Menkule.com.tr");

            //send mail
            Mailgun.Send("rezervation", new Dictionary <string, object>()
            {
                { "fullname", System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.name) + " " + System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.lastname) }, { "advert_id", advert.id }, { "checkin", Convert.ToDateTime(rezervation.checkin).ToShortDateString() }, { "checkout", Convert.ToDateTime(rezervation.checkout).ToShortDateString() }, { "days", rezervation.days }, { "price", rezervation.total_price + " TL." }
            }, owner.email, "Yeni rezervasyon talebi");

            return(Ok());
        }