Automatic Invoice Numbers with PHP and MySQL

How to make automatic Invoice Number with php, that is to say without taking sequentially inputted manually, but according to the last invoice number.

The case is the same as the invoice number, registration number and etc.. For example if the system last purchase invoice number is in-reg-000067, so when someone orders will receive an invoice number is in-reg-000068.

In such cases, we can use the autonumber. I do not mean AutoNumber Auto Increment in mysql, but AutoNumber to print the invoice number. Why is it necessary? because they are fixed invoice number on each record that is input then we use the CHAR type field in the Database. Examples of such invoice number format 0080.000.67.00000003, (Depending on programmer or Order).

we learn how to make the autonumber for invoice number, how can that be? instead of characters to format the invoice number in the example above is not all but there are a number of characters dot (.)! it is true not all the characters in the example above invoice number is a number, but when we look at the last 8 characters on the invoice number is a number we can get a little play - playing with the script in order to generate autonumber.

Create a simple table as below :

 CREATE TABLE IF NOT EXISTS `transaction` (
      `INVOICE_NUMBER` char(19) NOT NULL,
      `DAY` datetime NOT NULL,
      `GRAND_TOTAL` double NOT NULL,
      PRIMARY KEY (`INVOICE_NUMBER`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Then fill in the table with a note in accordance with the wishes of the invoice number as the content of the following formats 010.000.13.00000001 for some records but it's a unique invoice number, for example on the second record 010.000.13.00000002 contents with the invoice number and so on.

     mysql>SELECT INVOICE_NUMBER FROM transaction;

The sql code to display the contents of the field INVOICE_NUMBER and how to take numbers from the invoice number, we see the following code.

     mysql>SELECT SUBSTR(MAX(INVOICE_NUMBER),-5) FROM transaction;

The above code will display the maximum value on record INVOICE_NUMBER, then we will combine it with PHP.

In this php code, I created a function that we can use it efficiently without rewriting any code will create an invoice.

function trans_id($param='inv-str') {
$dataMax = mysql_fetch_assoc(mysql_query(
"SELECT SUBSTR(MAX(`INVOICE_NUMBER`),-5) AS ID  FROM transaction")); // capture maximum data from id transaction

            if($dataMax['ID']=='') { // if data empty
                $ID = $param."00001";
            }else {
                $MaksID = $dataMax['ID'];
                $MaksID++;
                if($MaksID < 10) $ID = $param."0000".$MaksID; // value under 10
                else if($MaksID < 100) $ID = $param."000".$MaksID; // value under 100
                else if($MaksID < 1000) $ID = $param."00".$MaksID; // value under 1000
                else if($MaksID < 10000) $ID = $param."0".$MaksID; // value under 10000
                else $ID = $MaksID; // lebih dari 10000
            }

            return $ID;
        }

The function above will be automatically generated number. How to use it is also very easy.
Previous
Next Post »