示例#1
0
        public Order executeOrder(OrderDTO orderdto, string UserName, string StripeClientId)
        {
            var order   = _orderFactory.Create(orderdto, UserName);
            var invoice = _invoiceFactory.Create(order);

            if (!_orderRepo.CheckOrder(order))
            {
                return(null);
            }
            order   = _orderRepo.addOrder(order);
            invoice = _invoiceRepo.addInvoice(invoice);
            var     succeded = _stripeService.executeCharge(order.OrderId, invoice.TotalSum, StripeClientId);
            Payment payment;

            if (succeded.Item2)
            {
                payment = _paymentFactory.Create(succeded.Item1, order.OrderId);
                _orderRepo.SetExecuted(order);
                _paymentRepo.addPayment(payment);
            }
            else
            {
                payment = _paymentFactory.Create(succeded.Item1, order.OrderId);
                _paymentRepo.addPayment(payment);
            }

            return(order);
            //create charge for stripe from order, complete it
            //create invoice from return of stripe call
        }
        public async Task AddAsync(InvoiceAddModel 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 customer = await _customerRepository.GetAsync(model.CustomerId);

            //if (customer.Discount != null)
            //{
            //    model.Discount = model.TotalAmount * customer.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 _invoiceRepository.getCount();

            //await _invoiceRepository.AddAsync(InvoiceFactory.Create(model, _userId, items));


            var invoice = InvoiceFactory.Create(model, _userId, count);
            await _invoiceRepository.AddAsync(invoice);

            await _unitOfWork.SaveChangesAsync();

            var transaction = TransactionFactory.CreateByInvoice(invoice);
            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.CreateByInvoiceItemsAndTax(invoice, 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.CreateByInvoiceItemsAndTax(invoice, id, amount);
                    await _transactionRepository.AddAsync(taxData);

                    await _unitOfWork.SaveChangesAsync();
                }
            }
        }