Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (4 years ago)
Author:
chronos
Message:
  • Added: Static types added to almost all classes, methods and function. Supported by PHP 7.4.
  • Fixed: Various found code issues.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Finance/Finance.php

    r877 r887  
    4141  var $Rounding;
    4242
    43   function LoadMonthParameters($Period = 1) // 0 - now, 1 - next month
     43  function LoadMonthParameters(int $Period = 1) // 0 - now, 1 - next month
    4444  {
    4545    $DbResult = $this->Database->query('SELECT * FROM `FinanceBillingPeriod`');
     
    7878  }
    7979
    80   function W2Kc($Spotreba)
     80  function W2Kc($Spotreba): string
    8181  {
    8282    return round($Spotreba * 0.72 * $this->kWh);
     
    8888    $EndTime = mktime(0, 0, 0, 12, 31, $Year);
    8989    $this->Database->insert('FinanceYear', array('Year' => $Year,
    90           'DateStart' => TimeToMysqlDate($StartTime), 'DateEnd' => TimeToMysqlDate($EndTime), 'Closed' => 0));
    91         $YearId = $this->Database->insert_id;
     90            'DateStart' => TimeToMysqlDate($StartTime), 'DateEnd' => TimeToMysqlDate($EndTime), 'Closed' => 0));
     91          $YearId = $this->Database->insert_id;
    9292
    9393    // Create DocumentLineSequence from previous
     
    9595    while ($DbRow = $DbResult->fetch_assoc())
    9696    {
    97           $this->Database->insert('DocumentLineSequence', array('FinanceYear' => $YearId,
    98             'NextNumber' => 1, 'YearPrefix' => 1, 'DocumentLine' => $DbRow['Id']));
    99         }
    100   }
    101 
    102   function GetFinanceYear($Year)
     97            $this->Database->insert('DocumentLineSequence', array('FinanceYear' => $YearId,
     98              'NextNumber' => 1, 'YearPrefix' => 1, 'DocumentLine' => $DbRow['Id']));
     99          }
     100  }
     101
     102  function GetFinanceYear(int $Year)
    103103  {
    104104    if ($Year == 0)
     
    107107      $DbResult = $this->Database->select('FinanceYear', '*', '1 ORDER BY `Year` DESC LIMIT 1');
    108108    } else $DbResult = $this->Database->select('FinanceYear', '*', '`Year`='.$Year);
    109     if ($DbResult->num_rows == 0) {
    110           if ($Year == date('Y'))
    111           {
    112                 $this->CreateFinanceYear($Year);
     109    if ($DbResult->num_rows == 0)
     110    {
     111            if ($Year == date('Y'))
     112            {
     113                    $this->CreateFinanceYear($Year);
    113114        $DbResult = $this->Database->select('FinanceYear', '*', '`Year`='.$Year);
    114           } else throw new Exception('Rok '.$Year.' nenalezen');
    115         }
     115            } else throw new Exception('Rok '.$Year.' nenalezen');
     116          }
    116117    $FinanceYear = $DbResult->fetch_assoc();
    117118    if ($FinanceYear['Closed'] == 1)
     
    120121  }
    121122
    122   function GetNextDocumentLineNumber($Id, $FinanceYear = 0)
    123   {
    124         $FinanceYear = $this->GetFinanceYear($FinanceYear);
     123  function GetNextDocumentLineNumber(string $Id, int $FinanceYear = 0)
     124  {
     125          $FinanceYear = $this->GetFinanceYear($FinanceYear);
    125126
    126127    $DbResult = $this->Database->query('SELECT `Shortcut`, `Id` FROM `DocumentLine` WHERE `Id`='.$Id);
     
    141142  }
    142143
    143   function GetNextDocumentLineNumberId($Id, $FinanceYear = 0)
     144  function GetNextDocumentLineNumberId(string $Id, int $FinanceYear = 0): int
    144145  {
    145146    $Code = $this->GetNextDocumentLineNumber($Id, $FinanceYear);
     
    148149  }
    149150
    150   function GetFinanceGroupById($Id, $Table)
     151  function GetFinanceGroupById(string $Id, string $Table): ?array
    151152  {
    152153    $DbResult = $this->Database->query('SELECT * FROM `'.$Table.'` WHERE `Id`= '.$Id);
     
    154155      $Group = $DbResult->fetch_assoc();
    155156      return $Group;
    156     } else die('Finance group id '.$Id.' not found in table '.$Table);
    157   }
    158 
    159   function RecalculateMemberPayment()
     157    }
     158    echo('Finance group id '.$Id.' not found in table '.$Table);
     159    return null;
     160  }
     161
     162  function RecalculateMemberPayment(): string
    160163  {
    161164    $Output = 'Aktualizuji finance členů...<br />';
     
    199202      $Consumption = 0;
    200203      $this->Database->insert('MemberPayment', array('Member' => $Member['Id'],
    201           'MonthlyInternet' => $MonthlyInet,
    202           'MonthlyTotal' => $Monthly, 'MonthlyConsumption' => $this->W2Kc($Consumption),
    203           'Cash' => $Cash, 'MonthlyPlus' => $this->W2Kc($ConsumptionPlus)));
     204        'MonthlyInternet' => $MonthlyInet,
     205        'MonthlyTotal' => $Monthly, 'MonthlyConsumption' => $this->W2Kc($Consumption),
     206        'Cash' => $Cash, 'MonthlyPlus' => $this->W2Kc($ConsumptionPlus)));
    204207    }
    205     $this->System->ModuleManager->Modules['Log']->NewRecord('Finance', 'RecalculateMemberPayment');
     208    ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('Finance', 'RecalculateMemberPayment');
    206209    return $Output;
    207210  }
    208211
    209   function GetVATByType($TypeId)
     212  function GetVATByType(string $TypeId): string
    210213  {
    211214    $Time = time();
     
    220223class ModuleFinance extends AppModule
    221224{
    222   function __construct($System)
     225  public Finance $Finance;
     226  public Bill $Bill;
     227
     228  function __construct(System $System)
    223229  {
    224230    parent::__construct($System);
     
    229235    $this->Description = 'Base module for finance management';
    230236    $this->Dependencies = array('File', 'EmailQueue');
    231   }
    232 
    233   function DoInstall()
    234   {
    235   }
    236 
    237   function DoUninstall()
    238   {
    239   }
    240 
    241   function DoStart()
     237
     238    $this->Bill = new Bill($this->System);
     239    $this->Finance = new Finance($this->System);
     240  }
     241
     242  function DoInstall(): void
     243  {
     244  }
     245
     246  function DoUninstall(): void
     247  {
     248  }
     249
     250  function DoStart(): void
    242251  {
    243252    global $Config;
    244253
    245     $this->System->RegisterPage(array('finance', 'sprava'), 'PageFinanceManage');
    246     $this->System->RegisterPage(array('finance', 'platby'), 'PageFinanceUserState');
    247     $this->System->RegisterPage(array('finance', 'import'), 'PageFinanceImportPayment');
    248     $this->System->RegisterPage(array('finance', 'zivnost'), 'PageFinanceTaxFiling');
     254    $this->System->RegisterPage(['finance', 'sprava'], 'PageFinanceManage');
     255    $this->System->RegisterPage(['finance', 'platby'], 'PageFinanceUserState');
     256    $this->System->RegisterPage(['finance', 'import'], 'PageFinanceImportPayment');
     257    $this->System->RegisterPage(['finance', 'zivnost'], 'PageFinanceTaxFiling');
    249258
    250259    $this->System->FormManager->RegisterClass('FinanceOperation', array(
     
    644653    ));
    645654
    646 
    647     $this->System->AddModule(new Bill($this->System));
    648     $this->System->AddModule(new Finance($this->System));
    649     $this->System->Modules['Finance']->MainSubject = $Config['Finance']['MainSubjectId'];
    650     $this->System->Modules['Finance']->DirectoryId = $Config['Finance']['DirectoryId'];
    651 
    652     $this->System->ModuleManager->Modules['IS']->RegisterDashboardItem('Finance',
    653       array('ModuleFinance', 'ShowDashboardItem'));
    654   }
    655 
    656   function ShowDashboardItem()
     655    $this->Finance->MainSubject = $Config['Finance']['MainSubjectId'];
     656    $this->Finance->DirectoryId = $Config['Finance']['DirectoryId'];
     657
     658    ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('Finance', array($this, 'ShowDashboardItem'));
     659  }
     660
     661  function ShowDashboardItem(): string
    657662  {
    658663    $DbResult = $this->Database->select('FinanceOperation', 'ROUND(SUM(`Value`))', '1');
     
    662667  }
    663668
    664   function DoStop()
    665   {
    666   }
    667 
    668   function BeforeInsertFinanceOperation($Form)
     669  function DoStop(): void
     670  {
     671  }
     672
     673  function BeforeInsertFinanceOperation(Form $Form): array
    669674  {
    670675    if (array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
    671676      else $Year = date("Y", $Form->Values['ValidFrom']);
    672     $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
    673     $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
    674     return $Form->Values;
    675   }
    676 
    677   function AfterInsertFinanceOperation($Form, $Id)
    678   {
    679     $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
     677    $FinanceGroup = $this->Finance->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
     678    $Form->Values['BillCode'] = $this->Finance->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
     679    return $Form->Values;
     680  }
     681
     682  function AfterInsertFinanceOperation(Form $Form, string $Id): array
     683  {
     684    $FinanceGroup = $this->Finance->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
    680685    $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
    681686      ($Form->Values['ValueUser'] * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
     
    683688  }
    684689
    685   function BeforeModifyFinanceOperation($Form, $Id)
    686   {
    687     $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
     690  function BeforeModifyFinanceOperation(Form $Form, string $Id): array
     691  {
     692    $FinanceGroup = $this->Finance->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
    688693    $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
    689694      ($Form->Values['ValueUser'] * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
     
    691696  }
    692697
    693   function BeforeInsertFinanceInvoice($Form)
     698  function BeforeInsertFinanceInvoice(Form $Form): array
    694699  {
    695700    // Get new DocumentLineCode by selected invoice Group
    696701    if (array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
    697702      else $Year = date("Y", $Form->Values['ValidFrom']);
    698     $Group = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
    699     $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($Group['DocumentLine'], $Year);
    700     return $Form->Values;
    701   }
    702 
    703   function AfterInsertFinanceInvoice($Form, $Id)
    704   {
    705     $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
     703    $Group = $this->Finance->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
     704    $Form->Values['BillCode'] = $this->Finance->GetNextDocumentLineNumberId($Group['DocumentLine'], $Year);
     705    return $Form->Values;
     706  }
     707
     708  function AfterInsertFinanceInvoice(Form $Form, string $Id): array
     709  {
     710    $FinanceGroup = $this->Finance->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
    706711    $DbResult = $this->Database->query(str_replace('#Id', $Id, $Form->Definition['Items']['ValueUser']['SQL']));
    707712    $DbRow = $DbResult->fetch_row();
     
    713718  }
    714719
    715   function BeforeModifyFinanceInvoice($Form, $Id)
    716   {
    717     $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
     720  function BeforeModifyFinanceInvoice(Form $Form, string $Id): array
     721  {
     722    $FinanceGroup = $this->Finance->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
    718723    $DbResult = $this->Database->query(str_replace('#Id', $Id, $Form->Definition['Items']['ValueUser']['SQL']));
    719724    $DbRow = $DbResult->fetch_row();
     
    724729  }
    725730
    726   function AfterInsertFinanceInvoiceItem($Form, $Id)
     731  function AfterInsertFinanceInvoiceItem(Form $Form, string $Id): array
    727732  {
    728733    $ParentForm = new Form($this->System->FormManager);
     
    733738  }
    734739
    735   function AfterModifyFinanceInvoiceItem($Form, $Id)
     740  function AfterModifyFinanceInvoiceItem(Form $Form, string $Id): array
    736741  {
    737742    $ParentForm = new Form($this->System->FormManager);
     
    742747  }
    743748
    744   function BeforeInsertContract($Form)
     749  function BeforeInsertContract(Form $Form): array
    745750  {
    746751    if (array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
    747752      else $Year = date("Y", $Form->Values['ValidFrom']);
    748     $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($Form->Values['DocumentLine'], $Year);
    749     return $Form->Values;
     753    $Form->Values['BillCode'] = $this->Finance->GetNextDocumentLineNumberId($Form->Values['DocumentLine'], $Year);
     754    return $Form->Values;
     755  }
     756
     757  static function Cast(AppModule $AppModule): ModuleFinance
     758  {
     759    if ($AppModule instanceof ModuleFinance)
     760    {
     761      return $AppModule;
     762    }
     763    throw new Exception('Expected ModuleFinance type but '.gettype($AppModule));
    750764  }
    751765}
Note: See TracChangeset for help on using the changeset viewer.