/// <summary>
        /// Loads a rule object.
        /// </summary>
        /// <param name="ruleID">The rule of of the rule that should be loaded.</param>
        /// <returns>The loaded rule.</returns>
        public Rule LoadRule(string ruleID)
        {
            //  Load searched rule from file and get all elements
            IEnumerable<XElement> rules = LoadData(_fileNameRule, "rule", new KeyValuePair<string, string>("id", "_" + ruleID));

            //  new rule
            Rule rule = new Rule();
            rule.RuleID = ruleID;
            rule.RuleElements = new List<RuleElement>();

            if (rules.Elements().Count() > 0)
            {
                rules = rules.Elements();

                rule.RuleElements = this.GetRuleElements(rules);
            }

            return rule;
        }
Example #2
0
        /// <summary>
        /// Considers rules. When there are rules that match to bank ident or account number, those rules take effect.
        /// </summary>
        /// <param name="bank">The bank, for which the rules depend to.</param>
        /// <param name="rule">The rules.</param>
        /// <param name="country">The country the rules depend to.</param>
        /// <param name="accountNumber">The account number for which the rules should be checked for.</param>
        /// <returns>The generated result.</returns>
        /// <exception cref="IbanException">
        /// <list type="table">
        ///     <item>
        ///         <term><see cref="RuleType"/></term>
        ///         <description>NoCalculation</description>
        ///         <description>When iban calculation is not allowed.</description>
        ///     </item>
        /// </list>
        /// </exception>
        private IbanBic ConsiderRule(Bank bank, Rule rule, Country country, string accountNumber)
        {
            string temp_bank_ident = bank.BankIdentification;
            string temp_account_number = accountNumber;
            string temp_bic = bank.BIC.Bic;
            Iban iban = new Iban();
            IbanBic result = new IbanBic();

            //  Check if rule is 000000
            if (rule.RuleID.Equals("000000"))
            {
                iban.Bank = new Bank();
                iban.Bank.BankIdentification = bank.BankIdentification;
                iban.AccountNumber = accountNumber;

                result.IBAN = iban;
                result.BIC = new BankIdentifierCode() { Bic = temp_bic };

                return result;
            }

            //  Check if rule is 000100
            if (rule.RuleID.Equals("000100"))
                throw new IbanException(IbanExceptionType.NoCalculation);

            //  delete leading 0s in account number
            temp_account_number = temp_account_number.TrimStart(new char[] { '0' });

            //  consider each possible rule
            //  1. No Calculation
            if ((from count in rule.RuleElements
                 where count.RuleType == RuleType.No_Calculation
                 && (from bi in count.Childs
                     where bi.RuleType == RuleType.Kto_Number_Range
                     && _regexpHelper.RegexpMatch(bi.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident)
                     && _regexpHelper.RegexpMatch(bi.Data, temp_account_number)
                     select bi).Count() == 1
                 select count).Count() > 0)
            {
                //  No calculation
                throw new IbanException(IbanExceptionType.NoCalculation);
            }

            //  2. Kto Mapping
            if ((from count in rule.RuleElements
                    where count.RuleType == RuleType.Mappings_Kto
                    && _regexpHelper.RegexpMatch(count.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident)
                    && (from mapp in count.Childs
                        where mapp.RuleType == RuleType.Mapping
                        && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_account_number)
                        select mapp).Count() == 1
                    select count).Count() == 1)
            {
                var kto_mapping = (from kto in rule.RuleElements
                                    where kto.RuleType == RuleType.Mappings_Kto
                                    && _regexpHelper.RegexpMatch(kto.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident)
                                    && (from mapp in kto.Childs
                                        where mapp.RuleType == RuleType.Mapping
                                        && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_account_number)
                                        select mapp).Count() == 1
                                    select kto).First();

                //  account number
                temp_account_number = (from mapp in kto_mapping.Childs
                                        where mapp.RuleType == RuleType.Mapping
                                        && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_account_number)
                                        select mapp.Data).First();

                //  should also bank ident be mapped?
                if (kto_mapping.Attributes.Where(t => t.Key.Equals("blz_new")).Select(t => t.Value).Count() == 1)
                {
                    //  there should also be a bank ident mapping
                    temp_bank_ident = kto_mapping.Attributes.Where(t => t.Key.Equals("blz_new")).Select(t => t.Value).First();

                    //  if bank ident has changed, get new BIC
                    using (Bank bank_temp = _dataService.LoadBank(temp_bank_ident))
                    {
                        temp_bic = bank_temp.BIC.Bic;
                    }
                }
            }

            //  3. KtoKr Mapping
            if ((from count in rule.RuleElements
                    where count.RuleType == RuleType.Mappings_KtoKr
                    && _regexpHelper.RegexpMatch(count.Attributes.Where(t => t.Key.Equals("kto")).Select(t => t.Value).First(), temp_account_number)
                    && (from mapp in count.Childs
                        where mapp.RuleType == RuleType.Mapping
                        && mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First().Equals(temp_account_number.Substring(0, 3))
                        select mapp).Count() == 1
                    select count).Count() == 1)
            {
                var ktokr_mapping = (from ktokr in rule.RuleElements
                                        where ktokr.RuleType == RuleType.Mappings_KtoKr
                                        && _regexpHelper.RegexpMatch(ktokr.Attributes.Where(t => t.Key.Equals("kto")).Select(t => t.Value).First(), temp_account_number)
                                        && (from mapp in ktokr.Childs
                                            where mapp.RuleType == RuleType.Mapping
                                            && mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First().Equals(temp_account_number.Substring(0, 3))
                                            select mapp).Count() == 1
                                        select ktokr).First();

                temp_bank_ident = (from mapp in ktokr_mapping.Childs
                                    where mapp.RuleType == RuleType.Mapping
                                    && mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First().Equals(temp_account_number.Substring(0, 3))
                                    select mapp.Data).First();

                //  if bank ident has changed, get new BIC
                using (Bank bank_temp = _dataService.LoadBank(temp_bank_ident))
                {
                    temp_bic = bank_temp.BIC.Bic;
                }
            }

            //  4. Bank-Ident Mapping
            if ((from count in rule.RuleElements
                    where count.RuleType == RuleType.Mappings_Blz
                    && (from mapp in count.Childs
                        where mapp.RuleType == RuleType.Mapping
                        && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_bank_ident)
                        select mapp).Count() == 1
                    select count).Count() == 1)
            {
                var bankident_mapping = (from bi in rule.RuleElements
                                            where bi.RuleType == RuleType.Mappings_Blz
                                                && (from mapp in bi.Childs
                                                    where mapp.RuleType == RuleType.Mapping
                                                    && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_bank_ident)
                                                    select mapp).Count() == 1
                                            select bi).First();

                temp_bank_ident = (from mapp in bankident_mapping.Childs
                                    where mapp.RuleType == RuleType.Mapping
                                    && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_bank_ident)
                                    select mapp.Data).First();

                //  if bank ident has changed, get new BIC
                using (Bank bank_temp = _dataService.LoadBank(temp_bank_ident))
                {
                    temp_bic = bank_temp.BIC.Bic;
                }
            }

            //  5. Kto Mod
            if ((from count in rule.RuleElements
                    where count.RuleType == RuleType.Modification_Kto
                    && (from mapp in count.Childs
                        where mapp.RuleType == RuleType.Modification
                        && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident)
                        && _regexpHelper.RegexpMatch(mapp.Data, temp_account_number)
                        select mapp).Count() == 1
                    select count).Count() == 1)
            {
                var kto_mod = (from kto in rule.RuleElements
                                where kto.RuleType == RuleType.Modification_Kto
                                && (from mapp in kto.Childs
                                    where mapp.RuleType == RuleType.Modification
                                    && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident)
                                    && _regexpHelper.RegexpMatch(mapp.Data, temp_account_number)
                                    select mapp).Count() == 1
                                select kto).First();

                var mod = (from mapp in kto_mod.Childs
                            where mapp.RuleType == RuleType.Modification
                            && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident)
                            && _regexpHelper.RegexpMatch(mapp.Data, temp_account_number)
                            select mapp).First();

                _regexpHelper.RegexpMatch(mod.Data, temp_account_number, out temp_account_number);
            }

            //  6. BIC mapping
            if ((from count in rule.RuleElements
                 where count.RuleType == RuleType.Mappings_Bic
                 && (from mapp in count.Childs
                     where mapp.RuleType == RuleType.Mapping
                     && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), bank.BankIdentification)
                     select mapp).Count() == 1
                 select count).Count() == 1)
            {
                var mapp_bic = (from mbic in rule.RuleElements
                            where mbic.RuleType == RuleType.Mappings_Bic
                            && (from mapp in mbic.Childs
                                where mapp.RuleType == RuleType.Mapping
                                && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), bank.BankIdentification)
                                select mapp).Count() == 1
                            select mbic).First();

                temp_bic = (from bic in mapp_bic.Childs
                            where bic.RuleType == RuleType.Mapping
                             && _regexpHelper.RegexpMatch(bic.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), bank.BankIdentification)
                            select bic.Data).First();
            }

            iban.AccountNumber = temp_account_number;

            //  if bank ident changed, reload bank
            if (!temp_bank_ident.Equals(bank.BankIdentification))
            {
                bank = _dataService.LoadBank(temp_bank_ident);
            }

            iban.Bank = bank;

            result.IBAN = iban;
            result.BIC = new BankIdentifierCode() { Bic = temp_bic };

            return result;
        }