Beispiel #1
0
 public Task <IEnumerable <Billing> > GetByBillingInputIdAsync(long BillingInputId, CancellationToken token = default(CancellationToken))
 => billingQueryProcessor.GetByBillingInputIdAsync(BillingInputId, token);
Beispiel #2
0
        public async Task <IEnumerable <Billing> > SaveAsync(IEnumerable <Billing> billings, CancellationToken token = default(CancellationToken))
        {
            var results = new List <Billing>();

            using (var scope = transactionScopeBuilder.Create())
            {
                var source     = billings.ToArray();
                var first      = source.First();
                var userId     = first.UpdateBy;
                var appControl = await applicationControlQueryProcessor.GetAsync(first.CompanyId, token);

                List <CustomerDiscount> discounts = null;
                var useDiscount = appControl.UseDiscount == 1;
                if (useDiscount)
                {
                    discounts = (await customerDiscountQueryProcessor.GetAsync(first.CustomerId, token)).ToList();
                }
                var useLongTermAdv             = appControl.UseLongTermAdvanceReceived == 1;
                var contractRegistered         = appControl.RegisterContractInAdvance == 1;
                BillingDivisionSetting setting = null;
                if (useLongTermAdv && !contractRegistered)
                {
                    setting = await billingDivisionSettingQueryProcessor.GetAsync(first.CompanyId, token);
                }
                var inputId = first.BillingInputId;
                var isInput = first.InputType == (int)BillingInputType.BillingInput ||
                              first.InputType == (int)BillingInputType.PeriodicBilling;
                var requireInputId = isInput && !inputId.HasValue;
                if (requireInputId)
                {
                    inputId = (await addBillingInputQueryProcessor.AddAsync(token)).Id;
                    foreach (var item in source)
                    {
                        item.BillingInputId = inputId;
                    }
                }
                else if (isInput && inputId.HasValue)
                {
                    var registeredIds = (await billingQueryProcessor.GetByBillingInputIdAsync(inputId.Value)).Select(x => x.Id).ToList();
                    var deleteSource  = new BillingDeleteSource {
                        Ids         = registeredIds.Where(x => !source.Any(y => y.Id == x)).ToArray(),
                        CompanyId   = first.CompanyId,
                        LoginUserId = userId,
                    };
                    await billingProcessor.DeleteAsync(deleteSource, token);
                }
                var requireDiscount = source.Any(x => x.UseDiscount);

                foreach (var item in source)
                {
                    var result = await SaveInnerAsync(item, token);

                    if (useDiscount)
                    {
                        if (!item.UseDiscount && item.BillingDiscountId.HasValue)
                        {
                            await deleteBillingDiscountQueryProcessor.DeleteAsync(result.Id, token);
                        }
                        if (item.UseDiscount && discounts.Any())
                        {
                            await deleteBillingDiscountQueryProcessor.DeleteAsync(result.Id, token);

                            var billingDiscount = new BillingDiscount {
                                BillingId = result.Id
                            };
                            foreach (var x in discounts)
                            {
                                billingDiscount.DiscountType   = x.Sequence;
                                billingDiscount.DiscountAmount = x.GetRoundingValue(item.BillingAmount, item.CurrencyPrecision);
                                await addBillingDiscountQueryProcessor.SaveAsync(billingDiscount, token);
                            }
                        }
                    }
                    if (useLongTermAdv && item.BillingDiscountId > 0)
                    {
                        var contract = new BillingDivisionContract
                        {
                            BillingId      = result.Id,
                            CreateBy       = userId,
                            UpdateBy       = userId,
                            CompanyId      = item.CompanyId,
                            CustomerId     = item.CustomerId,
                            ContractNumber = item.ContractNumber,
                        };
                        if (item.BillingDivisionContract == 1)
                        {
                            await updateBillingDivisionContractQueryProcessor.UpdateBillingAsync(contract, token);
                        }
                        else if (item.BillingDivisionContract == 2)
                        {
                            await deleteBillingDivisionContractQueryProcessor.DeleteWithBillingIdAsync(result.Id, token);
                        }
                        else if (item.BillingDivisionContract == 3)
                        {
                            var newNumber = await billingDivisionContractQueryProcessor.GetNewContractNumberAsync(contract.CompanyId, token);

                            contract.ContractNumber       = newNumber.ToString();
                            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;
                            await addBillingDivisionContractQueryProcessor.SaveAsync(contract, token);
                        }
                    }
                    results.Add(result);
                }
                scope.Complete();
            }

            return(results);
        }