This is a PHP application that fetches Alko's daily price list, converts prices to GBP using the CurrencyLayer API, and stores the data in a MySQL database. It also provides a front-end to list the products and modify the order amounts using AJAX.
- Features
- Requirements
- Installation
- Configuration
- Usage
- Running Tests
- Directory Structure
- Scheduling the PHP Script with Cron
- Instructions for Docker Usage
- License
- Fetches and parses Alko's daily price list (from an Excel file).
- Fetches the EUR to GBP conversion rate from the CurrencyLayer API.
- Stores product data in a MySQL database, with prices converted to GBP.
- Provides a simple front-end with AJAX functionality to list and modify product data.
- PHP 8.0 or higher
- MySQL 5.7+ or MariaDB
- Composer
- PHP extensions:
- PDO
- JSON
- cURL
- CurrencyLayer API key (free tier is sufficient)
git clone https://github.com/esa-kian/alkolist.git
cd alkolistMake sure Composer is installed on your system. Then, run the following command to install the project dependencies:
composer install-
Create a MySQL database:
CREATE DATABASE alko_prices;
-
You can find the SQL structure in the database.sql file located in the root of the project. Import the SQL file into your MySQL database:
mysql -u your_user -p alko_prices < database.sql
You can configure the app using either .env
Create a .env file from the provided .env.example file:
cp .env.example .envEdit the .env file with your database credentials, CurrencyLayer API key, and the Alko price list URL:
DB_DSN="mysql:host=localhost;dbname=alko_prices;charset=utf8mb4"
DB_USERNAME="your_database_user"
DB_PASSWORD="your_database_password"
CURRENCY_API_KEY="your_currencylayer_api_key"
ALKO_EXCEL_URL="https://www.alko.fi/valikoimat-ja-hinnasto/hinnasto"
After configuring the database and API keys, run the script that fetches the Alko price list, retrieves the latest exchange rates, and stores the data in the database:
php public/retrieve.phpThis script will:
- Fetch the Alko price list (Excel file).
- Parse the Excel file to extract the relevant data.
- Fetch the EUR to GBP exchange rate using the CurrencyLayer API.
- Update or insert the data into the MySQL database.
The application provides a simple front-end where you can view and interact with the product data stored in the database.
- Start a local PHP server (or use Apache/Nginx):
php -S localhost:8000- Navigate to
http://localhost:8000/public/index.phpin your browser.
This page includes two buttons:
- List: Loads the product data from the database and displays it in a table.
- Empty: Clears the displayed table.
- For each product listed, you can:
- Add: Increments the
orderamountby 1. - Clear: Sets the
orderamountto 0.
Both actions update the database without reloading the page using AJAX.
To run unit tests for the application, ensure PHPUnit is installed via Composer. Then run:
composer testThis command will execute all tests located in the tests/ directory.
The project's directory structure is organized as follows:
├── src/ # Application source code
│ ├── Controllers/ # Controllers for business logic
│ ├── Services/ # Services for handling API calls and business operations
│ ├── Repositories/ # Repository classes for database operations
│ ├── DTOs/ # Data Transfer Objects
│ └── Models/ # Data models (Entities)
├── public/ # Frontend PHP files for AJAX interface
├── tests/ # Unit tests
├── database.sql # SQL file to set up the database schema
├── config.php # Configuration file (alternative to .env)
├── .env.example # Example environment variables file
├── composer.json # Composer configuration for dependencies
├── README.md # Project documentation
└── .env # Environment configuration (optional)To automate the execution of the retrieve.php file every day at 8:00 AM, you can use cron, a time-based job scheduler in Unix-like operating systems. Follow these steps:
-
Open the Crontab Configuration: Run the following command in your terminal to edit the crontab configuration for the current user:
crontab -e
-
Add a New Cron Job: Add the following line to the crontab file:
0 8 * * * /usr/bin/php /path/to/project/public/retrieve.php >> /path/to/project/logs/output.log 2>&1
0 8 * * *specifies that the command should run at 8:00 AM every day./usr/bin/phpshould be replaced with the path to your PHP executable, which you can find by runningwhich phpin the terminal./path/to/project/public/retrieve.phpshould be replaced with the actual path to yourretrieve.phpfile.>> /path/to/project/logs/output.log 2>&1redirects standard output and errors to a log file, allowing you to review the output of the script execution.
- Save and Exit: After adding the line, save the changes and exit the editor. The cron job will now be scheduled to run the specified PHP script at the designated time.
- Build the Docker Image:
- Make sure to update the
DB_DSNvariable in your.envfile with the correct database connection details to ensure proper connectivity.
docker build -t php-alko-app .
- Run the Container:
docker run -p -d 8000:80 php-alko-app
- Stop the Container:
docker stop php-alko-app
This project is open-source and available under the MIT License.