public async Task <ImportResult> ImportCommissions(ScopeOptions scope, Guid commissionStatementId, IEnumerable <ImportCommission> importData)
        {
            var importResult = new ImportResult();

            CommissionsToInsert      = new List <CommissionEntity>();
            CommissionErrorsToInsert = new List <CommissionErrorEntity>();

            //Scope check
            var queryOptions = new CommissionStatementQueryOptions(scope, "", "", 0, 0);

            queryOptions.CommissionStatementId = commissionStatementId;
            var statements = await _commissionStatementService.GetCommissionStatements(queryOptions);

            if (!statements.Items.Any())
            {
                return(importResult);
            }

            var statement = statements.Items.Single();

            var commissionTypes = await _commissionLookupService.GetCommissionTypes();

            var commissionTypesDictionary = BuildCommissionTypesDictionary(commissionTypes);
            var company = await _lookupService.GetCompany(statement.CompanyId);

            var policyQueryOptions = new PolicyQueryOptions(scope, "", "", 0, 0);

            policyQueryOptions.CompanyId.Add(statement.CompanyId);
            var policies         = (await _policyService.GetPolicies(policyQueryOptions)).Items.ToList();
            var policyDictionary = BuildPolicyDictionary(policies, company.CommissionPolicyNumberPrefixes.ToList());

            var commissionSplitRulesQueryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
            var commissionSplitRules             = (await _commissionSplitService.GetCommissionSplitRules(commissionSplitRulesQueryOptions)).Items.ToList();

            var commissionSplitRulePolicyQueryOptions = new CommissionSplitRulePolicyQueryOptions(scope, "", "", 0, 0);
            var commissionSplitRulePolicies           = (await _commissionSplitRulePolicyService.GetCommissionSplitRulePolicies(commissionSplitRulePolicyQueryOptions)).Items.ToList();

            foreach (var data in importData)
            {
                var result = ImportCommission(scope, statement, data, policyDictionary, commissionTypesDictionary, commissionSplitRules, commissionSplitRulePolicies);

                importResult.Results.Add(result);

                if (data.CommissionTypeCode == CommissionType.COMMISSION_TYPE_UNKNOWN_CODE)
                {
                    importResult.AddUnknownCommissionTypeValue(data.CommissionTypeValue);
                }
            }

            if (CommissionsToInsert.Any())
            {
                await _bulkActions.BulkInsertCommissionsAsync(_context, CommissionsToInsert);
            }

            if (CommissionErrorsToInsert.Any())
            {
                await _bulkActions.BulkInsertCommissionErrorsAsync(_context, CommissionErrorsToInsert);
            }

            await _auditService.InsertAuditLog(scope, "Import", "Commission", statement.Id,
                                               new
            {
                commissionStatementId = commissionStatementId,
                importCount           = importResult.ImportCount,
                errorCount            = importResult.ErrorCount,
                errors = importResult.Results.Where(r => !r.Success).ToList()
            }
                                               );

            return(importResult);
        }
        public async Task <Config> GetDefaultConfig()
        {
            var sheetConfig = new SheetConfig()
            {
                //No header
                HeaderIdentifier = new Identifier()
                {
                    Column = "",
                    Value  = ""
                },
                Fields = new List <Field>()
                {
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.PolicyNumber), Column = "A"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.AmountIncludingVAT), Column = "B"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.VAT), Column = "C"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.LastName), Column = "E"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.DateOfBirth), Column = "F"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.FirstName), Column = "G"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.IdNumber), Column = "H"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.Initials), Column = "I"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.FullName), Column = "J"
                    },
                    new Field()
                    {
                        Name = Enum.GetName(typeof(FieldNames), FieldNames.BrokerFullName), Column = "K"
                    }
                },
                CommissionTypes = new CommissionTypes()
                {
                    MappingTemplate           = "D",
                    DefaultCommissionTypeCode = "unknown",
                    Types = new List <CommissionType>()
                }
            };

            var commissionTypes = await _lookupService.GetCommissionTypes();

            sheetConfig.CommissionTypes.Types = commissionTypes
                                                .Select(c => new CommissionType()
            {
                CommissionTypeCode = c.Code,
                Value = c.Code
            }
                                                        )
                                                .ToList();

            var sheet = new Sheet();

            sheet.Position = 1;
            sheet.Config   = sheetConfig;

            var config = new Config();

            config.Sheets = new List <Sheet>()
            {
                sheet
            };

            return(config);
        }