public async Task <BillingImportResult> ImportAsync(BillingImportItems items, CancellationToken token)
        {
            var details = (await importerSettingDetailQueryProcessor.GetAsync(new ImporterSetting {
                Id = items.ImporterSettingId
            }, token)).ToList();

            var billingImport = items.Items;
            var CompanyId     = items.CompanyId;
            var LoginUserId   = items.LoginUserId;
            var newCustomers  = billingImport.Where(x => x.AutoCreationCustomerFlag == 1)
                                .GroupBy(x => x.CustomerCode).ToDictionary(x => x.Key);

            using (var scope = transactionScopeBuilder.Create())
            {
                foreach (var key in newCustomers.Keys)
                {
                    var customer = await PrepareDataForCustomerAsync(newCustomers[key].First(), items.CompanyId, details, LoginUserId, token);

                    var res = await addCustomerQueryProcessor.SaveAsync(customer, token : token);

                    foreach (var x in newCustomers[key])
                    {
                        x.CustomerId = res.Id;
                    }
                }

                await billingImporterCodeToIdSolveProcessor.SolveAsync(CompanyId, billingImport, token);

                var billingCategories = await GetBillingCategoryDictionary(billingImport, token);

                if (details.Any(x => x.DoOverwrite == 1))
                {
                    var toDeleteIds = new HashSet <long>();
                    foreach (var option in billingImport.Select(x => PrepareDataForOverWrite(x, items.CompanyId, details)).Where(x => x != null))
                    {
                        var ids = (await billingSearchForImportQueryProcessor.GetItemsForImportAsync(option, token)).ToArray();
                        foreach (var id in ids)
                        {
                            if (!toDeleteIds.Contains(id))
                            {
                                toDeleteIds.Add(id);
                            }
                        }
                    }
                    if (toDeleteIds.Count > 0)
                    {
                        await updatebillingQueryProcessor.UpdateDeleteAtAsync(toDeleteIds, LoginUserId, token);
                    }
                }

                foreach (var importData in billingImport)
                {
                    var customerId = importData.CustomerId;
                    if (importData.AutoCreationCustomerFlag != 1)
                    {
                        /* request no. 419: 最後に更新した値が有効となる */
                        if (!string.IsNullOrEmpty(importData.ExclusiveBranchCode) &&
                            !string.IsNullOrEmpty(importData.ExclusiveBankCode) &&
                            !string.IsNullOrEmpty(importData.ExclusiveVirtualBranchCode) &&
                            !string.IsNullOrEmpty(importData.ExclusiveAccountNumber))
                        {
                            var customer = new Customer {
                                CompanyId              = CompanyId,
                                Code                   = importData.CustomerCode,
                                ExclusiveBankCode      = importData.ExclusiveBankCode,
                                ExclusiveBranchCode    = importData.ExclusiveBranchCode,
                                ExclusiveAccountNumber = importData.ExclusiveVirtualBranchCode + importData.ExclusiveAccountNumber,
                                UpdateBy               = LoginUserId,
                            };
                            await updateCustomerQueryProcessor.UpdateForBilingImportAsync(customer, token);
                        }
                    }

                    var billing           = importData.ConvertToBilling(LoginUserId);
                    var billingSaveResult = await billingSaveProcessor.SaveAsync(billing, token);

                    var billingId = billingSaveResult.Id;

                    var category = billingCategories[billing.BillingCategoryId];
                    if (category.UseLongTermAdvanceReceived == 1)
                    {
                        if (importData.RegisterContractInAdvance == 1)
                        {
                            var contract = (await billingDivisionContractQueryProcessor.GetItemsAsync(
                                                new BillingDivisionContractSearch {
                                CompanyId = billing.CompanyId,
                                CustomerId = billing.CustomerId,
                                ContractNumber = importData.ContractNumber,
                            }, token)).FirstOrDefault();
                            if (contract == null || contract.BillingId.HasValue)
                            {
                                return new BillingImportResult
                                       {
                                           ProcessResult = new ProcessResult {
                                               Result = false
                                           },
                                       }
                            }
                            ;
                            contract.BillingId = billingId;
                            await updateBillingDivisionContractQueryProcessor.UpdateBillingAsync(contract, token);
                        }
                        else
                        {
                            var contract = await PrepareDataForBillingDivisionContract(importData, billingId, CompanyId, token);

                            if (contract == null)
                            {
                                return new BillingImportResult
                                       {
                                           ProcessResult = new ProcessResult {
                                               Result = false
                                           },
                                       }
                            }
                            ;
                            contract.CreateBy  = LoginUserId;
                            contract.UpdateBy  = LoginUserId;
                            contract.CompanyId = CompanyId;
                            await addBillingDivisionContractQueryProcessor.SaveAsync(contract, token);
                        }
                    }

                    if (importData.UseDiscount == 1)
                    {
                        var discount = await PrepareDataForBillingDiscount(importData, billingId, CompanyId, customerId, token);

                        if (discount.Count > 0)
                        {
                            foreach (var saveDiscount in discount)
                            {
                                await addBillingDiscountQueryProcessor.SaveAsync(saveDiscount, token);
                            }
                        }
                    }
                }

                scope.Complete();
            }

            return(new BillingImportResult
            {
                ProcessResult = new ProcessResult {
                    Result = true
                },
            });
        }
Example #2
0
        public async Task <IEnumerable <Billing> > SaveItemsAsync(IEnumerable <Billing> billings, CancellationToken token = default(CancellationToken))
        {
            var source = billings.ToArray();
            var result = new List <Billing>();

            using (var scope = transactionScopeBuilder.Create())
            {
                BillingInput billingInput = null;
                var          index        = 0;
                if (source[0].BillingInputId == null && source[0].Id == 0)
                {
                    billingInput = await addBillingInputQueryProcessor.AddAsync(token);
                }

                foreach (var billing in source)
                {
                    if (billingInput != null)
                    {
                        billing.BillingInputId = billingInput.Id;
                    }

                    result.Add(await SaveInnerAsync(billing));

                    if (billing.UseDiscount)
                    {
                        var discounts = (await customerDiscountQueryProcessor.GetAsync(billing.CustomerId, token)).ToList();

                        if (discounts.Count > 0)
                        {
                            var discount  = new BillingDiscount();
                            var billingId = result[index].Id;
                            discount.BillingId = billingId;
                            await deleteBillingDiscountQueryProcessor.DeleteAsync(billingId, token);

                            for (var i = 0; i < discounts.Count; i++)
                            {
                                var customerDiscount = discounts[i];
                                discount.DiscountType   = customerDiscount.Sequence;
                                discount.DiscountAmount = customerDiscount.GetRoundingValue(billing.BillingAmount, billing.CurrencyPrecision);
                                await addBillingDiscountQueryProcessor.SaveAsync(discount, token);
                            }
                        }
                    }
                    else if (!billing.UseDiscount && billing.BillingDiscountId != null)
                    {
                        var billingId = result[index].Id;
                        await deleteBillingDiscountQueryProcessor.DeleteAsync(billingId, token);
                    }

                    /* query から見直し updateBilling の 修正が必要 */
                    var contract = new BillingDivisionContract();
                    contract.BillingId      = result[index].Id;
                    contract.CreateBy       = billing.CreateBy;
                    contract.UpdateBy       = billing.UpdateBy;
                    contract.CompanyId      = billing.CompanyId;
                    contract.CustomerId     = billing.CustomerId;
                    contract.ContractNumber = billing.ContractNumber;

                    if (billing.BillingDivisionContract == 1)
                    {
                        await updateBillingDivisionContractQueryProcessor.UpdateBillingAsync(contract, token);
                    }
                    else if (billing.BillingDivisionContract == 2)
                    {
                        await deleteBillingDivisionContractQueryProcessor.DeleteWithBillingIdAsync(billing.Id, token);
                    }
                    else if (billing.BillingDivisionContract == 3)
                    {
                        var newContractNo = await billingDivisionContractQueryProcessor.GetNewContractNumberAsync(contract.CompanyId, token);

                        contract.ContractNumber = newContractNo.ToString();
                        var setting = await billingDivisionSettingQueryProcessor.GetAsync(billing.CompanyId, token);

                        contract.FirstDateType        = setting.FirstDateType;
                        contract.Monthly              = setting.Monthly;
                        contract.BasisDay             = setting.BasisDay;
                        contract.DivisionCount        = setting.DivisionCount;
                        contract.RoundingMode         = setting.RoundingMode;
                        contract.RemainsApportionment = setting.RemainsApportionment;
                        contract.Comfirm              = 0;
                        contract.CancelDate           = null;
                        await addBillingDivisionContractQueryProcessor.SaveAsync(contract, token);
                    }
                    index++;
                }
                scope.Complete();
            }
            return(result);
        }