Opened 5 years ago

Closed 5 years ago

#2317 closed gap (fixed)

Update dataviews to accommodate the new noxious weed restriction database schema changes

Reported by: Lisa Burke Owned by: Gary Kinard
Priority: medium Milestone:
Component: Dataviews (CT) Version: N/A
Severity: Keywords:
Cc: lisa.burke@…, stacey.estrada@… Database Engine: N/A
Operating System: N/A

Description

The order_wizard_get_order_request_item and order_wizard_get_packet_label dataviews need to be updated to use the new noxious weed restriction database schema.

Attachments (2)

order_wizard_get_packet_label.sql​ (7.2 KB ) - added by Pete Cyr 5 years ago.
order_wizard_get_order_request_item.sql​ (5.1 KB ) - added by Pete Cyr 5 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 by Kurt Endress, 5 years ago

Cc: devteam@… removed
Owner: changed from Kurt Endress to Pete Cyr

Wrong owner. Pete is in charge of Wizard dataviews.

comment:2 by Lisa Burke, 5 years ago

Owner: changed from Pete Cyr to Kurt Endress

It was my understanding that Kurt Endress was responsible for putting the dataviews on the server.

comment:3 by Gary Kinard, 5 years ago

Owner: changed from Kurt Endress to Gary Kinard

If the dataview is updated and ready to deploy, please do so. This is a separate issue from who has primary responsibility to originate updates, fixes, etc. to dataviews that drive the CT. That can be discussed separately. This is about fixing a problem, which is the first order of business. Once on Dev: Ames please test using whatever protocol you use internally. Once validated it can be moved to production immediately.

comment:4 by Kurt Endress, 5 years ago

order_wizard_get_order_request_item_20210709.dataviewxml loaded for testing on npgsDev.

comment:5 by Pete Cyr, 5 years ago

There are two dataviews that need to be addressed with the new SQL logic for properly processing the new database schema changes for noxious weeds. The dataviews are:

order_wizard_get_order_request_item

and

order_wizard_get_packet_label

comment:6 by Pete Cyr, 5 years ago

Cc: stacey.estrada@… added

comment:7 by Pete Cyr, 5 years ago

This is the updated SQL that has been tested on npgsTest1.agron.iastate.edu in Ames. It is confirmed to fix this Trac ticket:

/*
SQL For order_wizard_get_order_request_item...
*/

WITH treg AS (SELECT rtf.family_name, rtg.genus_name, rts.species_name, rts.subspecies_name, rts.variety_name, rts.subvariety_name, rts.forma_name

, tr.regulation_type_code, g.country_code, g.adm1, cvl.title

FROM taxonomy_regulation_map trm
LEFT JOIN taxonomy_regulation tr ON tr.taxonomy_regulation_id = trm.taxonomy_regulation_id
LEFT JOIN geography g ON g.geography_id = tr.geography_id
LEFT JOIN code_value cv ON g.country_code = cv.value AND cv.group_name = 'GEOGRAPHY_COUNTRY_CODE'
LEFT JOIN code_value_lang cvl ON cv.code_value_id = cvl.code_value_id and cvl.sys_lang_id = 1
LEFT JOIN taxonomy_species ts ON ts.taxonomy_species_id = trm.taxonomy_species_id
LEFT JOIN taxonomy_species rts ON rts.taxonomy_species_id = ts.current_taxonomy_species_id
LEFT join taxonomy_genus tg ON tg.taxonomy_genus_id = trm.taxonomy_genus_id
LEFT JOIN taxonomy_genus rtg ON rtg.taxonomy_genus_id = COALESCE(rts.taxonomy_genus_id, tg.current_taxonomy_genus_id)
LEFT JOIN taxonomy_family rtf ON rtf.taxonomy_family_id = COALESCE(rtg.taxonomy_family_id, trm.taxonomy_family_id)
WHERE COALESCE(trm.is_exempt, 'N') != 'Y'

)
SELECT

