# GoG Dropshipping - Magento 2 Installation Guide

Detaillierte Schritt-für-Schritt Anleitung zur Installation und Konfiguration.

## Voraussetzungen

- Magento 2.4.x Installation
- SSH-Zugang zum Server
- Datenbank-Zugang
- GoG API Credentials (api_token, orders_api_token)
- Admin-Zugang zum Magento Backend

## Installation

### Schritt 1: Extension hochladen

**Via Composer (empfohlen):**
```bash
cd /pfad/zu/magento
composer require gog/magento2-dropshipping
```

**Manuell:**
```bash
cd /pfad/zu/magento
mkdir -p app/code/GoG/Dropshipping
# Kopiere alle Dateien in app/code/GoG/Dropshipping/
```

### Schritt 2: Magento Setup

```bash
# Module aktivieren
php bin/magento module:enable GoG_Dropshipping

# Setup ausführen
php bin/magento setup:upgrade

# Dependency Injection kompilieren (Production Mode)
php bin/magento setup:di:compile

# Static Content deployen (Production Mode)
php bin/magento setup:static-content:deploy de_DE en_US

# Cache leeren
php bin/magento cache:clean
php bin/magento cache:flush
```

### Schritt 3: Datenbank erweitern

```bash
# Sources-Tabelle erweitern
mysql -u DB_USER -p DB_NAME < app/code/GoG/Dropshipping/setup/sources_magento_extension.sql
```

Oder manuell in phpMyAdmin/Adminer:

```sql
ALTER TABLE sources 
ADD COLUMN magento_store_code VARCHAR(50) NULL,
ADD COLUMN magento_base_url VARCHAR(255) NULL,
ADD COLUMN magento_api_key VARCHAR(255) NULL,
ADD COLUMN magento_api_secret VARCHAR(255) NULL;

CREATE INDEX idx_magento_store_code ON sources(magento_store_code);
```

### Schritt 4: Source-Eintrag erstellen

Verbinde dich mit der Datenbank und führe aus:

```sql
INSERT INTO sources (
    source_name,
    shop_system,
    shop_url,
    api_token,
    orders_api_token,
    magento_store_code,
    magento_base_url,
    is_active,
    created_at,
    updated_at
) VALUES (
    'magento-SHOPNAME',                    -- Eindeutiger Name
    'magento',
    'https://DEINE-SHOP-URL.com',
    'GENERIERTER_API_TOKEN',               -- Token für Produktsync
    'GENERIERTER_ORDERS_TOKEN',            -- Token für Order Bridge
    'default',                              -- Magento Store Code
    'https://DEINE-SHOP-URL.com',
    1,
    NOW(),
    NOW()
);
```

**Token generieren:**
```bash
# API Token
openssl rand -hex 32

# Orders API Token
openssl rand -hex 32
```

### Schritt 5: Produkt-Attribute erstellen

**Option A: Via Admin Panel**

1. Gehe zu: **Stores → Attributes → Product**
2. Klicke "Add New Attribute"
3. Erstelle folgende Attribute:

**Attribut 1: gog_stecklingsverkauf**
- Attribute Code: `gog_stecklingsverkauf`
- Catalog Input Type: Yes/No
- Default Value: No
- Scope: Global
- Use in Product Listing: Yes
- Used in Grid: Yes
- Visible in Grid: Yes
- Filterable in Grid: Yes

**Attribut 2: gog_product_id**
- Attribute Code: `gog_product_id`
- Catalog Input Type: Text Field
- Scope: Global
- Unique Value: Yes
- Use in Product Listing: Yes

**Attribut 3-7: gog_breeder, gog_category, gog_thc, gog_cbd, gog_genetics**
- Catalog Input Type: Text Field
- Scope: Global
- Use in Product Listing: Yes

4. Füge alle Attribute zum Default Attribute Set hinzu

**Option B: Via Setup Script** (Fortgeschritten)

Erstelle `Setup/InstallData.php` - siehe Magento 2 Dokumentation.

### Schritt 6: Import-Kategorie erstellen

1. **Catalog → Categories**
2. Erstelle neue Kategorie: "GoG Produkte"
3. Notiere die Category ID (z.B. 42)

### Schritt 7: Extension konfigurieren

Gehe zu: **Stores → Configuration → GoG → Dropshipping**

**General Settings:**
- Enable GoG Dropshipping: **Yes**
- Debug Mode: **Yes** (für Testing)

**API Configuration:**
- API Endpoint URL: `https://stecklingsverkauf.de/api_order_bridge.php`
- API Token: `[Dein API Token aus sources]`
- Orders API Token: `[Dein Orders Token aus sources]`
- Source Name: `magento-SHOPNAME` (exakt wie in sources-Tabelle)
- Magento Store Code: `default`

**Product Import Settings:**
- Product API URL: `https://strainworld.de/api/products.php`
- Product API Token: `[Dein API Token]`
- Enable Automatic Import: **Yes**
- Import Category ID: `42` (Deine Category ID)
- Attribute Set ID: `4` (Default Attribute Set)

**Notification Settings:**
- Admin Notification Email: `deine-email@example.com`

Klicke **Save Config**

### Schritt 8: Permissions setzen

```bash
cd /pfad/zu/magento

# Permissions für Magento
find var generated vendor pub/static pub/media app/etc -type f -exec chmod 664 {} \;
find var generated vendor pub/static pub/media app/etc -type d -exec chmod 775 {} \;
chmod u+x bin/magento

# Owner setzen (anpassen an deinen Webserver)
chown -R www-data:www-data .
```

