Skip to content

Commit

Permalink
Merge pull request #79 from bcgov/feature/3.5.0
Browse files Browse the repository at this point in the history
Feature/3.5.0
  • Loading branch information
vivid-esnauffer authored Oct 17, 2024
2 parents 57e8139 + 418c351 commit 9375c30
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void testRolloverGrainInventoryContract() throws CirrasUnderwritingServic
Assert.assertEquals(referrer.getContractId(), invContract.getContractId());
Assert.assertEquals(referrer.getCropYear(), invContract.getCropYear());
Assert.assertEquals(false, invContract.getFertilizerInd());
Assert.assertEquals(false, invContract.getGrainFromPrevYearInd());
Assert.assertNull(invContract.getGrainFromPrevYearInd()); //No default is set anymore
Assert.assertEquals(false, invContract.getHerbicideInd());
Assert.assertEquals(null, invContract.getInventoryContractGuid());
Assert.assertEquals(null, invContract.getOtherChangesComment());
Expand All @@ -222,15 +222,20 @@ public void testRolloverGrainInventoryContract() throws CirrasUnderwritingServic
Assert.assertEquals(1, field.getDisplayOrder().intValue());

Assert.assertNotNull(field.getPlantings());
Assert.assertEquals("More plantings than expected returned", 5, field.getPlantings().size());
Assert.assertEquals("More plantings than expected returned", 7, field.getPlantings().size());

for (InventoryField invField: field.getPlantings()) {

Assert.assertFalse("is hidden on printout ind is not false", invField.getIsHiddenOnPrintoutInd());

Integer lastYearCmdtyId = invField.getLastYearCropCommodityId();
Integer lastYearVrtyId = invField.getLastYearCropVarietyId();

//Check if commodity is oat or canola
Assert.assertTrue("commodity id ("+lastYearCmdtyId+") not 24, 18, 65, 71 or none", lastYearCmdtyId == null || lastYearCmdtyId.equals(24) || lastYearCmdtyId.equals(18)|| lastYearCmdtyId.equals(65)|| lastYearCmdtyId.equals(71));
Assert.assertTrue("commodity id ("+lastYearCmdtyId+") not 24, 18, 65, 71, 1010893 or none", lastYearCmdtyId == null
|| lastYearCmdtyId.equals(24) || lastYearCmdtyId.equals(18)
|| lastYearCmdtyId.equals(65)|| lastYearCmdtyId.equals(71)
|| lastYearCmdtyId.equals(1010893));

Assert.assertNull("Unseeded crop commodity not null", invField.getInventoryUnseeded().getCropCommodityId());

Expand All @@ -256,7 +261,7 @@ public void testRolloverGrainInventoryContract() throws CirrasUnderwritingServic
Assert.assertNull(invField.getLastYearCropVarietyId());
Assert.assertEquals("Canola not correct acres", (Double)50.0, invField.getInventoryUnseeded().getAcresToBeSeeded());
Assert.assertTrue("Unseeded Insurable not true", invField.getInventoryUnseeded().getIsUnseededInsurableInd());
} else if (lastYearCmdtyId.equals(65)) {
} else if (lastYearCmdtyId.equals(65) && lastYearVrtyId != null && lastYearVrtyId.equals(119)) {
//Forage - From underseeded Alfalfa
Assert.assertEquals("ALFALFA", invField.getLastYearCropVarietyName());
Assert.assertEquals(119, invField.getLastYearCropVarietyId().intValue());
Expand All @@ -270,6 +275,20 @@ public void testRolloverGrainInventoryContract() throws CirrasUnderwritingServic
Assert.assertEquals("SILAGE CORN", invField.getLastYearCropCommodityName());
Assert.assertEquals("SILAGE CORN not correct acres", (Double)34.0, invField.getInventoryUnseeded().getAcresToBeSeeded());
Assert.assertTrue("Unseeded Insurable not true", invField.getInventoryUnseeded().getIsUnseededInsurableInd());
} else if (lastYearCmdtyId.equals(65) && lastYearVrtyId != null && lastYearVrtyId.equals(220)) {
//Underseeded forage seeded with CLOVER/GRASS
Assert.assertEquals("CLOVER/GRASS", invField.getLastYearCropVarietyName());
Assert.assertEquals(220, invField.getLastYearCropVarietyId().intValue());
Assert.assertEquals("FORAGE", invField.getLastYearCropCommodityName());
Assert.assertEquals("FORAGE CLOVER/GRASS not correct acres", (Double)90.0, invField.getInventoryUnseeded().getAcresToBeSeeded());
Assert.assertFalse("IsGrainUnseededDefaultInd", invField.getInventoryUnseeded().getIsUnseededInsurableInd());
} else if (lastYearCmdtyId.equals(1010893)) {
//FORAGE SEED - Brome
Assert.assertEquals("FORAGE SEED", invField.getLastYearCropCommodityName());
Assert.assertEquals("BROME", invField.getLastYearCropVarietyName());
Assert.assertEquals(1010999, invField.getLastYearCropVarietyId().intValue());
Assert.assertEquals("FORAGE SEED not correct acres", (Double)28.0, invField.getInventoryUnseeded().getAcresToBeSeeded());
Assert.assertFalse("IsGrainUnseededDefaultInd", invField.getInventoryUnseeded().getIsUnseededInsurableInd()); //Expected false because it's a forage commodity
}
}

