prepare(""); //clear it out return $alwDB; } function disconnectDB() { global $alwDB; $alwDB = null; } function findCustomerByMacDB($mac) { $DB = connectToDB(); $sql = $DB->prepare("select * from CUSTOMERS where MAC_ADDRESS = :mac"); $sql->bindParam(":mac",$mac); if($sql->execute()) { $customer = $sql->fetch(); } if($customer) { return $customer;//return array of customer info } return false;//no customer found } function GetRelayDB($id) { $DB = connectToDB(); $sql = $DB->prepare("select * from RELAYS where ID = :id"); $sql->bindParam(":id",$id); if($sql->execute()) { $relay = $sql->fetch(); } if($relay) { return $relay;//return array of customer info } return false;//no customer found } function UpdateRelayDB($Relay) { $DB = connectToDB(); $sql = $DB->prepare("update RELAYS set RELAY = :relay, NOTES = :notes, GPS_CORDS_LAT = :gpslat, GPS_CORDS_LONG = :gpslon where ID=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$Relay['ID']); $sql->bindParam("relay", $Relay['RELAY']); $sql->bindParam("notes",$Relay['NOTES']); $sql->bindParam("gpslat",$Relay['GPS_CORDS_LAT']); $sql->bindParam("gpslon",$Relay['GPS_CORDS_LONG']); $sql->execute(); } function findCustomerByEmailDB($email) { $DB = connectToDB(); $sql = $DB->prepare("select * from CUSTOMERS where EMAIL = :email"); $sql->bindParam(":email",$email); if($sql->execute()) { $customer = $sql->fetch(); } if($customer) { return $customer;//return array of customer info } return false;//no customer found } function addCustomertoDB($custData) { if(@$custData['RELAY']) { $custData['RELAY'] = 1; $custData['ACCOUNT_BALENCE'] = 75; } else { $custData['RELAY'] = 0; $custData['ACCOUNT_BALENCE'] = 0; } if($custData['CUSTOMER_ROUTER']) { $custData['CUSTOMER_ROUTER'] = 1; } else { $custData['CUSTOMER_ROUTER'] = 0; } if(!($custData['NOTES'])){ $custData['NOTES'] = ""; } $DB = connectToDB(); $sql = $DB->prepare("insert into CUSTOMERS (FIRST_NAME,LAST_NAME,EMAIL,PHONE,STREET_ADDRESS,CITY, STATE,ZIPCODE,MAC_ADDRESS,IP_ADDRESS,AP,CUSTOMER_ROUTER,EQUIPMENT,OWNEDEQUIPMENT,LABOR,RELAY,ACCOUNT_BALENCE,NOTES,GPS_CORDS_LAT,GPS_CORDS_LONG) VALUES (:FIRST_NAME,:LAST_NAME,:EMAIL,:PHONE,:STREET_ADDRESS,:CITY, :STATE,:ZIPCODE,:MAC_ADDRESS,:IP_ADDRESS,:AP,:CUSTOMER_ROUTER,:EQUIPMENT,:OEQUIPMENT,:LABOR,:RELAY,:ACCOUNT_BALENCE,:NOTES,:GPS_CORDS_LAT,:GPS_CORDS_LONG)"); $sql->bindParam(":FIRST_NAME",$custData['FIRST_NAME']); $sql->bindParam(":LAST_NAME",$custData['LAST_NAME']); $sql->bindParam(":EMAIL",$custData['EMAIL']); $sql->bindParam(":PHONE",$custData['PHONE']); $sql->bindParam(":STREET_ADDRESS",$custData['STREET_ADDRESS']); $sql->bindParam(":CITY",$custData['CITY']); $sql->bindParam(":STATE",$custData['STATE']); $sql->bindParam(":ZIPCODE",$custData['ZIPCODE']); $sql->bindParam(":MAC_ADDRESS",$custData['MAC']); $sql->bindParam(":IP_ADDRESS",$custData['IPADDRESS']); $sql->bindParam(":AP",$custData['AP']); $sql->bindParam(":CUSTOMER_ROUTER",$custData['CUSTOMER_ROUTER']); $sql->bindParam(":EQUIPMENT",$custData['EQUIPMENT']); $sql->bindParam(":OEQUIPMENT",$custData['OEQUIPMENT']);//O = Owned $sql->bindParam(":LABOR",$custData['LABOR']);//O = Owned $sql->bindParam(":RELAY",$custData['RELAY']); $sql->bindParam(":ACCOUNT_BALENCE",$custData['ACCOUNT_BALENCE']); $sql->bindParam(":NOTES",$custData['NOTES']); $sql->bindParam(":GPS_CORDS_LAT",$custData['GPS_CORDS_LAT']); $sql->bindParam(":GPS_CORDS_LONG",$custData['GPS_CORDS_LONG']); $sql->execute(); } function getFreePlanDetails($customer) { global $planCount; $DB = connectToDB(); $sql = $DB->prepare("select * from PLANS where ACTIVE = '9'");// FIND ALL FREE PLANS if($sql->execute()) { $freePlans = $sql->fetch(); } $planHTML = ""; $planHTML .= getPlanHTML($freePlans, $customer['MAC_ADDRESS']); $planCount += 1; return $planHTML; } function getPlanDetailsDB($customer) { global $planCount; $DB = connectToDB(); $sql = $DB->prepare("select * from PLANS where ACTIVE = '1' ORDER BY `ID` ASC");// FIND ALL active PLANS if($sql->execute()) { $activePlans = $sql->fetchall(); } $planHTML = ""; if(@$customer['VIP'] == 1) { $sql = $DB->prepare("select * from PLANS where NAME='VIP' ORDER BY `PROFILE` ASC"); $sql->execute(); $sql = $sql->fetchall(); $sql = $sql['0']; $planHTML .= getPlanHTML($sql, $customer['MAC_ADDRESS'], $customer['ACCOUNT_BALENCE']); } foreach($activePlans as $plan) { $planHTML .= getPlanHTML($plan, @$customer['MAC_ADDRESS'], $customer["ACCOUNT_BALENCE"]); $planCount += 1; } return $planHTML; } function getPlanInfoDB($ID) {//Using the Plan ID, return all info related to the plan $DB = connectToDB(); $sql = $DB->prepare("select * from PLANS where ID=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$ID); $sql->execute(); return $sql->fetch(); } function getAllPlanInfoDB() {//Return All Plans $DB = connectToDB(); $sql = $DB->prepare("select * from PLANS");// FIND ALL active PLANS $sql->execute(); return $sql->fetchall(); } function getAllActivePlansHtmlDB($customer) { $DB = connectToDB(); $sql = $DB->prepare("select * from PLANS where ACTIVE = '1' ORDER BY `PROFILE` ASC");// FIND ALL active PLANS if($sql->execute()) { $plans = $sql->fetchall(); } foreach($plans as $plan) { $planHTML .= getPlanHTML($plan, 'N/A', '0'); } return $planHTML; } function addToPlanHistory($customer) { //take current plan save to history $DB = connectToDB(); $sql = $DB->prepare("insert into PLAN_HISTORY(CUSTOMER_ID,PLAN_ID,DATAUSAGE) VALUES(:CUSTOMER_ID,:PLAN_ID,:DATAUSAGE)");// FIND ALL active PLANS $sql->bindParam("CUSTOMER_ID",$customer["ID"]); $sql->bindParam("PLAN_ID",$customer['PLAN_ID']); $sql->bindParam("DATAUSAGE",$customer['DATAUSAGE']); $sql->execute(); } function GetPlanHistoryDB($customer) { $DB = connectToDB(); $sql = $DB->prepare("select * from PLAN_HISTORY WHERE CUSTOMER_ID=:ID ORDER BY ID DESC"); $sql->bindParam("ID",$customer['ID']); $sql->execute(); $PlanHistory = $sql->fetchall(); $NewHistory = array(); $Plans = getAllPlanInfoDB(); foreach($PlanHistory as $Entry) { $Entry['PLAN_DATA']['NAME'] = 'Not Found'; $Entry['PLAN_DATA']['UPLOAD_SPEED'] = 'Not Found'; $Entry['PLAN_DATA']['DOWNLOAD_SPEED'] = 'Not Found'; $Entry['PLAN_DATA']['COST'] = 'Not Found'; foreach($Plans as $Plan) { if($Entry['PLAN_ID'] == $Plan['ID']) { $Entry['PLAN_DATA'] = $Plan; } } array_push($NewHistory, $Entry); } return $NewHistory; } function UpdateCustomerDB($customer) { $DB = connectToDB(); $sql = $DB->prepare("update CUSTOMERS set FIRST_NAME = :fn, LAST_NAME = :ln, EMAIL = :email, PHONE = :phone, STREET_ADDRESS =:street, CITY=:city, STATE=:state, ZIPCODE=:zip, IP_ADDRESS=:ip, CUSTOMER_ROUTER=:custr, EQUIPMENT=:EQ, OWNEDEQUIPMENT=:OEQ, RELAY=:relay, AP=:ap, NOTES=:notes, GPS_CORDS_LAT=:gpslat, GPS_CORDS_LONG=:gpslon, ACCOUNT_BALENCE=:accbal, PLAN_ID=:planid, PLAN_START=:pstart, PLAN_END=:pend, EWARNING=:ewarn, AUTOBILL=:autobill, AUTOBILLPLAN=:abp, AUTOBILLDATE=:abd, ACTIVE=:active where id=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$customer['ID']); $sql->bindParam("fn",$customer['FIRST_NAME']); $sql->bindParam("ln",$customer['LAST_NAME']); $sql->bindParam("email",$customer['EMAIL']); $sql->bindParam("phone",$customer['PHONE']); $sql->bindParam("street",$customer['STREET_ADDRESS']); $sql->bindParam("city",$customer['CITY']); $sql->bindParam("state",$customer['STATE']); $sql->bindParam("zip",$customer['ZIPCODE']); $sql->bindParam("ip",$customer['IP_ADDRESS']); $sql->bindParam("custr",$customer['CUSTOMER_ROUTER']); $sql->bindParam("EQ",$customer['EQUIPMENT']); $sql->bindParam("OEQ",$customer['OWNEDEQUIPMENT']); $sql->bindParam("relay",$customer['RELAY']); $sql->bindParam("ap",$customer['AP']); $sql->bindParam("notes",$customer['NOTES']); $sql->bindParam("gpslat",$customer['GPS_CORDS_LAT']); $sql->bindParam("gpslon",$customer['GPS_CORDS_LONG']); $sql->bindParam("accbal",$customer['ACCOUNT_BALENCE']); $sql->bindParam("planid",$customer['PLAN_ID']); $sql->bindParam("pstart",$customer['PLAN_START']); $sql->bindParam("pend",$customer['PLAN_END']); $sql->bindParam("ewarn",$customer['EWARNING']); $sql->bindParam("autobill",$customer['AUTOBILL']); $sql->bindParam("abp",$customer['AUTOBILLPLAN']); $sql->bindParam("abd",$customer['AUTOBILLDATE']); $sql->bindParam("active",$customer['ACTIVE']); $sql->execute(); } function updateUserDataDB($customerID, $rosUserInfo) { $totalBytes = @$rosUserInfo["download-used"]+@$rosUserInfo["upload-used"]; $DB = connectToDB(); $sql = $DB->prepare("update CUSTOMERS set LASTUPDATED = :NOW, DATA_USAGE=:DATAUSED where id=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$customerID); $sql->bindParam("NOW",date('c')); //$sql->bindParam("LIMIT_BYTES_TOTAL",$rosUserInfo["transfer-limit"]); //UM no longer is the limit bytes total deal $sql->bindParam("DATAUSED",$totalBytes); $sql->execute(); } function updateAllUserDataDB($users) { date_default_timezone_set('America/Los_Angeles'); foreach($users as $userData) { //check to see if custom exists if(($customer = findCustomerByMac($userData['mac-address']))) { //Updates customer DB $DB = connectToDB(); $totalBytes = $userData["bytes-in"]+$userData["bytes-out"]; $sql = $DB->prepare("update CUSTOMERS set LASTUPDATED = :NOW, LIMIT_BYTES_TOTAL = :LIMIT_BYTES_TOTAL, DATA_USAGE=:DATAUSED where id=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$customer["ID"]); $sql->bindParam("NOW",date('c')); $sql->bindParam("LIMIT_BYTES_TOTAL",$userData["limit-bytes-total"]); $sql->bindParam("DATAUSED",$totalBytes); $sql->execute(); } } } function updateUserUsageDB($customer) { if($customer['ACTIVE'] > 0){ date_default_timezone_set('America/Los_Angeles'); $D = Date('Y-m-d'); $DStr = strval($D); $DB = connectToDB(); $sql = $DB->prepare("SELECT * FROM DATAUSAGE WHERE CUSTOMER_ID='{$customer['ID']}' AND DATE='{$DStr}'"); $sql->execute(); $DataUsage = $sql->fetchall(); if(@$DataUsage[0]){ //print 'found'; $DB = connectToDB(); $sql = $DB->prepare("update DATAUSAGE set DATAUSED=:DATAUSED where ID=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$DataUsage[0]['ID']); $sql->bindParam("DATAUSED",$customer["DATA_USAGE"]); $sql->execute(); } else { $sql = $DB->prepare("INSERT INTO DATAUSAGE (PLAN_ID, CUSTOMER_ID, DATE, DATAUSED) VALUES(:PLAN_ID, :CUSTOMER_ID, :DATE, :DATAUSED)"); $sql->bindParam("PLAN_ID",$customer['PLAN_ID']); $sql->bindParam("CUSTOMER_ID",$customer['ID']); $sql->bindParam("DATE",$D); $sql->bindParam("DATAUSED",$customer['DATA_USAGE']); $sql->execute(); } } } function RemoveCustomerDataUsage($customer) { $DB = connectToDB(); $sql = $DB->prepare("DELETE FROM DATAUSAGE WHERE CUSTOMER_ID=:CUSTOMER_ID"); $sql->bindParam("CUSTOMER_ID",$customer["ID"]); $sql->execute(); } function GetEquipmentRentals() { $DB = connectToDB(); $sql = $DB->prepare("select * from EQRental WHERE Reoccuring='1'");// get customers if($sql->execute()) { $customers = $sql->fetchall(); } return $customers; } function GetEquipment() { $DB = connectToDB(); $sql = $DB->prepare("select * from EQRental WHERE Reoccuring='0'");// get customers if($sql->execute()) { $customers = $sql->fetchall(); } return $customers; } function GetAllRelaysDB() {//This dosn't work, commented out due to php v8 error $DB = connectToDB(); /* $sql = $DB->prepare("select * from RELAY");// get customers if($sql->execute()) { $RELAYS = $sql->fetchall(); }*/ return null; } function GetAllCustomersDB() { $DB = connectToDB(); $sql = $DB->prepare("select * from CUSTOMERS");// get customers if($sql->execute()) { $customers = $sql->fetchall(); } return $customers; } function GetCustomerUsageDB($id) { $DB = connectToDB(); $sql = $DB->prepare("select * from DATAUSAGE where CUSTOMER_ID = $id ORDER BY `ID`");// FIND ALL active PLANS if($sql->execute()) { $USAGEDATA = $sql->fetchall(); } return($USAGEDATA); } function invoiceToDB($customer,$plan,$btResult,$amountDue) { $DB = connectToDB(); $sql = $DB->prepare("insert into INVOICES (CUSTOMER_ID,PLAN_ID,DATE,ACCOUNT_BALENCE,PLAN_COST,AMOUNT_DUE,PAID_VIA,BRAIN_TREE_ID) VALUES (:CUSTOMER_ID,:PLAN_ID,:DATE,:ACCOUNT_BALENCE,:PLAN_COST,:AMOUNT_DUE,:PAID_VIA,:BRAIN_TREE_ID)"); $sql->bindParam("CUSTOMER_ID",$customer["ID"]); $sql->bindParam("PLAN_ID",$plan["ID"]); $sql->bindParam("DATE",date('c')); $sql->bindParam("ACCOUNT_BALENCE",$customer["ACCOUNT_BALENCE"]); $sql->bindParam("PLAN_COST",$plan["COST"]); $sql->bindParam("AMOUNT_DUE",$amountDue); $accountbal = "Account balence"; $sql->bindParam("PAID_VIA",$accountbal); $nothing = ""; $sql->bindParam("BRAIN_TREE_ID",$nothing); if($btResult) {//Brainttree payment $brainTree = "Braintree"; $sql->bindParam("PAID_VIA",$brainTree); //braintree transaction search for $btResult->transaction->transaction-id; $btID = $btResult->transaction->id; $sql2 = $DB->prepare("select ID from BRAINTREETRANSACTIONS where TRANSACTION_ID = :btID"); $sql2->bindParam("btID",$btID); $sql2->execute(); $btID = $sql2->fetch(); $sql->bindParam("BRAIN_TREE_ID",$btID['ID']); } $sql->execute(); //print_r($sql->errorInfo()); } function getRelaysDB() { $DB = connectToDB(); $sql = $DB->prepare("select * from CUSTOMERS where RELAY = '1'"); if($sql->execute()) { $customers = $sql->fetchall(); } if($customers) { return $customers;//return array of customer info } return false;//no customer found } function addCreditDB($customer,$credit) { $DB = connectToDB(); $totalAmount = $customer["ACCOUNT_BALENCE"] + $credit; $sql = $DB->prepare("update CUSTOMERS set ACCOUNT_BALENCE = :ACCOUNT_BALENCE where ID=:ID");// FIND ALL active PLANS $sql->bindParam("ID",$customer["ID"]); $sql->bindParam("ACCOUNT_BALENCE",$totalAmount); $sql->execute(); $newCustomer = findCustomerByMac($customer["MAC_ADDRESS"]); return $newCustomer["ACCOUNT_BALENCE"]; } function btPaymentToDB($result) { $DB = connectToDB(); $sql = $DB->prepare("insert into BRAINTREETRANSACTIONS (CUSTOMER_ID,PLAN_ID,MAC_ADDRESS,TRANSACTION_ID,AMOUNT,DATE_PROCESSED,PAYMENT_TYPE,CARDTYPE,LAST4,PAYER_EMAIL,PAYMENT_ID) VALUES (:CUSTOMER_ID,:PLAN_ID,:MAC_ADDRESS,:TRANSACTION_ID,:AMOUNT,:DATE_PROCESSED,:PAYMENT_TYPE,:CARDTYPE,:LAST4,:PAYER_EMAIL,:PAYMENT_ID)"); $sql->bindParam("CUSTOMER_ID",$result["CUSTOMER_ID"]); $sql->bindParam("PLAN_ID",$result["PLAN_ID"]); $sql->bindParam("MAC_ADDRESS",$result["MAC_ADDRESS"]); $sql->bindParam("TRANSACTION_ID",$result["TRANSACTION_ID"]); $sql->bindParam("AMOUNT",$result["AMOUNT"]); $sql->bindParam("DATE_PROCESSED",$result["DATE_PROCESSED"]); $sql->bindParam("PAYMENT_TYPE",$result["PAYMENT_TYPE"]); $sql->bindParam("CARDTYPE",$result["CARDTYPE"]); $sql->bindParam("LAST4",$result["LAST4"]); $sql->bindParam("PAYER_EMAIL",$result["PAYER_EMAIL"]); $sql->bindParam("PAYMENT_ID",$result["PAYMENT_ID"]); return $sql->execute(); } function updatePlanHistoryDB($customer) { if($customer["PLAN_ID"] != null)//Make sure there is a plan before backing it update { $DB = connectToDB(); $sql = $DB->prepare("insert into PLAN_HISTORY (CUSTOMER_ID,PLAN_ID,PLAN_START,PLAN_END,LIMIT_BYTES_TOTAL,DATA_USAGE) VALUES (:CUSTOMER_ID,:PLAN_ID,:PLAN_START,:PLAN_END,:LIMIT_BYTES_TOTAL,:DATA_USAGE)"); $sql->bindParam("CUSTOMER_ID",$customer["ID"]); $sql->bindParam("PLAN_ID",$customer["PLAN_ID"]); $sql->bindParam("PLAN_START",$customer["PLAN_START"]); $sql->bindParam("PLAN_END",$customer["PLAN_END"]); $sql->bindParam("LIMIT_BYTES_TOTAL",$customer["LIMIT_BYTES_TOTAL"]); $sql->bindParam("DATA_USAGE",$customer["DATA_USAGE"]); return $sql->execute(); } else { return false; } } //remove? we have a function that we can just pass the customer to update all data function updateCustomerAccountBalenceDB($customer,$newBalence) { $DB = connectToDB(); $sql = $DB->prepare("update CUSTOMERS set ACCOUNT_BALENCE = :newBalence where ID=:ID"); $sql->bindParam("ID",$customer["ID"]); $sql->bindParam("newBalence",$newBalence); $sql->execute(); } function updateCustomerPlanDB($customer,$newPlan) { $DB = connectToDB(); //wipe old data usage $sql = $DB->prepare("DELETE FROM DATAUSAGE WHERE CUSTOMER_ID=:CUSTOMER_ID"); $sql->bindParam("CUSTOMER_ID",$customer["ID"]); $sql->execute(); //update plan ID, plan start, plan end for $customer[ID] $sql = $DB->prepare("update CUSTOMERS set PLAN_ID = :PLAN_ID, PLAN_START = :PLAN_START, PLAN_END = :PLAN_END, LASTUPDATED = :NOW, AUTOBILL = :AUTOBILL, AUTOBILLPLAN = :PLAN_ID, AUTOBILLDATE = :PLAN_END, EWARNING = 0 where ID=:ID"); $sql->bindParam("ID",$customer['ID']); $sql->bindParam("PLAN_ID",$newPlan['ID']); $sql->bindParam("PLAN_START",date('c')); $sql->bindParam("PLAN_END",date('c',strtotime("+".$newPlan['TERM']." days"))); $sql->bindParam("NOW",date('c')); $sql->bindParam("AUTOBILL",$customer['AUTOBILL']); $sql->execute(); } function BackupReceiptDB($customer, $HTML) { $DB = connectToDB(); $sql = $DB->prepare("insert into RECEIPT_HISTORY (CUSTOMER_ID,HTML) VALUES (:CUSTOMER_ID,:HTML)"); $sql->bindParam("CUSTOMER_ID",$customer["ID"]); $sql->bindParam("HTML", stripslashes($HTML)); return $sql->execute(); } function GetReceiptsDB($customer) { $DB = connectToDB(); $sql = $DB->prepare("select * from RECEIPT_HISTORY where CUSTOMER_ID = :ID ORDER BY `ID` DESC"); $sql->bindParam("ID",$customer['ID']); if($sql->execute()) { $HTML = $sql->fetchall(); } if($HTML) { return $HTML;//return array of customer info } return false;//no customer found } function GetAllReceiptsDB($customer) { $DB = connectToDB(); $sql = $DB->prepare("select * from RECEIPT_HISTORY"); if($sql->execute()) { $HTML = $sql->fetchall(); } if($HTML) { return $HTML;//return array of customer info } return false;//no customer found } ?>