phpBMS

Show
Ignore:
Timestamp:
04/07/09 11:44:18 (3 years ago)
Author:
nate
Message:
  • Merged Nathan branch back into trunk.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/phpbms/modules/recurringinvoices/scheduler_recurr.php

    r384 r485  
    1 <?php  
    2  
    3 if(!class_exists("appError")) 
    4         include_once("../../include/session.php"); 
     1<?php 
     2//uncomment for debug purposes 
     3//if(!class_exists("appError")) 
     4//      include_once("../../include/session.php"); 
    55 
    66class recurr{ 
     
    1010        } 
    1111 
    12          
     12 
    1313        function getInvoicesToRepeat($dateToCheck = NULL){ 
    14                 if($dateToCheck == NULL)  
     14                if($dateToCheck == NULL) 
    1515                        $dateToCheck = mktime(0,0,0); 
    16          
     16 
    1717                $invoiceList = array(); 
    18          
     18 
    1919                $querystatement = "SELECT invoiceid,invoices.invoicedate, firstrepeat, lastrepeat, 
    20                                                         recurringinvoices.`type`,`eachlist`,`every`,`ontheday`,`ontheweek`  
     20                                                        recurringinvoices.`type`,`eachlist`,`every`,`ontheday`,`ontheweek` 
    2121                                                        FROM recurringinvoices INNER JOIN invoices ON recurringinvoices.invoiceid = invoices.id 
    22                                                         WHERE invoices.invoicedate <= '".dateToString($dateToCheck,"SQL")."'  
     22                                                        WHERE invoices.invoicedate <= '".dateToString($dateToCheck,"SQL")."' 
    2323                                                        AND (recurringinvoices.until IS NULL OR recurringinvoices.until >= '".dateToString($dateToCheck,"SQL")."') 
    2424                                                        AND (recurringinvoices.times IS NULL OR recurringinvoices.times > recurringinvoices.timesrepeated)"; 
    2525 
    2626                $queryresult = $this->db->query($querystatement); 
    27                  
     27 
    2828                while($therecord = $this->db->fetchArray($queryresult)){ 
    29                          
     29 
    3030                        if($therecord["lastrepeat"]) 
    3131                                $startDate = stringToDate($therecord["lastrepeat"],"SQL"); 
    3232                        else 
    3333                                $startDate = stringToDate($therecord["invoicedate"],"SQL"); 
    34                          
     34 
    3535                        $dateArray = $this->getValidInRange($startDate,$dateToCheck,$therecord); 
    3636 
    3737                        if( in_array($dateToCheck, $dateArray)) 
    3838                                $invoiceList[] = $therecord["invoiceid"]; 
    39                          
     39 
    4040                }//end while 
    41                  
     41 
    4242                return $invoiceList; 
    43          
     43 
    4444        }//end method 
    4545 
     
    5050                //should pad the end date to make sure we get all weekly repeats 
    5151                $endDate = strtotime("+7 days",$endDate); 
    52                  
     52 
    5353                $validDates = array(); 
    54                                  
     54 
    5555                while($nextDate <= $endDate){ 
    56                                          
     56 
    5757                        switch($therecord["type"]){ 
    5858                                case "Daily": 
     
    6161                                        $nextDate = strtotime("+".$therecord["every"]." days",$nextDate); 
    6262                                        break; 
    63                                          
    64                                 case "Weekly":                                                                                   
     63 
     64                                case "Weekly": 
    6565                                        //================================================================================== 
    6666                                        $weekDayArray = explode("::",$therecord["eachlist"]); 
     
    6969                                        $tempDate = strtotime(nl_langinfo( constant("DAY_1") ),$nextDate); 
    7070                                        $tempDate = strtotime("-7 days",$tempDate); 
    71                                          
     71 
    7272                                        foreach($weekDayArray as $weekday){ 
    7373                                                if($weekday == 7) 
    7474                                                        $validDates[]=$tempDate; 
    7575                                                else{ 
    76                                                         $weekday++;  
     76                                                        $weekday++; 
    7777                                                        $validDates[] = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate); 
    7878                                                } 
    7979                                        }// endforeach 
    80                                                                                                                                                                                  
    81                                          
     80 
     81 
    8282                                        $nextDate = strtotime("+".$therecord["every"]." week",$nextDate); 
    83                                          
    84                                         break; 
    85                                  
     83 
     84                                        break; 
     85 
    8686                                case "Monthly": 
    8787                                        //================================================================================== 
    88                                         $dateArray = localtime($nextDate,true);                                          
    89                                          
     88                                        $dateArray = localtime($nextDate,true); 
     89 
    9090                                        if($therecord["eachlist"]){ 
    9191                                                $dayArray = explode("::",$therecord["eachlist"]); 
    92                                                  
     92 
    9393                                                foreach($dayArray as $theday) 
    94                                                         $validDates[] = mktime(0,0,0,$dateArray["tm_mon"]+1,$theday,$dateArray["tm_year"]+1900);                                                         
    95                                                  
     94                                                        $validDates[] = mktime(0,0,0,$dateArray["tm_mon"]+1,$theday,$dateArray["tm_year"]+1900); 
     95 
    9696                                        } else{ 
    9797                                                // check for things like second tuesday or last friday; 
    9898                                                $tempDate = mktime(0,0,0,$dateArray["tm_mon"]+1,1,$dateArray["tm_year"]+1900); 
    9999                                                $weekday = $therecord["ontheday"]; 
    100                                                 $weekday = ($weekday == 7)? 1: ($weekday+1);                                                     
     100                                                $weekday = ($weekday == 7)? 1: ($weekday+1); 
    101101                                                if($therecord["ontheday"] != strftime("%u",$tempDate)); 
    102102                                                        $tempDate = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate); 
    103                                                          
     103 
    104104                                                while(date("n",$tempDate) == ($dateArray["tm_mon"]+1)){ 
    105                                                  
     105 
    106106                                                        if($therecord["ontheweek"] == 5){ 
    107107                                                                // 5 is the "last" option, so we just need to see if 
     
    109109                                                                if($daysInMonth - date("d",$tempDate) < 7) 
    110110                                                                        $validDates[] = $tempDate; 
    111                                                                          
     111 
    112112                                                        } else { 
    113113                                                                if( ceil(date("d",$tempDate)/7) == $therecord["ontheweek"]) 
    114                                                                         $validDates[] = $tempDate;                                                                               
     114                                                                        $validDates[] = $tempDate; 
    115115                                                        }// endif 
    116116 
     
    122122                                        $nextDate = strtotime("+".$therecord["every"]." months",$nextDate); 
    123123                                        break; 
    124                                          
     124 
    125125                                case "Yearly": 
    126126                                        //================================================================================== 
    127127                                        $monthArray = explode("::",$therecord["eachlist"]); 
    128128                                        foreach($monthArray as $monthNum){ 
    129                                                 $dateArray = localtime($nextDate,true);                                          
     129                                                $dateArray = localtime($nextDate,true); 
    130130                                                $daysInMonth = date("d", mktime(0,0,0,$monthNum,0,$dateArray["tm_year"]+1900) ); 
    131131 
     
    137137                                                        // check for things like second tuesday or last friday; 
    138138                                                        $tempDate = mktime(0,0,0,$monthNum,1,$dateArray["tm_year"]+1900); 
    139                                                          
     139 
    140140                                                        $weekday = $therecord["ontheday"]; 
    141                                                         $weekday = ($weekday == 7)? 1: ($weekday+1);                                                     
     141                                                        $weekday = ($weekday == 7)? 1: ($weekday+1); 
    142142                                                        if($therecord["ontheday"] != strftime("%u",$tempDate)); 
    143143                                                                $tempDate = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate); 
    144                                                          
    145                                                          
     144 
     145 
    146146                                                        while(date("n",$tempDate) == $monthNum){ 
    147147                                                                if($therecord["ontheweek"] == 5){ 
     
    150150                                                                        if($daysInMonth - date("d",$tempDate) < 7) 
    151151                                                                                $validDates[] = $tempDate; 
    152                                                                                  
     152 
    153153                                                                } else { 
    154154                                                                        if( ceil(date("d",$tempDate)/7) == $therecord["ontheweek"]) 
    155                                                                                 $validDates[] = $tempDate;                                                                               
     155                                                                                $validDates[] = $tempDate; 
    156156                                                                }// endif 
    157          
     157 
    158158                                                                $tempDate = strtotime("+7 days",$tempDate); 
    159          
     159 
    160160                                                        }// endwhile 
    161161 
    162                                                 }//endif                         
     162                                                }//endif 
    163163 
    164164                                        }//endforeach 
     
    168168                                        break; 
    169169                        }//endswitch 
    170                                                  
     170 
    171171                }//end while 
    172                  
     172 
    173173                return $validDates; 
    174                  
    175         }//end method 
    176          
    177          
     174 
     175        }//end method 
     176 
     177 
    178178        function copyInvoice($invoiceid){ 
    179179                $querystatement = " 
    180                         SELECT  
    181                                 invoices.*,  
    182                                 firstrepeat,  
    183                                 includepaymenttype,  
    184                                 includepaymentdetails,  
     180                        SELECT 
     181                                invoices.*, 
     182                                firstrepeat, 
     183                                includepaymenttype, 
     184                                includepaymentdetails, 
    185185                                recurringinvoices.id AS recurrid, 
    186                                 recurringinvoices.statusid AS newstatusid,  
     186                                recurringinvoices.statusid AS newstatusid, 
    187187                                recurringinvoices.assignedtoid AS newassignedtoid, 
    188188                                notificationroleid 
    189                         FROM  
     189                        FROM 
    190190                                invoices INNER JOIN recurringinvoices ON invoices.id = recurringinvoices.invoiceid 
    191                         WHERE  
     191                        WHERE 
    192192                                invoices.id = ".$invoiceid; 
    193193 
    194194                $queryresult = $this->db->query($querystatement); 
    195                  
     195 
    196196                $therecord = $this->db->fetchArray($queryresult); 
    197                  
     197 
    198198                $fieldList = array(); 
    199199                foreach($therecord as $name=>$value){ 
     
    206206                                case "modifieddate": 
    207207                                case "createdby": 
    208                                 case "creationdate":                             
     208                                case "creationdate": 
    209209                                case "newstatusid": 
    210210                                case "newassignedtoid": 
     
    212212                                case "recurrid": 
    213213                                        break; 
    214                                          
     214 
    215215                                case "checkno": 
    216216                                case "webconfirmationno": 
     
    222222                                        $therecord[$name] = NULL; 
    223223                                        break; 
    224                                          
     224 
    225225                                case "statusdate": 
    226226                                case "orderdate": 
     
    250250                                        $therecord[$name] = $therecord["newstatusid"]; 
    251251                                        break; 
    252                                  
     252 
    253253                                case "assignedtoid": 
    254254                                        $fieldlist[] = $name; 
     
    277277 
    278278                $insertstatement = $this->prepareInsert("invoices",$fieldlist,$therecord); 
    279          
     279 
    280280                $this->db->query($insertstatement); 
    281                  
     281 
    282282                $theid = $this->db->insertId(); 
    283                  
     283 
    284284                $this->copyLineItems($therecord["id"],$theid); 
    285285                $this->insertHistory($theid,$therecord["statusid"],$therecord["statusdate"],$therecord["assignedtoid"]); 
    286                  
     286 
    287287                $this->updateReccurence($therecord["recurrid"],$therecord["firstrepeat"]); 
    288                  
     288 
    289289                if($therecord["notificationroleid"]) 
    290290                        $this->sendNotification($therecord["notificationroleid"],$theid); 
    291                  
    292         }//end method 
    293          
    294          
     291 
     292        }//end method 
     293 
     294 
    295295        function copyLineItems($oldInvoiceID, $newInvoiceID){ 
    296                  
     296 
    297297                $querystatement = "SELECT * FROM lineitems WHERE invoiceid = ".$oldInvoiceID; 
    298298                $queryresult = $this->db->query($querystatement); 
    299299 
    300300                while($therecord = $this->db->fetchArray($queryresult)){ 
    301                          
     301 
    302302                        $fieldlist = array(); 
    303                          
     303 
    304304                        foreach($therecord as $name=>$value){ 
    305305                                switch($name){ 
     
    308308                                        case "modifieddate": 
    309309                                        case "createdby": 
    310                                         case "creationdate":                             
     310                                        case "creationdate": 
    311311                                                break; 
    312                                                  
     312 
    313313                                        case "invoiceid": 
    314314                                                $therecord[$name] = $newInvoiceID; 
    315                                                 $fieldlist[] = $name;                                    
     315                                                $fieldlist[] = $name; 
    316316                                                break; 
    317                                          
     317 
    318318                                        default: 
    319                                                 $fieldlist[] = $name;                                    
     319                                                $fieldlist[] = $name; 
    320320                                }// endswitch 
    321321                        }// endforeach 
    322                          
     322 
    323323                        $insertstatement = $this->prepareInsert("lineitems",$fieldlist,$therecord); 
    324                          
     324 
    325325                        $this->db->query($insertstatement); 
    326                          
     326 
    327327                }// endwhile 
    328                  
    329         }//end method 
    330          
    331          
     328 
     329        }//end method 
     330 
     331 
    332332        function prepareInsert($tablename, $fieldlist, $therecord){ 
    333333                $insertstatement = "INSERT INTO ".$tablename." ("; 
    334                  
     334 
    335335                foreach($fieldlist as $name) 
    336336                        $insertstatement .= "`".$name."`, "; 
    337337 
    338338                $insertstatement .= " createdby,creationdate) VALUES ("; 
    339                  
     339 
    340340                foreach($fieldlist as $name) 
    341341                        if($therecord[$name] !== NULL) 
     
    343343                        else 
    344344                                $insertstatement .= "NULL, "; 
    345                                  
     345 
    346346                $insertstatement .="-3, NOW());"; 
    347                  
     347 
    348348                return $insertstatement; 
    349349        }//end method 
    350          
    351          
     350 
     351 
    352352        function insertHistory($invoiceid, $statusid, $statusdate, $assignedtoid){ 
    353353                $insertstatement = "INSERT INTO invoicestatushistory (invoiceid, invoicestatusid, statusdate, assignedtoid) VALUES ("; 
     
    356356                $insertstatement .= "'".$statusdate."', "; 
    357357                $insertstatement .= $assignedtoid.")"; 
    358                  
     358 
    359359                $this->db->query($insertstatement); 
    360                  
    361         }//end method 
    362          
    363          
     360 
     361        }//end method 
     362 
     363 
    364364        function updateReccurence($recurrid, $firstrepeat){ 
    365365                $updatestatement = "UPDATE recurringinvoices SET timesrepeated = timesrepeated+1, lastrepeat=NOW()"; 
    366366                if(!$firstrepeat) 
    367367                        $updatestatement .= ", firstrepeat=NOW()"; 
    368                  
     368 
    369369                $updatestatement .= " WHERE id = ".$recurrid; 
    370          
     370 
    371371                $this->db->query($updatestatement); 
    372                  
     372 
    373373        } 
    374          
    375          
     374 
     375 
    376376        function sendNotification($roleid, $newInvoiceID){ 
    377377                if($roleid == -100) 
     
    379379                else 
    380380                        $whereclause = "rolestousers.roleid = ".$roleid; 
    381                          
     381 
    382382                $querystatement = "SELECT email FROM rolestousers INNER JOIN users ON rolestousers.userid = users.id 
    383383                                                        WHERE email != '' AND ".$whereclause; 
    384384 
    385385                $queryresult = $this->db->query($querystatement); 
    386                  
     386 
    387387                $subject = APPLICATION_NAME." recurring invoice notification."; 
    388388                $message = APPLICATION_NAME." has created a new order from a recurring invoice.  The new order id is ".$newInvoiceID; 
     
    394394                        @ mail ($to,$subject,$message,$headers); 
    395395                }// endwhile 
    396                  
     396 
    397397        }//end method 
    398398}//end class 
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.