Skip to content

Commit

Permalink
AO-802: Added PriceChange entity
Browse files Browse the repository at this point in the history
  • Loading branch information
sradziszewski committed Dec 5, 2023
1 parent 39dc622 commit bb80e35
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 16 deletions.
100 changes: 100 additions & 0 deletions src/main/java/org/openlmis/referencedata/domain/PriceChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact [email protected].
*/

package org.openlmis.referencedata.domain;

import java.time.ZonedDateTime;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Type;
import org.joda.money.Money;

@Getter
@Setter
@Entity
@Table(name = "price_changes")
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class PriceChange extends BaseEntity {

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "programOrderableId")
private ProgramOrderable programOrderable;

@Type(type = "org.openlmis.referencedata.util.CustomSingleColumnMoneyUserType")
private Money price;

@ManyToOne
@JoinColumn(name = "authorId", nullable = false)
private User author;

@Column(columnDefinition = "timestamp with time zone", nullable = false)
private ZonedDateTime occurredDate;

/**
* Creates new instance based on data from {@link PriceChange.Importer}.
*
* @param importer instance of {@link PriceChange.Importer}
* @return new instance of PriceChange.
*/
public static PriceChange newInstance(PriceChange.Importer importer, User author) {
PriceChange priceChange = new PriceChange();
priceChange.setPrice(importer.getPrice());
priceChange.setOccurredDate(importer.getOccurredDate());
priceChange.setAuthor(author);

return priceChange;
}

/**
* Export this object to the specified exporter (DTO).
*
* @param exporter exporter to export to
*/
public void export(PriceChange.Exporter exporter) {
exporter.setPrice(price);
exporter.setAuthor(author);
exporter.setOccurredDate(occurredDate);
}

public interface Exporter {

void setPrice(Money price);

void setAuthor(User author);

void setOccurredDate(ZonedDateTime occurredDate);

}

