From 3357c1f0320b668ea2e9b0ac184451a6bdb0690b Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Fri, 27 Jun 2014 15:50:05 -0400 Subject: [PATCH] Select current plan and show that correctly --- .../controllers/BillingController.php | 114 ++++++++++-------- .../forms/BillingUpgradeDowngrade.php | 6 +- .../views/scripts/billing/client.phtml | 1 + .../views/scripts/billing/upgrade.phtml | 7 +- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/airtime_mvc/application/controllers/BillingController.php b/airtime_mvc/application/controllers/BillingController.php index 4f967264a..696c69319 100644 --- a/airtime_mvc/application/controllers/BillingController.php +++ b/airtime_mvc/application/controllers/BillingController.php @@ -394,60 +394,11 @@ class BillingController extends Zend_Controller_Action { self::viewInvoice($invoice_id); } + /** Get the Airtime instance ID of the instance the customer is currently viewing. */ private static function getClientInstanceId() { - $credentials = self::getAPICredentials(); - - $postfields = array(); - $postfields["username"] = $credentials["username"]; - $postfields["password"] = md5($credentials["password"]); - $postfields["action"] = "getclientsproducts"; - $postfields["responsetype"] = "json"; - $postfields["clientid"] = Application_Model_Preference::GetClientId(); - - $query_string = ""; - foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; - - $result = self::makeRequest($credentials["url"], $query_string); - Logging::info($result); - - //XXX: Debugging / local testing - if ($_SERVER['SERVER_NAME'] == "airtime.localhost") { - return "1384"; - } - - //This code must run on airtime.pro for it to work... it's trying to match - //the server's hostname with the client subdomain. Once it finds a match - //between the product and the server's hostname/subdomain, then it - //returns the ID of that product (aka. the service ID of an Airtime instance) - foreach ($result["products"] as $product) - { - if (strpos($product[0]["groupname"], "Airtime") === FALSE) - { - //Ignore non-Airtime products - continue; - } - else - { - if ($product[0]["status"] === "Active") { - $airtimeProduct = $product[0]; - $subdomain = ''; - - foreach ($airtimeProduct['customfields']['customfield'] as $customField) - { - if ($customField['name'] === SUBDOMAIN_WHMCS_CUSTOM_FIELD_NAME) - { - $subdomain = $customField['value']; - if (($subdomain . ".airtime.pro") === $_SERVER['SERVER_NAME']) - { - return $airtimeProduct['id']; - } - } - } - } - } - } - throw new Exception("Unable to match subdomain to a service ID"); + $currentProduct = BillingController::getClientCurrentAirtimeProduct(); + return $currentProduct["id"]; } public static function getProducts() @@ -491,6 +442,65 @@ class BillingController extends Zend_Controller_Action { return array($productPrices, $productTypes); } + /** Get the plan (or product in WHMCS lingo) that the customer is currently on. + * @return An associative array containing the fields for the product + * */ + public static function getClientCurrentAirtimeProduct() + { + static $airtimeProduct = null; + //Ghetto caching to avoid multiple round trips to WHMCS + if ($airtimeProduct) { + return $airtimeProduct; + } + $credentials = self::getAPICredentials(); + + $postfields = array(); + $postfields["username"] = $credentials["username"]; + $postfields["password"] = md5($credentials["password"]); + $postfields["action"] = "getclientsproducts"; + $postfields["responsetype"] = "json"; + $postfields["clientid"] = Application_Model_Preference::GetClientId(); + + $query_string = ""; + foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; + + $result = self::makeRequest($credentials["url"], $query_string); + + //XXX: Debugging / local testing + if ($_SERVER['SERVER_NAME'] == "airtime.localhost") { + $_SERVER['SERVER_NAME'] = "bananas.airtime.pro"; + } + + //This code must run on airtime.pro for it to work... it's trying to match + //the server's hostname with the client subdomain. Once it finds a match + //between the product and the server's hostname/subdomain, then it + //returns the ID of that product (aka. the service ID of an Airtime instance) + foreach ($result["products"] as $product) + { + if (strpos($product[0]["groupname"], "Airtime") === FALSE) + { + //Ignore non-Airtime products + continue; + } + else + { + if ($product[0]["status"] === "Active") { + $airtimeProduct = $product[0]; + $subdomain = ''; + + foreach ($airtimeProduct['customfields']['customfield'] as $customField) { + if ($customField['name'] === SUBDOMAIN_WHMCS_CUSTOM_FIELD_NAME) { + $subdomain = $customField['value']; + if (($subdomain . ".airtime.pro") === $_SERVER['SERVER_NAME']) { + return $airtimeProduct; + } + } + } + } + } + } + throw new Exception("Unable to match subdomain to a service ID"); + } public static function getClientDetails() { diff --git a/airtime_mvc/application/forms/BillingUpgradeDowngrade.php b/airtime_mvc/application/forms/BillingUpgradeDowngrade.php index b89136689..cc2cea6e6 100644 --- a/airtime_mvc/application/forms/BillingUpgradeDowngrade.php +++ b/airtime_mvc/application/forms/BillingUpgradeDowngrade.php @@ -17,14 +17,16 @@ class Application_Form_BillingUpgradeDowngrade extends Zend_Form $pid->setLabel(_('Plan type:')) ->setMultiOptions($productTypes) ->setRequired(true) - ->setValue(26); + ->setValue(BillingController::getClientCurrentAirtimeProduct()["pid"]); $this->addElement($pid); + Logging::info(BillingController::getClientCurrentAirtimeProduct()); $billingcycle = new Zend_Form_Element_Radio('newproductbillingcycle'); $billingcycle->setLabel(_('Billing cycle:')) ->setMultiOptions(array('monthly' => 'Monthly', 'annually' => 'Annually')) ->setRequired(true) - ->setValue('monthly'); + ->setValue(BillingController::getClientCurrentAirtimeProduct()["billingcycle"]); + $this->addElement($billingcycle); $paymentmethod = new Zend_Form_Element_Radio('paymentmethod'); diff --git a/airtime_mvc/application/views/scripts/billing/client.phtml b/airtime_mvc/application/views/scripts/billing/client.phtml index e57fb140b..44a3c8d72 100644 --- a/airtime_mvc/application/views/scripts/billing/client.phtml +++ b/airtime_mvc/application/views/scripts/billing/client.phtml @@ -10,5 +10,6 @@ $this->form->getElement("securityqid")->setAttrib("class", "right-align"); successMessage)) {?>
successMessage ?>
+ form ?> \ No newline at end of file diff --git a/airtime_mvc/application/views/scripts/billing/upgrade.phtml b/airtime_mvc/application/views/scripts/billing/upgrade.phtml index 8eb573249..a70e31ced 100644 --- a/airtime_mvc/application/views/scripts/billing/upgrade.phtml +++ b/airtime_mvc/application/views/scripts/billing/upgrade.phtml @@ -138,6 +138,8 @@ $(document).ready(function() { vatFieldChangeTimer = null; $(vatFieldId).change(function() { + $("#vaterror").text("Please wait, checking VAT number..."); + if (vatFieldChangeTimer) { clearTimeout(vatFieldChangeTimer); } @@ -246,7 +248,10 @@ $(document).ready(function() { -

Current Plan: +

Current Plan: +

Choose a plan: