Beispiel #1
0
        /// <summary>
        /// Create new master user in membership service, in modubiz db, in curent app db
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="createInUsersTable"></param>
        /// <param name="CreateInMembership"></param>
        /// <param name="createInCurrentAppUsersTable"></param>
        /// <param name="createInUserAppTable"></param>
        private void CreateNewMasterUser(string userName, string password, bool createInUsersTable, bool CreateInMembership, bool createInCurrentAppUsersTable, bool createInUserAppTable)
        {
            Dictionary <string, object> parameters = new Dictionary <string, object>();
            string email             = "*****@*****.**";
            string role              = "User";
            string encryptedPassword = CryptorHelper.Encrypt(password, true);

            if (createInUsersTable)
            {
                string firstName = "wix";
                string lastName  = "wix";
                Guid   guid      = Guid.NewGuid();
                string sql       = "INSERT INTO [durados_User] ([Username],[FirstName],[LastName],[Email],[Role],[Guid], [Password]) VALUES (@Username,@FirstName,@LastName,@Email,@Role,@Guid, @Password); SELECT IDENT_CURRENT(N'[durados_User]') AS ID ";

                parameters.Add("@Email", email);
                parameters.Add("@Username", userName);
                parameters.Add("@Password", encryptedPassword);
                parameters.Add("@FirstName", firstName);
                parameters.Add("@LastName", lastName);
                parameters.Add("@Role", role);
                parameters.Add("@Guid", guid);

                object scalar = SqlAccess.ExecuteScalar(Maps.Instance.DuradosMap.Database.ConnectionString, sql, parameters);
            }

            if (createInUserAppTable)
            {
                parameters = new Dictionary <string, object>();
                parameters.Add("newUser", userName);
                parameters.Add("appName", Map.AppName);
                parameters.Add("role", role);
                sqlAccess.ExecuteNonQuery(Maps.Instance.DuradosMap.connectionString, "durados_NewAppAsignment @newUser, @appName, @role", parameters, null);
            }

            if (createInCurrentAppUsersTable)
            {
                int userId = Map.Database.GetUserID(userName);

                if (userId == -1)
                {
                    throw new DuradosException("Problem with get user detalis");
                }

                PlugInHelper.AddUserToApp(Convert.ToInt32(Map.Id), userId, role);
            }

            if (CreateInMembership)
            {
                System.Web.Security.MembershipCreateStatus createStatus = (new Durados.Web.Mvc.Controllers.AccountMembershipService()).CreateUser(userName, password, email);
                if (createStatus == System.Web.Security.MembershipCreateStatus.Success)
                {
                    System.Web.Security.Roles.AddUserToRole(userName, role);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Change web master access password if neccessary
        /// </summary>
        /// <param name="changeMembershipPassword"></param>
        /// <param name="changeUserPasswordInDB"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        private void changeWebMasterPassword(bool changeMembershipPassword, bool changeUserPasswordInDB, string userName, string password)
        {
            //Get newUser id
            DataRow newUserRow = Maps.Instance.DuradosMap.Database.GetUserRow(userName);

            if (newUserRow == null)
            {
                throw new DuradosException("Error in create new web master user");
            }

            int newUserId = Convert.ToInt32(newUserRow["ID"]);

            //change password in Membership
            if (changeMembershipPassword)
            {
                string currentEncriptedPassword = Convert.ToString(newUserRow["Password"]);
                string currentPassword          = CryptorHelper.Decrypt(currentEncriptedPassword, true);

                if (!changeUserPasswordInDB || !MembershipService.ChangePassword(userName, currentPassword, password, false))
                {
                    currentPassword = MembershipService.ResetPassword(userName);
                    MembershipService.ChangePassword(userName, currentPassword, password, false);
                }
            }

            //change password in DB
            if (changeUserPasswordInDB)
            {
                string encryptedPassword = CryptorHelper.Encrypt(password, true);
                Dictionary <string, object> updatedValues = new Dictionary <string, object>();

                updatedValues.Add("Password", encryptedPassword);

                SqlGeneralAccess.Update(updatedValues, "durados_User", "ID=" + newUserId.ToString(), Maps.Instance.DuradosMap.Database.ConnectionString);
            }

            MembershipService.UnlockUser(userName);
        }
        public ActionResult Approval([Bind(Include = "request_id, approval_id, fl_approval, approval_suggestion_id, approval_noted, asset_number,asset_name, location_name, department_name, fl_SuggestionChanges")] disposalViewModel disposal_req)
        {
            if (disposal_req.fl_approval != null)
            {
                if (disposal_req.fl_approval == true)
                {
                    if (disposal_req.approval_suggestion_id == null || disposal_req.approval_suggestion_id == 0)
                    {
                        ModelState.AddModelError("approval_suggestion_id", "Suggestion is Mandatory.");
                    }
                }
            }
            else if (disposal_req.approval_noted == null || disposal_req.approval_noted.Trim() == string.Empty)
            {
                ModelState.AddModelError("fl_approval", "Approval is Mandatory.");
                ModelState.AddModelError("approval_noted", "Reject Reason is Mandatory.");
            }

            //update disposal request and approval data with transaction
            if (ModelState.IsValid)
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        bool iscomplete = false;
                        #region "Save update Request Disposal"
                        tr_disposal_request disposal_request = db.tr_disposal_request.Find(disposal_req.request_id);
                        var doneapp = (from app in db.tr_disposal_approval
                                       where (app.approval_date == null && app.fl_active == true &&
                                              app.deleted_date == null && app.request_id == disposal_req.request_id)
                                       select app).ToList();

                        if (disposal_req.fl_SuggestionChanges == false)
                        {
                            disposal_request.fl_approval = disposal_req.fl_approval;
                            if (disposal_req.fl_approval == true)
                            {
                                if (doneapp.Count == 1)
                                {
                                    disposal_request.request_status_id = 5;//complete
                                    iscomplete = true;
                                }
                                else
                                {
                                    disposal_request.request_status_id = 2;//approve
                                }
                            }
                            else
                            {
                                disposal_request.request_status_id = 3; //reject
                            }

                            disposal_request.approval_date = DateTime.Now;
                            disposal_request.updated_date  = DateTime.Now;
                            disposal_request.update_by     = UserProfile.UserId;
                            disposal_request.deleted_date  = null;
                            disposal_request.deleted_by    = null;

                            db.Entry(disposal_request).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        #endregion

                        #region "Save update Approval Disposal"
                        tr_disposal_approval disposal_approval = db.tr_disposal_approval.Find(disposal_req.approval_id);

                        if (disposal_req.fl_approval == true)
                        {
                            disposal_approval.approval_status_id     = 2;//approve
                            disposal_approval.approval_suggestion_id = disposal_req.approval_suggestion_id;
                        }
                        else
                        {
                            disposal_approval.approval_status_id = 3; //reject
                            disposal_approval.approval_noted     = disposal_req.approval_noted;
                            disposal_approval.fl_active          = false;

                            //kembalikan status announcement ke suggestion terakhir
                            tr_disposal_announcement disposal_ann_old = (from tda in db.tr_disposal_announcement
                                                                         where tda.request_id == disposal_approval.request_id && tda.fl_suggestion_changes == true
                                                                         select tda).FirstOrDefault();
                            disposal_ann_old.fl_suggestion_changes = false;
                            disposal_ann_old.fl_active             = true;
                            disposal_ann_old.updated_date          = DateTime.Now;
                            disposal_ann_old.updated_by            = UserProfile.UserId;
                            db.Entry(disposal_ann_old).State       = EntityState.Modified;
                            db.SaveChanges();
                        }
                        disposal_approval.approval_date        = DateTime.Now;
                        disposal_approval.approval_location_id = UserProfile.asset_reg_location_id;
                        disposal_approval.updated_date         = DateTime.Now;
                        disposal_approval.updated_by           = UserProfile.UserId;
                        disposal_approval.deleted_date         = null;
                        disposal_approval.deteled_by           = null;

                        db.Entry(disposal_approval).State = EntityState.Modified;
                        db.SaveChanges();
                        #endregion

                        if (disposal_req.fl_approval == true)
                        {
                            //cek untuk disposal changes: site atau ho
                            tr_asset_registration assetreg = db.tr_asset_registration.Find(disposal_request.asset_id);

                            if (doneapp.Count == 1)
                            {
                                iscomplete = true;
                            }

                            if (!iscomplete && ((assetreg.location_id != 1 && disposal_req.fl_SuggestionChanges == true) || (disposal_req.fl_SuggestionChanges == false)))
                            {
                                #region "kirim email ke approval"

                                var next_approval = (from app in db.tr_disposal_approval
                                                     where (app.approval_date == null && app.fl_active == true && app.deleted_date == null) &&
                                                     app.request_id == disposal_req.request_id
                                                     orderby app.approval_id ascending

                                                     join a in db.ms_employee on app.approval_employee_id equals a.employee_id
                                                     where a.fl_active == true && a.deleted_date == null

                                                     join b in db.ms_user on a.employee_id equals b.employee_id
                                                     where b.fl_active == true && b.deleted_date == null

                                                     select new disposalViewModel
                                {
                                    approval_id = app.approval_id,
                                    ms_employee = a,
                                    ms_user = b
                                }).FirstOrDefault <disposalViewModel>();

                                if (next_approval != null)
                                {
                                    sy_email_log sy_email_log = new sy_email_log();
                                    sy_email_log.elog_to       = next_approval.ms_employee.employee_email;
                                    sy_email_log.elog_subject  = string.Format("Asset Disposal Need Approval");
                                    sy_email_log.elog_template = "EMAIL_TEMPLATE_04";

                                    #region "body mail"
                                    var    _bodymail   = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains("EMAIL_TEMPLATE_04"));
                                    string strBodyMail = _bodymail.FirstOrDefault().app_value;
                                    strBodyMail = strBodyMail.Replace("[to]", next_approval.ms_employee.employee_name);
                                    strBodyMail = strBodyMail.Replace("[action]", "Dispose");
                                    strBodyMail = strBodyMail.Replace("[assetnumber]", disposal_req.asset_number);
                                    strBodyMail = strBodyMail.Replace("[aseetname]", disposal_req.asset_name);
                                    strBodyMail = strBodyMail.Replace("[assetlocation]", disposal_req.location_name);
                                    strBodyMail = strBodyMail.Replace("[department]", disposal_req.department_name);

                                    //token untuk link approval di email
                                    string token = string.Format("DisposalApproval|Approval/{0}|{1}|{2}|{3}", next_approval.approval_id, next_approval.ms_user.user_name, UserProfile.company_id, UserProfile.asset_reg_location_id);
                                    token = CryptorHelper.Encrypt(token, "MD5", true).Replace("+", "plus").Replace("=", "equal");
                                    string linkapp = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/Account/Login?ReturnUrl=%2f&token=" + token));
                                    string strImg  = string.Format("http://{0}/Content/EmailImage/button_approval_disposal.png", Request.Url.Authority);

                                    linkapp = string.Format(@"<a href={0}><img src=""{1}"" alt=""click for approval""/></a>", linkapp, strImg);

                                    strBodyMail = strBodyMail.Replace("[link]", linkapp);

                                    sy_email_log.elog_body = strBodyMail;
                                    #endregion

                                    var EmailHelper = new EmailHelper()
                                    {
                                        ToAddress      = sy_email_log.elog_to,
                                        Email_Template = sy_email_log.elog_template,
                                        MailSubject    = sy_email_log.elog_subject,
                                        MailBody       = sy_email_log.elog_body
                                    };
                                    EmailHelper.Send();
                                }
                                #endregion

                                #region "Save Sy_Message_notification ke approval"

                                sy_message_notification msg = new sy_message_notification();
                                msg.notif_group    = "BALOON_RECEIPT_04";
                                msg.notify_user    = next_approval.ms_user.user_name;
                                msg.notify_ip      = next_approval.ms_employee.ip_address;
                                msg.notify_message = "Ada permintaan approval untuk Asset disposal.";
                                msg.fl_active      = true;
                                msg.created_date   = DateTime.Now;
                                msg.created_by     = UserProfile.UserId;
                                msg.fl_shown       = 0;

                                db.sy_message_notification.Add(msg);
                                db.SaveChanges();
                                #endregion
                            }
                            else
                            {
                                #region "Save Update Process Disposal"
                                ms_disposal_type suggestion = db.ms_disposal_type.Find(disposal_req.approval_suggestion_id);

                                tr_disposal_announcement disposalproses = new tr_disposal_announcement();
                                disposalproses.request_id = disposal_req.request_id;
                                disposalproses.approval_disposal_type_id = disposal_req.approval_suggestion_id;
                                if (disposal_req.approval_suggestion_id != 3)
                                {
                                    disposalproses.fl_announcement_status = true;
                                }
                                else
                                {
                                    disposalproses.fl_remove_asset      = true;
                                    disposalproses.remove_asset_dept_id = 5;
                                }
                                disposalproses.fl_active    = true;
                                disposalproses.created_date = DateTime.Now;
                                disposalproses.created_by   = UserProfile.UserId;
                                disposalproses.updated_date = DateTime.Now;
                                disposalproses.updated_by   = UserProfile.UserId;
                                disposalproses.deleted_date = null;
                                disposalproses.deleted_by   = null;
                                disposalproses.org_id       = UserProfile.OrgId;
                                disposalproses = db.tr_disposal_announcement.Add(disposalproses);
                                db.SaveChanges();
                                #endregion

                                #region "kirim email ke PIC Process Disposal"
                                string emailsetting = string.Empty;
                                string to_name      = string.Empty;


                                switch (disposal_req.approval_suggestion_id)
                                {
                                //resale => procurement
                                case 1:
                                    emailsetting = "EMAIL_TO_DISPOSAL_PROCUREMENT";
                                    to_name      = "Department Procurement";
                                    break;

                                //donation => CSR
                                case 2:
                                    emailsetting = "EMAIL_TO_DISPOSAL_CSR";
                                    to_name      = "Department CSR";
                                    break;

                                //destroy => Acounting
                                case 3:
                                    emailsetting = "EMAIL_TO_DISPOSAL_ACCOUNTING";
                                    to_name      = "Department Accounting";
                                    break;
                                }
                                var _emailto = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains(emailsetting));

                                sy_email_log sy_email_log = new sy_email_log();
                                sy_email_log.elog_to       = _emailto.FirstOrDefault().app_value;
                                sy_email_log.elog_subject  = string.Format("Asset Disposal Need Follow Up ({0})", suggestion.disposal_type_name);
                                sy_email_log.elog_template = "EMAIL_TEMPLATE_05";

                                #region "body mail"
                                var    _bodymail   = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains("EMAIL_TEMPLATE_05"));
                                string strBodyMail = _bodymail.FirstOrDefault().app_value;
                                strBodyMail = strBodyMail.Replace("[to]", to_name);
                                strBodyMail = strBodyMail.Replace("[assetnumber]", disposal_req.asset_number);
                                strBodyMail = strBodyMail.Replace("[aseetname]", disposal_req.asset_name);
                                strBodyMail = strBodyMail.Replace("[assetlocation]", disposal_req.location_name);
                                strBodyMail = strBodyMail.Replace("[department]", disposal_req.department_name);
                                strBodyMail = strBodyMail.Replace("[suggestion]", suggestion.disposal_type_name);
                                strBodyMail = strBodyMail.Replace("[freetext]", "");

                                string linkapp = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/Account/Login?ReturnUrl=%2f"));
                                string strImg  = string.Format("http://{0}/Content/EmailImage/button_asmin.png", Request.Url.Authority);
                                linkapp     = string.Format(@"<a href={0}><img src=""{1}"" alt=""click for process""/></a>", linkapp, strImg);
                                strBodyMail = strBodyMail.Replace("[link]", linkapp);

                                sy_email_log.elog_body = strBodyMail;
                                #endregion

                                var EmailHelper = new EmailHelper()
                                {
                                    ToAddress      = sy_email_log.elog_to,
                                    Email_Template = sy_email_log.elog_template,
                                    MailSubject    = sy_email_log.elog_subject,
                                    MailBody       = sy_email_log.elog_body
                                };
                                EmailHelper.Send();
                                #endregion
                            }
                        }
                        transaction.Commit();
                        ViewBag.ResultMessage = "Update table successfully.";
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception)
                    {
                        // roll back all database operations, if any thing goes wrong
                        transaction.Rollback();
                        ViewBag.ResultMessage = "Error occured, records rolledback.";
                    }
                }
            }
            disposal_req = DataDisposalView(disposal_req.request_id, disposal_req);
            return(View(disposal_req));
        }
        public ActionResult DisposalFollowup([Bind(Include = "announcement_id, request_id, announcement_upload_address, announcement_description, asset_number, asset_name, location_name, department_name, fl_SuggestionChanges, approval_suggestion_id")] disposalViewModel disposal_prs)
        {
            tr_disposal_announcement disposal_announcement = db.tr_disposal_announcement.Find(disposal_prs.announcement_id);

            if (disposal_prs.fl_SuggestionChanges == null || disposal_prs.fl_SuggestionChanges == false)
            {
                if (Request.Files.Count > 0)
                {
                    var file = Request.Files["announcement_upload_address"];
                    if (file == null || file.ContentLength == 0)
                    {
                        ModelState.AddModelError("announcement_upload_address", "Berita Acara is Mandatory.");
                    }
                }
            }
            else
            {
                if (disposal_prs.approval_suggestion_id == null || disposal_prs.approval_suggestion_id == 0)
                {
                    ModelState.AddModelError("approval_suggestion_id", "Suggestion is Mandatory.");
                }
            }
            //update disposal request and approval data with transaction
            if (ModelState.IsValid)
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        if (disposal_prs.fl_SuggestionChanges == null || disposal_prs.fl_SuggestionChanges == false)
                        {
                            //Process Normal
                            #region "Save update Disposal Announcement"
                            if (Request.Files.Count > 0)
                            {
                                //var file = Request.Files[0];
                                app_root_path = Server.MapPath("~/");
                                if (string.IsNullOrWhiteSpace(base_file_path))
                                {
                                    base_file_path = asset_registrationViewModel.path_file_disposal;
                                }

                                string _file = Server.MapPath(base_file_path);
                                if (!Directory.Exists(_file))
                                {
                                    Directory.CreateDirectory(_file);
                                }

                                var file = Request.Files["announcement_upload_address"];
                                if (file != null && file.ContentLength > 0)
                                {
                                    var fileName = "dispose_precurement_" + disposal_prs.request_id.ToString() + "_" + Path.GetFileName(file.FileName);
                                    var path     = Path.Combine(_file, fileName);
                                    file.SaveAs(path);

                                    disposal_announcement.announcement_upload_address = fileName;
                                    disposal_announcement.announcement_description    = disposal_prs.announcement_description;
                                    disposal_announcement.announcement_date           = DateTime.Now;
                                    disposal_announcement.announcement_emp_id         = UserProfile.employee_id;

                                    disposal_announcement.fl_remove_asset      = true;
                                    disposal_announcement.remove_asset_dept_id = 5;
                                }
                            }


                            disposal_announcement.updated_date = DateTime.Now;
                            disposal_announcement.updated_by   = UserProfile.UserId;
                            disposal_announcement.deleted_date = null;
                            disposal_announcement.deleted_by   = null;

                            db.Entry(disposal_announcement).State = EntityState.Modified;
                            db.SaveChanges();
                            #endregion

                            #region "kirim email ke PIC Process Disposal"
                            string emailsetting = string.Empty;
                            string to_name      = string.Empty;
                            string freetext     = string.Empty;


                            emailsetting = "EMAIL_TO_DISPOSAL_ACCOUNTING";
                            to_name      = "Department Accounting";
                            freetext     = "Dokumen <strong>Berita Acara</strong> sudah tersedia. Asset menunggu tindak lanjut (<strong>Dispose</strong>) oleh Accounting Department.";


                            var _emailto = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains(emailsetting));

                            sy_email_log sy_email_log = new sy_email_log();
                            sy_email_log.elog_to       = _emailto.FirstOrDefault().app_value;
                            sy_email_log.elog_subject  = string.Format("Asset Disposal Need Follow Up (Donation)");
                            sy_email_log.elog_template = "EMAIL_TEMPLATE_05";

                            #region "body mail"
                            var    _bodymail   = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains("EMAIL_TEMPLATE_05"));
                            string strBodyMail = _bodymail.FirstOrDefault().app_value;
                            strBodyMail = strBodyMail.Replace("[to]", to_name);
                            strBodyMail = strBodyMail.Replace("[assetnumber]", disposal_prs.asset_number);
                            strBodyMail = strBodyMail.Replace("[aseetname]", disposal_prs.asset_name);
                            strBodyMail = strBodyMail.Replace("[assetlocation]", disposal_prs.location_name);
                            strBodyMail = strBodyMail.Replace("[department]", disposal_prs.department_name);
                            strBodyMail = strBodyMail.Replace("[suggestion]", "Donation");
                            strBodyMail = strBodyMail.Replace("[freetext]", freetext);

                            string linkapp = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/Account/Login?ReturnUrl=%2f"));
                            string strImg  = string.Format("http://{0}/Content/EmailImage/button_asmin.png", Request.Url.Authority);
                            linkapp     = string.Format(@"<a href={0}><img src=""{1}"" alt=""click for process""/></a>", linkapp, strImg);
                            strBodyMail = strBodyMail.Replace("[link]", linkapp);

                            sy_email_log.elog_body = strBodyMail;
                            #endregion

                            var EmailHelper = new EmailHelper()
                            {
                                ToAddress      = sy_email_log.elog_to,
                                Email_Template = sy_email_log.elog_template,
                                MailSubject    = sy_email_log.elog_subject,
                                MailBody       = sy_email_log.elog_body
                            };
                            EmailHelper.Send();
                            #endregion
                        }
                        else if (disposal_prs.fl_SuggestionChanges == true)
                        {
                            #region "Update Data Announcement"
                            //update flag active = false
                            disposal_announcement.fl_suggestion_changes = true;
                            disposal_announcement.fl_active             = false;
                            disposal_announcement.updated_date          = DateTime.Now;
                            disposal_announcement.updated_by            = UserProfile.UserId;
                            disposal_announcement.deleted_date          = null;
                            disposal_announcement.deleted_by            = null;

                            db.Entry(disposal_announcement).State = EntityState.Modified;
                            db.SaveChanges();
                            #endregion

                            #region "Save create disposal_approval"
                            var refApproval = (from a in db.ms_job_level
                                               where (a.fl_active == true && a.deleted_date == null &&
                                                      a.job_level_id == 2)

                                               join b in db.ms_employee_detail on a.job_level_id equals b.job_level_id
                                               where (b.fl_active == true && b.deleted_date == null &&
                                                      b.department_id == 10 && b.company_id == UserProfile.company_id)

                                               join c in db.ms_employee on b.employee_id equals c.employee_id
                                               where c.fl_active == true && c.deleted_date == null

                                               select new disposalViewModel()
                            {
                                department_id = b.department_id,
                                employee_id = b.employee_id,
                                job_level_id = a.job_level_id,
                                employee_email = c.employee_email,
                                employee_name = c.employee_name
                            }).First <disposalViewModel>();

                            //approval disposal changes level 1
                            tr_disposal_approval disposal_approval = new tr_disposal_approval();
                            disposal_approval.request_id             = disposal_prs.request_id;
                            disposal_approval.approval_date          = null;
                            disposal_approval.approval_dept_id       = refApproval.department_id;
                            disposal_approval.approval_employee_id   = refApproval.employee_id;
                            disposal_approval.approval_level_id      = refApproval.job_level_id;
                            disposal_approval.approval_status_id     = 1;//waiting approval
                            disposal_approval.approval_suggestion_id = disposal_prs.approval_suggestion_id;
                            disposal_approval.fl_active    = true;
                            disposal_approval.created_date = DateTime.Now;
                            disposal_approval.created_by   = UserProfile.UserId;
                            disposal_approval.updated_date = DateTime.Now;
                            disposal_approval.updated_by   = UserProfile.UserId;
                            disposal_approval.deleted_date = null;
                            disposal_approval.deteled_by   = null;
                            disposal_approval.org_id       = UserProfile.OrgId;
                            disposal_approval = db.tr_disposal_approval.Add(disposal_approval);
                            db.SaveChanges();

                            int approval_id = disposal_approval.approval_id;

                            //approval disposal changes level 2
                            tr_disposal_request   disposalrequest = db.tr_disposal_request.Find(disposal_prs.request_id);
                            tr_asset_registration assetreg        = db.tr_asset_registration.Find(disposalrequest.asset_id);

                            if (assetreg.location_id != 1)
                            {
                                var refApproval2 = (from a in db.ms_job_level
                                                    where (a.fl_active == true && a.deleted_date == null &&
                                                           a.job_level_id == 3)

                                                    join b in db.ms_employee_detail on a.job_level_id equals b.job_level_id
                                                    where (b.fl_active == true && b.deleted_date == null &&
                                                           b.department_id == disposalrequest.request_dept_id && b.company_id == assetreg.company_id)

                                                    join c in db.ms_employee on b.employee_id equals c.employee_id
                                                    where c.fl_active == true && c.deleted_date == null

                                                    select new disposalViewModel()
                                {
                                    department_id = b.department_id,
                                    employee_id = b.employee_id,
                                    job_level_id = a.job_level_id,
                                    employee_email = c.employee_email,
                                    employee_name = c.employee_name
                                }).First <disposalViewModel>();

                                disposal_approval                        = new tr_disposal_approval();
                                disposal_approval.request_id             = disposal_prs.request_id;
                                disposal_approval.approval_date          = null;
                                disposal_approval.approval_dept_id       = refApproval2.department_id;
                                disposal_approval.approval_employee_id   = refApproval2.employee_id;
                                disposal_approval.approval_level_id      = refApproval2.job_level_id;
                                disposal_approval.approval_status_id     = 1;//waiting approval
                                disposal_approval.approval_suggestion_id = disposal_prs.approval_suggestion_id;
                                disposal_approval.fl_active              = true;
                                disposal_approval.created_date           = DateTime.Now;
                                disposal_approval.created_by             = UserProfile.UserId;
                                disposal_approval.updated_date           = DateTime.Now;
                                disposal_approval.updated_by             = UserProfile.UserId;
                                disposal_approval.deleted_date           = null;
                                disposal_approval.deteled_by             = null;
                                disposal_approval.org_id                 = UserProfile.OrgId;
                                disposal_approval                        = db.tr_disposal_approval.Add(disposal_approval);
                                db.SaveChanges();
                            }
                            #endregion

                            #region "Kirim email ke Procurement Head"
                            sy_email_log sy_email_log = new sy_email_log();
                            sy_email_log.elog_to       = refApproval.employee_email;
                            sy_email_log.elog_subject  = string.Format("Asset Disposal Changes Need Approval");
                            sy_email_log.elog_template = "EMAIL_TEMPLATE_04";

                            var    _bodymail   = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains("EMAIL_TEMPLATE_04"));
                            string strBodyMail = _bodymail.FirstOrDefault().app_value;
                            strBodyMail = strBodyMail.Replace("[to]", refApproval.employee_name);
                            strBodyMail = strBodyMail.Replace("[action]", "Disposal Changes");
                            strBodyMail = strBodyMail.Replace("[assetnumber]", disposal_prs.asset_number);
                            strBodyMail = strBodyMail.Replace("[aseetname]", disposal_prs.asset_name);
                            strBodyMail = strBodyMail.Replace("[assetlocation]", disposal_prs.location_name);
                            strBodyMail = strBodyMail.Replace("[department]", disposal_prs.department_name);

                            int     empid  = Convert.ToInt32(refApproval.employee_id);
                            ms_user msuser = (from m in db.ms_user
                                              where m.employee_id == empid
                                              select m).FirstOrDefault();

                            //token untuk link approval di email
                            string token = string.Format("DisposalApproval|Approval/{0}|{1}|{2}|{3}", approval_id, msuser.user_name, UserProfile.company_id, UserProfile.asset_reg_location_id);
                            token = CryptorHelper.Encrypt(token, "MD5", true).Replace("+", "plus").Replace("=", "equal");
                            string linkapp = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/Account/Login?ReturnUrl=%2f&token=" + token));
                            string strImg  = string.Format("http://{0}/Content/EmailImage/button_approval_disposal.png", Request.Url.Authority);
                            linkapp     = string.Format(@"<a href={0}><img src=""{1}"" alt=""click for approval""/></a>", linkapp, strImg);
                            strBodyMail = strBodyMail.Replace("[link]", linkapp);

                            sy_email_log.elog_body = strBodyMail;

                            var EmailHelper = new EmailHelper()
                            {
                                ToAddress      = sy_email_log.elog_to,
                                Email_Template = sy_email_log.elog_template,
                                MailSubject    = sy_email_log.elog_subject,
                                MailBody       = sy_email_log.elog_body
                            };
                            EmailHelper.Send();
                            #endregion
                        }
                        transaction.Commit();
                        ViewBag.ResultMessage = "Update table successfully.";
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception)
                    {
                        // roll back all database operations, if any thing goes wrong
                        transaction.Rollback();
                        ViewBag.ResultMessage = "Error occured, records rolledback.";
                    }
                }
            }

            disposal_prs = DataDisposalView(disposal_prs.request_id, disposal_prs);

            return(View(disposal_prs));
        }
