📜 ⬆️ ⬇️

Bank statements and PHP is just

1C Accounting provides for a format for exchanging text information with Banking systems. It is in this format that banks provide statements of accounts of organizations.

Not so long ago, on one of the projects, there was a need to “parse” such statements and process them. Then, after searching the Internet, I discovered that: either I’m looking badly or people don’t want to share ready-made solutions.

Having visited the site 1C, I saw that the format is quite simple and understandable even to a programmer without much experience.

The statement file is a simple text document.
')
Sample file
1CClientBankExchange =1.02 =Windows =C  - = ,  4.4 =27.11.2013 =27.11.2014 =12345678901234567890  =27.11.2013 =27.11.2014 =12345678901234567890 =0 =68770 =68770 =0  =  =1 =26.12.2013 =1000 =26.12.2013 = =12345678901234567890 = 778899001122    () = 778899001122 1=   () =12345678901234567890 1=  "" 2=.  =044525201 =98765432198765432100 =98765432198765432100 =89765456787654345678 = 7765434566\775001001   "" =7765434566 1=  "" =89765456787654345678 1=  "" 2=.  =044525201 =98765432198765432100 =17 =        ""  =  =176 =26.12.2013 =4770 = = 7725747515\772501001  "" =7725747515 =40702810822000034869 1= "" =40702810822000034869 1= " " () 2=.  =044525976 =30101810500000000976 =26.12.2013 = 778899001122     =778899001122 =12345678901234567890 1=    =12345678901234567890 1=  "" 2=.  =044525201 =98765432198765432100 =01 =26.12.2013 =5 =   â„–1  26  (     )   .  =  =1 =10.01.2014 =20 =10.01.2014 = =12345678901234567890 = 778899001122    () =778899001122 1=   () =12345678901234567890 1=  "" 2=.  =044525201 =98765432198765432100 =70601810200021210220 = 7765434566\775001001   "" =7765434566 1=  "" =70601810200021210220 1=  "" 2=.  =044525201 =98765432198765432100 =17 =        =  =1 =10.01.2014 =3500 =10.01.2014 = 778899001122\772801001    () =778899001122 =12345678901234567890 1=   () =12345678901234567890 1=  "" 2=.  =044525201 =98765432198765432100 = = 7707704692\772801001  "   " =7707704692 =40702810000760001497 1= "   " =40702810000760001497 1= " " 2=.  =044525219 =30101810500000000219 =01 =01 =772801001 =772801001 =00000000000000000000 =0 =0 =0 =0 =10.01.2014 =0 =10.01.2014 =5 = 3500.00,  ..  - 388,99   


It means that if the document is a text one, then “parse” it easily. The data in the file are presented in the format:

  =  

Therefore, it is enough to accept the file as an array of strings and begin to loop through this array. For convenience, the parser returns an array of objects.

Document.php - Model document with destination rules
 <?php namespace bank; class Document { public $doctype; public $inbankid; public $docdate; public $summ; public $outdate; public $indate; public $payeraccount; public $payerinfo; public $payerinn; public $payer; public $payerdealaccount; public $payerbank1; public $payerbank2; public $payerbik; public $payerfixaccount; public $recieveraccount; public $recieverinfo; public $recieverinn; public $reciever1; public $recieverdealaccount; public $recieverbank1; public $recieverbank2; public $recieverbik; public $recieverfixaccount; public $paytype; public $paydirection; public $makerstatus; public $payerkpp; public $recieverkpp; public $showerkbk; public $okato; public $showerfundament; public $showerperiod; public $showernumber; public $showerdate; public $showertype; public $paymentperiod; public $quenue; public function __construct() { } public function rules($rule) { $rules = [ '' => 'doctype', '' => 'inbankid', '' => 'docdate', '' => 'summ', '' => 'outdate', '' => 'indate', '' => 'payeraccount', '' => 'payerinfo', '' => 'payerinn', '1' => 'payer', '' => 'payerdealaccount', '1' => 'payerbank1', '2' => 'payerbank2', '' => 'payerbik', '' => 'payerfixaccount', '' => 'recieveraccount', '' => 'recieverinfo', '' => 'recieverinn', '1' => 'reciever1', '' => 'recieverdealaccount', '1' => 'recieverbank1', '2' => 'recieverbank2', '' => 'recieverbik', '' => 'recieverfixaccount', '' => 'paytype', '' => 'paydirection', '' => 'makerstatus', '' => 'payerkpp', '' => 'recieverkpp', '' => 'showerkbk', '' => 'okato', '' => 'showerfundament', '' => 'showerperiod', '' => 'showernumber', '' => 'showerdate', '' => 'showertype', '' => 'paymentperiod', '' => 'quenue', ]; return $rules[$rule]; } public function set($section, $param) { $rulled = $this->rules($section); $this->$rulled = $param; } } ?> 


Bank.php - Document parser class
 <?php include 'Document.php'; //   class Bank { protected $documents; //    ""  //       function __construct($fileaddr) { $maas = file($fileaddr); //      $documents = []; //       $docid = 0; //   ID   0 foreach ($maas as $key => $value) { //    $value2 = rtrim($value); //        $value2 = mb_convert_encoding($value2, "utf-8", "windows-1251"); //    utf-8      windows-1251 $result = explode('=', $value2); //     =>  if (count($result) == 2) { //     if ($result[0] == '') { //        $workflow = new Document(); //   } if (isset($workflow)) { //    $workflow->set($result[0], $result[1]); //  ,  } } else { //    if ($result[0] == '') { //      $documents[$docid] = $workflow; //      $docid++; //  } } } $this->documents = $documents; //       } function getDocs() { return $this->documents; //     } } ?> 


Verification code
 <?php include 'Bank.php'; //  $file = 'export_to_1c.txt'; //    $bank = new Bank($file); //   $docs = $bank->getDocs(); //    var_dump($docs);//    ?> 


It is not recommended to use this “parser” on any large projects, since the class does not take into account many nuances of the exchange format.
There is also no error handling. This class was created only for informational purposes and does not pretend to be a genius, but from my point of view, as a starting point, it is quite fit for yourself.

Thank you for your attention and thanks in advance for your comments.

Source: https://habr.com/ru/post/245397/


All Articles