# SAP S/4HANA Inventory Sync HMIS can fetch goods-receipt records from SAP Materials Management (MM) and compare them against the HMIS pharmacy item master. This helps identify which SAP materials have matching items in HMIS and which do not. > **Important**: This is an **audit sync only**. It does not create pharmacy stock entries or GRN (Goods Received Note) records in HMIS. Actual stock intake is handled by the normal HMIS pharmacy GRN workflow. --- ## How It Works 1. HMIS calls the SAP material document API for a date range. 2. SAP returns all goods-receipt line items posted in that period. 3. For each SAP material number, HMIS looks for a matching pharmacy item by `code` (or `barcode`, if configured). 4. The sync returns a summary: how many items matched, how many did not, and the list of unmatched SAP material numbers. 5. After a normal forward sync, HMIS updates an internal watermark timestamp so the next sync automatically picks up from where the last one ended. --- ## Configuration | Key | Description | |---|---| | `SAP Integration - Enabled` | Must be `true` | | `SAP Integration - Base URL` | SAP tenant URL | | `SAP Integration - Token URL`, `Client ID`, `Client Secret` | OAuth credentials | | `SAP Integration - Material Code Field` | Which HMIS item field to match against the SAP material number: `code` (default) or `barcode` | | `SAP Integration - Inventory Sync From Days` | When there is no previous sync, how many days back to look. Default: `7` | | `SAP Integration - Inventory Last Sync` | Auto-updated timestamp after each successful forward sync. Do not edit manually. | --- ## Running a Sync ### Forward Sync (Recommended) A forward sync uses the last-sync watermark to automatically determine the start date. If no previous sync has been run, it falls back to the configured number of days ago. **Request** ``` GET /api/sap/inventory/sync Finance: ``` ### Sync for a Specific Date Range (Backfill) Supply `fromDate` and/or `toDate` to override the automatic date range. The watermark is **not** updated after a backfill. **Request** ``` GET /api/sap/inventory/sync?fromDate=2024-01-01&toDate=2024-01-31 Finance: ``` --- ## Response ```json { "status": "success", "code": 200, "data": { "totalDocumentItems": 47, "matchedItems": 43, "unmatchedItems": 4, "syncFromDate": "2024-03-08", "syncToDate": "2024-03-15", "lastSyncTimestamp": "2024-03-15T23:59:59", "unmatchedMaterials": [ "MAT-9001", "MAT-9002", "MAT-9003", "MAT-9004" ], "warnings": [] } } ``` | Field | Description | |---|---| | `totalDocumentItems` | Total SAP goods-receipt line items fetched | | `matchedItems` | Items that have a matching HMIS pharmacy item | | `unmatchedItems` | Items with no match in HMIS | | `syncFromDate` | Start date used for the SAP query | | `syncToDate` | End date used for the SAP query | | `lastSyncTimestamp` | Updated watermark (null for backfill syncs) | | `unmatchedMaterials` | List of SAP material numbers with no HMIS match | | `warnings` | Any non-fatal issues encountered during the sync | --- ## Understanding the Watermark The watermark (`SAP Integration - Inventory Last Sync`) ensures that consecutive forward syncs do not re-fetch the same data or leave gaps. - After a successful forward sync, the watermark is set to the end date of the sync at `23:59:59` (the effective SAP upper bound). - The next forward sync starts from the date part of that watermark. - A backfill (explicit `fromDate`) does **not** update the watermark, so the normal forward sync schedule is not affected. --- ## Handling Unmatched Materials If SAP material numbers appear in `unmatchedMaterials`, it means those materials are not yet recorded in the HMIS item master. To resolve this: 1. Identify the unmatched material numbers from the sync result. 2. Find the corresponding pharmaceutical items in HMIS. 3. Set the `code` field (or `barcode`, if that is your configured match field) of each item to match the SAP material number. 4. Re-run the sync — the items should now match. If most items are unmatched and you are starting fresh, ensure `SAP Integration - Material Code Field` is set correctly. Contact your system administrator if you are unsure which field to use. --- ## Troubleshooting | Problem | What to check | |---|---| | `SAP integration is disabled` | Set `SAP Integration - Enabled = true` | | All items unmatched | Verify `SAP Integration - Material Code Field` is `code` or `barcode`. Confirm HMIS items have the SAP material number in the correct field. | | Warning: `Unsupported SAP Integration - Material Code Field` | The field name is not `code` or `barcode`. Correct it in Application Configuration. | | HTTP 502 error | SAP API returned an error — check Payara server log for details | | `totalDocumentItems` is 0 | No goods-receipt documents in SAP for the date range. Try widening the range with explicit `fromDate`/`toDate`. | --- ## Related Pages - [SAP S/4HANA Cloud Integration — Overview](SAP-S4HANA-Cloud-Integration-Overview) - [SAP S/4HANA Billing Outbound](SAP-S4HANA-Billing-Outbound) - [SAP S/4HANA Billing Inbound](SAP-S4HANA-Billing-Inbound)