| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Rev: 254 $ | $LastChangedBy: brieb $ |
|---|
| 4 | $LastChangedDate: 2007-08-07 18:38:38 -0600 (Tue, 07 Aug 2007) $ |
|---|
| 5 | +-------------------------------------------------------------------------+ |
|---|
| 6 | | Copyright (c) 2004 - 2010, Kreotek LLC | |
|---|
| 7 | | All rights reserved. | |
|---|
| 8 | +-------------------------------------------------------------------------+ |
|---|
| 9 | | | |
|---|
| 10 | | Redistribution and use in source and binary forms, with or without | |
|---|
| 11 | | modification, are permitted provided that the following conditions are | |
|---|
| 12 | | met: | |
|---|
| 13 | | | |
|---|
| 14 | | - Redistributions of source code must retain the above copyright | |
|---|
| 15 | | notice, this list of conditions and the following disclaimer. | |
|---|
| 16 | | | |
|---|
| 17 | | - Redistributions in binary form must reproduce the above copyright | |
|---|
| 18 | | notice, this list of conditions and the following disclaimer in the | |
|---|
| 19 | | documentation and/or other materials provided with the distribution. | |
|---|
| 20 | | | |
|---|
| 21 | | - Neither the name of Kreotek LLC nor the names of its contributore may | |
|---|
| 22 | | be used to endorse or promote products derived from this software | |
|---|
| 23 | | without specific prior written permission. | |
|---|
| 24 | | | |
|---|
| 25 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|---|
| 26 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|---|
| 27 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
|---|
| 28 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|---|
| 29 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|---|
| 30 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|---|
| 31 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|---|
| 32 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|---|
| 33 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|---|
| 34 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|---|
| 35 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|---|
| 36 | | | |
|---|
| 37 | +-------------------------------------------------------------------------+ |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | class arAging{ |
|---|
| 41 | |
|---|
| 42 | var $agingDate; |
|---|
| 43 | var $printClientStatements = false; |
|---|
| 44 | var $printSummary = false; |
|---|
| 45 | var $serviceCharges = 0; |
|---|
| 46 | |
|---|
| 47 | function arAging($db, $userid = NULL){ |
|---|
| 48 | |
|---|
| 49 | $this->db = $db; |
|---|
| 50 | |
|---|
| 51 | if($userid) |
|---|
| 52 | $this->userid = $userid; |
|---|
| 53 | else |
|---|
| 54 | $this->userid = $_SESSION["userinfo"]["id"]; |
|---|
| 55 | |
|---|
| 56 | }//end method |
|---|
| 57 | |
|---|
| 58 | function run($agingDate = NULL){ |
|---|
| 59 | |
|---|
| 60 | if($agingDate) |
|---|
| 61 | $this->agingDate = stringToDate($agingDate); |
|---|
| 62 | else |
|---|
| 63 | $this->agingDate = mktime(0,0,0); |
|---|
| 64 | |
|---|
| 65 | $querystatement = " |
|---|
| 66 | SELECT |
|---|
| 67 | * |
|---|
| 68 | FROM |
|---|
| 69 | aritems |
|---|
| 70 | WHERE |
|---|
| 71 | `status` = 'open' |
|---|
| 72 | AND posted = 1 |
|---|
| 73 | AND `type` = 'invoice'"; |
|---|
| 74 | |
|---|
| 75 | $queryresult = $this->db->query($querystatement); |
|---|
| 76 | |
|---|
| 77 | while($therecord = $this->db->fetchArray($queryresult)){ |
|---|
| 78 | |
|---|
| 79 | $itemdate = stringToDate($therecord["itemdate"], "SQL"); |
|---|
| 80 | $termDate[1] = strtotime(TERM1_DAYS." days", $itemdate); |
|---|
| 81 | $termDate[2] = strtotime(TERM2_DAYS." days", $itemdate); |
|---|
| 82 | $termDate[3] = strtotime(TERM3_DAYS." days", $itemdate); |
|---|
| 83 | |
|---|
| 84 | for($i = 1; $i < 4; $i++){ |
|---|
| 85 | |
|---|
| 86 | if($this->agingDate > $termDate[$i] && $therecord["aged".$i] == 0){ |
|---|
| 87 | if( $this->_createServiceCharge($therecord, constant("TERM".$i."_PERCENTAGE")) ) |
|---|
| 88 | $this->serviceCharges++; |
|---|
| 89 | |
|---|
| 90 | $this->_markCharged($therecord["id"], "aged".$i); |
|---|
| 91 | |
|---|
| 92 | }//endif |
|---|
| 93 | |
|---|
| 94 | }//endfor |
|---|
| 95 | |
|---|
| 96 | }//endwhile |
|---|
| 97 | |
|---|
| 98 | }//end method |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | function _markCharged($aritemid, $termField){ |
|---|
| 102 | |
|---|
| 103 | $updatestatement = " |
|---|
| 104 | UPDATE |
|---|
| 105 | aritems |
|---|
| 106 | SET |
|---|
| 107 | ".$termField." = 1 |
|---|
| 108 | WHERE |
|---|
| 109 | id = ".((int) $aritemid); |
|---|
| 110 | |
|---|
| 111 | $this->db->query($updatestatement); |
|---|
| 112 | |
|---|
| 113 | }//end method |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | function _createServiceCharge($arrecord, $percentage){ |
|---|
| 117 | |
|---|
| 118 | if($arrecord["amount"] - $arrecord["paid"] <= 0) |
|---|
| 119 | return false; |
|---|
| 120 | |
|---|
| 121 | $newAmount = round( ($arrecord["amount"] - $arrecord["paid"]) * ($percentage/100), CURRENCY_ACCURACY); |
|---|
| 122 | |
|---|
| 123 | if($newAmount <=0) |
|---|
| 124 | return false; |
|---|
| 125 | |
|---|
| 126 | if(!class_exists("phpbmsTable")) |
|---|
| 127 | include("include/tables.php"); |
|---|
| 128 | |
|---|
| 129 | $aritems = new phpbmsTable($this->db, "tbld:c595dbe7-6c77-1e02-5e81-c2e215736e9c"); |
|---|
| 130 | |
|---|
| 131 | $newarrecord = array(); |
|---|
| 132 | $newarrecord["uuid"] = uuid($aritems->prefix.":"); |
|---|
| 133 | $newarrecord["type"] = "service charge"; |
|---|
| 134 | $newarrecord["status"] = "open"; |
|---|
| 135 | $newarrecord["posted"] = 1; |
|---|
| 136 | $newarrecord["amount"] = $newAmount; |
|---|
| 137 | $newarrecord["itemdate"] = dateToString($this->agingDate); |
|---|
| 138 | $newarrecord["clientid"] = $arrecord["clientid"]; |
|---|
| 139 | $newarrecord["relatedid"] = $arrecord["relatedid"]; |
|---|
| 140 | |
|---|
| 141 | $aritems->insertRecord($newarrecord, $this->userid); |
|---|
| 142 | |
|---|
| 143 | return true; |
|---|
| 144 | |
|---|
| 145 | }//end method |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | function showResults(){ |
|---|
| 149 | |
|---|
| 150 | include_once("include/fields.php"); |
|---|
| 151 | global $phpbms; |
|---|
| 152 | $db = &$this->db; |
|---|
| 153 | |
|---|
| 154 | $phpbms->cssIncludes[] = "pages/aging.css"; |
|---|
| 155 | $phpbms->jsIncludes[] = "modules/bms/javascript/aritem_aging.js"; |
|---|
| 156 | $phpbms->showMenu = false; |
|---|
| 157 | |
|---|
| 158 | $formSubmit = htmlentities($_SERVER['REQUEST_URI']); |
|---|
| 159 | |
|---|
| 160 | include("header.php"); |
|---|
| 161 | ?> |
|---|
| 162 | <div class="bodyline" id="dialog"> |
|---|
| 163 | <h1><span>AR Aging Results</span></h1> |
|---|
| 164 | <form action="<?php echo $formSubmit ?>" id="record" method="post"> |
|---|
| 165 | |
|---|
| 166 | <fieldset> |
|---|
| 167 | <legend>details</legend> |
|---|
| 168 | <p> |
|---|
| 169 | <label for="agingdate">aging date</label><br /> |
|---|
| 170 | <input id="agingdate" class="uneditable" readonly="readonly" value="<?php echo dateToString($this->agingDate)?>"/> |
|---|
| 171 | </p> |
|---|
| 172 | |
|---|
| 173 | <p> |
|---|
| 174 | <label for="serviceCharges">service charge AR items created</label><br /> |
|---|
| 175 | <input id="serviceCharges" class="uneditable" readonly="readonly" value="<?php echo $this->serviceCharges?>"/> |
|---|
| 176 | </p> |
|---|
| 177 | </fieldset> |
|---|
| 178 | |
|---|
| 179 | <p class="notes">Any reports checked to be run should open in separate windows.</p> |
|---|
| 180 | |
|---|
| 181 | <p align="right"> |
|---|
| 182 | <input type="hidden" id="printClientStatements" value="<?php echo ((int) $this->printClientStatements)?>"/> |
|---|
| 183 | <input type="hidden" id="printSummary" value="<?php echo ((int) $this->printSummary)?>"/> |
|---|
| 184 | <input type="hidden" id="command" name="command" /> |
|---|
| 185 | <button type="button" class="Buttons" id="cancelButton">Done</button> |
|---|
| 186 | </p> |
|---|
| 187 | </form> |
|---|
| 188 | </div> |
|---|
| 189 | <?php |
|---|
| 190 | include("footer.php"); |
|---|
| 191 | |
|---|
| 192 | }//end method |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | function showDialog(){ |
|---|
| 196 | |
|---|
| 197 | include_once("include/fields.php"); |
|---|
| 198 | global $phpbms; |
|---|
| 199 | $db = &$this->db; |
|---|
| 200 | |
|---|
| 201 | $phpbms->cssIncludes[] = "pages/aging.css"; |
|---|
| 202 | $phpbms->jsIncludes[] = "modules/bms/javascript/aritem_aging.js"; |
|---|
| 203 | $phpbms->showMenu = false; |
|---|
| 204 | |
|---|
| 205 | $formSubmit = str_replace("&","&",$_SERVER['REQUEST_URI']); |
|---|
| 206 | |
|---|
| 207 | $theform = new phpbmsForm(); |
|---|
| 208 | |
|---|
| 209 | $theinput = new inputDatePicker("agingdate", dateToString(mktime(0,0,0), "SQL"), "aging date", true); |
|---|
| 210 | $theform->addField($theinput); |
|---|
| 211 | |
|---|
| 212 | $theform->jsMerge(); |
|---|
| 213 | |
|---|
| 214 | include("header.php"); |
|---|
| 215 | |
|---|
| 216 | ?> |
|---|
| 217 | <div class="bodyline" id="dialog"> |
|---|
| 218 | <h1><span>AR Aging</span></h1> |
|---|
| 219 | <form action="<?php echo $formSubmit ?>" id="record" method="post"> |
|---|
| 220 | |
|---|
| 221 | <fieldset> |
|---|
| 222 | <legend>options</legend> |
|---|
| 223 | <p><?php $theform->showField("agingdate")?></p> |
|---|
| 224 | </fieldset> |
|---|
| 225 | |
|---|
| 226 | <fieldset> |
|---|
| 227 | <legend>Current Items Needing Aging</legend> |
|---|
| 228 | <?php $this->_showNeedingAgingTotals()?> |
|---|
| 229 | |
|---|
| 230 | </fieldset> |
|---|
| 231 | |
|---|
| 232 | <fieldset> |
|---|
| 233 | <legend>Report Options</legend> |
|---|
| 234 | |
|---|
| 235 | <p> |
|---|
| 236 | <input type="checkbox" value="1" id="printStatements" name="printStatements" class="radiochecks"/> |
|---|
| 237 | <label for="printStatements">Print client statements for clients with open items.</label> |
|---|
| 238 | </p> |
|---|
| 239 | |
|---|
| 240 | <p> |
|---|
| 241 | <input type="checkbox" value="1" id="printSummary" name="printSummary" class="radiochecks"/> |
|---|
| 242 | <label for="printSummary">Print a statement summary.</label> |
|---|
| 243 | </p> |
|---|
| 244 | |
|---|
| 245 | </fieldset> |
|---|
| 246 | <p class="notes"> |
|---|
| 247 | Aging should be run in a consistent timeframe. Although |
|---|
| 248 | running again will not result in duplicate service charges |
|---|
| 249 | for an invoice in the same aging period, not running |
|---|
| 250 | the aging on the same specified date may result in client |
|---|
| 251 | statements showing the service charges too early or too late. |
|---|
| 252 | </p> |
|---|
| 253 | |
|---|
| 254 | <p align="right"> |
|---|
| 255 | <input type="hidden" name="command" id="command" /> |
|---|
| 256 | <button type="button" class="Buttons" id="runButton">run aging</button> |
|---|
| 257 | <button type="button" class="Buttons" id="cancelButton">cancel</button> |
|---|
| 258 | </p> |
|---|
| 259 | </form> |
|---|
| 260 | </div> |
|---|
| 261 | <?php |
|---|
| 262 | |
|---|
| 263 | include("footer.php"); |
|---|
| 264 | |
|---|
| 265 | }//end method |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | function _showNeedingAgingTotals(){ |
|---|
| 269 | |
|---|
| 270 | $totals = array( |
|---|
| 271 | "term1" => 0, |
|---|
| 272 | "term2" => 0, |
|---|
| 273 | "term3" => 0 |
|---|
| 274 | ); |
|---|
| 275 | |
|---|
| 276 | $querystatement = " |
|---|
| 277 | SELECT |
|---|
| 278 | itemdate, |
|---|
| 279 | aged1, |
|---|
| 280 | aged2, |
|---|
| 281 | aged3 |
|---|
| 282 | FROM |
|---|
| 283 | aritems |
|---|
| 284 | WHERE |
|---|
| 285 | `status` = 'open' |
|---|
| 286 | AND posted = 1 |
|---|
| 287 | AND `type` = 'invoice'"; |
|---|
| 288 | |
|---|
| 289 | $queryresult = $this->db->query($querystatement); |
|---|
| 290 | |
|---|
| 291 | while($therecord = $this->db->fetchArray($queryresult)){ |
|---|
| 292 | |
|---|
| 293 | $itemdate = stringToDate($therecord["itemdate"], "SQL"); |
|---|
| 294 | $term1Date = strtotime(TERM1_DAYS." days", $itemdate); |
|---|
| 295 | $term2Date = strtotime(TERM2_DAYS." days", $itemdate); |
|---|
| 296 | $term3Date = strtotime(TERM3_DAYS." days", $itemdate); |
|---|
| 297 | $today = mktime(0,0,0); |
|---|
| 298 | |
|---|
| 299 | if($today > $term1Date && $therecord["aged1"] == 0) |
|---|
| 300 | $totals["term1"]++; |
|---|
| 301 | elseif($today > $term2Date && $therecord["aged2"] == 0) |
|---|
| 302 | $totals["term2"]++; |
|---|
| 303 | elseif($today > $term2Date && $therecord["aged2"] == 0) |
|---|
| 304 | $totals["term3"]++; |
|---|
| 305 | |
|---|
| 306 | }//endwhile |
|---|
| 307 | ?> |
|---|
| 308 | <p> |
|---|
| 309 | <label for="term1"><?php echo (TERM1_DAYS+1)." - ".TERM2_DAYS." days" ?></label><br /> |
|---|
| 310 | <input id="term1" class="uneditable" readonly="readonly" value="<?php echo $totals["term1"] ?>"/> |
|---|
| 311 | </p> |
|---|
| 312 | |
|---|
| 313 | <p> |
|---|
| 314 | <label for="term2"><?php echo (TERM2_DAYS+1)." - ".TERM3_DAYS." days" ?></label><br /> |
|---|
| 315 | <input id="term2" class="uneditable" readonly="readonly" value="<?php echo $totals["term2"] ?>"/> |
|---|
| 316 | </p> |
|---|
| 317 | |
|---|
| 318 | <p> |
|---|
| 319 | <label for="term3"><?php echo (TERM3_DAYS+1)."+ days" ?></label><br /> |
|---|
| 320 | <input id="term3" class="uneditable" readonly="readonly" value="<?php echo $totals["term3"] ?>"/> |
|---|
| 321 | </p> |
|---|
| 322 | |
|---|
| 323 | <?php |
|---|
| 324 | |
|---|
| 325 | }//end method |
|---|
| 326 | |
|---|
| 327 | }//end class |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | //Processor |
|---|
| 333 | //=========================================================================== |
|---|
| 334 | if(!isset($noOutput)){ |
|---|
| 335 | |
|---|
| 336 | include_once("../../include/session.php"); |
|---|
| 337 | |
|---|
| 338 | $aging = new arAging($db); |
|---|
| 339 | |
|---|
| 340 | if(isset($_POST["command"])){ |
|---|
| 341 | |
|---|
| 342 | switch($_POST["command"]){ |
|---|
| 343 | |
|---|
| 344 | case "run": |
|---|
| 345 | |
|---|
| 346 | if(!isset($_POST["agingdate"])) |
|---|
| 347 | $error = new appError(200, "passed parameters not set"); |
|---|
| 348 | |
|---|
| 349 | $aging->run($_POST["agingdate"]); |
|---|
| 350 | |
|---|
| 351 | if(isset($_POST["printStatements"])) |
|---|
| 352 | $aging->printClientStatements = true; |
|---|
| 353 | |
|---|
| 354 | if(isset($_POST["printSummary"])) |
|---|
| 355 | $aging->printSummary = true; |
|---|
| 356 | |
|---|
| 357 | $aging->showResults(); |
|---|
| 358 | break; |
|---|
| 359 | |
|---|
| 360 | case "cancel": |
|---|
| 361 | |
|---|
| 362 | goURL(APP_PATH."search.php?id=tbld%3Ac595dbe7-6c77-1e02-5e81-c2e215736e9c"); |
|---|
| 363 | |
|---|
| 364 | }//endswitch |
|---|
| 365 | } else |
|---|
| 366 | $aging->showDialog(); |
|---|
| 367 | |
|---|
| 368 | }//end if |
|---|
| 369 | ?> |
|---|