AddReference('BankAccount', FinanceBankAccount::GetClassName());
$Desc->AddDate('Time');
$Desc->AddString('Identification');
$Desc->AddString('AccountNumber');
$Desc->AddString('BankCode');
$Desc->AddString('VariableSymbol');
$Desc->AddString('ConstantSymbol');
$Desc->AddString('SpecificSymbol');
$Desc->AddFloat('Value');
$Desc->AddReference('Currency', Currency::GetClassName());
$Desc->AddString('OffsetAccountName');
$Desc->AddReference('FinanceOperation', FinanceOperation::GetClassName());
return $Desc;
}
}
class ModuleFinanceBankAPI extends Module
{
function __construct(System $System)
{
parent::__construct($System);
$this->Name = 'FinanceBankAPI';
$this->Version = '1.0';
$this->Creator = 'Chronos';
$this->License = 'GNU/GPLv3';
$this->Description = 'Communication through API to various banks, manual file import';
$this->Dependencies = array(ModuleFinance::GetName(), ModuleScheduler::GetName(), ModuleIS::GetName(),
ModuleDocument::GetName());
$this->Models = array(FinanceBankImport::GetClassName());
}
function DoStart(): void
{
$this->System->FormManager->RegisterClass('ImportBankFile', array(
'Title' => 'Import souborů s platbami',
'Table' => 'FinanceBank',
'SubmitText' => 'Načíst',
'Items' => array(
'BankAccount' => array('Type' => 'TBankAccount', 'Caption' => 'Bankovní účet', 'Default' => ''),
'File' => array('Type' => 'File', 'Caption' => 'Soubor', 'Default' => ''),
),
));
$this->System->FormManager->RegisterClass('FinanceBankImport', array(
'Title' => 'Import plateb z účtu',
'Table' => 'FinanceBankImport',
'DefaultSortColumn' => 'Time',
'DefaultSortOrder' => 1,
'Items' => array(
'BankAccount' => array('Type' => 'TBankAccount', 'Caption' => 'Účet', 'Default' => ''),
'Time' => array('Type' => 'Date', 'Caption' => 'Čas', 'Default' => ''),
'Identification' => array('Type' => 'String', 'Caption' => 'Kód operace', 'Default' => ''),
'AccountNumber' => array('Type' => 'String', 'Caption' => 'Číslo účtu', 'Default' => ''),
'BankCode' => array('Type' => 'String', 'Caption' => 'Kód banky', 'Default' => ''),
'VariableSymbol' => array('Type' => 'String', 'Caption' => 'Variabilní symbol', 'Default' => ''),
'ConstantSymbol' => array('Type' => 'String', 'Caption' => 'Konstantní symbol', 'Default' => ''),
'SpecificSymbol' => array('Type' => 'String', 'Caption' => 'Specifický symbol', 'Default' => ''),
'Value' => array('Type' => 'Float', 'Caption' => 'Částka', 'Default' => '', 'Suffix' => 'Kč'),
'Currency' => array('Type' => 'TCurrency', 'Caption' => 'Měna', 'Default' => ''),
'Description' => array('Type' => 'String', 'Caption' => 'Popis operace', 'Default' => ''),
'OffsetAccountName' => array('Type' => 'String', 'Caption' => 'Jméno protiúčtu', 'Default' => ''),
'FinanceOperation' => array('Type' => 'TFinanceOperation', 'Caption' => 'Přiřazená operace', 'Default' => '', 'Null' => true, 'OnPreset' => array($this, 'PresetItem')),
),
));
$this->System->RegisterPage(['finance', 'import-api'], 'PageImportAPI');
$this->System->RegisterPage(['finance', 'import-soubor'], 'PageImportFile');
ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('FinanceBankAPI',
array($this, 'ShowDashboardItem'));
}
function ShowDashboardItem(): string
{
$DbResult = $this->Database->select('FinanceBankImport', 'COUNT(*)', '`FinanceOperation` IS NULL');
$DbRow = $DbResult->fetch_row();
$Output = 'Nezpárovaných plateb: '.$DbRow['0'].'
';
return $Output;
}
function PresetItem(array $Item): array
{
$Preset = array();
if ($Item['Value'] < 0) $OperationGroupId = OPERATION_GROUP_ACCOUNT_OUT;
else $OperationGroupId = OPERATION_GROUP_ACCOUNT_IN;
$FinanceGroup = ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance->GetFinanceGroupById($OperationGroupId, 'FinanceOperationGroup');
$Preset = array(
'presetTime' => $Item['Time'],
'presetValueUser' => abs($Item['Value']),
'presetTaxable' => 1,
'presetText' => $Item['Description'],
'presetNetwork' => 1,
'presetBankAccount' => $Item['BankAccount'],
'presetGroup' => $FinanceGroup['Id']);
return $Preset;
}
}
class ScheduleBankImport extends SchedulerTask
{
function Execute(): string
{
$Output = '';
$DbResult = $this->Database->select('FinanceBankAccount', '`Id`, `Comment`',
'(`AutoImport`=1) AND (`TimeCreate` < NOW()) AND '.
'((`TimeEnd` IS NULL) OR (`TimeEnd` > NOW()))');
while ($DbRow = $DbResult->fetch_assoc())
{
echo($DbRow['Comment']."\n");
$Page = new PageImportAPI($this->System);
$Output .= $Page->Import($DbRow['Id']);
}
return $Output;
}
}