Feature to reset id number each month

Hi,

I’m looking for a setting that can reset my invoice ID number each month. Is this possible someway, without doing it manually?

Take a look at the invoice groups
You’ll have a couple of variables there

But you usually get {{year}}{{month}}{{id}}
You don’t have:
“Oh, hey, it’s may now, let’s recet the number counter”

I think what you’re looking for is in this file:
application/modules/invoice_groups/models/Mdl_invoice_groups.php

There’s a function:

    public function set_next_invoice_number($invoice_group_id)
    {
        $this->db->where($this->primary_key, $invoice_group_id);
        $this->db->set('invoice_group_next_id', 'invoice_group_next_id+1', false);
        $this->db->update($this->table);

You want something similar:
“reset_next_invoice_number”

Hey, it’s may now => reset_next_invoice_number which updates invoice_group_next_id to 1, because it’s a new month

to the devs should this work?

ALTER TABLE invoice_groups ADD COLUMN invoice_group_last_reset DATE;

php

public function set_next_invoice_number($invoice_group_id)
{
    // Haal huidige datum
    $current_month = date('Y-m-01'); // 1e dag van huidige maand

    // Haal huidige gegevens van de invoice group op
    $this->db->where($this->primary_key, $invoice_group_id);
    $group = $this->db->get($this->table)->row();

    if (!$group) {
        return false; // niet gevonden
    }

    // Check of de maand anders is dan de laatste reset
    if (!$group->invoice_group_last_reset || $group->invoice_group_last_reset < $current_month) {
        // Reset teller naar 1 en update de resetdatum
        $this->db->where($this->primary_key, $invoice_group_id);
        $this->db->update($this->table, [
            'invoice_group_next_id' => 1,
            'invoice_group_last_reset' => $current_month
        ]);
    } else {
        // Verhoog gewoon de teller
        $this->db->where($this->primary_key, $invoice_group_id);
        $this->db->set('invoice_group_next_id', 'invoice_group_next_id+1', false);
        $this->db->update($this->table);
    }
}

Yes, your solution should work as intended.

The SQL:

ALTER TABLE invoice_groups ADD COLUMN invoice_group_last_reset DATE;

is correct for adding a monthly reset tracking column.

Your PHP function checks if the current date is in a new month by comparing it to invoice_group_last_reset, and then either:

Resets invoice_group_next_id to 1 (if a new month has started), or

Increments the counter for the same month.

The comparison using:

$current_month = date('Y-m-01');

is reliable, assuming invoice_group_last_reset is stored as a DATE. Since you’re only updating one row using:

$this->db->where($this->primary_key, $invoice_group_id);

So yes — this approach should work fine in production.

1 Like