public ENTITIES.Partner getMOUPartnerById(int partner_id)
 {
     try
     {
         ENTITIES.Partner p = db.Partners.Find(partner_id);
         return(p);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public void addMOU(MOUAdd input)
        {
            using (DbContextTransaction transaction = db.Database.BeginTransaction())
            {
                try
                {
                    //add MOU
                    //Check Partner
                    //add MOUPartner =>
                    //check or add PartnerScope
                    //add MOUPartnerScope
                    //add MOUPartnerSpecialization
                    //add MOUStatusHistory
                    DateTime mou_end_date = DateTime.ParseExact(input.BasicInfo.mou_end_date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    MOU      m            = new MOU
                    {
                        mou_code     = input.BasicInfo.mou_code,
                        mou_end_date = mou_end_date,
                        mou_note     = input.BasicInfo.mou_note,
                        evidence     = input.BasicInfo.evidence is null ? "" : input.BasicInfo.evidence,
                        office_id    = input.BasicInfo.office_id,
                        account_id   = 1,
                        add_time     = DateTime.Now,
                        is_deleted   = false,
                        noti_count   = 0
                    };
                    db.MOUs.Add(m);
                    //checkpoint 1
                    db.SaveChanges();
                    MOU objMOU = db.MOUs.Where(x => x.mou_code == input.BasicInfo.mou_code).First();

                    //Add MOUStatusHistory
                    db.MOUStatusHistories.Add(new ENTITIES.MOUStatusHistory
                    {
                        datetime      = DateTime.Now,
                        reason        = input.BasicInfo.reason,
                        mou_id        = objMOU.mou_id,
                        mou_status_id = input.BasicInfo.mou_status_id
                    });

                    foreach (PartnerInfo item in input.PartnerInfo.ToList())
                    {
                        int partner_id_item = 0;
                        //new partner
                        if (item.partner_id == 0)
                        {
                            db.Partners.Add(new ENTITIES.Partner
                            {
                                partner_name = item.partnername_add,
                                website      = item.website_add,
                                address      = item.address_add,
                                country_id   = 13
                            });
                            //checkpoint 2
                            db.SaveChanges();
                            ENTITIES.Partner objPartner = db.Partners.Where(x => x.partner_name == item.partnername_add).First();
                            partner_id_item = objPartner.partner_id;
                        }
                        else //old partner
                        {
                            partner_id_item = item.partner_id;
                        }
                        //add to MOUPartner via each partner of MOU
                        db.MOUPartners.Add(new ENTITIES.MOUPartner
                        {
                            mou_id              = objMOU.mou_id,
                            partner_id          = partner_id_item,
                            mou_start_date      = DateTime.ParseExact(item.sign_date_mou_add, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                            contact_point_name  = item.represent_add,
                            contact_point_email = item.email_add,
                            contact_point_phone = item.phone_add
                        });
                        //PartnerScope and MOUPartnerScope
                        foreach (int tokenScope in item.coop_scope_add.ToList())
                        {
                            PartnerScope objPS            = db.PartnerScopes.Where(x => x.partner_id == partner_id_item && x.scope_id == tokenScope).FirstOrDefault();
                            int          partner_scope_id = 0;
                            if (objPS == null)
                            {
                                db.PartnerScopes.Add(new PartnerScope
                                {
                                    partner_id      = partner_id_item,
                                    scope_id        = tokenScope,
                                    reference_count = 0
                                });
                                //checkpoint 3
                                db.SaveChanges();
                                PartnerScope newObjPS = db.PartnerScopes.Where(x => x.partner_id == partner_id_item && x.scope_id == tokenScope).FirstOrDefault();
                                partner_scope_id = newObjPS.partner_scope_id;
                            }
                            else
                            {
                                objPS.reference_count += 1;
                                db.Entry(objPS).State  = EntityState.Modified;
                                partner_scope_id       = objPS.partner_scope_id;
                            }
                            db.MOUPartnerScopes.Add(new MOUPartnerScope
                            {
                                partner_scope_id = partner_scope_id,
                                mou_id           = objMOU.mou_id
                            });
                        }
                        //checkpoint 4
                        db.SaveChanges();
                        //MOUPartnerSpe
                        MOUPartner objMOUPartner = db.MOUPartners.Where(x => (x.mou_id == objMOU.mou_id && x.partner_id == partner_id_item)).First();
                        foreach (int tokenSpe in item.specialization_add.ToList())
                        {
                            db.MOUPartnerSpecializations.Add(new MOUPartnerSpecialization
                            {
                                mou_partner_id    = objMOUPartner.mou_partner_id,
                                specialization_id = tokenSpe,
                            });
                        }
                    }
                    db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }
 public void addMOUPartner(PartnerInfo input, int mou_id)
 {
     using (DbContextTransaction transaction = db.Database.BeginTransaction())
     {
         try
         {
             int partner_id_item = 0;
             //new partner
             if (input.partner_id == 0)
             {
                 db.Partners.Add(new ENTITIES.Partner
                 {
                     partner_name = input.partnername_add,
                     website      = input.website_add,
                     address      = input.address_add,
                     country_id   = 13
                 });
                 //checkpoint 2
                 db.SaveChanges();
                 ENTITIES.Partner objPartner = db.Partners.Where(x => x.partner_name == input.partnername_add).First();
                 partner_id_item = objPartner.partner_id;
             }
             else //old partner
             {
                 partner_id_item = input.partner_id;
             }
             //add to MOUPartner via each partner of MOU
             db.MOUPartners.Add(new ENTITIES.MOUPartner
             {
                 mou_id              = mou_id,
                 partner_id          = partner_id_item,
                 mou_start_date      = DateTime.ParseExact(input.sign_date_mou_add, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                 contact_point_name  = input.represent_add,
                 contact_point_email = input.email_add,
                 contact_point_phone = input.phone_add
             });
             //PartnerScope and MOUPartnerScope
             foreach (int tokenScope in input.coop_scope_add.ToList())
             {
                 PartnerScope objPS            = db.PartnerScopes.Where(x => x.partner_id == partner_id_item && x.scope_id == tokenScope).FirstOrDefault();
                 int          partner_scope_id = 0;
                 if (objPS == null)
                 {
                     db.PartnerScopes.Add(new PartnerScope
                     {
                         partner_id      = partner_id_item,
                         scope_id        = tokenScope,
                         reference_count = 0
                     });
                     //checkpoint 3
                     db.SaveChanges();
                     PartnerScope newObjPS = db.PartnerScopes.Where(x => x.partner_id == partner_id_item && x.scope_id == tokenScope).FirstOrDefault();
                     partner_scope_id = newObjPS.partner_scope_id;
                 }
                 else
                 {
                     objPS.reference_count += 1;
                     db.Entry(objPS).State  = EntityState.Modified;
                     partner_scope_id       = objPS.partner_scope_id;
                 }
                 db.MOUPartnerScopes.Add(new MOUPartnerScope
                 {
                     partner_scope_id = partner_scope_id,
                     mou_id           = mou_id
                 });
             }
             //checkpoint 4
             db.SaveChanges();
             //MOUPartnerSpe
             MOUPartner objMOUPartner = db.MOUPartners.Where(x => (x.mou_id == mou_id && x.partner_id == partner_id_item)).First();
             foreach (int tokenSpe in input.specialization_add.ToList())
             {
                 db.MOUPartnerSpecializations.Add(new MOUPartnerSpecialization
                 {
                     mou_partner_id    = objMOUPartner.mou_partner_id,
                     specialization_id = tokenSpe,
                 });
             }
             db.SaveChanges();
             transaction.Commit();
         }
         catch (Exception ex)
         {
             transaction.Rollback();
             throw ex;
         }
     }
 }
        public void editMOUPartner(PartnerInfo input, int mou_id, int mou_partner_id)
        {
            using (DbContextTransaction transaction = db.Database.BeginTransaction())
            {
                try
                {
                    int partner_id_item = 0;
                    //new partner
                    if (input.partner_id == 0)
                    {
                        db.Partners.Add(new ENTITIES.Partner
                        {
                            partner_name = input.partnername_add,
                            website      = input.website_add,
                            address      = input.address_add,
                            country_id   = 13
                        });
                        //checkpoint 2
                        db.SaveChanges();
                        ENTITIES.Partner objPartner = db.Partners.Where(x => x.partner_name == input.partnername_add).First();
                        partner_id_item = objPartner.partner_id;
                    }
                    else //old partner
                    {
                        partner_id_item = input.partner_id;
                    }

                    //edit MOUPartner
                    ENTITIES.MOUPartner moup = db.MOUPartners.Where(x => x.mou_partner_id == mou_partner_id).First();
                    moup.mou_id              = mou_id;
                    moup.partner_id          = partner_id_item;
                    moup.mou_start_date      = DateTime.ParseExact(input.sign_date_mou_add, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    moup.contact_point_name  = input.represent_add;
                    moup.contact_point_email = input.email_add;
                    moup.contact_point_phone = input.phone_add;
                    db.Entry(moup).State     = EntityState.Modified;
                    //checkpoint 3
                    db.SaveChanges();

                    //remove old records of MOUPartnerSpecialization.
                    db.MOUPartnerSpecializations.RemoveRange(db.MOUPartnerSpecializations.Where(x => x.mou_partner_id == mou_partner_id).ToList());
                    //checkpoint 4
                    db.SaveChanges();

                    //add new records of MOUPartnerSpecialization.
                    foreach (int tokenSpe in input.specialization_add.ToList())
                    {
                        db.MOUPartnerSpecializations.Add(new MOUPartnerSpecialization
                        {
                            mou_partner_id    = mou_partner_id,
                            specialization_id = tokenSpe,
                        });
                    }
                    //checkpoint 5
                    db.SaveChanges();

                    //get old records of PartnerScope in MOU
                    string sql_old_partnerScope   = @"select t1.partner_scope_id,partner_id,scope_id from IA_Collaboration.PartnerScope t1 inner join 
                        IA_Collaboration.MOUPartnerScope t2 on
                        t1.partner_scope_id = t2.partner_scope_id
                        where mou_id = @mou_id";
                    List <PartnerScope> listOldPS = db.Database.SqlQuery <PartnerScope>(sql_old_partnerScope,
                                                                                        new SqlParameter("mou_id", mou_id)).ToList();

                    //reset old records of scopes of partner.
                    foreach (PartnerScope token in listOldPS.ToList())
                    {
                        PartnerScope objPS = db.PartnerScopes.Where(x => x.partner_scope_id == token.partner_scope_id).First();
                        //decrese refer_count in PartnerScope
                        objPS.reference_count -= 1;
                        db.Entry(objPS).State  = EntityState.Modified;

                        //delete record of MOUPartnerScope.
                        db.MOUPartnerScopes.Remove(db.MOUPartnerScopes.Where(x => x.partner_scope_id == token.partner_scope_id).First());
                    }

                    //add new records of scopes of partner.
                    foreach (int tokenScope in input.coop_scope_add.ToList())
                    {
                        PartnerScope objPS            = db.PartnerScopes.Where(x => x.partner_id == partner_id_item && x.scope_id == tokenScope).FirstOrDefault();
                        int          partner_scope_id = 0;
                        if (objPS == null) //add new to PartnerScope
                        {
                            db.PartnerScopes.Add(new PartnerScope
                            {
                                partner_id      = partner_id_item,
                                scope_id        = tokenScope,
                                reference_count = 0
                            });
                            //checkpoint 3
                            db.SaveChanges();
                            PartnerScope newObjPS = db.PartnerScopes.Where(x => x.partner_id == partner_id_item && x.scope_id == tokenScope).FirstOrDefault();
                            partner_scope_id = newObjPS.partner_scope_id;
                        }
                        else
                        {
                            objPS.reference_count += 1;
                            db.Entry(objPS).State  = EntityState.Modified;
                            partner_scope_id       = objPS.partner_scope_id;
                        }
                        db.MOUPartnerScopes.Add(new MOUPartnerScope
                        {
                            partner_scope_id = partner_scope_id,
                            mou_id           = mou_id
                        });
                    }
                    //checkpoint 5
                    db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }