Skip to content

Commit c765b03

Browse files
authored
Merge pull request #13 from ptisp/joao
Tax ID customfield config dropdown
2 parents d46aeae + b137240 commit c765b03

2 files changed

Lines changed: 114 additions & 19 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ $additionaldomainfields[".edu.pt"][] = array("Name" => "Visible", "LangVar" => "
2525

2626
###Module configuration:
2727
* `Username` - PTisp customer username. (usually your email address)
28-
* `Hash` - API Hash, you may find it at https://my.ptisp.pt/#profile/hash
29-
* `Nichandle` - Nichandle to be used as tech contact.
30-
* `Nameserver` - First default nameserver for registrations.
31-
* `Nameserver2` - Second default nameserver for registrations.
32-
* `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.
33-
* `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
28+
* `Hash` - API Hash, you may find it at https://my.ptisp.pt/profile/apihash
29+
* `Default Technical Nic-handle` - Nichandle to be used as tech contact.
30+
* `Default Name Server 1` - First default nameserver used on registrations.
31+
* `Default Name Server 2` - Second default nameserver used on registrations.
32+
* `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.
33+
* `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.
3434

3535

3636
#Contributions

ptisp.php

Lines changed: 108 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<?php
22

3-
//v2.2.9
3+
//v2.2.10
44

55
require_once("RestRequest.inc.php");
6+
use WHMCS\Database\Capsule;
67

7-
function ptisp_getConfigArray() {
8+
function ptisp_getConfigArray($params) {
89
$configarray = array(
9-
"Username" => array("Type" => "text", "Size" => "20", "Description" => "Enter your username here",),
10-
"Hash" => array("Type" => "password", "Size" => "100", "Description" => "Enter your access hash here",),
11-
"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",),
12-
"Nichandle" => array("Type" => "text", "Description" => "Specify your nichandle, it will be used as Tech Contact after a domain registration.",),
13-
"Nameserver" => array("Type" => "text", "Description" => "Default nameserver to use in registration.",),
14-
"Nameserver2" => array("Type" => "text", "Description" => "Default nameserver to use in registration.",),
15-
"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")
10+
"Username" => array("FriendlyName" => "Username", "Type" => "text", "Size" => "20", "Description" => "Enter your username here.",),
11+
"Hash" => array("FriendlyName" => "Hash", "Type" => "password", "Size" => "100", "Description" => "Enter your access hash here.",),
12+
"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."),
13+
"Nichandle" => array("FriendlyName" => "Default Technical Nic-handle", "Type" => "text", "Description" => "Default Tech contact used on domain registrations.",),
14+
"Nameserver" => array("FriendlyName" => "Default Name Server 1", "Type" => "text", "Description" => "Default nameserver to use in registration.",),
15+
"Nameserver2" => array("FriendlyName" => "Default Name Server 2", "Type" => "text", "Description" => "Default nameserver to use in registration.",),
1616
);
17+
if (!ptisp_isTaxIdEnabled()) {
18+
$options = ptisp_getCustomfieldDropdownOptions($params);
19+
$configarray["Vatcustom"] = array("FriendlyName" => "Tax ID Custom Field", "Type" => "dropdown", "Description" => "The custom field which stores the client's Tax ID.", "Options" => $options, "Default" => "");
20+
}
1721
return $configarray;
1822
}
1923

@@ -175,8 +179,6 @@ function ptisp_TransferDomain($params) {
175179

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

178-
error_log(print_r($result, true));
179-
180182
if ($result["result"] != "ok") {
181183
if (empty($result["message"])) {
182184
$values["error"] = "unknown";
@@ -282,7 +284,12 @@ function ptisp_RegisterDomain($params) {
282284
$sld = $params["sld"];
283285
$regperiod = $params["regperiod"];
284286

285-
$vatid = !empty($params["tax_id"]) ? $params["tax_id"] : (!empty($params[$params["Vatcustom"]]) ? $params[$params["Vatcustom"]] : null);
287+
if (ptisp_isTaxIdEnabled()) {
288+
$vatid = $params["tax_id"];
289+
} else {
290+
$vatcustomfield = ptisp_getTaxIdCustomfieldRef($params);
291+
$vatid = !is_null($vatcustomfield) ? $params[$vatcustomfield] : null;
292+
}
286293

287294
if (!empty($params["additionalfields"]["Nichandle"])) {
288295
$contact = $params["additionalfields"]["Nichandle"];
@@ -363,4 +370,92 @@ function utf8ToUnicode($str) {
363370
}, $str);
364371
}
365372

366-
?>
373+
function ptisp_getCustomfieldDropdownOptions($params) {
374+
try {
375+
$fields = Capsule::table("tblcustomfields")
376+
->select()
377+
->where("type", "=", "client")
378+
->where("fieldtype", "=", "text")
379+
->get();
380+
381+
preg_match('/^customfields(\d+)$/', $params["Vatcustom"], $matches);
382+
//retrocompatible with old configuration settings
383+
if (isset($matches[1])) {
384+
$fieldName = ptisp_getCustomFieldName($matches[1]);
385+
$options = array($fieldName => $fieldName);
386+
} else {
387+
$options = array("" => "None");
388+
}
389+
390+
foreach ($fields as $field) {
391+
$options[$field->fieldname] = $field->fieldname;
392+
}
393+
394+
return $options;
395+
} catch (\Exception $e) {
396+
error_log($e->getMessage());
397+
return "";
398+
}
399+
}
400+
401+
function ptisp_isTaxIdEnabled() {
402+
try {
403+
$setting = Capsule::table("tblconfiguration")
404+
->select()
405+
->where("setting", "=", "TaxIDDisabled")
406+
->first();
407+
if (is_null($setting)) {
408+
$isTaxIdEnabled = false;
409+
} else {
410+
$isTaxIdEnabled = !$setting->value;
411+
}
412+
return $isTaxIdEnabled;
413+
} catch (\Exception $e) {
414+
error_log($e->getMessage());
415+
return null;
416+
}
417+
}
418+
419+
function ptisp_getTaxIdCustomfieldRef($params) {
420+
$taxIdField = $params["Vatcustom"];
421+
$hasOldSetting = preg_match('/^customfields(\d+)$/', $taxIdField);
422+
423+
//retrocompatible with old configuration settings
424+
if ($hasOldSetting) {
425+
return $taxIdField;
426+
}
427+
428+
try {
429+
$field = Capsule::table("tblcustomfields")
430+
->select()
431+
->where("fieldname", "=", $taxIdField)
432+
->first();
433+
if (is_null($field)) {
434+
return null;
435+
} else {
436+
return "customfields" . $field->sortorder;
437+
}
438+
} catch (\Exception $e) {
439+
error_log($e->getMessage());
440+
return null;
441+
}
442+
}
443+
444+
function ptisp_getCustomFieldName($index) {
445+
try {
446+
$field = Capsule::table("tblcustomfields")
447+
->select()
448+
->where("sortorder", "=", $index)
449+
->first();
450+
if (is_null($field)) {
451+
return null;
452+
} else {
453+
return $field->fieldname;
454+
}
455+
} catch (\Exception $e) {
456+
error_log($e->getMessage());
457+
return null;
458+
}
459+
}
460+
461+
?>

0 commit comments

Comments
 (0)