updaing import items

This commit is contained in:
Flatlogic Bot 2026-02-21 10:33:17 +00:00
parent 2d01fa2766
commit a4b81a5393
3 changed files with 74 additions and 36 deletions

View File

@ -94,34 +94,15 @@ if ($page === 'activate') {
} }
} }
if (isset($_POST['update_license'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['start_trial'])) {
$id = (int)$_POST['id']; // We need a way to call startTrial() which is private in LicenseService
$status = $_POST['status'] ?? null; // I'll make it public or use a public wrapper
$owner = $_POST['owner'] ?? null; $res = LicenseService::initTrial();
$address = $_POST['address'] ?? null;
$updateData = [];
if ($status !== null) $updateData['status'] = $status;
if ($owner !== null) $updateData['owner'] = $owner;
if ($address !== null) $updateData['address'] = $address;
$res = LicenseService::updateLicense($id, $updateData);
if ($res['success']) { if ($res['success']) {
$message = "License updated successfully!"; $success = "Trial period started! Redirecting...";
header("refresh:2;url=index.php");
} else { } else {
$message = "Error: " . ($res['error'] ?? 'Unknown error'); $error = $res['error'];
}
}
if (isset($_POST['issue_license'])) {
$max = (int)($_POST['max_activations'] ?? 1);
$owner = $_POST['owner'] ?? null;
$address = $_POST['address'] ?? null;
$res = LicenseService::issueLicense($max, 'FLAT', $owner, $address);
if ($res['success']) {
$message = "New License Issued: " . $res['license_key'];
} else {
$message = "Error: " . ($res['error'] ?? 'Unknown error');
} }
} }
?> ?>
@ -171,8 +152,18 @@ if ($page === 'activate') {
<label class="form-label small fw-bold text-muted"><?= $lang === 'ar' ? 'بصمة السيرفر' : 'Server Fingerprint' ?></label> <label class="form-label small fw-bold text-muted"><?= $lang === 'ar' ? 'بصمة السيرفر' : 'Server Fingerprint' ?></label>
<div class="fingerprint text-muted"><?= LicenseService::getFingerprint() ?></div> <div class="fingerprint text-muted"><?= LicenseService::getFingerprint() ?></div>
</div> </div>
<button type="submit" name="activate" class="btn btn-primary w-100 py-2"><?= __('activate_now') ?></button> <button type="submit" name="activate" class="btn btn-primary w-100 py-2 mb-3"><?= __('activate_now') ?></button>
</form> </form>
<?php if (!$is_activated && $trial_days <= 0): ?>
<div class="text-center">
<hr>
<p class="text-muted small"><?= $lang === 'ar' ? 'أو ابدأ الفترة التجريبية (15 يوم)' : 'Or start your 15-day trial period' ?></p>
<form method="POST">
<button type="submit" name="start_trial" class="btn btn-outline-secondary w-100 py-2"><?= $lang === 'ar' ? 'بدء الفترة التجريبية' : 'Start Free Trial' ?></button>
</form>
</div>
<?php endif; ?>
</div> </div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body> </body>
@ -1188,15 +1179,37 @@ function getPromotionalPrice($item) {
if (isset($_POST['import_items'])) { if (isset($_POST['import_items'])) {
if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === 0) { if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === 0) {
$handle = fopen($_FILES['excel_file']['tmp_name'], "r"); $tmpPath = $_FILES['excel_file']['tmp_name'];
fgetcsv($handle); $count = 0; $firstBytes = file_get_contents($tmpPath, false, null, 0, 4);
while (($data = fgetcsv($handle)) !== FALSE) { if ($firstBytes === "PK\x03\x04") {
if ($data[1] || $data[2]) { $message = "Error: It looks like you uploaded an Excel (.xlsx) file. Please save it as CSV (UTF-8) and try again.";
db()->prepare("INSERT INTO stock_items (sku, name_en, name_ar, sale_price, purchase_price) VALUES (?, ?, ?, ?, ?)")->execute([$data[0] ?? '', $data[1] ?? '', $data[2] ?? '', (float)($data[3] ?? 0), (float)($data[4] ?? 0)]); } else {
$count++; $handle = fopen($tmpPath, "r");
fgetcsv($handle); // Skip header
$count = 0; $errors = 0;
while (($data = fgetcsv($handle)) !== FALSE) {
if (empty($data[1]) && empty($data[2])) continue;
try {
foreach ($data as &$val) {
if ($val === null) continue;
if (!mb_check_encoding($val, 'UTF-8')) {
$val = mb_convert_encoding($val, 'UTF-8', 'Windows-1252');
}
$val = mb_convert_encoding($val, 'UTF-8', 'UTF-8');
}
db()->prepare("INSERT INTO stock_items (sku, name_en, name_ar, sale_price, purchase_price)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE name_en=VALUES(name_en), name_ar=VALUES(name_ar),
sale_price=VALUES(sale_price), purchase_price=VALUES(purchase_price)")
->execute([$data[0] ?? '', $data[1] ?? '', $data[2] ?? '', (float)($data[3] ?? 0), (float)($data[4] ?? 0)]);
$count++;
} catch (Exception $e) {
$errors++;
}
} }
fclose($handle);
$message = "Import completed! $count items processed." . ($errors > 0 ? " ($errors rows skipped due to data errors.)" : "");
} }
fclose($handle); $message = "Imported $count items!";
} }
} }

View File

@ -21,8 +21,8 @@ class LicenseService {
$res = $stmt->fetch(); $res = $stmt->fetch();
if (!$res || !$res['trial_started_at']) { if (!$res || !$res['trial_started_at']) {
self::startTrial(); // self::startTrial(); // Disabled automatic trial start as per user request to keep things manual
return 15; return 0; // Return 0 if trial not started
} }
$started = strtotime($res['trial_started_at']); $started = strtotime($res['trial_started_at']);
@ -79,6 +79,18 @@ class LicenseService {
} }
} }
/**
* Public wrapper to initialize the trial period.
*/
public static function initTrial() {
try {
self::startTrial();
return ['success' => true];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
/** /**
* Initializes the trial period. * Initializes the trial period.
*/ */

View File

@ -127,12 +127,25 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$message = "License limits updated successfully."; $message = "License limits updated successfully.";
break; break;
} }
// Redirect to prevent re-submission on refresh
if ($message) {
$_SESSION['flash_message'] = $message;
header("Location: manage.php");
exit;
}
} }
} catch (Exception $e) { } catch (Exception $e) {
$error = "Operation failed: " . $e->getMessage(); $error = "Operation failed: " . $e->getMessage();
} }
} }
// Get flash message from session
if (isset($_SESSION['flash_message'])) {
$message = $_SESSION['flash_message'];
unset($_SESSION['flash_message']);
}
// Fetch Licenses // Fetch Licenses
try { try {
$licenses = $pdo->query("SELECT * FROM licenses ORDER BY created_at DESC")->fetchAll(); $licenses = $pdo->query("SELECT * FROM licenses ORDER BY created_at DESC")->fetchAll();