oi.order_request_item_id
,oi.order_request_id
,oi.sequence_number
,i.accession_id
,oi.inventory_id
,(select c.site_id from cooperator c where c.cooperator_id = i.owned_by) AS site_id
,oi.name
,oi.external_taxonomy
,(select s.geography_id from accession_source s where i.accession_id = s.accession_id and accession_source_id = ( select MIN(accession_source_id) from accession_source s2 where i.accession_id = s2.accession_id and is_origin = 'Y')) AS geography_id
,i.quantity_on_hand
,i.quantity_on_hand_unit_code
,i.availability_status_code
,oi.quantity_shipped
,oi.quantity_shipped_unit_code
,oi.distribution_form_code
,oi.status_code
,oi.status_date
,oi.note
,oi.source_cooperator_id
,i.distribution_default_quantity AS distribution_default_quantity
,imp.distribution_default_quantity AS policy_distribution_default_quantity
,(SELECT SUBSTRING((SELECT ', ' + ipr1.type_code AS [text()] FROM accession_ipr ipr1 WHERE ipr1.accession_id = i.accession_id AND ipr1.expired_date IS NULL ORDER BY ipr1.accession_id FOR XML PATH ()), 2, 1000)) AS ipr_restriction
,(SELECT top 1 'xPVP' FROM accession_ipr ipr2 WHERE ipr2.accession_id = i.accession_id AND ipr2.type_code = 'PVP' AND ipr2.expired_date IS NOT NULL) AS xpvp_warning
,(SELECT SUBSTRING((SELECT ', ' + iq.quarantine_type_code + ' = ' + iq.progress_status_code AS [text()] FROM accession_quarantine iq WHERE iq.accession_id = i.accession_id AND iq.released_date IS NULL ORDER BY iq.accession_id FOR XML PATH (
)), 2, 1000)) AS quarantine_restriction
,(SELECT STRING_AGG(regtext, ', ') FROM

(SELECT DISTINCT regulation_type_code + ' in ' + COALESCE(treg.adm1, ) + ', ' + treg.country_code AS regtext

FROM treg
WHERE regulation_type_code IN ('SEED', 'TERRESTRIAL', 'AQUATIC', 'PARASITIC')

AND treg.family_name = tf.family_name
AND COALESCE(treg.genus_name, tg.genus_name) = tg.genus_name
AND COALESCE(treg.species_name, ts.species_name) = ts.species_name
AND COALESCE(treg.subspecies_name, ts.subspecies_name, ) = COALESCE(ts.subspecies_name, )
AND COALESCE(treg.variety_name, ts.variety_name, )= COALESCE(ts.variety_name, )
AND COALESCE(treg.subvariety_name, ts.subvariety_name, )= COALESCE(ts.subvariety_name, )
AND COALESCE(treg.forma_name, ts.forma_name, )= COALESCE(ts.forma_name, )
AND treg.country_code = g3.country_code AND COALESCE(treg.adm1, g3.adm1, ) = COALESCE(g3.adm1,)

) disttreg

) AS noxious_restriction

,(SELECT top 1 'SMTA Required' FROM accession_ipr ipr3 WHERE ipr3.accession_id = i.accession_id AND ipr3.type_code = 'MTA-SMTA') AS smta_warning
,oi.web_order_request_item_id
,i.inventory_number_part1
,i.inventory_number_part2
,i.inventory_number_part3
,i.inventory_maint_policy_id
,(SELECT taxonomy_species_id FROM accession a WHERE i.accession_id = a.accession_id) AS taxonomy_species_id
,(SELECT taxonomy_genus_id FROM taxonomy_species ts LEFT JOIN accession a ON ts.taxonomy_species_id = a.taxonomy_species_id WHERE i.accession_id = a.accession_id) AS taxonomy_genus_id
,i.storage_location_part1
,i.storage_location_part2
,i.storage_location_part3
,i.storage_location_part4

FROM

order_request_item oi
INNER JOIN inventory i ON oi.inventory_id = i.inventory_id
INNER JOIN inventory_maint_policy imp ON i.inventory_maint_policy_id = imp.inventory_maint_policy_id
INNER JOIN accession a ON a.accession_id = i.accession_id
INNER JOIN taxonomy_species ts ON ts.taxonomy_species_id = a.taxonomy_species_id
INNER JOIN taxonomy_genus tg ON tg.taxonomy_genus_id = ts.taxonomy_genus_id
INNER JOIN taxonomy_family tf ON tf.taxonomy_family_id = tg.taxonomy_family_id
INNER JOIN order_request o ON o.order_request_id = oi.order_request_id
LEFT JOIN cooperator c3 ON o.final_recipient_cooperator_id = c3.cooperator_id
LEFT JOIN geography g3 ON c3.geography_id = g3.geography_id

WHERE oi.order_request_id IN (:orderrequestid)

/*
SQL for order_wizard_get_packet_label...
*/

