Skip to content

Commit

Permalink
Merge pull request #13 from ptisp/joao
Browse files Browse the repository at this point in the history
Tax ID customfield config dropdown
  • Loading branch information
joaograca authored Jan 12, 2022
2 parents d46aeae + b137240 commit c765b03
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 19 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ $additionaldomainfields[".edu.pt"][] = array("Name" => "Visible", "LangVar" => "

###Module configuration:
* `Username` - PTisp customer username. (usually your email address)
* `Hash` - API Hash, you may find it at https://my.ptisp.pt/#profile/hash
* `Nichandle` - Nichandle to be used as tech contact.
* `Nameserver` - First default nameserver for registrations.
* `Nameserver2` - Second default nameserver for registrations.
* `DisableFallback` - By default the module uses the nichandle specified in the whmcs domain order (additionaldomainfields makes this possible), if there isn't any it will try to create a contact using your customer's profile data if this fails it will register the domain using your reseller contact. If you want to disable this last fallback to your reseller data, check this checkbox.
* `Vatcustom` - Is the customer custom field ID where the VAT number is stored (customfieldsX). ex: "customfields1". Not required if using WHMCS' VAT Settings, available in version 7.7 and above
* `Hash` - API Hash, you may find it at https://my.ptisp.pt/profile/apihash
* `Default Technical Nic-handle` - Nichandle to be used as tech contact.
* `Default Name Server 1` - First default nameserver used on registrations.
* `Default Name Server 2` - Second default nameserver used on registrations.
* `Do not create contacts with my PTisp profile data` - By default the module uses the nichandle specified in the whmcs domain order (additionaldomainfields makes this possible), if there isn't any it will try to create a contact using your customer's profile data if this fails it will register the domain using your reseller contact. If you want to disable this last fallback to your reseller data, check this checkbox.
* `Tax ID Custom Field` - The customfield which stores client's Vat Number/Tax ID. Available only if the "Customer Tax IDs/VAT Number" setting is disabled or versions prior to WHMCS 7.7.


#Contributions
Expand Down
121 changes: 108 additions & 13 deletions ptisp.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?php

//v2.2.9
//v2.2.10

require_once("RestRequest.inc.php");
use WHMCS\Database\Capsule;

function ptisp_getConfigArray() {
function ptisp_getConfigArray($params) {
$configarray = array(
"Username" => array("Type" => "text", "Size" => "20", "Description" => "Enter your username here",),
"Hash" => array("Type" => "password", "Size" => "100", "Description" => "Enter your access hash here",),
"DisableFallback" => array("Type" => "yesno", "Description" => "If customer data is invalid, domain registration will fail with fallback disabled. Fallback uses your info to register a domain when your customer's info is invalid",),
"Nichandle" => array("Type" => "text", "Description" => "Specify your nichandle, it will be used as Tech Contact after a domain registration.",),
"Nameserver" => array("Type" => "text", "Description" => "Default nameserver to use in registration.",),
"Nameserver2" => array("Type" => "text", "Description" => "Default nameserver to use in registration.",),
"Vatcustom" => array("Type" => "text", "Size" => "100", "Description" => "VAT Number customfield name (format: customfieldsX - replace X accordingly). Not required if using WHMCS' VAT Settings, available in version 7.7 and above")
"Username" => array("FriendlyName" => "Username", "Type" => "text", "Size" => "20", "Description" => "Enter your username here.",),
"Hash" => array("FriendlyName" => "Hash", "Type" => "password", "Size" => "100", "Description" => "Enter your access hash here.",),
"DisableFallback" => array("FriendlyName" => "Do not create contacts with my PTisp profile data", "Contact", "Type" => "yesno", "Description" => "When this option is checked the module won't use your profile data on contact creation, whenever client's data is invalid."),
"Nichandle" => array("FriendlyName" => "Default Technical Nic-handle", "Type" => "text", "Description" => "Default Tech contact used on domain registrations.",),
"Nameserver" => array("FriendlyName" => "Default Name Server 1", "Type" => "text", "Description" => "Default nameserver to use in registration.",),
"Nameserver2" => array("FriendlyName" => "Default Name Server 2", "Type" => "text", "Description" => "Default nameserver to use in registration.",),
);
if (!ptisp_isTaxIdEnabled()) {
$options = ptisp_getCustomfieldDropdownOptions($params);
$configarray["Vatcustom"] = array("FriendlyName" => "Tax ID Custom Field", "Type" => "dropdown", "Description" => "The custom field which stores the client's Tax ID.", "Options" => $options, "Default" => "");
}
return $configarray;
}

Expand Down Expand Up @@ -175,8 +179,6 @@ function ptisp_TransferDomain($params) {

$result = json_decode($request->getResponseBody(), true);

error_log(print_r($result, true));

if ($result["result"] != "ok") {
if (empty($result["message"])) {
$values["error"] = "unknown";
Expand Down Expand Up @@ -282,7 +284,12 @@ function ptisp_RegisterDomain($params) {
$sld = $params["sld"];
$regperiod = $params["regperiod"];

$vatid = !empty($params["tax_id"]) ? $params["tax_id"] : (!empty($params[$params["Vatcustom"]]) ? $params[$params["Vatcustom"]] : null);
if (ptisp_isTaxIdEnabled()) {
$vatid = $params["tax_id"];
} else {
$vatcustomfield = ptisp_getTaxIdCustomfieldRef($params);
$vatid = !is_null($vatcustomfield) ? $params[$vatcustomfield] : null;
}

if (!empty($params["additionalfields"]["Nichandle"])) {
$contact = $params["additionalfields"]["Nichandle"];
Expand Down Expand Up @@ -363,4 +370,92 @@ function utf8ToUnicode($str) {
}, $str);
}

?>
function ptisp_getCustomfieldDropdownOptions($params) {
try {
$fields = Capsule::table("tblcustomfields")
->select()
->where("type", "=", "client")
->where("fieldtype", "=", "text")
->get();

preg_match('/^customfields(\d+)$/', $params["Vatcustom"], $matches);
//retrocompatible with old configuration settings
if (isset($matches[1])) {
$fieldName = ptisp_getCustomFieldName($matches[1]);
$options = array($fieldName => $fieldName);
} else {
$options = array("" => "None");
}

foreach ($fields as $field) {
$options[$field->fieldname] = $field->fieldname;
}

return $options;
} catch (\Exception $e) {
error_log($e->getMessage());
return "";
}
}

function ptisp_isTaxIdEnabled() {
try {
$setting = Capsule::table("tblconfiguration")
->select()
->where("setting", "=", "TaxIDDisabled")
->first();
if (is_null($setting)) {
$isTaxIdEnabled = false;
} else {
$isTaxIdEnabled = !$setting->value;
}
return $isTaxIdEnabled;
} catch (\Exception $e) {
error_log($e->getMessage());
return null;
}
}

function ptisp_getTaxIdCustomfieldRef($params) {
$taxIdField = $params["Vatcustom"];
$hasOldSetting = preg_match('/^customfields(\d+)$/', $taxIdField);

//retrocompatible with old configuration settings
if ($hasOldSetting) {
return $taxIdField;
}

try {
$field = Capsule::table("tblcustomfields")
->select()
->where("fieldname", "=", $taxIdField)
->first();
if (is_null($field)) {
return null;
} else {
return "customfields" . $field->sortorder;
}
} catch (\Exception $e) {
error_log($e->getMessage());
return null;
}
}

function ptisp_getCustomFieldName($index) {
try {
$field = Capsule::table("tblcustomfields")
->select()
->where("sortorder", "=", $index)
->first();
if (is_null($field)) {
return null;
} else {
return $field->fieldname;
}
} catch (\Exception $e) {
error_log($e->getMessage());
return null;
}
}

?>

0 comments on commit c765b03

Please sign in to comment.