Beispiel #5
0
        public ActionResult Create([Bind(Include = "asset_id, asset_number,asset_name, location_id, location_name, department_name, request_description")] disposalViewModel disposal_req)
        {
            if (Request.Files.Count > 0)
            {
                var fileexist = Request.Files["asset_img_address"];
                if (fileexist == null || fileexist.ContentLength == 0)
                {
                    ModelState.AddModelError("asset_img_address", "Asset image is mandatory.");
                }
            }
            //input data request disposal with transaction
            if (ModelState.IsValid)
            {
                //Int32 z = Convert.ToInt32("a");
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        #region "Save Request Disposal"
                        tr_disposal_request disposal_request = new tr_disposal_request();
                        disposal_request.disposal_number     = DisposalNumberNew(disposal_req.asset_id);
                        disposal_request.asset_id            = disposal_req.asset_id;
                        disposal_request.request_date        = DateTime.Now;
                        disposal_request.request_description = disposal_req.request_description;
                        disposal_request.request_location_id = disposal_req.location_id;
                        disposal_request.request_dept_id     = UserProfile.department_id;
                        disposal_request.request_emp_id      = UserProfile.employee_id;
                        disposal_request.request_status_id   = 1;
                        disposal_request.fl_active           = true;
                        disposal_request.created_date        = DateTime.Now;
                        disposal_request.created_by          = UserProfile.UserId;
                        disposal_request.updated_date        = DateTime.Now;
                        disposal_request.update_by           = UserProfile.UserId;
                        disposal_request.deleted_date        = null;
                        disposal_request.deleted_by          = null;
                        disposal_request.org_id = UserProfile.OrgId;

                        disposal_request = db.tr_disposal_request.Add(disposal_request);
                        db.SaveChanges();

                        //Save Approval Dept. Head
                        var _qry = (from sa in db.sy_ref_approval_level
                                    where sa.asset_reg_location_id == disposal_req.location_id && sa.job_level_id == 2

                                    join a in db.ms_job_level on sa.job_level_id equals a.job_level_id
                                    where (a.fl_active == true && a.deleted_date == null)

                                    join b in db.ms_employee_detail on a.job_level_id equals b.job_level_id
                                    where (b.fl_active == true && b.deleted_date == null &&
                                           b.department_id == UserProfile.department_id && b.company_id == UserProfile.company_id)

                                    join c in db.ms_employee on b.employee_id equals c.employee_id
                                    where c.fl_active == true && c.deleted_date == null

                                    orderby sa.order_no ascending
                                    select new disposalViewModel()
                        {
                            department_id = b.department_id,
                            employee_id = b.employee_id,
                            job_level_id = a.job_level_id,
                            employee_email = c.employee_email,
                            employee_name = c.employee_name,
                            ip_address = c.ip_address
                        }).ToList <disposalViewModel>();
                        int count       = 1;
                        int approval_id = 0;

                        if (_qry != null)
                        {
                            foreach (disposalViewModel refApproval in _qry)
                            {
                                tr_disposal_approval disposal_approval = new tr_disposal_approval();
                                disposal_approval.request_id           = disposal_request.request_id;
                                disposal_approval.approval_date        = null;
                                disposal_approval.approval_dept_id     = refApproval.department_id;
                                disposal_approval.approval_employee_id = refApproval.employee_id;
                                disposal_approval.approval_level_id    = refApproval.job_level_id;
                                disposal_approval.approval_status_id   = 1;//waiting approval
                                disposal_approval.fl_active            = true;
                                disposal_approval.created_date         = DateTime.Now;
                                disposal_approval.created_by           = UserProfile.UserId;
                                disposal_approval.updated_date         = DateTime.Now;
                                disposal_approval.updated_by           = UserProfile.UserId;
                                disposal_approval.deleted_date         = null;
                                disposal_approval.deteled_by           = null;
                                disposal_approval.org_id = UserProfile.OrgId;
                                disposal_approval        = db.tr_disposal_approval.Add(disposal_approval);
                                db.SaveChanges();

                                if (count == 1)
                                {
                                    //untuk kebutuhan link email
                                    count++;
                                    approval_id = disposal_approval.approval_id;
                                }
                            }
                        }

                        //Save Approval Dept. Head
                        var _qry_ktt = (from sa in db.sy_ref_approval_level
                                        where sa.asset_reg_location_id == disposal_req.location_id && sa.job_level_id == 3

                                        join a in db.ms_job_level on sa.job_level_id equals a.job_level_id
                                        where (a.fl_active == true && a.deleted_date == null)

                                        join b in db.ms_employee_detail on a.job_level_id equals b.job_level_id
                                        where (b.fl_active == true && b.deleted_date == null &&
                                               b.company_id == UserProfile.company_id)

                                        join c in db.ms_employee on b.employee_id equals c.employee_id
                                        where c.fl_active == true && c.deleted_date == null

                                        orderby sa.order_no ascending
                                        select new disposalViewModel()
                        {
                            department_id = b.department_id,
                            employee_id = b.employee_id,
                            job_level_id = a.job_level_id,
                            employee_email = c.employee_email,
                            employee_name = c.employee_name,
                            ip_address = c.ip_address
                        }).ToList <disposalViewModel>();
                        int count_ktt       = 1;
                        int approval_id_ktt = 0;

                        if (_qry_ktt != null)
                        {
                            foreach (disposalViewModel refApproval in _qry_ktt)
                            {
                                tr_disposal_approval disposal_approval = new tr_disposal_approval();
                                disposal_approval.request_id           = disposal_request.request_id;
                                disposal_approval.approval_date        = null;
                                disposal_approval.approval_dept_id     = refApproval.department_id;
                                disposal_approval.approval_employee_id = refApproval.employee_id;
                                disposal_approval.approval_level_id    = refApproval.job_level_id;
                                disposal_approval.approval_status_id   = 1;//waiting approval
                                disposal_approval.fl_active            = true;
                                disposal_approval.created_date         = DateTime.Now;
                                disposal_approval.created_by           = UserProfile.UserId;
                                disposal_approval.updated_date         = DateTime.Now;
                                disposal_approval.updated_by           = UserProfile.UserId;
                                disposal_approval.deleted_date         = null;
                                disposal_approval.deteled_by           = null;
                                disposal_approval.org_id = UserProfile.OrgId;
                                disposal_approval        = db.tr_disposal_approval.Add(disposal_approval);
                                db.SaveChanges();

                                if (count_ktt == 1)
                                {
                                    //untuk kebutuhan link email
                                    count_ktt++;
                                    approval_id_ktt = disposal_approval.approval_id;
                                }
                            }
                        }

                        //Approval BOD berdasarkan data ms_department >> employee_bod_id
                        ms_department dept = db.ms_department.Find(UserProfile.department_id);

                        if (dept != null)
                        {
                            tr_disposal_approval disposal_approval = new tr_disposal_approval();
                            disposal_approval.request_id           = disposal_request.request_id;
                            disposal_approval.approval_date        = null;
                            disposal_approval.approval_dept_id     = dept.department_id;
                            disposal_approval.approval_employee_id = dept.employee_bod_id;
                            disposal_approval.approval_level_id    = 9;
                            disposal_approval.approval_status_id   = 1;//waiting approval
                            disposal_approval.fl_active            = true;
                            disposal_approval.created_date         = DateTime.Now;
                            disposal_approval.created_by           = UserProfile.UserId;
                            disposal_approval.updated_date         = DateTime.Now;
                            disposal_approval.updated_by           = UserProfile.UserId;
                            disposal_approval.deleted_date         = null;
                            disposal_approval.deteled_by           = null;
                            disposal_approval.org_id = UserProfile.OrgId;
                            disposal_approval        = db.tr_disposal_approval.Add(disposal_approval);
                            db.SaveChanges();
                        }

                        if (Request.Files.Count > 0)
                        {
                            //var file = Request.Files[0];
                            app_root_path = Server.MapPath("~/");
                            if (string.IsNullOrWhiteSpace(base_image_path))
                            {
                                base_image_path = asset_registrationViewModel.path_file_disposal;
                            }

                            string img_path = Server.MapPath(base_image_path);
                            if (!Directory.Exists(img_path))
                            {
                                Directory.CreateDirectory(img_path);
                            }

                            var file = Request.Files["asset_img_address"];
                            if (file != null && file.ContentLength > 0)
                            {
                                var fileName = "asset" + disposal_req.asset_id.ToString() + "_" + Path.GetFileName(file.FileName);
                                var path     = Path.Combine(img_path, fileName);
                                file.SaveAs(path);
                                tr_disposal_image _ass_img = new tr_disposal_image()
                                {
                                    request_id        = disposal_request.request_id,
                                    asset_id          = Convert.ToInt32(disposal_req.asset_id),
                                    asset_img_address = fileName,
                                };
                                db.tr_disposal_image.Add(_ass_img);
                                db.SaveChanges();
                            }
                        }
                        #endregion

                        #region "kirim email ke approval level 1"
                        sy_email_log sy_email_log = new sy_email_log();
                        sy_email_log.elog_to       = _qry.FirstOrDefault().employee_email;
                        sy_email_log.elog_subject  = string.Format("Asset Disposal Need Approval");
                        sy_email_log.elog_template = "EMAIL_TEMPLATE_04";

                        var    _bodymail   = app_setting.APPLICATION_SETTING.Where(c => c.app_key.Contains("EMAIL_TEMPLATE_04"));
                        string strBodyMail = _bodymail.FirstOrDefault().app_value;
                        strBodyMail = strBodyMail.Replace("[to]", _qry.FirstOrDefault().employee_name);
                        strBodyMail = strBodyMail.Replace("[action]", "Dispose");
                        strBodyMail = strBodyMail.Replace("[assetnumber]", disposal_req.asset_number);
                        strBodyMail = strBodyMail.Replace("[aseetname]", disposal_req.asset_name);
                        strBodyMail = strBodyMail.Replace("[assetlocation]", disposal_req.location_name);
                        strBodyMail = strBodyMail.Replace("[department]", disposal_req.department_name);

                        int     empid  = Convert.ToInt32(_qry.FirstOrDefault().employee_id);
                        ms_user msuser = (from m in db.ms_user
                                          where m.employee_id == empid
                                          select m).FirstOrDefault();

                        //token untuk link approval di email
                        string token = string.Format("DisposalApproval|Approval/{0}|{1}|{2}|{3}", approval_id, msuser.user_name, UserProfile.company_id, UserProfile.asset_reg_location_id);
                        token = CryptorHelper.Encrypt(token, "MD5", true).Replace("+", "plus").Replace("=", "equal");
                        string linkapp = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/Account/Login?ReturnUrl=%2f&token=" + token));
                        string strImg  = string.Format("http://{0}/Content/EmailImage/button_approval_disposal.png", Request.Url.Authority);

                        linkapp = string.Format(@"<a href={0}><img src=""{1}"" alt=""click for approval""/></a>", linkapp, strImg);

                        strBodyMail            = strBodyMail.Replace("[link]", linkapp);
                        sy_email_log.elog_body = strBodyMail;

                        var EmailHelper = new EmailHelper()
                        {
                            ToAddress      = sy_email_log.elog_to,
                            Email_Template = sy_email_log.elog_template,
                            MailSubject    = sy_email_log.elog_subject,
                            MailBody       = sy_email_log.elog_body
                        };
                        EmailHelper.Send();
                        #endregion

                        #region "Save Sy_Message_notification"
                        sy_message_notification msg = new sy_message_notification();
                        msg.notif_group    = "BALOON_RECEIPT_04";
                        msg.notify_user    = msuser.user_name;
                        msg.notify_ip      = _qry.FirstOrDefault().ip_address;
                        msg.notify_message = "Ada permintaan approval untuk Asset disposal.";
                        msg.fl_active      = true;
                        msg.created_date   = DateTime.Now;
                        msg.created_by     = UserProfile.UserId;
                        msg.fl_shown       = 0;

                        db.sy_message_notification.Add(msg);
                        db.SaveChanges();
                        #endregion

                        transaction.Commit();

                        ViewBag.ResultMessage = "Record inserted into table successfully.";
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception ex)
                    {
                        // roll back all database operations, if any thing goes wrong
                        transaction.Rollback();

                        string msgErr = string.Format("An unknown error has occurred , Please contact your system administrator. {0}", ex.Message);
                        if (ex.InnerException != null)
                        {
                            msgErr += string.Format(" Inner Exception: {0}", ex.InnerException.Message);
                        }
                        ModelState.AddModelError("", msgErr);
                    }
                }
            }
            return(View(disposal_req));
        }