Skip to content

Commit

Permalink
Merge pull request #5883 from msupply-foundation/5841-gaps-item-impor…
Browse files Browse the repository at this point in the history
…t-script-is-incorrect

5841 gaps item import script is incorrect
  • Loading branch information
jmbrunskill authored Jan 7, 2025
2 parents 2a973ba + 5150763 commit 46bb407
Show file tree
Hide file tree
Showing 7 changed files with 6,926 additions and 1,486 deletions.
2 changes: 1 addition & 1 deletion server/gaps/items/create_gaps_item_sql_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@

# save the generated ids to a file
with open(id_lookup_file, 'w') as file:
json.dump(id_lookup, file)
json.dump(id_lookup, file, indent=1)
110 changes: 110 additions & 0 deletions server/gaps/items/gaps_code_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import csv
import re
import json

###### THIS SCRIPT WAS USED TO GENERATE ITEM CODES FOR VACCINE ITEMS IT IS NOT INTENDED TO BE RUN AGAIN ######

item_variants_file_path = 'gaps_item_variants.csv'

code_lookup = {}
old_code = {}
code_matched = {}


id_lookup_file = 'id_lookup.json'

id_lookup = {}

item_id_inserted = {}

# Load saved data
try:
with open(id_lookup_file, 'r') as file:
id_lookup = json.load(file)
except:
print("No id_lookup file found, starting fresh")


def create_prefix(vaccine_type_name, mSupply_item_name):

if(len(vaccine_type_name) <= 5):
return vaccine_type_name.upper().replace(",", "")

prefix = prefix=mSupply_item_name[0:3].upper()
if "DTwP" in vaccine_type_name:
prefix = f"DTWwP{len(vaccine_type_name)}"


if "Tetanus Toxoid" in mSupply_item_name:
prefix = f"{prefix}-TT"

if "ACYWX" in mSupply_item_name:
prefix = f"{prefix}-X"

# Find anything in brackets/clarifyer
m = re.search(r"\((.*)\)", vaccine_type_name)
if m:
prefix = f"{prefix}{len(m.group(1))}"
return prefix


def create_presentation(presentation):
# print(presentation)
presentation_section = ""
if "vial set" in presentation.lower():
presentation_section = "VS"
elif "vial" in presentation.lower():
presentation_section="V"
if "ampoule" in presentation.lower():
presentation_section = presentation_section + "A"
if "prefilled syringe" in presentation.lower():
presentation_section = presentation_section + "S"
if "uniject" in presentation.lower():
presentation_section = presentation_section + "U"
if "plastic tube" == presentation.lower():
presentation_section = presentation_section + "PT"
if "buffer sachet" in presentation.lower():
presentation_section = presentation_section + "BS"

if len(presentation_section) == 0:
presentation_section = len(presentation)

# print(presentation_section)
return presentation_section


# Main csv processing loop
with open(item_variants_file_path, 'r') as file:
reader = csv.DictReader(file)
for row in reader:

item_name = row['mSupply item name']

first_part = create_prefix(row["VaccineTypeName"],row['mSupply item name'])
middle = create_presentation(row['Presentation'])
last = row['DosesCount']

code = f"{first_part}-{middle}-{last}"

# code = row['mSupply item code']
if code in code_lookup:
if code_lookup[code] != row['mSupply item name']:
print(f"Duplicate code, {code}, \"{code_lookup[code]}\" , \"{row['mSupply item name']}\"")
code_lookup[code] = row['mSupply item name']
old_code[code] = row['mSupply item code']

if not row['mSupply item code'] in code_matched:
if row['mSupply item code'] in id_lookup:
id_lookup[code] = id_lookup[row['mSupply item code']]
code_matched[row['mSupply item code']] = True
else:
print(f"Missing code {row['mSupply item code']}")

for key, value in code_lookup.items():
old_code_value = old_code[key]
print(f"\"{value}\",\"{key}\", \"{old_code_value}\"")


# save the generated ids to a file
with open(id_lookup_file, 'w') as file:
json.dump(id_lookup, file)
18 changes: 12 additions & 6 deletions server/gaps/items/gaps_insert_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
def changelog_stmt(record_id, table_name):
# Thank you to whoever set the default value of cursor in ChangeLog to properly generate the next value!
# nextval('changelog_cursor_seq'::regclass)
return f"INSERT INTO changelog (record_id, table_name, row_action) VALUES ('{record_id}', '{table_name}', 'UPSERT');"
return f"INSERT INTO changelog (record_id, table_name, row_action) VALUES ('{record_id}', '{table_name}', 'UPSERT');\n"


def item_link_stmt(item_id):
Expand Down Expand Up @@ -123,25 +123,31 @@ def upsert_vaccine_packaging_variant_stmt(packaging_variant_id, item_variant_id,
if volume_per_unit == "":
volume_per_unit = 0

return f"INSERT INTO packaging_variant (id, item_variant_id, name, packaging_level, volume_per_unit) VALUES ('{packaging_variant_id}', '{item_variant_id}', '{packaging_name}', {packaging_level}, {volume_per_unit}) ON CONFLICT DO NOTHING;\n"
insert_statement = f"INSERT INTO packaging_variant (id, item_variant_id, name, packaging_level, volume_per_unit) VALUES ('{packaging_variant_id}', '{item_variant_id}', '{packaging_name}', {packaging_level}, {volume_per_unit}) ON CONFLICT DO NOTHING;\n"

return insert_statement + changelog_stmt(packaging_variant_id, 'packaging_variant')




def upsert_diluent_packaging_variant_stmt(packaging_variant_id, item_variant_id, row, packaging_level):
packaging_name = get_packaging_variant_name(packaging_level)
volume_per_unit = 0
if packaging_level == 1:
volume_per_unit= row["DiluentPrimaryVolume"]
volume_per_unit= float(row["DiluentPrimaryVolume"]) / 1000
elif packaging_level == 2:
volume_per_unit= row["DiluentSecondaryVolume"]
volume_per_unit= float(row["DiluentSecondaryVolume"]) / 1000
elif packaging_level == 3:
volume_per_unit= row["DiluentTertiaryVolume"]
volume_per_unit= float(row["DiluentTertiaryVolume"]) / 1000
else:
volume_per_unit = 0

if volume_per_unit == "":
volume_per_unit = 0

return f"INSERT INTO packaging_variant (id, item_variant_id, name, packaging_level, volume_per_unit) VALUES ('{packaging_variant_id}', '{item_variant_id}', '{packaging_name}', {packaging_level}, {volume_per_unit}) ON CONFLICT DO NOTHING;\n"
insert_statement = f"INSERT INTO packaging_variant (id, item_variant_id, name, packaging_level, volume_per_unit) VALUES ('{packaging_variant_id}', '{item_variant_id}', '{packaging_name}', {packaging_level}, {volume_per_unit}) ON CONFLICT DO NOTHING;\n"

return insert_statement + changelog_stmt(packaging_variant_id, 'packaging_variant')



Expand Down
Loading

0 comments on commit 46bb407

Please sign in to comment.