| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/backend-accounting.sk/app/modules/worksheets/code/ |
Upload File : |
<?php
namespace modules\worksheets\code;
use app\classes\PDOElement;
class WorksheetMainControl extends PDOElement {
public static $dbTable = 'worsheet_main_control';
protected $selfUpdate = true;
public const EDITABLE_FIELDS = [
'submission_date',
'vat',
'kv_vat',
'sv_vat',
'oss',
'td_submission',
'notifications',
'additional',
'errors',
'resolved_errors',
];
public const CHECKBOX_FIELDS = [
'vat',
'kv_vat',
'sv_vat',
'oss',
'td_submission',
'notifications',
'resolved_errors',
];
/** Fields that decide whether Kontrola podania cell is visible. */
public const VISIBILITY_FIELDS = [
'vat',
'kv_vat',
'sv_vat',
'oss',
'td_submission',
];
/** Fields used to calculate Kontrola podania completion percent. */
public const RATING_FIELDS = [
'vat',
'kv_vat',
'sv_vat',
'oss',
'td_submission',
'notifications',
];
/** Extra control fields loaded onto the main grid row for the cell UI. */
public const CELL_EXTRA_FIELDS = [
'errors',
'resolved_errors',
];
/**
* @return list<string>
*/
public static function getRowPayloadFields(): array {
return array_values(array_unique(array_merge(['id'], self::RATING_FIELDS, self::CELL_EXTRA_FIELDS)));
}
public function __construct($id) {
parent::__construct($id, self::$dbTable, ['id' => $id], 'id', false, false, true);
}
/**
* @return void
*/
public static function add(array $data): void {
$db = \app\classes\PDODatabase::getInstance();
// Skip post-insert id touch-ups: composite PK table; caller does not need a loaded model.
$db->addRecord(self::$dbTable, $data, [], false);
}
/**
* @return array<string, mixed>|null
*/
public static function fetchRow(int $period, int $companyId): ?array {
$db = \app\classes\PDODatabase::getInstance();
$row = $db->getRow(
self::$dbTable,
0,
'id',
false,
[
'PERIOD' => $period,
'COMPANY_ID' => $companyId,
],
'SELECT * FROM `' . self::$dbTable . '` '
. 'WHERE worksheets_main_period = :PERIOD AND worksheets_main_gdrive_watchdog_id = :COMPANY_ID LIMIT 1'
);
return is_array($row) ? $row : null;
}
/**
* Load by composite key or create an empty control row.
* Constructed from a row array to avoid PDOElement getRow(:id) binding issues.
*/
public static function findOrCreate(int $period, int $companyId): self {
if ($period <= 0 || $companyId <= 0) {
throw new \InvalidArgumentException('Missing period or gdrive_watchdog_id for worsheet_main_control.');
}
$row = self::fetchRow($period, $companyId);
if (is_array($row) && !empty($row['id'])) {
return new self($row);
}
self::add([
'worksheets_main_period' => $period,
'worksheets_main_gdrive_watchdog_id' => $companyId,
]);
$row = self::fetchRow($period, $companyId);
if (!is_array($row) || empty($row['id'])) {
throw new \RuntimeException('Failed to create worsheet_main_control row.');
}
return new self($row);
}
/**
* @return array<string, mixed>
*/
public function toFrontendArray(): array {
$data = [
'id' => (int) $this->_get('id'),
'worksheets_main_period' => (int) $this->_get('worksheets_main_period'),
'worksheets_main_gdrive_watchdog_id' => (int) $this->_get('worksheets_main_gdrive_watchdog_id'),
];
foreach (self::EDITABLE_FIELDS as $field) {
$data[$field] = $this->_get($field);
}
return $data;
}
/**
* Extended VAT checkbox: §7a unchecked (2) → classic 0; removed (4) → null.
*/
public static function normalizeClassicVatCheckbox($val): ?int {
if ($val === null || $val === '') {
return null;
}
$intVal = (int) $val;
if ($intVal === \modules\worksheets\code\columns\VatCheckboxColumnController::REMOVED) {
return null;
}
if ($intVal === \modules\worksheets\code\columns\VatCheckboxColumnController::UNCHECKED_7A) {
return 0;
}
if ($intVal === \modules\worksheets\code\columns\VatCheckboxColumnController::CHECKED_7A) {
return 1;
}
return $intVal;
}
/**
* Classic control checkbox: null / 0 / 1 only.
*
* @param mixed $val
*/
public static function normalizeClassicCheckbox($val): ?int {
if ($val === null || $val === '' || $val === false) {
return null;
}
if ($val === true || $val === 1 || $val === '1') {
return 1;
}
if ($val === 0 || $val === '0') {
return 0;
}
throw new \InvalidArgumentException('Invalid classic checkbox value.');
}
/**
* @param mixed $val
*/
public static function normalizeDate($val): ?string {
if ($val === null || $val === false) {
return null;
}
$val = trim((string) $val);
if ($val === '') {
return null;
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $val)) {
throw new \InvalidArgumentException('Invalid date value.');
}
return $val;
}
/**
* Seed a control row for a newly created worksheets_main period record.
*/
public static function createFromWorksheetMainData(array $worksheetData): void {
$period = (int) ($worksheetData['period'] ?? 0);
$companyId = (int) ($worksheetData['gdrive_watchdog_id'] ?? 0);
if ($period <= 0 || $companyId <= 0) {
throw new \InvalidArgumentException('Missing period or gdrive_watchdog_id for worsheet_main_control.');
}
self::add([
'worksheets_main_period' => $period,
'worksheets_main_gdrive_watchdog_id' => $companyId,
'vat' => self::normalizeClassicVatCheckbox($worksheetData['vat'] ?? null),
'kv_vat' => isset($worksheetData['kv_vat']) && $worksheetData['kv_vat'] !== ''
? (int) $worksheetData['kv_vat']
: null,
'sv_vat' => self::normalizeClassicVatCheckbox($worksheetData['sv_vat'] ?? null),
'oss' => isset($worksheetData['oss']) && $worksheetData['oss'] !== ''
? (int) $worksheetData['oss']
: null,
'td_submission' => array_key_exists('td_submission', $worksheetData)
? self::normalizeClassicCheckbox(
($worksheetData['td_submission'] === '' ? null : $worksheetData['td_submission'])
)
: null,
]);
}
/**
* When main vat / sv_vat is removed (× = 4) → control field null.
* When × is restored → control field 0.
*
* @param mixed $newValue
* @param mixed $previousValue
*/
public static function syncClassicVatFromMainField(
int $period,
int $companyId,
string $field,
$newValue,
$previousValue
): void {
if ($period <= 0 || $companyId <= 0 || !in_array($field, ['vat', 'sv_vat'], true)) {
return;
}
$removed = \modules\worksheets\code\columns\VatCheckboxColumnController::REMOVED;
$newInt = ($newValue === null || $newValue === '') ? null : (int) $newValue;
$prevInt = ($previousValue === null || $previousValue === '') ? null : (int) $previousValue;
if ($newInt === $removed) {
$controlValue = null;
} elseif ($prevInt === $removed) {
$controlValue = 0;
} else {
return;
}
$control = self::findOrCreate($period, $companyId);
$control->update([$field => $controlValue]);
}
/**
* Mirror main worksheets_main.td_submission into control (null / 0 / 1).
*
* @param mixed $value
*/
public static function syncTdSubmissionFromMain(int $period, int $companyId, $value): void {
if ($period <= 0 || $companyId <= 0) {
return;
}
try {
$stored = self::normalizeClassicCheckbox($value);
} catch (\InvalidArgumentException $e) {
return;
}
$control = self::findOrCreate($period, $companyId);
$control->update(['td_submission' => $stored]);
}
}