<?

include('const.php');

?>

<html>



<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">

<meta http-equiv="Content-Language" content="ru">

<title>Окно доставки товара</title>

<link rel="stylesheet" href="/antsoft.css" type="text/css">

</head>



<body topmargin="0" leftmargin="0" link="#0000FF" vlink="#0000FF" alink="#FF0000">



<table border="0" width="100%" cellspacing="0" cellpadding="0">

  <tr>

    <td width="100%" bgcolor="#9ACE8B"><img border="0" src="/pic_delivery.gif" width="247" height="38"></td>

  </tr>

  <tr>

    <td width="100%">

      <table border="0" width="100%" cellspacing="0" cellpadding="0">

        <tr>

          <td width="70" height="138" valign="top">

             <p>&nbsp;</p>

             <p align="center"><img id="pic1" border="0" src="img/pic_d_count.gif" width="32" height="50"></p>

          </td>

          <td width="280" height="138" valign="top">

<?

If ($err <> "") {

 echo "<p><font color='#FF0000'><b>Возникла ошибка: $err</b></p><p>Пожалуйста, обратитесь к администратору (<a class='cap' href='mailto:info@shopWM.ru'>info@shopWM.ru</a>)</font></p>";

}

If ($shopcnt <> "") {



 $fcnt = @file("shopcounts.inc");

 for ($i = 0; $i < count($fcnt); $i++) {

  $ar = explode("^", trim($fcnt[$i]));

  If ($ar[0] == $shopcnt) {

   $fid = $i;

   break;

  }

 }





 If ($fid < 0 or $ar[6] <> $wm) {

  print "<p><font color='#FF0000'><b>Счет не найден либо Вы пытаетесь получить доступ к неоплаченному Вами товару.</b></font></p>";

 }

 Else {



  $fusr = @file("users/$ar[2]");

  $inv_id   = $ar[0];		//$HTTP_POST_VARS["invc_n"];

  $wminv_id = $ar[1];		//$HTTP_POST_VARS["wminvc_n"];

  $wmid     = trim($fusr[7]);	//$HTTP_POST_VARS["wmid"];



  // Вызов сервисной функции модуля wm

  $stat = InvCheck($inv_id, $wmid, $wminv_id);





  if ($stat == -2) {

   print "<p><font color='#FF0000'><b>Ошибка проверки состояния!</b></p><p>Пожалуйста, обратитесь к администратору (<a class='cap4' href='mailto:info@shopWM.ru'>info@shopWM.ru</a>).</font></p>";

  }

  elseif ($stat == -1) {

   print "<p><font color='#FF0000'><b>Счет был удален Вами.</b></font></p>";



   $fw = fopen("shopcounts.inc", "w");

   $fcnt[$fid] = $ar[0];

   for ($t = 0; $t < count($fcnt); $t++) {

    fwrite($fw, trim($fcnt[$t]) . "\n");

   }

  }

  elseif ($stat ==  0) {

   print "<p>Счет ожидает оплаты.<p>";

  }

  elseif ($stat ==  1) {

   print "<p><font color='#FF0000'><b>Счет оплачен, но деньги не получены по причине протекции сделки.</b></p><p>Вам нужно дождаться возвращения Ваших денег и купить товар вновь.</font></p>";

  }

  elseif ($stat ==  2) {

   print "<p>Счет №$shopcnt оплачен!</p>";



   If ($ar[5] == md5($ar[0] . $ar[2] . "%f%" . $admxchar)) {



    If ($ar[7] == "0" or $ar[7] == "") {

     $fcnt[$fid] = "$ar[0]^$ar[1]^$ar[2]^$ar[3]^1^$ar[5]^$ar[6]";



     $fw = fopen("shopcounts.inc", "w");

     for ($k = 0; $k < count($fcnt); $k++){

      fwrite($fw, trim($fcnt[$k]) . "\n");

     }

     fclose($fw);



     If ($ar[4] == "0") {

      //добавление средств

      $fa = file("users/$ar[2]");

      $fg = fopen("users/$ar[2]", "w");

      fwrite($fg, trim($fa[0]) . "\n");

      fwrite($fg, trim($fa[1]) . "\n");

      fwrite($fg, trim($fa[2]) . "\n");

      fwrite($fg, trim($fa[3]) . "\n");

      fwrite($fg, trim($fa[4]) . "\n");

      fwrite($fg, trim($fa[5]) . "\n");

      fwrite($fg, trim($fa[6]) . "\n");

      fwrite($fg, trim($fa[7]) . "\n");

      fwrite($fg, trim($fa[8]) . "\n");

      fwrite($fg, (double) trim($fa[9]) + ceil($ar[3]*100)/100 . "\n");

      for ($rf = 10; $rf < count($fa); $rf++) {

       fwrite($fg, trim($fa[$rf]) . "\n");

      }

      fclose($fg);

      //end of добавление

      echo "<p>На Ваш счет поступила сумма в размере $ar[3]wm.</p>";

      history($ar[2], ceil($ar[3]*100)/100, "0", "0", "0", $ar[6], $serv_const_shopwmz, "Пополнение личного счета на $ar[3]wm.");

     }

    }

    Else {

     $tovid = $ar[7];

     $fgood = @file("goods/$tovid");



     $pos = -1;

     $d3 = dir("files/");

     while($text = $d3->read()) {

      If ($text <> "." and $text <> ".." and $text <> ".htaccess") {

       $as = explode(".", $text);

       If ((integer) $as[1] > $pos and $as[0] == $tovid) {

        $pos = (integer) $as[1];

       }

      }

     }

     $d3->close();



     If ($pos <= -1) {

      If ($ar[4] == "0") {

       If ($endtov == "ok") {





        $trn_id  = "0";

        $wmpurse = $wmz;

        $summ    = floor(trim($ar[3]) / (1 + 0.008)*100)/100;

        $dsc     = "Возврат денег, т.к. товар закончился.";

        $dsc     = str_replace("\\'", "'", str_replace("\\\"", "\"", $dsc));



        list($wmtrn_n, $err) = TransCreate($wmpurse, $summ, $trn_id, $dsc);



        if ($wmtrn_n>0) {

         //$fcnt[$fid] = "$ar[0]^$ar[1]^$ar[2]^$ar[3]^1^$ar[5]^$ar[6]^$ar[7]^$ar[8]^{$ar2[$pos]}";

         $fcnt[$fid] = "$ar[0]";

         $ar[9] = $ar2[$pos];

         $fw = fopen("shopcounts.inc", "w");

         for ($k = 0; $k < count($fcnt); $k++){

          fwrite($fw, trim($fcnt[$k]) . "\n");

         }

         fclose($fw);



         echo "<script> alert('Деньги успешно переведены!'); </script>";

        }

        Else {

         echo "<script> alert('Ошибка!!! $err \\nОбратитесь к администратору info@shopWM.ru, либо по внутренней почте.'); </script>";

        }

        exit;

       }

       echo "<p>К сожалению, товар закончился. Вы можете дождаться, пока продавец пополнит этот товар, либо введите в поле 'Ваш WMZ' Ваш кошелек Z-типа и нажмите на кнопку 'Вернуть деньги' для возврата денег.</p>";

       echo "<form name='frm2' method='POST'><input type='hidden' name='endtov' value='ok'>

             <p><input type='text' maxlength='13' name='wmz' size='15' style='font-family: Verdana, Arial; font-size: 8pt; border: 1 solid #7F9DB9'> <input type='submit' name='B1' style='font-family: Verdana, Arial; font-size: 8pt' value='Вернуть деньги'></p></form>";

       exit;

      }

     }



     If ($ar[4] == "0") {



      $agent = $ar[8];

      $agentsumm = 0;



      If ((double) trim($fgood[6]) > 0 and $agent <> trim($fgood[0]) and file_exists("users/$agent") == true and $agent <> ".htaccess" and $agent <> "") {

       $agentsumm = ceil($ar[3] * (double) trim($fgood[6])/100*100) / 100;



       //добавление средств

       $fa = file("users/" . $agent);

       $fg = fopen("users/" . $agent, "w");

       fwrite($fg, trim($fa[0]) . "\n");

       fwrite($fg, trim($fa[1]) . "\n");

       fwrite($fg, trim($fa[2]) . "\n");

       fwrite($fg, trim($fa[3]) . "\n");

       fwrite($fg, trim($fa[4]) . "\n");

       fwrite($fg, trim($fa[5]) . "\n");

       fwrite($fg, trim($fa[6]) . "\n");

       fwrite($fg, trim($fa[7]) . "\n");

       fwrite($fg, trim($fa[8]) . "\n");

       fwrite($fg, (double) trim($fa[9]) + $agentsumm . "\n");

       for ($rf = 10; $rf < count($fa); $rf++) {

        fwrite($fg, trim($fa[$rf]) . "\n");

       }

       fclose($fg);



       history($agent, $agentsumm, "0", "0", "0", $ar[6], $serv_const_shopwmz, "Выплата агентских средств за покупку товара №$tovid");

       //end of добавление

      }



      //добавление средств пользователю

      $fa = file("users/" . trim($fgood[0]));

      $fg = fopen("users/" . trim($fgood[0]), "w");

      fwrite($fg, trim($fa[0]) . "\n");

      fwrite($fg, trim($fa[1]) . "\n");

      fwrite($fg, trim($fa[2]) . "\n");

      fwrite($fg, trim($fa[3]) . "\n");

      fwrite($fg, trim($fa[4]) . "\n");

      fwrite($fg, trim($fa[5]) . "\n");

      fwrite($fg, trim($fa[6]) . "\n");

      fwrite($fg, trim($fa[7]) . "\n");

      fwrite($fg, trim($fa[8]) . "\n");

      $comis = ceil($ar[3]*$serv_const_commission*100) / 100;

      $summa = (double) $ar[3] - $comis - $agentsumm;

      fwrite($fg, (double) trim($fa[9]) + $summa . "\n");

      for ($rf = 10; $rf < count($fa); $rf++) {

       fwrite($fg, trim($fa[$rf]) . "\n");

      }

      fclose($fg);



      If ($agentsumm > 0) {

       $fraza = "Выплачено $agentsumm агенту $agent.";

      }



      history(trim($fgood[0]), $summa, "0", "0", $comis, $ar[6], $serv_const_shopwmz, "Покупка товара №$tovid, цена которого $ar[3]. $fraza");



      If(trim($fa[13]) == "1") {

       mail(trim($fa[6]), "Уведомление сервиса SHOPWM", "Куплен товар под номером $tovid, цена которого  $ar[3]. $fraza", "From: notify@$serv_const_servname Content-Type: text/plain; charset=\"koi8-r\"");

      }



      //end of добавление



      //добавление комиссии на админ. счет

      $fa = file("users/admin");

      $fg = fopen("users/admin", "w");

      fwrite($fg, trim($fa[0]) . "\n");

      fwrite($fg, trim($fa[1]) . "\n");

      fwrite($fg, trim($fa[2]) . "\n");

      fwrite($fg, trim($fa[3]) . "\n");

      fwrite($fg, trim($fa[4]) . "\n");

      fwrite($fg, trim($fa[5]) . "\n");

      fwrite($fg, trim($fa[6]) . "\n");

      fwrite($fg, trim($fa[7]) . "\n");

      fwrite($fg, trim($fa[8]) . "\n");

      $comis = ceil($ar[3]*$serv_const_commission*100) / 100;

      fwrite($fg, (double) trim($fa[9]) + $comis . "\n");

      for ($rf = 10; $rf < count($fa); $rf++) {

       fwrite($fg, trim($fa[$rf]) . "\n");

      }

      fclose($fg);

      history("admin", $comis, "0", "0", "0", $ar[6], $serv_const_shopwmz, "Покупка товара №$tovid, цена которого $ar[3]");

      //end of добавление



      $ar2 = explode("|", trim($fgood[10]));



      $fcnt[$fid] = "$ar[0]^$ar[1]^$ar[2]^$ar[3]^1^$ar[5]^$ar[6]^$ar[7]^$ar[8]^{$ar2[$pos]}";

      $ar[9] = $ar2[$pos];

      $fw = fopen("shopcounts.inc", "w");

      for ($k = 0; $k < count($fcnt); $k++){

       fwrite($fw, trim($fcnt[$k]) . "\n");

      }

      fclose($fw);



      If (trim($fgood[14]) == "0") {

       If (file_exists("files/purchased.{$shopcnt}") == false) {

        rename("files/{$tovid}.{$pos}", "files/purchased.{$shopcnt}");

       }

      }











      If (trim($fgood[14]) == "0") {

       $ff = fopen("goods/$tovid", "w");

       $fgood[10] = str_replace($ar[9] . "|", "", $fgood[10]);

       for ($i = 0; $i < count($fgood); $i++) {

        fwrite($ff, trim($fgood[$i]) . "\n");

       }

       fclose($ff);

      }



      $ff = fopen("goods/$tovid", "w");

      $fgood[15] = (integer) $fgood[15] + 1;

      for ($i = 0; $i < count($fgood); $i++) {

       fwrite($ff, trim($fgood[$i]) . "\n");

      }

      fclose($ff);

     }



     echo "<div style='overflow-y:scroll;height:120;width:99%;border: 1px solid #7F9DB9;'><p><b>Название: </b>" . trim($fgood[3]) . "</p>";

     If (trim($fgood[9]) == "0") {

      echo "<p><b>Продаваемая информация: </b></p>";



      If (trim($fgood[14]) == "0") {

       $fg = file("files/purchased.{$shopcnt}");

      }

      Else {

       $fg = file("files/{$tovid}.{$pos}");

      }



      for ($i = 0; $i < count($fg); $i++) {

       echo "<p>" . trim($fg[$i]) . "</p>";

      }

     }

     else {

      $ar2 = explode("|", trim($fgood[10]));

      If (trim($fgood[14]) == "0") {

       $kbs = ceil(filesize("files/purchased.{$shopcnt}") / 1024);

      }

      Else {

       $kbs = ceil(filesize("files/{$tovid}.{$pos}") / 1024);

      }



      echo "<p><a href='/getfile.php?shopcnt=$shopcnt&rnd=$rnd&wm=$wm&file=/" . $ar[9] . "' targe='_blank' class='cap4'>Скачать купленный товар ($kbs) kb</p>";

     }

     echo "</div>";



     $addr = "<input type='button' onClick=\"document.location.href='/responses.php?shopcnt=$shopcnt&wm=$wm';\" name='B1' style='font-family: Verdana, Arial; font-size: 8pt' value='Добавить отзыв'>";

    }

   }

  }

  else {

   print "<p>Внутренняя ошибка скрипта!!!</p>";

  }

 }



}

?>



          </td>

        </tr>

      </table>

    </td>

  </tr>

</table>

<p align="center"><input type="button" onClick="location.reload();" name="B1" style="font-family: Verdana, Arial; font-size: 8pt" value="Обновить"> <? echo $addr ?></p>

<?



 If ($stat == "-1") {

  $img = "img/pic_d_del.gif";

 }

 ElseIf ($stat == "0") {

  $img = "img/pic_d_count.gif";

 }

 ElseIf ($stat == "1") {

  $img = "img/pic_d_del.gif";

 }

 ElseIf ($stat == "2") {

  $img = "img/pic_d_ok.gif";

 }

 Else {

  $img = "img/pic_d_err.gif";

 }



 echo "<script> document.all.pic1.src = '$img'; </script>";

?>



</body>

</html>