I just found out that this part is responsible. It is after the code in InvoiceRepository above. It's for the making of an invoice if there is a shooting team with a few shooters. But I have not created any team so we shouldn't be here at all, but apparently we are. I think checking for if a team is present could be the solution. If I comment out all this code ordinary invoices are created correctly without any extra empty ones.
/**
* Create a separate invoice for the teams instead of attaching
* them to the same invoice.
*/
if($teamIds){
$query = \App\Models\Team::with('Competition', 'Weapongroup');
$query->whereIn('teams.id', $teamIds);
$query->orderBy('competitions_id') -> orderBy('name');
$query->where(function($query) use ($club, $sender, $type){
$query->whereNull('invoices_id');
$query->where('clubs_id', $club->id);
$query->whereHas('Competition', function($query) use ($sender, $type){
$query->where('invoices_recipient_type', $type);
$query->where('invoices_recipient_id', $sender->id);
});
});
$teams = $query->get();
} else {
$teams = false;
}
dump($invoice_nr);
if($teams):
// Create the invoice.
$invoice = new \App\Models\Invoice;
$invoice->created_by = \Auth::id();
$invoice->recipient_id = $club->id;
$invoice->recipient_type = 'App\Models\Club';
$invoice->recipient_name = $club->name;
$invoice->recipient_address_street = $club->address_street;
$invoice->recipient_address_street_2 = $club->address_street_2;
$invoice->recipient_address_zipcode = $club->address_zipcode;
$invoice->recipient_address_city = $club->address_city;
$invoice->sender_id = $sender->id;
$invoice->sender_type = $type;
$invoice->sender_name = $sender->name;
$invoice_nr = \App\Models\Invoice::GenerateInvoiceNumber($type, $sender->id);
$invoice->invoice_nr = $invoice_nr;
$invoice->invoice_reference = $sender->id.date('Y').$invoice_nr;
$invoice->sender_address_street = $sender->address_street;
$invoice->sender_address_street_2 = $sender->address_street_2;
$invoice->sender_address_zipcode = $sender->address_zipcode;
$invoice->sender_address_city = $sender->address_city;
$invoice->sender_bankgiro = $sender->bankgiro;
$invoice->sender_postgiro = $sender->postgiro;
$invoice->sender_swish = $sender->swish;
$invoice->save();
foreach($teams as $team):
$sortorder++;
$invoice->expiration_date = (!$invoice->expiration_date || $invoice->expiration_date > $team->Competition->signups_closing_date) ? $team->Competition->signups_closing_date : $invoice->expiration_date;
$invoice->InvoiceRows()->create([
'description' => _('Lag').': '.$team->name.' '.$team->Competition->name.' '.$team->Competition->date.' ('.$team->Weapongroup->name.')',
'quantity' => 1,
'unit' => _('st'),
'net_unit_amount' => $team->registration_fee,
'vat_percent' => 0,
'vat_amount' => 0,
'row_net_amount' => 1 * $team->registration_fee,
'row_vat_amount' => 0,
'row_sum_amount' => 1 * $team->registration_fee,
'sortorder' => $sortorder
]);
$invoice->Teams()->save($team);
endforeach;
$amount = $invoice->InvoiceRows()->sum('row_sum_amount');
$invoice->amount = $amount;
$invoice->expiration_date = (!$invoice->expiration_date || $invoice->expiration_date < date('Y-m-d')) ? date('Y-m-d') : $invoice->expiration_date;
>>>>>>>>>COMMENT Here I can see the extra invoice number being dumped:<<<<<<<<<<<<
dump($invoice_nr);
$invoice->save();
$invoices->push($invoice);
endif;