<?

$_LANG['zakazi'] = "Мои Покупки";
$id = $inUser->id;										
$usr['id']	 = $inUser->id;

$inCore->loadModel('torg');
$model_pages = new cms_model_torg();
$pagecfg=$model_pages->loadTorgConfig('pages', $usr['id']);

	$sql_usr = "SELECT TU.*, P.*, U.nickname
  FROM cms_torg_users TU
  LEFT JOIN cms_user_profiles P ON P.user_id = TU.id
  LEFT JOIN cms_users U ON U.id = TU.id
  WHERE TU.id = $id";
  
  
	//$sql_usr = "SELECT
	//A.id, A.icq, A.email, A.phone, A.mob, A.status,A.login,A.nickname, A.is_deleted,
	//B.user_id, B.imageurl
	//FROM cms_users as A, cms_user_profiles as B
	//WHERE A.id = $id AND B.user_id = $id";
	
	
	$result_usr = $inDB->query($sql_usr);

if ($inDB->num_rows($result_usr)>0){
		
		
$usr = $inDB->fetch_assoc($result_usr);
$inPage->addPathway($usr['nickname'], cmsUser::getProfileURL($usr['login']));
$inPage->addPathway($_LANG['zakazi']);
$inPage->setTitle($_LANG['zakazi'].' - '.$usr['nickname']);
		
//$usr['avatar'] = usrLink(usrImageNOdb($usr['id'], 'small', $usr['imageurl'], $usr['is_deleted']), $usr['login'], $menuid);
$usr['nickname'] = cmsUser::getProfileLink($usr['login'], $usr['nickname']);
$usr['zakazi_count'] = $inDB->rows_count('cms_user_zakazi', "from_id={$usr['id']} AND provesti=1 AND opla4en=1");
$ok_zero = ($inDB->rows_count('cms_user_zakazi', "from_id={$usr['id']} AND podtverjden=0")>0 ? true:false);						  

$ok = ($do=='viewpokupki-ok' ? 1 : 0);
$order = ($do=='viewpokupki-order' ? true : false);
if ($do=='viewpokupki') {$provesti=0; $opla4en="AND z.opla4en=0";}
else if ($do=='viewpokupki-ok') {$provesti=1; $opla4en="AND z.opla4en=1";}
else if ($do=='viewpokupki-order'){ $provesti=1; $opla4en="AND z.opla4en=0";}

		$sql = "SELECT z.*, z.message as title, z.senddate as pubdate,
		prodavec.id as prodavec_id, u.nickname as prodavec_nickname, prodavec.mob as prodavec_mob, prodavec.buy_info as prodavec_buy_info,
		i.billing_buy,i.wm_buy,i.eltovar, title as tit
		
				FROM cms_user_zakazi z
				LEFT JOIN cms_torg_users prodavec ON prodavec.id=z.to_id
				LEFT JOIN cms_users u ON u.id=z.to_id
				LEFT JOIN cms_torg_items i ON i.id=z.item_id
				WHERE z.from_id = $id AND z.provesti=$provesti $opla4en
				ORDER BY z.senddate DESC
				"; 
		
		$perpage = 10;
		if (isset($_REQUEST['page'])) { $page = abs((int)$_REQUEST['page']); } else { $page = 1; }
		$sql .= "LIMIT ".($page-1)*$perpage.", $perpage";

		$result = $inDB->query($sql);
    
if ($inDB->num_rows($result)>0){
$items = array();
while($item = $inDB->fetch_assoc($result)){

$item['pubdate']  = cmsCore::dateformat($item['pubdate']);
$items[] = $item;
}

	

	
$bagebar= pageBartorg($id, $page, $perpage);

		} else {$items=false;}
				
	
	
	}

	$smarty = $inCore->initSmarty('components', 'com_torg_pokupki.tpl');
		$smarty->assign('cfg', $cfg);
                $smarty->assign('pagecfg', $pagecfg);
	$smarty->assign('items', $items);
        $smarty->assign('usr', $usr);
	$smarty->assign('ok', $ok);
	$smarty->assign('ok_zero', $ok_zero);
	$smarty->assign('order', $order);
	$smarty->assign('bagebar', $bagebar);
	$smarty->display('com_torg_pokupki.tpl');
	
	     return true;
	
	?>