### Schritt 9: Cron einrichten

Füge zu crontab hinzu:

```bash
crontab -e -u www-data
```

Füge hinzu:
```
* * * * * php /pfad/zu/magento/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /pfad/zu/magento/var/log/magento.cron.log
```

### Schritt 10: Ersten Produktimport testen

**Via Admin Panel:**
1. Gehe zu **GoG Dropshipping → Product Sync**
2. Klicke "Start Sync"
3. Prüfe Logs: `var/log/system.log`

**Via CLI:**
```bash
php bin/magento cron:run --group default
```

**Logs prüfen:**
```bash
tail -f var/log/system.log | grep "GoG"
```

## Verifizierung

### 1. Produkte prüfen

```bash
# Anzahl importierter Produkte
mysql -u DB_USER -p -e "SELECT COUNT(*) FROM catalog_product_entity_int WHERE attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'gog_stecklingsverkauf' AND entity_type_id = 4) AND value = 1;" DB_NAME
```

### 2. Test-Bestellung

1. Erstelle Test-Produkt mit `gog_stecklingsverkauf = Yes`
2. Lege Bestellung an
3. Setze Status auf "Processing"
4. Prüfe Logs:

```bash
tail -f var/log/system.log | grep "GoG Dropshipping"
```

Erwartete Log-Einträge:
```
[GoG Dropshipping] Processing new order #000000123
[GoG Dropshipping] Order #000000123 successfully sent to Order Bridge
```

### 3. Order Bridge prüfen

Prüfe in der Datenbank:
```sql
SELECT * FROM orders WHERE source = 'magento-SHOPNAME' ORDER BY created_at DESC LIMIT 5;
```

## Troubleshooting

### Problem: Module wird nicht erkannt

```bash
php bin/magento module:status
# Sollte GoG_Dropshipping als enabled zeigen

# Falls nicht:
php bin/magento module:enable GoG_Dropshipping
php bin/magento setup:upgrade
```

### Problem: Produkte werden nicht importiert

1. **API-Verbindung testen:**
```bash
curl "https://strainworld.de/api/products.php?token=DEIN_TOKEN&url=https://deine-shop-url.com"
```

2. **Logs prüfen:**
```bash
tail -100 var/log/system.log | grep "GoG Product Sync"
```

3. **Cron Status:**
```bash
php bin/magento cron:run --group default
mysql -u DB_USER -p -e "SELECT * FROM cron_schedule WHERE job_code LIKE '%gog%' ORDER BY scheduled_at DESC LIMIT 10;" DB_NAME
```

### Problem: Bestellungen werden nicht gesendet

1. **Event Observer prüfen:**
```bash
php bin/magento events:list | grep gog
```

2. **Test-Bestellung mit Debug:**
```bash
# In OrderPlaceAfter.php temporär hinzufügen:
var_dump($order->getData());
die();
```

3. **Order Bridge Verbindung testen:**
```bash
curl -X POST https://stecklingsverkauf.de/api_order_bridge.php \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer DEIN_ORDERS_TOKEN" \
  -d '{"source":"magento-test","shop_system":"magento","order":{"woocommerce_order_id":"TEST"}}'
```

### Problem: Attribute fehlen

```bash
# Attribute auflisten
php bin/magento catalog:product:attributes:list | grep gog

# Attribute neu indexieren
php bin/magento indexer:reindex
```

## Performance-Optimierung

### Production Mode

```bash
php bin/magento deploy:mode:set production
php bin/magento cache:enable
```

### Indexer

```bash
# Indexer auf "Update on Schedule" setzen
php bin/magento indexer:set-mode schedule

# Indexer Status
php bin/magento indexer:status
```

### Cache

```bash
# Varnish/Redis konfigurieren (optional)
# Siehe Magento 2 Performance Best Practices
```

## Sicherheit

### 1. Produktiv-Modus

```bash
# Debug Mode ausschalten
Stores → Configuration → GoG → Dropshipping → Debug Mode: No
```

### 2. API Tokens schützen

- Tokens niemals in Git committen
- Regelmäßig rotieren
- Nur HTTPS verwenden

### 3. Permissions

```bash
# Restriktive Permissions
find app/code/GoG -type f -exec chmod 644 {} \;
find app/code/GoG -type d -exec chmod 755 {} \;
```

## Wartung

### Logs rotieren

```bash
# Logrotate konfigurieren
sudo nano /etc/logrotate.d/magento

# Inhalt:
/pfad/zu/magento/var/log/*.log {
    daily
    rotate 7
    compress
    delaycompress
    notifempty
    create 0644 www-data www-data
}
```

### Updates

```bash
# Extension aktualisieren
composer update gog/magento2-dropshipping
php bin/magento setup:upgrade
php bin/magento cache:clean
```

## Support

Bei Problemen:
1. Logs prüfen: `var/log/system.log`, `var/log/exception.log`
2. Debug Mode aktivieren
3. Kontakt: martin@eatpixel.com

## Checkliste

- [ ] Extension installiert
- [ ] Datenbank erweitert
- [ ] Source-Eintrag erstellt
- [ ] Produkt-Attribute erstellt
- [ ] Import-Kategorie erstellt
- [ ] Extension konfiguriert
- [ ] Cron eingerichtet
- [ ] Erster Produktimport erfolgreich
- [ ] Test-Bestellung erfolgreich
- [ ] Logs geprüft
- [ ] Production Mode aktiviert
- [ ] Debug Mode deaktiviert
