| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | include("../../include/session.php"); |
|---|
| 4 | |
|---|
| 5 | class receiptARItemAjax{ |
|---|
| 6 | |
|---|
| 7 | function receiptARItemAjax($db){ |
|---|
| 8 | |
|---|
| 9 | $this->db = $db; |
|---|
| 10 | |
|---|
| 11 | }//end method |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | function getItemsByClient($clientid){ |
|---|
| 15 | |
|---|
| 16 | $querystatement = " |
|---|
| 17 | SELECT |
|---|
| 18 | `aritems`.`id`, |
|---|
| 19 | IF(`aritems`.`type` = 'invoice', `invoices`.`id`, '\'\'') AS `invoiceid`, |
|---|
| 20 | `aritems`.`uuid`, |
|---|
| 21 | `aritems`.`relatedid`, |
|---|
| 22 | `aritems`.`itemdate`, |
|---|
| 23 | `aritems`.`type`, |
|---|
| 24 | `aritems`.`amount`, |
|---|
| 25 | `aritems`.`paid` |
|---|
| 26 | FROM |
|---|
| 27 | `aritems` LEFT JOIN `invoices` ON `aritems`.`relatedid`=`invoices`.`uuid` |
|---|
| 28 | WHERE |
|---|
| 29 | `aritems`.`status` = 'open' |
|---|
| 30 | AND `aritems`.`clientid` = '".mysql_real_escape_string($clientid)."' |
|---|
| 31 | AND `aritems`.`posted` = '1' |
|---|
| 32 | ORDER BY |
|---|
| 33 | `aritems`.`itemdate` |
|---|
| 34 | "; |
|---|
| 35 | |
|---|
| 36 | return $this->db->query($querystatement); |
|---|
| 37 | |
|---|
| 38 | }//end method |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | function getItemByID($aritemid){ |
|---|
| 42 | |
|---|
| 43 | $querystatement = " |
|---|
| 44 | SELECT |
|---|
| 45 | `aritems`.`id`, |
|---|
| 46 | IF(`aritems`.`type` = 'invoice', `invoices`.`id`, '\'\'') AS `invoiceid`, |
|---|
| 47 | `aritems`.`uuid`, |
|---|
| 48 | `aritems`.`relatedid`, |
|---|
| 49 | `aritems`.`itemdate`, |
|---|
| 50 | `aritems`.`type`, |
|---|
| 51 | `aritems`.`amount`, |
|---|
| 52 | `aritems`.`paid` |
|---|
| 53 | FROM |
|---|
| 54 | `aritems` LEFT JOIN `invoices` ON `aritems`.`relatedid`=`invoices`.`uuid` |
|---|
| 55 | WHERE |
|---|
| 56 | `aritems`.`uuid` = '".mysql_real_escape_string($aritemid)."' |
|---|
| 57 | ORDER BY |
|---|
| 58 | `aritems`.`itemdate`"; |
|---|
| 59 | |
|---|
| 60 | return $this->db->query($querystatement); |
|---|
| 61 | |
|---|
| 62 | }//end method |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | function outputItemsJSON($queryresult){ |
|---|
| 66 | |
|---|
| 67 | $jsonOutput = "{\n"; |
|---|
| 68 | $count = 1; |
|---|
| 69 | |
|---|
| 70 | while($therecord = $this->db->fetchArray($queryresult)){ |
|---|
| 71 | |
|---|
| 72 | $jsonOutput .= "\titem".$count.": {\n"; |
|---|
| 73 | |
|---|
| 74 | foreach($therecord as $key=>$value){ |
|---|
| 75 | |
|---|
| 76 | $jsonOutput .= "\t\t".$key.":"; |
|---|
| 77 | |
|---|
| 78 | switch($key){ |
|---|
| 79 | |
|---|
| 80 | case "id": |
|---|
| 81 | case "amount": |
|---|
| 82 | case "paid": |
|---|
| 83 | case "invoiceid": |
|---|
| 84 | $jsonOutput .= $value.",\n"; |
|---|
| 85 | break; |
|---|
| 86 | |
|---|
| 87 | default: |
|---|
| 88 | $jsonOutput .= "'".$value."',\n"; |
|---|
| 89 | break; |
|---|
| 90 | |
|---|
| 91 | }//endswitch |
|---|
| 92 | |
|---|
| 93 | }//endforeach |
|---|
| 94 | |
|---|
| 95 | $jsonOutput = substr($jsonOutput, 0, strlen($jsonOutput)-2); |
|---|
| 96 | |
|---|
| 97 | $jsonOutput .= "\n\t},\n"; |
|---|
| 98 | |
|---|
| 99 | $count++; |
|---|
| 100 | |
|---|
| 101 | }//endwhile |
|---|
| 102 | |
|---|
| 103 | if(strlen($jsonOutput) > 2) |
|---|
| 104 | $jsonOutput = substr($jsonOutput, 0, strlen($jsonOutput)-2); |
|---|
| 105 | |
|---|
| 106 | $jsonOutput .= "\n}"; |
|---|
| 107 | |
|---|
| 108 | return $jsonOutput; |
|---|
| 109 | |
|---|
| 110 | }//end method |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | function _showOpenARSelect($clientid, $type){ |
|---|
| 114 | |
|---|
| 115 | $queryType = $type; |
|---|
| 116 | if($type == "credit") |
|---|
| 117 | $queryType = "credit"; |
|---|
| 118 | |
|---|
| 119 | $querystatement = " |
|---|
| 120 | SELECT |
|---|
| 121 | `aritems`.`id`, |
|---|
| 122 | `aritems`.`uuid`, |
|---|
| 123 | `aritems`.`amount`, |
|---|
| 124 | `aritems`.`paid`, |
|---|
| 125 | `aritems`.`relatedid`, |
|---|
| 126 | `aritems`.`itemdate`, |
|---|
| 127 | `invoices`.`id` AS `invoiceid` |
|---|
| 128 | FROM |
|---|
| 129 | `aritems` LEFT JOIN `invoices` ON `aritems`.`relatedid`=`invoices`.`uuid` |
|---|
| 130 | WHERE |
|---|
| 131 | `aritems`.`status` = 'open' |
|---|
| 132 | AND |
|---|
| 133 | `aritems`.`clientid` = '".mysql_real_escape_string($clientid)."' |
|---|
| 134 | AND |
|---|
| 135 | `aritems`.`type` = '".$queryType."' |
|---|
| 136 | AND |
|---|
| 137 | `aritems`.`posted` = '1' |
|---|
| 138 | ORDER BY |
|---|
| 139 | "; |
|---|
| 140 | |
|---|
| 141 | if($type == "credit") |
|---|
| 142 | $querystatement .= "itemdate"; |
|---|
| 143 | else |
|---|
| 144 | $querystatement .= "relatedid"; |
|---|
| 145 | |
|---|
| 146 | $queryresult = $this->db->query($querystatement); |
|---|
| 147 | |
|---|
| 148 | if($this->db->numRows($queryresult)){ |
|---|
| 149 | ?><select id="newItem<?php echo str_replace(" ","",ucwords($type)) ?>ARID"> |
|---|
| 150 | <?php |
|---|
| 151 | |
|---|
| 152 | while($therecord = $this->db->fetchArray($queryresult)){ |
|---|
| 153 | |
|---|
| 154 | ?><option value="<?php echo $therecord["uuid"]?>"><?php |
|---|
| 155 | |
|---|
| 156 | if($therecord["invoiceid"]) |
|---|
| 157 | echo $therecord["invoiceid"].": "; |
|---|
| 158 | |
|---|
| 159 | echo formatFromSQLDate($therecord["itemdate"])." "; |
|---|
| 160 | echo formatVariable($therecord["amount"], "currency"); |
|---|
| 161 | |
|---|
| 162 | if($therecord["paid"] != 0) |
|---|
| 163 | echo " (".formatVariable(((real) $therecord["amount"]) - ((real) $therecord["paid"]), "currency").")"; |
|---|
| 164 | |
|---|
| 165 | ?></option><?php |
|---|
| 166 | |
|---|
| 167 | }//endwhile |
|---|
| 168 | |
|---|
| 169 | ?> |
|---|
| 170 | </select><?php |
|---|
| 171 | } else { |
|---|
| 172 | |
|---|
| 173 | ?><p class="disabledtext">No existing open <?php echo $type ?> AR items found for client.</p><?php |
|---|
| 174 | |
|---|
| 175 | } // endif |
|---|
| 176 | |
|---|
| 177 | }//end method |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | function getAddNewDialog($clientid){ |
|---|
| 181 | |
|---|
| 182 | include("include/fields.php"); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | ?> |
|---|
| 186 | <fieldset> |
|---|
| 187 | <legend>type</legend> |
|---|
| 188 | |
|---|
| 189 | <p> |
|---|
| 190 | <select id="newItemType"> |
|---|
| 191 | <option value="credit">credit</option> |
|---|
| 192 | <option value="invoice">invoice</option> |
|---|
| 193 | <option value="service charge">service charge</option> |
|---|
| 194 | </select> |
|---|
| 195 | </p> |
|---|
| 196 | |
|---|
| 197 | </fieldset> |
|---|
| 198 | |
|---|
| 199 | <fieldset id="newItemCreditFieldset"> |
|---|
| 200 | <legend>Credits</legend> |
|---|
| 201 | |
|---|
| 202 | <p id="newItemCreditNewP"> |
|---|
| 203 | <input type="radio" class="radiochecks" name="newItemCreditType" id="newItemCreditNew" checked="checked"/> |
|---|
| 204 | <label for="newItemCreditNew">new</label> |
|---|
| 205 | </p> |
|---|
| 206 | |
|---|
| 207 | <p id="newItemCreditExistingP"> |
|---|
| 208 | <input type="radio" class="radiochecks" name="newItemCreditType" id="newItemCreditExisting"/> |
|---|
| 209 | <label for="newItemCreditExisting">existing credit</label> |
|---|
| 210 | </p> |
|---|
| 211 | |
|---|
| 212 | <p id="newItemExisingListP"> |
|---|
| 213 | <?php $this->_showOpenARSelect($clientid, "credit"); ?> |
|---|
| 214 | </p> |
|---|
| 215 | </fieldset> |
|---|
| 216 | |
|---|
| 217 | <fieldset id="newItemInvoiceFieldset"> |
|---|
| 218 | <legend>Invoice AR Items</legend> |
|---|
| 219 | <p> |
|---|
| 220 | <?php $this->_showOpenARSelect($clientid, "invoice"); ?> |
|---|
| 221 | </p> |
|---|
| 222 | </fieldset> |
|---|
| 223 | |
|---|
| 224 | <fieldset id="newItemServiceChargeFieldset"> |
|---|
| 225 | <legend>Service Charges</legend> |
|---|
| 226 | <p> |
|---|
| 227 | <?php $this->_showOpenARSelect($clientid, "service charge"); ?> |
|---|
| 228 | </p> |
|---|
| 229 | </fieldset> |
|---|
| 230 | |
|---|
| 231 | <p class="standout" id="newItemMessage"> </p> |
|---|
| 232 | |
|---|
| 233 | <p align="right"> |
|---|
| 234 | <button type="button" id="newItemLoadButton" class="Buttons">add</button> |
|---|
| 235 | <button type="button" id="newItemCancelButton" class="Buttons">cancel</button> |
|---|
| 236 | </p> |
|---|
| 237 | <?php |
|---|
| 238 | |
|---|
| 239 | } |
|---|
| 240 | }//end class |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | //PROCESSOR |
|---|
| 244 | //============================================================================= |
|---|
| 245 | if(isset($_GET["cm"])){ |
|---|
| 246 | |
|---|
| 247 | $processor = new receiptARItemAjax($db); |
|---|
| 248 | if(!isset($_GET["cid"])) |
|---|
| 249 | $_GET["cid"] = ""; |
|---|
| 250 | |
|---|
| 251 | switch($_GET["cm"]){ |
|---|
| 252 | |
|---|
| 253 | case "getAllOpen": |
|---|
| 254 | |
|---|
| 255 | $result = $processor->getItemsByClient($_GET["cid"]); |
|---|
| 256 | echo $processor->outputItemsJSON($result); |
|---|
| 257 | break; |
|---|
| 258 | |
|---|
| 259 | case "getAddNewDialog": |
|---|
| 260 | |
|---|
| 261 | $processor->getAddNewDialog($_GET["cid"]); |
|---|
| 262 | break; |
|---|
| 263 | |
|---|
| 264 | case "getARItem": |
|---|
| 265 | |
|---|
| 266 | if(!isset($_GET["arid"])) |
|---|
| 267 | $_GET["arid"] = ""; |
|---|
| 268 | |
|---|
| 269 | $result = $processor->getItemByID($_GET["arid"]); |
|---|
| 270 | echo $processor->outputItemsJSON($result); |
|---|
| 271 | break; |
|---|
| 272 | |
|---|
| 273 | }//endswitch |
|---|
| 274 | |
|---|
| 275 | }//end if |
|---|
| 276 | ?> |
|---|