<?php

class ImportPS extends FileImport
{
  function ImportFile($Content, $Ext)
  {
    if($Ext == 'txt') $this->ImportTxt($Content);
      else if($Ext == 'cvs') $this->ImportCVS($Content);
  }

  function ImportTxt($Content)
  {
    $Finance = &$this->System->Modules['Finance'];
    $Data = explode("\n", $Content);
  }
  
  function ImportCVS($Content)
  {
    $Finance = &$this->System->Modules['Finance'];
    
    $Data = explode("\n", $Content);
    foreach($Data as $Key => $Value)
    {
      $Value = str_replace('\"', '"', $Value);
      $Data[$Key] = str_getcsv($Value, ',', '"', "\\");
      //print_r($Data[$Key]);
      foreach($Data[$Key] as $Key2 => $Value2)
      {
        if(substr($Data[$Key][$Key2], 0, 2) == '\"')
          $Data[$Key][$Key2] = substr($Data[$Key][$Key2], 2, -2);
      }
    }
    $Header = array(
        0 => 'datum zaúčtování',
        1 => 'částka',
        2 => 'měna',
        3 => 'zůstatek',
        4 => 'konstantní symbol',
        5 => 'variabilní symbol',
        6 => 'specifický symbol',
        7 => 'označení operace',
        8 => 'název protiúčtu',
        9 => 'protiúčet',
        10 => 'poznámka',
        11 => '',
    );
    
    if($Header != $Data[0]) $Output = 'Nekompatibilní struktura CSV';
    else
    {
      array_shift($Data);
      $Automatic = '';
      $Manual = '';
      $Output = '<form action="?Operation=insert" method="post">';
      $I = 0;
      foreach($Data as $Key => $Value)
      {
        if(count($Value) <= 1) continue;
        if($Value[9] == '') $Value[5] = 128; // Žádný účet => Poštovní spořitelna
        $Time = explode('.', $Value[0]);
        $Time = $Time[2].'-'.$Time[1].'-'.$Time[0];
        $Money = $Value[1];
        if(is_numeric($Value[5]))
        {
          $Subject = $Value[5] * 1;
          $DbResult = $this->Database->query('SELECT Id FROM Subject WHERE Id='.$this->Database->real_escape_string($Subject));
          if($DbResult->num_rows == 0) $Subject = '? ('.($Value[5] * 1).')';
        } else
        {
          $Subject = '? ('.$Value[5].')';
        }
        if(!is_numeric($Subject))
        {
          $Mode = 'Ručně';
          $Style = 'style="background-color: LightPink;" ';
        } else
        {
          $Mode = 'Automaticky';
          $Style = '';
        }
    
        if($Money < 0) $Text = 'Platba převodem';
        else $Text = 'Přijatá platba';
        $Automatic .= '<tr>'.
            //'<td>'.$Mode.'</td>'.
        '<td><input type="text" name="Date'.$I.'" value="'.$Time.'"/></td>'.
        '<td><input type="text" '.$Style.'name="Subject'.$I.'" value="'.$Subject.'"/></td>'.
        '<td>'.$Value[8].'</td>'.
        '<td><input type="text" name="Money'.$I.'" value="'.$Money.'"/></td>'.
        '<td><input type="text" name="Text'.$I.'" value="'.$Text.'"/></td>'.
        '<td><input type="text" name="Taxable'.$I.'" value="1"/></td>'.
        '<td><input type="text" name="Network'.$I.'" value="1"/></td>'.
        '</tr><tr><td colspan="7">'.implode(', ', $Value).'</td></tr>';
        $I++;
      }
      $Output .= '<table class="WideTable">'.
          '<tr>'.
          //'<th>Zpracování</th>'.
      '<th>Datum</th><th>Var. symbol</th><th>Protiúčet</th><th>Částka [Kč]</th><th>Text</th><th>Zdanitelné</th><th>Síť</th></tr>';
      $Output .= $Automatic.'</table>';
      $Output .= '<input type="hidden" name="ItemCount" value="'.$I.'"/>';
      $Output .= '<input type="submit" value="Zpracovat"/></form>';
    }
  } 
}

?>