Expand Down Expand Up @@ -337,14 +356,24 @@ public void testRolloverGrainInventoryContract() throws CirrasUnderwritingServic
INSERT INTO cuws.inventory_field(inventory_field_guid, insurance_plan_id, field_id, last_year_crop_commodity_id, crop_year, planting_number, underseeded_crop_variety_id, underseeded_acres, create_user, create_date, update_user, update_date, is_hidden_on_printout_ind)
VALUES ('AbcDeFg12394039485-5', 4, 123456999, null, 2021, 5, 1010863, 34, 'admin', now(), 'admin', now(), 'Y');
INSERT INTO cuws.inventory_field(inventory_field_guid, insurance_plan_id, field_id, last_year_crop_commodity_id, crop_year, planting_number, create_user, create_date, update_user, update_date, is_hidden_on_printout_ind)
VALUES ('AbcDeFg12394039485-6', 4, 123456999, null, 2021, 6, 'admin', now(), 'admin', now(), 'Y');
INSERT INTO cuws.inventory_field(inventory_field_guid, insurance_plan_id, field_id, last_year_crop_commodity_id, crop_year, planting_number, underseeded_crop_variety_id, underseeded_acres, create_user, create_date, update_user, update_date, is_hidden_on_printout_ind)
VALUES ('AbcDeFg12394039485-7', 4, 123456999, null, 2021, 7, 220, 50, 'admin', now(), 'admin', now(), 'Y');
INSERT INTO cuws.inventory_field(inventory_field_guid, insurance_plan_id, field_id, last_year_crop_commodity_id, crop_year, planting_number, underseeded_crop_variety_id, underseeded_acres, create_user, create_date, update_user, update_date, is_hidden_on_printout_ind)
VALUES ('AbcDeFg12394039485-8', 4, 123456999, null, 2021, 8, 220, 40, 'admin', now(), 'admin', now(), 'Y');
--Plantings
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-1', 24, 1010570, 'Forage Oat', 'N', 'N', 'N', null, 25, 'N', 'admin', now(), 'admin', now());
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-1', 26, 1010603, 'CPSW', 'N', 'Y', 'N', null, 0, 'N', 'admin', now(), 'admin', now());
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-2', 18, 1010471, 'Argentine Canola', 'N', 'N', 'N', null, 40, 'N', 'admin', now(), 'admin', now());
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-2', 18, 1010940, 'Polish Canola', 'N', 'N', 'N', null, 40, 'N', 'admin', now(), 'admin', now());
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-3', null, null, null, 'N', 'N', 'N', null, 30, 'N', 'admin', now(), 'admin', now());
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
Expand All @@ -353,6 +382,15 @@ public void testRolloverGrainInventoryContract() throws CirrasUnderwritingServic
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-4', 18, 1010471, 'Argentine Canola', 'N', 'Y', 'N', null, 100, 'N', 'admin', now(), 'admin', now());
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-5', 18, null, null, 'N', 'Y', 'N', null, null, 'N', 'admin', now(), 'admin', now());
--Variety: BROME Commodity: FORAGE SEED
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-6', 1010893, 1010999, 'Forage Seed', 'N', 'Y', 'N', null, 28, 'N', 'admin', now(), 'admin', now());
--Variety: TIMOTHY Commodity: FORAGE SEED -> Underseeded with Clover/Grass
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-7', 1010893, 1010998, 'Forage Seed', 'N', 'Y', 'N', null, 17.5, 'N', 'admin', now(), 'admin', now());
--Grain commodity with underseeded Clover/Grass -> Should merge with AbcDeFg12394039485-7
INSERT INTO cuws.inventory_seeded_grain (inventory_seeded_grain_guid, inventory_field_guid, crop_commodity_id, crop_variety_id, commodity_type_code, is_quantity_insurable_ind, is_replaced_ind, is_pedigree_ind, seeding_date, seeded_acres, is_spot_loss_insurable_ind, create_user, create_date, update_user, update_date)
VALUES (replace(cast(gen_random_uuid() as text), '-', ''), 'AbcDeFg12394039485-8', 18, 1010471, 'Argentine Canola', 'N', 'N', 'N', null, 40, 'N', 'admin', now(), 'admin', now());
*****************
DELETE STATEMENTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,59 +277,109 @@

