Ignore:
Timestamp:
Apr 14, 2015, 10:20:16 PM (10 years ago)
Author:
chronos
Message:
  • Removed: Spaces on end of line.
  • Modified: Tabs converted to spaces.
Location:
trunk/Modules/FinanceBankAPI
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/FinanceBankAPI/FileImport.php

    r719 r738  
    88  var $Database;
    99  var $BankAccount;
    10  
     10
    1111  function __construct($System)
    1212  {
     
    1414    $this->System = &$System;
    1515  }
    16      
     16
    1717  function Import()
    1818  {
    1919  }
    20  
     20
    2121  function ImportFile($Content, $Ext)
    22   {   
     22  {
    2323  }
    24  
     24
    2525  function PairOperations()
    2626  {
     
    3232        $DbResult2 = $this->Database->select('Subject', 'Id', 'Id='.$DbRow['VariableSymbol']);
    3333        if($DbResult2->num_rows == 1)
    34         { 
     34        {
    3535          $DbRow2 = $DbResult2->fetch_assoc();
    36           // TODO: Replace constants by links to real database numbers 
     36          // TODO: Replace constants by links to real database numbers
    3737          if($DbRow['Value'] >= 0) {
    38                 $DocumentLine = 3; // Receive money
    39                 $Direction = 1;                 
     38            $DocumentLine = 3; // Receive money
     39            $Direction = 1;
    4040          } else {
    41                 $DocumentLine = 4; // Send money
    42                 $Direction = -1;
     41            $DocumentLine = 4; // Send money
     42            $Direction = -1;
    4343          }
    4444          $Year = date('Y', MysqlDateToTime($DbRow['Time']));
    4545          $BillCode = $this->System->Modules['Finance']->GetNextDocumentLineNumber($DocumentLine, $Year);
    46           $DbResult3 = $this->Database->insert('FinanceOperation', array('Subject' => $DbRow2['Id'], 'Cash' => 0, 
     46          $DbResult3 = $this->Database->insert('FinanceOperation', array('Subject' => $DbRow2['Id'], 'Cash' => 0,
    4747            'Value' => Abs($DbRow['Value']), 'Direction' => $Direction, 'Taxable' => 1, 'BankAccount' => $DbRow['BankAccount'], 'Network' => 1,
    4848            'Time' => $DbRow['Time'], 'Text' => $DbRow['Description'], 'BillCode' => $BillCode, 'DocumentLine' => $DocumentLine));
     
    6161
    6262  function Show()
    63   {   
     63  {
    6464    $Output = '';
    6565    if(!$this->System->User->CheckPermission('Finance', 'SubjectList')) return('Nemáte oprávnění');
    66      
     66
    6767    $DbResult = $this->Database->select('FinanceBankAccount', '*', 'Id='.$_GET['i']);
    6868    $BankAccount = $DbResult->fetch_assoc();
    69    
     69
    7070    $DbResult = $this->Database->select('FinanceBank', '*', 'Id='.$BankAccount['Bank']);
    7171    $Bank = $DbResult->fetch_assoc();
    7272    $Output .= 'Účet: '.$BankAccount['Number'].'/'.$Bank['Code'].' ('.$Bank['Name'].')';
    73    
     73
    7474    if($Bank['Code'] == '2010') $Import = new ImportFio($this->System);
    7575      else if($Bank['Code'] == '0300') $Import = new ImportPS($this->System);
     
    8080      $Output .= $Import->Import();
    8181      $Import->PairOperations();
    82     } 
     82    }
    8383    return($Output);
    8484  }
    8585}
    86  
     86
    8787class PageImportFile extends Page
    8888{
     
    9090  var $ShortTitle = 'Import plateb ze souboru';
    9191  var $ParentClass = 'PageFinance';
    92  
     92
    9393  function Show()
    9494  {
     
    103103    return($Output);
    104104  }
    105  
     105
    106106  function ShowForm()
    107107  {
     
    113113    return($Output);
    114114  }
    115  
     115
    116116  function Prepare()
    117   {   
     117  {
    118118    $Form = new Form($this->System->FormManager);
    119119    $Form->SetClass('ImportBankFile');
     
    125125    $DbResult = $this->Database->select('FinanceBankAccount', '*', 'Id='.$Form->Values['BankAccount']);
    126126    $BankAccount = $DbResult->fetch_assoc();
    127    
     127
    128128    $DbResult = $this->Database->select('FinanceBank', '*', 'Id='.$BankAccount['Bank']);
    129129    $Bank = $DbResult->fetch_assoc();
    130130    $Output .= 'Účet: '.$BankAccount['Number'].'/'.$Bank['Code'].' ('.$Bank['Name'].')';
    131    
     131
    132132    if($Bank['Code'] == '2010') $Import = new ImportFio($this->System);
    133133      else if($Bank['Code'] == '0300') $Import = new ImportPS($this->System);
     
    135135    $Import->BankAccount = $BankAccount;
    136136    $Output .= $Import->ImportFile($File->GetContent(), $File->GetExt());
    137    
     137
    138138    return($Output);
    139139  }
    140  
     140
    141141  function InsertMoney($Subject, $Value, $Direction, $Cash, $Taxable, $Time, $Text, $DocumentLine)
    142142  {
    143     $Year = date('Y', $Time);     
     143    $Year = date('Y', $Time);
    144144    $BillCode = $this->System->Modules['Finance']->GetNextDocumentLineNumber($DocumentLine, $Year);
    145     $this->Database->insert('FinanceOperation', array('Text' => $Text, 
    146         'Subject' => $Subject, 'Cash' => $Cash, 'Value' => $Value, 'Direction' => $Direction,
    147         'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable, 'BillCode' => $BillCode));
     145    $this->Database->insert('FinanceOperation', array('Text' => $Text,
     146      'Subject' => $Subject, 'Cash' => $Cash, 'Value' => $Value, 'Direction' => $Direction,
     147      'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable, 'BillCode' => $BillCode));
    148148  }
    149149
     
    152152    $Finance = $this->System->Modules['Finance'];
    153153    $Output = '';
    154    
     154
    155155    for($I = $_POST['ItemCount'] - 1; $I >= 0 ; $I--)
    156156    {
    157157      // TODO: Use links to database records instead of contants
    158158      if($_POST['Money'.$I] < 0) {
    159         $DocumentLine = 4;
    160         $Direction = -1;
     159        $DocumentLine = 4;
     160        $Direction = -1;
    161161      } else {
    162         $DocumentLine = 3;
    163         $Direction = 1;
     162        $DocumentLine = 3;
     163        $Direction = 1;
    164164      }
    165         $Date = explode('-', $_POST['Date'.$I]);
     165      $Date = explode('-', $_POST['Date'.$I]);
    166166      $Date = mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]);
    167167      $this->InsertMoney($_POST['Subject'.$I], Abs($_POST['Money'.$I]), $Direction, 0, $_POST['Taxable'.$I], $Date, $_POST['Text'.$I], $DocumentLine);
  • trunk/Modules/FinanceBankAPI/FinanceBankAPI.php

    r735 r738  
    5959
    6060    $this->System->RegisterPage(array('finance', 'import-api'), 'PageImportAPI');
    61     $this->System->RegisterPage(array('finance', 'import-soubor'), 'PageImportFile');   
     61    $this->System->RegisterPage(array('finance', 'import-soubor'), 'PageImportFile');
    6262  }
    6363
     
    7979      'presetBankAccount' => $Item['BankAccount'],
    8080      'presetDocumentLine' => $DocumentLine);
    81         return($Preset);
     81    return($Preset);
    8282  }
    8383}
     
    8585class ScheduleBankImport extends SchedulerTask
    8686{
    87         function Execute()
    88         {
    89                 $Output = '';
    90                 $DbResult = $this->Database->select('FinanceBankAccount', 'Id, Comment',
    91                         '(`AutoImport`=1) AND ((`TimeEnd` IS NULL) OR (`TimeEnd` > NOW()))');
    92                 while($DbRow = $DbResult->fetch_assoc())
    93                 {                       
    94                         echo($DbRow['Comment']."\n");
    95                         $Page = new PageImportAPI($this->System);
    96                         $Output .= $Page->Show();
    97                 }
    98                 return($Output);
    99         }
     87  function Execute()
     88  {
     89    $Output = '';
     90    $DbResult = $this->Database->select('FinanceBankAccount', 'Id, Comment',
     91      '(`AutoImport`=1) AND ((`TimeEnd` IS NULL) OR (`TimeEnd` > NOW()))');
     92    while($DbRow = $DbResult->fetch_assoc())
     93    {
     94      echo($DbRow['Comment']."\n");
     95      $Page = new PageImportAPI($this->System);
     96      $Output .= $Page->Show();
     97    }
     98    return($Output);
     99  }
    100100}
  • trunk/Modules/FinanceBankAPI/Fio.php

    r550 r738  
    11<?php
    2  
     2
    33include('GPC.php');
    4  
    5 class Fio 
     4
     5class Fio
    66{
    77  var $UserName;
    88  var $Password;
    9   var $Account; 
    10  
     9  var $Account;
     10
    1111  function Import($TimeFrom, $TimeTo)
    1212  {
     
    1414    if($this->Password == '') throw new Exception('Missing value for Password property.');
    1515    if(!is_numeric($this->Account)) throw new Exception('Missing or not numeric value for Account property.');
    16  
     16
    1717    $fp = fsockopen('ssl://www.fio.cz', 443, $errno, $errstr, 30);
    18     if(!$fp) 
     18    if(!$fp)
    1919    {
    2020      throw new Exception('Connection error: '.$errstr);
    21     } else 
     21    } else
    2222    {
    2323      // Send request
     
    3232      $Request .= "Connection: Close\r\n\r\n";
    3333      fwrite($fp, $Request);
    34  
     34
    3535      // Read response
    3636      $Response = array();
    37       while(!feof($fp)) 
     37      while(!feof($fp))
    3838      {
    3939        $Response[] = trim(fgets($fp, 1024));
    4040      }
    4141      fclose($fp);
    42  
     42
    4343      // Strip HTTP header
    4444      while($Response[0] != '') array_shift($Response);
    4545      array_shift($Response); // Remove empty line
    4646      //echo(implode("\n", $Response));
    47  
     47
    4848      // Parse all GPC lines
    4949      $GPC = new GPC();
     
    5858    }
    5959  }
    60  
     60
    6161  function NoValidDataError($Response)
    6262  {
    6363    // Try to get error message
    6464    // If something go wrong fio show HTML login page and display error message
    65         $Response = implode('', $Response);
     65  $Response = implode('', $Response);
    6666    $ErrorMessageStart = '<div id="oldform_warning">';
    67     if(strpos($Response, $ErrorMessageStart) !== false) 
    68         {
    69           $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart));
    70           $ErrorMessage = trim(substr($Response, 0, strpos($Response, '</div>')));
    71         } else $ErrorMessage = '';
    72         throw new Exception('No valid GPC data: '.$ErrorMessage);
     67    if(strpos($Response, $ErrorMessageStart) !== false)
     68  {
     69    $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart));
     70    $ErrorMessage = trim(substr($Response, 0, strpos($Response, '</div>')));
     71  } else $ErrorMessage = '';
     72  throw new Exception('No valid GPC data: '.$ErrorMessage);
    7373  }
    7474}
  • trunk/Modules/FinanceBankAPI/FioAPI.php

    r628 r738  
    11<?php
    2  
     2
    33// Specifikace API: http://www.fio.cz/docs/cz/API_Bankovnictvi.pdf
    44
     
    1010    else return($Text);
    1111}
    12  
     12
    1313class FioAPI
    1414{
     
    1616  var $Encoding;
    1717  var $Format;
    18  
     18
    1919  function __construct()
    2020  {
     
    2222    $this->Format = 'csv';
    2323  }
    24  
     24
    2525  function Import($TimeFrom, $TimeTo)
    2626  {
    2727    if($this->Token == '') throw new Exception('Missing value for Token property.');
    28  
     28
    2929    // URL format: https://www.fio.cz/ib_api/rest/periods/{token}/{datum od}/{datum do}/transactions.{format}
    3030    // Send request
     
    3232      date('Y-m-d', $TimeFrom).'/'.date('Y-m-d', $TimeTo).'/transactions.'.$this->Format;
    3333    $Response = file_get_contents('https://www.fio.cz'.$RequestURL);
    34     if($Response == FALSE) 
     34    if($Response == FALSE)
    3535    {
    3636      throw new Exception('Connection error');
    37     } else 
     37    } else
    3838    {
    3939      if($this->Format == 'gpc') $Response = iconv('windows-1250', $this->Encoding, $Response);
    4040      $Response = explode("\n", $Response);
    41      
     41
    4242      if($this->Format == 'gpc')
    43       {       
     43      {
    4444        // Parse all GPC lines
    4545        $GPC = new GPC();
     
    5757          'Items' => array(),
    5858        );
    59        
     59
    6060        // CVS header
    61         while($Response[0] != '') 
     61        while($Response[0] != '')
    6262        {
    6363          $Line = explode(';', $Response[0]);
    6464          if($Line[0] == 'accountId') $Result['AccountNumber'] = $Line[0];
    65           else if($Line[0] == 'bankId') $Result['BankId'] = $Line[0]; 
     65          else if($Line[0] == 'bankId') $Result['BankId'] = $Line[0];
    6666          else if($Line[0] == 'currency') $Result['Currency'] = $Line[0];
    6767          else if($Line[0] == 'iban') $Result['IBAN'] = $Line[0];
     
    7272          else if($Line[0] == 'dateEnd') $Result['DateEnd'] = $Line[0];
    7373          else if($Line[0] == 'idFrom') $Result['IdFrom'] = $Line[0];
    74           else if($Line[0] == 'idTo') $Result['IdTo'] = $Line[0];           
     74          else if($Line[0] == 'idTo') $Result['IdTo'] = $Line[0];
    7575          array_shift($Response);
    7676        }
     
    8686          $Date = explode('.', $Line[1]);
    8787          $Date = mktime(0, 0, 0, $Date[1], $Date[0], $Date[2]);
    88           $NewRecord = array('ID' => $Line[0], 'Date' => $Date, 'Value' => $Line[2], 'CurrencyCode' => $Line[3], 
     88          $NewRecord = array('ID' => $Line[0], 'Date' => $Date, 'Value' => $Line[2], 'CurrencyCode' => $Line[3],
    8989            'OffsetAccount' => $Line[4], 'OffsetAccountName' => $Line[5], 'BankCode' => $Line[6], 'BankName' => RemoveComma($Line[7]),
    9090            'ConstantSymbol' => $Line[8], 'VariableSymbol' => $Line[9], 'SpecificSymbol' => $Line[10],
    91             'UserIdent' => RemoveComma($Line[11]), 'Message' => RemoveComma($Line[12]), 'Type' => RemoveComma($Line[13]), 
    92             'User' => RemoveComma($Line[14]), 'Details' => RemoveComma($Line[15]), 'Comment' => RemoveComma($Line[16]), 
     91            'UserIdent' => RemoveComma($Line[11]), 'Message' => RemoveComma($Line[12]), 'Type' => RemoveComma($Line[13]),
     92            'User' => RemoveComma($Line[14]), 'Details' => RemoveComma($Line[15]), 'Comment' => RemoveComma($Line[16]),
    9393            'BIC' => $Line[17], 'OrderID' => $Line[18]);
    94           $Result['Items'][] = $NewRecord;         
     94          $Result['Items'][] = $NewRecord;
    9595        }
    9696      }
     
    9898    }
    9999  }
    100  
     100
    101101  function NoValidDataError($Response)
    102102  {
     
    105105    $Response = implode('', $Response);
    106106    $ErrorMessageStart = '<div id="oldform_warning">';
    107     if(strpos($Response, $ErrorMessageStart) !== false) 
     107    if(strpos($Response, $ErrorMessageStart) !== false)
    108108    {
    109109      $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart));
  • trunk/Modules/FinanceBankAPI/FioDemo.php

    r548 r738  
    11<?php
    2  
     2
    33include('FioAPI.php');
    4  
     4
    55$Fio = new FioAPI();
    66$Fio->Token = '';
     
    1414{
    1515  echo('<tr>');
    16   if($Record['Type'] == GPC_TYPE_REPORT) 
     16  if($Record['Type'] == GPC_TYPE_REPORT)
    1717  {
    1818    echo('<td>Jméno účtu: '.$Record['AccountName'].'</td>');
     
    2222    echo('<td>Suma příjmů: '.$Record['CreditValue'].' Kč</td>');
    2323    echo('<td>Suma výdajů: '.$Record['DebitValue'].' Kč</td>');
    24  
     24
    2525    echo('</tr></table><br><br>');
    2626    echo('<table border="1"><tr>');
    27  
     27
    2828    echo('<th>Datum</th>');
    2929    echo('<th>Částka</th>');
     
    3535    echo('<th>Uživatelská identifikace</th>');
    3636  } else
    37   if($Record['Type'] == GPC_TYPE_ITEM) 
     37  if($Record['Type'] == GPC_TYPE_ITEM)
    3838  {
    3939    echo('<td>'.date('j.n.Y', $Record['DueDate']).'</td>');
     
    4545    echo('<td>'.$Record['SpecificSymbol'].'</td>');
    4646    echo('<td>'.$Record['ClientName'].'</td>');
    47   }         
     47  }
    4848  echo('</tr>');
    49  
     49
    5050}
    5151echo('</table>');
  • trunk/Modules/FinanceBankAPI/GPC.php

    r550 r738  
    11<?php
    2  
     2
    33define('GPC_TYPE_REPORT', '074');
    44define('GPC_TYPE_ITEM', '075');
    5  
     5
    66class GPC
    77{
    88  function ParseLine($Line)
    9   { 
     9  {
    1010    $Line = ' '.$Line;
    11     $Type = mb_substr($Line, 1, 3);                                                   
    12  
     11    $Type = mb_substr($Line, 1, 3);
     12
    1313    if($Type == GPC_TYPE_REPORT)
    1414    {
     
    1919        'AccountName' => trim(mb_substr($Line, 20, 20)),
    2020        'OldBalanceDate' => mktime(0, 0, 0, mb_substr($Line, 42, 2), mb_substr($Line, 40, 2), '20'.mb_substr($Line, 44, 2)),
    21         'OldBalanceValue' => (mb_substr($Line, 60, 1).mb_substr($Line, 46, 14)) / 100, 
     21        'OldBalanceValue' => (mb_substr($Line, 60, 1).mb_substr($Line, 46, 14)) / 100,
    2222        'NewBalanceValue' => (mb_substr($Line, 75, 1).mb_substr($Line, 61, 14)) / 100,
    23         'DebitValue' => (mb_substr($Line, 90, 1).mb_substr($Line, 76, 14)) / 100,   
    24         'CreditValue' => (mb_substr($Line, 105, 1).mb_substr($Line, 91, 14)) / 100,   
     23        'DebitValue' => (mb_substr($Line, 90, 1).mb_substr($Line, 76, 14)) / 100,
     24        'CreditValue' => (mb_substr($Line, 105, 1).mb_substr($Line, 91, 14)) / 100,
    2525        'SequenceNumber' => intval(mb_substr($Line, 106, 3)),
    2626        'Date' => mktime(0, 0, 0, mb_substr($Line, 111, 2), mb_substr($Line, 109, 2), '20'.mb_substr($Line, 113, 2)),
     
    2828        'CheckSum' => sha1(md5($Line).$Line),
    2929      );
    30     } else   
     30    } else
    3131    if($Type == GPC_TYPE_ITEM)
    32     {   
     32    {
    3333      $GPCLine = array
    3434      (
    3535        'Type' => GPC_TYPE_ITEM,
    3636        'AccountNumber' => mb_substr($Line, 4, 16),
    37         'OffsetAccount' => mb_substr($Line, 20, 16), 
    38         'RecordNumber' => mb_substr($Line, 36, 13), 
     37        'OffsetAccount' => mb_substr($Line, 20, 16),
     38        'RecordNumber' => mb_substr($Line, 36, 13),
    3939        'Value' => mb_substr($Line, 49, 12) / 100,
    40         'Code' => mb_substr($Line, 61, 1),   
     40        'Code' => mb_substr($Line, 61, 1),
    4141        'VariableSymbol' => intval(mb_substr($Line, 62, 10)),
    4242        'BankCode' => mb_substr($Line, 74, 4),
    4343        'ConstantSymbol' => intval(mb_substr($Line, 78, 4)),
    44         'SpecificSymbol' => intval(mb_substr($Line, 82, 10)), 
     44        'SpecificSymbol' => intval(mb_substr($Line, 82, 10)),
    4545        'Valut' => mb_substr($Line, 92, 6),
    46         'ClientName' => mb_substr($Line, 98, 20), 
     46        'ClientName' => mb_substr($Line, 98, 20),
    4747        //'Zero' => substr($Line, 118, 1),
    4848        'CurrencyCode' => mb_substr($Line, 119, 4),
     
    5050        'CheckSum' => sha1(md5($Line).$Line),
    5151      );
    52     } else 
     52    } else
    5353    $GPCLine = NULL;
    54  
     54
    5555    return($GPCLine);
    5656  }
  • trunk/Modules/FinanceBankAPI/ImportFio.php

    r715 r738  
    11<?php
    2  
     2
    33include('FioAPI.php');
    4  
     4
    55class ImportFio extends BankImport
    66{
    77  function Import()
    8   {   
     8  {
    99    $Fio = new FioAPI();
    1010    $Fio->Token = $this->BankAccount['LoginName'];
     
    2020        //$Output .= '<td>Ke dni '.date('j.n.Y', $Records['DateEnd']).' je stav účtu '.$Records['ClosingBalance'].' Kč</td>';
    2121        //$Output .= '<td>Suma příjmů: '.$Records['CreditValue'].' Kč</td>';
    22         //$Output .= '<td>Suma výdajů: '.$Records['DebitValue'].' Kč</td>';     
     22        //$Output .= '<td>Suma výdajů: '.$Records['DebitValue'].' Kč</td>';
    2323      //$Output .= '</tr>';
    2424    foreach($Records['Items'] as $Record)
     
    2626      $DbResult = $this->Database->select('FinanceBankImport', 'ID', 'Identification='.$Record['ID']);
    2727      if($DbResult->num_rows == 0)
    28       {     
     28      {
    2929        $Output .= '<tr>';
    3030        $this->Database->insert('FinanceBankImport', array('Time' => TimeToMysqlDate($Record['Date']),
    31           'BankAccount' => $this->BankAccount['Id'], 'Value' => $Record['Value'], 
     31          'BankAccount' => $this->BankAccount['Id'], 'Value' => $Record['Value'],
    3232          'SpecificSymbol' => $Record['SpecificSymbol'], 'VariableSymbol' => $Record['VariableSymbol'],
    3333          'ConstantSymbol' => $Record['ConstantSymbol'], 'Currency' => $this->BankAccount['Currency'],
     
    4141      }
    4242    }
    43     $Output .= '</table>';   
    44     $this->Database->update('FinanceBankAccount', 'Id='.$this->BankAccount['Id'], 
    45         array('LastImportDate' => TimeToMysqlDate($PeriodEnd)));
     43    $Output .= '</table>';
     44    $this->Database->update('FinanceBankAccount', 'Id='.$this->BankAccount['Id'],
     45      array('LastImportDate' => TimeToMysqlDate($PeriodEnd)));
    4646    return($Output);
    4747  }
  • trunk/Modules/FinanceBankAPI/ImportPS.php

    r550 r738  
    1414    $Data = explode("\n", $Content);
    1515  }
    16  
     16
    1717  function ImportCVS($Content)
    1818  {
    1919    $Finance = &$this->System->Modules['Finance'];
    20    
     20
    2121    $Data = explode("\n", $Content);
    2222    foreach($Data as $Key => $Value)
     
    4545        11 => '',
    4646    );
    47    
     47
    4848    if($Header != $Data[0]) $Output = 'Nekompatibilní struktura CSV';
    4949    else
     
    7979          $Style = '';
    8080        }
    81    
     81
    8282        if($Money < 0) $Text = 'Platba převodem';
    8383        else $Text = 'Přijatá platba';
     
    102102      $Output .= '<input type="submit" value="Zpracovat"/></form>';
    103103    }
    104   } 
     104  }
    105105}
Note: See TracChangeset for help on using the changeset viewer.