コード例 #1
0
        public async Task AddAsync(BillAddModel model)
        {
            //var items = (await _itemRepository.GetAsync(model.Items)).ToList();

            //model.TotalAmount = items.Sum(x => x.Rate);

            //model.Tax = items.Where(x => x.IsTaxable).Sum(x => x.Rate * x.SalesTax.TaxPercentage / 100);

            //var vendor = await _vendorRepository.GetAsync(model.VendorId);

            //if (vendor.Discount != null)
            //{
            //    model.Discount = model.TotalAmount * vendor.Discount / 100;
            //    model.TotalAmount = model.TotalAmount - (model.Discount ?? 0);
            //}

            //if (model.Tax != null)
            //{
            //    model.TotalAmount = model.TotalAmount + (model.Tax ?? 0);
            //}
            model.LineAmountSubTotal = model.Items.Sum(x => x.LineAmount);
            var count = await _repository.getCount();

            var bill = BillFactory.Create(model, _userId, count);
            await _repository.AddAsync(bill);

            await _unitOfWork.SaveChangesAsync();

            var transaction = TransactionFactory.CreateByBill(bill);
            await _transactionRepository.AddAsync(transaction);

            await _unitOfWork.SaveChangesAsync();

            var itemsList = (model.Items.GroupBy(l => l.BankAccountId, l => new { l.BankAccountId, l.LineAmount })
                             .Select(g => new { GroupId = g.Key, Values = g.ToList() })).ToList();

            foreach (var item in itemsList)
            {
                var id     = item.GroupId;
                var amount = item.Values.Sum(x => x.LineAmount);

                var itemsData = TransactionFactory.CreateByBillItemsAndTax(bill, id, amount);
                await _transactionRepository.AddAsync(itemsData);

                await _unitOfWork.SaveChangesAsync();
            }

            var taxlistList = (model.Items.GroupBy(l => l.TaxBankAccountId, l => new { l.TaxBankAccountId, l.TaxPrice })
                               .Select(g => new { GroupId = g.Key, Values = g.ToList() })).ToList();

            foreach (var tax in taxlistList)
            {
                if (tax.GroupId > 0)
                {
                    var id     = tax.GroupId;
                    var amount = tax.Values.Sum(x => x.TaxPrice);

                    var taxData = TransactionFactory.CreateByBillItemsAndTax(bill, id, amount);
                    await _transactionRepository.AddAsync(taxData);

                    await _unitOfWork.SaveChangesAsync();
                }
            }
        }