예제 #1
0
        public async Task <ActionResult <InvoiceSettingsDTO> > GetUserInvoiceSettings()
        {
            var currentUser = await _userManager.FindByNameAsync(User?.Identity?.Name);

            var invoiceSettings = await _context.UserInvoiceSettings.Where(settings => settings.UserId.Equals(currentUser.Id)).FirstOrDefaultAsync();

            var dtos = ModelToDto.InvoiceSettingsToDTO(invoiceSettings);

            return(dtos);
        }
        public async Task <ActionResult <InvoiceDataDTO> > PostInvoice(InvoiceDTO invoice)
        {
            var user = await _userManager.FindByNameAsync(User?.Identity?.Name);

            if (user == null)
            {
                return(Forbid());
            }

            var isInvoiceValid = !_context.Invoice.Any(i => i.TimesheetId.Equals(invoice.TimesheetId)) &&
                                 _context.Timesheet.Any(t =>
                                                        t.Id.Equals(invoice.TimesheetId) && t.State.Equals(Const.States.Closed));

            if (!isInvoiceValid)
            {
                return(BadRequest(new { Message = "Cannot create new invoice for provided timesheet!" }));
            }

            var invoiceTimesheet = await _context.Timesheet.FindAsync(invoice.TimesheetId);

            var invoiceObj = DtoToModel.DtoToInvoice(invoice);

            invoiceObj.InvoiceNumber = $"1/{invoiceTimesheet.Date.Month}/{invoiceTimesheet.Date.Year}";
            invoiceObj.UserId        = user.Id;

            await _context.Invoice.AddAsync(invoiceObj);

            await _context.SaveChangesAsync();

            var obj = _context.Invoice.Include(i => i.Timesheet).Include(i => i.Timesheet.Rows)
                      .First(i => i.Id.Equals(invoiceObj.Id));

            var invoiceSettings = await _context.UserInvoiceSettings.FirstAsync(settings => settings.UserId.Equals(user.Id));

            var invoiceSettingsDto = ModelToDto.InvoiceSettingsToDTO(invoiceSettings);

            var rateTypes = _context.RateTypes.ToList();

            var userRateAmounts = _context.UserRateAmount.Where(amount => amount.UserId.Equals(user.Id)).ToList();

            var dto = ModelToDto.InvoiceToDto(obj, invoiceSettingsDto, rateTypes, userRateAmounts);

            return(dto);
        }
예제 #3
0
        public void InvoiceSettingsToDTO_Should_ReturnValidDTO()
        {
            var mockUserInvoiceSettings = CreateMockUserInvoiceSettings();

            var sut = ModelToDto.InvoiceSettingsToDTO(mockUserInvoiceSettings);

            Assert.AreEqual(mockUserInvoiceSettings.BuyerAddressLine1, sut.BuyerAddressLine1);
            Assert.AreEqual(mockUserInvoiceSettings.BuyerAddressLine2, sut.BuyerAddressLine2);
            Assert.AreEqual(mockUserInvoiceSettings.BuyerName, sut.BuyerName);
            Assert.AreEqual(mockUserInvoiceSettings.BuyerPhone, sut.BuyerPhone);
            Assert.AreEqual(mockUserInvoiceSettings.BuyerTaxId, sut.BuyerTaxId);
            Assert.AreEqual(mockUserInvoiceSettings.IssuedBy, sut.IssuedBy);
            Assert.AreEqual(mockUserInvoiceSettings.SellerAccountNumber, sut.SellerAccountNumber);
            Assert.AreEqual(mockUserInvoiceSettings.SellerAddressLine1, sut.SellerAddressLine1);
            Assert.AreEqual(mockUserInvoiceSettings.SellerAddressLine2, sut.SellerAddressLine2);
            Assert.AreEqual(mockUserInvoiceSettings.SellerBankName, sut.SellerBankName);
            Assert.AreEqual(mockUserInvoiceSettings.SellerEmail, sut.SellerEmail);
            Assert.AreEqual(mockUserInvoiceSettings.SellerName, sut.SellerName);
            Assert.AreEqual(mockUserInvoiceSettings.SellerTaxId, sut.SellerTaxId);
        }