шаг 4 завершен
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers\Admin;
|
||||
|
||||
use App\Repositories\CategoryRepository;
|
||||
use App\Repositories\ProductRepository;
|
||||
|
||||
final class ProductAdminController
|
||||
{
|
||||
public function index(): void
|
||||
{
|
||||
$categoryRepository = new CategoryRepository();
|
||||
$productRepository = new ProductRepository();
|
||||
$message = null;
|
||||
$error = null;
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$oldPackagePrice = $_POST['old_package_price'] !== '' ? (float) $_POST['old_package_price'] : null;
|
||||
$discountPercent = $_POST['discount_percent'] !== '' ? (float) $_POST['discount_percent'] : null;
|
||||
$packagePrice = (float) ($_POST['package_price'] ?? 0);
|
||||
$discountType = 'none';
|
||||
|
||||
if ($oldPackagePrice !== null && $discountPercent !== null && $discountPercent > 0) {
|
||||
$packagePrice = round($oldPackagePrice * (1 - ($discountPercent / 100)), 2);
|
||||
$discountType = 'percent';
|
||||
} elseif ($oldPackagePrice !== null) {
|
||||
$discountType = 'manual_old_price';
|
||||
}
|
||||
|
||||
$productRepository->createWithVariant([
|
||||
'category_id' => $_POST['category_id'] !== '' ? (int) $_POST['category_id'] : null,
|
||||
'name' => trim((string) $_POST['name']),
|
||||
'slug' => trim((string) $_POST['slug']),
|
||||
'short_description' => trim((string) ($_POST['short_description'] ?? '')),
|
||||
'seo_title' => trim((string) ($_POST['seo_title'] ?? '')),
|
||||
'seo_description' => trim((string) ($_POST['seo_description'] ?? '')),
|
||||
'seo_keywords' => trim((string) ($_POST['seo_keywords'] ?? '')),
|
||||
'base_unit' => (string) ($_POST['base_unit'] ?? 'kg'),
|
||||
'is_published' => isset($_POST['is_published']) ? 1 : 0,
|
||||
'variant_name' => trim((string) ($_POST['variant_name'] ?? '')),
|
||||
'package_quantity' => (float) ($_POST['package_quantity'] ?? 1),
|
||||
'step_quantity' => (float) ($_POST['step_quantity'] ?? 1),
|
||||
'price_per_unit' => $_POST['price_per_unit'] !== '' ? (float) $_POST['price_per_unit'] : null,
|
||||
'package_price' => $packagePrice,
|
||||
'old_package_price' => $oldPackagePrice,
|
||||
'discount_type' => $discountType,
|
||||
'discount_percent' => $discountPercent,
|
||||
]);
|
||||
$message = 'Товар добавлен.';
|
||||
} catch (\Throwable $exception) {
|
||||
$error = app_env('APP_DEBUG', 'false') === 'true' ? $exception->getMessage() : 'Не удалось добавить товар.';
|
||||
}
|
||||
}
|
||||
|
||||
view('admin/products', [
|
||||
'title' => 'Товары - админка Рыбсток',
|
||||
'breadcrumbs' => [
|
||||
['title' => 'Главная', 'url' => '/'],
|
||||
['title' => 'Админка', 'url' => '/admin/'],
|
||||
['title' => 'Товары'],
|
||||
],
|
||||
'categories' => $categoryRepository->flatActive(),
|
||||
'products' => $productRepository->adminLatest(30),
|
||||
'message' => $message,
|
||||
'error' => $error,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user