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

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

$isQuick = cot_import('isQuick', 'P', 'BOL');
$isCart = cot_import('isCart', 'P', 'BOL');

require_once cot_incfile('api', 'plug', 'cart_add');
if($isQuick || $isCart == 0) {
  if($return['success'] && ($return['id'] > 0 || $indodji)) {
    $return['purse_amount'] = ($indodji ? $return['item']['cost_dodji'] : $return['item']['cost']);
    if($return['purse_amount'] > 0) {
      $ubalance = cot_payments_getuserbalance($usr['id'], $indodji);

      if($ubalance < $return['purse_amount']) {
        if($ubalance > 0) $return['purse_amount'] -= $ubalance;
        $return['success'] = false;
        $return['reason'] = 'purse';
      } else {
        $return['item'] = array(
          'title' => $item['item_title'],
          'photo_first' => $item['photo_first'],
          'cnt_item' => $prddata['prdcnt'],
          'cnt_extras' => $prddata['extrascnt'],
          'cost' => (!$indodji ? round($costtoshow, 0) : 0),
          'cost_format' => (!$indodji ? number_format(round($costtoshow, 0), 0, ' ', '') : 0),
          'cost_dodji' => ($indodji ? round($costtoshow, 0) : 0),
          'cost_dodji_format' => ($indodji ? number_format(round($costtoshow, 0), 0, ' ', '') : 0),
          'ciexists' => ($ciexists > 0 ? 1 : 0)
        );

        $return['cartid'] = $citem['ci_cid'];
        $return['id'] = $ci_id;

        $rorder = array();
        $rorder['order_cid'] = $ci_id;
        $rorder['order_pid'] = $item['item_id'];
        $rorder['order_userid'] = $usr['id'];
        $rorder['order_date'] = $sys['now'];
        $rorder['order_status'] = 'new';
        $rorder['order_title'] = $item['item_title'];
        $rorder['order_text'] = $item['item_text'];
        $rorder['order_seller'] = $item['item_userid'];
        $rorder['order_count'] = $citem['ci_count'];
        $rorder['order_cost'] = (!$indodji ? $citem['ci_cost'] : 0);
        $rorder['order_cost_dodji'] = ($indodji ? $citem['ci_cost_dodji'] : 0);
        $rorder['order_catcostplans'] = $citem['ci_catcostplans'];

        if($db->insert($db_market_orders, $rorder))
        {
        	$orderid = $db->lastInsertId();

          $options['paydodji'] = ($indodji ? 1 : 0);
        	$options['code'] = $orderid;
        	$options['desc'] = $item['item_title'];

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

        	/* === Hook === */
        	foreach (cot_getextplugins('marketorders.paymentbilling.payorder.done') as $pl)
        	{
        		include $pl;
        	}
        	/* ===== */

          $pid = cot_payments_create_order('market_payorder', ($indodji ? $rorder['order_cost_dodji'] : $rorder['order_cost']), $options);

        	if($pid > 0) {
            $return['success'] = true;
            $return['redirect'] = cot_url('payments', 'm=billing&pid=' . $pid, '', true);
          }
        }
      }
    }
  } else {
    $return['success'] = false;
  }
}

?>