The following article will show you how to manage the transaction code at the websites that generated by PHPMaker by calling the certain function from the related field. The transaction number will automatically be increased by your web application only for the year of that code. When the current year is greater than the year in the last transaction code, then the transaction number will automatically be started from the value of one again; otherwise, it will increase as usual. In other words, the function has the ability to manage the changing of the year.
So we will create the function for this purpose. For example, we want to create the transaction code which has the format like this: "YYYY-NNNNN". The first four "Y" is the year of transaction, and the last five "N" is the transaction number. Let's say that we will name the main function with getNextTransaction(). This getNextTransaction() function is really useful when you want to assign the certain field with the transaction code. You only type the function name getNextTransaction() in the Default Page below the Add Page column of the related field from your PHPMaker application.
As mentioned before, the usage implementation of the function is very easy. Assuming this transaction code will be called by the Transaction_Code field when users add a new record from the transactions table, then simply assign that function name in the Add column -> Default Page sub column of the Transaction_Code field from your PHPMaker application.
[hidepost]
- Open your PHPMaker project file (.pmp) using your PHPMaker application.
- Click on one of the tables you desire, and then in the right pane, click on the Server Events/Client Scripts tab.
-
Go to Server Events -> Global -> All Pages -> Global Code, and then insert the following code to the bottom of the code editor in the right side of your PHPMaker:
function getLastTransaction() { global $conn; // The following SQL syntax is for SQL Server database $sSql = "SELECT TOP 1 * FROM transactions ORDER BY Transaction_Code DESC"; // Adjust the "transactions" and "Transaction_Code" with yours /* // The following SQL syntax is for MySQL database $sSql = "SELECT * FROM transactions ORDER BY Transaction_Code DESC LIMIT 0, 1"; // Adjust the "transactions" and "Transaction_Code" with yours */ $rs = $conn->Execute($sSql); if ($rs->RecordCount() > 0) { $LastTrans = $rs->fields("Transaction_Code"); } else { $LastTrans = '0000-00000'; } return $LastTrans; } function getNextTransaction() { $LastCompleteTrans = getLastTransaction(); $LastYear = intval(substr($LastCompleteTrans, 0, 4)); $LastTrans = substr($LastCompleteTrans, 5, 5); $LastTrans = intval($LastTrans) + 1; $NextNumber = sprintf('%05s', $LastTrans); $dot = date("d-m-Y-H-i-s"); $now = explode ("-",$dot); $NowYear = intval($now[2]); if ($NowYear > $LastYear) { $TheYear = $NowYear; } else { $TheYear = $LastYear; } $NextTrans = $TheYear . '-' . $NextNumber; return $NextTrans; }and don't forget to save the changes in your .pmp file.
- Now, let's try to use the function. Assuming we will call this function whenever users add a new record in the transactions table. Click on the transactions table from your PHPMaker, and make sure you have already clicked or selected the Fields tab. Then click on the Transaction_Code, scroll to the right and find the Add Page. In the Default Page of the field, please type: getNextTransaction().
- Re-generate your script files as usual using PHPMaker, and then test it by adding a new record. You will see that in the Transaction Code row, you will see a transaction code appears there.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.