Skip to content

Commit 7f10a54

Browse files
vishwab1claude
andauthored
Facility hierarchy inventory mapping and store field management (#128)
* fix:changed the pom xml * fix: added facilty type master change * feat: created facility creation * feat:added work location * feat:added work location * fix: rabiit review fix * fix: rabiit review fix * fix: rabiit review fix * fix: ui chnges * fix: pom version * fix: corrections * fix: facilty hierachy * fix: facility heirachy * fix: item facility mapping and store updates Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: fixed inventory flow --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c701ce9 commit 7f10a54

7 files changed

Lines changed: 109 additions & 8 deletions

File tree

4.14 KB
Binary file not shown.

src/main/java/com/iemr/admin/controller/itemfacilitymapping/MItemFacilityMappingController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ public String getAllFacilityMappedData(@RequestBody String getAllFacilityMappedD
228228

229229
}
230230

231+
@RequestMapping(value = "/getItemMappingsByFacility", headers = "Authorization", method = {
232+
RequestMethod.POST }, produces = { "application/json" })
233+
public String getItemMappingsByFacility(@RequestBody String request) {
234+
OutputResponse response = new OutputResponse();
235+
try {
236+
V_fetchItemFacilityMap reqObj = InputMapper.gson().fromJson(request,
237+
V_fetchItemFacilityMap.class);
238+
ArrayList<V_fetchItemFacilityMap> data = M_itemfacilitymappingInter
239+
.getItemMappingsByFacilityID(reqObj.getFacilityID());
240+
response.setResponse(data.toString());
241+
} catch (Exception e) {
242+
logger.error("Unexpected error:", e);
243+
response.setError(e);
244+
}
245+
return response.toString();
246+
}
247+
231248
@Operation(summary = "Get item from store id")
232249
@RequestMapping(value = "/getItemFromStoreID/{storeID}", headers = "Authorization", method = {
233250
RequestMethod.POST }, produces = { "application/json" })

src/main/java/com/iemr/admin/repository/itemfacilitymapping/V_fetchItemFacilityMapRepo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ public interface V_fetchItemFacilityMapRepo extends CrudRepository<V_fetchItemFa
3636
@Query("SELECT u FROM V_fetchItemFacilityMap u where u.providerServiceMapID = :providerServiceMapID")
3737
ArrayList<V_fetchItemFacilityMap> getAllFacilityMappedData(@Param("providerServiceMapID") Integer providerServiceMapID);
3838

39+
@Query("SELECT u FROM V_fetchItemFacilityMap u where u.facilityID = :facilityID")
40+
ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityID(@Param("facilityID") Integer facilityID);
41+
42+
@Query(value = "SELECT u.* FROM v_fetchItemFacilityMap u WHERE u.FacilityID = :facilityID "
43+
+ "OR u.FacilityID IN (SELECT f.FacilityID FROM m_facility f WHERE f.MainFacilityID = :facilityID AND f.Deleted = false)",
44+
nativeQuery = true)
45+
ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityAndSubStores(@Param("facilityID") Integer facilityID);
46+
3947
}