public interface Importer {

Money getPrice();

ZonedDateTime getOccurredDate();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import static org.openlmis.referencedata.web.csv.processor.CsvCellProcessors.PROGRAM_TYPE;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import lombok.AllArgsConstructor;
Expand All @@ -39,6 +43,7 @@
import org.javers.core.metamodel.annotation.TypeName;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.openlmis.referencedata.dto.PriceChangeDto;
import org.openlmis.referencedata.web.csv.model.ImportField;

@Entity
Expand Down Expand Up @@ -107,9 +112,17 @@ public class ProgramOrderable extends BaseEntity {
@ImportField(name = PRICE_PER_PACK, type = MONEY_TYPE)
private Money pricePerPack;

@OneToMany(
mappedBy = "programOrderable",
cascade = CascadeType.ALL,
orphanRemoval = true)
@Getter
@Setter
private List<PriceChange> priceChanges = new ArrayList<>();

private ProgramOrderable(Program program,
Orderable product,
OrderableDisplayCategory orderableDisplayCategory) {
Orderable product,
OrderableDisplayCategory orderableDisplayCategory) {
this.program = program;
this.product = product;
this.orderableDisplayCategory = orderableDisplayCategory;
Expand Down Expand Up @@ -159,9 +172,9 @@ public boolean isForProgram(Program program) {
* @return see other
*/
public static final ProgramOrderable createNew(Program program,
OrderableDisplayCategory category,
Orderable product,
CurrencyUnit currencyUnit) {
OrderableDisplayCategory category,
Orderable product,
CurrencyUnit currencyUnit) {
ProgramOrderable programOrderable = new ProgramOrderable(program, product, category);
programOrderable.pricePerPack = Money.of(currencyUnit, BigDecimal.ZERO);
return programOrderable;
Expand All @@ -179,14 +192,14 @@ public static final ProgramOrderable createNew(Program program,
* @return a new ProgramOrderable.
*/
public static final ProgramOrderable createNew(Program program,
OrderableDisplayCategory category,
Orderable product,
Integer dosesPerPatient,
boolean active,
boolean fullSupply,
int displayOrder,
Money pricePerPack,
CurrencyUnit currencyUnit) {
OrderableDisplayCategory category,
Orderable product,
Integer dosesPerPatient,
boolean active,
boolean fullSupply,
int displayOrder,
Money pricePerPack,
CurrencyUnit currencyUnit) {
ProgramOrderable programOrderable = createNew(program, category, product, currencyUnit);
programOrderable.dosesPerPatient = dosesPerPatient;
programOrderable.active = active;
Expand Down Expand Up @@ -262,7 +275,7 @@ public void export(Exporter exporter) {
if (pricePerPack != null) {
exporter.setPricePerPack(pricePerPack);
}

exporter.setPriceChanges(PriceChangeDto.newInstance(priceChanges));
}

public interface Exporter {
Expand All @@ -283,6 +296,8 @@ public interface Exporter {
void setDosesPerPatient(Integer dosesPerPatient);

void setPricePerPack(Money pricePerPack);

void setPriceChanges(List<PriceChangeDto> priceChanges);
}

public interface Importer {
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/org/openlmis/referencedata/dto/PriceChangeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact [email protected].
*/

package org.openlmis.referencedata.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.joda.money.Money;
import org.openlmis.referencedata.domain.PriceChange;
import org.openlmis.referencedata.domain.User;
import org.openlmis.referencedata.serializer.MoneyDeserializer;
import org.openlmis.referencedata.serializer.MoneySerializer;
import org.openlmis.referencedata.web.BaseController;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@ToString
public final class PriceChangeDto extends BaseDto implements PriceChange.Exporter,
PriceChange.Importer {

@JsonSerialize(using = MoneySerializer.class)
@JsonDeserialize(using = MoneyDeserializer.class)
private Money price;
private ZonedDateTime occurredDate;
private UserObjectReferenceDto author;

/**
* Create new List containing PriceChangeDto based on given a set of {@link PriceChange}.
*
* @param priceChanges Price changes.
* @return a list containing dtos for all price changes.
*/
public static List<PriceChangeDto> newInstance(List<PriceChange> priceChanges) {
if (priceChanges == null) {
return Collections.emptyList();
}
return priceChanges.stream()
.map(PriceChangeDto::newInstance)
.collect(Collectors.toList());
}

/**
* Create new PriceChangeDto based on a given {@link PriceChange}.
*
* @param priceChange Price change.
* @return a price change object converted to dto.
*/
public static PriceChangeDto newInstance(PriceChange priceChange) {
PriceChangeDto dto = new PriceChangeDto();
priceChange.export(dto);
return dto;
}

@Override
public void setAuthor(User author) {
this.author = new UserObjectReferenceDto("referencedata",
BaseController.API_PATH + "/users", author.getId());
this.author.setFirstName(author.getFirstName());
this.author.setLastName(author.getLastName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -60,6 +65,8 @@ public class ProgramOrderableDto extends BaseDto
@JsonDeserialize(using = MoneyDeserializer.class)
private Money pricePerPack;

private List<PriceChangeDto> priceChanges;

/**
* Create new list of ProgramOrderableDto based on given list of {@link ProgramOrderable}.
*
Expand Down Expand Up @@ -89,4 +96,10 @@ public static ProgramOrderableDto newInstance(ProgramOrderable po) {

return programDto;
}

public List<PriceChangeDto> getPriceChanges() {
return Lists.newArrayList(Optional.ofNullable(priceChanges)
.orElse(Collections.emptyList()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public List<ProgramOrderable> createOrUpdate(List<ProgramOrderableCsvModel> dtoL
dto.getDisplayOrder(),
dto.getDosesPerPatient(),
dto.getPricePerPack() != null ? Money.of(currency,
Double.parseDouble(dto.getPricePerPack())) : null
Double.parseDouble(dto.getPricePerPack())) : null,
null
);

ProgramOrderable programOrderable = programOrderableRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE price_changes (
id UUID PRIMARY KEY,
programOrderableId UUID,
price numeric(19,2) NOT NULL,
authorId uuid NOT NULL,
occurredDate TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT fk_program_orderable FOREIGN KEY (programOrderableId) REFERENCES program_orderables(id),
CONSTRAINT fk_author FOREIGN KEY (authorId) REFERENCES users(id)
);
Loading

0 comments on commit bb80e35

Please sign in to comment.