Exemple #1
0
 public Task <BillingInput> SaveAsync(CancellationToken token = default(CancellationToken))
 => addBillingInputQueryProcessor.AddAsync(token);
        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);
        }