1, 'pacana' => 0, 'stornirana' => 0);
function __construct(){
// Zakesiramo vse pakete
$this->cachePackages();
if(isset($_SESSION['narocila_filter']['neplacana']) && $_SESSION['narocila_filter']['neplacana'] == '0')
$this->narocila_filter['neplacana'] = 0;
if(isset($_SESSION['narocila_filter']['placana']) && $_SESSION['narocila_filter']['placana'] == '1')
$this->narocila_filter['placana'] = 1;
if(isset($_SESSION['narocila_filter']['stornirana']) && $_SESSION['narocila_filter']['stornirana'] == '1')
$this->narocila_filter['stornirana'] = 1;
}
// Dobimo podatke zadnjega narocila za dolocenega uporabnika
public function getNarocilo($narocilo_id){
global $admin_type;
$return = array();
// Dobimo narocilo uporabnika
$sqlNarocilo = sisplet_query("SELECT un.*, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, user_access_paket up
WHERE un.id='".$narocilo_id."' AND un.package_id=up.id
");
// Uporabnik nima se nobenega narocila
if(mysqli_num_rows($sqlNarocilo) == 0){
$return['id'] = '0';
}
else{
$row = mysqli_fetch_array($sqlNarocilo);
$return = $row;
// Cena
$cena = $this->getPrice($row['package_name'], $row['trajanje'], $row['discount'], $row['time']);
// Zavezanec iz tujine nima ddv-ja
if($this->isWithoutDDV($row['id']))
$return['koncna_cena'] = $cena['final_without_tax'];
else
$return['koncna_cena'] = $cena['final'];
}
return $return;
}
// Dobimo array narocil (vseh oz. za dolocenega uporabnika)
private function getNarocila($usr_id=0){
global $admin_type;
$narocila = array();
// Vsa narocila lahko pregledujejo samo admini
if($admin_type == 0 && $usr_id == 0){
// Filter po statusu
$status = ' AND un.status IN (';
$status .= ($this->narocila_filter['neplacana'] == 1) ? '0,' : '';
$status .= ($this->narocila_filter['placana'] == 1) ? '1,' : '';
$status .= ($this->narocila_filter['stornirana'] == 1) ? '2,' : '';
$status = substr($status, 0, -1);
$status .= ') ';
// Loop po vseh narocilih v sistemu
$sqlNarocilo = sisplet_query("SELECT un.*, u.name, u.surname, u.email, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, users u, user_access_paket up
WHERE un.usr_id=u.id AND un.package_id=up.id ".$status."
ORDER BY un.id DESC
");
while($rowNarocilo = mysqli_fetch_array($sqlNarocilo)){
$narocila[] = $rowNarocilo;
}
}
elseif($usr_id > 0){
// Loop po vseh narocilih uporabnika
$sqlNarocilo = sisplet_query("SELECT un.*, u.name, u.surname, u.email, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, users u, user_access_paket up
WHERE un.usr_id='".$usr_id."' AND un.usr_id=u.id AND un.package_id=up.id
ORDER BY un.id DESC
");
while($rowNarocilo = mysqli_fetch_array($sqlNarocilo)){
$narocila[] = $rowNarocilo;
}
}
return $narocila;
}
// Dobimo podatke zadnjega narocila za dolocenega uporabnika
public function getLastNarocilo($usr_id){
global $admin_type;
$return = array();
// Dobimo zadnje narocilo uporabnika
$sqlNarocilo = sisplet_query("SELECT un.*, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, user_access_paket up
WHERE un.usr_id='".$usr_id."' AND un.package_id=up.id
ORDER BY un.time DESC
");
// Uporabnik nima se nobenega narocila
if(mysqli_num_rows($sqlNarocilo) == 0){
$return['id'] = '0';
}
else{
$return = mysqli_fetch_array($sqlNarocilo);
}
return $return;
}
// Izracunamo koncno ceno glede na paket, trajanje in popust (v eur)
public function getPrice($package_name, $trajanje, $discount=0, $time=''){
$cena = array();
// Mesecna cena paketa
$package_price = $this->packages[$package_name]['price'];
// Narocila pred 7.12. morajo imeti stare cene
if($time != '' && strtotime($time) < strtotime('2020-12-06 20:00:00')){
// Mesecno ceno zmanjsamo glede na trajanje
if($package_name == '2ka'){
if((int)$trajanje >= 12){
$cena['monthly'] = number_format(11.90 - 2, 2, '.', '');
}
elseif((int)$trajanje >= 3){
$cena['monthly'] = number_format(11.90 - 1, 2, '.', '');
}
else{
$cena['monthly'] = number_format(11.90, 2, '.', '');
}
}
elseif($package_name == '3ka'){
if((int)$trajanje >= 12){
$cena['monthly'] = number_format(21.90 - 2, 2, '.', '');
}
elseif((int)$trajanje >= 3){
$cena['monthly'] = number_format(21.90 - 1, 2, '.', '');
}
else{
$cena['monthly'] = number_format(21.90, 2, '.', '');
}
}
}
else{
// Mesecno ceno zmanjsamo glede na trajanje
if($package_name == '2ka'){
if((int)$trajanje >= 12){
$cena['monthly'] = number_format($package_price - 4, 2, '.', '');
}
elseif((int)$trajanje >= 3){
$cena['monthly'] = number_format($package_price - 2, 2, '.', '');
}
else{
$cena['monthly'] = number_format($package_price, 2, '.', '');
}
}
elseif($package_name == '3ka'){
if((int)$trajanje >= 12){
$cena['monthly'] = number_format($package_price - 3, 2, '.', '');
}
elseif((int)$trajanje >= 3){
$cena['monthly'] = number_format($package_price - 1.5, 2, '.', '');
}
else{
$cena['monthly'] = number_format($package_price, 2, '.', '');
}
}
}
// Se brez davka za monthly
$cena['monthly_without_tax'] = number_format(floatval($cena['monthly']) / 1.22, 2, '.', '');
// Cena za celotno obdobje
$cena['full'] = number_format((int)$trajanje * floatval($cena['monthly']), 2, '.', '');
// Se brez davka za full
$cena['full_without_tax'] = number_format(floatval($cena['full']) / 1.22, 2, '.', '');
// Cena s popustom
$cena['full_discount'] = $cena['full'];
$cena['discount'] = $discount;
// Odstejemo se popust ce je posebej nastavljen
if($discount != 0){
// Ce je popust vecji od celotnega zneska, je cena 0 (cena ne more biti negativna)
if($discount > $cena['full']){
$cena['full_discount'] = 0;
$cena['discount'] = $cena['full'];
}
else{
//$cena['full_discount'] = number_format(floatval($cena['full_discount']) - (floatval($discount) * floatval($cena['full']) / 100), 2, '.', '');
$cena['full_discount'] = number_format(floatval($cena['full_discount']) - floatval($discount), 2, '.', '');
}
}
// Dodatno se izracunamo popust v %
if($cena['full'] > 0)
$cena['discount_percentage'] = round(floatval($cena['discount']) / floatval($cena['full']) * 100);
else
$cena['discount_percentage'] = 0;
// Se davek
$cena['final_without_tax'] = number_format(floatval($cena['full_discount']) / 1.22, 2, '.', '');
$cena['tax'] = number_format($cena['full_discount'] - $cena['final_without_tax'], 2, '.', '');
$cena['final'] = $cena['full_discount'];
return $cena;
}
// Izracunamo popust glede na uporabnika (pri upgradu / downgradu paketa)
public function getDiscount($usr_id, $package_name, $trajanje){
// Dobimo trenuten dostop userja
$ua = UserAccess::getInstance($usr_id);
$user_access = $ua->getAccess();
// Dobimo polno ceno za paket
$price = $this->getPrice($package_name, $trajanje);
// UPGRADE oz. DOWNGRADE - iz 2ka na 3ka ali iz 3ka na 2ka
if( isset($user_access['package_name']) && (($user_access['package_name'] == '2ka' && $package_name == '3ka') || ($user_access['package_name'] == '3ka' && $package_name == '2ka')) ){
// Mesecna cena obstojecega paketa
$package_price = floatval($this->packages[$user_access['package_name']]['price']);
// Stevilo dni dokler je obstojeci paket se veljaven
$now = time();
$expire = strtotime($user_access['time_expire']);
$expire_in_days = floor(($expire - $now) / (60 * 60 * 24));
// Popravimo ceno, ce ima veljaven paket se za 3 mesece ali vec (pomeni, da je imel popust pri nakupu kar upostevamo)
if($package_name == '2ka'){
if($expire_in_days > 92){
$package_price = number_format($package_price - 4, 2, '.', '');
}
elseif($expire_in_days > 31){
$package_price = number_format($package_price - 2, 2, '.', '');
}
}
elseif($package_name == '3ka'){
if($expire_in_days > 92){
$package_price = number_format($package_price - 3, 2, '.', '');
}
elseif($expire_in_days > 31){
$package_price = number_format($package_price - 1.5, 2, '.', '');
}
}
// Popust izracunamo kot delez cene paketa in
$dayly_discount = number_format($package_price / 31, 2, '.', '');
$discount = number_format($dayly_discount * $expire_in_days, 2, '.', '');
return $discount;
}
// Drugace nimamo nobenega popusta
else{
return 0;
}
}
// Preverimo, ce narocilo slucajno nima ddv-ja (zavezanec za ddv iz tujine)
public function isWithoutDDV($narocilo_id){
$sqlNarocilo = sisplet_query("SELECT podjetje_drzava, podjetje_no_ddv
FROM user_access_narocilo
WHERE id='".$narocilo_id."'
");
if(mysqli_num_rows($sqlNarocilo) == 1){
$rowNarocilo = mysqli_fetch_array($sqlNarocilo);
// Slovenija ima vedno ddv
if($rowNarocilo['podjetje_drzava'] == 'Slovenija' || $rowNarocilo['podjetje_drzava'] == 'Slovenia'){
return false;
}
// Ce ni iz slovenije in ima oznaceno da ne placa ddv-ja
if($rowNarocilo['podjetje_no_ddv'] == '1'){
return true;
}
}
return false;
}
// Dobimo jezik narocila - v istem jeziku so potem emaili in racun/predracun
public function getNarociloLanguage($narocilo_id){
$sqlNarocilo = sisplet_query("SELECT language
FROM user_access_narocilo
WHERE id='".$narocilo_id."'
");
if(mysqli_num_rows($sqlNarocilo) == 1){
$rowNarocilo = mysqli_fetch_array($sqlNarocilo);
if($rowNarocilo['language'] == 'sl'){
return 'si';
}
else{
return 'en';
}
}
return 'en';
}
// Izpisemo podatke o narocilih uporabnika
public function displayNarocila(){
global $lang, $global_user_id;
// Podatki o trenutnem paketu uporabnika
echo '
';
// Tabela vseh narocil uporabnika
echo '';
}
// Izpisemo seznam vseh narocil uporabnika
public function displayNarocilaTable(){
global $lang, $global_user_id;
// Dobimo vsa narocila uporabnika
$data = $this->getNarocila($global_user_id);
echo '
';
}
// Izpisemo seznam vseh narocil - admin
public function displayNarocilaTableAdmin(){
global $lang, $global_user_id;
// Admini vidijo vsa narocila
$data = $this->getNarocila();
// Filtri po statusu
echo '
';
// Ce je bila cena 0 je bil avtomatsko "placan" in nima racuna oz. predracuna
if($cena['final'] == 0){
echo '/';
}
else{
echo ''.$lang['srv_narocilo_pdf_predracun'].'';
if($data_row['status'] == '1'){
echo ' | '.$lang['srv_narocilo_pdf_racun'].'';
}
}
echo '
';
// Se inicializiramo dataTable jquery
echo '';
}
// Prikazemo popup za pregled in urejanje narocilo
private function displayNarociloEdit($narocilo_id){
global $lang;
// Loop po vseh narocilih uporabnika
$sqlNarocilo = sisplet_query("SELECT un.*, u.name, u.surname, u.email, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, users u, user_access_paket up
WHERE un.id='".$narocilo_id."' AND un.usr_id=u.id AND un.package_id=up.id
");
if(mysqli_num_rows($sqlNarocilo) == 0){
echo 'Naročilo ne obstaja!';
return;
}
$rowNarocilo = mysqli_fetch_array($sqlNarocilo);
echo '
';
}
// Ustvari novo narocilo za uporabnika
public function createNarocilo($narocilo_data){
global $global_user_id;
global $lang;
$response = array();
if(isset($narocilo_data['email'])){
$uporabnik = sisplet_query("SELECT id, email FROM users WHERE email='".$narocilo_data['email']."'", "obj");
$usr_id = $uporabnik->id;
}
$status = isset($narocilo_data['status']) ? $narocilo_data['status'] : 0;
$package_id = isset($narocilo_data['package_id']) ? $narocilo_data['package_id'] : 1;
$payment_method = isset($narocilo_data['payment_method']) ? $narocilo_data['payment_method'] : 0;
//$discount = isset($narocilo_data['discount']) ? $narocilo_data['discount'] : 0;
$trajanje = isset($narocilo_data['trajanje']) ? $narocilo_data['trajanje'] : 0;
$ime_na_racunu = isset($narocilo_data['ime']) ? $narocilo_data['ime'] : '';
$phone = isset($narocilo_data['phone']) ? $narocilo_data['phone'] : '';
$podjetje_ime = isset($narocilo_data['podjetje_ime']) ? $narocilo_data['podjetje_ime'] : '';
$podjetje_naslov = isset($narocilo_data['podjetje_naslov']) ? $narocilo_data['podjetje_naslov'] : '';
$podjetje_postna = isset($narocilo_data['podjetje_postna']) ? $narocilo_data['podjetje_postna'] : '';
$podjetje_posta = isset($narocilo_data['podjetje_posta']) ? $narocilo_data['podjetje_posta'] : '';
$podjetje_drzava = isset($narocilo_data['podjetje_drzava']) ? $narocilo_data['podjetje_drzava'] : '';
$podjetje_davcna = isset($narocilo_data['podjetje_davcna']) ? $narocilo_data['podjetje_davcna'] : '';
$podjetje_eracun = isset($narocilo_data['podjetje_eracun']) ? '1' : '0';
$language = isset($narocilo_data['lang']) ? $narocilo_data['lang'] : 'sl';
if($usr_id <= 0){
$response['error'] = 'ERROR! Missing user ID.';
$response['success'] = false;
return $response;
}
// Ce je slucajno drzava prazna jo nastavimo na slovenijo - zankrat pustimo, da vidimo, ce se se kdaj poslje prazno polje (naceloma se nebi smelo)
/*if($podjetje_drzava == '')
$podjetje_drzava = 'Slovenija';*/
// Nastavimo ce placa DDV (zavezanci iz EU ga ne placajo)
if(self::checkPayDDV($podjetje_davcna, $podjetje_drzava))
$podjetje_no_ddv = '0';
else
$podjetje_no_ddv = '1';
$brezplacen_preklop = false;
// Preverimo, ce ima uporabnik ze aktiven paket - po novem lahko to predhodno preklaplja, ker se to preracuna v popust
$sqlAccess = sisplet_query("SELECT * FROM user_access WHERE usr_id='".$usr_id."' AND package_id != '1' AND time_expire > NOW()");
if(mysqli_num_rows($sqlAccess) > 0){
$rowAccess = mysqli_fetch_array($sqlAccess);
// Ce zeli uporabnik kupiti drug placljiv paket kot ga ima trenutno, preracunamo obstojec paket v popust
if($rowAccess['package_id'] != $package_id){
$sqlPackage = sisplet_query("SELECT name FROM user_access_paket WHERE id='".$package_id."'");
$rowPackage = mysqli_fetch_array($sqlPackage);
$discount = $this->getDiscount($usr_id, $rowPackage['name'], $trajanje);
// Preverimo, ce je cena slucajno 0 - oznacimo, da gre za brezplacen preklop
$cena = $this->getPrice($rowPackage['name'], $trajanje, $discount);
if($cena['final'] == 0){
$brezplacen_preklop = true;
// Dodamo piškotek, če gre za brezplačni nakup
global $cookie_domain;
setcookie('brezplacen_preklop', 1, time()+1800, '/', $cookie_domain);
}
}
}
$sqlNarocilo = sisplet_query("INSERT INTO user_access_narocilo
(usr_id, status, time, package_id, ime, payment_method, discount, trajanje, phone, podjetje_ime, podjetje_naslov, podjetje_postna, podjetje_posta, podjetje_drzava, podjetje_davcna, podjetje_no_ddv, podjetje_eracun, language)
VALUES
('".$usr_id."', '".$status."', NOW(), '".$package_id."', '".$ime_na_racunu."', '".$payment_method."', '".$discount."', '".$trajanje."', '".$phone."', '".$podjetje_ime."', '".$podjetje_naslov."', '".$podjetje_postna."', '".$podjetje_posta."', '".$podjetje_drzava."', '".$podjetje_davcna."', '".$podjetje_no_ddv."', '".$podjetje_eracun."', '".$language."')
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
$narocilo_id = mysqli_insert_id($GLOBALS['connect_db']);
// Ce gre za brezplacen preklop izvedemo placilo (aktivacijo paketa) brez izdaje racuna, maila...
if($brezplacen_preklop){
$response = $this->payNarocilo($narocilo_id, $brezplacen_preklop);
}
// Drugace izdamo predracun oz. placamo s kartico/paypalom
else{
// Glede na tip plačila dobmo ustrezen url (predracun, paypal, kartica)
// Paypal
if($payment_method == '2'){
$response = $this->finishNarociloPaypal($narocilo_id, $narocilo_data);
}
// Kartica
elseif($payment_method == '3'){
$response = $this->finishNarociloStripe($narocilo_id, $narocilo_data);
}
// Predracun
else{
$response = $this->finishNarociloPredracun($narocilo_id, $narocilo_data);
}
}
return $response;
}
// Dokoncaj narocilo s placilom preko predracuna
private function finishNarociloPredracun($narocilo_id, $narocilo_data){
global $lang;
$response = array();
$response['narocilo_id'] = $narocilo_id;
$cebelica = new UserNarocilaCebelica($narocilo_id);
$response['payment_link'] = $cebelica->getNarociloPredracun();
// Posljemo mail s predracunom
$subject = $lang['srv_narocilo_email_predracun_subject'].' '.$narocilo_id;
$content = $lang['srv_narocilo_email_predracun_content1'];
$content .= '
'.$lang['srv_narocilo_email_predracun_content2'];
$content .= ' '.$lang['srv_narocilo_email_predracun_file'].'';
// Podpis
$signature = Common::getEmailSignature();
$content .= $signature;
try{
$MA = new MailAdapter($anketa=null, $type='payments');
$MA->addRecipients($narocilo_data['email']);
// Dodamo predracun v attachment
$MA->addAttachment(file_get_contents($response['payment_link']), $file_name='1ka_narocilo_'.$narocilo_id.'_predracun.pdf');
// Posljemo mail
$resultX = $MA->sendMail($content, $subject);
$response['success'] = true;
}
catch (Exception $e){
$response['error'] = 'ERROR! Sending email with invoice failed.';
$response['success'] = false;
return $response;
}
return $response;
}
// Dokoncaj narocilo s placilom preko predracuna
private function finishNarociloStripe($narocilo_id, $narocilo_data){
global $lang;
$response = array();
// Inicializiramo paypal
$stripe = new UserNarocilaStripe($narocilo_id);
// Ustvarimo stripe session za placilo in vrnemo id sessiona, da uporabnik potrdi placilo
$stripe_response = $stripe->stripeCreateSession();
// Ce je bilo placilo preko stripa uspesno zgeneriramo racun in uporabniku aktiviramo paket
if($stripe_response['success'] == true){
$response['session_id'] = $stripe_response['session_id'];
$response['success'] = true;
}
else{
$response['error'] = $stripe_response['error'];
$response['success'] = false;
}
return $response;
}
// Dokoncaj narocilo s placilom preko predracuna
private function finishNarociloPaypal($narocilo_id, $narocilo_data){
global $lang;
$response = array();
// Inicializiramo paypal
$paypal = new UserNarocilaPaypal($narocilo_id);
// Ustvarimo paypal placilo in vrnemo url, da se uporabnik prijavi v paypal in potrdi placilo
$paypal_response = $paypal->paypalCreatePayment();
// Ce je bilo placilo preko stripa uspesno zgeneriramo racun in uporabniku aktiviramo paket
if($paypal_response['success'] == true){
$response['paypal_link'] = $paypal_response['paypal_link'];
$response['success'] = true;
}
else{
$response['error'] = $paypal_response['error'];
$response['success'] = false;
}
return $response;
}
// Posodobi obstojece narocilo za uporabnika
public function updateNarocilo($narocilo_data){
global $global_user_id;
$response = array();
// ce nimamo id-ja narocila vrnemo error
if(!isset($narocilo_data['narocilo_id']) || $narocilo_data['narocilo_id'] == '0'){
$response['error'] = 'Napaka! Manjka ID narocila!';
$response['success'] = false;
return $response;
}
$update = '';
$update .= isset($narocilo_data['status']) ? ', status='.$narocilo_data['status'] : '';
$update .= isset($narocilo_data['package_id']) ? ', package_id='.$narocilo_data['package_id'] : '';
$update .= isset($narocilo_data['payment_method']) ? ', payment_method='.$narocilo_data['payment_method'] : '';
$update .= isset($narocilo_data['discount']) ? ', discount='.$narocilo_data['discount'] : '';
$update .= isset($narocilo_data['ime']) ? ', ime='.$narocilo_data['ime'] : '';
$update .= isset($narocilo_data['trajanje']) ? ', trajanje='.$narocilo_data['trajanje'] : '';
$update .= isset($narocilo_data['phone']) ? ', phone='.$narocilo_data['phone'] : '';
$update .= isset($narocilo_data['podjetje_ime']) ? ', podjetje_ime='.$narocilo_data['podjetje_ime'] : '';
$update .= isset($narocilo_data['podjetje_naslov']) ? ', podjetje_naslov='.$narocilo_data['podjetje_naslov'] : '';
$update .= isset($narocilo_data['podjetje_postna']) ? ', podjetje_postna='.$narocilo_data['podjetje_postna'] : '';
$update .= isset($narocilo_data['podjetje_posta']) ? ', podjetje_posta='.$narocilo_data['podjetje_posta'] : '';
$update .= isset($narocilo_data['podjetje_drzava']) ? ', podjetje_drzava='.$narocilo_data['podjetje_drzava'] : '';
$update .= isset($narocilo_data['podjetje_davcna']) ? ', podjetje_davcna='.$narocilo_data['podjetje_davcna'] : '';
$update .= isset($narocilo_data['podjetje_no_ddv']) ? ', podjetje_no_ddv='.$narocilo_data['podjetje_no_ddv'] : '';
$update .= isset($narocilo_data['podjetje_eracun']) ? ', podjetje_eracun='.$narocilo_data['podjetje_eracun'] : '';
$update = substr($update, 1);
// Update narocila in pobrisemo id racuna in predracuna, ker ga moramo generirati na novo
$sqlNarocilo = sisplet_query("UPDATE user_access_narocilo SET ".$update.", cebelica_id_racun='0', cebelica_id_predracun='0' WHERE id='".$narocilo_data['narocilo_id']."'");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
$response['success'] = true;
return $response;
}
// Placa obstojece narocilo, uporabniku aktivira paket, zgenerira racun in ga poslje po mailu
public function payNarocilo($narocilo_id, $brezplacen_preklop=false){
global $global_user_id;
global $lang;
$response = array();
// Ce nimamo id-ja narocila vrnemo error
if($narocilo_id == 0){
$response['error'] = 'Napaka! Manjka ID narocila!';
$response['success'] = false;
return $response;
}
// Dobimo podatke narocila
$sqlNarocilo = sisplet_query("SELECT n.*, u.email FROM user_access_narocilo n, users u WHERE n.id='".$narocilo_id."' AND u.id=n.usr_id");
$rowNarocilo = mysqli_fetch_array($sqlNarocilo);
// Ce je bil racun ze placan ne naredimo nicesar
if($rowNarocilo['status'] == 1){
$response['error'] = 'Napaka! Račun je že plačan!';
$response['success'] = false;
return $response;
}
// Nastavimo ustrezen jezik - mail mora biti v istem jeziku kot je bilo narocilo
if($rowNarocilo['language'] == 'en'){
include('../../lang/2.php');
}
// Preverimo, ce ima uporabnik ze aktiven paket in ce je ta paket isti kot ta, ki ga je kupil
$sqlAccessCheck = sisplet_query("SELECT * FROM user_access WHERE usr_id='".$rowNarocilo['usr_id']."' AND package_id != '1' AND time_expire > NOW()");
if(mysqli_num_rows($sqlAccessCheck) > 0){
$rowAccessCheck = mysqli_fetch_array($sqlAccessCheck);
// Ce zeli uporabnik kupiti drug placljiv paket kot ga ima trenutno, ga zavrnemo - po novem normalno izvedemo ker preracunamo v popust
if($rowAccessCheck['package_id'] != $rowNarocilo['package_id']){
// Nastavimo dostop uporabniku
$sqlAccess = sisplet_query("INSERT INTO user_access
(usr_id, time_activate, time_expire, package_id)
VALUES
('".$rowNarocilo['usr_id']."', NOW(), NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, '".$rowNarocilo['package_id']."')
ON DUPLICATE KEY UPDATE
time_activate=NOW(), time_expire=NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, package_id='".$rowNarocilo['package_id']."'
");
if (!$sqlAccess){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
// Uporabnik kupuje isti paket kot ga ze ima - mu ga samo podaljsamo
else{
$sqlAccess = sisplet_query("UPDATE user_access SET time_expire = time_expire + INTERVAL '".$rowNarocilo['trajanje']."' MONTH WHERE usr_id='".$rowNarocilo['usr_id']."'");
if (!$sqlAccess){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
}
else{
// Nastavimo dostop uporabniku
$sqlAccess = sisplet_query("INSERT INTO user_access
(usr_id, time_activate, time_expire, package_id)
VALUES
('".$rowNarocilo['usr_id']."', NOW(), NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, '".$rowNarocilo['package_id']."')
ON DUPLICATE KEY UPDATE
time_activate=NOW(), time_expire=NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, package_id='".$rowNarocilo['package_id']."'
");
if (!$sqlAccess){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
// Nastavimo status narocila na placan
$sqlNarociloStatus = sisplet_query("UPDATE user_access_narocilo SET status='1' WHERE id='".$narocilo_id."'");
if (!$sqlNarociloStatus){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
// Brezplacen preklop - samo posljemo mail z obvestilom o vklopu paketa
if($brezplacen_preklop){
// Posljemo mail z obvestilom
$subject = $lang['srv_narocilo_free_email_subject'].' '.$rowNarocilo['id'];
$content = $lang['srv_narocilo_free_email_content1'];
$content .= '
'.$lang['srv_narocilo_free_email_content2'];
// Podpis
$signature = Common::getEmailSignature();
$content .= $signature;
try{
$MA = new MailAdapter($anketa=null, $type='payments');
$MA->addRecipients($rowNarocilo['email']);
$resultX = $MA->sendMail($content, $subject);
}
catch (Exception $e){
$response['false'] = true;
}
}
// Ce ne gre za brezplacen preklop zapisemo v placila, izdamo racun in posljemo mail
else{
// Ustvarimo placilo v tabeli placil
$up = new UserPlacila();
$up->createPlacilo($rowNarocilo);
// Ustvarimo racun
try{
$cebelica = new UserNarocilaCebelica($narocilo_id);
$response['racun'] = $cebelica->getNarociloRacun();
}
// Napaka pri komunikaciji s cebelco - posljemo mail Goranu
catch (Exception $e){
// Posljemo mail Goranu, da je prislo do napake pri generiranju racuna
$subject = '1ka - napaka pri generiranju računa';
$content = 'Prišlo je do napake pri generiranju računa (komunikacija s čebelco) pri naročilu št. '.$rowNarocilo['id'].' (uporabnik '.$rowNarocilo['email'].')!';
$content .= '
Preveri in samo še enkrat odpri račun za to plačilo preko vmesnika (ga bo zgeneriralo avtomatsko).';
$MA = new MailAdapter($anketa=null, $type='payments');
$MA->addRecipients('goran@go-tel.si');
$MA->addRecipients('peter@1ka.si');
$resultX = $MA->sendMail($content, $subject);
$response['false'] = true;
return $response;
}
// Posljemo mail z racunom
$subject = $lang['srv_narocilo_email_subject'].' '.$rowNarocilo['id'];
$content = $lang['srv_narocilo_email_content1'];
$content .= '
'.$lang['srv_narocilo_email_content2'];
$content .= ' '.$lang['srv_narocilo_email_file'].'';
// Podpis
$signature = Common::getEmailSignature();
$content .= $signature;
try{
$MA = new MailAdapter($anketa=null, $type='payments');
$MA->addRecipients($rowNarocilo['email']);
// Dodamo predracun v attachment
$MA->addAttachment(file_get_contents($response['racun']), $file_name='1ka_narocilo_'.$rowNarocilo['id'].'_racun.pdf');
$resultX = $MA->sendMail($content, $subject);
}
catch (Exception $e){
$response['false'] = true;
}
}
$response['success'] = true;
return $response;
}
// Placa narocilo - za eracune, kjer se jih zabelezi kot placane ampak imajo 30 dnevni rok
public function payNarociloEracun($narocilo_id){
global $global_user_id;
global $lang;
$response = array();
// Ce nimamo id-ja narocila vrnemo error
if($narocilo_id == 0){
$response['error'] = 'Napaka! Manjka ID narocila!';
$response['success'] = false;
return $response;
}
// Dobimo podatke narocila
$sqlNarocilo = sisplet_query("SELECT n.*, u.email FROM user_access_narocilo n, users u WHERE n.id='".$narocilo_id."' AND u.id=n.usr_id");
$rowNarocilo = mysqli_fetch_array($sqlNarocilo);
// Ce je bil racun ze placan ne naredimo nicesar
if($rowNarocilo['status'] == 1){
$response['error'] = 'Napaka! Račun je že plačan!';
$response['success'] = false;
return $response;
}
// Nastavimo ustrezen jezik - mail mora biti v istem jeziku kot je bilo narocilo
if($rowNarocilo['language'] == 'en'){
include('../../lang/2.php');
}
// Preverimo, ce ima uporabnik ze aktiven paket in ce je ta paket isti kot ta, ki ga je kupil
$sqlAccessCheck = sisplet_query("SELECT * FROM user_access WHERE usr_id='".$rowNarocilo['usr_id']."' AND package_id != '1' AND time_expire > NOW()");
if(mysqli_num_rows($sqlAccessCheck) > 0){
$rowAccessCheck = mysqli_fetch_array($sqlAccessCheck);
// Ce zeli uporabnik kupiti drug placljiv paket kot ga ima trenutno, ga zavrnemo - po novem normalno izvedemo ker preracunamo v popust
if($rowAccessCheck['package_id'] != $rowNarocilo['package_id']){
// Nastavimo dostop uporabniku
$sqlAccess = sisplet_query("INSERT INTO user_access
(usr_id, time_activate, time_expire, package_id)
VALUES
('".$rowNarocilo['usr_id']."', NOW(), NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, '".$rowNarocilo['package_id']."')
ON DUPLICATE KEY UPDATE
time_activate=NOW(), time_expire=NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, package_id='".$rowNarocilo['package_id']."'
");
if (!$sqlAccess){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
// Uporabnik kupuje isti paket kot ga ze ima - mu ga samo podaljsamo
else{
$sqlAccess = sisplet_query("UPDATE user_access SET time_expire = time_expire + INTERVAL '".$rowNarocilo['trajanje']."' MONTH WHERE usr_id='".$rowNarocilo['usr_id']."'");
if (!$sqlAccess){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
}
else{
// Nastavimo dostop uporabniku
$sqlAccess = sisplet_query("INSERT INTO user_access
(usr_id, time_activate, time_expire, package_id)
VALUES
('".$rowNarocilo['usr_id']."', NOW(), NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, '".$rowNarocilo['package_id']."')
ON DUPLICATE KEY UPDATE
time_activate=NOW(), time_expire=NOW() + INTERVAL '".$rowNarocilo['trajanje']."' MONTH, package_id='".$rowNarocilo['package_id']."'
");
if (!$sqlAccess){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
// Nastavimo status narocila na placan
$sqlNarociloStatus = sisplet_query("UPDATE user_access_narocilo SET status='1' WHERE id='".$narocilo_id."'");
if (!$sqlNarociloStatus){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
// Ustvarimo placilo v tabeli placil
$up = new UserPlacila();
$up->createPlacilo($rowNarocilo, $eracun=true);
// Ustvarimo racun - nima "markPayed"!
$cebelica = new UserNarocilaCebelica($narocilo_id);
$response['racun'] = $cebelica->getNarociloRacun($eracun=true);
// Posljemo mail z racunom
$subject = $lang['srv_narocilo_email_subject'].' '.$rowNarocilo['id'];
$content = $lang['srv_narocilo_email_content1'];
$content .= '