Example #1
0
        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);
            }
        }
Example #2
0
        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);
                }
            }
        }