I created a hook to modify the due date of the invoice.
The whole code executes fine, but at the end I don't see any fields I update in the database.
Can anyone help me to see where the problem is.
Thanks for everyone's help
<?php
use WHMCS\Database\Capsule;
function hook_invoice_payment_terms($vars) {
$pdo = Capsule::connection()->getPdo();
$pdo->beginTransaction();
// Setup
$adminuser = "Jerry";
$responsetype = "json";
$customFieldName = 'Payment Terms';
$options=payment_terms_getoptions();
$defaultTerms=$options["DefaultPaymentTerm"];
$invoiceid = $vars['invoiceid'];
$invoicesource =$vars['source'] ;
$creatinguser = $vars['user'] ;
// Get Invoice structure
$command = "getinvoice";
$values["invoiceid"] = $invoiceid;
$values["responsetype"] = $responsetype ;
$getinvoice = localAPI($command,$values,$adminuser);
//var_dump($getinvoice);
//echo "<br/>";
logModuleCall( 'paymenmterms', $command, $values, $getinvoice);
if ($getinvoice['result']=="success") {
$values = null;
unset($values);
// Get customer details
$command = "getclientsdetails";
$values["clientid"] = $getinvoice['userid'];
$values["stats"] = false;
$values["responsetype"] = $responsetype ;
$getclientsdetails = localAPI($command,$values,$adminuser);
logModuleCall( 'paymenmterms', $command, $values, $getclientsdetails);
$sql = "SELECT DISTINCT SUBSTRING_INDEX(cv.value, '|', 1) FROM tblcustomfieldsvalues AS cv JOIN tblcustomfields AS cf ON cv.fieldid = cf.id WHERE cf.fieldname LIKE '".$customFieldName."' AND cv.relid = ".$getinvoice['userid'];
$res = $pdo->query($sql);
$numrows = $res->rowCount();
/*
$sql = "SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(cv.value, '|', 1), '|', -1) FROM tblcustomfieldsvalues AS cv JOIN tblcustomfields AS cf ON cv.fieldid = cf.id WHERE cf.fieldname LIKE '".$customFieldName."' AND cv.relid = ".$getinvoice['userid'];
$result = mysql_query($sql);
$numrows = mysql_num_rows($result); */
if ($numrows > 0)
{
// logModuleCall( 'paymenmterms', "GetCustomValue", $sql, $result);
$TermDays = $res->fetchColumn($result);
} else {
$TermDays=$defaultTerms;
}
// Get Invoice Due Date
$newInvoiceDate = time() + ($TermDays * 86400);
$newInvoiceDate = date("Y-m-d", $newInvoiceDate);
// Update invoice if not error
//if ( !($errorOnTerms) ) {
logActivity("Updating Invoice $invoiceid due date to $newInvoiceDate");
$command = "UpdateInvoice";
$values["invoiceid"] = $invoiceid;
$values["duedate"] = $newInvoiceDate;
$values["responsetype"] = $responsetype ;
$values["notes"] = "Updating Invoice $invoiceid due date to $newInvoiceDate";
$updateinvoice = localAPI($command,$values,$adminuser);
logModuleCall( 'paymenmterms', $command, $values, $updateinvoice);
if ($updateinvoice['result']!="success") {
// Error Updating Invoice
logActivity("An Error Occurred while updating Invoice: ".$updateinvoice['result']);
} Else {
// Invoice Update Successful!
// Get Invoice structure
$values = array();
$command = "getinvoice";
$values["invoiceid"] = $invoiceid;
$values["responsetype"] = $responsetype ;
$getinvoice = localAPI($command,$values,$adminuser);
//print_r($updateinvoice);
//print_r($getinvoice);
//exit;
}
// Cleanup
$values = null;
unset($values);
} Else {
// Error Getting Invoice
logActivity("ERROR: Unable to retreive Invoice ID $invoiceid!");
}
}
function payment_terms_getoptions() {
// Get configurable options
$table = "tbladdonmodules";
$fields = "setting,value";
$where = array("module"=>"paymentterms");
$result = select_query($table,$fields,$where);
$options = array();
while ($data = mysql_fetch_array($result)) {
$option_name = $data['setting'];
$options["$option_name"] = $data['value'];
}
return $options;
}
add_hook("InvoiceCreation",9999,"hook_invoice_payment_terms");
When I was debugging, after I tried update in the hook, I read out the invoice information, and it was the same as what I submitted for the update;
But in the end this information is not written to the mysql.