Wes Gamble
04/04/2022, 10:29 PMWes Gamble
04/05/2022, 6:01 PM# First query is items
# Second query is kits with items that have inventory
<<~STOP
SELECT Item.id,
Item.Itemid AS sku,
Item.displayname AS name,
InventoryItemLocations.quantityavailable AS available_inventory,
Item.#{ITEM_OLD_SKU_FIELD} AS old_sku,
Item.cost,
Item.#{ITEM_IMAGE_FIELD} AS photo_url
FROM Item AS Item
JOIN InventoryItemLocations
ON InventoryItemLocations.Item = Item.id
WHERE InventoryItemLocations.Location = '#{warehouse_id}'
AND Item.itemtype = 'InvtPart' AND InventoryItemLocations.quantityavailable > 0
AND Item.isinactive = 'F'
UNION
SELECT DISTINCT ParentItem.id,
ParentItem.Itemid AS sku,
ParentItem.displayname AS name,
NULL AS available_inventory,
NULL AS old_sku,
NULL AS cost,
NULL AS photo_url
FROM Item AS ParentItem
JOIN KitItemMember
ON KitItemMember.ParentItem = ParentItem.ID
JOIN Item AS MemberItem
ON MemberItem.ID = KitItemMember.Item
WHERE ParentItem.Itemid != 'AG Test Item Group'
AND EXISTS (SELECT *
FROM InventoryItemLocations
WHERE InventoryItemLocations.Item = MemberItem.id
AND InventoryItemLocations.Location = '#{warehouse_id}'
AND InventoryItemLocations.quantityavailable > 0)
ORDER BY name
STOP
I can basically pull all kits that have inventory and then for those kits that are members of kits, I can compose the subkit results to determine if the parent kits have inventory.
So not one query, but close.