<?php
defined('COT_CODE') or die('Wrong URL');

$return['purse_amount'] = 0;
$return['redirect'] = '';
$return['error'] = '';
$return['success'] = false;

$id = cot_import('id', 'G', 'TXT');
$ads = array();

if($usr['id'] > 0 && $id > 0) {
  $ads = $db->query("SELECT * FROM $db_ads WHERE item_id = ".(int)$id." AND item_userid=".$usr['id']." LIMIT 1")->fetch();

  if($ads['item_id'] > 0 && $ads['item_expire'] == 0 && $ads['item_paused'] == 0)
  {
    if($ads['item_marketid'] > 0) {
      require_once cot_incfile('market', 'module');
      $item = $db->query("SELECT * FROM $db_market WHERE item_id=".$ads['item_marketid']." AND item_userid=".$ads['item_userid'].' AND item_state=0 AND item_ismoder!=0 LIMIT 1')->fetch();
      if($item['item_id'] > 0) {
        $return['success'] = true;

        $data = array(
          'market' => array(),
          'cost' => 0,
        );

        $data['market'] = ($ads['item_marketid'] > 0 ? $db->query("SELECT * FROM $db_market WHERE item_id=".$ads['item_marketid']." AND item_userid=".$usr['id'].' AND item_state=0 AND item_ismoder!=0 LIMIT 1')->fetch() : array('item_id' => 0));
        if($data['market']['item_id'] > 0) {
          $data['cost'] = (!empty($cfg['market']['cat_' . $ads['item_cat']]['adsprice']) ? $cfg['market']['cat_' . $ads['item_cat']]['adsprice'] : $cfg['market']['cat___default']['adsprice'])-0;
        } else {
          $return['error'] = cot_sitename('padeg', 'i').' '.cot_sitename('words', 'notfound').', либо '.cot_sitename('words', 'notactive');
          $return['success'] = false;
        }

        $data['cost'] = $data['cost'] * $ads['item_period'];

        if($ads['item_position'] > 0 && is_array($ads_positions[$ads['item_position']])) {
          $data['cost'] = $data['cost'] * $ads_positions[$ads['item_position']]['ratio'];
        } else {
          $return['error'] = 'Не корректно указано место размещения';
          $return['success'] = false;
        }

        require_once cot_incfile('configuration');

        if($cfg['plugin']['ads']['purchase_period'] == 'day')
        {
      		$options['time'] = $ads['item_period'] * 24 * 60 * 60;
        }
        elseif($cfg['plugin']['ads']['purchase_period'] == 'week')
        {
      		$options['time'] = $ads['item_period'] * 7 * 24 * 60 * 60;
        }
        else
        {
      		$options['time'] = $ads['item_period'] * 30 * 24 * 60 * 60;
        }

        $summ = $data['cost'];
        $options['desc'] = $L['ads_payments_desc'];
      	$options['code'] = $ads['item_id'];

      	if ($db->fieldExists($db_payments, "pay_redirect")){
      		$options['redirect'] = $cfg['mainurl'].'/'.cot_url('plug', 'e=ads', '', true);
      	}

        $return['purse_amount'] = $summ;
        if(!($return['purse_amount'] > 0)) {
          $return['error'] = 'Не корректная сумма для оплаты';
          $return['success'] = false;
        }

      	if ($return['success'])
      	{
          $return['success'] = false;

          $ubalance = cot_payments_getuserbalance($usr['id']);
          $pid = cot_payments_create_order('ads', $summ, $options);
          if($pid > 0) {
            if($ubalance < $return['purse_amount']) {
              if($ubalance > 0) $return['purse_amount'] -= $ubalance;
              $return['payment_id'] = $pid;
            } else {
              $return['success'] = true;
              $return['redirect'] = cot_url('payments', 'm=billing&pid=' . $pid, '', true);
            }
          }
        }
      } else {
        $return['error'] = cot_sitename('padeg', 'i').' '.cot_sitename('words', 'notfound').', либо '.cot_sitename('words', 'notactive');
      }
    }
  }
}

?>