Current Path : /data/web/virtuals/51568/virtual/www/subdom/srps/src/models/ |
Current File : /data/web/virtuals/51568/virtual/www/subdom/srps/src/models/ParseXML.php |
<?php namespace app\models; use app\Application; class ParseXML { private $idakce = 0; private static $kredit = ['DBIT' => '0', 'CRDT' => '1']; private static $IBAN = 'CZ9720100000002300747551'; private static $bic = [ 'KOMBCZPP' => '100', 'CEKOCZPP' => '300', 'AGBACZPP' => '600', 'CNBACZPP' => '710', 'GIBACZP' => '800', 'FIOBCZPP' => '2010', 'CITFCZPP' => '2060', 'MPUBCZPP' => '2070', 'ARTTCZPP' => '2220', 'CTASCZ22' => '2250', 'CITICZP' => '2600', 'BACXCZPP' => '2700', 'AIRACZPP' => '3030', 'BPPFCZP1' => '3050', 'BPKOCZPP' => '3060', 'INGBCZPP' => '3500', 'EXPNCZPP' => '4000', 'NROZCZPP' => '4300', 'RZBCCZPP' => '5500', 'JTBPCZPP' => '5800', 'PMBPCZPP' => '6000', 'COBACZPX' => '6200', 'BREXCZPP' => '6210', 'GEBACZPP' => '6300', 'SUBACZPP' => '6700', 'VBOECZ2' => '6800', 'DEUTCZP' => '7910', 'GENOCZ21' => '8030', 'OBKLCZ2' => '8040', 'CZEECZPP' => '8090', 'MIDLCZPP' => '8150', 'FFCSCZP1' => '8198', 'PAERCZP1' => '8220', 'BKCHCZPP' => '8250', 'COMMCZPP' => '8255', 'ICBKCZPP' => '8265', 'FAPOCZP1' => '8270', 'BEFKCZP1' => '8280', 'BEORCZP2' => '8299', ]; public function vd($x) { echo "<pre>"; var_dump($x); echo "</pre>"; } public function __construct (int $idakce) { $this->idakce = $idakce; } private function vypisZalozit(string $vypis, $fromDate, $toDate) { $id = Application::$app->db->fetchOne('SELECT `id` FROM `sr_vypisy` WHERE `vypis` = :vypis', $vypis)->id; if ($id) { return false; } // Application::$app->db->query('INSERT INTO `sr_vypisy`(`idakce`, `vypis`) VALUES (:idakce, :vypis)', $this->idakce, $vypis); Application::$app->db->query('INSERT INTO `sr_vypisy`(`idakce`, `vypis`, `fromDate`, `toDate`) VALUES (:idakce, :vypis, :fromDate, :toDate)', $this->idakce, $vypis, $fromDate, $toDate); return Application::$app->db->lastInsertId(); } public function parseXML($xml) { error_reporting(E_ERROR | E_PARSE); try { //$xml = new \SimpleXMLElement($this->XMLFile, 0, true); $xml_vypis_id = $xml->BkToCstmrStmt->GrpHdr->MsgId; [$xmlns, $year, $month, $day, $number] = explode('-', $xml_vypis_id); if ($xmlns !== 'camt.053') { return [false, 'Výpis nemá požadovaný formát (camt.053)']; } $IBAN = $xml->BkToCstmrStmt->Stmt->Acct->Id; $IBAN = json_decode(json_encode($IBAN), 1); if (self::$IBAN !== $IBAN['IBAN']) { return [false, 'Toto není bankovní výpis SR při ZŠ náměstí Republiky (' . $IBAN['IBAN'] . ')']; } $fromDate = date('Y-m-d', \strtotime($xml->BkToCstmrStmt->Stmt->FrToDt->FrDtTm)); $toDate = date('Y-m-d', \strtotime($xml->BkToCstmrStmt->Stmt->FrToDt->ToDtTm)); $idvypis = $this->vypisZalozit($xml_vypis_id, $fromDate, $toDate); if ($idvypis === false){ return [false, "Tento výpis už byl použit."]; } // $pohyby = []; $cislo_vypisu = $year.'-'.$month.'-'.$day.'-'.$number; $idSkolniRok = Application::$app->db->fetchone("SELECT `id` FROM `jak_roky` WHERE `aktualni` = '1'")->id; foreach($xml->BkToCstmrStmt->Stmt->Ntry as $ntry){ $ntry = json_decode(json_encode($ntry), 1); //z objektu udela retezce // $this->vd($ntry) ; $vs = $ntry['NtryDtls']['TxDtls']['Refs']['EndToEndId']; if (\strpos($vs, '/') !== false) { [$vs, $ss] = explode('/', $vs); } elseif (\strpos($vs, ':') !== false) { $posSS = \strpos($vs, 'SS'); $vs = substr($vs, 1, $posSS-1); } else { $ss = $ntry['NtryDtls']['TxDtls']['Refs']['PmtInfId']; } $vs = ($vs) ? substr($vs, 2) : ""; $ss = ($ss) ? substr($ss, 2) : ""; $ucet = $ntry['NtryDtls']['TxDtls']['RltdPties']; $banka = $ntry['NtryDtls']['TxDtls']['RltdAgts']; $banka_bic = $banka['DbtrAgt']['FinInstnId']['BIC']; if ($banka_bic){ $banka_bic = rtrim($banka_bic, 'X'); $banka_platce = self::$bic[$banka_bic]; } else { $banka_platce = $banka['DbtrAgt']['FinInstnId']['Othr']['Id']; } $banka_bic = $banka['CdtrAgt']['FinInstnId']['BIC']; if ($banka_bic){ $banka_bic = rtrim($banka_bic, 'X'); $banka_prijemce = self::$bic[$banka_bic]; } else { $banka_prijemce = $banka['CdtrAgt']['FinInstnId']['Othr']['Id']; } if ($ntry['CdtDbtInd'] == 'CRDT'){ $pohyby = []; $pohyby = [ // 'ref' => $ntry['NtryDtls']['TxDtls']['Refs']['AcctSvcrRef'], 'ref' => $ntry['NtryDtls']['TxDtls']['Refs']['MsgId'], 'castka' => $ntry['Amt'], 'kredit'=> self::$kredit[$ntry['CdtDbtInd']], 'dattranskace' => $ntry['ValDt']['Dt'], 'SS' => $ss, 'VS' => $vs, 'ucet-platce' => ltrim($ucet['DbtrAcct']['Id']['Othr']['Id'], '0'), 'banka-platce' => $banka_platce, // 'jmeno-platce' => $ucet['DbtrAcct']['Nm'], 'jmeno-platce' => $ntry['NtryDtls']['TxDtls']['AddtlTxInf'], 'ucet-prijemce' => ltrim($ucet['CdtrAcct']['Id']['Othr']['Id'], '0'), 'banka-prijemce' => $banka_prijemce, 'jmeno-prijemce' => $ucet['CdtrAcct']['Nm'], 'zprava' => $ntry['NtryDtls']['TxDtls']['RmtInf']['Ustrd'], ]; $jeReference = Application::$app->db->fetchone("SELECT `id` FROM `sr_vypisy_polozky` WHERE `ref` = :ref", $pohyby['ref']); if (!$jeReference) { if ($vs) // to je quli zakum bez rc { $hledamZaka = Application::$app->db->fetchone("SELECT `id` FROM `zaci` WHERE `idskolnirok`= :idSkolniRok and `rodne_cislo`= :rodneCislo", $idSkolniRok, $vs); } else { $hledamZaka = false; } // $idZaka = ($hledamZaka) ? $hledamZaka->id : 0; Application::$app->db->query('INSERT INTO `sr_vypisy_polozky` (`idvypis`, `ref`, `castka`, `dattransakce`, `SS`, `VS`, `ucetplatce`, `bankaplatce`, `jmenoplatce`, `zprava`) VALUES (:idvypis, :ref, :castka, :dattransakce, :SS, :VS, :ucetplatce, :bankaplatce, :jmenoplatce, :zprav)', $idvypis, $pohyby['ref'], $pohyby['castka'], $pohyby['dattranskace'], $pohyby['SS'], $pohyby['VS'], $pohyby['ucet-platce'], $pohyby['banka-platce'], $pohyby['jmeno-platce'], $pohyby['zprava']); $lastID = Application::$app->db->lastInsertId(); if ($hledamZaka) { Application::$app->db->query("INSERT INTO `sr_zaci_pohyby`(`idzak`, `idakce`, `pohybtyp`, `idvypisypolozky`, `castka`) VALUES (:idzak, :idakce, :pohybtyp, :idvypisypolozky, :castka)", $hledamZaka->id, $this->idakce, 'b', $lastID, $pohyby['castka']); } } } } return [true, $idvypis]; } catch (Exception $e) { return [false, $e]; } } }