| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Rev: 702 $ | $LastChangedBy: brieb $ |
|---|
| 4 | $LastChangedDate: 2010-01-01 15:14:57 -0700 (Fri, 01 Jan 2010) $ |
|---|
| 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 clientConsolidator{ |
|---|
| 41 | |
|---|
| 42 | var $db; |
|---|
| 43 | |
|---|
| 44 | function clientConsolidator($db){ |
|---|
| 45 | |
|---|
| 46 | $this->db = $db; |
|---|
| 47 | |
|---|
| 48 | }//end init |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | function showPicker($idsArray, $useUuid = false){ |
|---|
| 52 | |
|---|
| 53 | $querystatement = " |
|---|
| 54 | SELECT |
|---|
| 55 | clients.id, |
|---|
| 56 | clients.uuid |
|---|
| 57 | FROM |
|---|
| 58 | `clients` |
|---|
| 59 | WHERE |
|---|
| 60 | clients."; |
|---|
| 61 | |
|---|
| 62 | if($useUuid){ |
|---|
| 63 | |
|---|
| 64 | $querystatement .= "uuid IN ("; |
|---|
| 65 | $inStatement =""; |
|---|
| 66 | |
|---|
| 67 | foreach($idsArray as $uuid) |
|---|
| 68 | $inStatement .= ", '".mysql_real_escape_string($uuid)."'"; |
|---|
| 69 | |
|---|
| 70 | $querystatement .= substr($inStatement, 2).")"; |
|---|
| 71 | |
|---|
| 72 | } else { |
|---|
| 73 | |
|---|
| 74 | $querystatement .= "id IN ("; |
|---|
| 75 | |
|---|
| 76 | $inStatement =""; |
|---|
| 77 | |
|---|
| 78 | foreach($idsArray as $id) |
|---|
| 79 | $inStatement .= ", ".((int) $id); |
|---|
| 80 | |
|---|
| 81 | $querystatement .= substr($inStatement, 2).")"; |
|---|
| 82 | |
|---|
| 83 | }//endif |
|---|
| 84 | |
|---|
| 85 | $queryresult = $this->db->query($querystatement); |
|---|
| 86 | |
|---|
| 87 | global $phpbms; |
|---|
| 88 | $db = $this->db; |
|---|
| 89 | |
|---|
| 90 | $pageTitle = "Consolidate Clients and Prospects"; |
|---|
| 91 | $phpbms->cssIncludes[] = "pages/bms/consolidateclients.css"; |
|---|
| 92 | include("header.php"); |
|---|
| 93 | |
|---|
| 94 | ?> |
|---|
| 95 | <form action="modules/bms/clients_consolidate.php" method="post" name="print_form"> |
|---|
| 96 | <div class="bodyline"> |
|---|
| 97 | |
|---|
| 98 | <h1 id="topTitle"><span><?php echo formatVariable($pageTitle) ?></span></h1> |
|---|
| 99 | |
|---|
| 100 | <p class="notes"> |
|---|
| 101 | Consolidation will relink all related records (sales orders, addresses, notes, etc...) from all the |
|---|
| 102 | selected client records to a single record selected below. It will then delete the other records. |
|---|
| 103 | This is useful when duplicate records exist in the database that already have supporting records or |
|---|
| 104 | if you have two companies that have merged. |
|---|
| 105 | </p> |
|---|
| 106 | <p> |
|---|
| 107 | <label for="conolidateTo">consolidate <?php echo count($idsArray) ?> selected records to record</label><br /> |
|---|
| 108 | <?php $uuidArray = $this->_showSelect($queryresult) ?> |
|---|
| 109 | <textarea name="uuidsArray" id="uuidsArray"><?php echo formatVariable(json_encode($uuidArray)) ?></textarea> |
|---|
| 110 | </p> |
|---|
| 111 | <p align="right"> |
|---|
| 112 | <input name="command" type="submit" class="Buttons" id="print" value="consolidate" /> |
|---|
| 113 | <input name="command" type="submit" class="Buttons" id="cancel" value="cancel"/> |
|---|
| 114 | </p> |
|---|
| 115 | |
|---|
| 116 | </div> |
|---|
| 117 | </form> |
|---|
| 118 | <?php |
|---|
| 119 | |
|---|
| 120 | include("footer.php"); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | }//end function showPicker |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | function _showSelect($queryresult){ |
|---|
| 127 | |
|---|
| 128 | ?><select name="consolidateTo"><?php |
|---|
| 129 | |
|---|
| 130 | $uuidArray = array(); |
|---|
| 131 | |
|---|
| 132 | while($therecord = $this->db->fetchArray($queryresult)){ |
|---|
| 133 | |
|---|
| 134 | $uuidArray[] = $therecord["uuid"]; |
|---|
| 135 | |
|---|
| 136 | ?><option value="<?php echo formatVariable($therecord["uuid"])?>">UUID: <?php echo formatVariable($therecord["uuid"])?> ID: <?php echo formatVariable($therecord["id"])?></option><?php |
|---|
| 137 | |
|---|
| 138 | }//endwhile |
|---|
| 139 | |
|---|
| 140 | ?></select><?php |
|---|
| 141 | |
|---|
| 142 | return $uuidArray; |
|---|
| 143 | |
|---|
| 144 | }//end function _showSelect |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | function consolidate($consolidateTo, $uuidsArray){ |
|---|
| 148 | |
|---|
| 149 | $changeArray = array(); |
|---|
| 150 | $changeIn = ""; |
|---|
| 151 | $consolidateTo = mysql_real_escape_string($consolidateTo); |
|---|
| 152 | |
|---|
| 153 | foreach(json_decode(stripslashes($uuidsArray)) as $uuid){ |
|---|
| 154 | |
|---|
| 155 | if($consolidateTo != $uuid) { |
|---|
| 156 | |
|---|
| 157 | $changeIn .= ", '".mysql_real_escape_string($uuid)."'"; |
|---|
| 158 | |
|---|
| 159 | $changeArray[] = mysql_real_escape_string($uuid); |
|---|
| 160 | |
|---|
| 161 | }//endif |
|---|
| 162 | |
|---|
| 163 | }//endforeach |
|---|
| 164 | |
|---|
| 165 | $changeIn = substr($changeIn, 2); |
|---|
| 166 | |
|---|
| 167 | //update addresses |
|---|
| 168 | $updatestatement = " |
|---|
| 169 | UPDATE |
|---|
| 170 | `addresstorecord` |
|---|
| 171 | SET |
|---|
| 172 | `recordid` = '".$consolidateTo."', |
|---|
| 173 | `primary` = 0, |
|---|
| 174 | `defaultshipto` = 0 |
|---|
| 175 | WHERE |
|---|
| 176 | `tabledefid` = 'tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083' |
|---|
| 177 | AND `recordid` IN (".$changeIn.")"; |
|---|
| 178 | |
|---|
| 179 | $this->db->query($updatestatement); |
|---|
| 180 | |
|---|
| 181 | //change notes |
|---|
| 182 | $updatestatement = " |
|---|
| 183 | UPDATE |
|---|
| 184 | `notes` |
|---|
| 185 | SET |
|---|
| 186 | `attachedid` = '".$consolidateTo."' |
|---|
| 187 | WHERE |
|---|
| 188 | `attachedtabledefid` = 'tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083' |
|---|
| 189 | AND `attachedid` IN (".$changeIn.")"; |
|---|
| 190 | |
|---|
| 191 | $this->db->query($updatestatement); |
|---|
| 192 | |
|---|
| 193 | //change attachments |
|---|
| 194 | $updatestatement = " |
|---|
| 195 | UPDATE |
|---|
| 196 | `attachments` |
|---|
| 197 | SET |
|---|
| 198 | `recordid` = '".$consolidateTo."' |
|---|
| 199 | WHERE |
|---|
| 200 | `tabledefid` = 'tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083' |
|---|
| 201 | AND `recordid` IN (".$changeIn.")"; |
|---|
| 202 | |
|---|
| 203 | $this->db->query($updatestatement); |
|---|
| 204 | |
|---|
| 205 | //change sales orders |
|---|
| 206 | $updatestatement = " |
|---|
| 207 | UPDATE |
|---|
| 208 | `invoices` |
|---|
| 209 | SET |
|---|
| 210 | `clientid` = '".$consolidateTo."' |
|---|
| 211 | WHERE |
|---|
| 212 | `clientid` IN (".$changeIn.")"; |
|---|
| 213 | |
|---|
| 214 | $this->db->query($updatestatement); |
|---|
| 215 | |
|---|
| 216 | //change AR |
|---|
| 217 | $updatestatement = " |
|---|
| 218 | UPDATE |
|---|
| 219 | `aritems` |
|---|
| 220 | SET |
|---|
| 221 | `clientid` = '".$consolidateTo."' |
|---|
| 222 | WHERE |
|---|
| 223 | `clientid` IN (".$changeIn.")"; |
|---|
| 224 | |
|---|
| 225 | $this->db->query($updatestatement); |
|---|
| 226 | |
|---|
| 227 | //change receipts |
|---|
| 228 | $updatestatement = " |
|---|
| 229 | UPDATE |
|---|
| 230 | `receipts` |
|---|
| 231 | SET |
|---|
| 232 | `clientid` = '".$consolidateTo."' |
|---|
| 233 | WHERE |
|---|
| 234 | `clientid` IN (".$changeIn.")"; |
|---|
| 235 | |
|---|
| 236 | $this->db->query($updatestatement); |
|---|
| 237 | |
|---|
| 238 | //remove clients |
|---|
| 239 | $deletestatement = " |
|---|
| 240 | DELETE FROM |
|---|
| 241 | `clients` |
|---|
| 242 | WHERE |
|---|
| 243 | `uuid` IN (".$changeIn.")"; |
|---|
| 244 | |
|---|
| 245 | $this->db->query($deletestatement); |
|---|
| 246 | |
|---|
| 247 | global $phpbms; |
|---|
| 248 | $db = $this->db; |
|---|
| 249 | |
|---|
| 250 | $pageTitle = "Consolidate Clients and Prospects"; |
|---|
| 251 | $phpbms->cssIncludes[] = "pages/bms/consolidateclients.css"; |
|---|
| 252 | |
|---|
| 253 | $statusmessage = count($changeArray)." record(s) consolidated successfully"; |
|---|
| 254 | |
|---|
| 255 | include("header.php"); |
|---|
| 256 | |
|---|
| 257 | ?> |
|---|
| 258 | <form action="clients_consolidate.php" method="post" name="print_form"> |
|---|
| 259 | <div class="bodyline"> |
|---|
| 260 | |
|---|
| 261 | <h1 id="topTitle"><span><?php echo formatVariable($pageTitle) ?></span></h1> |
|---|
| 262 | |
|---|
| 263 | <p class="notes"> |
|---|
| 264 | Consolidation has completed. All related infomation has been transferred to a single client. You may need |
|---|
| 265 | to remove any duplicate addresses that may be associated with the client record. |
|---|
| 266 | </p> |
|---|
| 267 | <p align="right"> |
|---|
| 268 | <input name="command" type="submit" class="Buttons" id="done" value="done"/> |
|---|
| 269 | </p> |
|---|
| 270 | |
|---|
| 271 | </div> |
|---|
| 272 | </form> |
|---|
| 273 | <?php |
|---|
| 274 | |
|---|
| 275 | include("footer.php"); |
|---|
| 276 | |
|---|
| 277 | }//end function consolidate |
|---|
| 278 | |
|---|
| 279 | }//end class clientConsolidator |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * PROCESSING ================================================================== |
|---|
| 284 | */ |
|---|
| 285 | if(!isset($noOutput)){ |
|---|
| 286 | |
|---|
| 287 | require_once("../../include/session.php"); |
|---|
| 288 | |
|---|
| 289 | $consolidator = new clientConsolidator($db); |
|---|
| 290 | |
|---|
| 291 | $_POST = addSlashesToArray($_POST); |
|---|
| 292 | |
|---|
| 293 | if(!isset($_POST["command"])) |
|---|
| 294 | $error = new appError(200, "passed parameters are not set"); |
|---|
| 295 | |
|---|
| 296 | if($_POST["command"] === "cancel" || $_POST["command"] === "done") |
|---|
| 297 | goURL("../../search.php?id=tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083"); |
|---|
| 298 | |
|---|
| 299 | if(!isset($_POST["consolidateTo"]) || !isset($_POST["uuidsArray"])) |
|---|
| 300 | $error = new appError(200, "passed parameters are not set"); |
|---|
| 301 | |
|---|
| 302 | $consolidator->consolidate($_POST["consolidateTo"], $_POST["uuidsArray"]); |
|---|
| 303 | |
|---|
| 304 | }//endif |
|---|
| 305 | ?> |
|---|