migrate/app/Http/Controllers/ProductsController.php

266 lines
8.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\MagentoProductMigrationService;
use App\Services\MagentoCategoryMigrationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
class ProductsController extends Controller
{
protected $productService;
protected $categoryService;
public function __construct(MagentoProductMigrationService $productService, MagentoCategoryMigrationService $categoryService)
{
$this->productService = $productService;
$this->categoryService = $categoryService;
// Inject category service into product service
$this->productService->setCategoryService($categoryService);
}
/**
* Show the products page
*/
public function index()
{
$m1Products = $this->productService->getMagento1Products();
$m2Products = $this->productService->getMagento2Products();
$m1ProductsNotInM2 = $this->productService->getM1ProductsNotInM2();
$m2ProductsNotInM1 = $this->productService->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);
// Get progress key from request or generate one (only for non-dry-run)
$progressKey = $request->input('progress_key');
if (!$dryRun && !$progressKey) {
$progressKey = 'product_migration_progress_' . uniqid();
}
$result = $this->productService->migrateProducts($dryRun, $progressKey);
// Add progress key to result
if ($progressKey) {
$result['progress_key'] = $progressKey;
}
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->productService->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->productService->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->productService->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->productService->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->productService->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);
}
}
/**
* Compare M1 and M2 product values by SKU
*/
public function compareProductBySku(Request $request)
{
try {
$sku = $request->input('sku');
if (empty($sku)) {
return response()->json([
'success' => false,
'message' => 'SKU is required',
'changes' => []
], 400);
}
$result = $this->productService->compareProductBySku($sku);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Product comparison error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Comparison failed: ' . $e->getMessage(),
'changes' => []
], 500);
}
}
/**
* Get 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 migration progress error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to get progress: ' . $e->getMessage()
], 500);
}
}
}