Search in sources :

Example 1 with InvoiceItemDto

use of org.estatio.canonical.invoice.v1.InvoiceItemDto in project estatio by estatio.

the class InvoiceItemForLeaseDtoFactory method newDto.

@Programmatic
public InvoiceItemDto newDto(final InvoiceItem item) {
    InvoiceItemDto dto = new InvoiceItemDto();
    if (item instanceof InvoiceItemForLease) {
        InvoiceItemForLease invoiceItemForLease = (InvoiceItemForLease) item;
        final Lease lease = invoiceItemForLease.getLease();
        if (lease != null) {
            dto.setAgreementReference(lease.getReference());
        }
        final FixedAsset fixedAsset = invoiceItemForLease.getFixedAsset();
        if (fixedAsset != null) {
            dto.setFixedAssetReference(fixedAsset.getReference());
            dto.setFixedAssetExternalReference(fixedAsset.getExternalReference());
        }
    }
    final Charge charge = item.getCharge();
    dto.setChargeReference(charge.getReference());
    dto.setChargeDescription(charge.getDescription());
    dto.setChargeExternalReference(charge.getExternalReference());
    dto.setChargeName(charge.getName());
    final ChargeGroup group = charge.getGroup();
    dto.setChargeGroupReference(group.getReference());
    dto.setChargeGroupName(group.getName());
    final Tax tax = item.getTax();
    final TaxRate rate = item.getTaxRate();
    dto.setTaxReference(tax.getReference());
    dto.setTaxName(tax.getName());
    dto.setTaxDescription(tax.getDescription());
    dto.setTaxExternalReference(rate == null || rate.getExternalReference() == null ? tax.getExternalReference() : rate.getExternalReference());
    dto.setNetAmount(item.getNetAmount());
    dto.setGrossAmount(item.getGrossAmount());
    dto.setVatAmount(item.getVatAmount());
    dto.setDescription(item.getDescription());
    dto.setStartDate(asXMLGregorianCalendar(item.getStartDate()));
    dto.setEndDate(asXMLGregorianCalendar(item.getEndDate()));
    dto.setEffectiveStartDate(asXMLGregorianCalendar(firstNonNull(item.getEffectiveStartDate(), item.getStartDate())));
    dto.setEffectiveEndDate(asXMLGregorianCalendar(firstNonNull(item.getEffectiveEndDate(), item.getEndDate())));
    if (item instanceof InvoiceItemForLease) {
        final InvoiceItemForLease invoiceItemForLease = (InvoiceItemForLease) item;
        final InvoiceForLease invoice = (InvoiceForLease) invoiceItemForLease.getInvoice();
        final Lease leaseIfAny = invoice.getLease();
        if (leaseIfAny != null) {
            final SortedSet<Occupancy> occupancies = leaseIfAny.getOccupancies();
            if (!occupancies.isEmpty()) {
                // final Optional<Occupancy> occupancyIfAny =
                // occupancies.stream().filter(x -> x.getInterval().overlaps(item.getEffectiveInterval())).findFirst();
                final Optional<Occupancy> occupancyIfAny = Optional.ofNullable(occupancies.first());
                if (occupancyIfAny.isPresent()) {
                    final Occupancy occupancy = occupancyIfAny.orElse(occupancies.last());
                    final Brand brand = occupancy.getBrand();
                    dto.setOccupancyBrand(brand == null ? null : brand.getName());
                    if (dto.getFixedAssetReference() == null) {
                        // the unit was not retrieved through the invoice item, so get it from the occupancy then.
                        dto.setFixedAssetReference(occupancy.getUnit().getReference());
                        dto.setFixedAssetExternalReference(occupancy.getUnit().getExternalReference());
                    }
                } else {
                    // throw new IllegalArgumentException("Invoice has an effective date range outside the scope of the occupanies");
                    throw new IllegalArgumentException("No Occupancy Found");
                }
            }
        }
    }
    return dto;
}
Also used : InvoiceItemDto(org.estatio.canonical.invoice.v1.InvoiceItemDto) Lease(org.estatio.module.lease.dom.Lease) InvoiceForLease(org.estatio.module.lease.dom.invoicing.InvoiceForLease) InvoiceItemForLease(org.estatio.module.lease.dom.invoicing.InvoiceItemForLease) Charge(org.estatio.module.charge.dom.Charge) InvoiceItemForLease(org.estatio.module.lease.dom.invoicing.InvoiceItemForLease) Tax(org.estatio.module.tax.dom.Tax) FixedAsset(org.estatio.module.asset.dom.FixedAsset) Brand(org.estatio.module.lease.dom.occupancy.tags.Brand) InvoiceForLease(org.estatio.module.lease.dom.invoicing.InvoiceForLease) ChargeGroup(org.estatio.module.charge.dom.ChargeGroup) Occupancy(org.estatio.module.lease.dom.occupancy.Occupancy) TaxRate(org.estatio.module.tax.dom.TaxRate) Programmatic(org.apache.isis.applib.annotation.Programmatic)

Example 2 with InvoiceItemDto

use of org.estatio.canonical.invoice.v1.InvoiceItemDto in project estatio by estatio.