src/main/java/com/iemr/admin/repository/store/MainStoreRepo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public interface MainStoreRepo extends CrudRepository<M_Facility, Integer> {
3737

3838
List<M_Facility> findByProviderServiceMapIDOrderByFacilityName(Integer providerServiceMapID);
3939

40+
@Query("SELECT f FROM M_Facility f WHERE (f.providerServiceMapID = :providerServiceMapID OR f.providerServiceMapID IS NULL) ORDER BY f.facilityName")
41+
List<M_Facility> findByProviderServiceMapIDOrNullOrderByFacilityName(@Param("providerServiceMapID") Integer providerServiceMapID);
42+
4043
@Query("SELECT u FROM M_Facility u WHERE u.providerServiceMapID=:providerServiceMapID AND u.isMainFacility=:isMainFacility AND deleted=false order by u.facilityName")
4144
ArrayList<M_Facility> getAllMainFacility(@Param("providerServiceMapID") Integer providerServiceMapID,
4245
@Param("isMainFacility") Boolean isMainFacility);
@@ -90,4 +93,11 @@ ArrayList<M_Facility> findByBlockIDAndLevelValue(@Param("blockID") Integer block
9093
int clearParentFacilityID(@Param("parentFacilityID") Integer parentFacilityID,
9194
@Param("modifiedBy") String modifiedBy);
9295

96+
@Modifying
97+
@Query(value = "UPDATE m_facility SET IsMainFacility = :isMainFacility, MainFacilityID = :mainFacilityID, StoreType = :storeType WHERE FacilityID = :facilityID", nativeQuery = true)
98+
int updateStoreFields(@Param("facilityID") Integer facilityID,
99+
@Param("isMainFacility") Boolean isMainFacility,
100+
@Param("mainFacilityID") Integer mainFacilityID,
101+
@Param("storeType") String storeType);
102+
93103
}

src/main/java/com/iemr/admin/service/itemfacilitymapping/M_itemfacilitymappingImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public ArrayList<V_fetchItemFacilityMap> getAllFacilityMappedData(Integer provid
9191
return data;
9292
}
9393

94+
@Override
95+
public ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityID(Integer facilityID) {
96+
return v_fetchItemFacilityMapRepo.getItemMappingsByFacilityAndSubStores(facilityID);
97+
}
98+
9499
@Override
95100
public List<ItemInStore> getItemMastersFromStoreID(Integer storeID) {
96101
// TODO Auto-generated method stub

src/main/java/com/iemr/admin/service/itemfacilitymapping/M_itemfacilitymappingInter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public interface M_itemfacilitymappingInter{
3939
ArrayList<M_itemfacilitymapping> getsubitemforsubStote(Integer providerServiceMapID, Integer facilityID);
4040

4141
ArrayList<V_fetchItemFacilityMap> getAllFacilityMappedData(Integer providerServiceMapID);
42-
42+
43+
ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityID(Integer facilityID);
44+
4345
List<ItemInStore> getItemMastersFromStoreID(Integer storeID);
4446

4547
Integer deleteItemStoreMapping(M_itemfacilitymapping storeID);

src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public M_Facility getMainStore(Integer mainStoreID) {
100100
@Override
101101
public List<M_Facility> getAllMainStore(Integer providerServiceMapID) {
102102
// TODO Auto-generated method stub
103-
return (List<M_Facility>) mainStoreRepo.findByProviderServiceMapIDOrderByFacilityName(providerServiceMapID);
103+
return (List<M_Facility>) mainStoreRepo.findByProviderServiceMapIDOrNullOrderByFacilityName(providerServiceMapID);
104104
}
105105

106106
// @Override
@@ -321,6 +321,15 @@ public M_Facility createFacilityWithHierarchy(M_Facility facility, List<Integer>
321321
child.setParentFacilityID(savedFacility.getFacilityID());
322322
child.setModifiedBy(facility.getCreatedBy());
323323
mainStoreRepo.save(child);
324+
325+
// Only update store fields for NEW hierarchy facilities (PSMID is NULL)
326+
// Existing stores (PSMID set) keep their store chain intact for inventory compatibility
327+
if (child.getProviderServiceMapID() == null) {
328+
if (child.getIsMainFacility() == null || child.getIsMainFacility()) {
329+
mainStoreRepo.updateStoreFields(childID, false,
330+
savedFacility.getFacilityID(), "SUB");
331+
}
332+
}
324333
}
325334
}
326335
}
@@ -356,11 +365,16 @@ public M_Facility deleteFacilityWithHierarchy(Integer facilityID, String modifie
356365
throw new Exception("Facility not found");
357366
}
358367
// Fix 19: clear parentFacilityID on children (unlink from hierarchy, don't block)
368+
// Revert children to MainStore since parent is being deleted
359369
ArrayList<M_Facility> children = mainStoreRepo.findByParentFacilityIDAndDeletedFalseOrderByFacilityName(facilityID);
360370
for (M_Facility child : children) {
361371
child.setParentFacilityID(null);
362372
child.setModifiedBy(modifiedBy);
363373
mainStoreRepo.save(child);
374+
// Only revert store fields for new facilities (PSMID NULL)
375+
if (child.getProviderServiceMapID() == null) {
376+
mainStoreRepo.updateStoreFields(child.getFacilityID(), true, null, "MAIN");
377+
}
364378
}
365379
facility.setDeleted(true);
366380
facility.setModifiedBy(modifiedBy);
@@ -386,14 +400,39 @@ public M_Facility updateFacilityWithHierarchy(M_Facility facility, List<Integer>
386400
throw new RuntimeException("Facility not found");
387401
}
388402

389-
if (mainStoreRepo.existsByFacilityNameAndBlockIDAndNotFacilityID(facility.getFacilityName(), existing.getBlockID(), facility.getFacilityID())) {
390-
throw new RuntimeException("Facility with this name already exists in this block");
403+
if (existing.getBlockID() != null && facility.getFacilityName() != null) {
404+
if (mainStoreRepo.existsByFacilityNameAndBlockIDAndNotFacilityID(facility.getFacilityName(), existing.getBlockID(), facility.getFacilityID())) {
405+
throw new RuntimeException("Facility with this name already exists in this block");
406+
}
391407
}
392408

393-
existing.setFacilityName(facility.getFacilityName());
394-
existing.setFacilityDesc(facility.getFacilityDesc());
395-
existing.setFacilityCode(facility.getFacilityCode());
396-
// Rural/Urban and FacilityType are read-only on edit — admin must delete and recreate to change
409+
if (facility.getFacilityName() != null) {
410+
existing.setFacilityName(facility.getFacilityName());
411+
}
412+
if (facility.getFacilityDesc() != null) {
413+
existing.setFacilityDesc(facility.getFacilityDesc());
414+
}
415+
if (facility.getFacilityCode() != null) {
416+
existing.setFacilityCode(facility.getFacilityCode());
417+
}
418+
// Set hierarchy fields: facilityType, ruralUrban, and location
419+
if (facility.getFacilityTypeID() != null) {
420+
existing.setFacilityTypeID(facility.getFacilityTypeID());
421+
}
422+
if (facility.getRuralUrban() != null) {
423+
existing.setRuralUrban(facility.getRuralUrban());
424+
}
425+
if (facility.getStateID() != null) {
426+
existing.setStateID(facility.getStateID());
427+
}
428+
if (facility.getDistrictID() != null) {
429+
existing.setDistrictID(facility.getDistrictID());
430+
}
431+
if (facility.getBlockID() != null) {
432+
existing.setBlockID(facility.getBlockID());
433+
}
434+
// Keep store relationships intact (isMainFacility, mainFacilityID, storeType)
435+
// Only hierarchy columns are added. Store chain stays for inventory compatibility.
397436
existing.setMainVillageID(mainVillageID);
398437
existing.setModifiedBy(facility.getModifiedBy());
399438
M_Facility savedFacility = mainStoreRepo.save(existing);
@@ -458,13 +497,33 @@ public M_Facility updateFacilityWithHierarchy(M_Facility facility, List<Integer>
458497
}
459498
}
460499
}
500+
// Revert old children to MainStore before re-linking
501+
ArrayList<M_Facility> oldChildren = mainStoreRepo
502+
.findByParentFacilityIDAndDeletedFalseOrderByFacilityName(facility.getFacilityID());
461503
mainStoreRepo.clearParentFacilityID(facility.getFacilityID(), facility.getModifiedBy());
504+
for (M_Facility oldChild : oldChildren) {
505+
if (!childFacilityIDs.contains(oldChild.getFacilityID())) {
506+
// Child was removed — revert to MainStore only for new facilities (PSMID NULL)
507+
if (oldChild.getProviderServiceMapID() == null) {
508+
mainStoreRepo.updateStoreFields(oldChild.getFacilityID(), true, null, "MAIN");
509+
}
510+
}
511+
}
512+
462513
for (Integer childID : childFacilityIDs) {
463514
M_Facility child = mainStoreRepo.findByFacilityID(childID);
464515
if (child != null) {
465516
child.setParentFacilityID(savedFacility.getFacilityID());
466517
child.setModifiedBy(facility.getModifiedBy());
467518
mainStoreRepo.save(child);
519+
520+
// Only update store fields for NEW hierarchy facilities (PSMID is NULL)
521+
if (child.getProviderServiceMapID() == null) {
522+
if (child.getIsMainFacility() == null || child.getIsMainFacility()) {
523+
mainStoreRepo.updateStoreFields(childID, false,
524+
savedFacility.getFacilityID(), "SUB");
525+
}
526+
}
468527
}
469528
}
470529
}

0 commit comments

Comments
 (0)