<?php //session_name('iAdmin'); session_start();
include('../lib/PHPExcel.php');
include("../lib/ado/adodb.inc.php");
include("../lib/generic.inc.php");
include("../lib/visual.inc.php");
include("ishop_all.inc.php");
include("ishop.inc.php");

define('MY_SESSION_NAME', 'iAdmin');
implement_session();

/*
detailLog(__LINE__);
detailLogFlash();
*/

$db = &db_open();

$fu=(int)@$_REQUEST['FUNCTION'];

$x2=new cX2($db);

if ( !$x2->validate_session() ) { // err
	$fu=0;
   $x2->lpath="Unknown";
}
else { // ok
	$x2->read_agent_main();
}

// $x2 has been defined and initialized, so we can now include ishop_tax.inc.php
include('ishop_tax.inc.php');

switch ($fu)
{
case 1:
   do_export_log($forPOS=false);
   break;
case 2:
   do_export_log($forPOS=true);
   break;
case 3:
   do_export_info(0);  // taxspooler version
   break;
case 4:
   do_export_info(1);  // O/S version
   break;
case 5:
   my_record_info(); // mydata record info
   break;
case 6:
   get_iform_docid();
   break;
case 7:
   do_export_mydata_recs();
   break;
case 8:
   my_record_orgin();
   break;
case 9:
   cre_mycancel_by_xflag();
   break;
case 10:
   z_record_info();
   break;
case 20:
   do_export_STXT_log();
   break;
case 21:
   do_export_aeskey();
   break;
case 29:
   do_export_compar();
   break;
case 30:
   queryPnameForPgroup();
   break;
}

