phpBMS

Show
Ignore:
Timestamp:
04/07/09 11:44:18 (3 years ago)
Author:
nate
Message:
  • Merged Nathan branch back into trunk.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/phpbms/modules/bms/include/clients.php

    r431 r485  
    4040if(class_exists("phpbmsTable")){ 
    4141        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 
    4350                function checkForInvoices($id){ 
    4451                        $querystatement="SELECT id FROM invoices WHERE clientid=".((int) $id); 
    4552                        $queryresult = $this->db->query($querystatement); 
    46          
     53 
    4754                        return !($this->db->numRows($queryresult)===0); 
    4855                }//end method 
    49          
    50          
     56 
     57 
    5158                // CLASS OVERRIDES =================================================================== 
    5259                // =================================================================================== 
    5360 
    5461                function clients($db,$tabledefid = 0,$backurl = NULL){ 
    55                          
     62 
    5663                        $this->phpbmsTable($db,$tabledefid,$backurl); 
    57                          
     64 
    5865                        $this->address = new addresstorecord($db, 306); 
    59                          
     66 
    6067                }//end function - init 
    61                  
     68 
    6269 
    6370                function getDefaults(){ 
     
    6673 
    6774                        $therecord["type"] = DEFAULT_CLIENTTYPE; 
    68                          
     75 
    6976                        if($therecord["type"] == "client") { 
    7077 
     
    7481 
    7582                        }//end if 
    76                          
     83 
    7784                        $therecord["webaddress"] = "http://"; 
    78                          
     85 
    7986                        //now for the address information. 
    8087                        $addressinfo = $this->address->getDefaults(); 
    8188                        unset($addressinfo["id"], $addressinfo["createdby"], $addressinfo["creationdate"], $addressinfo["modifiedby"], $addressinfo["modifieddate"]); 
    8289                        $addressinfo["addressid"] = NULL; 
    83                          
     90 
    8491                        return array_merge($therecord, $addressinfo); 
    85                                                  
     92 
    8693                }//end function - getDefaults 
    87                  
    88          
     94 
     95 
    8996                function getRecord($id){ 
    9097 
    9198                        $id = (int) $id; 
    92                          
     99 
    93100                        $therecord = parent::getRecord($id); 
    94                          
     101 
    95102                        if($therecord["id"]){ 
    96103                                //need to grab the address as well 
    97                                  
     104 
    98105                                $querystatement = " 
    99106                                        SELECT 
     
    107114 
    108115                                $queryresult = $this->db->query($querystatement); 
    109                                  
     116 
    110117                                $addressinfo = $this->db->fetchArray($queryresult); 
    111118 
    112119                                if($addressinfo) { 
    113                                          
     120 
    114121                                        $addressinfo = $this->address->getRecord($addressinfo["id"]); 
    115                                  
     122 
    116123                                } else { 
    117124 
    118125                                        $addressinfo = $this->address->getDefaults(); 
    119126                                        $addressinfo["addressid"] = NULL; 
    120                                          
    121                                 }//endif                         
     127 
     128                                }//endif 
    122129 
    123130                                unset($addressinfo["id"], $addressinfo["notes"], $addressinfo["email"], $addressinfo["createdby"], $addressinfo["creationdate"], $addressinfo["modifiedby"], $addressinfo["modifieddate"]); 
    124131 
    125132                                $therecord = array_merge($therecord, $addressinfo); 
    126                          
     133 
    127134                        }//endif 
    128                          
     135 
    129136                        return $therecord; 
    130                  
     137 
    131138                }//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 
    133421 
    134422                function prepareVariables($variables){ 
    135                  
     423 
    136424                        if(isset($variables["webaddress"])) 
    137                                 if ($variables["webaddress"]=="http://")  
     425                                if ($variables["webaddress"]=="http://") 
    138426                                        $variables["webaddress"] = NULL; 
    139427 
    140428                        if(!isset($variables["type"])) 
    141429                                $variables["type"] = "client"; 
    142                                  
     430 
    143431                        if($variables["type"] == "prospect"){ 
    144432 
     
    149437                                $variables["type"] = "client"; 
    150438                                if(!isset($variables["becameclient"])) 
     439                                        $variables["becameclient"] = NULL; 
     440                                if(!$variables["becameclient"]) 
    151441                                        $variables["becameclient"] = dateToString(mktime()); 
    152442                        }//end if 
    153443 
    154444                        return $variables; 
    155                          
     445 
    156446                }//end method 
    157                  
    158          
     447 
     448 
    159449                function updateRecord($variables, $modifiedby = NULL){ 
    160                          
    161                         $variables = $this->prepareVariables($variables); 
    162                          
     450 
     451                        //$variables = $this->prepareVariables($variables); 
     452 
    163453                        $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 
    165457                        //need to update the address 
    166458                        $variables["id"] = $variables["addressid"]; 
     
    174466                        unset($this->address->fields["creationdate"]); 
    175467 
    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 
    177476 
    178477                        //restore the fields 
    179478                        $this->address->getTableInfo(); 
    180                          
     479 
    181480                        return $thereturn; 
    182                          
     481 
    183482                }//end method - updateRecord 
    184                  
    185                  
     483 
     484 
    186485                function insertRecord($variables, $createdby = NULL){ 
    187                          
    188                         $variables = $this->prepareVariables($variables); 
    189                          
     486 
     487                        //$variables = $this->prepareVariables($variables); 
     488 
    190489                        $newid = parent::insertRecord($variables, $createdby); 
    191                          
     490 
    192491                        //need to create the address and addresstorecord id 
    193492                        // make sure we are not setting extra info 
     
    200499                        $variables["defaultshipto"] = 1; 
    201500                        $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 
    204510 
    205511                        //restore the fields 
    206512                        $this->address->getTableInfo(); 
    207                          
     513 
    208514                        return $newid; 
    209                          
     515 
    210516                }//end method - insertRecord 
    211                  
     517 
    212518        }//end class 
    213          
     519 
    214520}//end if 
    215521 
     
    219525 
    220526                function mark_asclient(){ 
    221                  
     527 
    222528                        //passed variable is array of user ids to be revoked 
    223                         $whereclause = $this->buildWhereClause();        
    224                  
     529                        $whereclause = $this->buildWhereClause(); 
     530 
    225531                        $querystatement = "UPDATE clients SET clients.type=\"client\",modifiedby=\"".$_SESSION["userinfo"]["id"]."\" WHERE (".$whereclause.");"; 
    226532                        $queryresult = $this->db->query($querystatement); 
    227                          
     533 
    228534                        $message = $this->buildStatusMessage(); 
    229535                        $message.=" converted to client."; 
    230536                        return $message; 
    231537                } 
    232                  
    233                  
     538 
     539 
    234540                //Stamp Comments Field with info packet sent 
    235541                function stamp_infosent(){ 
    236                  
     542 
    237543                        //passed variable is array of user ids to be revoked 
    238544                        $whereclause = $this->buildWhereClause(); 
    239                  
     545 
    240546                        $querystatement = " 
    241547                                UPDATE 
    242                                         clients  
     548                                        clients 
    243549                                SET 
    244                                         clients.comments = concat('Information Packet Sent', char(10), clients.comments),  
     550                                        clients.comments = concat('Information Packet Sent', char(10), clients.comments), 
    245551                                        clients.modifiedby=".$_SESSION["userinfo"]["id"].", 
    246552                                        clients.modifieddate = NOW() 
    247553                                WHERE (".$whereclause.") AND clients.comments IS NOT NULL"; 
    248554                        $queryresult = $this->db->query($querystatement); 
    249                          
     555 
    250556                        $affected = $this->db->affectedRows(); 
    251557 
    252558                        $querystatement = " 
    253559                                UPDATE 
    254                                         clients  
     560                                        clients 
    255561                                SET 
    256                                         clients.comments = 'Information Packet Sent',  
     562                                        clients.comments = 'Information Packet Sent', 
    257563                                        clients.modifiedby=".$_SESSION["userinfo"]["id"].", 
    258564                                        clients.modifieddate = NOW() 
     
    261567 
    262568                        $affected += $this->db->affectedRows(); 
    263                          
     569 
    264570                        $message = $this->buildStatusMessage($affected); 
    265571                        $message.=" marked as info packet sent."; 
    266572                        return $message; 
    267573                } 
    268                  
    269                  
     574 
     575 
    270576                //remove prospects 
    271577                function delete_prospects(){ 
    272                  
     578 
    273579                        //passed variable is array of user ids to be revoked 
    274580                        $clientWhereClause = $this->buildWhereClause(); 
     
    282588                                        (".$clientWhereClause.") 
    283589                                        AND clients.type = 'prospect'"; 
    284                                          
     590 
    285591                        $queryresult = $this->db->query($querystatement); 
    286                          
     592 
    287593                        //build array of ids to be removed 
    288594                        $deleteIDs = array(); 
     
    291597 
    292598                        if(count($deleteIDs)){ 
    293                          
     599 
    294600                                $a2rWhere = $this->buildWhereClause("recordid", $deleteIDs); 
    295601 
     
    303609                                                tabledefid = 2 
    304610                                                AND (".$a2rWhere.")"; 
    305                                                  
     611 
    306612                                $a2rResult = $this->db->query($querystatement); 
    307                                  
     613 
    308614                                $addressIDs = array(); 
    309615                                while($a2r = $this->db->fetchArray($a2rResult)) 
    310616                                        array_push($addressIDs, $a2r["addressid"]); 
    311                                          
    312                                 // delete all a2r records for prospect   
     617 
     618                                // delete all a2r records for prospect 
    313619                                $deletestatement = " 
    314620                                        DELETE FROM 
     
    317623                                                tabledefid = 2 
    318624                                                AND (".$a2rWhere.")"; 
    319                                                  
     625 
    320626                                $this->db->query($deletestatement); 
    321                                  
     627 
    322628                                //now go get a list of orphaned addresses 
    323629                                $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 
    328634                                                addresses LEFT JOIN addresstorecord ON addresstorecord.addressid = addresses.id 
    329635                                        WHERE 
    330636                                                ".$this->buildWhereClause("addresses.id", $addressIDs); 
    331                                                  
     637 
    332638                                $addressResult = $this->db->query($querystatement); 
    333                                  
     639 
    334640                                $addressIDs = array(); 
    335641                                while($address = $this->db->fetchArray($addressResult)) 
    336642                                        if(!$address["a2rid"]) 
    337643                                                array_push($addressIDs, $address["id"]); 
    338                                                  
     644 
    339645                                if(count($addressIDs)){ 
    340                                  
    341                                         //delete orphaned addresses              
     646 
     647                                        //delete orphaned addresses 
    342648                                        $deletestatement = " 
    343                                                 DELETE FROM  
     649                                                DELETE FROM 
    344650                                                        addresses 
    345651                                                WHERE 
    346652                                                        ".$this->buildWhereClause("addresses.id", $addressIDs); 
    347                                                          
     653 
    348654                                        $this->db->query($deletestatement); 
    349                                          
     655 
    350656                                }//endif - addressids 
    351657 
     
    359665                                        WHERE 
    360666                                                ".$invoiceWhereClause; 
    361                                                  
     667 
    362668                                $invoiceresult = $this->db->query($invoicestatement); 
    363                                  
     669 
    364670                                //build invoice id array 
    365671                                $invoiceids = array(); 
    366672                                while($therecord = $this->db->fetchArray($invoiceresult)) 
    367673                                        array_push($invoiceids, $therecord["id"]); 
    368                                  
     674 
    369675                                if(count($invoiceids)) { 
    370676                                        $invoiceWhereClause = $this->buildWhereClause("invoices.id", $invoiceids); 
    371                                          
     677 
    372678                                        $lineitemWhereClause = $this->buildWhereClause("invoiceid", $invoiceids); 
    373                  
     679 
    374680                                        $lineItemDeleteStatement = " 
    375681                                                DELETE FROM 
     
    377683                                                WHERE 
    378684                                                        ".$lineitemWhereClause; 
    379                  
     685 
    380686                                        $queryresult = $this->db->query($lineItemDeleteStatement); 
    381                  
     687 
    382688                                        $statushistoryDeleteStatement = " 
    383689                                                DELETE FROM 
     
    385691                                                WHERE 
    386692                                                        ".$lineitemWhereClause; 
    387                  
     693 
    388694                                        $queryresult = $this->db->query($statushistoryDeleteStatement); 
    389                                          
     695 
    390696                                        $invoiceDeleteStatement = " 
    391697                                                DELETE FROM 
    392698                                                        invoices 
    393                                                 WHERE                                    
     699                                                WHERE 
    394700                                                        ".$invoiceWhereClause; 
    395          
     701 
    396702                                        $queryresult = $this->db->query($invoiceDeleteStatement); 
    397          
     703 
    398704                                }//end if 
    399705 
     
    402708 
    403709                                $deletestatement = " 
    404                                         DELETE FROM  
     710                                        DELETE FROM 
    405711                                                clients 
    406                                         WHERE  
     712                                        WHERE 
    407713                                                ".$delWhere; 
    408                                                  
     714 
    409715                                $this->db->query($deletestatement); 
    410                                                          
    411                         }//endif - count deleteIDS                                                                                               
    412  
    413                  
     716 
     717                        }//endif - count deleteIDS 
     718 
     719 
    414720                        $message = $this->buildStatusMessage(count($deleteIDs)); 
    415721                        $message.=" deleted."; 
    416                         return $message;         
    417                          
     722                        return $message; 
     723 
    418724                }// end method - delete_prospects 
    419                  
    420                  
     725 
     726 
    421727                function massEmail(){ 
    422728                        if(DEMO_ENABLED != "true"){ 
     
    428734                } 
    429735 
    430          
     736 
    431737        }//end class 
    432738}//end if 
    433739if(class_exists("phpbmsImport")){ 
    434740        class clientsImport extends phpbmsImport{ 
    435                  
    436                  
     741 
     742 
    437743                function clientsImport($table, $importType = "csv"){ 
    438                          
     744 
    439745                        if($importType == "sugarcrm"){ 
    440                                  
     746 
    441747                                $importType = "csv"; 
    442748                                $switchedFrom = "sugarcrm"; 
    443                                  
     749 
    444750                        }//end if 
    445                          
     751 
    446752                        parent::phpbmsImport($table, $importType); 
    447                          
     753 
    448754                        if(isset($switchedFrom)) 
    449755                                $this->importType = $switchedFrom; 
    450                          
     756 
    451757                }//end method --clientsImport-- 
    452                  
    453                  
     758 
     759 
    454760                function _parseFromData($data){ 
    455                          
     761 
    456762                        if($this->importType == "sugarcrm"){ 
    457                                  
     763 
    458764                                $this->importType = "csv"; 
    459765                                $switchedFrom = "sugarcrm"; 
    460                                  
     766 
    461767                        }//end if 
    462                          
     768 
    463769                        $thereturn = parent::_parseFromData($data); 
    464                          
     770 
    465771                        if(isset($switchedFrom)) 
    466772                                $this->importType = $switchedFrom; 
    467                          
     773 
    468774                        return $thereturn; 
    469                  
     775 
    470776                }//end method --_parseFromFile-- 
    471                  
    472                  
     777 
     778 
    473779                function _formatSugarVariables($rows, $titles){ 
    474                          
     780 
    475781                        //Replace the titles with valid ones 
    476782                        //(At the moment we only really need 
     
    480786                        $newTitles = array(); 
    481787                        foreach($titles as $index => $name){ 
    482                                  
     788 
    483789                                switch($name){ 
    484                                          
     790 
    485791                                        case "name": 
    486792                                                $newTitles[] = "company"; 
    487793                                        break; 
    488                                          
     794 
    489795                                        case "date_entered": 
    490796                                                $newTitles[] = "becameclient"; 
    491797                                        break; 
    492                                          
     798 
    493799                                        case "description": 
    494800                                                $newTitles[] = "comments"; 
    495801                                        break; 
    496                                          
     802 
    497803                                        case "deleted": 
    498804                                                $newTitles[] = "inactive"; 
    499805                                        break; 
    500                                          
     806 
    501807                                        case "account_type": 
    502808                                                $newTitles[] = "type"; 
    503809                                        break; 
    504                                          
     810 
    505811                                        case "industry": 
    506812                                                $newTitles[] = "category"; 
    507813                                        break; 
    508                                          
     814 
    509815                                        case "phone_fax": 
    510816                                                $newTitles[] = "fax"; 
    511817                                        break; 
    512                                          
     818 
    513819                                        case "billing_address_street": 
    514820                                                $newTitles[] = "address1"; 
    515821                                        break; 
    516                                          
     822 
    517823                                        case "billing_address_city": 
    518824                                                $newTitles[] = "city"; 
    519825                                        break; 
    520                                          
     826 
    521827                                        case "billing_address_state": 
    522828                                                $newTitles[] = "state"; 
    523829                                        break; 
    524                                          
     830 
    525831                                        case "billing_address_postalcode": 
    526832                                                $newTitles[] = "postalcode"; 
    527833                                        break; 
    528                                          
     834 
    529835                                        case "billing_address_country": 
    530836                                                $newTitles[] = "country"; 
    531837                                        break; 
    532                                          
     838 
    533839                                        case "phone_office": 
    534840                                                $newTitles[] = "workphone"; 
    535841                                        break; 
    536                                          
     842 
    537843                                        case "phone_alternate": 
    538844                                                $newTitles[] = "otherphone"; 
    539845                                        break; 
    540                                          
     846 
    541847                                        case "website": 
    542848                                                $newTitles[] = "webaddress"; 
    543849                                        break; 
    544                                          
     850 
    545851                                        case "shipping_address_street": 
    546852                                                $newTitles[] = "shipaddress1"; 
    547853                                        break; 
    548                                          
     854 
    549855                                        case "shipping_address_city": 
    550856                                                $newTitles[] = "shipcity"; 
    551857                                        break; 
    552                                          
     858 
    553859                                        case "shipping_address_state": 
    554860                                                $newTitles[] = "shipstate"; 
    555861                                        break; 
    556                                          
     862 
    557863                                        case "shipping_address_postalcode": 
    558864                                                $newTitles[] = "shippostalcode"; 
    559865                                        break; 
    560                                          
     866 
    561867                                        case "shipping_address_country": 
    562868                                                $newTitles[] = "shipcountry"; 
    563869                                        break; 
    564                                          
     870 
    565871                                }//end switch 
    566                                  
     872 
    567873                        }//end foreach 
    568                          
    569                          
     874 
     875 
    570876                        $newRows = array(); 
    571877                        foreach($rows as $rowData){ 
    572                                  
     878 
    573879                                $newRowData = array(); 
    574880                                $addComments = ""; 
    575881                                foreach($rowData as $name => $data){ 
    576                                          
     882 
    577883                                        switch($name){ 
    578                                          
     884 
    579885                                                case "name": 
    580886                                                        $newRowData["company"] = trim($data); 
    581887                                                break; 
    582                                                  
     888 
    583889                                                case "date_entered": 
    584890                                                        $newRowData["becameclient"] = trim($data); 
    585891                                                break; 
    586                                                  
     892 
    587893                                                case "description": 
    588894                                                        $newRowData["comments"] = trim($data); 
    589895                                                break; 
    590                                                  
     896 
    591897                                                case "deleted": 
    592898                                                        $newRowData["inactive"] = trim($data); 
    593899                                                break; 
    594                                                  
     900 
    595901                                                case "industry": 
    596902                                                        $newRowData["category"] = trim($data); 
    597903                                                break; 
    598                                                  
     904 
    599905                                                case "account_type": 
    600906                                                        $newRowData["type"] = trim($data); 
    601907                                                break; 
    602                                                  
     908 
    603909                                                case "phone_fax": 
    604910                                                        $newRowData["fax"] = trim($data); 
    605911                                                break; 
    606                                                  
     912 
    607913                                                case "billing_address_street": 
    608914                                                        $newRowData["address1"] = trim($data); 
    609915                                                break; 
    610                                                  
     916 
    611917                                                case "billing_address_city": 
    612918                                                        $newRowData["city"] = trim($data); 
    613919                                                break; 
    614                                                  
     920 
    615921                                                case "billing_address_state": 
    616922                                                        $newRowData["state"] = trim($data); 
    617923                                                break; 
    618                                                  
     924 
    619925                                                case "billing_address_postalcode": 
    620926                                                        $newRowData["postalcode"] = trim($data); 
    621927                                                break; 
    622                                                  
     928 
    623929                                                case "billing_address_country": 
    624930                                                        $newRowData["country"] = trim($data); 
    625931                                                break; 
    626                                                  
     932 
    627933                                                case "phone_office": 
    628934                                                        $newRowData["workphone"] = trim($data); 
    629935                                                break; 
    630                                                  
     936 
    631937                                                case "phone_alternate": 
    632938                                                        $newRowData["otherphone"] = trim($data); 
    633939                                                break; 
    634                                                  
     940 
    635941                                                case "website": 
    636942                                                        $newRowData["webaddress"] = trim($data); 
    637943                                                break; 
    638                                                  
     944 
    639945                                                case "shipping_address_street": 
    640946                                                        $newRowData["shipaddress1"] = trim($data); 
    641947                                                break; 
    642                                                  
     948 
    643949                                                case "shipping_address_city": 
    644950                                                        $newRowData["shipcity"] = trim($data); 
    645951                                                break; 
    646                                                  
     952 
    647953                                                case "shipping_address_state": 
    648954                                                        $newRowData["shipstate"] = trim($data); 
    649955                                                break; 
    650                                                  
     956 
    651957                                                case "shipping_address_postalcode": 
    652958                                                        $newRowData["shippostalcode"] = trim($data); 
    653959                                                break; 
    654                                                  
     960 
    655961                                                case "shipping_address_country": 
    656962                                                        $newRowData["shipcountry"] = trim($data); 
    657963                                                break; 
    658                                                  
     964 
    659965                                                case "annual_revenue": 
    660966                                                case "rating": 
     
    665971                                                                $addComments .= "\n".str_replace("_"," ",$name).": ".trim($data); 
    666972                                                break; 
    667                                                  
     973 
    668974                                        }//end switch 
    669                                          
     975 
    670976                                }//end foreach 
    671                                  
     977 
    672978                                if($newRowData["type"] == "prospect") 
    673979                                        $newRowData["becameclient"] = NULL; 
    674                                  
     980                                else 
     981                                        $newRowData["type"] = "client"; 
     982 
    675983                                $newRowData["comments"] .= $addComments; 
    676984                                $newRows[] = $newRowData; 
    677                                  
     985 
    678986                        }//end foreach 
    679                          
     987 
    680988                        $thereturn["rows"] = $newRows; 
    681989                        $thereturn["titles"] = $newTitles; 
    682990                        return $thereturn; 
    683                          
     991 
    684992                }//end method --_formatSugarvariables-- 
    685                  
    686                  
     993 
     994 
    687995                function importRecords($rows, $titles){ 
    688                          
     996 
    689997                        switch($this->importType){ 
    690                                  
     998 
    691999                                case "sugarcrm": 
    6921000                                        $thereturn = $this->_formatSugarVariables($rows, $titles); 
    6931001                                        $rows = $thereturn["rows"]; 
    6941002                                        $titles = $thereturn["titles"]; 
    695                                  
    696                                 case "csv":                      
     1003 
     1004                                case "csv": 
    6971005                                        //count total fieldnames (top row of csv document) 
    6981006                                        $fieldNum = count($titles); 
    699                                          
     1007 
    7001008                                        //the file starts at line number 1, but since line 1 is 
    7011009                                        //supposed to be the fieldnames in the table(s), the lines 
    7021010                                        //being insereted start @ 2. 
    7031011                                        $rowNum = 2; 
    704                                          
     1012 
    7051013                                        //get the data one row at a time 
    7061014                                        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 
    7101019                                                //trim off leading/trailing spaces 
    7111020                                                $trimmedRowData = array(); 
    7121021                                                foreach($rowData as $name => $data) 
    7131022                                                        $trimmedRowData[$name] = trim($data); 
    714                                                  
     1023 
    7151024                                                //check to see if number of fieldnames is consistent for each row 
    7161025                                                $rowFieldNum = count($trimmedRowData); 
    717                                                  
     1026 
    7181027                                                //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 
    7221034                                                        $this->error .= '<li> incorrect amount of fields for line number '.$rowNum.'.</li>'; 
    723                                                  
     1035 
    7241036                                                if($theid){ 
    7251037                                                        //keep track of the ids in the transaction to be able to select them 
    7261038                                                        //for preview purposes 
    7271039                                                        $this->transactionIDs[] = $theid; 
    728                                                          
     1040 
    7291041                                                        //get first id to correct auto increment 
    7301042                                                        if(!$this->revertID) 
    7311043                                                                $this->revertID = $theid; 
    732                                                          
     1044 
    7331045                                                        //If it is a sugarcrm import, insert the shipping address as well 
     1046                                                        $addressVerify = array(); 
    7341047                                                        if($this->importType == "sugarcrm"){ 
    735                                                                  
    736                                                                  
     1048 
     1049 
    7371050                                                                $variables = array(); 
    7381051                                                                if($trimmedRowData["shipaddress1"]) $variables["address1"] = $trimmedRowData["shipaddress1"]; 
     
    7401053                                                                if($trimmedRowData["shipstate"]) $variables["state"] = $trimmedRowData["shipstate"]; 
    7411054                                                                if($trimmedRowData["shipcountry"]) $variables["country"] = $trimmedRowData["shipcountry"]; 
    742                                                                  
     1055 
    7431056                                                                //check to see if there is a shipping address 
    7441057                                                                if(count($variables)){ 
    745                                                                          
     1058 
    7461059                                                                        //If there is a shipping address, we need to make any others' 
    7471060                                                                        //`defaultshipto` to 0 
    748                                                                          
     1061 
    7491062                                                                        $querystatement = " 
    7501063                                                                                UPDATE 
     
    7571070                                                                                        `addresstorecord`.`tabledefid` = '2'; 
    7581071                                                                                "; 
    759                                                                                  
     1072 
    7601073                                                                        $this->table->db->query($querystatement); 
    761                                                                          
     1074 
    7621075                                                                        $variables["title"] = "Main Shipping Addresss"; 
    7631076                                                                        $variables["tabledefid"] = 2; 
     
    7651078                                                                        $variables["defaultshipto"] = 1; 
    7661079                                                                        $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 
    7701086                                                                }//end if 
    771                                                                  
     1087 
    7721088                                                        }//end if 
    7731089                                                }else 
    7741090                                                        $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 
    7761099                                                $rowNum++; 
    777                                                  
     1100 
    7781101                                        }//end foreach 
    7791102                                break; 
    780                                  
     1103 
    7811104                        }//end switch 
    782                          
     1105 
    7831106                }//end method --importRecords-- 
    784                  
    785                  
     1107 
     1108 
    7861109                function _getTransactionData(){ 
    787                          
     1110 
    7881111                        $inStatement = ""; 
    7891112                        foreach($this->transactionIDs as $theid) 
     
    7921115                        if($inStatement) 
    7931116                                $inStatement = substr($inStatement, 0, -1); 
    794                          
     1117                        else 
     1118                                $inStatement = "0"; 
     1119 
    7951120                        //There are two cases to minimize joins for csv files 
    7961121                        switch($this->importType){ 
    797                                  
     1122 
    7981123                                case "sugarcrm": 
    7991124                                        $querystatement = " 
     
    8541179                                                "; 
    8551180                                break; 
    856                                  
     1181 
    8571182                                case "csv": 
    8581183                                        $querystatement = " 
     
    8951220                                                        `addresses`.`postalcode`, 
    8961221                                                        `addresses`.`country`, 
    897                                                         `addresses`.`phone`, 
    898                                                         `addresses`.`email` 
     1222                                                        `addresses`.`phone` 
    8991223                                                FROM 
    9001224                                                        ((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) 
     
    9051229                                                "; 
    9061230                                break; 
    907                          
     1231 
    9081232                        }//end switch 
    909                          
     1233 
    9101234                        $queryresult = $this->table->db->query($querystatement); 
    911                          
     1235 
    9121236                        while($therecord = $this->table->db->fetchArray($queryresult)) 
    9131237                                $this->transactionRecords[] = $therecord; 
    914                          
    915                          
     1238 
     1239 
    9161240                }//end method --_gettransactionData-- 
    917                  
    918                  
     1241 
     1242 
    9191243                function displayTransaction($recordsArray, $fieldsArray){ 
    920                          
     1244 
    9211245                        if(count($recordsArray) && count($fieldsArray)){ 
    922                                  
     1246 
    9231247                                //Need to include addresses in the fieldArray 
    924                                  
     1248 
    9251249                                //list of values that should not be displayed 
    9261250                                $removalArray = array("id", "modifiedby", "modifieddate", "createdby", "creationdate", "notes", "title", "shiptoname"); 
    9271251                                //gets the address table's columnnames/information (fields) 
    9281252                                $addressArray = $this->table->address->fields; 
    929                                  
     1253 
    9301254                                //gets rid of the values that should not be displayed 
    9311255                                foreach($removalArray as $removalField){ 
    932                                          
     1256 
    9331257                                        if(isset($addressArray[$removalField])){ 
    934                                                  
     1258 
    9351259                                                unset($addressArray[$removalField]); 
    936                                                  
     1260 
    9371261                                        }//end if 
    938                                          
     1262 
    9391263                                }//end foreach 
    940                                  
     1264 
    9411265                                //get rid of stuff that should only be in addresses but is present in clients 
    9421266                                foreach($addressArray as $removalField => $junk){ 
    943                                          
     1267 
    9441268                                        if(isset($fieldsArray[$removalField])){ 
    945                                                  
     1269 
    9461270                                                unset($fieldsArray[$removalField]); 
    947                                                  
     1271 
    9481272                                        }//end if 
    949                                          
     1273 
    9501274                                }//end foreach 
    951                                  
     1275 
    9521276                                //need to get two sets of address fields, one named main* and the other ship*. 
    9531277                                if($this->importType == "sugarcrm"){ 
    954                                          
     1278 
    9551279                                        foreach($addressArray as $field => $junk){ 
    956                                                  
     1280 
    9571281                                                $mainAddressArray["main".$field] = $junk; 
    9581282                                                $shipAddressArray["ship".$field] = $junk; 
    959                                                  
     1283 
    9601284                                        }//end foreach 
    961                                          
     1285 
    9621286                                        $addressArray = $mainAddressArray + $shipAddressArray; 
    963                                          
     1287 
    9641288                                }//end if 
    965                                  
     1289 
    9661290                                $fieldsArray = $fieldsArray + $addressArray; 
    967                                  
     1291 
    9681292                                parent::displayTransaction($recordsArray, $fieldsArray); 
    969                                  
     1293 
    9701294                        }//end if 
    971                          
     1295 
    9721296                }//end method --displayTransaction-- 
    973                  
     1297 
    9741298        }//end class --clientsImport-- 
    9751299}//end if 
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.