WITH treg AS (SELECT rtf.family_name, rtg.genus_name, rts.species_name, rts.subspecies_name, rts.variety_name, rts.subvariety_name, rts.forma_name

, tr.regulation_type_code, g.country_code, g.adm1, cvl.title

FROM taxonomy_regulation_map trm
LEFT JOIN taxonomy_regulation tr ON tr.taxonomy_regulation_id = trm.taxonomy_regulation_id
LEFT JOIN geography g ON g.geography_id = tr.geography_id
LEFT JOIN code_value cv ON g.country_code = cv.value AND cv.group_name = 'GEOGRAPHY_COUNTRY_CODE'
LEFT JOIN code_value_lang cvl ON cv.code_value_id = cvl.code_value_id and cvl.sys_lang_id = 1
LEFT JOIN taxonomy_species ts ON ts.taxonomy_species_id = trm.taxonomy_species_id
LEFT JOIN taxonomy_species rts ON rts.taxonomy_species_id = ts.current_taxonomy_species_id
LEFT join taxonomy_genus tg ON tg.taxonomy_genus_id = trm.taxonomy_genus_id
LEFT JOIN taxonomy_genus rtg ON rtg.taxonomy_genus_id = COALESCE(rts.taxonomy_genus_id, tg.current_taxonomy_genus_id)
LEFT JOIN taxonomy_family rtf ON rtf.taxonomy_family_id = COALESCE(rtg.taxonomy_family_id, trm.taxonomy_family_id)
WHERE COALESCE(trm.is_exempt, 'N') != 'Y'

)
SELECT

oi.order_request_item_id
,oi.order_request_id
,oi.sequence_number
,Row_number() Over (PARTITION BY oi.order_request_id ORDER BY oi.order_request_id, (select name from taxonomy_species ts WHERE ts.taxonomy_species_id = a.taxonomy_species_id), inventory_number_part1, inventory_number_part2, inventory_number_part3) AS office_use
,(select min(plant_name) from accession_inv_name an, inventory i where

i.inventory_id = an.inventory_id and i.accession_id = a.accession_id and
plant_name_rank = (select MIN(plant_name_rank) from accession_inv_name an2, inventory i2 where an2.inventory_id = i2.inventory_id and i2.accession_id = a.accession_id)) AS plant_name

