public void editMOABasicInfo(int moa_id, MOABasicInfo newBasicInfo)
        {
            using (DbContextTransaction transaction = db.Database.BeginTransaction())
            {
                try
                {
                    //update basicInfo
                    MOA moa = db.MOAs.Find(moa_id);
                    moa.moa_code        = newBasicInfo.moa_code;
                    moa.moa_end_date    = newBasicInfo.moa_end_date;
                    moa.moa_note        = newBasicInfo.moa_note;
                    moa.evidence        = newBasicInfo.evidence;
                    db.Entry(moa).State = EntityState.Modified;
                    db.SaveChanges();

                    db.MOAStatusHistories.Add(new MOAStatusHistory
                    {
                        mou_status_id = newBasicInfo.moa_status_id,
                        reason        = newBasicInfo.reason,
                        moa_id        = moa_id,
                        datetime      = DateTime.Now
                    });

                    db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }
 public ActionResult editMOABasicInfo(MOABasicInfo input)
 {
     try
     {
         string id = Session["moa_detail_id"].ToString();
         moa.editMOABasicInfo(int.Parse(id), input);
         return(Json("", JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(new HttpStatusCodeResult(400));
     }
 }
 public ActionResult getBasicInfo()
 {
     try
     {
         string       id   = Session["moa_detail_id"].ToString();
         MOABasicInfo data = moa.getBasicInfoMOA(int.Parse(id));
         return(Json(data, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(new HttpStatusCodeResult(400));
     }
 }
        public MOABasicInfo getBasicInfoMOA(int moa_id)
        {
            try
            {
                string sql_moaBasicInfo =
                    @"select 
                        moa.moa_id, moa.moa_code, mou.office_abbreviation, moap.moa_start_date, moa.moa_end_date,
                        moah.mou_status_name, moah.reason, moaps.scope_abbreviation, moa.evidence, moa.moa_note
                        from IA_Collaboration.MOA moa
                        inner join
	                        (select moa_id, max(moa_start_date) 'moa_start_date'
	                        from IA_Collaboration.MOAPartner
	                        where moa_id = @moa_id
	                        group by moa_id) as moap on moap.moa_id = moa.moa_id
                        inner join
	                        (select moah1.moa_id, cs.mou_status_name, moah2.reason, moah1.[datetime]  
	                        from
	                        (select moa_id, max([datetime]) 'datetime'
	                        from IA_Collaboration.MOAStatusHistory
	                        group by moa_id) as moah1
	                        left join
	                        IA_Collaboration.MOAStatusHistory moah2 on moah1.moa_id = moah2.moa_id and moah1.[datetime] = moah2.[datetime] 
	                        left join
	                        IA_Collaboration.CollaborationStatus cs on cs.mou_status_id = moah2.mou_status_id
	                        where moah1.moa_id = @moa_id) as moah on moah.moa_id = moa.moa_id
                        inner join
	                        (select moaps.moa_id, ps.partner_id, cs.scope_abbreviation
	                        from IA_Collaboration.MOAPartnerScope moaps
	                        inner join IA_Collaboration.PartnerScope ps on moaps.partner_scope_id = ps.partner_scope_id
	                        inner join IA_Collaboration.CollaborationScope cs on cs.[scope_id] = ps.[scope_id]
	                        where moaps.moa_id = @moa_id) as moaps on moaps.moa_id = moa.moa_id
                        inner join
	                        (select mou.mou_id, offi.office_abbreviation
	                        from IA_Collaboration.MOU mou
                            inner join General.Office offi on offi.office_id = mou.office_id) as mou on mou.mou_id = moa.mou_id ";
                MOABasicInfo basicInfo = db.Database.SqlQuery <MOABasicInfo>(sql_moaBasicInfo,
                                                                             new SqlParameter("moa_id", moa_id)).First();
                handlingMOAData(basicInfo);
                return(basicInfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private void handlingMOAData(MOABasicInfo basicInfo)
 {
     //handle date display
     basicInfo.moa_end_date_string   = basicInfo.moa_end_date.ToString("dd'/'MM'/'yyyy");
     basicInfo.moa_start_date_string = basicInfo.moa_start_date.ToString("dd'/'MM'/'yyyy");
 }