- Timestamp:
- 04/07/09 11:44:18 (3 years ago)
- 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"); 5 5 6 6 class recurr{ … … 10 10 } 11 11 12 12 13 13 function getInvoicesToRepeat($dateToCheck = NULL){ 14 if($dateToCheck == NULL) 14 if($dateToCheck == NULL) 15 15 $dateToCheck = mktime(0,0,0); 16 16 17 17 $invoiceList = array(); 18 18 19 19 $querystatement = "SELECT invoiceid,invoices.invoicedate, firstrepeat, lastrepeat, 20 recurringinvoices.`type`,`eachlist`,`every`,`ontheday`,`ontheweek` 20 recurringinvoices.`type`,`eachlist`,`every`,`ontheday`,`ontheweek` 21 21 FROM recurringinvoices INNER JOIN invoices ON recurringinvoices.invoiceid = invoices.id 22 WHERE invoices.invoicedate <= '".dateToString($dateToCheck,"SQL")."' 22 WHERE invoices.invoicedate <= '".dateToString($dateToCheck,"SQL")."' 23 23 AND (recurringinvoices.until IS NULL OR recurringinvoices.until >= '".dateToString($dateToCheck,"SQL")."') 24 24 AND (recurringinvoices.times IS NULL OR recurringinvoices.times > recurringinvoices.timesrepeated)"; 25 25 26 26 $queryresult = $this->db->query($querystatement); 27 27 28 28 while($therecord = $this->db->fetchArray($queryresult)){ 29 29 30 30 if($therecord["lastrepeat"]) 31 31 $startDate = stringToDate($therecord["lastrepeat"],"SQL"); 32 32 else 33 33 $startDate = stringToDate($therecord["invoicedate"],"SQL"); 34 34 35 35 $dateArray = $this->getValidInRange($startDate,$dateToCheck,$therecord); 36 36 37 37 if( in_array($dateToCheck, $dateArray)) 38 38 $invoiceList[] = $therecord["invoiceid"]; 39 39 40 40 }//end while 41 41 42 42 return $invoiceList; 43 43 44 44 }//end method 45 45 … … 50 50 //should pad the end date to make sure we get all weekly repeats 51 51 $endDate = strtotime("+7 days",$endDate); 52 52 53 53 $validDates = array(); 54 54 55 55 while($nextDate <= $endDate){ 56 56 57 57 switch($therecord["type"]){ 58 58 case "Daily": … … 61 61 $nextDate = strtotime("+".$therecord["every"]." days",$nextDate); 62 62 break; 63 64 case "Weekly": 63 64 case "Weekly": 65 65 //================================================================================== 66 66 $weekDayArray = explode("::",$therecord["eachlist"]); … … 69 69 $tempDate = strtotime(nl_langinfo( constant("DAY_1") ),$nextDate); 70 70 $tempDate = strtotime("-7 days",$tempDate); 71 71 72 72 foreach($weekDayArray as $weekday){ 73 73 if($weekday == 7) 74 74 $validDates[]=$tempDate; 75 75 else{ 76 $weekday++; 76 $weekday++; 77 77 $validDates[] = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate); 78 78 } 79 79 }// endforeach 80 81 80 81 82 82 $nextDate = strtotime("+".$therecord["every"]." week",$nextDate); 83 84 break; 85 83 84 break; 85 86 86 case "Monthly": 87 87 //================================================================================== 88 $dateArray = localtime($nextDate,true); 89 88 $dateArray = localtime($nextDate,true); 89 90 90 if($therecord["eachlist"]){ 91 91 $dayArray = explode("::",$therecord["eachlist"]); 92 92 93 93 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 96 96 } else{ 97 97 // check for things like second tuesday or last friday; 98 98 $tempDate = mktime(0,0,0,$dateArray["tm_mon"]+1,1,$dateArray["tm_year"]+1900); 99 99 $weekday = $therecord["ontheday"]; 100 $weekday = ($weekday == 7)? 1: ($weekday+1); 100 $weekday = ($weekday == 7)? 1: ($weekday+1); 101 101 if($therecord["ontheday"] != strftime("%u",$tempDate)); 102 102 $tempDate = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate); 103 103 104 104 while(date("n",$tempDate) == ($dateArray["tm_mon"]+1)){ 105 105 106 106 if($therecord["ontheweek"] == 5){ 107 107 // 5 is the "last" option, so we just need to see if … … 109 109 if($daysInMonth - date("d",$tempDate) < 7) 110 110 $validDates[] = $tempDate; 111 111 112 112 } else { 113 113 if( ceil(date("d",$tempDate)/7) == $therecord["ontheweek"]) 114 $validDates[] = $tempDate; 114 $validDates[] = $tempDate; 115 115 }// endif 116 116 … … 122 122 $nextDate = strtotime("+".$therecord["every"]." months",$nextDate); 123 123 break; 124 124 125 125 case "Yearly": 126 126 //================================================================================== 127 127 $monthArray = explode("::",$therecord["eachlist"]); 128 128 foreach($monthArray as $monthNum){ 129 $dateArray = localtime($nextDate,true); 129 $dateArray = localtime($nextDate,true); 130 130 $daysInMonth = date("d", mktime(0,0,0,$monthNum,0,$dateArray["tm_year"]+1900) ); 131 131 … … 137 137 // check for things like second tuesday or last friday; 138 138 $tempDate = mktime(0,0,0,$monthNum,1,$dateArray["tm_year"]+1900); 139 139 140 140 $weekday = $therecord["ontheday"]; 141 $weekday = ($weekday == 7)? 1: ($weekday+1); 141 $weekday = ($weekday == 7)? 1: ($weekday+1); 142 142 if($therecord["ontheday"] != strftime("%u",$tempDate)); 143 143 $tempDate = strtotime(nl_langinfo( constant("DAY_".$weekday) ),$tempDate); 144 145 144 145 146 146 while(date("n",$tempDate) == $monthNum){ 147 147 if($therecord["ontheweek"] == 5){ … … 150 150 if($daysInMonth - date("d",$tempDate) < 7) 151 151 $validDates[] = $tempDate; 152 152 153 153 } else { 154 154 if( ceil(date("d",$tempDate)/7) == $therecord["ontheweek"]) 155 $validDates[] = $tempDate; 155 $validDates[] = $tempDate; 156 156 }// endif 157 157 158 158 $tempDate = strtotime("+7 days",$tempDate); 159 159 160 160 }// endwhile 161 161 162 }//endif 162 }//endif 163 163 164 164 }//endforeach … … 168 168 break; 169 169 }//endswitch 170 170 171 171 }//end while 172 172 173 173 return $validDates; 174 175 }//end method 176 177 174 175 }//end method 176 177 178 178 function copyInvoice($invoiceid){ 179 179 $querystatement = " 180 SELECT 181 invoices.*, 182 firstrepeat, 183 includepaymenttype, 184 includepaymentdetails, 180 SELECT 181 invoices.*, 182 firstrepeat, 183 includepaymenttype, 184 includepaymentdetails, 185 185 recurringinvoices.id AS recurrid, 186 recurringinvoices.statusid AS newstatusid, 186 recurringinvoices.statusid AS newstatusid, 187 187 recurringinvoices.assignedtoid AS newassignedtoid, 188 188 notificationroleid 189 FROM 189 FROM 190 190 invoices INNER JOIN recurringinvoices ON invoices.id = recurringinvoices.invoiceid 191 WHERE 191 WHERE 192 192 invoices.id = ".$invoiceid; 193 193 194 194 $queryresult = $this->db->query($querystatement); 195 195 196 196 $therecord = $this->db->fetchArray($queryresult); 197 197 198 198 $fieldList = array(); 199 199 foreach($therecord as $name=>$value){ … … 206 206 case "modifieddate": 207 207 case "createdby": 208 case "creationdate": 208 case "creationdate": 209 209 case "newstatusid": 210 210 case "newassignedtoid": … … 212 212 case "recurrid": 213 213 break; 214 214 215 215 case "checkno": 216 216 case "webconfirmationno": … … 222 222 $therecord[$name] = NULL; 223 223 break; 224 224 225 225 case "statusdate": 226 226 case "orderdate": … … 250 250 $therecord[$name] = $therecord["newstatusid"]; 251 251 break; 252 252 253 253 case "assignedtoid": 254 254 $fieldlist[] = $name; … … 277 277 278 278 $insertstatement = $this->prepareInsert("invoices",$fieldlist,$therecord); 279 279 280 280 $this->db->query($insertstatement); 281 281 282 282 $theid = $this->db->insertId(); 283 283 284 284 $this->copyLineItems($therecord["id"],$theid); 285 285 $this->insertHistory($theid,$therecord["statusid"],$therecord["statusdate"],$therecord["assignedtoid"]); 286 286 287 287 $this->updateReccurence($therecord["recurrid"],$therecord["firstrepeat"]); 288 288 289 289 if($therecord["notificationroleid"]) 290 290 $this->sendNotification($therecord["notificationroleid"],$theid); 291 292 }//end method 293 294 291 292 }//end method 293 294 295 295 function copyLineItems($oldInvoiceID, $newInvoiceID){ 296 296 297 297 $querystatement = "SELECT * FROM lineitems WHERE invoiceid = ".$oldInvoiceID; 298 298 $queryresult = $this->db->query($querystatement); 299 299 300 300 while($therecord = $this->db->fetchArray($queryresult)){ 301 301 302 302 $fieldlist = array(); 303 303 304 304 foreach($therecord as $name=>$value){ 305 305 switch($name){ … … 308 308 case "modifieddate": 309 309 case "createdby": 310 case "creationdate": 310 case "creationdate": 311 311 break; 312 312 313 313 case "invoiceid": 314 314 $therecord[$name] = $newInvoiceID; 315 $fieldlist[] = $name; 315 $fieldlist[] = $name; 316 316 break; 317 317 318 318 default: 319 $fieldlist[] = $name; 319 $fieldlist[] = $name; 320 320 }// endswitch 321 321 }// endforeach 322 322 323 323 $insertstatement = $this->prepareInsert("lineitems",$fieldlist,$therecord); 324 324 325 325 $this->db->query($insertstatement); 326 326 327 327 }// endwhile 328 329 }//end method 330 331 328 329 }//end method 330 331 332 332 function prepareInsert($tablename, $fieldlist, $therecord){ 333 333 $insertstatement = "INSERT INTO ".$tablename." ("; 334 334 335 335 foreach($fieldlist as $name) 336 336 $insertstatement .= "`".$name."`, "; 337 337 338 338 $insertstatement .= " createdby,creationdate) VALUES ("; 339 339 340 340 foreach($fieldlist as $name) 341 341 if($therecord[$name] !== NULL) … … 343 343 else 344 344 $insertstatement .= "NULL, "; 345 345 346 346 $insertstatement .="-3, NOW());"; 347 347 348 348 return $insertstatement; 349 349 }//end method 350 351 350 351 352 352 function insertHistory($invoiceid, $statusid, $statusdate, $assignedtoid){ 353 353 $insertstatement = "INSERT INTO invoicestatushistory (invoiceid, invoicestatusid, statusdate, assignedtoid) VALUES ("; … … 356 356 $insertstatement .= "'".$statusdate."', "; 357 357 $insertstatement .= $assignedtoid.")"; 358 358 359 359 $this->db->query($insertstatement); 360 361 }//end method 362 363 360 361 }//end method 362 363 364 364 function updateReccurence($recurrid, $firstrepeat){ 365 365 $updatestatement = "UPDATE recurringinvoices SET timesrepeated = timesrepeated+1, lastrepeat=NOW()"; 366 366 if(!$firstrepeat) 367 367 $updatestatement .= ", firstrepeat=NOW()"; 368 368 369 369 $updatestatement .= " WHERE id = ".$recurrid; 370 370 371 371 $this->db->query($updatestatement); 372 372 373 373 } 374 375 374 375 376 376 function sendNotification($roleid, $newInvoiceID){ 377 377 if($roleid == -100) … … 379 379 else 380 380 $whereclause = "rolestousers.roleid = ".$roleid; 381 381 382 382 $querystatement = "SELECT email FROM rolestousers INNER JOIN users ON rolestousers.userid = users.id 383 383 WHERE email != '' AND ".$whereclause; 384 384 385 385 $queryresult = $this->db->query($querystatement); 386 386 387 387 $subject = APPLICATION_NAME." recurring invoice notification."; 388 388 $message = APPLICATION_NAME." has created a new order from a recurring invoice. The new order id is ".$newInvoiceID; … … 394 394 @ mail ($to,$subject,$message,$headers); 395 395 }// endwhile 396 396 397 397 }//end method 398 398 }//end class