Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.iemr.inventory</groupId>
<artifactId>inventory-api</artifactId>
<version>3.9.0</version>
<version>3.12.0</version>
<packaging>war</packaging>
<name>Inventory-API</name>
<description>Inventory Page</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@
return response.toString();
}

@ApiOperation(value = "Get low stock and out of stock report for a store", consumes = "application/json", produces = "application/json")
@RequestMapping(value = "/getLowStockReport", headers = "Authorization", method = {

Check warning on line 90 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace "@RequestMapping(method = RequestMethod.POST)" with "@PostMapping"

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nhZ&open=AaFcvaaZ2_K4psad3nhZ&pullRequest=138
RequestMethod.POST }, produces = { "application/json" })
public String getLowStockReport(@RequestBody String request) {
logger.info("getLowStockReport request " + request.toString());

Check warning on line 93 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Log Injection via HTTP request body in CRMReportController.getLowStockReport()

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nhd&open=AaFcvaaZ2_K4psad3nhd&pullRequest=138

Check warning on line 93 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

"request" is already a string, there's no need to call "toString()" on it.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nhb&open=AaFcvaaZ2_K4psad3nhb&pullRequest=138

Check warning on line 93 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Invoke method(s) only conditionally. Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nhX&open=AaFcvaaZ2_K4psad3nhX&pullRequest=138

Check warning on line 93 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nha&open=AaFcvaaZ2_K4psad3nha&pullRequest=138
OutputResponse response = new OutputResponse();
try {

ItemStockEntryReport report = InputMapper.gson().fromJson(request, ItemStockEntryReport.class);
String res = crmReportService.getLowStockReport(report);
response.setResponse(res);
logger.info("getLowStockReport response " + response.toString());

Check warning on line 100 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nhc&open=AaFcvaaZ2_K4psad3nhc&pullRequest=138

Check warning on line 100 in src/main/java/com/iemr/inventory/controller/report/CRMReportController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Invoke method(s) only conditionally. Use the built-in formatting to construct this argument.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaaZ2_K4psad3nhY&open=AaFcvaaZ2_K4psad3nhY&pullRequest=138
} catch (Exception e) {
logger.error(e.getMessage());
response.setError(e);
}
return response.toString();
}

@ApiOperation(value = "Get consumption report", consumes = "application/json", produces = "application/json")
@RequestMapping(value = "/getConsumptionReport", headers = "Authorization", method = {
RequestMethod.POST }, produces = { "application/json" })
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/iemr/inventory/model/report/LowStockReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.inventory.model.report;

import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;

import lombok.Data;

