migrate/app/Http/Controllers/ProductsController.php

177 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\MagentoCategoryMigrationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class ProductsController extends Controller
{
protected $migrationService;
public function __construct(MagentoCategoryMigrationService $migrationService)
{
$this->migrationService = $migrationService;
}
/**
* Show the products page
*/
public function index()
{
$m1Products = $this->migrationService->getMagento1Products();
$m2Products = $this->migrationService->getMagento2Products();
$m1ProductsNotInM2 = $this->migrationService->getM1ProductsNotInM2();
$m2ProductsNotInM1 = $this->migrationService->getM2ProductsNotInM1();
return view('products.index', [
'm1Products' => $m1Products,
'm2Products' => $m2Products,
'm1ProductsNotInM2' => $m1ProductsNotInM2,
'm2ProductsNotInM1' => $m2ProductsNotInM1,
]);
}
/**
* Migrate all products from Magento 1 to Magento 2
*/
public function migrateProducts(Request $request)
{
try {
$dryRun = $request->input('dry_run', false);
$result = $this->migrationService->migrateProducts($dryRun);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Product migration error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Migration failed: ' . $e->getMessage(),
'added' => 0,
'updated' => 0,
'errors' => 0,
'log' => [],
'missing_attributes' => []
], 500);
}
}
/**
* Delete a single product from Magento 2
*/
public function deleteM2Product(Request $request, $productId)
{
try {
$result = $this->migrationService->deleteM2Product($productId);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Delete M2 product error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Deletion failed: ' . $e->getMessage()
], 500);
}
}
/**
* Sync product category assignments from M1 to M2
*/
public function syncProductCategories(Request $request)
{
try {
$result = $this->migrationService->syncProductCategories();
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Sync product categories error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Sync failed: ' . $e->getMessage(),
'updated' => 0,
'skipped' => 0,
'errors' => 0,
'log' => []
], 500);
}
}
/**
* Delete all products in Magento 2 that have entity_id greater than max M1 product ID
*/
public function deleteM2ProductsAboveM1Max(Request $request)
{
try {
$result = $this->migrationService->deleteM2ProductsAboveM1Max();
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Delete M2 products error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Deletion failed: ' . $e->getMessage(),
'deleted' => 0
], 500);
}
}
/**
* Fix category products - ensure products are added to categories if missing from catalog_category_product table
*/
public function fixCategoryProducts(Request $request)
{
try {
$result = $this->migrationService->fixCategoryProducts();
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Fix category products error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Fix failed: ' . $e->getMessage(),
'added' => 0,
'skipped' => 0,
'errors' => 0,
'log' => []
], 500);
}
}
/**
* Migrate product options from M1 mageworx_custom_option_ tables to M2 mageworx_optiontemplates_ tables
*/
public function migrateProductOptions(Request $request)
{
try {
$dryRun = $request->input('dry_run', false);
$result = $this->migrationService->migrateProductOptions($dryRun);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Product options migration error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Migration failed: ' . $e->getMessage(),
'migrated' => 0,
'skipped' => 0,
'errors' => 0,
'log' => []
], 500);
}
}
}