$db->Close();
//--------------------------------------
// Functions
//--------------------------------------
function queryPnameForPgroup()
{
   global $x2;
 	$db=&$x2->db;

   ob_start();

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Content-Type: application/json");

   $oret = (object)array('error'=>false, 'data'=>array() );

   $query = "SELECT DISTINCT PNAME FROM taxi_wmic WHERE AGENTID=? AND PGROUP=? AND PNAME!='Description' ORDER BY PNAME";
   $data = array($x2->agentid, @$_REQUEST['PGROUP']);
   if (($rs=$db->Execute($query, $data))===false) {
      $oret->error = $db->ErrorMsg();
   }
   while ($row=$rs->FetchRow()) {
      $oret->data[] = $row[0];
   }
   $rs->Close();

   ob_end_clean();
   echo json_encode($oret);
}
function do_export_info($LiType)
{
   global $x2;
 	$db=&$x2->db;
   $verbose=false;

   $x2->download=true;

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   if (!empty($_REQUEST['v'])) {
      $verbose=true;
   }
   else {
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=taxcenterinfo.xlsx");
   }

   $titles_ui=array(
      'RECTYPE' => 'Record Type',
      'TXVERSION' => 'TaxSpooler version',
      'TXLOCATION' => 'Location',
      'L_PROCID' => 'Processor ID',
      'L_NPROC' => 'Number of processors',
      'L_OS' => 'O/S',
      'L_ARCH' => 'System architecture',
      'F1' => 'Last update'
   );

   $started=false;
   $cells=array();
   $row=2;
   $cmt=new cMACHINETYPE();
   $objPHPExcel = new PHPExcel();
   if ($LiType==0) {
      $fields =  "T1.RECTYPE, T1.TXVERSION, T2.TXLOCATION";
   }
   else {
      $fields =  "T1.RECTYPE, T2.TXLOCATION, T2.L_OS, T2.L_ARCH, T2.L_PROCID, T2.L_NPROC";
   }

   $query="SELECT MAX(T1.LASTUPDATE) F1, $fields FROM taxi_log T1
      LEFT JOIN taxi_eafdss T2 ON (T2.MS11=T1.MS11)
      INNER JOIN (
	      SELECT MS11, MAX(LASTUPDATE) LUPD FROM taxi_log WHERE AGENTID=? AND RECTYPE<>3
		   GROUP BY MS11
      ) J1
      ON T1.MS11=J1.MS11 AND T1.LASTUPDATE>=J1.LUPD
      WHERE T1.AGENTID=? AND T2.ENABLED<>2
      GROUP BY $fields";
   $data=array($x2->agentid, $x2->agentid);
   detailLog(__LINE__);
   if (($rs=$db->Execute($query,$data))===false) {
      $c1->error = __LINE__.$db->ErrorMsg();
      return;
   }
   detailLog(__LINE__);
   detailLogFlash();
   while ($o1=$rs->FetchNextObject()) {
      $o1->RECTYPE = $o1->RECTYPE==1?'Online':'Disk';

      $ar1 = (array)$o1;
      $ar2 = $db->verify_8( $ar1 );

      if (!$started) {
         $c=ord('A');
         $i=0;
         foreach ($ar2 as $k=>$v) {
            $k2 = (array_key_exists($k, $titles_ui)?$titles_ui[$k]:$k);

            if ($i<26) {
               $c1 = chr($c+$i);
            }
            else {
               $i1 = $i/26;
               $i2 = $i%26;
               $c1 = chr($c+$i1-1).chr($c+$i2);
            }
            $cells[]=$c1;
            $c1 .= '1';
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($c1, $k2);

            $i++;
         }

         $osheet=$objPHPExcel->getActiveSheet();

         $started=true;
      }

      $i=0;
      foreach ($ar2 as $v) {
         $osheet->getCell( $cells[$i++].$row )->setValueExplicit((string)$v, PHPExcel_Cell_DataType::TYPE_STRING2);
      }
      $row++;
   }
   $rs->Close();

   // Save Excel 2007 file
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   $objWriter->save('php://output');
}
//--------------------------------------
function do_export_aeskey()
{
   global $x2;
   $db=&$x2->db;

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Content-Type: application/octet-stream");
   header("Content-Disposition: attachment; filename=taxcenter.xlsx");

   $started=false;
   $cells=array();
   $row=2;
   $objPHPExcel = new PHPExcel();
   //$compar = $forPOS ? "=3" : "<>3";
   $query = 'SELECT MS11, AESKEYHEX, TXLOCATION, SAPENUMCODE, TAXISBRANCHID, MYSTREET, MYNUMBER, MYZIP, MYCITY FROM taxi_eafdss';
   if (($rs=$db->Execute($query))===false) {
      echo __LINE__.' '.$db->ErrorMsg();
      return;
   }
   while ($o1=$rs->FetchNextObject()) {
      $ar1 = (array)$o1;
      $ar2 = $db->verify_8( $ar1 );

      if (!$started) {
         $c=ord('A');
         $i=0;
         foreach ($ar2 as $k=>$v) {
            if ($i<26) {
               $c1 = chr($c+$i);
            }
            else {
               $i1 = $i/26;
               $i2 = $i%26;
               $c1 = chr($c+$i1-1).chr($c+$i2);
            }
            $cells[]=$c1;
            $c1 .= '1';
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($c1, $k);

            $i++;
         }

         $osheet=$objPHPExcel->getActiveSheet();

         $started=true;
      }

      $i=0;
      foreach ($ar2 as $v) {
         $osheet->getCell( $cells[$i++].$row )->setValueExplicit((string)$v, PHPExcel_Cell_DataType::TYPE_STRING2);
      }
      $row++;
   }
   $rs->Close();

   // Save Excel 2007 file
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   $objWriter->save('php://output');
}
//--------------------------------------
function do_export_log($forPOS)
{
   global $x2;
 	$db=&$x2->db;
   $verbose=false;

   $x2->download=true;

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   if (!empty($_REQUEST['v'])) {
      $verbose=true;
   }
   else {
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=taxcenter.xlsx");
   }

   $titles_ui=array(
      'RECTYPE' => 'Record Type',
      'TXVERSION' => 'TaxSpooler version',
      'SENDERIP' => 'Sender IP',
      'EAFDSSPORT' => 'Θύρα ΕΑΦΔΣΣ',
      'MS11' => 'Serial',
      'MVENDOR' => 'Vendor',
      'MMODEL' => 'Model',
      'MVERSION' => 'Version',
      'CLOGIC' => 'Client program version',
      'OPENTIME' => 'Time opened',
      'FIRSTSIGNINZ' => 'First Sign in Z',
      'LASTZTAKEN' => 'Last Z taken',
      'LASTZNR' => 'Last Z no.',
      'MPROGRESSIVE' => 'Progressive no.',
      'MDAILY' => 'Daily no',
      'MTIMEPASSED' => 'Time since last communication, in seconds',
      'MLASTOK' => 'Last  O.K.',
      'MLASTFAIL' => 'Last failure',
      'MLASTERROR' => 'Last error code',
      'MLASTHARDST' => 'Last Device Hardware Status',
      'MLASTAPPST' => 'Last Device Application Status',
      'TXLOCATION' => 'Location',
      'L_PROCID' => 'Processor ID',
      'L_NPROC' => 'Number of processors',
      'L_OS' => 'O/S',
      'L_ARCH' => 'System architecture',
      'AESKEYHEX' => 'Taxis Key',
      'ALLOWSENDTAXIS' => 'Send to Taxis'
   );

   $started=false;
   $cells=array();
   $row=2;
   $cmt=new cMACHINETYPE();
   $objPHPExcel = new PHPExcel();
   $compar = $forPOS ? "=3" : "<>3";
   $query="SELECT T1.*, T2.ENABLED, T2.MACHINETYPE, T2.TXLOCATION, T2.AESKEYHEX, T2.ALLOWSENDTAXIS, T2.L_PROCID, T2.L_NPROC, T2.L_OS, T2.L_ARCH
      FROM taxi_log T1
      LEFT JOIN taxi_eafdss T2 ON (T2.MS11=T1.MS11)
      INNER JOIN (
	      SELECT MS11, MAX(LASTUPDATE) LUPD FROM taxi_log WHERE AGENTID=? AND RECTYPE $compar
		   GROUP BY MS11
      ) J1
      ON T1.MS11=J1.MS11 AND T1.LASTUPDATE>=J1.LUPD
      WHERE T1.AGENTID=?
      ORDER BY T1.MS11";
   $data=array($x2->agentid, $x2->agentid);
   detailLog(__LINE__);
   if (($rs=$db->Execute($query,$data))===false) {
      $c1->error = __LINE__.$db->ErrorMsg();
      return;
   }
   detailLog(__LINE__);
   detailLogFlash();
   while ($o1=$rs->FetchNextObject()) {
      $rsx=new cTXCERROR($o1);
      $o1->XSTATUS = $rsx->Get();

      $o1->RECTYPE = $o1->RECTYPE==1?'Online':'Disk';

      $cmt->reset($o1->MACHINETYPE);
      $o1->MACHINETYPE = $cmt->ToString();

      $ar1 = (array)$o1;
      $ar2 = $db->verify_8( $ar1 );

      if (!$started) {
         $c=ord('A');
         $i=0;
         foreach ($ar2 as $k=>$v) {
            $k2 = (array_key_exists($k, $titles_ui)?$titles_ui[$k]:$k);

            if ($i<26) {
               $c1 = chr($c+$i);
            }
            else {
               $i1 = $i/26;
               $i2 = $i%26;
               $c1 = chr($c+$i1-1).chr($c+$i2);
            }
            $cells[]=$c1;
            $c1 .= '1';
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($c1, $k2);

            $i++;
         }

         $osheet=$objPHPExcel->getActiveSheet();

         $started=true;
      }

      $i=0;
      foreach ($ar2 as $v) {
         $osheet->getCell( $cells[$i++].$row )->setValueExplicit((string)$v, PHPExcel_Cell_DataType::TYPE_STRING2);
      }
      $row++;
   }
   $rs->Close();

   // Save Excel 2007 file
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   $objWriter->save('php://output');
}
//--------------------------------------
function do_export_compar()
{
   global $x2;
 	$db=&$x2->db;

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   if (!empty($_REQUEST['v'])) {
      header("Content-type: text/plain; charset=UTF-8");
      $verbose=true;
   }
   else {
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=taxcenter29.xlsx");
   }

   $titles_ui=array(
      'TXLOCATION' => 'Location',
      'PNODE' => 'Node',
      'RECNR' => 'Εγγραφή',
      'PVALUE' => 'Αξία',
      'CREDATE' => 'Ενημέρωση',
      'CRETIME' => 'Ωρα'
      );

   $started=false;
   $cells=array();
   $row=2;
   $objPHPExcel = new PHPExcel();
   $pgroup=$_SESSION['PGROUP'];
   $pname=$_SESSION['PNAME'];
   $query="SELECT TXLOCATION, PNODE, RECNR, PVALUE, CREDATE, CRETIME FROM taxi_wmic
      WHERE AGENTID=? AND PGROUP=? AND PNAME=?
      ORDER BY TXLOCATION, PNODE, RECNR";
   $data=array($x2->agentid, $pgroup, $pname);
   if (($rs=$db->Execute($query,$data))===false) {
      $c1->error = __LINE__.$db->ErrorMsg();
      return;
   }
   while ($o1=$rs->FetchNextObject()) {
      $ar1 = (array)$o1;
      $ar2 = $db->verify_8( $ar1 );

      if (!$started) {
         $c=ord('A');
         $i=0;
         foreach ($ar2 as $k=>$v) {
            $k2 = (array_key_exists($k, $titles_ui)?$titles_ui[$k]:$k);

            if ($i<26) {
               $c1 = chr($c+$i);
            }
            else {
               $i1 = $i/26;
               $i2 = $i%26;
               $c1 = chr($c+$i1-1).chr($c+$i2);
            }
            $cells[]=$c1;
            $c1 .= '1';
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($c1, $k2);

            $i++;
         }

         $osheet=$objPHPExcel->getActiveSheet();

         $started=true;
      }

      $i=0;
      foreach ($ar2 as $v) {
         $osheet->getCell( $cells[$i++].$row )->setValueExplicit((string)$v, PHPExcel_Cell_DataType::TYPE_STRING2);
      }
      $row++;
   }
   $rs->Close();

   // Save Excel 2007 file
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   $objWriter->save('php://output');
}
//--------------------------------------
function do_export_STXT_log()
{
   global $x2;
 	$db=&$x2->db;
   $verbose=false;

   $x2->download=true;

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   if (!empty($_REQUEST['v'])) {
      header("Content-type: text/plain; charset=UTF-8");
      $verbose=true;
   }
   else {
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=taxcenter.xlsx");
   }

   $titles_ui=array(
      'RECTYPE' => 'Record Type',
      'TXVERSION' => 'TaxSpooler version',
      'MACHINETYPE' => 'Machine Type',
      'MS11' => 'Serial',
      'TXLOCATION' => 'Location',
      'LASTZTAKEN' => 'Last Z taken',
      'LASTZNR' => 'Last Z no.',
      'FIRSTSTXT' => 'First S.TXT ever created',
      'FIRSTNOTSENT' => 'First S.TXT no. not sent o.k. or not sent at all',
      'NOTSENTCRE' => 'Cretion datetime of S.TXT not sent',
      'NOTSENTERROR' => 'TAXIS error for not sent S.TXT',
      'LASTUPDATE' => 'Last record update'
   );

   $started=false;
   $cells=array();
   $row=2;
   $cmt=new cMACHINETYPE();
   $objPHPExcel = new PHPExcel();
   $query="SELECT T1.RECTYPE, T1.TXVERSION, T1.MS11, T1.LASTZTAKEN, T1.LASTZNR,
      T1.FIRSTSTXT, T1.FIRSTNOTSENT, T1.NOTSENTCRE, T1.NOTSENTERROR, T1.LASTUPDATE,
      T2.ENABLED, T2.MACHINETYPE, T2.TXLOCATION
      FROM taxi_log T1
      LEFT JOIN taxi_eafdss T2 ON (T2.MS11=T1.MS11)
      INNER JOIN (
	      SELECT MS11, MAX(LASTUPDATE) LUPD FROM taxi_log WHERE AGENTID=? AND RECTYPE<>3
		   GROUP BY MS11
      ) J1
      ON T1.MS11=J1.MS11 AND T1.LASTUPDATE>=J1.LUPD
      WHERE T1.AGENTID=?
      ORDER BY T1.MS11";
   $data=array($x2->agentid, $x2->agentid);
   detailLog(__LINE__);
   if (($rs=$db->Execute($query,$data))===false) {
      $c1->error = __LINE__.$db->ErrorMsg();
      return;
   }
   detailLog(__LINE__);
   detailLogFlash();
   while ($o1=$rs->FetchNextObject()) {
      //$rsx=new cTXCERROR($o1);
      //$o1->XSTATUS = $rsx->Get();

      $o1->RECTYPE = $o1->RECTYPE==1?'Online':'Disk';

      $cmt->reset($o1->MACHINETYPE);
      $o1->MACHINETYPE = $cmt->ToString();

      $ar1 = (array)$o1;
      $ar2 = $db->verify_8( $ar1 );

      if (!$started) {
         $c=ord('A');
         $i=0;
         foreach ($ar2 as $k=>$v) {
            $k2 = (array_key_exists($k, $titles_ui)?$titles_ui[$k]:$k);

            if ($i<26) {
               $c1 = chr($c+$i);
            }
            else {
               $i1 = $i/26;
               $i2 = $i%26;
               $c1 = chr($c+$i1-1).chr($c+$i2);
            }
            $cells[]=$c1;
            $c1 .= '1';
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($c1, $k2);

            $i++;
         }

         $osheet=$objPHPExcel->getActiveSheet();

         $started=true;
      }

      $i=0;
      foreach ($ar2 as $v) {
         $osheet->getCell( $cells[$i++].$row )->setValueExplicit((string)$v, PHPExcel_Cell_DataType::TYPE_STRING2);
      }
      $row++;
   }
   $rs->Close();

//die( (string)__LINE__ );
   // Save Excel 2007 file
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   $objWriter->save('php://output');
}
//--------------------------------------
function get_iform_docid()
{
   global $x2;
 	$db=&$x2->db;

   $rid = (int)$_REQUEST['RID'];
   $query = "SELECT T1.FHMKEY, T2.ETXT FROM mydata T1
      LEFT JOIN abmaiex T2 ON (T2.S11 = SUBSTRING(T1.FHMKEY, 1, 11)
         AND T2.Z4 = SUBSTRING(T1.FHMKEY, 12, 4) AND T2.DAILY= SUBSTRING(T1.FHMKEY, 16, 4))
      WHERE T1.RID=$rid";
   if (($rs=$db->Execute($query))===false) {
      echo 'ERROR '.$db->ErrorMsg();
      return;
   }
   $o1 = $rs->FetchNextObject();
   $rs->Close();
   if ($o1===false) {
      echo 'ERROR Record disappeared!';
   }
   else {
      $ar1 = explode(';', $o1->ETXT);
      if (count($ar1) >= 23) {
         $docid = sprintf('EAFDSS.%s.%08d', substr($o1->FHMKEY, 0, 11), $ar1[6]);
         echo $docid;
      }
      else {
         echo 'ERROR E.TXT not available!';
      }
   }
}
//--------------------------------------
function cre_mycancel_by_xflag()
{
   global $x2;
 	$db=&$x2->db;

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

   if (empty($_REQUEST['MYRID'])) {
      echo 'Missing variable MYRID';
      return;
   }
   $rid = (int)$_REQUEST['MYRID'];

   // Read document to be canceled
   //
   $query = 'SELECT T2.RID, T2.MARK, T1.RECTYPE, T1.AGENTID, T1.OWNOTHER, CONVERT(VARCHAR, T2.SUBMISSION, 120) AS SUBMISSION FROM mydata T1
	          LEFT JOIN MYDATA T2 ON (T2.RID=T1.XFLAG AND T2.OWNOTHER=T1.OWNOTHER AND T2.RECTYPE=T1.RECTYPE)
	          WHERE T1.RID=?';
   $data = array($rid);
   if (($rs=$db->Execute($query, $data))===false) {
      echo __LINE__.' '.$db->ErrorMsg();
      return;
   }
   $o1 = $rs->FetchNextObject();
   $rs->Close();
   if ($o1 === false) {
      echo "Record Id $rid disappeared!";
      return;
   }
   else if (empty($o1->RID)) {
      echo 'Δεν υπάρχει η εγγραφή προς ακύρωση!';
      return;
   }
   else if ($o1->RECTYPE != 1 || $o1->OWNOTHER != 1) {
      echo "Ακατάλληλος τύπος εγγραφής (RID=$rid), μόνο 'ίδιες' πωλήσεις επιτρέπονται!";
      return;
   }
   else if (empty($o1->MARK)) {
      echo "Η εγγραφή $o1->RID δεν έχει πάρει MARK, δεν μπορεί να γίνει ακύρωση!";
      return;
   }

   // Check if cancelation record already exists
   //
   $query = 'SELECT RID, CONVERT(VARCHAR, SUBMISSION, 120) AS SUBMISSION FROM mydata WHERE AGENTID=? AND RECTYPE=6 AND XMLTOSEND=?';
   $data = array($o1->AGENTID, $o1->MARK);
   if (($rs=$db->Execute($query, $data))===false) {
      echo __LINE__.' '.$db->ErrorMsg();
      return;
   }
   $o2 = $rs->FetchNextObject();
   $rs->Close();
   if ($o2 !== false) {
      echo "Ηδη υπάρχει η εγγραφή $o2->RID στις $o2->SUBMISSION";
      return;
   }

   // Create cancelation rec
   //
   $fhm = 'CAN.' .$o1->MARK;
   $query = 'INSERT INTO mydata (AGENTID, OWNOTHER, RECTYPE, FHMKEY, XMLTOSEND, CREDAYTIME, SUBMISSION) VALUES(?,?,6,?,  ?,?,?)';
   $data = array($o1->AGENTID, $o1->OWNOTHER, $fhm, $o1->MARK, date('Y-m-d H:i:s'), $o1->SUBMISSION);
   if (!$db->Execute($query, $data)) {
      echo 'Database INSERT error!';
   }
   else {
      $query = 'SELECT @@IDENTITY'; // do not use SCOPE_IDENTITY()
      if (($rs=$db->Execute($query))!==false) {
         if (($row = $rs->FetchRow())!==false) {
            $rid2 = $row[0];
         }
         $rs->Close();
      }
      echo "OK Δημιουργήθηκε η εγγραφή με Id $rid2 στις $o1->SUBMISSION";
   }
}
//--------------------------------------
function do_export_mydata_recs()
{
   $myagentid = (int)@$_SESSION['MYAGENTID'];
   $mydate = @$_SESSION['MYDATE'];

   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Content-Type: application/octet-stream");
   header("Content-Disposition: attachment; filename=mydata{$mydate}.csv");

   global $x2;
 	$db=&$x2->db;

   if (empty($mydate)) {
      echo 'DATE not defined!';
      return;
   }

   $ownother = $_SESSION['OWNOTHER']??1;

   $fields = [
      'RID' => 'Record Id',
      'RECTYPE' => 'Record type',
      'SUBMISSION' => 'Submission',
      'CREDAYTIME' => 'Created',
      'LASTSENT' => 'Last sent',
      'RETVAL' => 'Return value',
      'DATALEN' => 'Data length',
      'DOCSERIES' => 'Doc.Series',
      'DOCAA' => 'Doc.A/A',
      'DOCNET'	=> 'Net value',
      'DOCGROSS' => 'Gross value',
      'DOCTYPE' => 'Doc.type',
      'ISSBRANCH' => 'Issuer branch',
      'DESTAFM' => ($ownother==1?'Destination AFM':'Issuer AFM')
   ];

   $query = make_query_mydata_recs();
   $query .= " WHERE T1.AGENTID=? AND T1.OWNOTHER=$ownother AND SUBMISSION=? ";
   $data = [$myagentid, $mydate];
   $query .= " ORDER BY T1.RECTYPE, T1.SUBMISSION, T1.RID";

   if (($rs=$db->Execute($query, $data))===false) {
      echo $db->ErrorMsg();
      return;
   }
   // Read record info
   $nf = $rs->FieldCount();
   $row = array();
   for ($i=0; $i<$nf; $i++) {
      $fieldname = $rs->FetchField($i)->name;
      if (array_key_exists($fieldname, $fields)) {
         $fieldname = $fields[$fieldname]; // reset
      }
      $row[] = $fieldname;
   }
   echo implode(';', $row)."\r\n";
   // Fetch all records
   while ($row = $rs->FetchRow()) {
      echo implode(';', $row)."\r\n";
   }
   $rs->Close();
}
//--------------------------------------
function my_record_orgin()
{
   global $x2;
   $db=&$x2->db;

   $rid = (int)$_REQUEST['RID'];
   $query = "SELECT XFLAG FROM mydata WHERE RID=$rid";
   if (($rs=$db->Execute($query))===false) {
      echo $db->ErrorMsg();
      return;
   }
   $row = $rs->FetchRow();
   $rs->Close();
   if ($row === false) {
      umessage("Record RID=$rid dissapeared!");
      return;
   }

   $xflag = (int)$row[0];
   if (empty($xflag)) {
      umessage('Information not available');
      return;
   }
   else if ($xflag < 100) {
      umessage('Δεν υπάρχουν πληροφορίες για το ακυρωτικό');
      return;
   }

   $query = "SELECT ATXT FROM mydata T1
      LEFT JOIN abmaiex T2 ON (T2.S11=SUBSTRING(T1.FHMKEY, 1, 11) AND T2.Z4=SUBSTRING(T1.FHMKEY, 12, 4) AND T2.DAILY=SUBSTRING(T1.FHMKEY, 16, 4))
      WHERE T1.RID=?";
   $data = [$xflag];
   if (($rs=$db->Execute($query, $data))===false) {
      echo $db->ErrorMsg();
      return;
   }
   $o1 = $rs->FetchNextObject();
   $rs->Close();
   if ($o1===false) {
      umessage('Δεν υπάρχουν πληροφορίες για το πρωτότυπο, rid='.$xflag);
      return;
   }
   if (empty($o1->ATXT)) {
      umessage('Δεν υπάρχει το Α.ΤΧΤ για το πρωτότυπο, rid='.$xflag);
      return;
   }
   echo '<pre>'.myhtml($o1->ATXT).'</pre>';
}
//--------------------------------------
function z_record_info()
{
   global $x2;
 	$db=&$x2->db;

   $s11 = (string)@$_REQUEST['S11'];
   $znr = (int)($_REQUEST['ZNR']??0);
   $daily = (int)($_REQUEST['DAILY']??0);

   if (($xml = simplexml_load_file("c:\\inca hellas\\taxspooler.xml")) === false) {
      echo "Could not load taxspooler.xml";
      return;
   }
?>
   <div id="tabs">
      <ul>
         <li><a href="#tabs-1">A.TXT</a></li>
         <li><a href="#tabs-2">B.TXT</a></li>
         <li><a href="#tabs-3">E.TXT</a></li>
      </ul>
<?php
   $query = 'SELECT YYMMDD, /*ATXT,*/ BTXT, ETXT FROM abmaiex WHERE S11=? AND Z4=? AND DAILY=?';
   $data = array($s11, $znr, $daily);
   if (($rs=$db->Execute($query, $data))===false) {
      echo $db->ErrorMsg();
   }
   else {
      $o1 = $rs->FetchNextObject();
      $rs->Close();
      if ($o1===false) {
         echo 'Record disappeared!';
      }
      else { // o.k.
         $xpath = sprintf('%s\\%s\\%04d\\%s%06d%04d%04d_A.TXT', $xml->ClientSaveDir, $s11, $znr,
            $s11, (int)$o1->YYMMDD, $znr, $daily);
         $atxt = file_get_contents($xpath);
         echo  '<div id="tabs-1" class="inframe">'.
               '<pre>'.myhtml(ToUtf8($atxt)).'</pre>'.
               '</div>'.
               '<div id="tabs-2" class="inframe">'.
               '<pre>'.myhtml($o1->BΤΧΤ).'</pre>'.
               '</div>'.
               '<div id="tabs-3" class="inframe">'.
               '<pre>'.myhtml($o1->EΤΧΤ).'</pre>'.
               '</div>';
      }
   }
?>
   </div><!-- id=tabs -->
   <script>
   $('#tabs').tabs();
   </script>
<?php
}
//--------------------------------------
function my_record_info()
{
   global $x2;
 	$db=&$x2->db;

   $rid = (int)(@$_REQUEST['RID']);
?>
<div id="tabs">
   <ul>
      <li><a href="#tabs-1">Απάντηση</a></li>
      <li><a href="#tabs-2">Αποστάλθηκε</a></li>
      <li><a href="#tabs-3">A.TXT</a></li>
      <li><a href="#tabs-4">E.TXT</a></li>
   </ul>
<?php
   $o1 = false;
   echo '<div id="tabs-1" class="inframe">';
   $query = "SELECT RETDATA, FHMKEY, XMLSENT FROM mydata WHERE RID=$rid";
   if (($rs=$db->Execute($query))===false) {
      echo $db->ErrorMsg();
   }
   else {
      $o1 = $rs->FetchNextObject();
      $rs->Close();
      if ($o1===false) {
         echo 'Record disappeared!';
      }
      else {
         echo '<pre>'.myhtml($o1->RETDATA).'</pre>';
      }
   }
   echo '</div>';

   echo '<div id="tabs-2" class="inframe">';
   if ($o1===false) {
      echo 'Record disappeared!';
   }
   else {
      echo '<pre>'.myhtml($o1->XMLSENT).'</pre>';
   }
   echo '</div>';

   $o2 = false;

   echo '<div id="tabs-3" class="inframe">';
   do {
      if ($o1 === false)
         break;

      $s11 = substr($o1->FHMKEY, 0, 11);
      $z4 = (int)substr($o1->FHMKEY, 11, 4);
      $daily = (int)substr($o1->FHMKEY, 15, 4);
      $query = "SELECT ATXT, ETXT FROM abmaiex WHERE S11=? AND Z4=? AND DAILY=?";
      $data = [$s11, $z4, $daily];
      if (($rs=$db->Execute($query, $data))===false) {
         echo $db->ErrorMsg();
         break;
      }
      $o2 = $rs->FetchNextObject();
      $rs->Close();
      if ($o2===false || empty($o2->ATXT)) {
         echo 'A.TXT not available!';
         break;
      }
      echo '<pre>'.myhtml($o2->ATXT).'</pre>';

   } while (false);
   echo '</div>';

   echo '<div id="tabs-4" class="inframe">';
   do {
      if ($o2 === false || empty($o2->ETXT)) {
         echo 'E.TXT not available!';
         break;
      }
      echo '<pre>'.myhtml($o2->ETXT).'</pre>';
   } while (false);
   echo '</div>';
?>
</div><!-- id=tabs -->
<script>
$('#tabs').tabs();
</script>
<?php
}
//--------------------------------------
?>
