# Changelog

All notable changes to the Caterpoint Integration Module are documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v1.4.7] - 2026-05-18
### Added
- Caterpoint account and product metadata helper models for storing Caterpoint-specific codes in NGauge dynamic data segments.

### Changed
- Order import now maps normalized Caterpoint payloads through `portalpeople/ngauge-cart` for basket creation instead of maintaining a separate local cart import path.
- Caterpoint account resolution now reads the delivery account from `Accounts.extended.caterpoint_code`, with product lookup using `Products.data.caterpoint.stock_code`.

### Fixed
- Declared the `portalpeople/ngauge-cart` runtime dependency required by the import service.

## [v1.4.5] - 2026-03-10
### Fixed
- Bound the bulk import submit button to the explicit form id so submit works even when browser DOM normalization places the button outside the form element
- Switched from button click handling to form submit handling for the `Processing...` state and validation

## [v1.4.4] - 2026-03-10
### Fixed
- Removed Select2 `dropdownParent` modal selector from bulk mapping fields to prevent empty parent resolution
- Resolved `select2.full.js` `isConnected` runtime error when opening product mapping dropdowns

## [v1.4.3] - 2026-03-10
### Fixed
- Replaced manual Select2 CDN integration in bulk import mapping with `kartik-v/yii2-widget-select2`
- Removed external Select2 bootstrap theme URL dependency that caused runtime 404 errors
- Configured each mapping field to use modal-aware Select2 dropdown parents via widget plugin options

## [v1.4.2] - 2026-03-10
### Fixed
- Select2 `isConnected` error by switching from `4.1.0-rc.0` to stable `4.0.13`
- Replaced `select2-bootstrap-5-theme` with `select2-bootstrap4-theme` to match the Bootstrap 4 layout
- Removed incorrect blanket Select2 init that passed `document` as `dropdownParent`; dropdowns now initialise only on `shown.bs.modal` with the correct modal element as parent

## [v1.4.1] - 2026-03-10
### Changed
- Product dropdown fallback now lists all products (up to 2000) with Select2 typeahead search when no SKU/name candidates are found
- Product option labels now use `stock_code - product_name` format (hyphenated)

## [v1.4.0] - 2026-03-10
### Added
- Product lookup helper for Caterpoint bulk imports that checks `Productsbe` by SKU first and then exact product name
- Item-level mapping dropdowns in the bulk import modal showing `stock_code product_name` options for manual override

### Changed
- Bulk import now passes selected product mappings into the import routine before falling back to automatic SKU/name matching
- Cached order payloads are enriched with product match status so unmatched lines are visible before import

## [v1.3.2] - 2026-03-10
### Fixed
- Centered bulk-list selection checkboxes inside the first table column
- Removed inherited negative checkbox offset that caused checkboxes to clip into the table border

## [v1.3.1] - 2026-03-10
### Fixed
- Improved padding and spacing in bulk-list view for better UX
- Normalized table cell padding to 0.75rem for consistency
- Added proper left/right padding (15px) to wrapper
- Improved card body spacing (1.5rem) and alert separation

## [v1.3.0] - 2026-03-10
### Added
- Bulk order browser at `/NGauge/caterpoint/order/bulk-list` for reviewing incoming Caterpoint orders before import
- Selective bulk import flow with checkbox selection, cached normalized payloads, and per-order success logging
- Order line item preview modals and cache refresh controls in the bulk import interface

### Changed
- Renamed the Magento integration guide to `CATERPOINT_INTEGRATION.md`
- Added dedicated HTML controller actions for cached bulk order listing and manual batch imports

## [v1.2.1] - 2026-03-10
### Added
- OrderImporter component for independent backend order creation
- `OrderImporter::newCaterPointOrder()` static method to create backend orders from normalized data
- Full Yii logging integration for order import operations (channel: 'caterpoint-import')

### Changed
- Removed dependency on ahs-magento module
- OrderImporter handles all payment methods directly (PayPal, Stripe, Caterpoint)
- Improved error handling with try-catch and graceful failure modes

### Fixed
- Payment handling now integrated directly instead of relying on external module

## [v1.2.0] - 2026-03-10
### Added
- DataNormalizer component to transform Caterpoint orders into Magento-compatible format
- New `normalize` parameter in fetch-raw endpoint for Magento integration (e.g., `?normalize=1`)
- Example normalized response in `examples/fetch-raw-normalized.json`
- Support for seamless integration with `ahs-magento\models\Carts::NewMagentoOrder()`
- Orders converted with buyer name, delivery date, line items (SKU, qty), and shipping information

## [v1.1.8] - 2026-03-10
### Fixed
- Format detection now analyzes actual content before trusting Content-Type header
- Caterpoint API now works correctly even when returning XML with text/html Content-Type
- Added logging for Content-Type/content mismatches for debugging

