phpBMS

root/trunk/phpbms/modules/recurringinvoices/scheduler_recurr.php

Revision 771, 11.3 KB (checked in by brieb, 2 years ago)
  • fixed installtion issue with creating table with field type text and default value
  • changed paymentmethods type to varchar instead of enum
Line 
1<?php
2//uncomment if need debug
3//if(!class_exists("appError"))
4//      include_once("../../include/session.php");
5
6
7class recurr{
8
9        function recurr($db){
10                $this->db = $db;
11        }
12
13
14        function getInvoicesToRepeat($dateToCheck = NULL){
15                if($dateToCheck == NULL)
16                        $dateToCheck = mktime(0,0,0);
17
18                $invoiceList = array();
19
20                $querystatement = "
21                        SELECT
22                                `invoiceid`,
23                                `invoices`.`invoicedate`,
24                                `firstrepeat`,
25                                `lastrepeat`,
26                                `recurringinvoices`.`type`,
27                                `eachlist`,`every`,
28                                `ontheday`,`ontheweek`
29                        FROM
30                                `recurringinvoices` INNER JOIN `invoices` ON `recurringinvoices`.`invoiceid` = `invoices`.`uuid`
31                        WHERE
32                                `invoices`.`invoicedate` <= '".dateToString($dateToCheck,"SQL")."'
33                                AND
34                                (`recurringinvoices`.`until` IS NULL OR `recurringinvoices`.`until` >= '".dateToString($dateToCheck,"SQL")."')
35                                AND
36                                (`recurringinvoices`.`times` IS NULL OR `recurringinvoices`.`times` > `recurringinvoices`.`timesrepeated`)";
37
38                $queryresult = $this->db->query($querystatement);
39
40                while($therecord = $this->db->fetchArray($queryresult)){
41
42                        if($therecord["lastrepeat"])
43                                $startDate = stringToDate($therecord["lastrepeat"],"SQL");
44                        else
45                                $startDate = stringToDate($therecord["invoicedate"],"SQL");
46
47                        $dateArray = $this->getValidInRange($startDate,$dateToCheck,$therecord);
48
49                        if( in_array($dateToCheck, $dateArray))
50                                $invoiceList[] = $therecord["invoiceid"];
51
52                }//end while
53
54                return $invoiceList;
55
56        }//end method
57
58
59        function getValidInRange($startDate,$endDate,$therecord){
60                $nextDate = $startDate;
61
62                //should pad the end date to make sure we get all weekly repeats
63                $endDate = strtotime("+7 days",$endDate);
64
65                $validDates = array();
66
67                while($nextDate <= $endDate){
68
69                        switch($therecord["type"]){
70                                case "Daily":
71                                        //==================================================================================
72                                        $validDates[] = $nextDate;
73                                        $nextDate = strtotime("+".$therecord["every"]." days",$nextDate);
74                                        break;
75
76                                case "Weekly":
77                                        //==================================================================================
78                                        $weekDayArray = explode("::",$therecord["eachlist"]);
79
80                                        //need to start from the sunday of the current week
81                                        $tempDate = strtotime(nl_langinfo( constant("DAY_1") ),$nextDate);
82                                        $tempDate = strtotime("-7 days",$tempDate);
83
84                                        foreach($weekDayArray as $weekday){
85                                                if($weekday == 7)
86                                                        $validDates[]=$tempDate;
87                                                else{
88                                                        $weekday++;
89                                                        $validDates[] = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate);
90                                                }
91                                        }// endforeach
92
93
94                                        $nextDate = strtotime("+".$therecord["every"]." week",$nextDate);
95
96                                        break;
97
98                                case "Monthly":
99                                        //==================================================================================
100                                        $dateArray = localtime($nextDate,true);
101
102                                        if($therecord["eachlist"]){
103                                                $dayArray = explode("::",$therecord["eachlist"]);
104
105                                                foreach($dayArray as $theday)
106                                                        $validDates[] = mktime(0,0,0,$dateArray["tm_mon"]+1,$theday,$dateArray["tm_year"]+1900);
107
108                                        } else{
109                                                // check for things like second tuesday or last friday;
110                                                $tempDate = mktime(0,0,0,$dateArray["tm_mon"]+1,1,$dateArray["tm_year"]+1900);
111                                                $weekday = $therecord["ontheday"];
112                                                $weekday = ($weekday == 7)? 1: ($weekday+1);
113                                                if($therecord["ontheday"] != @strftime("%u",$tempDate));
114                                                        $tempDate = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate);
115
116                                                while(date("n",$tempDate) == ($dateArray["tm_mon"]+1)){
117
118                                                        if($therecord["ontheweek"] == 5){
119                                                                // 5 is the "last" option, so we just need to see if
120                                                                // the date falls in the last 6 days
121                                                                if($daysInMonth - date("d",$tempDate) < 7)
122                                                                        $validDates[] = $tempDate;
123
124                                                        } else {
125                                                                if( ceil(date("d",$tempDate)/7) == $therecord["ontheweek"])
126                                                                        $validDates[] = $tempDate;
127                                                        }// endif
128
129                                                        $tempDate = strtotime("+7 days",$tempDate);
130
131                                                }// endwhile
132                                        }//endif
133
134                                        $nextDate = strtotime("+".$therecord["every"]." months",$nextDate);
135                                        break;
136
137                                case "Yearly":
138                                        //==================================================================================
139                                        $monthArray = explode("::",$therecord["eachlist"]);
140                                        foreach($monthArray as $monthNum){
141                                                $dateArray = localtime($nextDate,true);
142                                                $daysInMonth = date("d", mktime(0,0,0,$monthNum,0,$dateArray["tm_year"]+1900) );
143
144                                                if(!$therecord["ontheday"]){
145                                                        $tempDay = ($dateArray["tm_mday"] > $daysInMonth)? $daysInMonth :$dateArray["tm_mday"];
146                                                        $validDates[] = mktime(0,0,0,$monthNum,$tempDay,$dateArray["tm_year"]+1900);
147
148                                                } else {
149                                                        // check for things like second tuesday or last friday;
150                                                        $tempDate = mktime(0,0,0,$monthNum,1,$dateArray["tm_year"]+1900);
151
152                                                        $weekday = $therecord["ontheday"];
153                                                        $weekday = ($weekday == 7)? 1: ($weekday+1);
154                                                        if($therecord["ontheday"] != @strftime("%u",$tempDate));
155                                                                $tempDate = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate);
156
157
158                                                        while(date("n",$tempDate) == $monthNum){
159                                                                if($therecord["ontheweek"] == 5){
160                                                                        // 5 is the "last" option, so we just need to see if
161                                                                        // the date falls in the last 6 days
162                                                                        if($daysInMonth - date("d",$tempDate) < 7)
163                                                                                $validDates[] = $tempDate;
164
165                                                                } else {
166                                                                        if( ceil(date("d",$tempDate)/7) == $therecord["ontheweek"])
167                                                                                $validDates[] = $tempDate;
168                                                                }// endif
169
170                                                                $tempDate = strtotime("+7 days",$tempDate);
171
172                                                        }// endwhile
173
174                                                }//endif
175
176                                        }//endforeach
177
178                                        $nextDate = strtotime("+".$therecord["every"]." years",$nextDate);
179
180                                        break;
181                        }//endswitch
182
183                }//end while
184
185                return $validDates;
186
187        }//end method
188
189
190        function copyInvoice($invoiceid){
191                $querystatement = "
192                        SELECT
193                                invoices.*,
194                                firstrepeat,
195                                includepaymenttype,
196                                recurringinvoices.id AS recurrid,
197                                recurringinvoices.statusid AS newstatusid,
198                                recurringinvoices.assignedtoid AS newassignedtoid,
199                                notificationroleid
200                        FROM
201                                invoices INNER JOIN recurringinvoices ON invoices.uuid = recurringinvoices.invoiceid
202                        WHERE
203                                invoices.uuid = '".$invoiceid."'
204                ";
205
206                $queryresult = $this->db->query($querystatement);
207
208                $therecord = $this->db->fetchArray($queryresult);
209
210                $fieldList = array();
211
212                foreach($therecord as $name=>$value){
213                        switch($name){
214                                case "id":
215                                case "notificationroleid":
216                                case "includepaymenttype":
217                                case "modifiedby":
218                                case "modifieddate":
219                                case "createdby":
220                                case "creationdate":
221                                case "newstatusid":
222                                case "newassignedtoid":
223                                case "firstrepeat":
224                                case "recurrid":
225                                case "bankname":
226                                case "ccnumber":
227                                case "routingnumber":
228                                case "ccexpiration":
229                                case "ccverification":
230                                case "accountnumber":
231                                        break;
232
233                                case "uuid":
234                                        $fieldlist[] = "uuid";
235                                        $therecord["uuid"] = uuid(getUuidPrefix($this->db, "tbld:62fe599d-c18f-3674-9e54-b62c2d6b1883").":");
236                                        break;
237
238                                case "checkno":
239                                case "webconfirmationno":
240                                case "trackingno":
241                                case "weborder":
242                                case "transactionid":
243                                case "invoicedate":
244                                        $fieldlist[] = $name;
245                                        $therecord[$name] = NULL;
246                                        break;
247
248                                case "statusdate":
249                                case "orderdate":
250                                        $fieldlist[] = $name;
251                                        $therecord[$name] = dateToString(mktime(),"SQL");
252                                        break;
253
254                                case "paymenttypeid":
255                                        $fieldlist[] = $name;
256                                        if(!$therecord["includepaymenttype"])
257                                                $therecord[$name] = NULL;
258                                        break;
259
260                                case "statusid":
261                                        $fieldlist[] = $name;
262                                        $therecord[$name] = $therecord["newstatusid"];
263                                        break;
264
265                                case "assignedtoid":
266                                        $fieldlist[] = $name;
267                                        $therecord[$name] = $therecord["newassignedtoid"];
268                                        break;
269
270                                case "amountpaid":
271                                        $fieldlist[] = $name;
272                                        $therecord[$name] = 0;
273                                        break;
274
275                                case "type":
276                                        $fieldlist[] = $name;
277                                        $therecord[$name] = "Order";
278                                        break;
279
280                                case "readytopost":
281                                        $fieldlist[] = $name;
282                                        $therecord[$name] = 0;
283                                        break;
284
285                                default:
286                                        $fieldlist[] = $name;
287                        }//endswitch
288                }//endforeach
289
290                $insertstatement = $this->prepareInsert("invoices",$fieldlist,$therecord);
291
292                $this->db->query($insertstatement);
293
294                $theid = $this->db->insertId();
295
296                $this->copyLineItems($therecord["id"], $theid);
297
298                $this->insertHistory($therecord["uuid"], $therecord["statusid"], $therecord["statusdate"], $therecord["assignedtoid"]);
299
300                $this->updateReccurence($therecord["recurrid"],$therecord["firstrepeat"]);
301
302                if($therecord["notificationroleid"])
303                        $this->sendNotification($therecord["notificationroleid"],$theid);
304
305        }//end method
306
307
308        function copyLineItems($oldInvoiceID, $newInvoiceID){
309
310                $querystatement = "SELECT * FROM lineitems WHERE invoiceid = '".$oldInvoiceID."'";
311                $queryresult = $this->db->query($querystatement);
312
313                while($therecord = $this->db->fetchArray($queryresult)){
314
315                        $fieldlist = array();
316
317                        foreach($therecord as $name=>$value){
318                                switch($name){
319                                        case "id":
320                                        case "modifiedby":
321                                        case "modifieddate":
322                                        case "createdby":
323                                        case "creationdate":
324                                                break;
325
326                                        case "invoiceid":
327                                                $therecord[$name] = $newInvoiceID;
328                                                $fieldlist[] = $name;
329                                                break;
330
331                                        default:
332                                                $fieldlist[] = $name;
333                                }// endswitch
334                        }// endforeach
335
336                        $insertstatement = $this->prepareInsert("lineitems",$fieldlist,$therecord);
337
338                        $this->db->query($insertstatement);
339
340                }// endwhile
341
342        }//end method
343
344
345        function prepareInsert($tablename, $fieldlist, $therecord){
346                $insertstatement = "INSERT INTO ".$tablename." (";
347
348                foreach($fieldlist as $name)
349                        $insertstatement .= "`".$name."`, ";
350
351                $insertstatement .= " createdby,creationdate) VALUES (";
352
353                foreach($fieldlist as $name)
354                        if($therecord[$name] !== NULL)
355                                $insertstatement .= "'".$therecord[$name]."', ";
356                        else
357                                $insertstatement .= "NULL, ";
358
359                $insertstatement .="-3, NOW());";
360
361                return $insertstatement;
362        }//end method
363
364
365        function insertHistory($invoiceid, $statusid, $statusdate, $assignedtoid){
366                $insertstatement = "INSERT INTO invoicestatushistory (invoiceid, invoicestatusid, statusdate, assignedtoid) VALUES (";
367                $insertstatement .= "'".$invoiceid."', ";
368                $insertstatement .= "'".$statusid."', ";
369                $insertstatement .= "'".$statusdate."', ";
370                $insertstatement .= "'".$assignedtoid."')";
371
372                $this->db->query($insertstatement);
373
374        }//end method
375
376
377        function updateReccurence($recurrid, $firstrepeat){
378                $updatestatement = "UPDATE recurringinvoices SET timesrepeated = timesrepeated+1, lastrepeat=NOW()";
379                if(!$firstrepeat)
380                        $updatestatement .= ", firstrepeat=NOW()";
381
382                $updatestatement .= " WHERE id = ".$recurrid;
383
384                $this->db->query($updatestatement);
385
386        }
387
388
389        function sendNotification($roleid, $newInvoiceID){
390                if($roleid == -100)
391                        $whereclause = "users.admin = 1";
392                else
393                        $whereclause = "rolestousers.roleid = '".$roleid."'";
394
395                $querystatement = "SELECT email FROM rolestousers INNER JOIN users ON rolestousers.userid = users.uuid
396                                                        WHERE email != '' AND ".$whereclause;
397
398                $queryresult = $this->db->query($querystatement);
399
400                $subject = APPLICATION_NAME." recurring invoice notification.";
401                $message = APPLICATION_NAME." has created a new order from a recurring invoice.  The new order id is ".$newInvoiceID;
402
403                while($therecord = $this->db->fetchArray($queryresult)){
404                        $to = $therecord["email"];
405                        $headers = "From: ".$to;
406
407                        @ mail($to,$subject,$message,$headers);
408                }// endwhile
409
410        }//end method
411}//end class
412
413
414
415//PROCESSOR
416//=============================================================================================
417if(!isset($noOutput)){
418        $recurr = new recurr($db);
419        $invoiceArray = $recurr->getInvoicesToRepeat();
420        foreach($invoiceArray as $invoiceid)
421                $recurr->copyInvoice($invoiceid);
422}
423?>
Note: See TracBrowser for help on using the browser.
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.