Changeset 485 for trunk/phpbms/modules/bms/include/clients.php
- Timestamp:
- 04/07/09 11:44:18 (3 years ago)
- Files:
-
- 1 modified
-
trunk/phpbms/modules/bms/include/clients.php (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpbms/modules/bms/include/clients.php
r431 r485 40 40 if(class_exists("phpbmsTable")){ 41 41 class clients extends phpbmsTable{ 42 42 43 var $availablePaymentMethodIDs = array(); 44 var $availableShippingMethodIDs = array(); 45 var $availableDiscountIDs = array(); 46 var $availableTaxIDs = array(); 47 var $availableUserIDs = array(); 48 var $verifyErrors = array(); 49 43 50 function checkForInvoices($id){ 44 51 $querystatement="SELECT id FROM invoices WHERE clientid=".((int) $id); 45 52 $queryresult = $this->db->query($querystatement); 46 53 47 54 return !($this->db->numRows($queryresult)===0); 48 55 }//end method 49 50 56 57 51 58 // CLASS OVERRIDES =================================================================== 52 59 // =================================================================================== 53 60 54 61 function clients($db,$tabledefid = 0,$backurl = NULL){ 55 62 56 63 $this->phpbmsTable($db,$tabledefid,$backurl); 57 64 58 65 $this->address = new addresstorecord($db, 306); 59 66 60 67 }//end function - init 61 68 62 69 63 70 function getDefaults(){ … … 66 73 67 74 $therecord["type"] = DEFAULT_CLIENTTYPE; 68 75 69 76 if($therecord["type"] == "client") { 70 77 … … 74 81 75 82 }//end if 76 83 77 84 $therecord["webaddress"] = "http://"; 78 85 79 86 //now for the address information. 80 87 $addressinfo = $this->address->getDefaults(); 81 88 unset($addressinfo["id"], $addressinfo["createdby"], $addressinfo["creationdate"], $addressinfo["modifiedby"], $addressinfo["modifieddate"]); 82 89 $addressinfo["addressid"] = NULL; 83 90 84 91 return array_merge($therecord, $addressinfo); 85 92 86 93 }//end function - getDefaults 87 88 94 95 89 96 function getRecord($id){ 90 97 91 98 $id = (int) $id; 92 99 93 100 $therecord = parent::getRecord($id); 94 101 95 102 if($therecord["id"]){ 96 103 //need to grab the address as well 97 104 98 105 $querystatement = " 99 106 SELECT … … 107 114 108 115 $queryresult = $this->db->query($querystatement); 109 116 110 117 $addressinfo = $this->db->fetchArray($queryresult); 111 118 112 119 if($addressinfo) { 113 120 114 121 $addressinfo = $this->address->getRecord($addressinfo["id"]); 115 122 116 123 } else { 117 124 118 125 $addressinfo = $this->address->getDefaults(); 119 126 $addressinfo["addressid"] = NULL; 120 121 }//endif 127 128 }//endif 122 129 123 130 unset($addressinfo["id"], $addressinfo["notes"], $addressinfo["email"], $addressinfo["createdby"], $addressinfo["creationdate"], $addressinfo["modifiedby"], $addressinfo["modifieddate"]); 124 131 125 132 $therecord = array_merge($therecord, $addressinfo); 126 133 127 134 }//endif 128 135 129 136 return $therecord; 130 137 131 138 }//end function - getRecord 132 139 140 141 function populatePaymentMethodArray(){ 142 143 $this->availablePaymentMethodIDs = array(); 144 145 $querystatement = " 146 SELECT 147 `id` 148 FROM 149 `paymentmethods`; 150 "; 151 152 $queryresult = $this->db->query($querystatement); 153 154 $this->availablePaymentMethodIDs[] = 0;//for none 155 156 while($therecord = $this->db->fetchArray($queryresult)) 157 $this->availablePaymentMethodIDs[] = $therecord["id"]; 158 159 }//end method --populatePaymentMethodArray-- 160 161 162 function populateShippingMethodArray(){ 163 164 $this->availableShippingMethodIDs = array(); 165 166 $querystatement = " 167 SELECT 168 `id` 169 FROM 170 `shippingmethods`; 171 "; 172 173 $queryresult = $this->db->query($querystatement); 174 175 $this->availableShippingMethodIDs[] = 0;//for none 176 177 while($therecord = $this->db->fetchArray($queryresult)) 178 $this->availableShippingMethodIDs[] = $therecord["id"]; 179 180 }//end method --populateShippingMethodArray-- 181 182 183 function populateDiscountArray(){ 184 185 $this->availableDiscountIDs = array(); 186 187 $querystatement = " 188 SELECT 189 `id` 190 FROM 191 `discounts`; 192 "; 193 194 $queryresult = $this->db->query($querystatement); 195 196 $this->availableDiscountIDs[] = 0;//for none 197 198 while($therecord = $this->db->fetchArray($queryresult)) 199 $this->availableDiscountIDs[] = $therecord["id"]; 200 201 }//end method --populateDiscountArray-- 202 203 204 function populateTaxArray(){ 205 206 $this->availableTaxIDs = array(); 207 208 $querystatement = " 209 SELECT 210 `id` 211 FROM 212 `tax`; 213 "; 214 215 $queryresult = $this->db->query($querystatement); 216 217 $this->availableTaxIDs[] = 0;//for none 218 219 while($therecord = $this->db->fetchArray($queryresult)) 220 $this->availableTaxIDs[] = $therecord["id"]; 221 222 }//end method --populateTaxArray-- 223 224 225 function populateUserArray(){ 226 227 $this->availableUserIDs = array(); 228 229 $querystatement = " 230 SELECT 231 `id` 232 FROM 233 `users`; 234 "; 235 236 $queryresult = $this->db->query($querystatement); 237 238 $this->availableUserIDs[] = 0;//for none 239 240 while($therecord = $this->db->fetchArray($queryresult)) 241 $this->availableUserIDs[] = $therecord["id"]; 242 243 }//end method --populateUserArray-- 244 245 246 function verifyVariables($variables){ 247 248 if(isset($variables["type"])){ 249 switch($variables["type"]){ 250 251 case "prospect": 252 253 if(isset($variables["becameclient"])){ 254 if($variables["becameclient"] !== "" || $variables["becameclient"] !== NULL) 255 $this->verifyErrors[] = "Records with `type` of 'prospect' 256 must have the `becameclient` field kept blank."; 257 }//end if 258 259 if(isset($variables["hascredit"])){ 260 if($variables["hascredit"]) 261 $this->verifyErrors[] = "Records with `type` of 'prospect' 262 must have the `hascredit` field kept blank or 0."; 263 }//end if 264 265 if(isset($variables["creditlimit"])){ 266 if($variables["creditlimit"]) 267 $this->verifyErrors[] = "Records with `type` of 'prospect' 268 must have the `creditlimit` field kept blank or 0."; 269 }//end if 270 271 break; 272 273 case "client": 274 if(isset($variables["becameclient"])){ 275 //Possibly run through string to date functions 276 if(!$variables["becameclient"]) 277 $this->verifyErrors[] = "Records with `type` of 'client' 278 must have not have the `becameclient` field blank."; 279 }else 280 $this->verifyErrors[] = "Records with `type` of 'client' 281 must set the `becameclient` field."; 282 break; 283 284 default: 285 $this->verifyErrors[] = "The value of the `type` field is invalid. 286 It must either be 'prospect' or 'client'."; 287 break; 288 289 }//end switch 290 }else 291 $this->verifyErrors[] = "The `type` field must be set."; 292 293 //check for currency on credit limit (((real value) >= 0 ... non-negative) 294 if(isset($variables["creditlimit"])) 295 if(!is_numeric($variables["creditlimit"]) && $variables["creditlimit"]) 296 $this->verifyErrors[] = "The `creditlimit` field must be a real number or equivalent to zero."; 297 298 //----------------[ phone & email ]------------------------------------------------------ 299 //check valid email 300 if(isset($variables["email"])) 301 if( $variables["email"] !== NULL && $variables["email"] !== "" && !validateEmail($variables["email"])) 302 $this->verifyErrors[] = "The `email` field must have a valid email or must be left blank."; 303 304 //check valid homephone 305 if(isset($variables["homephone"])) 306 if( $variables["homephone"] !== NULL && $variables["homephone"] !== "" && !validatePhone($variables["homephone"])) 307 $this->verifyErrors[] = "The `homephone` field must have a valid phone number (as set in configuration) or must be left blank."; 308 309 //check valid workphone 310 if(isset($variables["workphone"])) 311 if( $variables["workphone"] !== NULL && $variables["workphone"] !== "" && !validatePhone($variables["workphone"])) 312 $this->verifyErrors[] = "The `workphone` field must have a valid phone number (as set in configuration) or must be left blank."; 313 314 //check valid mobilephone 315 if(isset($variables["mobilephone"])) 316 if( $variables["mobilephone"] !== NULL && $variables["mobilephone"] !== "" && !validatePhone($variables["mobilephone"])) 317 $this->verifyErrors[] = "The `mobilephone` field must have a valid phone number (as set in configuration) or must be left blank."; 318 319 //check valid fax 320 if(isset($variables["fax"])) 321 if( $variables["fax"] !== NULL && $variables["fax"] !== "" && !validatePhone($variables["fax"])) 322 $this->verifyErrors[] = "The `fax` field must have a valid phone number (as set in configuration) or must be left blank."; 323 324 //check valid otherphone 325 if(isset($variables["otherphone"])) 326 if( $variables["otherphone"] !== NULL && $variables["otherphone"] !== "" && !validatePhone($variables["otherphone"])) 327 $this->verifyErrors[] = "The `otherphone` field must have a valid phone number (as set in configuration) or must be left blank."; 328 329 //check bool on has credit 330 if(isset($variables["hascredit"])) 331 if($variables["hascredit"] && $variables["hascredit"] != 1) 332 $this->verifyErrors[] = "The `hascredit` field must be a boolean (equivalent to 0 or exactly 1)."; 333 334 335 //----------------[ Order Defaults]------------------------------------------------------ 336 337 //Payement Method 338 if(isset($variables["paymentmethodid"])){ 339 340 if( !$variables["paymentmethodid"] || ((int)$variables["paymentmethodid"]) > 0 ){ 341 342 if(!count($this->availablePaymentMethodIDs)) 343 $this->populatePaymentMethodArray(); 344 345 if(!in_array($variables["paymentmethodid"], $this->availablePaymentMethodIDs)) 346 $this->verifyErrors[] = "The `paymentmethodid` field does not give an existing/acceptable payment method id number."; 347 348 }else 349 $this->verifyErrors[] = "The `addroleid` field must be a non-negative number or equivalent to 0."; 350 351 }//end if 352 353 if(isset($variables["shippingmethodid"])){ 354 355 if( !$variables["shippingmethodid"] || ((int)$variables["shippingmethodid"]) > 0){ 356 357 if(!count($this->availableShippingMethodIDs)) 358 $this->populateShippingMethodArray(); 359 360 if(!in_array($variables["shippingmethodid"], $this->availableShippingMethodIDs)) 361 $this->verifyErrors[] = "The `shippingmethodid` field does not give an existing/acceptable shipping method id number."; 362 363 }else 364 $this->verifyErrors[] = "The `shippingmethodid` field must be a non-negative number or equivalent to 0."; 365 366 }//end if 367 368 if(isset($variables["discountid"])){ 369 370 if( !$variables["discountid"] || ((int)$variables["discountid"]) > 0){ 371 372 if(!count($this->availableDiscountIDs)) 373 $this->populateDiscountArray(); 374 375 if(!in_array($variables["discountid"], $this->availableDiscountIDs)) 376 $this->verifyErrors[] = "The `discount` field does not give an existing/acceptable discount id number."; 377 378 }else 379 $this->verifyErrors[] = "The `discountid` field must be a non-negative number or equivalent to 0."; 380 381 }//end if 382 383 if(isset($variables["taxareaid"])){ 384 385 if( !$variables["taxareaid"] || ((int)$variables["taxareaid"]) > 0){ 386 387 if(!count($this->availableTaxIDs)) 388 $this->populateTaxArray(); 389 390 if(!in_array($variables["taxareaid"], $this->availableTaxIDs)) 391 $this->verifyErrors[] = "The `taxareaid` field does not give an existing/acceptable tax id number."; 392 393 }else 394 $this->verifyErrors[] = "The `taxareaid` field must be a non-negative number or equivalent to 0."; 395 396 }//end if 397 398 //---------------------[ end order defaults ]---------------------------------------- 399 400 //check sales manager id 401 if(isset($variables["salesmanagerid"])){ 402 403 if( !$variables["salesmanagerid"] || ((int)$variables["salesmanagerid"]) > 0 ){ 404 405 if(!count($this->availableUserIDs)) 406 $this->populateUserArray(); 407 408 if(!in_array($variables["salesmanagerid"], $this->availableUserIDs)) 409 $this->verifyErrors[] = "The `salesmanagerid` field does not give an existing/acceptable user id number."; 410 411 }else 412 $this->verifyErrors[] = "The `salesmanagerid` field must be a non-negative number or equivalent to 0."; 413 414 }//end if 415 416 417 return parent::verifyVariables($variables); 418 419 }//end method 420 133 421 134 422 function prepareVariables($variables){ 135 423 136 424 if(isset($variables["webaddress"])) 137 if ($variables["webaddress"]=="http://") 425 if ($variables["webaddress"]=="http://") 138 426 $variables["webaddress"] = NULL; 139 427 140 428 if(!isset($variables["type"])) 141 429 $variables["type"] = "client"; 142 430 143 431 if($variables["type"] == "prospect"){ 144 432 … … 149 437 $variables["type"] = "client"; 150 438 if(!isset($variables["becameclient"])) 439 $variables["becameclient"] = NULL; 440 if(!$variables["becameclient"]) 151 441 $variables["becameclient"] = dateToString(mktime()); 152 442 }//end if 153 443 154 444 return $variables; 155 445 156 446 }//end method 157 158 447 448 159 449 function updateRecord($variables, $modifiedby = NULL){ 160 161 $variables = $this->prepareVariables($variables);162 450 451 //$variables = $this->prepareVariables($variables); 452 163 453 $thereturn = parent::updateRecord($variables, $modifiedby); 164 454 455 $variables["recordid"] = $variables["id"];//here to pass addresstorecord validation 456 $variables["tabledefid"] = 2;//here to pass addresstorecord validation 165 457 //need to update the address 166 458 $variables["id"] = $variables["addressid"]; … … 174 466 unset($this->address->fields["creationdate"]); 175 467 176 $this->address->updateRecord($variables, $modifiedby); 468 $variables = $this->address->prepareVariables($variables); 469 $errorArray = $this->address->verifyVariables($variables); 470 if(!count($errorArray)){ 471 $this->address->updateRecord($variables, $modifiedby); 472 }else{ 473 foreach($errorArray as $error) 474 $logError = new appError(-910, $error, "Address Verification Error"); 475 }//end if 177 476 178 477 //restore the fields 179 478 $this->address->getTableInfo(); 180 479 181 480 return $thereturn; 182 481 183 482 }//end method - updateRecord 184 185 483 484 186 485 function insertRecord($variables, $createdby = NULL){ 187 188 $variables = $this->prepareVariables($variables);189 486 487 //$variables = $this->prepareVariables($variables); 488 190 489 $newid = parent::insertRecord($variables, $createdby); 191 490 192 491 //need to create the address and addresstorecord id 193 492 // make sure we are not setting extra info … … 200 499 $variables["defaultshipto"] = 1; 201 500 $variables["primary"] = 1; 202 203 $this->address->insertRecord($variables, $createdby); 501 502 $variables = $this->address->prepareVariables($variables); 503 $errorArray = $this->address->verifyVariables($variables); 504 if(!count($errorArray)){ 505 $this->address->insertRecord($variables, $createdby); 506 }else{ 507 foreach($errorArray as $error) 508 $logError = new appError(-910, $error, "Address Verification Error"); 509 }//end if 204 510 205 511 //restore the fields 206 512 $this->address->getTableInfo(); 207 513 208 514 return $newid; 209 515 210 516 }//end method - insertRecord 211 517 212 518 }//end class 213 519 214 520 }//end if 215 521 … … 219 525 220 526 function mark_asclient(){ 221 527 222 528 //passed variable is array of user ids to be revoked 223 $whereclause = $this->buildWhereClause(); 224 529 $whereclause = $this->buildWhereClause(); 530 225 531 $querystatement = "UPDATE clients SET clients.type=\"client\",modifiedby=\"".$_SESSION["userinfo"]["id"]."\" WHERE (".$whereclause.");"; 226 532 $queryresult = $this->db->query($querystatement); 227 533 228 534 $message = $this->buildStatusMessage(); 229 535 $message.=" converted to client."; 230 536 return $message; 231 537 } 232 233 538 539 234 540 //Stamp Comments Field with info packet sent 235 541 function stamp_infosent(){ 236 542 237 543 //passed variable is array of user ids to be revoked 238 544 $whereclause = $this->buildWhereClause(); 239 545 240 546 $querystatement = " 241 547 UPDATE 242 clients 548 clients 243 549 SET 244 clients.comments = concat('Information Packet Sent', char(10), clients.comments), 550 clients.comments = concat('Information Packet Sent', char(10), clients.comments), 245 551 clients.modifiedby=".$_SESSION["userinfo"]["id"].", 246 552 clients.modifieddate = NOW() 247 553 WHERE (".$whereclause.") AND clients.comments IS NOT NULL"; 248 554 $queryresult = $this->db->query($querystatement); 249 555 250 556 $affected = $this->db->affectedRows(); 251 557 252 558 $querystatement = " 253 559 UPDATE 254 clients 560 clients 255 561 SET 256 clients.comments = 'Information Packet Sent', 562 clients.comments = 'Information Packet Sent', 257 563 clients.modifiedby=".$_SESSION["userinfo"]["id"].", 258 564 clients.modifieddate = NOW() … … 261 567 262 568 $affected += $this->db->affectedRows(); 263 569 264 570 $message = $this->buildStatusMessage($affected); 265 571 $message.=" marked as info packet sent."; 266 572 return $message; 267 573 } 268 269 574 575 270 576 //remove prospects 271 577 function delete_prospects(){ 272 578 273 579 //passed variable is array of user ids to be revoked 274 580 $clientWhereClause = $this->buildWhereClause(); … … 282 588 (".$clientWhereClause.") 283 589 AND clients.type = 'prospect'"; 284 590 285 591 $queryresult = $this->db->query($querystatement); 286 592 287 593 //build array of ids to be removed 288 594 $deleteIDs = array(); … … 291 597 292 598 if(count($deleteIDs)){ 293 599 294 600 $a2rWhere = $this->buildWhereClause("recordid", $deleteIDs); 295 601 … … 303 609 tabledefid = 2 304 610 AND (".$a2rWhere.")"; 305 611 306 612 $a2rResult = $this->db->query($querystatement); 307 613 308 614 $addressIDs = array(); 309 615 while($a2r = $this->db->fetchArray($a2rResult)) 310 616 array_push($addressIDs, $a2r["addressid"]); 311 312 // delete all a2r records for prospect 617 618 // delete all a2r records for prospect 313 619 $deletestatement = " 314 620 DELETE FROM … … 317 623 tabledefid = 2 318 624 AND (".$a2rWhere.")"; 319 625 320 626 $this->db->query($deletestatement); 321 627 322 628 //now go get a list of orphaned addresses 323 629 $querystatement = " 324 SELECT 325 addresses.id, 326 addresstorecord.id as a2rid 327 FROM 630 SELECT 631 addresses.id, 632 addresstorecord.id as a2rid 633 FROM 328 634 addresses LEFT JOIN addresstorecord ON addresstorecord.addressid = addresses.id 329 635 WHERE 330 636 ".$this->buildWhereClause("addresses.id", $addressIDs); 331 637 332 638 $addressResult = $this->db->query($querystatement); 333 639 334 640 $addressIDs = array(); 335 641 while($address = $this->db->fetchArray($addressResult)) 336 642 if(!$address["a2rid"]) 337 643 array_push($addressIDs, $address["id"]); 338 644 339 645 if(count($addressIDs)){ 340 341 //delete orphaned addresses 646 647 //delete orphaned addresses 342 648 $deletestatement = " 343 DELETE FROM 649 DELETE FROM 344 650 addresses 345 651 WHERE 346 652 ".$this->buildWhereClause("addresses.id", $addressIDs); 347 653 348 654 $this->db->query($deletestatement); 349 655 350 656 }//endif - addressids 351 657 … … 359 665 WHERE 360 666 ".$invoiceWhereClause; 361 667 362 668 $invoiceresult = $this->db->query($invoicestatement); 363 669 364 670 //build invoice id array 365 671 $invoiceids = array(); 366 672 while($therecord = $this->db->fetchArray($invoiceresult)) 367 673 array_push($invoiceids, $therecord["id"]); 368 674 369 675 if(count($invoiceids)) { 370 676 $invoiceWhereClause = $this->buildWhereClause("invoices.id", $invoiceids); 371 677 372 678 $lineitemWhereClause = $this->buildWhereClause("invoiceid", $invoiceids); 373 679 374 680 $lineItemDeleteStatement = " 375 681 DELETE FROM … … 377 683 WHERE 378 684 ".$lineitemWhereClause; 379 685 380 686 $queryresult = $this->db->query($lineItemDeleteStatement); 381 687 382 688 $statushistoryDeleteStatement = " 383 689 DELETE FROM … … 385 691 WHERE 386 692 ".$lineitemWhereClause; 387 693 388 694 $queryresult = $this->db->query($statushistoryDeleteStatement); 389 695 390 696 $invoiceDeleteStatement = " 391 697 DELETE FROM 392 698 invoices 393 WHERE 699 WHERE 394 700 ".$invoiceWhereClause; 395 701 396 702 $queryresult = $this->db->query($invoiceDeleteStatement); 397 703 398 704 }//end if 399 705 … … 402 708 403 709 $deletestatement = " 404 DELETE FROM 710 DELETE FROM 405 711 clients 406 WHERE 712 WHERE 407 713 ".$delWhere; 408 714 409 715 $this->db->query($deletestatement); 410 411 }//endif - count deleteIDS 412 413 716 717 }//endif - count deleteIDS 718 719 414 720 $message = $this->buildStatusMessage(count($deleteIDs)); 415 721 $message.=" deleted."; 416 return $message; 417 722 return $message; 723 418 724 }// end method - delete_prospects 419 420 725 726 421 727 function massEmail(){ 422 728 if(DEMO_ENABLED != "true"){ … … 428 734 } 429 735 430 736 431 737 }//end class 432 738 }//end if 433 739 if(class_exists("phpbmsImport")){ 434 740 class clientsImport extends phpbmsImport{ 435 436 741 742 437 743 function clientsImport($table, $importType = "csv"){ 438 744 439 745 if($importType == "sugarcrm"){ 440 746 441 747 $importType = "csv"; 442 748 $switchedFrom = "sugarcrm"; 443 749 444 750 }//end if 445 751 446 752 parent::phpbmsImport($table, $importType); 447 753 448 754 if(isset($switchedFrom)) 449 755 $this->importType = $switchedFrom; 450 756 451 757 }//end method --clientsImport-- 452 453 758 759 454 760 function _parseFromData($data){ 455 761 456 762 if($this->importType == "sugarcrm"){ 457 763 458 764 $this->importType = "csv"; 459 765 $switchedFrom = "sugarcrm"; 460 766 461 767 }//end if 462 768 463 769 $thereturn = parent::_parseFromData($data); 464 770 465 771 if(isset($switchedFrom)) 466 772 $this->importType = $switchedFrom; 467 773 468 774 return $thereturn; 469 775 470 776 }//end method --_parseFromFile-- 471 472 777 778 473 779 function _formatSugarVariables($rows, $titles){ 474 780 475 781 //Replace the titles with valid ones 476 782 //(At the moment we only really need … … 480 786 $newTitles = array(); 481 787 foreach($titles as $index => $name){ 482 788 483 789 switch($name){ 484 790 485 791 case "name": 486 792 $newTitles[] = "company"; 487 793 break; 488 794 489 795 case "date_entered": 490 796 $newTitles[] = "becameclient"; 491 797 break; 492 798 493 799 case "description": 494 800 $newTitles[] = "comments"; 495 801 break; 496 802 497 803 case "deleted": 498 804 $newTitles[] = "inactive"; 499 805 break; 500 806 501 807 case "account_type": 502 808 $newTitles[] = "type"; 503 809 break; 504 810 505 811 case "industry": 506 812 $newTitles[] = "category"; 507 813 break; 508 814 509 815 case "phone_fax": 510 816 $newTitles[] = "fax"; 511 817 break; 512 818 513 819 case "billing_address_street": 514 820 $newTitles[] = "address1"; 515 821 break; 516 822 517 823 case "billing_address_city": 518 824 $newTitles[] = "city"; 519 825 break; 520 826 521 827 case "billing_address_state": 522 828 $newTitles[] = "state"; 523 829 break; 524 830 525 831 case "billing_address_postalcode": 526 832 $newTitles[] = "postalcode"; 527 833 break; 528 834 529 835 case "billing_address_country": 530 836 $newTitles[] = "country"; 531 837 break; 532 838 533 839 case "phone_office": 534 840 $newTitles[] = "workphone"; 535 841 break; 536 842 537 843 case "phone_alternate": 538 844 $newTitles[] = "otherphone"; 539 845 break; 540 846 541 847 case "website": 542 848 $newTitles[] = "webaddress"; 543 849 break; 544 850 545 851 case "shipping_address_street": 546 852 $newTitles[] = "shipaddress1"; 547 853 break; 548 854 549 855 case "shipping_address_city": 550 856 $newTitles[] = "shipcity"; 551 857 break; 552 858 553 859 case "shipping_address_state": 554 860 $newTitles[] = "shipstate"; 555 861 break; 556 862 557 863 case "shipping_address_postalcode": 558 864 $newTitles[] = "shippostalcode"; 559 865 break; 560 866 561 867 case "shipping_address_country": 562 868 $newTitles[] = "shipcountry"; 563 869 break; 564 870 565 871 }//end switch 566 872 567 873 }//end foreach 568 569 874 875 570 876 $newRows = array(); 571 877 foreach($rows as $rowData){ 572 878 573 879 $newRowData = array(); 574 880 $addComments = ""; 575 881 foreach($rowData as $name => $data){ 576 882 577 883 switch($name){ 578 884 579 885 case "name": 580 886 $newRowData["company"] = trim($data); 581 887 break; 582 888 583 889 case "date_entered": 584 890 $newRowData["becameclient"] = trim($data); 585 891 break; 586 892 587 893 case "description": 588 894 $newRowData["comments"] = trim($data); 589 895 break; 590 896 591 897 case "deleted": 592 898 $newRowData["inactive"] = trim($data); 593 899 break; 594 900 595 901 case "industry": 596 902 $newRowData["category"] = trim($data); 597 903 break; 598 904 599 905 case "account_type": 600 906 $newRowData["type"] = trim($data); 601 907 break; 602 908 603 909 case "phone_fax": 604 910 $newRowData["fax"] = trim($data); 605 911 break; 606 912 607 913 case "billing_address_street": 608 914 $newRowData["address1"] = trim($data); 609 915 break; 610 916 611 917 case "billing_address_city": 612 918 $newRowData["city"] = trim($data); 613 919 break; 614 920 615 921 case "billing_address_state": 616 922 $newRowData["state"] = trim($data); 617 923 break; 618 924 619 925 case "billing_address_postalcode": 620 926 $newRowData["postalcode"] = trim($data); 621 927 break; 622 928 623 929 case "billing_address_country": 624 930 $newRowData["country"] = trim($data); 625 931 break; 626 932 627 933 case "phone_office": 628 934 $newRowData["workphone"] = trim($data); 629 935 break; 630 936 631 937 case "phone_alternate": 632 938 $newRowData["otherphone"] = trim($data); 633 939 break; 634 940 635 941 case "website": 636 942 $newRowData["webaddress"] = trim($data); 637 943 break; 638 944 639 945 case "shipping_address_street": 640 946 $newRowData["shipaddress1"] = trim($data); 641 947 break; 642 948 643 949 case "shipping_address_city": 644 950 $newRowData["shipcity"] = trim($data); 645 951 break; 646 952 647 953 case "shipping_address_state": 648 954 $newRowData["shipstate"] = trim($data); 649 955 break; 650 956 651 957 case "shipping_address_postalcode": 652 958 $newRowData["shippostalcode"] = trim($data); 653 959 break; 654 960 655 961 case "shipping_address_country": 656 962 $newRowData["shipcountry"] = trim($data); 657 963 break; 658 964 659 965 case "annual_revenue": 660 966 case "rating": … … 665 971 $addComments .= "\n".str_replace("_"," ",$name).": ".trim($data); 666 972 break; 667 973 668 974 }//end switch 669 975 670 976 }//end foreach 671 977 672 978 if($newRowData["type"] == "prospect") 673 979 $newRowData["becameclient"] = NULL; 674 980 else 981 $newRowData["type"] = "client"; 982 675 983 $newRowData["comments"] .= $addComments; 676 984 $newRows[] = $newRowData; 677 985 678 986 }//end foreach 679 987 680 988 $thereturn["rows"] = $newRows; 681 989 $thereturn["titles"] = $newTitles; 682 990 return $thereturn; 683 991 684 992 }//end method --_formatSugarvariables-- 685 686 993 994 687 995 function importRecords($rows, $titles){ 688 996 689 997 switch($this->importType){ 690 998 691 999 case "sugarcrm": 692 1000 $thereturn = $this->_formatSugarVariables($rows, $titles); 693 1001 $rows = $thereturn["rows"]; 694 1002 $titles = $thereturn["titles"]; 695 696 case "csv": 1003 1004 case "csv": 697 1005 //count total fieldnames (top row of csv document) 698 1006 $fieldNum = count($titles); 699 1007 700 1008 //the file starts at line number 1, but since line 1 is 701 1009 //supposed to be the fieldnames in the table(s), the lines 702 1010 //being insereted start @ 2. 703 1011 $rowNum = 2; 704 1012 705 1013 //get the data one row at a time 706 1014 foreach($rows as $rowData){ 707 708 $theid = 0; 709 1015 1016 $theid = 0; // set for when verifification does not pass 1017 $verify = array(); //set for when number of field rows does not match number of titles 1018 710 1019 //trim off leading/trailing spaces 711 1020 $trimmedRowData = array(); 712 1021 foreach($rowData as $name => $data) 713 1022 $trimmedRowData[$name] = trim($data); 714 1023 715 1024 //check to see if number of fieldnames is consistent for each row 716 1025 $rowFieldNum = count($trimmedRowData); 717 1026 718 1027 //if valid, insert, if not, log error and don't insert. 719 if($rowFieldNum == $fieldNum) 720 $theid = $this->table->insertRecord($trimmedRowData); 721 else 1028 if($rowFieldNum == $fieldNum){ 1029 //$trimmedRowData = $this->table->prepareVariables($trimmedRowData); 1030 $verify = $this->table->verifyVariables($trimmedRowData); 1031 if(!count($verify)) 1032 $theid = $this->table->insertRecord($trimmedRowData); 1033 }else 722 1034 $this->error .= '<li> incorrect amount of fields for line number '.$rowNum.'.</li>'; 723 1035 724 1036 if($theid){ 725 1037 //keep track of the ids in the transaction to be able to select them 726 1038 //for preview purposes 727 1039 $this->transactionIDs[] = $theid; 728 1040 729 1041 //get first id to correct auto increment 730 1042 if(!$this->revertID) 731 1043 $this->revertID = $theid; 732 1044 733 1045 //If it is a sugarcrm import, insert the shipping address as well 1046 $addressVerify = array(); 734 1047 if($this->importType == "sugarcrm"){ 735 736 1048 1049 737 1050 $variables = array(); 738 1051 if($trimmedRowData["shipaddress1"]) $variables["address1"] = $trimmedRowData["shipaddress1"]; … … 740 1053 if($trimmedRowData["shipstate"]) $variables["state"] = $trimmedRowData["shipstate"]; 741 1054 if($trimmedRowData["shipcountry"]) $variables["country"] = $trimmedRowData["shipcountry"]; 742 1055 743 1056 //check to see if there is a shipping address 744 1057 if(count($variables)){ 745 1058 746 1059 //If there is a shipping address, we need to make any others' 747 1060 //`defaultshipto` to 0 748 1061 749 1062 $querystatement = " 750 1063 UPDATE … … 757 1070 `addresstorecord`.`tabledefid` = '2'; 758 1071 "; 759 1072 760 1073 $this->table->db->query($querystatement); 761 1074 762 1075 $variables["title"] = "Main Shipping Addresss"; 763 1076 $variables["tabledefid"] = 2; … … 765 1078 $variables["defaultshipto"] = 1; 766 1079 $variables["primary"] = 0; 767 768 $this->table->address->insertRecord($variables); 769 1080 $variables["existingaddressid"] = false; 1081 1082 $addressVerify = $this->table->address->verifyVariables($variables);//verify address 1083 if(!count($addressVerify))//check for errors 1084 $this->table->address->insertRecord($variables);//insert if no errors 1085 770 1086 }//end if 771 1087 772 1088 }//end if 773 1089 }else 774 1090 $this->error .= '<li> failed insert for line number '.$rowNum.'.</li>'; 775 1091 1092 foreach($verify as $error)//log verify errors for display 1093 $this->error .= '<li class="subError">'.$error.'</li>'; 1094 1095 if(isset($addressVerify)) 1096 foreach($addressVerify as $error)//log address verify errors for display 1097 $this->error .= '<li class="subError">'.$error.'</li>'; 1098 776 1099 $rowNum++; 777 1100 778 1101 }//end foreach 779 1102 break; 780 1103 781 1104 }//end switch 782 1105 783 1106 }//end method --importRecords-- 784 785 1107 1108 786 1109 function _getTransactionData(){ 787 1110 788 1111 $inStatement = ""; 789 1112 foreach($this->transactionIDs as $theid) … … 792 1115 if($inStatement) 793 1116 $inStatement = substr($inStatement, 0, -1); 794 1117 else 1118 $inStatement = "0"; 1119 795 1120 //There are two cases to minimize joins for csv files 796 1121 switch($this->importType){ 797 1122 798 1123 case "sugarcrm": 799 1124 $querystatement = " … … 854 1179 "; 855 1180 break; 856 1181 857 1182 case "csv": 858 1183 $querystatement = " … … 895 1220 `addresses`.`postalcode`, 896 1221 `addresses`.`country`, 897 `addresses`.`phone`, 898 `addresses`.`email` 1222 `addresses`.`phone` 899 1223 FROM 900 1224 ((clients INNER JOIN addresstorecord ON clients.id = addresstorecord.recordid AND addresstorecord.tabledefid=2 AND addresstorecord.primary=1) INNER JOIN addresses ON addresstorecord.addressid = addresses.id) … … 905 1229 "; 906 1230 break; 907 1231 908 1232 }//end switch 909 1233 910 1234 $queryresult = $this->table->db->query($querystatement); 911 1235 912 1236 while($therecord = $this->table->db->fetchArray($queryresult)) 913 1237 $this->transactionRecords[] = $therecord; 914 915 1238 1239 916 1240 }//end method --_gettransactionData-- 917 918 1241 1242 919 1243 function displayTransaction($recordsArray, $fieldsArray){ 920 1244 921 1245 if(count($recordsArray) && count($fieldsArray)){ 922 1246 923 1247 //Need to include addresses in the fieldArray 924 1248 925 1249 //list of values that should not be displayed 926 1250 $removalArray = array("id", "modifiedby", "modifieddate", "createdby", "creationdate", "notes", "title", "shiptoname"); 927 1251 //gets the address table's columnnames/information (fields) 928 1252 $addressArray = $this->table->address->fields; 929 1253 930 1254 //gets rid of the values that should not be displayed 931 1255 foreach($removalArray as $removalField){ 932 1256 933 1257 if(isset($addressArray[$removalField])){ 934 1258 935 1259 unset($addressArray[$removalField]); 936 1260 937 1261 }//end if 938 1262 939 1263 }//end foreach 940 1264 941 1265 //get rid of stuff that should only be in addresses but is present in clients 942 1266 foreach($addressArray as $removalField => $junk){ 943 1267 944 1268 if(isset($fieldsArray[$removalField])){ 945 1269 946 1270 unset($fieldsArray[$removalField]); 947 1271 948 1272 }//end if 949 1273 950 1274 }//end foreach 951 1275 952 1276 //need to get two sets of address fields, one named main* and the other ship*. 953 1277 if($this->importType == "sugarcrm"){ 954 1278 955 1279 foreach($addressArray as $field => $junk){ 956 1280 957 1281 $mainAddressArray["main".$field] = $junk; 958 1282 $shipAddressArray["ship".$field] = $junk; 959 1283 960 1284 }//end foreach 961 1285 962 1286 $addressArray = $mainAddressArray + $shipAddressArray; 963 1287 964 1288 }//end if 965 1289 966 1290 $fieldsArray = $fieldsArray + $addressArray; 967 1291 968 1292 parent::displayTransaction($recordsArray, $fieldsArray); 969 1293 970 1294 }//end if 971 1295 972 1296 }//end method --displayTransaction-- 973 1297 974 1298 }//end class --clientsImport-- 975 1299 }//end if