import { string } from 'alga-js' export const useOpenShipments = () => { const openShipments = ref([]) const isLoading = ref(false) const error = ref(null) const lastFetched = ref(null) // Fetch fulfillment customers const fetchFulfillmentCustomers = async (): Promise => { const filter = string.urlEncode("isFulfillmentCustomer eq true AND isActive eq true") const res: any = await $fetch( `/api/filters/c_bpartner/${filter}&$select=isFulfillmentCustomer,isActive,name`, { headers: useRequestHeaders(['cookie']) } ) return res?.records?.map((p: any) => p.id) || [] } // Fetch organizations related to fulfillment customers const fetchFulfillmentOrgs = async (partnerIds: number[]): Promise => { if (partnerIds.length === 0) return [] // Build OR filter for all partner IDs const orConditions = partnerIds.map(id => `C_BPartner_ID eq ${id}`).join(' OR ') const filter = string.urlEncode(orConditions) const res: any = await $fetch( `/api/filters/ad_org/${filter}&$select=ad_org_id`, { headers: useRequestHeaders(['cookie']) } ) return res?.records?.map((o: any) => o.id) || [] } // Fetch open (non-commissioned) shipments for given org IDs const fetchOpenShipmentsForOrgs = async (orgIds: number[]): Promise => { if (orgIds.length === 0) return [] // Build IN filter for org IDs const orgList = orgIds.join(',') const filterStr = `iscommissioned eq 'N' AND IsActive eq true AND MovementType eq 'C-' AND DocStatus eq 'CO' AND ad_org_id in (${orgList})` const filter = string.urlEncode(filterStr) const res: any = await $fetch( `/api/filters/m_inout/${filter}&$expand=m_inoutline($expand=m_product_id),AD_Org_ID,C_BPartner_ID,C_BPartner_Location_ID,M_Shipper_ID&$orderby=${string.urlEncode('MovementDate desc')}`, { headers: useRequestHeaders(['cookie']) } ) // Calculate total qty for each shipment const shipmentsWithQty = (res?.records || []).map((shipment: any) => { const lines = shipment.m_inoutline || [] const totalQty = lines.reduce((sum: number, line: any) => sum + Number(line.QtyEntered || 0), 0) return { ...shipment, totalQty } }) return shipmentsWithQty } // Main fetch function - chains all requests const fetchOpenShipments = async () => { isLoading.value = true error.value = null try { // Step 1: Get fulfillment customers const partnerIds = await fetchFulfillmentCustomers() // Step 2: Get related organizations const orgIds = await fetchFulfillmentOrgs(partnerIds) // Step 3: Get open shipments for those orgs const shipments = await fetchOpenShipmentsForOrgs(orgIds) openShipments.value = shipments lastFetched.value = new Date() } catch (err: any) { console.error('Error fetching open shipments:', err) error.value = err?.message || 'Failed to fetch open shipments' } finally { isLoading.value = false } } // Refresh data (for manual refresh or after commission) const refresh = () => { return fetchOpenShipments() } // Remove a shipment from the list (e.g., after it's been commissioned) const removeShipment = (shipmentId: number) => { openShipments.value = openShipments.value.filter(s => s.id !== shipmentId) } return { openShipments, isLoading, error, lastFetched, fetchOpenShipments, refresh, removeShipment } }