<sql id="selectForRolloverGrain">
-- All plantings for this field
WITH plantings AS (
SELECT inv.inventory_field_guid,
inv.field_id,
inv.is_hidden_on_printout_ind,
inv.underseeded_crop_variety_id,
inv.underseeded_acres
FROM inventory_field inv
WHERE inv.field_id = #{fieldId}
AND inv.crop_year = #{cropYear}
AND inv.insurance_plan_id = #{insurancePlanId}
)
-- Only returns records if seeded acres > 0 exists for this field.
SELECT p.field_id,
ROW_NUMBER() OVER() AS planting_number,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN isg.crop_commodity_id ELSE v.crop_commodity_id END AS last_year_crop_commodity_id,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN c.commodity_name ELSE cv.commodity_name END AS last_year_crop_commodity_name,
p.underseeded_crop_variety_id AS last_year_crop_variety_id,
v.variety_name AS last_year_crop_variety_name,
vi.is_grain_unseeded_default_ind,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN SUM(isg.seeded_acres) ELSE SUM(p.underseeded_acres) END AS acres_to_be_seeded,
-- Rollover for is_hidden_on_printout_ind is based on ALL plantings,
-- not just those with seeded acres > 0. Set to Y if all plantings are Y,
-- otherwise set to N.
(SELECT MIN(all_p.is_hidden_on_printout_ind) FROM plantings all_p) AS is_hidden_on_printout_ind
FROM plantings p
LEFT JOIN (SELECT isg2.inventory_seeded_grain_guid, isg2.inventory_field_guid, isg2.seeded_acres, isg2.crop_commodity_id
FROM inventory_seeded_grain isg2
WHERE isg2.seeded_acres > 0) as isg
ON isg.inventory_field_guid = p.inventory_field_guid
LEFT JOIN crop_commodity c ON c.crop_commodity_id = isg.crop_commodity_id
LEFT JOIN crop_variety v ON v.crop_variety_id = p.underseeded_crop_variety_id
LEFT JOIN crop_commodity cv ON cv.crop_commodity_id = v.crop_commodity_id
LEFT JOIN crop_variety_insurability vi ON vi.crop_variety_id = v.crop_variety_id
WHERE ((isg.seeded_acres > 0 and v.crop_variety_id is null and isg.inventory_seeded_grain_guid is not null) or p.underseeded_crop_variety_id is not null)
GROUP BY p.field_id, isg.crop_commodity_id, c.commodity_name, p.underseeded_crop_variety_id, v.variety_name, v.crop_commodity_id, cv.commodity_name, vi.is_grain_unseeded_default_ind
UNION
-- Only returns if no seeded acres > 0 exists for this field.
SELECT p.field_id,
1 AS planting_number,
null AS last_year_crop_commodity_id,
null AS last_year_crop_commodity_name,
null AS acres_to_be_seeded,
null AS last_year_crop_variety_id,
null AS last_year_crop_variety_name,
null AS is_grain_unseeded_default_ind,
-- Set to Y if all plantings are Y, otherwise set to N.
MIN(p.is_hidden_on_printout_ind) AS is_hidden_on_printout_ind
FROM plantings p
WHERE NOT EXISTS (SELECT 1
FROM plantings p2
JOIN inventory_seeded_grain isg2 ON isg2.inventory_field_guid = p2.inventory_field_guid
WHERE (isg2.seeded_acres > 0 and p.underseeded_crop_variety_id is null) or p.underseeded_crop_variety_id is not null)
GROUP BY p.field_id
SELECT field_id,
ROW_NUMBER() OVER() AS planting_number,--planting_number,
last_year_crop_commodity_id,
last_year_crop_commodity_name,
SUM(acres_to_be_seeded) AS acres_to_be_seeded,
last_year_crop_variety_id,
last_year_crop_variety_name,
is_grain_unseeded_default_ind,
is_hidden_on_printout_ind
FROM (
WITH plantings AS (
SELECT inv.inventory_field_guid,
inv.field_id,
inv.is_hidden_on_printout_ind,
inv.underseeded_crop_variety_id,
inv.underseeded_acres,
v.variety_name AS underseeded_crop_variety_name,
cv.crop_commodity_id AS underseeded_crop_commodity_id,
cv.commodity_name AS underseeded_crop_commodity_name,
cvi.is_grain_unseeded_default_ind AS underseeded_is_grain_unseeded_default_ind
FROM inventory_field inv
LEFT JOIN crop_variety v ON v.crop_variety_id = inv.underseeded_crop_variety_id
LEFT JOIN crop_commodity cv ON cv.crop_commodity_id = v.crop_commodity_id
LEFT JOIN crop_variety_insurability cvi ON cvi.crop_variety_id = v.crop_variety_id
WHERE inv.field_id = #{fieldId}
AND inv.crop_year = #{cropYear}
AND inv.insurance_plan_id = #{insurancePlanId}
)
-- Only returns records if seeded acres > 0 exists for this field.
SELECT p.field_id,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN isg.crop_commodity_id ELSE p.underseeded_crop_commodity_id END AS last_year_crop_commodity_id,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN c.commodity_name ELSE p.underseeded_crop_commodity_name END AS last_year_crop_commodity_name,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN
--Only set variety if last years crop was forage
CASE WHEN c.insurance_plan_id = 5 THEN
isg.crop_variety_id
ELSE
NULL
END
ELSE p.underseeded_crop_variety_id
END AS last_year_crop_variety_id,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN
--Only set variety if last years crop was forage
CASE WHEN c.insurance_plan_id = 5 THEN
v.variety_name
ELSE
NULL
END
ELSE p.underseeded_crop_variety_name
END AS last_year_crop_variety_name,
CASE WHEN p.underseeded_crop_variety_id IS NULL THEN
--Only set variety if last years crop was forage
CASE WHEN c.insurance_plan_id = 5 THEN
vi.is_grain_unseeded_default_ind
ELSE
NULL
END
ELSE p.underseeded_is_grain_unseeded_default_ind
END AS is_grain_unseeded_default_ind,

CASE WHEN p.underseeded_crop_variety_id IS NULL THEN SUM(isg.seeded_acres) ELSE SUM(p.underseeded_acres) END AS acres_to_be_seeded,
-- Rollover for is_hidden_on_printout_ind is based on ALL plantings,
-- not just those with seeded acres > 0. Set to Y if all plantings are Y,
-- otherwise set to N.
(SELECT MIN(all_p.is_hidden_on_printout_ind) FROM plantings all_p) AS is_hidden_on_printout_ind
FROM plantings p
LEFT JOIN (SELECT isg2.inventory_seeded_grain_guid, isg2.inventory_field_guid, isg2.seeded_acres, isg2.crop_commodity_id, isg2.crop_variety_id
FROM inventory_seeded_grain isg2
WHERE isg2.seeded_acres > 0) as isg
ON isg.inventory_field_guid = p.inventory_field_guid
LEFT JOIN crop_commodity c ON c.crop_commodity_id = isg.crop_commodity_id
LEFT JOIN crop_variety v ON v.crop_variety_id = isg.crop_variety_id
LEFT JOIN crop_variety_insurability vi ON vi.crop_variety_id = v.crop_variety_id
WHERE ((isg.seeded_acres > 0 and p.underseeded_crop_variety_id is null and isg.inventory_seeded_grain_guid is not null) or p.underseeded_crop_variety_id is not null)
GROUP BY p.field_id, isg.crop_commodity_id, c.commodity_name
, isg.crop_variety_id, v.variety_name, c.insurance_plan_id, vi.is_grain_unseeded_default_ind
, p.underseeded_crop_variety_id, p.underseeded_crop_variety_name
, p.underseeded_crop_commodity_id, p.underseeded_crop_commodity_name, p.underseeded_is_grain_unseeded_default_ind
UNION ALL
-- Only returns if no seeded acres > 0 exists for this field.
SELECT p.field_id,
null AS last_year_crop_commodity_id,
null AS last_year_crop_commodity_name,
null AS acres_to_be_seeded,
null AS last_year_crop_variety_id,
null AS last_year_crop_variety_name,
null AS is_grain_unseeded_default_ind,
-- Set to Y if all plantings are Y, otherwise set to N.
MIN(p.is_hidden_on_printout_ind) AS is_hidden_on_printout_ind
FROM plantings p
WHERE NOT EXISTS (SELECT 1
FROM plantings p2
JOIN inventory_seeded_grain isg2 ON isg2.inventory_field_guid = p2.inventory_field_guid
WHERE (isg2.seeded_acres > 0 and p.underseeded_crop_variety_id is null) or p.underseeded_crop_variety_id is not null)
GROUP BY p.field_id
) as rollover_plantings
GROUP BY field_id,
last_year_crop_commodity_id,
last_year_crop_commodity_name,
last_year_crop_variety_id,
last_year_crop_variety_name,
is_grain_unseeded_default_ind,
is_hidden_on_printout_ind
ORDER BY planting_number
</sql>

Expand Down
Loading

0 comments on commit 9375c30

Please sign in to comment.