# MYSOFT Integration - Quick Reference

## Setup (One-Time)

```bash
# 1. Create migration
php artisan make:migration add_mysoft_fields_to_integrations_table --table=integrations

# 2. Edit and run migration
# (Edit the migration file to add access_token, token_expires_at, refresh_token columns)
php artisan migrate

# 3. Add to .env
MYSOFT_BASE_URL=https://edocumentapi.mytest.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mytest.tr/oauth/token

# 4. Create integration record (via tinker)
php artisan tinker
>>> \App\Models\Integration::create([
    'type' => 'mysoft',
    'tool' => 'mysoft',
    'title' => 'MYSOFT E-Invoice',
    'url' => 'https://edocumentapi.mytest.tr/api',
    'username' => 'your-portal-username',
    'password' => 'your-portal-password',
    'api_key' => 'your-connector-guid',
    'is_active' => true,
    'current_team_id' => 1,
    'user_id' => 1,
]);

# 5. Test
php artisan mysoft:test
```

## API Usage

### Create Single Invoice

```javascript
POST /api/mysoft/create-invoice
{
    "sale_id": 123
}
```

### Batch Create

```javascript
POST /api/mysoft/batch-create
{
    "sales": [
        {"sale_id": 123},
        {"sale_id": 124}
    ]
}
```

### Test Connection

```javascript
POST /api/mysoft/test-connection
{
    "integration_id": 1
}
```

### Refresh Token

```javascript
POST /api/mysoft/refresh-token
{
    "integration_id": 1
}
```

## Routes to Add

Add to `routes/api.php`:

```php
use App\Http\Controllers\MysoftEInvoiceController;

Route::prefix('mysoft')->group(function () {
    Route::get('/', [MysoftEInvoiceController::class, 'index']);
    Route::post('/test-connection', [MysoftEInvoiceController::class, 'testConnection']);
    Route::post('/create-invoice', [MysoftEInvoiceController::class, 'createInvoice']);
    Route::post('/batch-create', [MysoftEInvoiceController::class, 'batchCreateInvoices']);
    Route::post('/refresh-token', [MysoftEInvoiceController::class, 'refreshToken']);
});
```

## Testing Commands

```bash
# Test with active integration
php artisan mysoft:test

# Test specific integration
php artisan mysoft:test --integration=2

# View logs
tail -f storage/logs/laravel.log | grep MYSOFT
```

## Troubleshooting

### No Active Integration Found

```sql
SELECT id, title, type, tool, is_active, url
FROM integrations
WHERE type = 'mysoft' OR tool = 'mysoft';
```

### Clear Token Cache

```bash
php artisan cache:clear
# Or via API
POST /api/mysoft/refresh-token
```

### Check Logs

```bash
tail -f storage/logs/laravel.log
```

### Verify Token in Database

```sql
SELECT id, title, access_token, token_expires_at
FROM integrations
WHERE id = 1;
```

## Data Customization

### Update Supplier Info

Edit `app/Services/MysoftEInvoiceService.php`:

```php
protected function buildSupplierData(): array
{
    return [
        'agentAccountName' => 'Your Company Name',
        'agentNumber' => 'Tax ID or Mersis',
        'city' => ['name' => 'ISTANBUL'],
        'country' => ['code' => 'TR', 'name' => 'TÜRKİYE'],
        // ... update all fields
    ];
}
```

## Production Deployment

1. Update `.env`:

```env
MYSOFT_BASE_URL=https://edocumentapi.mysoft.com.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mysoft.com.tr/oauth/token
```

2. Update integration record URL:

```sql
UPDATE integrations
SET url = 'https://edocumentapi.mysoft.com.tr/api'
WHERE type = 'mysoft';
```

3. Test connection:

```bash
php artisan mysoft:test
```

## Monitoring

### Recent Invoices

```sql
SELECT document_no, ettn, status, sent_at, created_at
FROM eadisyon
ORDER BY created_at DESC
LIMIT 10;
```

### Integration Status

```sql
SELECT id, title, is_active, access_token, token_expires_at
FROM integrations
WHERE type = 'mysoft';
```

### Failed Attempts

```bash
grep "MYSOFT.*error" storage/logs/laravel.log | tail -20
```

## File Locations

-   Service: `app/Services/MysoftEInvoiceService.php`
-   Controller: `app/Http/Controllers/MysoftEInvoiceController.php`
-   Command: `app/Console/Commands/TestMysoftIntegration.php`
-   Config: `config/mysoft.php`
-   Docs: `MYSOFT_INTEGRATION_SETUP.md`
-   Summary: `MYSOFT_IMPLEMENTATION_SUMMARY.md`