## [v1.1.7] - 2026-03-10
### Changed
- Updated branch-alias dev reference to v1.x-dev for better versioning

## [v1.1.6] - 2026-03-10
### Added
- Yii::warning() logging for HTML responses and API errors with full response details
- Logs include Content-Type headers, status codes, endpoints, and response previews

## [v1.1.5] - 2026-03-10
### Fixed
- Added response preview to HTML error message for debugging

## [v1.1.4] - 2026-03-10

### Fixed
- `fetch-raw` no longer silently redirects when upstream returns an error payload
- `/NGauge/caterpoint/order/fetch-raw?format=json|xml` now returns explicit structured error responses with HTTP 502 on failure
- Friendly HTML `fetch-raw` now renders an inline error panel instead of appearing to do nothing
- Corrected displayed API endpoint links in order dashboard/raw views to `/api/caterpoint/order/fetch-raw`

## [v1.1.3] - 2026-03-10

### Fixed
- Removed yii2-bootstrap5 dependency from views
- Views now use plain HTML and CSS classes for compatibility
- Fixed "Class yii\bootstrap5\Alert not found" error

## [v1.1.2] - 2026-03-10

### Added
- Dashboard view for order management (views/order/index.php)
- Raw data view for order inspection (views/order/fetch-raw.php)
- Form for manual import with limit and dry-run options
- Status display with current cursor tracking
- Quick links to API endpoints and alternative formats

## [v1.1.1] - 2026-03-10

### Removed
- Removed `portalpeople/statis-webhook` from production dependencies
- Webhook triggering is a development/deployment workflow tool, not a runtime dependency
- Use `php yii statis-webhook/webhook/trigger` in development environment instead

## [v1.1.0] - 2026-03-10

### Changed
- **BREAKING**: Separated routing for /NGauge and /api paths
- /NGauge routes now return friendly HTML responses (user interfaces)
- /api routes now return raw JSON/XML responses (REST API)
- OrderController split into two controllers:
  - `controllers/OrderController.php` - friendly HTML for /NGauge/caterpoint/*
  - `controllers/api/OrderController.php` - raw JSON for /api/caterpoint/*

### Added
- Support for format parameter in fetch-raw endpoint (?format=html|json|xml)
- Dashboard view for order management
- Friendly error messages with flash notifications

## [v1.0.9] - 2026-03-10

### Fixed
- OrderController now forces JSON response format via contentNegotiator behavior
- Ensures fetch-raw and other REST endpoints always return application/json

## [v1.0.8] - 2026-03-10

### Changed
- Testing statis-webhook module integration and automated package index updates

## [v1.0.7] - 2026-03-10

### Added
- Integrated statis-webhook module for automated package index updates
- Programmatic webhook notification support via portalpeople/statis-webhook dependency

### Deprecated
- trigger-webhook.sh bash script (replace with `php yii statis-webhook/webhook/trigger`)

## [v1.0.6] - 2026-03-10

### Fixed
- Added explicit upstream response format detection (XML, JSON, HTML, text) for Caterpoint API responses.
- Normalized XML responses to JSON-compatible arrays and exposed detected source format in raw fetch and setup connection test output.

## [v1.0.5] - 2026-03-10

### Fixed
- Caterpoint client now supports XML API responses with safe parsing and JSON fallback.
- Improved response handling to avoid frontend JSON parse errors when upstream returns HTML/XML error pages.

### Added
- Magento-style Caterpoint setup screen with API settings, import settings, connection test, and manual import actions.
- New web configuration controller and form model for persisting Caterpoint module settings to params-local.php.

### Changed
- Module bootstrap now maps Caterpoint setup routes similar to ahs-magento (`caterpoint/<action>` to config actions).
- Caterpoint client request handling now normalizes endpoint paths to avoid duplicated `/api` segments and improves HTTP error handling.
- Order import service can now optionally mark imported Caterpoint orders as exported.

## [v1.0.0] - 2026-03-06

### Added
- Initial release of Caterpoint integration module
- CaterpointClient component for API communication via GuzzleHTTP
- OrderImportService for order synchronization with cursor-based incremental imports
- CaterpointCursorStore service for persistent import state management
- OrderImportController console command for CLI-based imports with dry-run support
- OrderController REST API endpoints for web-based import triggers
- CaterpointOrder template model for database integration
- Support for order status mapping and customization
- Support for order confirmation and invoice submission
- Comprehensive error handling and logging
- Full documentation and configuration examples

### Features
- Incremental order imports using cursor tracking
- Dry-run mode for previewing changes without persistence
- Console command with verbose output and progress tracking
- REST API endpoints for programmatic access
- Transaction-based imports for data consistency
- Automatic status code translation
- Support for local order number generation
- Per-order error handling with detailed error reporting
