| 1 | 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
|
|---|
| 2 | , tr.regulation_type_code, g.country_code, g.adm1, cvl.title
|
|---|
| 3 | FROM taxonomy_regulation_map trm
|
|---|
| 4 | LEFT JOIN taxonomy_regulation tr ON tr.taxonomy_regulation_id = trm.taxonomy_regulation_id
|
|---|
| 5 | LEFT JOIN geography g ON g.geography_id = tr.geography_id
|
|---|
| 6 | LEFT JOIN code_value cv ON g.country_code = cv.value AND cv.group_name = 'GEOGRAPHY_COUNTRY_CODE'
|
|---|
| 7 | LEFT JOIN code_value_lang cvl ON cv.code_value_id = cvl.code_value_id and cvl.sys_lang_id = 1
|
|---|
| 8 | LEFT JOIN taxonomy_species ts ON ts.taxonomy_species_id = trm.taxonomy_species_id
|
|---|
| 9 | LEFT JOIN taxonomy_species rts ON rts.taxonomy_species_id = ts.current_taxonomy_species_id
|
|---|
| 10 | LEFT join taxonomy_genus tg ON tg.taxonomy_genus_id = trm.taxonomy_genus_id
|
|---|
| 11 | LEFT JOIN taxonomy_genus rtg ON rtg.taxonomy_genus_id = COALESCE(rts.taxonomy_genus_id, tg.current_taxonomy_genus_id)
|
|---|
| 12 | LEFT JOIN taxonomy_family rtf ON rtf.taxonomy_family_id = COALESCE(rtg.taxonomy_family_id, trm.taxonomy_family_id)
|
|---|
| 13 | WHERE COALESCE(trm.is_exempt, 'N') != 'Y'
|
|---|
| 14 | )
|
|---|
| 15 | SELECT
|
|---|
| 16 | oi.order_request_item_id
|
|---|
| 17 | ,oi.order_request_id
|
|---|
| 18 | ,oi.sequence_number
|
|---|
| 19 | ,i.accession_id
|
|---|
| 20 | ,oi.inventory_id
|
|---|
| 21 | ,(select c.site_id from cooperator c where c.cooperator_id = i.owned_by) AS site_id
|
|---|
| 22 | ,oi.name
|
|---|
| 23 | ,oi.external_taxonomy
|
|---|
| 24 | ,(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
|
|---|
| 25 | ,i.quantity_on_hand
|
|---|
| 26 | ,i.quantity_on_hand_unit_code
|
|---|
| 27 | ,i.availability_status_code
|
|---|
| 28 | ,oi.quantity_shipped
|
|---|
| 29 | ,oi.quantity_shipped_unit_code
|
|---|
| 30 | ,oi.distribution_form_code
|
|---|
| 31 | ,oi.status_code
|
|---|
| 32 | ,oi.status_date
|
|---|
| 33 | ,oi.note
|
|---|
| 34 | ,oi.source_cooperator_id
|
|---|
| 35 | ,i.distribution_default_quantity AS distribution_default_quantity
|
|---|
| 36 | ,imp.distribution_default_quantity AS policy_distribution_default_quantity
|
|---|
| 37 | ,(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
|
|---|
| 38 | ,(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
|
|---|
| 39 | ,(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
|
|---|
| 40 | ,(SELECT STRING_AGG(regtext, ', ') FROM
|
|---|
| 41 | (SELECT DISTINCT regulation_type_code + ' in ' + COALESCE(treg.adm1, '') + ', ' + treg.country_code AS regtext
|
|---|
| 42 | FROM treg
|
|---|
| 43 | WHERE regulation_type_code IN ('SEED', 'TERRESTRIAL', 'AQUATIC', 'PARASITIC')
|
|---|
| 44 | AND treg.family_name = tf.family_name
|
|---|
| 45 | AND COALESCE(treg.genus_name, tg.genus_name) = tg.genus_name
|
|---|
| 46 | AND COALESCE(treg.species_name, ts.species_name) = ts.species_name
|
|---|
| 47 | AND COALESCE(treg.subspecies_name, ts.subspecies_name, '') = COALESCE(ts.subspecies_name, '')
|
|---|
| 48 | AND COALESCE(treg.variety_name, ts.variety_name, '')= COALESCE(ts.variety_name, '')
|
|---|
| 49 | AND COALESCE(treg.subvariety_name, ts.subvariety_name, '')= COALESCE(ts.subvariety_name, '')
|
|---|
| 50 | AND COALESCE(treg.forma_name, ts.forma_name, '')= COALESCE(ts.forma_name, '')
|
|---|
| 51 | AND treg.country_code = g3.country_code AND COALESCE(treg.adm1, g3.adm1, '') = COALESCE(g3.adm1,'')
|
|---|
| 52 | ) disttreg
|
|---|
| 53 | ) AS noxious_restriction
|
|---|
| 54 | ,(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
|
|---|
| 55 | ,oi.web_order_request_item_id
|
|---|
| 56 | ,i.inventory_number_part1
|
|---|
| 57 | ,i.inventory_number_part2
|
|---|
| 58 | ,i.inventory_number_part3
|
|---|
| 59 | ,i.inventory_maint_policy_id
|
|---|
| 60 | ,(SELECT taxonomy_species_id FROM accession a WHERE i.accession_id = a.accession_id) AS taxonomy_species_id
|
|---|
| 61 | ,(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
|
|---|
| 62 | ,i.storage_location_part1
|
|---|
| 63 | ,i.storage_location_part2
|
|---|
| 64 | ,i.storage_location_part3
|
|---|
| 65 | ,i.storage_location_part4
|
|---|
| 66 | FROM
|
|---|
| 67 | order_request_item oi
|
|---|
| 68 | INNER JOIN inventory i ON oi.inventory_id = i.inventory_id
|
|---|
| 69 | INNER JOIN inventory_maint_policy imp ON i.inventory_maint_policy_id = imp.inventory_maint_policy_id
|
|---|
| 70 | INNER JOIN accession a ON a.accession_id = i.accession_id
|
|---|
| 71 | INNER JOIN taxonomy_species ts ON ts.taxonomy_species_id = a.taxonomy_species_id
|
|---|
| 72 | INNER JOIN taxonomy_genus tg ON tg.taxonomy_genus_id = ts.taxonomy_genus_id
|
|---|
| 73 | INNER JOIN taxonomy_family tf ON tf.taxonomy_family_id = tg.taxonomy_family_id
|
|---|
| 74 | INNER JOIN order_request o ON o.order_request_id = oi.order_request_id
|
|---|
| 75 | LEFT JOIN cooperator c3 ON o.final_recipient_cooperator_id = c3.cooperator_id
|
|---|
| 76 | LEFT JOIN geography g3 ON c3.geography_id = g3.geography_id
|
|---|
| 77 | WHERE oi.order_request_id IN (:orderrequestid) |
|---|