-1

I have some increment of invoice numbers, where there is Prefix and Counter, Say Prefix is INV- and Counter is started with 001.

Below is code used in Laravel to get the next Invoice number automatically,

        $lastInv = explode('-', $record->invoice_number); //Gets record and split number

        $nextInv = $settings->invoice_number_prefix.'-'. ($lastInv[1] + 1);

In this case, there are two records in DB with INV-001, INV-002, But with the above code, I am getting $nextInv as INV-3

How can I generate next invoice as INV-003

thank you,

rjcode
  • 1,193
  • 1
  • 25
  • 56

1 Answers1

1

you can use str_pad to flll 0 with perfix

like this

$lastInv = explode('-', $record->invoice_number); //Gets record and split number
$number = str_pad($lastInv[1] + 1,3,"0",STR_PAD_LEFT);
$nextInv = $settings->invoice_number_prefix.'-'. $number;

NOTE here str_pad('',3,'0','') here 3 is the number with will fill with zero so you can adjust based on your need

ref link https://www.php.net/manual/en/function.str-pad.php

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33