@Data
public class LowStockReport {

private Long slNo;

private String facilityName;

private Integer itemID;

private String itemName;

private String itemCategory;

private String strength;

private Integer minStock;

private Integer quantityInHand;

// "Out of stock" (nothing usable left) or "Low stock" (at or below the item's minimum stock)
private String stockStatus;

public LowStockReport() {

}

public LowStockReport(String facilityName, Integer itemID, String itemName, String itemCategory, String strength,
Integer minStock, Integer quantityInHand) {

this.facilityName = facilityName;
this.itemID = itemID;
this.itemName = itemName;
this.itemCategory = itemCategory;
this.strength = strength;
this.minStock = minStock;
this.quantityInHand = quantityInHand;
this.stockStatus = (quantityInHand == null || quantityInHand <= 0) ? "Out of stock" : "Low stock";
}

@Override
public String toString() {

return new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls().create()
.toJson(this);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ List<ItemStockEntryReport> getItemStockEntryReportByFacilityID(@Param("startDate
List<Object[]> getExpiryReportByFacilityID(@Param("startDate") Date startDate,
@Param("endDate") Date endDate, @Param("facilityID") Integer facilityID);

// Live stock (db_iemr, not the reporting copy): items mapped to the store whose usable (not expired) stock
// is 0 or at/below the item's Min_Stock.
@Query(value = "SELECT f.FacilityName, i.ItemID, i.ItemName, c.ItemCategoryName, i.Strength, i.Min_Stock, "
+ "COALESCE((SELECT SUM(e.QuantityInHand) FROM db_iemr.t_itemstockentry e WHERE e.FacilityID = f.FacilityID "
+ " AND e.ItemID = i.ItemID AND e.Deleted = false AND (e.ExpiryDate IS NULL OR e.ExpiryDate >= CURDATE())), 0) AS inHand "
+ "FROM db_iemr.m_facility f "
+ "JOIN db_iemr.m_item i ON i.ItemID IN (SELECT m.ItemID FROM db_iemr.m_itemfacilitymapping m "
+ " WHERE m.FacilityID = f.FacilityID AND m.Deleted = false) AND i.Deleted = false "
+ " AND (i.Discontinued IS NULL OR i.Discontinued = false) "
+ "LEFT JOIN db_iemr.m_itemcategory c ON c.ItemCategoryID = i.ItemCategoryID "
+ "WHERE f.FacilityID = :facilityID "
+ "HAVING inHand <= 0 OR (i.Min_Stock > 0 AND inHand <= i.Min_Stock) "
+ "ORDER BY inHand ASC, i.ItemName ASC", nativeQuery = true)
List<Object[]> getLowStockReportByFacilityID(@Param("facilityID") Integer facilityID);

@Query(value="Select ISEx.*, db.BeneficiaryID, concat(IFNULL(db.Title,''),' ', IFNULL(db.FirstName,''),' ', "
+ "IFNULL(db.MiddleName,''),' ', IFNULL(db.LastName,'')) Name from db_reporting.fact_itemstockexit ISEx left join db_iemr.t_patientissue PI "
+ "on ISEx.ExitTypeID = PI.VanSerialNo and ISEx.VanID = PI.VanID "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ List<Object[]> findByItemIDInQuantityInHandGreaterThanForFacilityID(@Param("item
@Query("UPDATE ItemStockEntry c SET c.quantityInHand = c.quantityInHand + :quant WHERE c.itemStockEntryID = :id")
Integer addStock(@Param("id") Long id, @Param("quant") Integer quant);

@Transactional
@Modifying
@Query("UPDATE ItemStockEntry c SET c.processed = 'N' WHERE c.itemStockEntryID = :id")
Integer markForUpload(@Param("id") Long id);

@Transactional
@Modifying
@Query("UPDATE ItemStockEntry c SET c.processed = 'N' WHERE c.vanSerialNo = :vanSerialNo and c.facilityID = :facilityID")
Integer markForUploadByVanSerialNo(@Param("vanSerialNo") Long vanSerialNo, @Param("facilityID") Integer facilityID);

@Transactional
@Modifying
@Query("UPDATE ItemStockEntry c SET c.quantityInHand = c.quantityInHand - :quant WHERE c.itemStockEntryID = :id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.iemr.inventory.service.stockEntry.StockUploadMarker;
import org.springframework.stereotype.Service;

import com.iemr.inventory.data.indent.Indent;
Expand All @@ -59,6 +60,9 @@
@Service
public class IndentServiceImpl implements IndentService{

@Autowired

Check warning on line 63 in src/main/java/com/iemr/inventory/service/indent/IndentServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaW52_K4psad3nhV&open=AaFcvaW52_K4psad3nhV&pullRequest=138
private StockUploadMarker stockUploadMarker;

private static final Logger logger = LoggerFactory.getLogger(IndentServiceImpl.class);

@PersistenceContext
Expand Down Expand Up @@ -237,6 +241,7 @@
{
indentOrderRepo.updateQuantityInStock(indentIssue.getIssuedQty(), indentIssue.getItemStockEntryID(),
indentIssue.getFromFacilityID());
stockUploadMarker.batchChanged(indentIssue.getItemStockEntryID());

indentIssue.setCreatedDate(new Timestamp(System.currentTimeMillis()));
indentIssue.setIssueDate(new Timestamp(System.currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.iemr.inventory.service.stockEntry.StockUploadMarker;
import org.springframework.stereotype.Service;

import com.iemr.inventory.data.patientreturn.ItemDetailModel;
Expand All @@ -44,6 +45,9 @@
@Service
public class PatientReturnServiceImpl implements PatientReturnService{

@Autowired

Check warning on line 48 in src/main/java/com/iemr/inventory/service/patientreturn/PatientReturnServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaYi2_K4psad3nhW&open=AaFcvaYi2_K4psad3nhW&pullRequest=138
private StockUploadMarker stockUploadMarker;

private static final Logger logger = LoggerFactory.getLogger(PatientReturnServiceImpl.class);

@Autowired(required=false)
Expand Down Expand Up @@ -113,6 +117,7 @@
for(ItemDetailModel itemDetail : list)
{
int result = patientReturnRepo.updateQuantityReturned(itemDetail.getReturnQuantity(), itemDetail.getItemStockEntryID());
stockUploadMarker.batchChanged(itemDetail.getItemStockEntryID());
patientReturnRepo.updateIssuedQuantity(itemDetail.getReturnQuantity(), itemDetail.getItemStockExitID());
ItemReturnEntry itemReturnEntry = new ItemReturnEntry();
itemReturnEntry.setCreatedBy(itemDetail.getCreatedBy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

String getExpiryReport(ItemStockEntryReport entryReport);

String getLowStockReport(ItemStockEntryReport entryReport) throws Exception;

Check warning on line 36 in src/main/java/com/iemr/inventory/service/report/CRMReportService.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace generic exceptions with specific library exceptions or a custom exception.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaSj2_K4psad3nhS&open=AaFcvaSj2_K4psad3nhS&pullRequest=138

String getConsumptionReport(ItemStockExitReport exitReport);

String getBenDrugIssueReport(PatientIssueExitReport exitReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.iemr.inventory.model.report.DailyStockSummary;
import com.iemr.inventory.model.report.ExpiryReport;
import com.iemr.inventory.model.report.InwardStockReport;
import com.iemr.inventory.model.report.LowStockReport;
import com.iemr.inventory.model.report.MonthlyReport;
import com.iemr.inventory.model.report.TransitReport;
import com.iemr.inventory.model.report.YearlyReport;
Expand Down Expand Up @@ -203,6 +204,29 @@
return reportList.toString();
}

@Override
public String getLowStockReport(ItemStockEntryReport entryReport) throws Exception {

if (entryReport == null || entryReport.getFacilityID() == null) {
throw new Exception("facilityID is required");

Check warning on line 211 in src/main/java/com/iemr/inventory/service/report/CRMReportServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace generic exceptions with specific library exceptions or a custom exception.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaSZ2_K4psad3nhR&open=AaFcvaSZ2_K4psad3nhR&pullRequest=138
}
List<Object[]> list = itemStockReportRepo.getLowStockReportByFacilityID(entryReport.getFacilityID());
List<LowStockReport> reportList = new ArrayList<LowStockReport>();
Long slNo = 1L;
for (Object[] object : list) {
if (object != null) {
LowStockReport report = new LowStockReport(text(object[0]),
object[1] != null ? ((Number) object[1]).intValue() : null, text(object[2]), text(object[3]),
text(object[4]), object[5] != null ? ((Number) object[5]).intValue() : null,
object[6] != null ? ((Number) object[6]).intValue() : 0);
report.setSlNo(slNo++);
reportList.add(report);
}
}

return reportList.toString();
}

@Override
public String getConsumptionReport(ItemStockExitReport exitReport) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
@Service
public class StockEntryServiceImpl implements StockEntryService {

@Autowired
private StockUploadMarker stockUploadMarker;

@Autowired

Check warning on line 61 in src/main/java/com/iemr/inventory/service/stockEntry/StockEntryServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaUb2_K4psad3nhT&open=AaFcvaUb2_K4psad3nhT&pullRequest=138
private PhysicalStockEntryRepo physicalStockEntryRepo;

@Autowired(required=false)
Expand Down Expand Up @@ -129,6 +132,8 @@
for (ItemStockExit itemStockExit : itemissueListUpdated) {
cnt = cnt + itemStockEntryRepo.updateStock(itemStockExit.getFacilityID(),
itemStockExit.getItemStockEntryID(), itemStockExit.getQuantity());
stockUploadMarker.batchChangedByVanSerialNo(itemStockExit.getItemStockEntryID(),
itemStockExit.getFacilityID());
}

return cnt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.inventory.service.stockEntry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.iemr.inventory.repo.stockEntry.ItemStockEntryRepo;

/**
* StopTB camp laptops (stoptb.enforce.vanid=true): the camp's stock is reported on central from uploaded rows, so a
* batch whose QuantityInHand changes (adjustment, stock exit, patient return, indent issue) is marked
* Processed='N' and uploaded again; central updates its copy by VanSerialNo + store. Other deployments: no change.
*/
@Component
public class StockUploadMarker {

@Value("${stoptb.enforce.vanid:false}")
private boolean stopTBLaptop;

@Autowired

Check warning on line 41 in src/main/java/com/iemr/inventory/service/stockEntry/StockUploadMarker.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaVL2_K4psad3nhU&open=AaFcvaVL2_K4psad3nhU&pullRequest=138
private ItemStockEntryRepo itemStockEntryRepo;

public void batchChanged(Long itemStockEntryID) {
if (stopTBLaptop && itemStockEntryID != null)
itemStockEntryRepo.markForUpload(itemStockEntryID);
}

public void batchChangedByVanSerialNo(Long vanSerialNo, Integer facilityID) {
if (stopTBLaptop && vanSerialNo != null && facilityID != null)
itemStockEntryRepo.markForUploadByVanSerialNo(vanSerialNo, facilityID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import com.iemr.inventory.service.stockEntry.StockUploadMarker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -51,6 +52,9 @@
@Service
public class StockAdjustmentServiceImpl implements StockAdjustmentService {

@Autowired

Check warning on line 55 in src/main/java/com/iemr/inventory/service/stockadjustment/StockAdjustmentServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Inventory-API&issues=AaFcvaOf2_K4psad3nhQ&open=AaFcvaOf2_K4psad3nhQ&pullRequest=138
private StockUploadMarker stockUploadMarker;

@Autowired(required=false)
StockAdjustmentDraftRepo stockAdjustmentDraftRepo;

Expand Down Expand Up @@ -184,6 +188,7 @@
} else {
itemStockEntryRepo.subtractStock(action.getItemStockEntryID(), action.getAdjustedQuantity());
}
stockUploadMarker.batchChanged(action.getItemStockEntryID());

});

Expand Down
Loading