Jump to content

UpdateInvoice api doesn't work


jerry.li

Recommended Posts

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.

Link to comment
Share on other sites

On 8/1/2022 at 11:57 AM, brianoz said:

You're missing committing the transaction at the end, just before you finish, so mysql is rolling all your changes back!


$pdo->commit();

 

Thanks, using this method solved my problem.


Please tell me, I use localAPI update, why do I need pdo-commit()?

Link to comment
Share on other sites

  • 1 month later...

The commit causes the changes made by localAPI to become permanent.  Changes made after beginTransaction() are otherwise just temporary.   If you left out the beginTransaction() method call it would also work.   The idea of using a transaction is that changes are rolled back if something goes wrong, and the assumption is that things did go wrong if you don't call the commit() method.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated