Esempio n. 1
0
        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
                },
            });
        }
Esempio n. 2
0
 public async Task <IEnumerable <BillingDivisionContract> > GetAsync(BillingDivisionContractSearch option, CancellationToken token = default(CancellationToken))
 => await billingDivisionContractQueryProcessor.GetItemsAsync(option, token);