Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

knubbe's avatar
Level 36

HOW TO GENERATE INVOICE NUMBER

I have a table invoices with invoice_no column and I want to save a specific number with a following rules: YEAR / iteration from 1 to ... For new year, iteration go from start again. Help :)

1 like
4 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@KNUBBE

I assume that you have a pattern to generate invoice number that is like so-

2017-0001
2017-0002
2017-0003
.
.

In 2018 it could be like that-

2018-0001
2018-0002
2018-0003
.
.
.

Here is the code-

//get last record
$record = RecordModel::latest()->first();
$expNum = explode('-', $record->invoiceno);

//check first day in a year
if ( date('l',strtotime(date('Y-01-01'))) ){
    $nextInvoiceNumber = date('Y').'-0001';
} else {
    //increase 1 with last invoice number
    $nextInvoiceNumber = $expNum[0].'-'. $expNum[1]+1;
}


//now add into database $nextInvoiceNumber as a next number.

now store $nextInvoiceNumber as a next invoice number. If last invoice number is 2017-0287 than next will be 2017-0288

12 likes
melx's avatar

@tisuchi how to add that nextinvoice Number into database?, show the code

3 likes
melx's avatar

@tisuchi can you assist me , i have my code also i want to generate the invoice no automatically

     Invoice no can start like this  "three end digtis of year(019), month-1,2,3... ,number 001P
     so the first invoice looks like this  0191001P, next = 019002P like this on the column of invoice no

see my code: of store sales records

                  public function store(Request $request)


            {


    // dd($request);

    $validator = Validator::make($request->all(), [
        'invoice_no' => 'required',
        'clientAddress' => 'required|max:100',
        'ClientPhone' => 'required',
        'product_name' => 'required',
        'qty' => 'required',
        'price' => 'required',
        'total' => 'required',
        'sub_total' => 'required',
        'tax_amount' => 'required',
        'total_amount' => 'required',
        // 'ClientPhone' => 'required',
    ]);


    $CURDproduct = $request->all();

    if ($validator->passes()) {

        $CURDproduct = new sales;

        $CURDproduct->invoice_no = $request->input('invoice_no');
        $CURDproduct->clientAddress = $request->input('clientAddress');
        $CURDproduct->ClientPhone = $request->input('ClientPhone');
        $CURDproduct->product_name = $request->input('product_name');
        $CURDproduct->qty = $request->input('qty');
        $CURDproduct->price = $request->input('price');
        $CURDproduct->total = $request->input('total');
        $CURDproduct->sub_total = $request->input('sub_total');
        $CURDproduct->tax_amount = $request->input('tax_amount');
        $CURDproduct->total_amount = $request->input('total_amount');
        $CURDproduct->save();
    // return view('product.CURDproduct')->with('CURDproduct', $CURDproduct)->with('message', 'Successfully Stored');
        return redirect('salesView')->with('message', 'Successfully Stored');

    }
    return Response::json(['errors' => $validator->errors()]);      // 'stops'= json_encode($CURDproduct),
}
3 likes

Please or to participate in this conversation.