migrate/app/Http/Controllers/OrdersController.php

170 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\MagentoCategoryMigrationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
class OrdersController extends Controller
{
protected $migrationService;
public function __construct(MagentoCategoryMigrationService $migrationService)
{
$this->migrationService = $migrationService;
}
/**
* Show the orders page
*/
public function index()
{
$m1Orders = $this->migrationService->getMagento1Orders();
$m2Orders = $this->migrationService->getMagento2Orders();
$m1OrdersNotInM2 = $this->migrationService->getM1OrdersNotInM2();
$m2OrdersNotInM1 = $this->migrationService->getM2OrdersNotInM1();
return view('orders.index', [
'm1Orders' => $m1Orders,
'm2Orders' => $m2Orders,
'm1OrdersNotInM2' => $m1OrdersNotInM2,
'm2OrdersNotInM1' => $m2OrdersNotInM1,
]);
}
/**
* Migrate all orders from Magento 1 to Magento 2
*/
public function migrateOrders(Request $request)
{
try {
// Handle both JSON and form data
$dryRun = false;
$progressKey = null;
if ($request->isJson()) {
$dryRun = $request->json()->get('dry_run', false);
$progressKey = $request->json()->get('progress_key', null);
} else {
$dryRun = $request->input('dry_run', false);
$progressKey = $request->input('progress_key', null);
}
// Convert string "true"/"false" to boolean if needed
if (is_string($dryRun)) {
$dryRun = filter_var($dryRun, FILTER_VALIDATE_BOOLEAN);
}
$result = $this->migrationService->migrateOrders($dryRun, $progressKey);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Order migration error: ' . $e->getMessage());
Log::error('Stack trace: ' . $e->getTraceAsString());
return response()->json([
'success' => false,
'message' => 'Migration failed: ' . $e->getMessage(),
'added' => 0,
'updated' => 0,
'errors' => 0,
'log' => []
], 500);
}
}
/**
* Get order migration progress
*/
public function getMigrationProgress(Request $request)
{
try {
$progressKey = $request->input('progress_key');
if (empty($progressKey)) {
return response()->json([
'success' => false,
'message' => 'Progress key is required'
], 400);
}
$progress = Cache::get($progressKey);
if (!$progress) {
return response()->json([
'success' => false,
'message' => 'Progress not found',
'progress' => null
], 404);
}
return response()->json([
'success' => true,
'progress' => $progress
]);
} catch (\Exception $e) {
Log::error('Get order migration progress error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to get progress: ' . $e->getMessage()
], 500);
}
}
/**
* Get order statistics
*/
public function getStatistics()
{
try {
$m1Orders = $this->migrationService->getMagento1Orders();
$m2Orders = $this->migrationService->getMagento2Orders();
$m1OrdersNotInM2 = $this->migrationService->getM1OrdersNotInM2();
$m2OrdersNotInM1 = $this->migrationService->getM2OrdersNotInM1();
return response()->json([
'success' => true,
'statistics' => [
'm1Orders' => $m1Orders->count(),
'm2Orders' => $m2Orders->count(),
'm1OrdersNotInM2' => $m1OrdersNotInM2->count(),
'm2OrdersNotInM1' => $m2OrdersNotInM1->count(),
]
]);
} catch (\Exception $e) {
Log::error('Get order statistics error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to get statistics: ' . $e->getMessage()
], 500);
}
}
/**
* Delete a single order from Magento 2
*/
public function deleteM2Order(Request $request, $orderId)
{
try {
$result = $this->migrationService->deleteM2Order($orderId);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Delete M2 order error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Deletion failed: ' . $e->getMessage()
], 500);
}
}
}