summaryrefslogtreecommitdiffstats
path: root/frontend/payments/classes/class.UserNarocilaCron.php
blob: 92ff8eb514da1503c0ca769c7c04ab1181e457f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

/**
 *
 *  Class ki skrbi za opomnike trgoovine (cronjob)
 *
 *  Cron jobi nej bodo 3. (ob 9h zjutraj) 
 *  1. 6 dni prej k zgenerira predračun. Vsebina v smislu: vaša naročnina se bo kmalu iztekla. Zagotovite si neomejeno uporabo 2ka/3ka še naprej s plačilom predračuna v priponki. 
 *  2. 1 dan pred potekom. Vsebina v smislu: Danes je zadnji dan veljavnosti vašega paketa. Zagotovite si delovanje 1ka še naprej ...
 *  3. En dan po poteku. Vsebina v smislu: Vaša naročnina se je iztekla. Nov paket lahko naročite na 1ka.si
 *
*/


class UserNarocilaCron{


    public function __construct(){

    }


    /**
	* izvede ustrezno akcijo
	*/
	public function executeAction($action = '') {
		
		// Izvedemo ustrezno akcijo	
		if(method_exists('UserNarocilaCron', $action) && $action != '')
			$this->$action();
		else
			echo 'Method '.$action.' does not exist!';
	}


    // Mail 6 dni pred potekom narocnine
    private function notifyIn6(){

        $expired_users = $this->getExpiredUsers($expire='in6');

        foreach($expired_users as $usr_id => $user){

            $narocilo = $this->getLastNarocilo($usr_id);

            // Nastavimo ustrezen jezik - mail mora biti v istem jeziku kot je bilo zadnje narocilo
            if($narocilo['language'] == 'en')
                include('../lang/2.php');
            else
                include('../lang/1.php');


            // Posljemo mail
            $subject = $lang['srv_access_expire_in6_subject'];  
            $content = str_replace('#PACKAGE_NAME#', $narocilo['package_id'].'ka', $lang['srv_access_expire_in6_content1']);
            $content .= str_replace('#PACKAGE_ID#', $narocilo['package_id'], $lang['srv_access_expire_in6_content2']);
            $content .= $lang['srv_access_expire_in6_content3'] . $user['email'];

            // Podpis
            $signature = Common::getEmailSignature();
            $content .= $signature;

            try{
                $MA = new MailAdapter();
                $MA->addRecipients($user['email']);
                $resultX = $MA->sendMail($content, $subject);
            }
            catch (Exception $e){
            }
        }
        
    }

    // Mail 1 dan pred potekom narocnine
    private function notifyIn1(){

        $expired_users = $this->getExpiredUsers($expire='in1');

        foreach($expired_users as $usr_id => $user){

            $narocilo = $this->getLastNarocilo($usr_id);

            // Nastavimo ustrezen jezik - mail mora biti v istem jeziku kot je bilo zadnje narocilo
            if($narocilo['language'] == 'en')
                include('../lang/2.php');
            else
                include('../lang/1.php');

                
            // Posljemo mail
            $subject = $lang['srv_access_expire_in1_subject'];  
            $content = str_replace('#PACKAGE_NAME#', $narocilo['package_id'].'ka', $lang['srv_access_expire_in1_content1']);
            $content .= str_replace('#PACKAGE_ID#', $narocilo['package_id'], $lang['srv_access_expire_in1_content2']);
            $content .= $lang['srv_access_expire_in1_content3'] . $user['email'];

            // Podpis
            $signature = Common::getEmailSignature();
            $content .= $signature;

            try{
                $MA = new MailAdapter();
                $MA->addRecipients($user['email']);
                $resultX = $MA->sendMail($content, $subject);
            }
            catch (Exception $e){
            }
        }
    }

    // Mail 1 dan po poteku narocnine
    private function notifyExpired(){

        $expired_users = $this->getExpiredUsers($expire='expired');

        foreach($expired_users as $usr_id => $user){

            $narocilo = $this->getLastNarocilo($usr_id);

            // Nastavimo ustrezen jezik - mail mora biti v istem jeziku kot je bilo zadnje narocilo
            if($narocilo['language'] == 'en')
                include('../lang/2.php');
            else
                include('../lang/1.php');

                
            // Posljemo mail
            $subject = $lang['srv_access_expire_expired_subject'];  
            $content = str_replace('#PACKAGE_NAME#', $narocilo['package_id'].'ka', $lang['srv_access_expire_expired_content1']);
            $content .= str_replace('#PACKAGE_ID#', $narocilo['package_id'], $lang['srv_access_expire_expired_content2']);
            $content .= $lang['srv_access_expire_expired_content3'] . $user['email'];

            // Podpis
            $signature = Common::getEmailSignature();
            $content .= $signature;

            try{
                $MA = new MailAdapter();
                $MA->addRecipients($user['email']);
                $resultX = $MA->sendMail($content, $subject);
            }
            catch (Exception $e){
            }
        }
    }



    // Dobimo seznam uporabnikov, ki jim potece paket na dolocen dan
    private function getExpiredUsers($expire){

        if($expire == 'in6'){
            $interval_query = 'DATE(time_expire) = DATE(NOW() + INTERVAL 6 DAY)';
        }
        elseif($expire == 'in1'){
            $interval_query = 'DATE(time_expire) = DATE(NOW() + INTERVAL 1 DAY)';
        }
        if($expire == 'expired'){
            $interval_query = 'DATE(time_expire) = DATE(NOW() - INTERVAL 1 DAY)';
        }
        
        $result = array();

        // Loop po vseh uporabnikih, ki imajo zakupljen paket in jim potece cez 6 dni
        $sqlAccess = sisplet_query("SELECT a.*, u.email 
                                        FROM user_access a, users u 
                                        WHERE (a.package_id = 2 OR a.package_id = 3) 
                                            AND ".$interval_query."
                                            AND u.id=a.usr_id
                                    ");

        while($rowAccess = mysqli_fetch_array($sqlAccess)){
            $result[$rowAccess['usr_id']] = $rowAccess;
        }

        return $result;
    }

    // Dobimo zadnji placan paket uporabnika
    private function getLastNarocilo($usr_id){

        // Dobimo podatke zadnjega placanega narocila za tega uporabnika
        $sqlNarocilo = sisplet_query("SELECT * 
                                        FROM user_access_narocilo 
                                        WHERE usr_id='".$usr_id."' AND status='1'
                                        ORDER BY time DESC
                                        LIMIT 1
                                    ");

        // Uporabnik nima nobenega placanega paketa
        if(mysqli_num_rows($sqlNarocilo) == 0){
            return false;
        }

        $rowNarocilo = mysqli_fetch_array($sqlNarocilo);

        return $rowNarocilo;
    }
}