Example #1
0
        public override void SaveRecord(Model.SavingParemeter Paras)
        {
            DAL.tblCompany SaveModel = null;

            DAL.tblPayrollMonth PayRollMonth = null;


            if (Paras.SavingInterface == SavingParemeter.eSavingInterface.AddNew || EditRecordDataSource == null)
            {
                SaveModel = new tblCompany();
            }
            else
            {
                SaveModel = DALObject.FindSaveModelByPrimeKey(((CompanyEditListModel)EditRecordDataSource).CompanyID);

                PayRollMonth = PayrollMonthDAL.GetLatestPayrollMonthByCompanyID(((CompanyEditListModel)EditRecordDataSource).CompanyID);
                //DO SOMETHING HERE FOR PAYROLLMONTH
                if (SaveModel == null)
                {
                    Paras.SavingResult = new SavingResult();
                    Paras.SavingResult.ExecutionResult = eExecutionResult.ValidationError;
                    Paras.SavingResult.ValidationError = "Can not edit. Selected record not found, it may be deleted by another user.";
                    return;
                }
            }

            // on Creating new company or while editing there is no month exists.
            if (PayRollMonth == null)
            {
                PayRollMonth = new tblPayrollMonth()
                {
                    tblCompany = SaveModel
                };
            }


            SaveModel.CompanyCode = Model.CommonFunctions.ParseInt(txtCode.Text) + 1;
            SaveModel.CompanyName = txtCompanyName.Text;
            if (Paras.SavingInterface == SavingParemeter.eSavingInterface.AddNew)
            {
                SaveModel.BusinessStartedFrom = (DateTime)deBusinessStartedFrom.EditValue;
            }
            SaveModel.CurrencyID = (int)lookupCurrency.EditValue;
            //SaveModel.CompanyLogoFileName = CompanyLogoSelectedFileName;

            SaveModel.Address1 = txtAddressLine1.Text;
            SaveModel.Address2 = txtAddressLine2.Text;
            SaveModel.Address3 = txtAddressLine3.Text;

            SaveModel.CityID = (int)lookupCity.EditValue;
            SaveModel.PIN    = txtPin.Text;

            SaveModel.Phone1    = txtPhone1.Text;
            SaveModel.Phone2    = txtPhoneNo2.Text;
            SaveModel.MobileNo1 = txtMobileNo1.Text;
            SaveModel.MobileNo2 = txtMobileNo2.Text;
            SaveModel.EMailID   = txtEMailID.Text;
            SaveModel.Website   = txtWebsite.Text;

            SaveModel.IncorporationNo   = txtIncorporationNo.Text;
            SaveModel.IncorporationDate = deIncorporationDate.DateTime;
            SaveModel.PINNo_RegNo       = txtPINNo.Text;
            SaveModel.NSSFNo            = txtNSSFNo.Text;
            SaveModel.NHIFNo            = txtNHIFNo.Text;
            SaveModel.PFNo       = txtPFNo.Text;
            SaveModel.SaccoRegNo = txtSACCORegNo.Text;

            SaveModel.Bank1_AccountNo  = txtBank1_AcNo.Text;
            SaveModel.Bank1_Name       = txtBank1_Name.Text;
            SaveModel.Bank1_BranchName = txtBank1_BranchName.Text;
            SaveModel.Bank1_Currency   = txtBank1_Currency.Text;

            SaveModel.Bank2_AccountNo  = txtBank2_AcNo.Text;
            SaveModel.Bank2_Name       = txtBank2_Name.Text;
            SaveModel.Bank2_BranchName = txtBank2_BranchName.Text;
            SaveModel.Bank2_Currency   = txtBank2_Currency.Text;

            SaveModel.Bank3_AccountNo  = txtBank3_AcNo.Text;
            SaveModel.Bank3_Name       = txtBank3_Name.Text;
            SaveModel.Bank3_BranchName = txtBank3_BranchName.Text;
            SaveModel.Bank3_Currency   = txtBank3_Currency.Text;

            PayRollMonth.PayrollMonthStartDate = payRollStartDate.DateTime;
            PayRollMonth.PayrollMonthEndDate   = payRollEndDate.DateTime;
            PayRollMonth.PayrollMonthName      = payRollEndDate.DateTime.ToString("MMMM-yyyy"); //this is not user selectable
            PayRollMonth.PayrollMonth          = payRollEndDate.DateTime.Date;

            long?CopySettingsFromCompanyID = null;

            //if(lcgSettings.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            //{
            //    CopySettingsFromCompanyID = (long?)lookupCopySettingFromCompany.EditValue;
            //}

            /// In Editing if logo image is cleared
            if (SaveModel.CompanyLogoFileName != null && CompanyLogoSelectedFileName == null)
            {
                try
                {
                    File.Delete(SaveModel.CompanyLogoFileName);
                }
                catch (Exception) { }
                SaveModel.CompanyLogoFileName = null;
            }

            Paras.SavingResult = DALObject.SaveNewRecord(SaveModel, CopySettingsFromCompanyID, PayRollMonth);


            /// CompanyLogoSelectedFileName != null -- it means user has changed logo image
            if (CompanyLogoSelectedFileName != null &&
                Paras.SavingResult.ExecutionResult == eExecutionResult.CommitedSucessfuly &&
                SaveModel.CompanyLogoFileName != CompanyLogoSelectedFileName)
            {
                string LogoImageCopiedFileName = null;

                // to seperate both process file copy and saving file name in db, so that if exception occures than we can get in which part the exception occured
                bool IsFileCopied = false;

                // Copieng Image File
                if (System.IO.File.Exists(CompanyLogoSelectedFileName))
                {
                    string ImageDirectory = null;
                    if (CommonProperties.LoginInfo.SoftwareSettings.DocumentLocation_EmployeeImage != null)
                    {
                        ImageDirectory = CommonProperties.LoginInfo.SoftwareSettings.DocumentLocation_EmployeeImage + @"\Logo\";
                    }
                    else
                    {
                        ImageDirectory = @"\..\IMAGES\Logo\";
                        System.IO.Directory.CreateDirectory(ImageDirectory);
                    }

                    if (!Directory.Exists(ImageDirectory))
                    {
                        Directory.CreateDirectory(ImageDirectory);
                    }

                    LogoImageCopiedFileName = ImageDirectory + "" + ((int)Paras.SavingResult.PrimeKeyValue).ToString() + System.IO.Path.GetExtension(CompanyLogoSelectedFileName);
                    IsFileCopied            = false;

                    try
                    {
                        if (File.Exists(LogoImageCopiedFileName))
                        {
                            File.Delete(LogoImageCopiedFileName);
                        }
                        File.Copy(CompanyLogoSelectedFileName, LogoImageCopiedFileName);
                        IsFileCopied = true;
                    }
                    catch (Exception ex)
                    {
                        ex = DAL.CommonFunctions.GetFinalError(ex);
                        Paras.SavingResult.MessageAfterSave = "Error while trying to copy log image file. Please check following error \r\n" + ex.Message;
                    }

                    // if file successfully copied only then save file name
                    if (IsFileCopied)
                    {
                        SavingResult res = DALObject.SaveLogoFileName((int)Paras.SavingResult.PrimeKeyValue, LogoImageCopiedFileName);
                        if (res.ExecutionResult != eExecutionResult.CommitedSucessfuly && res.ExecutionResult != eExecutionResult.NotExecutedYet)
                        {
                            if (res.ExecutionResult == eExecutionResult.ErrorWhileExecuting)
                            {
                                Paras.SavingResult.MessageAfterSave = "Error while saving logo image file name.\r\n" + (res.Exception != null ? res.Exception.Message : "");
                            }
                            else if (res.ExecutionResult == eExecutionResult.ValidationError)
                            {
                                Paras.SavingResult.MessageAfterSave = "Error while saving logo image file name \r\n" + res.ValidationError;
                            }
                        }
                    }
                }
            }

            base.SaveRecord(Paras);
        }