Skip to content

Commit

Permalink
updated resources for Attribute and attribute resources 1
Browse files Browse the repository at this point in the history
  • Loading branch information
spbogui committed Oct 18, 2021
1 parent ed9f574 commit 5b7e39d
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public interface ProductAttributeService extends OpenmrsService {
ProductAttribute getOneProductAttributeByBatchNumberAndExpiryDate(String batchNumber, Date expiryDate);
@Authorized(value = {PrivilegeConstants.SAVE_PRODUCT_ATTRIBUTE})
Integer purgeUnusedAttributes();

List<ProductAttribute> getAllAttributesNotInStock(Location location, Boolean includeVoided);
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public interface ProductReportService extends OpenmrsService {
ProductReport getLastProductReportByProductAndByDate(Location location, ProductProgram productProgram, Product product, Date reportDate, Boolean urgent);
@Authorized(value = {PrivilegeConstants.VIEW_REPORT})
ProductReport getPeriodTreatedProductReportsByReportPeriodAndLocation(String operationNumber, ProductProgram program, Location childLocation, boolean isUrgent);
@Authorized(value = {PrivilegeConstants.VIEW_REPORT})
ProductReport getLatestReportByProductAndLocationAndInventory(Location location, ProductInventory inventory);
@Authorized(value = {PrivilegeConstants.VIEW_REPORT})
ProductReport getLatestDistributionByLocationAndInventory(Location location, Location reportLocation, ProductInventory inventory);
// List<ProductReportReturnDTO> getProductReportReturnDTOs(ProductReport productReport) throws APIException;
// ProductReportReturnDTO getOneProductReportReturnDTO(ProductReport reception, ProductAttribute productAttribute) throws APIException;
// List<ProductReportListDTO> getProductReportListDTOs() throws APIException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public interface ProductAttributeDAO {
ProductAttribute getOneProductAttributeByBatchNumber(String batchNumber, Location location);
ProductAttribute getOneProductAttributeByBatchNumberAndExpiryDate(String batchNumber, Date expiryDate);
Integer purgeUnusedAttributes();

List<ProductAttribute> getAllAttributesNotInStock(Location location, Boolean includeVoided);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public interface ProductReportDAO {
ProductAttributeOtherFlux getPreviousReportProductAttributeOtherFluxByLabel(Product product, String label, ProductReport report, Location location);
ProductReport getLastProductReportByProductAndByDate(Location location, ProductProgram productProgram, Product product, Date reportDate, Boolean urgent);
ProductReport getPeriodTreatedProductReportsByReportPeriodAndLocation(String reportPeriod, ProductProgram program, Location childLocation, boolean isUrgent);

ProductReport getLatestReportByLocationAndInventory(Location location, ProductInventory inventory);
ProductReport getLatestDistributionByLocationAndInventory(Location location, Location reportLocation, ProductInventory inventory);
// List<ProductReportReturnDTO> getProductReportReturnDTOs(ProductReport productReport);
// ProductReportReturnDTO getOneProductReportReturnDTO(ProductReport reception, ProductAttribute productAttribute);
// List<ProductReportListDTO> getProductReportListDTOs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,9 @@ public Boolean validateOperation(ProductOperation operation) {
Set<ProductAttributeFlux> fluxes = operation.getProductAttributeFluxes();
if (fluxes != null && fluxes.size() != 0) {
for (ProductAttributeFlux flux : fluxes) {
Integer quantityInStock = 0;

ProductAttributeStock attributeStock = Context.getService(ProductAttributeStockService.class)
.getOneProductAttributeStockByAttribute(flux.getProductAttribute(), operation.getLocation(), false);
if (attributeStock != null) {
// System.out.println("--------------------------------------> attribute stock found");
quantityInStock = attributeStock.getQuantityInStock();
// System.out.println("--------------------------------------> got quantity");
attributeStock.setVoided(true);
attributeStock.setDateVoided(new Date());
attributeStock.setVoidedBy(Context.getAuthenticatedUser());
attributeStock.setVoidReason("Voided by user because not to be used");
Context.getService(ProductAttributeStockService.class).saveProductAttributeStock(attributeStock);
// System.out.println("--------------------------------------> attribute stock voided");
}
Integer quantity = getQuantity(operation, flux, true);

Integer quantity = operation.getIncidence().equals(Incidence.POSITIVE) ?
quantityInStock + flux.getQuantity() :
(operation.getIncidence().equals(Incidence.NEGATIVE) ? quantityInStock - flux.getQuantity() : flux.getQuantity());

attributeStock = new ProductAttributeStock();
ProductAttributeStock attributeStock = new ProductAttributeStock();
attributeStock.setQuantityInStock(quantity);
attributeStock.setLocation(operation.getLocation());
attributeStock.setProductAttribute(flux.getProductAttribute());
Expand All @@ -135,24 +117,9 @@ public Boolean cancelOperation(ProductOperation operation) {
if (fluxes != null && fluxes.size() != 0) {
for (ProductAttributeFlux flux : fluxes) {
if (flux.getStatus().equals(OperationStatus.VALIDATED)) {
Integer quantityInStock = 0;
ProductAttributeStock attributeStock = Context.getService(ProductAttributeStockService.class)
.getOneProductAttributeStockByAttribute(
flux.getProductAttribute(),
operation.getLocation(), false);
if (attributeStock != null) {
quantityInStock = attributeStock.getQuantityInStock();
attributeStock.setVoided(true);
attributeStock.setDateVoided(new Date());
attributeStock.setVoidedBy(Context.getAuthenticatedUser());
attributeStock.setVoidReason("Voided by user because not to be used");
Context.getService(ProductAttributeStockService.class).saveProductAttributeStock(attributeStock);
}
Integer quantity = operation.getIncidence().equals(Incidence.POSITIVE) ?
quantityInStock - flux.getQuantity() :
(operation.getIncidence().equals(Incidence.NEGATIVE) ? quantityInStock + flux.getQuantity() : flux.getQuantity());

attributeStock = new ProductAttributeStock();
Integer quantity = getQuantity(operation, flux, false);

ProductAttributeStock attributeStock = new ProductAttributeStock();
attributeStock.setQuantityInStock(quantity);
attributeStock.setLocation(operation.getLocation());
attributeStock.setProductAttribute(flux.getProductAttribute());
Expand All @@ -171,6 +138,39 @@ public Boolean cancelOperation(ProductOperation operation) {
return saveProductOperation(operation) != null;
}

private Integer voidPreviousStock(ProductOperation operation, ProductAttributeFlux flux) {
Integer quantityInStock = 0;
ProductAttributeStock attributeStock = Context.getService(ProductAttributeStockService.class)
.getOneProductAttributeStockByAttribute(flux.getProductAttribute(), operation.getLocation(), false);
if (attributeStock != null) {
quantityInStock = attributeStock.getQuantityInStock();
attributeStock.setVoided(true);
attributeStock.setDateVoided(new Date());
attributeStock.setVoidedBy(Context.getAuthenticatedUser());
attributeStock.setVoidReason("Voided by user because not to be used");
Context.getService(ProductAttributeStockService.class).saveProductAttributeStock(attributeStock);
}
return quantityInStock;
}

private Integer getQuantity(ProductOperation operation, ProductAttributeFlux flux, Boolean isValidation) {
Integer quantityInStock = voidPreviousStock(operation, flux);
if (isValidation) {
if (operation.getIncidence().equals(Incidence.POSITIVE)) {
return quantityInStock + flux.getQuantity();
} else if (operation.getIncidence().equals(Incidence.NEGATIVE)) {
return quantityInStock - flux.getQuantity();
}
} else {
if (operation.getIncidence().equals(Incidence.POSITIVE)) {
return quantityInStock - flux.getQuantity();
} else if (operation.getIncidence().equals(Incidence.NEGATIVE)) {
return quantityInStock + flux.getQuantity();
}
}
return flux.getQuantity();
}

@Override
public ProductOperation getOneProductOperationById(Integer productOperationId) {
return (ProductOperation) sessionFactory.getCurrentSession().get(ProductOperation.class, productOperationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,19 @@ public ProductAttribute getOneProductAttributeByBatchNumberAndExpiryDate(String
@Override
public Integer purgeUnusedAttributes() {
List<ProductAttribute> productAttributes = sessionFactory.getCurrentSession().createQuery("" +
"FROM ProductAttribute p WHERE p.productAttributeId NOT IN (SELECT pf.productAttribute.productAttributeId FROM ProductAttributeFlux pf)").list();
"FROM ProductAttribute p WHERE " +
"p.productAttributeId NOT IN (SELECT pf.productAttribute.productAttributeId FROM ProductAttributeFlux pf) " +
"AND p.productAttributeId NOT IN (SELECT pof.productAttribute.productAttributeId FROM ProductAttributeOtherFlux pof) " +
"AND p.productAttributeId NOT IN (SELECT ps.productAttribute.productAttributeId FROM ProductAttributeStock ps)").list();
Integer quantityDeleted = productAttributes.size();
for (ProductAttribute attribute : productAttributes) {
removeProductAttribute(attribute);
}
return productAttributes.size();
return quantityDeleted;
}

@Override
public List<ProductAttribute> getAllAttributesNotInStock(Location location, Boolean includeVoided) {
return null;
}
}
Loading

0 comments on commit 5b7e39d

Please sign in to comment.