Exemple #1
0
        internal static DataSet GetSyncData(DateTime toDate)
        {
            MealSetTableAdapter mealSetTA = new MealSetTableAdapter();
            ScheduleTableAdapter scheduleTA = new ScheduleTableAdapter();
            ScheduleMealSetDetailTableAdapter scheduleMealSetDetailTA = new ScheduleMealSetDetailTableAdapter();
            ServingTimeTableAdapter servingTimeTA = new ServingTimeTableAdapter();
            TransactionHistoryTableAdapter transactionHistoryTA = new TransactionHistoryTableAdapter();
            TransactionTypeTableAdapter transactionTypeTA = new TransactionTypeTableAdapter();
            UserInfoTableAdapter userInfoTA = new UserInfoTableAdapter();
            UserTypeTableAdapter userTypeTA = new UserTypeTableAdapter();

            DateTime minDate = (DateTime)SqlDateTime.MinValue;

            DataSet ds = new DataSet();
            ds.Tables.Add(userTypeTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(userInfoTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(mealSetTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(servingTimeTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(scheduleTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(scheduleMealSetDetailTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(transactionTypeTA.GetDataByDate(minDate, toDate));
            ds.Tables.Add(transactionHistoryTA.GetDataByDate(minDate, toDate));

            return ds;
        }
        public JsonResult AddScheduleMealSet(int mealSetID, int scheduleID, bool isDayOn)
        {
            //if isDayOn = true, just insert into ScheduleMealSetDetail
            // else open transaction and set isDayOn of schedule = true
            if (isDayOn)
            {
                try
                {
                    ScheduleMealSetDetailTableAdapter scheduleMealSetDetailTA = new ScheduleMealSetDetailTableAdapter();

                    DataTable dt = scheduleMealSetDetailTA.GetDataByMealSetIDScheduleID(mealSetID, scheduleID);
                    if (dt.Rows.Count != 0)
                    {
                        return Json("duplicate", JsonRequestBehavior.AllowGet);
                    }

                    DateTime now = DateTime.Now;
                    string newScheduleMealSetDetailIDStr = scheduleMealSetDetailTA.InsertScalar(mealSetID, scheduleID, now, now).ToString();
                    int newScheduleMealSetDetailID = int.Parse(newScheduleMealSetDetailIDStr);

                    XmlSync.SaveScheduleMealSetDetailXml(newScheduleMealSetDetailID, mealSetID, scheduleID, now, now, null);

                    return Json(new { result = "done" }, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    Log.ErrorLog(ex.Message);
                    return Json("error", JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                ScheduleMealSetDetailTableAdapter scheduleMealSetDetailTA = new ScheduleMealSetDetailTableAdapter();
                ScheduleTableAdapter scheduleTA = new ScheduleTableAdapter();

                scheduleMealSetDetailTA.Connection.Open();
                scheduleTA.Connection = scheduleMealSetDetailTA.Connection;

                using (SqlTransaction transaction = scheduleMealSetDetailTA.Connection.BeginTransaction())
                {
                    try
                    {
                        scheduleMealSetDetailTA.AttachTransaction(transaction);
                        scheduleTA.AttachTransaction(transaction);

                        DataTable scheduleDT = scheduleTA.GetDataByID(scheduleID);

                        if (scheduleDT.Rows.Count != 1)
                        {
                            throw new Exception("Can't find schedule with [scheduleID] = " + scheduleID);
                        }

                        DataRow scheduleRow = scheduleDT.Rows[0];
                        string username = AccountInfo.GetUserName(Request);
                        DateTime now = DateTime.Now;

                        scheduleTA.Update(scheduleRow.Field<DateTime>("Date"), scheduleRow.Field<int>("ServingTimeID"), true
                            , scheduleRow.Field<DateTime>("InsertedDate"), username, now, scheduleID);

                        XmlSync.SaveScheduleXml(scheduleID, scheduleRow.Field<DateTime>("Date"), scheduleRow.Field<int>("ServingTimeID"), true
                            , scheduleRow.Field<DateTime>("InsertedDate"), username, now, null);

                        string newScheduleMealSetDetailIDStr = scheduleMealSetDetailTA.InsertScalar(mealSetID, scheduleID, now, now).ToString();
                        int newScheduleMealSetDetailID = int.Parse(newScheduleMealSetDetailIDStr);

                        XmlSync.SaveScheduleMealSetDetailXml(newScheduleMealSetDetailID, mealSetID, scheduleID, now, now, null);

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Log.ErrorLog(ex.Message);
                        return Json("error", JsonRequestBehavior.AllowGet);
                    }
                }

                return Json(new { result = "done" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult SetIsDayOn(bool isDayOn, int scheduleID)
        {
            ScheduleMealSetDetailTableAdapter scheduleMealSetDetailTA = new ScheduleMealSetDetailTableAdapter();
            ScheduleTableAdapter scheduleTA = new ScheduleTableAdapter();

            scheduleMealSetDetailTA.Connection.Open();
            scheduleTA.Connection = scheduleMealSetDetailTA.Connection;

            using (SqlTransaction transaction = scheduleMealSetDetailTA.Connection.BeginTransaction())
            {
                scheduleMealSetDetailTA.AttachTransaction(transaction);
                scheduleTA.AttachTransaction(transaction);

                try
                {
                    if (!isDayOn)
                    {
                        DataTable scheduleMealSetDetailDT = scheduleMealSetDetailTA.GetDataByScheduleID(scheduleID);

                        foreach (DataRow row in scheduleMealSetDetailDT.Rows)
                        {
                            int scheduleMealSetDetailID = row.Field<int>("ScheduleMealSetDetailID");
                            scheduleMealSetDetailTA.Delete(scheduleMealSetDetailID);
                        }
                    }

                    DataTable scheduleDT = scheduleTA.GetDataByID(scheduleID);

                    if (scheduleDT.Rows.Count != 1)
                    {
                        throw new Exception("Can't find schedule with [scheduleID] = " + scheduleID);
                    }

                    DataRow scheduleRow = scheduleDT.Rows[0];
                    string username = AccountInfo.GetUserName(Request);
                    DateTime now = DateTime.Now;

                    scheduleTA.Update(scheduleRow.Field<DateTime>("Date"), scheduleRow.Field<int>("ServingTimeID"), isDayOn
                        , scheduleRow.Field<DateTime>("InsertedDate"), username, now, scheduleID);

                    XmlSync.SaveScheduleXml(scheduleID, scheduleRow.Field<DateTime>("Date"), scheduleRow.Field<int>("ServingTimeID"), isDayOn
                        , scheduleRow.Field<DateTime>("InsertedDate"), username, now, null);

                    transaction.Commit();
                    return Json(new { result = "done" }, JsonRequestBehavior.AllowGet);
                }
                catch (Exception ex)
                {
                    Log.ErrorLog(ex.Message);
                    transaction.Rollback();
                    return Json("error", JsonRequestBehavior.AllowGet);
                }
            }
        }
        public JsonResult GetScheduleID(int selectedDay, int selectedMonth, int selectedYear, int ServingTimeID)
        {
            try
            {
                DateTime selectedDate = DateTime.Parse(selectedYear + "-" + selectedMonth + "-" + selectedDay);

                ScheduleTableAdapter scheduleTA = new ScheduleTableAdapter();
                int? ScheduleID = scheduleTA.GetIDFromDateAndServingTime(selectedDate, ServingTimeID);

                if (ScheduleID == null)
                {
                    DateTime now = DateTime.Now;
                    string username = AccountInfo.GetUserName(Request);

                    string newScheduleID = scheduleTA.InsertScalar(selectedDate, ServingTimeID, true, now, username, now).ToString();

                    ScheduleID = int.Parse(newScheduleID);
                    XmlSync.SaveScheduleXml(ScheduleID.Value, selectedDate, ServingTimeID, true, now, username, now, null);
                }

                return Json(new { ScheduleID = ScheduleID.Value }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                return Json("error", JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult GetDateRange()
        {
            ScheduleTableAdapter scheduleAdapter = new ScheduleTableAdapter();

            DateTime? maxDate = null;
            DateTime? minDate = null;
            try
            {
                maxDate = scheduleAdapter.GetMaxDate();
                minDate = scheduleAdapter.GetMinDate();
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
                return Json("error", JsonRequestBehavior.AllowGet);
            }

            IList<string> dateMinMax = new List<string>();
            dateMinMax.Add(minDate.Value.ToString("yyyy-MM"));
            dateMinMax.Add(maxDate.Value.ToString("yyyy-MM"));
            dateMinMax.Add(DateTime.Now.ToString("yyyy-MM"));

            return Json(dateMinMax, JsonRequestBehavior.AllowGet);
        }
 public ActionResult DeleteServingTime(string servingTimeID)
 {
     ServingTimeTableAdapter servingTimeTableAdapter = new ServingTimeTableAdapter();
     ScheduleTableAdapter scheduleTableAdapter = new ScheduleTableAdapter();
     try
     {
         int id = Convert.ToInt32(servingTimeID);
         servingTimeTableAdapter.DeleteServingTimeByID(id);
         Session["deleteServingTime"] = "Xóa thành công!";
     }
     catch (Exception ex)
     {
         Log.ErrorLog(ex.Message);
         Session["deleteServingTime"] = "Xóa thất bại! Thời gian phục vụ này đang được sử dụng.";
     }
     return RedirectToAction("ViewServingTime", "Schedule");
 }