,i.accession_id
,i.inventory_number_part1
,i.inventory_number_part2
,i.inventory_number_part3
,o.requestor_cooperator_id
,o.final_recipient_cooperator_id
,o.ship_to_cooperator_id
,imp.distribution_default_quantity
,i.inventory_maint_policy_id
,oi.inventory_id
,(SELECT c.site_id FROM cooperator c WHERE c.cooperator_id = i.owned_by) AS site_id
,(SELECT s.site_long_name FROM site s WHERE s.site_id = (SELECT c.site_id FROM cooperator c WHERE c.cooperator_id = o.owned_by)) AS site_long_name
,oi.name
,oi.external_taxonomy
,(SELECT taxonomy_species_id FROM accession a WHERE i.accession_id = a.accession_id) AS taxonomy_species_id
,(SELECT s.geography_id FROM accession_source s WHERE i.accession_id = s.accession_id AND accession_source_id = (SELECT MIN(accession_source_id) FROM accession_source s2 WHERE i.accession_id = s2.accession_id AND is_origin = 'Y')) AS geography_id
,i.storage_location_part1
,i.storage_location_part2
,i.storage_location_part3
,i.storage_location_part4
,(SELECT COALESCE(i.storage_location_part1, ) + '_' + COALESCE(i.storage_location_part2, ) + '_' + COALESCE(i.storage_location_part3, ) + '_' + COALESCE(i.storage_location_part4, )) AS storage_location
,i.quantity_on_hand
,i.quantity_on_hand_unit_code
,i.availability_status_code
,oi.quantity_shipped
,oi.quantity_shipped_unit_code
,oi.distribution_form_code
,oi.status_code
,oi.status_date
,oi.note
,oi.source_cooperator_id
,i.hundred_seed_weight
,(SELECT i.hundred_seed_weight * oi.quantity_shipped / 100) AS order_weight
,(SELECT TOP 1 percent_viable FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS percent_viable
,(SELECT TOP 1 percent_normal FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS percent_normal
,(SELECT TOP 1 percent_dormant FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS percent_dormant
,(SELECT TOP 1 tested_date FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC) AS tested_date
,(SELECT TOP 1 inventory_viability_rule_id FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS inventory_viability_rule_id
,(SELECT SUBSTRING((SELECT ', ' + ipr1.type_code AS [text()] FROM accession_ipr ipr1 WHERE ipr1.accession_id = i.accession_id AND ipr1.expired_date IS NULL ORDER BY ipr1.accession_id FOR XML PATH ()), 2, 1000)) AS ipr_restriction
,(SELECT DISTINCT 'xPVP' FROM accession_ipr ipr2 WHERE ipr2.accession_id = i.accession_id AND ipr2.type_code = 'PVP' AND ipr2.expired_date IS NOT NULL) AS xpvp_warning
,(SELECT SUBSTRING((SELECT ', ' + iq.quarantine_type_code + ' = ' + iq.progress_status_code AS [text()] FROM accession_quarantine iq WHERE iq.accession_id = i.accession_id AND iq.released_date IS NULL ORDER BY iq.accession_id FOR XML PATH (
)), 2, 1000)) AS quarantine_restriction
,(SELECT STRING_AGG(regtext, ', ') FROM

(SELECT DISTINCT regulation_type_code + ' in ' + COALESCE(treg.adm1, ) + ', ' + treg.country_code AS regtext

FROM treg
WHERE regulation_type_code IN ('SEED', 'TERRESTRIAL', 'AQUATIC', 'PARASITIC')

AND treg.family_name = tf.family_name
AND COALESCE(treg.genus_name, tg.genus_name) = tg.genus_name
AND COALESCE(treg.species_name, ts.species_name) = ts.species_name
AND COALESCE(treg.subspecies_name, ts.subspecies_name, ) = COALESCE(ts.subspecies_name, )
AND COALESCE(treg.variety_name, ts.variety_name, )= COALESCE(ts.variety_name, )
AND COALESCE(treg.subvariety_name, ts.subvariety_name, )= COALESCE(ts.subvariety_name, )
AND COALESCE(treg.forma_name, ts.forma_name, )= COALESCE(ts.forma_name, )
AND treg.country_code = g3.country_code AND COALESCE(treg.adm1, g3.adm1, ) = COALESCE(g3.adm1,)

) disttreg

) AS noxious_restriction

,(SELECT taxonomy_genus_id FROM taxonomy_species ts LEFT JOIN accession a ON ts.taxonomy_species_id = a.taxonomy_species_id WHERE i.accession_id = a.accession_id) AS taxonomy_genus_id
,oi.web_order_request_item_id
,(SELECT a.accession_number_part1 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part1
,(SELECT a.accession_number_part2 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part2
,(SELECT a.accession_number_part3 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part3

FROM

order_request_item oi
INNER JOIN inventory i ON oi.inventory_id = i.inventory_id
INNER JOIN inventory_maint_policy imp ON i.inventory_maint_policy_id = imp.inventory_maint_policy_id
INNER JOIN accession a ON a.accession_id = i.accession_id
INNER JOIN taxonomy_species ts ON ts.taxonomy_species_id = a.taxonomy_species_id
INNER JOIN taxonomy_genus tg ON tg.taxonomy_genus_id = ts.taxonomy_genus_id
INNER JOIN taxonomy_family tf ON tf.taxonomy_family_id = tg.taxonomy_family_id
INNER JOIN order_request o ON o.order_request_id = oi.order_request_id
LEFT JOIN cooperator c3 ON o.final_recipient_cooperator_id = c3.cooperator_id
LEFT JOIN geography g3 ON c3.geography_id = g3.geography_id

WHERE oi.order_request_id IN (:orderrequestid)
AND oi.quantity_shipped > 0
ORDER BY oi.sequence_number

comment:8 by Pete Cyr, 5 years ago

NOTE: The Trac system inadvertently removed the empty single quotes (like ) from the SQL above so do not use the SQL in the comment above. Instead use the SQL in the attached files (named order_wizard_get_packet_label.sql and order_wizard_get_order_request_item).

by Pete Cyr, 5 years ago

comment:9 by Kurt Endress, 5 years ago

Dataviews order_wizard_get_order_request_item_20210727.dataviewxml and
order_wizard_get_packet_label_20210831.dataviewxml from Pete's attached SQL loaded on npgsDev for testing.

comment:10 by Lisa Burke, 5 years ago

Resolution: → fixed
Status: assigned → closed
Note: See TracTickets for help on using tickets.