the class InvoiceItemForLeaseDtoFactory_Test method occupancy_is_ignored_when_invoice_item_has_unit.

@Test
public void occupancy_is_ignored_when_invoice_item_has_unit() throws Exception {
    // Given
    invoiceItem.setEffectiveStartDate(new LocalDate(2013, 10, 1));
    Unit unitOnItem = new Unit();
    unitOnItem.setReference("XXX");
    invoiceItem.setFixedAsset(unitOnItem);
    // When
    InvoiceItemDto invoiceItemDto = new InvoiceItemForLeaseDtoFactory().newDto(invoiceItem);
    // Then
    assertThat(invoiceItemDto.getFixedAssetReference()).isEqualTo("XXX");
    assertThat(invoiceItemDto.getOccupancyBrand()).isEqualTo("BRAND");
}
Also used : InvoiceItemDto(org.estatio.canonical.invoice.v1.InvoiceItemDto) Unit(org.estatio.module.asset.dom.Unit) LocalDate(org.joda.time.LocalDate) Test(org.junit.Test)

Example 3 with InvoiceItemDto

use of org.estatio.canonical.invoice.v1.InvoiceItemDto in project estatio by estatio.

the class InvoiceItemForLeaseDtoFactory_Test method occupancy_is_used_when_in_scope.

@Test
public void occupancy_is_used_when_in_scope() throws Exception {
    // Given
    invoiceItem.setEffectiveStartDate(new LocalDate(2013, 10, 1));
    // When
    InvoiceItemDto invoiceItemDto = new InvoiceItemForLeaseDtoFactory().newDto(invoiceItem);
    // Then
    assertThat(invoiceItemDto.getFixedAssetReference()).isEqualTo("UN");
    assertThat(invoiceItemDto.getOccupancyBrand()).isEqualTo("BRAND");
}
Also used : InvoiceItemDto(org.estatio.canonical.invoice.v1.InvoiceItemDto) LocalDate(org.joda.time.LocalDate) Test(org.junit.Test)

Example 4 with InvoiceItemDto

use of org.estatio.canonical.invoice.v1.InvoiceItemDto in project estatio by estatio.

the class InvoiceItemForLeaseDtoFactory_Test method fall_back_to_date_when_effective_is_empty.

@Test
public void fall_back_to_date_when_effective_is_empty() throws Exception {
    // Given
    assertThat(invoiceItem.getEffectiveStartDate()).isNull();
    assertThat(invoiceItem.getStartDate()).isNull();
    final LocalDate startDate = new LocalDate(2016, 1, 1);
    // When
    invoiceItem.setStartDate(startDate);
    InvoiceItemDto invoiceItemDto = new InvoiceItemForLeaseDtoFactory().newDto(invoiceItem);
    // Then
    assertThat(invoiceItemDto.getStartDate().toString()).isEqualTo("2016-01-01T00:00:00.000Z");
    assertThat(invoiceItemDto.getEffectiveStartDate().toString()).isEqualTo("2016-01-01T00:00:00.000Z");
}
Also used : InvoiceItemDto(org.estatio.canonical.invoice.v1.InvoiceItemDto) LocalDate(org.joda.time.LocalDate) Test(org.junit.Test)

Example 5 with InvoiceItemDto

use of org.estatio.canonical.invoice.v1.InvoiceItemDto in project estatio by estatio.

the class InvoiceItemForLeaseDtoFactory_Test method throw_error_when_outside_scope_of_occupancies.

// TODO: See ECP-196
@Ignore
@Test(expected = IllegalArgumentException.class)
public void throw_error_when_outside_scope_of_occupancies() throws Exception {
    // Given
    invoiceItem.setEffectiveStartDate(new LocalDate(2014, 1, 1));
    // When
    InvoiceItemDto invoiceItemDto = new InvoiceItemForLeaseDtoFactory().newDto(invoiceItem);
}
Also used : InvoiceItemDto(org.estatio.canonical.invoice.v1.InvoiceItemDto) LocalDate(org.joda.time.LocalDate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

InvoiceItemDto (org.estatio.canonical.invoice.v1.InvoiceItemDto)5 LocalDate (org.joda.time.LocalDate)4 Test (org.junit.Test)4 Programmatic (org.apache.isis.applib.annotation.Programmatic)1 FixedAsset (org.estatio.module.asset.dom.FixedAsset)1 Unit (org.estatio.module.asset.dom.Unit)1 Charge (org.estatio.module.charge.dom.Charge)1 ChargeGroup (org.estatio.module.charge.dom.ChargeGroup)1 Lease (org.estatio.module.lease.dom.Lease)1 InvoiceForLease (org.estatio.module.lease.dom.invoicing.InvoiceForLease)1 InvoiceItemForLease (org.estatio.module.lease.dom.invoicing.InvoiceItemForLease)1 Occupancy (org.estatio.module.lease.dom.occupancy.Occupancy)1 Brand (org.estatio.module.lease.dom.occupancy.tags.Brand)1 Tax (org.estatio.module.tax.dom.Tax)1 TaxRate (org.estatio.module.tax.dom.TaxRate)1 Ignore (org.junit.Ignore)1