Changeset 738 for trunk/Modules/FinanceBankAPI
- Timestamp:
- Apr 14, 2015, 10:20:16 PM (10 years ago)
- Location:
- trunk/Modules/FinanceBankAPI
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/FinanceBankAPI/FileImport.php
r719 r738 8 8 var $Database; 9 9 var $BankAccount; 10 10 11 11 function __construct($System) 12 12 { … … 14 14 $this->System = &$System; 15 15 } 16 16 17 17 function Import() 18 18 { 19 19 } 20 20 21 21 function ImportFile($Content, $Ext) 22 { 22 { 23 23 } 24 24 25 25 function PairOperations() 26 26 { … … 32 32 $DbResult2 = $this->Database->select('Subject', 'Id', 'Id='.$DbRow['VariableSymbol']); 33 33 if($DbResult2->num_rows == 1) 34 { 34 { 35 35 $DbRow2 = $DbResult2->fetch_assoc(); 36 // TODO: Replace constants by links to real database numbers 36 // TODO: Replace constants by links to real database numbers 37 37 if($DbRow['Value'] >= 0) { 38 39 $Direction = 1;38 $DocumentLine = 3; // Receive money 39 $Direction = 1; 40 40 } else { 41 42 41 $DocumentLine = 4; // Send money 42 $Direction = -1; 43 43 } 44 44 $Year = date('Y', MysqlDateToTime($DbRow['Time'])); 45 45 $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, 47 47 'Value' => Abs($DbRow['Value']), 'Direction' => $Direction, 'Taxable' => 1, 'BankAccount' => $DbRow['BankAccount'], 'Network' => 1, 48 48 'Time' => $DbRow['Time'], 'Text' => $DbRow['Description'], 'BillCode' => $BillCode, 'DocumentLine' => $DocumentLine)); … … 61 61 62 62 function Show() 63 { 63 { 64 64 $Output = ''; 65 65 if(!$this->System->User->CheckPermission('Finance', 'SubjectList')) return('Nemáte oprávnění'); 66 66 67 67 $DbResult = $this->Database->select('FinanceBankAccount', '*', 'Id='.$_GET['i']); 68 68 $BankAccount = $DbResult->fetch_assoc(); 69 69 70 70 $DbResult = $this->Database->select('FinanceBank', '*', 'Id='.$BankAccount['Bank']); 71 71 $Bank = $DbResult->fetch_assoc(); 72 72 $Output .= 'Účet: '.$BankAccount['Number'].'/'.$Bank['Code'].' ('.$Bank['Name'].')'; 73 73 74 74 if($Bank['Code'] == '2010') $Import = new ImportFio($this->System); 75 75 else if($Bank['Code'] == '0300') $Import = new ImportPS($this->System); … … 80 80 $Output .= $Import->Import(); 81 81 $Import->PairOperations(); 82 } 82 } 83 83 return($Output); 84 84 } 85 85 } 86 86 87 87 class PageImportFile extends Page 88 88 { … … 90 90 var $ShortTitle = 'Import plateb ze souboru'; 91 91 var $ParentClass = 'PageFinance'; 92 92 93 93 function Show() 94 94 { … … 103 103 return($Output); 104 104 } 105 105 106 106 function ShowForm() 107 107 { … … 113 113 return($Output); 114 114 } 115 115 116 116 function Prepare() 117 { 117 { 118 118 $Form = new Form($this->System->FormManager); 119 119 $Form->SetClass('ImportBankFile'); … … 125 125 $DbResult = $this->Database->select('FinanceBankAccount', '*', 'Id='.$Form->Values['BankAccount']); 126 126 $BankAccount = $DbResult->fetch_assoc(); 127 127 128 128 $DbResult = $this->Database->select('FinanceBank', '*', 'Id='.$BankAccount['Bank']); 129 129 $Bank = $DbResult->fetch_assoc(); 130 130 $Output .= 'Účet: '.$BankAccount['Number'].'/'.$Bank['Code'].' ('.$Bank['Name'].')'; 131 131 132 132 if($Bank['Code'] == '2010') $Import = new ImportFio($this->System); 133 133 else if($Bank['Code'] == '0300') $Import = new ImportPS($this->System); … … 135 135 $Import->BankAccount = $BankAccount; 136 136 $Output .= $Import->ImportFile($File->GetContent(), $File->GetExt()); 137 137 138 138 return($Output); 139 139 } 140 140 141 141 function InsertMoney($Subject, $Value, $Direction, $Cash, $Taxable, $Time, $Text, $DocumentLine) 142 142 { 143 $Year = date('Y', $Time); 143 $Year = date('Y', $Time); 144 144 $BillCode = $this->System->Modules['Finance']->GetNextDocumentLineNumber($DocumentLine, $Year); 145 $this->Database->insert('FinanceOperation', array('Text' => $Text, 146 147 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)); 148 148 } 149 149 … … 152 152 $Finance = $this->System->Modules['Finance']; 153 153 $Output = ''; 154 154 155 155 for($I = $_POST['ItemCount'] - 1; $I >= 0 ; $I--) 156 156 { 157 157 // TODO: Use links to database records instead of contants 158 158 if($_POST['Money'.$I] < 0) { 159 160 159 $DocumentLine = 4; 160 $Direction = -1; 161 161 } else { 162 163 162 $DocumentLine = 3; 163 $Direction = 1; 164 164 } 165 165 $Date = explode('-', $_POST['Date'.$I]); 166 166 $Date = mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]); 167 167 $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 59 59 60 60 $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'); 62 62 } 63 63 … … 79 79 'presetBankAccount' => $Item['BankAccount'], 80 80 'presetDocumentLine' => $DocumentLine); 81 81 return($Preset); 82 82 } 83 83 } … … 85 85 class ScheduleBankImport extends SchedulerTask 86 86 { 87 88 89 90 $DbResult = $this->Database->select('FinanceBankAccount', 'Id, Comment', 91 92 93 { 94 95 96 97 98 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 } 100 100 } -
trunk/Modules/FinanceBankAPI/Fio.php
r550 r738 1 1 <?php 2 2 3 3 include('GPC.php'); 4 5 class Fio 4 5 class Fio 6 6 { 7 7 var $UserName; 8 8 var $Password; 9 var $Account; 10 9 var $Account; 10 11 11 function Import($TimeFrom, $TimeTo) 12 12 { … … 14 14 if($this->Password == '') throw new Exception('Missing value for Password property.'); 15 15 if(!is_numeric($this->Account)) throw new Exception('Missing or not numeric value for Account property.'); 16 16 17 17 $fp = fsockopen('ssl://www.fio.cz', 443, $errno, $errstr, 30); 18 if(!$fp) 18 if(!$fp) 19 19 { 20 20 throw new Exception('Connection error: '.$errstr); 21 } else 21 } else 22 22 { 23 23 // Send request … … 32 32 $Request .= "Connection: Close\r\n\r\n"; 33 33 fwrite($fp, $Request); 34 34 35 35 // Read response 36 36 $Response = array(); 37 while(!feof($fp)) 37 while(!feof($fp)) 38 38 { 39 39 $Response[] = trim(fgets($fp, 1024)); 40 40 } 41 41 fclose($fp); 42 42 43 43 // Strip HTTP header 44 44 while($Response[0] != '') array_shift($Response); 45 45 array_shift($Response); // Remove empty line 46 46 //echo(implode("\n", $Response)); 47 47 48 48 // Parse all GPC lines 49 49 $GPC = new GPC(); … … 58 58 } 59 59 } 60 60 61 61 function NoValidDataError($Response) 62 62 { 63 63 // Try to get error message 64 64 // If something go wrong fio show HTML login page and display error message 65 65 $Response = implode('', $Response); 66 66 $ErrorMessageStart = '<div id="oldform_warning">'; 67 if(strpos($Response, $ErrorMessageStart) !== false) 68 69 70 71 72 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); 73 73 } 74 74 } -
trunk/Modules/FinanceBankAPI/FioAPI.php
r628 r738 1 1 <?php 2 2 3 3 // Specifikace API: http://www.fio.cz/docs/cz/API_Bankovnictvi.pdf 4 4 … … 10 10 else return($Text); 11 11 } 12 12 13 13 class FioAPI 14 14 { … … 16 16 var $Encoding; 17 17 var $Format; 18 18 19 19 function __construct() 20 20 { … … 22 22 $this->Format = 'csv'; 23 23 } 24 24 25 25 function Import($TimeFrom, $TimeTo) 26 26 { 27 27 if($this->Token == '') throw new Exception('Missing value for Token property.'); 28 28 29 29 // URL format: https://www.fio.cz/ib_api/rest/periods/{token}/{datum od}/{datum do}/transactions.{format} 30 30 // Send request … … 32 32 date('Y-m-d', $TimeFrom).'/'.date('Y-m-d', $TimeTo).'/transactions.'.$this->Format; 33 33 $Response = file_get_contents('https://www.fio.cz'.$RequestURL); 34 if($Response == FALSE) 34 if($Response == FALSE) 35 35 { 36 36 throw new Exception('Connection error'); 37 } else 37 } else 38 38 { 39 39 if($this->Format == 'gpc') $Response = iconv('windows-1250', $this->Encoding, $Response); 40 40 $Response = explode("\n", $Response); 41 41 42 42 if($this->Format == 'gpc') 43 { 43 { 44 44 // Parse all GPC lines 45 45 $GPC = new GPC(); … … 57 57 'Items' => array(), 58 58 ); 59 59 60 60 // CVS header 61 while($Response[0] != '') 61 while($Response[0] != '') 62 62 { 63 63 $Line = explode(';', $Response[0]); 64 64 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]; 66 66 else if($Line[0] == 'currency') $Result['Currency'] = $Line[0]; 67 67 else if($Line[0] == 'iban') $Result['IBAN'] = $Line[0]; … … 72 72 else if($Line[0] == 'dateEnd') $Result['DateEnd'] = $Line[0]; 73 73 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]; 75 75 array_shift($Response); 76 76 } … … 86 86 $Date = explode('.', $Line[1]); 87 87 $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], 89 89 'OffsetAccount' => $Line[4], 'OffsetAccountName' => $Line[5], 'BankCode' => $Line[6], 'BankName' => RemoveComma($Line[7]), 90 90 '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]), 93 93 'BIC' => $Line[17], 'OrderID' => $Line[18]); 94 $Result['Items'][] = $NewRecord; 94 $Result['Items'][] = $NewRecord; 95 95 } 96 96 } … … 98 98 } 99 99 } 100 100 101 101 function NoValidDataError($Response) 102 102 { … … 105 105 $Response = implode('', $Response); 106 106 $ErrorMessageStart = '<div id="oldform_warning">'; 107 if(strpos($Response, $ErrorMessageStart) !== false) 107 if(strpos($Response, $ErrorMessageStart) !== false) 108 108 { 109 109 $Response = substr($Response, strpos($Response, $ErrorMessageStart) + strlen($ErrorMessageStart)); -
trunk/Modules/FinanceBankAPI/FioDemo.php
r548 r738 1 1 <?php 2 2 3 3 include('FioAPI.php'); 4 4 5 5 $Fio = new FioAPI(); 6 6 $Fio->Token = ''; … … 14 14 { 15 15 echo('<tr>'); 16 if($Record['Type'] == GPC_TYPE_REPORT) 16 if($Record['Type'] == GPC_TYPE_REPORT) 17 17 { 18 18 echo('<td>Jméno účtu: '.$Record['AccountName'].'</td>'); … … 22 22 echo('<td>Suma příjmů: '.$Record['CreditValue'].' Kč</td>'); 23 23 echo('<td>Suma výdajů: '.$Record['DebitValue'].' Kč</td>'); 24 24 25 25 echo('</tr></table><br><br>'); 26 26 echo('<table border="1"><tr>'); 27 27 28 28 echo('<th>Datum</th>'); 29 29 echo('<th>Částka</th>'); … … 35 35 echo('<th>Uživatelská identifikace</th>'); 36 36 } else 37 if($Record['Type'] == GPC_TYPE_ITEM) 37 if($Record['Type'] == GPC_TYPE_ITEM) 38 38 { 39 39 echo('<td>'.date('j.n.Y', $Record['DueDate']).'</td>'); … … 45 45 echo('<td>'.$Record['SpecificSymbol'].'</td>'); 46 46 echo('<td>'.$Record['ClientName'].'</td>'); 47 } 47 } 48 48 echo('</tr>'); 49 49 50 50 } 51 51 echo('</table>'); -
trunk/Modules/FinanceBankAPI/GPC.php
r550 r738 1 1 <?php 2 2 3 3 define('GPC_TYPE_REPORT', '074'); 4 4 define('GPC_TYPE_ITEM', '075'); 5 5 6 6 class GPC 7 7 { 8 8 function ParseLine($Line) 9 { 9 { 10 10 $Line = ' '.$Line; 11 $Type = mb_substr($Line, 1, 3); 12 11 $Type = mb_substr($Line, 1, 3); 12 13 13 if($Type == GPC_TYPE_REPORT) 14 14 { … … 19 19 'AccountName' => trim(mb_substr($Line, 20, 20)), 20 20 '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, 22 22 '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, 25 25 'SequenceNumber' => intval(mb_substr($Line, 106, 3)), 26 26 'Date' => mktime(0, 0, 0, mb_substr($Line, 111, 2), mb_substr($Line, 109, 2), '20'.mb_substr($Line, 113, 2)), … … 28 28 'CheckSum' => sha1(md5($Line).$Line), 29 29 ); 30 } else 30 } else 31 31 if($Type == GPC_TYPE_ITEM) 32 { 32 { 33 33 $GPCLine = array 34 34 ( 35 35 'Type' => GPC_TYPE_ITEM, 36 36 '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), 39 39 'Value' => mb_substr($Line, 49, 12) / 100, 40 'Code' => mb_substr($Line, 61, 1), 40 'Code' => mb_substr($Line, 61, 1), 41 41 'VariableSymbol' => intval(mb_substr($Line, 62, 10)), 42 42 'BankCode' => mb_substr($Line, 74, 4), 43 43 'ConstantSymbol' => intval(mb_substr($Line, 78, 4)), 44 'SpecificSymbol' => intval(mb_substr($Line, 82, 10)), 44 'SpecificSymbol' => intval(mb_substr($Line, 82, 10)), 45 45 'Valut' => mb_substr($Line, 92, 6), 46 'ClientName' => mb_substr($Line, 98, 20), 46 'ClientName' => mb_substr($Line, 98, 20), 47 47 //'Zero' => substr($Line, 118, 1), 48 48 'CurrencyCode' => mb_substr($Line, 119, 4), … … 50 50 'CheckSum' => sha1(md5($Line).$Line), 51 51 ); 52 } else 52 } else 53 53 $GPCLine = NULL; 54 54 55 55 return($GPCLine); 56 56 } -
trunk/Modules/FinanceBankAPI/ImportFio.php
r715 r738 1 1 <?php 2 2 3 3 include('FioAPI.php'); 4 4 5 5 class ImportFio extends BankImport 6 6 { 7 7 function Import() 8 { 8 { 9 9 $Fio = new FioAPI(); 10 10 $Fio->Token = $this->BankAccount['LoginName']; … … 20 20 //$Output .= '<td>Ke dni '.date('j.n.Y', $Records['DateEnd']).' je stav účtu '.$Records['ClosingBalance'].' Kč</td>'; 21 21 //$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>'; 23 23 //$Output .= '</tr>'; 24 24 foreach($Records['Items'] as $Record) … … 26 26 $DbResult = $this->Database->select('FinanceBankImport', 'ID', 'Identification='.$Record['ID']); 27 27 if($DbResult->num_rows == 0) 28 { 28 { 29 29 $Output .= '<tr>'; 30 30 $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'], 32 32 'SpecificSymbol' => $Record['SpecificSymbol'], 'VariableSymbol' => $Record['VariableSymbol'], 33 33 'ConstantSymbol' => $Record['ConstantSymbol'], 'Currency' => $this->BankAccount['Currency'], … … 41 41 } 42 42 } 43 $Output .= '</table>'; 44 $this->Database->update('FinanceBankAccount', 'Id='.$this->BankAccount['Id'], 45 43 $Output .= '</table>'; 44 $this->Database->update('FinanceBankAccount', 'Id='.$this->BankAccount['Id'], 45 array('LastImportDate' => TimeToMysqlDate($PeriodEnd))); 46 46 return($Output); 47 47 } -
trunk/Modules/FinanceBankAPI/ImportPS.php
r550 r738 14 14 $Data = explode("\n", $Content); 15 15 } 16 16 17 17 function ImportCVS($Content) 18 18 { 19 19 $Finance = &$this->System->Modules['Finance']; 20 20 21 21 $Data = explode("\n", $Content); 22 22 foreach($Data as $Key => $Value) … … 45 45 11 => '', 46 46 ); 47 47 48 48 if($Header != $Data[0]) $Output = 'Nekompatibilní struktura CSV'; 49 49 else … … 79 79 $Style = ''; 80 80 } 81 81 82 82 if($Money < 0) $Text = 'Platba převodem'; 83 83 else $Text = 'Přijatá platba'; … … 102 102 $Output .= '<input type="submit" value="Zpracovat"/></form>'; 103 103 } 104 } 104 } 105 105 }
Note:
See TracChangeset
for help on using the changeset viewer.