Home > Enterprise >  Reset automatic code number every year on laravel
Reset automatic code number every year on laravel

Time:08-21

guys! i have automatic code number for invoice. the automatic code number is "xx/month/year". i want "xx" in my automatic code number reset to 1 when the year change or every 1 january. this is my code

function noinvoice()

{

$latest = Pengiriman::latest()->first();
$month = date('m');
$year = date('Y');

if (!$latest) {
    return '1/' . $month . '/' . $year;
}

$string = preg_replace("/[^0-9\.]/", '', $latest->noinvoice);


return sprintf($string   1) . '/' . $month . '/' . $year;

}

CodePudding user response:

You need to inspect your $latest object. If $latest doesn't exist OR its year doesn't match this year, then the invoice number is 1. Otherwise, it's $latest->noinvoice 1.

You never mentioned where you store your invoice creation date, so I'll assume you use the default created_at Laravel approach. For this task you should add one more condition to your if statement:

if (!$latest or date('Y', strtotime($latest->created_at)) != $year) {
  • Related