<?
/***********************************************************************/
/*                                                                     */
/*  This file is created by deZender                                   */
/*                                                                     */
/*  deZender (Decoder for Zend Encoder/SafeGuard):                     */
/*    Version:      0.9.3.0                                            */
/*    Author:       qinvent.com                                        */
/*    Release on:   2005.11.12                                         */
/*                                                                     */
/***********************************************************************/


  echo '<table cellspacing=0 cellpadding=2 border=0>
 <td colspan=2><b>Import Transactions:</b><br><br></td>
</tr>
</table>

';
  $errors = array ();
  if ($frm['action'] == 'import')
  {
    if (($_FILES['file']['size'] == 0 OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE))
    {
      array_push ($errors, array ('name' => 'Please, provide E-Gold CSV file.'));
    }
    else
    {
      if ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
      {
        array_push ($errors, array ('name' => 'Provided file is too big. Max file size is ' . ini_get ('upload_max_filesize')));
      }
      else
      {
        if ($_FILES['file']['error'] == UPLOAD_ERR_PARTIAL)
        {
          array_push ($errors, array ('name' => 'Uploading was faid. Please try again.'));
        }
      }
    }

    if (!$errors)
    {
      $fp = fopen ($_FILES['file']['tmp_name'], 'r');
      $tmp_fields = fgetcsv ($fp, 1024, ',');
      $fields = array ();
      if (!$tmp_fields)
      {
        array_push ($errors, array ('name' => 'Invalid CSV file. Mast be comma separated!'));
      }
      else
      {
        for ($i = 0; $i < sizeof ($tmp_fields); ++$i)
        {
          $fields[$tmp_fields[$i]] = $i;
        }

        if (!in_array ('Time', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV do not contains required <b>Time</b> field!'));
        }

        if (!in_array ('Type', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV does not contain the required <b>Type</b> field!'));
        }

        if (!in_array ('Batch', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV does not contain the required <b>Batch</b> field!'));
        }

        if (!in_array ('To/From Number', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV does not contain the required <b>To/From Number</b> field!'));
        }

        if (!in_array ('Entered Amount', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV does not contain the required <b>Entered Amount</b> field!'));
        }

        if (!in_array ('Entered Currency', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV does not contain the required <b>Entered Currency</b> field!'));
        }

        if (!in_array ('Memo', array_keys ($fields)))
        {
          array_push ($errors, array ('name' => 'CSV does not contain the required <b>Memo</b> field!'));
        }

        $i = 1;
        $skiped = 0;
        $added = 0;
        if (!$errors)
        {
          echo '<table cellspacing=1 cellpadding=2 border=0>
<tr>
  <th class=title>#</td>
  <th class=title>Status</th>
  <th class=title>Type</th>
  <th class=title>Time</th>
  <th class=title>Account</th>
  <th class=title>Amount</th>
  <th class=title>Batch</th>
  <th class=title>Comment</th>
</tr>
';
          while ($data = fgetcsv ($fp, 1024, ','))
          {
            $error = '';
            if ($data[$fields['Type']] != 'Payment Received')
            {
              $error = 'Invalid Type: <b>' . $data[$fields['Type']] . '</b>. Should be \'Payment Received\' only';
            }
            else
            {
              if ($data[$fields['Entered Currency']] != 'USD')
              {
                $error = 'Invalid Currency: <b>' . $data[$fields['Entered Currency']] . '</b>. Should be \'USD\' only';
              }
              else
              {
                preg_match ('~(\\d+)/(\\d+)/(\\d+) (\\d+):(\\d+)~', $data[$fields['Time']], $vals);
                if (!$vals)
                {
                  $error = 'Invalid Time Format: <b>' . $data[$fields['Time']] . '</b>. Should be \'mm/dd/yy hh:mm\' only';
                }
                else
                {
                  $date = sprintf ('%04d-%02d-%02d %02d:%02d:00', $vals[3] + (2000 < $vals[3] ? 0 : 2000), $vals[1], $vals[2], $vals[4], $vals[5]);
                  $q = 'select count(*) as cnt from hl_statistics where batch = \'' . quote ($data[$fields['Batch']]) . '\'';
                  ($sth = mysql_query ($q) OR print mysql_error ());
                  $row = mysql_fetch_array ($sth);
                  if (0 < $row['cnt'])
                  {
                    $error = '<b>Batch ' . $data[$fields['Batch']] . '</b> already exists';
                  }
                  else
                  {
                    $q = 'select id, name from hl_listings where account like \'%|' . quote ($data[$fields['To/From Number']]) . '|%\'';
                    $sth = mysql_query ($q);
                    $row = mysql_fetch_array ($sth);
                    if (!$row['id'])
                    {
                      $error = '<b>Account ' . $data[$fields['To/From Number']] . '</b> does not exist in our database.';
                    }
                    else
                    {
                      $listing_id = $row['id'];
                      $hyip_name = $row['name'];
                      $q = '' . 'insert into hl_statistics set 
                             listing_id = ' . $listing_id . ',
                             type = 1,
                             date = \'' . $date . '\',
                             amount = \'' . quote ($data[$fields['Entered Amount']]) . '\',
                             batch = \'' . quote ($data[$fields['Batch']]) . '\',
                             comment = \'' . quote ($data[$fields['Memo']]) . '\'
                         ';
                      (mysql_query ($q) OR print mysql_error ());
                    }
                  }
                }
              }
            }

            if ($error)
            {
              echo '<tr bgcolor=';
              echo ($i % 2 == 0 ? '#FFFFFF' : '#FEEEDD');
              echo ' align=center>
  <td>';
              echo $i;
              echo '</td>
  <td style="color: red">';
              echo $error;
              echo '</td>
  <td>';
              echo $data[$fields['Type']];
              echo '</td>
  <td nowrap>';
              echo $data[$fields['Time']];
              echo '</td>
  <td>';
              echo $data[$fields['To/From Number']];
              echo '</td>
  <td nowrap>';
              echo $data[$fields['Entered Amount']];
              echo ' ';
              echo $data[$fields['Entered Currency']];
              echo '</td>
  <td>';
              echo $data[$fields['Batch']];
              echo '</td>
  <td>';
              echo $data[$fields['Memo']];
              echo '</td>
</tr>
';
              ++$skiped;
            }
            else
            {
              echo '<tr bgcolor=';
              echo ($i % 2 == 0 ? '#FFFFFF' : '#FEEEDD');
              echo ' align=center>
  <td>';
              echo $i;
              echo '</td>
  <td style="color: green">OK</td>
  <td>';
              echo $data[$fields['Type']];
              echo '</td>
  <td nowrap>';
              echo $data[$fields['Time']];
              echo '</td>
  <td>';
              echo $data[$fields['To/From Number']];
              echo '</td>
  <td nowrap>';
              echo $data[$fields['Entered Amount']];
              echo ' ';
              echo $data[$fields['Entered Currency']];
              echo '</td>
  <td>';
              echo $data[$fields['Batch']];
              echo '</td>
  <td>';
              echo $data[$fields['Memo']];
              echo '</td>
</tr>
';
              ++$added;
            }

            ++$i;
          }
        }

        fclose ($fp);
        echo '</table>
<br>
<b>Added: ';
        echo $added;
        echo '</b><br>
<b>Skiped: ';
        echo $skiped;
        echo '</b><br>
<b>Total: ';
        echo $i - 1;
        echo '</b><br>
';
      }
    }

    if ($errors)
    {
      foreach (array_values ($errors) as $error)
      {
        echo '<li style="color: red"> ';
        echo $error['name'];
        echo ' <br>';
      }
    }
  }
  else
  {
    echo '
';
    echo start_info_table ('100%');
    echo 'You can import programs\' payouts into the system by uploading the E-Gold CSV history report.<br>
You can get it in this way:<br>
1. Login to your e-gold account.<br>
2. Go to History Section.<br>
3. Fill in the form and click \'View Detailed History\'.<br>
4. Click on the \'Click here to download CSV format file of this history report.\' link on the bottom of History report page to download CSV file.<br>
Yo';
    echo 'u can edit this file in MS Excel or any other CSV or Text editor to remove not related transactions.<br><br>

';
    echo end_info_table ();
  }

  echo '<br>
';
  if ((ini_get ('file_uploads') != 1 AND strtoupper (ini_get ('file_uploads')) != 'ON'))
  {
    echo 'Sorry, your current PHP settings do not allow files uploading.
';
  }
  else
  {
    echo '<form method=post enctype="multipart/form-data">
<input type=hidden name=a value=import_transactions>
<input type=hidden name=action value=import>
<table cellspacing=0 cellpadding=2 border=0>
<tr>
 <td>History File:</td>
 <td><input type="file" name="file" class=inpts></td>
</tr>
<tr>
 <td colspan=2><input type="submit" value="Import" class=sbmt></td>
</tr>
</table>
</form>
';
  }

?>
