예제 #1
0
        public async Task <IActionResult> Create(InvoiceModel invoice)
        {
            var model       = Mapper.Map <Invoice>(invoice);
            var changesMade = await _repository.AddAsync(model);

            return(Ok(changesMade));
        }
        public async Task <CommandResult> Handle(AddInvoiceCommand request, CancellationToken cancellationToken)
        {
            var invoice = Invoice.Create(request.InvoiceNumber, request.InvoiceDate, request.CompanyCode, request.ReferenceId);

            try
            {
                //await _storage.UploadFile(new MemoryStream(Encoding.UTF8.GetBytes(request.ReferenceId)), "Test.xml", "test");


                var invoiceDataReference = InvoiceDataReference.Create(invoice, Guid.NewGuid());

                await _invoiceRepository.AddAsync(invoice);

                await _invoiceDataReferenceRepository.AddAsync(invoiceDataReference);

                await _invoiceRepository.UnitOfWork.SaveEntitiesAsync();

                request.InvoiceData.InvoiceId = invoice.Id;
                await _invoiceDataRepository.AddAsync(request.InvoiceData);

                invoice.Change(request.InvoiceData.Id, invoice.CurrentStatus);
                invoiceDataReference.Change(request.InvoiceData.Id);

                await _invoiceRepository.UnitOfWork.SaveEntitiesAsync();
            }
            catch (Exception ex)
            {
                return(CommandResult.Fail(ex.ToString()));
            }

            return(CommandResult.Ok($"Invoice has been created successuful: [ {invoice.Id} ]"));
        }
예제 #3
0
        public async Task <InvoiceViewModel> CreateInvoice(InvoiceViewModel model)
        {
            var result = await _repository.AddAsync(_mapper.Map <InvoiceModel>(model));

            if (result != null)
            {
                return(_mapper.Map <InvoiceViewModel>(result));
            }
            return(null);
        }
        public async Task <int> AddInvoiceAsync(string filePath, CancellationToken cancellationToken)
        {
            var validator = new StringValidator();
            await validator.ValidateAndThrowAsync(filePath, null, cancellationToken);

            var invoiceToAdd = new Invoice()
            {
                URLPath = filePath
            };

            var result = await _invoiceRepository.AddAsync(invoiceToAdd, cancellationToken);

            return(result.Id);
        }
예제 #5
0
        public async Task <InvoiceViewModel> AddInvoiceAsync(InvoiceViewModel newInvoiceViewModel, CancellationToken ct = default(CancellationToken))
        {
            var invoice = new Invoice
            {
                CustomerId        = newInvoiceViewModel.CustomerId,
                InvoiceDate       = newInvoiceViewModel.InvoiceDate,
                BillingAddress    = newInvoiceViewModel.BillingAddress,
                BillingCity       = newInvoiceViewModel.BillingCity,
                BillingState      = newInvoiceViewModel.BillingState,
                BillingCountry    = newInvoiceViewModel.BillingCountry,
                BillingPostalCode = newInvoiceViewModel.BillingPostalCode,
                Total             = newInvoiceViewModel.Total
            };

            invoice = await _invoiceRepository.AddAsync(invoice, ct);

            newInvoiceViewModel.InvoiceId = invoice.InvoiceId;
            return(newInvoiceViewModel);
        }
        public async Task <ActionResult <InvoiceItem> > Post([FromBody] InvoiceItem value)
        {
            try
            {
                if (value == null) //TODO: add validators
                {
                    return(new BadRequestResult());
                }

                var newInvoice     = _invoiceMapper.Map(value);
                var updatedInvoice = await _invoiceRepository.AddAsync(newInvoice);

                var createdInvoiceItem = _invoiceItemMapper.Map(updatedInvoice);
                return(new OkObjectResult(createdInvoiceItem));
            }
            catch (Exception e) //TODO middleware
            {
                return(_errorService.Catch(e));
            }
        }
예제 #7
0
        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();
                }
            }
        }
예제 #8
0
        public async Task <Invoice> InvoiceCreateNewAsync(Invoice invoice)
        {
            await _invoiceRepository.AddAsync(invoice);

            return(invoice);
        }