Skip to content

Commit

Permalink
Merge pull request #6 from slubwama/U4X-263
Browse files Browse the repository at this point in the history
U4X-263 Create stock Item Source Reference backend support
  • Loading branch information
Mwanje authored Dec 11, 2023
2 parents 1699392 + 93dff03 commit 4da539a
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ StockInventoryResult getStockInventory(StockItemInventorySearchFilter filter,

@Transactional(readOnly = true)
@Authorized(value = { Privileges.APP_STOCKMANAGEMENT_STOCKITEMS }, requireAll = false)
StockItem getStockItemByDrug(Integer drugId);
List<StockItem> getStockItemByDrug(Integer drugId);

@Transactional(readOnly = true)
@Authorized(value = { Privileges.APP_STOCKMANAGEMENT_STOCKITEMS }, requireAll = false)
StockItem getStockItemByConcept(Integer conceptId);
List<StockItem> getStockItemByConcept(Integer conceptId);

@Transactional(readOnly = true)
@Authorized(value = { Privileges.APP_STOCKMANAGEMENT_STOCKITEMS }, requireAll = false)
Expand Down Expand Up @@ -529,4 +529,12 @@ <T extends StockItemInventory> void getStockInventory(StockItemInventorySearchFi
@Transactional
@Authorized(Privileges.TASK_STOCKMANAGEMENT_STOCKOPERATIONS_MUTATE)
StockOperationBatchNumbersDTO saveStockOperationBatchNumbers(StockOperationBatchNumbersDTO stockOperationBatchNumbers);

@Transactional(readOnly = true)
@Authorized(Privileges.APP_STOCKMANAGEMENT_STOCKITEMS)
StockItem getStockItemByReference(StockSource stockSource, String code);

@Transactional
@Authorized(Privileges.APP_STOCKMANAGEMENT_STOCKITEMS)
StockItemReference saveStockItemReference(StockItemReference stockItemReference);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3264,14 +3264,13 @@ public List<StockItemPackagingUOM> getStockItemPackagingUOMs(List<StockItemPacka
return criteria.list();
}

public StockItem getStockItemByDrug(Integer drugId) {
return (StockItem) getSession().createCriteria(StockItem.class).add(Restrictions.eq("drug.drugId", drugId))
.setMaxResults(1).uniqueResult();
public List<StockItem> getStockItemByDrug(Integer drugId) {
return getSession().createCriteria(StockItem.class).add(Restrictions.eq("drug.drugId", drugId)).list();
}

public StockItem getStockItemByConcept(Integer conceptId) {
return (StockItem) getSession().createCriteria(StockItem.class).add(Restrictions.isNull("drug.drugId"))
.add(Restrictions.eq("concept.conceptId", conceptId)).uniqueResult();
public List<StockItem> getStockItemByConcept(Integer conceptId) {
return getSession().createCriteria(StockItem.class).add(Restrictions.isNull("drug.drugId"))
.add(Restrictions.eq("concept.conceptId", conceptId)).list();
}

public StockItemPackagingUOM getStockItemPackagingUOMByConcept(Integer stockItemId, Integer conceptId) {
Expand Down Expand Up @@ -5272,4 +5271,16 @@ public Map<Integer, Boolean> checkStockBatchHasTransactionsAfterOperation(Intege

return mapResult;
}

public StockItemReference getStockItemByReference(StockSource stockSource, String stockReferenceCode) {
Criteria criteria = getSession().createCriteria(StockItemReference.class);
criteria.add(Restrictions.eq("referenceSource", stockSource));
criteria.add(Restrictions.eq("stockReferenceCode", stockReferenceCode));
return (StockItemReference) criteria.setMaxResults(1).uniqueResult();
}

public StockItemReference saveStockItemReference(StockItemReference stockItemReference) {
getSession().saveOrUpdate(stockItemReference);
return stockItemReference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.openmrs.Drug;
import org.openmrs.module.stockmanagement.api.model.StockBatch;
import org.openmrs.module.stockmanagement.api.model.StockItemPackagingUOM;
import org.openmrs.module.stockmanagement.api.model.StockItemReference;
import org.openmrs.module.stockmanagement.api.model.StockSource;

import java.math.BigDecimal;
Expand Down Expand Up @@ -73,6 +74,8 @@ public class StockItemDTO {

private List<StockItemPackagingUOMDTO> stockItemPackagingUOMs;

private List<StockItemReference> stockItemReferences;

private boolean voided;

private Integer creator;
Expand Down Expand Up @@ -347,6 +350,14 @@ public void setStockItemPackagingUOMs(List<StockItemPackagingUOMDTO> stockItemPa
this.stockItemPackagingUOMs = stockItemPackagingUOMs;
}

public List<StockItemReference> getStockItemReferences() {
return stockItemReferences;
}

public void setStockItemReferences(List<StockItemReference> stockItemReferences) {
this.stockItemReferences = stockItemReferences;
}

public boolean getVoided() {
return voided;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2563,16 +2563,17 @@ public List<StockItem> getStockItems(Collection<Integer> stockItemIds) {
return dao.getStockItems(stockItemIds);
}


public List<StockItemPackagingUOM> getStockItemPackagingUOMs(
List<StockItemPackagingUOMSearchFilter.ItemGroupFilter> filters) {
return dao.getStockItemPackagingUOMs(filters);
}

public StockItem getStockItemByDrug(Integer drugId) {
public List<StockItem> getStockItemByDrug(Integer drugId) {
return dao.getStockItemByDrug(drugId);
}

public StockItem getStockItemByConcept(Integer conceptId) {
public List<StockItem> getStockItemByConcept(Integer conceptId) {
return dao.getStockItemByConcept(conceptId);
}

Expand Down Expand Up @@ -3431,4 +3432,19 @@ public StockOperationBatchNumbersDTO saveStockOperationBatchNumbers(StockOperati

return stockOperationBatchNumbers;
}

@Override
public StockItem getStockItemByReference(StockSource stockSource, String code) {
StockItemReference stockItemReference = dao.getStockItemByReference(stockSource,code);
if(stockItemReference!=null) {
return dao.getStockItemByReference(stockSource, code).getStockItem();
}

return null;
}

@Override
public StockItemReference saveStockItemReference(StockItemReference stockItemReference) {
return dao.saveStockItemReference(stockItemReference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.openmrs.module.stockmanagement.api.dto.*;
import org.openmrs.module.stockmanagement.api.model.StockItem;
import org.openmrs.module.stockmanagement.api.model.StockItemPackagingUOM;
import org.openmrs.module.stockmanagement.api.model.StockItemReference;
import org.openmrs.module.stockmanagement.api.model.StockSource;
import org.openmrs.module.stockmanagement.api.utils.GlobalProperties;

Expand Down Expand Up @@ -62,6 +63,8 @@ public class StockItemImportJob {

private int PURCHASE_PRICE_PUOM = 14;

private int VENDOR_REFERENCE_ITEM_CODE = 15;

private static Pattern NON_ASCII_PATTERN = Pattern.compile("[^A-Za-z0-9]");

public StockItemImportJob(Path file, boolean hasHeader) {
Expand All @@ -86,7 +89,7 @@ private boolean isBlank(String value) {

private Object validateLine(String[] line) {
if (line == null || line.length == 0) return null;
Object[] objects = new Object[15];
Object[] objects = new Object[16];
List<String> errors = new ArrayList<>();
if (line.length < 3) {
errors.add(Context.getMessageSourceService().getMessage("stockmanagement.importoperation.minimumfields"));
Expand Down Expand Up @@ -216,6 +219,11 @@ private Object validateLine(String[] line) {
objects[PREFERRED_VENDOR] = line[PREFERRED_VENDOR];
}


if (line.length > VENDOR_REFERENCE_ITEM_CODE && !isBlank(line[VENDOR_REFERENCE_ITEM_CODE])) {
objects[VENDOR_REFERENCE_ITEM_CODE] = line[VENDOR_REFERENCE_ITEM_CODE];
}

if (line.length > REORDER_LEVEL && !isBlank(line[REORDER_LEVEL])) {
try {
BigDecimal value = new BigDecimal(line[REORDER_LEVEL]);
Expand Down Expand Up @@ -760,6 +768,17 @@ private void updateStockItems(Map<Integer, Object[]> stockItems) {
saveStockItem=true;
}

if (updates[VENDOR_REFERENCE_ITEM_CODE] != null && stockItem.getPreferredVendor() != null) {
StockItemReference stockItemReference = new StockItemReference();
Set<StockItemReference> stockItemReferenceSet = new HashSet<>();
stockItemReference.setStockReferenceCode(updates[VENDOR_REFERENCE_ITEM_CODE].toString());
stockItemReference.setStockItem(stockItem);
stockItemReference.setReferenceSource(stockItem.getPreferredVendor());
stockItemReferenceSet.add(stockItemReference);
stockItem.setStockItemReferences(stockItemReferenceSet);
saveStockItem = true;
}

if (saveStockItem) {
stockItem.setDateChanged(new Date());
stockItem.setChangedBy(Context.getAuthenticatedUser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class StockItem extends org.openmrs.BaseChangeableOpenmrsData implements
@OneToMany(mappedBy = "stockItem")
private Set<StockItemPackagingUOM> stockItemPackagingUOMs;

@OneToMany(mappedBy = "stockItem", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<StockItemReference> stockItemReferences;

@Field
@Column(name = "is_drug", nullable = false)
private Boolean isDrug;
Expand Down Expand Up @@ -182,6 +185,27 @@ public StockItemPackagingUOM removeStockItemPackagingUom(StockItemPackagingUOM s
return stockItemPackagingUom;
}

public Set<StockItemReference> getStockItemReferences() {
return stockItemReferences;
}

public void setStockItemReferences(Set<StockItemReference> stockItemReferences) {
this.stockItemReferences = stockItemReferences;
}

public StockItemReference addStockItemReference(StockItemReference stockItemReference) {
getStockItemReferences().add(stockItemReference);
stockItemReference.setStockItem(this);

return stockItemReference;
}

public StockItemReference removeStockItemReferences(StockItemReference stockItemReference) {
getStockItemReferences().remove(stockItemReference);
stockItemReference.setStockItem(null);
return stockItemReference;
}

public boolean isHasExpiration() {
return hasExpiration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.openmrs.module.stockmanagement.api.model;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.openmrs.Concept;
import org.openmrs.Drug;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Set;

/**
* The persistent class for the stockmgmt_stock_item database table.
*/
@Entity(name = "stockmanagement.StockItemReference")
@Table(name = "stockmgmt_stock_item_reference")
@Indexed
public class StockItemReference extends org.openmrs.BaseChangeableOpenmrsData implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "stock_item_reference_id")
@DocumentId
private Integer id;

@JoinColumn(name = "stock_source_id")
@ManyToOne(fetch = FetchType.LAZY)
private StockSource referenceSource;

@JoinColumn(name = "stock_item_id")
@ManyToOne(fetch = FetchType.EAGER)
private StockItem stockItem;

@Field
@Column(name = "stock_reference_code", length = 255)
private String stockReferenceCode;

@Override
public Integer getId() {
return id;
}

@Override
public void setId(Integer id) {
this.id = id;
}

public StockSource getReferenceSource() {
return referenceSource;
}

public void setReferenceSource(StockSource referenceSource) {
this.referenceSource = referenceSource;
}

public StockItem getStockItem() {
return stockItem;
}

public void setStockItem(StockItem stockItem) {
this.stockItem = stockItem;
}

public String getStockReferenceCode() {
return stockReferenceCode;
}

public void setStockReferenceCode(String stockReferenceCode) {
this.stockReferenceCode = stockReferenceCode;
}

}
78 changes: 78 additions & 0 deletions api/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4564,4 +4564,82 @@ CREATE TRIGGER stockmgmt_location_after_delete
<where>operation_type in ('receipt', 'initial')</where>
</update>
</changeSet>
<changeSet id="stockmanagement-06-12-0700" author="slubwama">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="stockmgmt_stock_item_reference" />
</not>
</preConditions>
<comment>
Creating the stockmgmt_batch_job_owner table
</comment>
<createTable tableName="stockmgmt_stock_item_reference">
<column name="stock_item_reference_id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="stock_source_id" type="int">
<constraints nullable="false"/>
</column>
<column name="stock_item_id" type="int">
<constraints nullable="false"/>
</column>
<column name="stock_reference_code" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="creator" type="int">
<constraints nullable="false"/>
</column>
<column name="date_created" type="date">
<constraints nullable="true"/>
</column>
<column name="changed_by" type="int">
<constraints nullable="true"/>
</column>
<column name="date_changed" type="date">
<constraints nullable="true"/>
</column>
<column name="voided" type="tinyint(1)">
<constraints nullable="true"/>
</column>
<column name="date_voided" type="date">
<constraints nullable="true"/>
</column>
<column name="voided_by" type="int">
<constraints nullable="true"/>
</column>
<column name="void_reason" type="varchar(255)">
<constraints nullable="true"/>
</column>
</createTable>

<addForeignKeyConstraint baseTableName="stockmgmt_stock_item_reference"
baseColumnNames="stock_item_id"
referencedTableName="stockmgmt_stock_item"
referencedColumnNames="stock_item_id"
constraintName="stock_item_reference_stock_item_stock_item_id_fk"/>

<addForeignKeyConstraint baseTableName="stockmgmt_stock_item_reference"
baseColumnNames="stock_source_id"
referencedTableName="stockmgmt_stock_source"
referencedColumnNames="stock_source_id"
constraintName="stock_item_reference_stock_source_stock_source_id_fk"/>

<addForeignKeyConstraint baseTableName="stockmgmt_stock_item_reference"
baseColumnNames="changed_by"
referencedTableName="users"
referencedColumnNames="user_id"
constraintName="stock_item_reference_users_changed_by_id_fk"/>

<addForeignKeyConstraint baseTableName="stockmgmt_stock_item_reference"
baseColumnNames="creator"
referencedTableName="users"
referencedColumnNames="user_id"
constraintName="stock_item_reference_users_creator_id_fk"/>

<addForeignKeyConstraint baseTableName="stockmgmt_stock_item_reference"
baseColumnNames="voided_by"
referencedTableName="users"
referencedColumnNames="user_id"
constraintName="stock_item_reference_users_voided_by_id_fk"/>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class EntityUtil {

public static final String STOCK_OPERATION_TYPE_DATA_SET = BASE_DATASET_DIR + "StockOperationType.xml";

public static final String STOCK_ITEMS_IMPORT_CSV = BASE_DATASET_DIR + "StockItemsImport.csv";
public static final String STOCK_ITEMS_IMPORT_CSV = BASE_DATASET_DIR + "StockItemsImport2.csv";

private static Random random = new Random();

Expand Down
Loading

0 comments on commit 4da539a

Please sign in to comment.