public void DtoToInvoice_Should_ReturnValidInvoiceObject()
        {
            var invoiceDto = CreateMockInvoiceDTO();

            var sut = DtoToModel.DtoToInvoice(invoiceDto);

            Assert.AreEqual(0, sut.Id);
            Assert.AreEqual(1, sut.TimesheetId);
            Assert.AreEqual(DateTime.Today, sut.GeneratedDate);
            Assert.AreEqual(Array.Empty <byte>(), sut.FileArray);
        }
        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);
        }