In my system for pistol competition we have an invoice system. If my club has competitors that are signed up on another club's competition we can create an invoice because they want a fee for each competitor. The invoice is created so we are the receiver and the other club is sender. In this case, say we have two competitors, John Doe and Mary Doe and I choose only John Doe for invoice but both are created in one invoice. There are checkboxes for each competitor. This is how it looks like: https://imgur.com/wAnWYit There are some code in the beginning that deals with the chosen signups but further down it's not taking care of so we are getting both on the same invoice. This code:
if($signupIds){
$query->whereIn('competitions_signups.id', $signupIds);
} dump($signupIds); //Only the chosen is present here. ***COMMENT***
as here is the dump with only one competitor chosen:
------------ ----------------------------------------
date Thu, 26 May 2022 13:32:25 +0200
controller "ClubInvoicesController"
source InvoiceRepository.php on line 90
file app/Repositories/InvoiceRepository.php
------------ ----------------------------------------
array:1 [
0 => "34"
]
The file is InvoiceRepository.php where the invoice is created:
/**
* Collect all the clubs which to generate a invoice to.
* The club must have one or more competitions that has signups where invoices_id is null.
* Or the club must have one or more competitions that has teams where invoices_id is null.
*/
public function createInvoicesForClub($club, $signupIds = null, $teamIds = null)
{
/**
* Instanciate $invoices as new collection.
*/
$invoices = new \Illuminate\Database\Eloquent\Collection;
$query = Competition::where(function($query) use ($club, $signupIds, $teamIds){
$query->where(function($query) use ($club, $signupIds, $teamIds){
$query->whereHas('Signups', function($query) use ($club, $signupIds){
$query->where(function($query) use ($club) {
$query->whereNull('invoices_id');
$query->where('clubs_id', $club->id);
});
if($signupIds){
$query->whereIn('competitions_signups.id', $signupIds);
} dump($signupIds); ************//Only the chosen one is present here. **
});
$query->orWhereHas('Teams', function($query) use ($club){
$query->where(function($query) use ($club) {
$query->whereNull('invoices_id');
$query->where('clubs_id', $club->id);
});
});
});
});
$competitions = $query->get();
$invoicesRecipientTypes = $competitions->groupBy('invoices_recipient_type');
$invoicesRecipientTypes->each(function($invoiceRecipientType, $type) use($club, $invoices, $teamIds){
$signupsGroupedBySender = $invoiceRecipientType->groupBy('invoices_recipient_id');
$signupsGroupedBySender->each(function($signups, $index) use($club, $invoices, $type, $teamIds) {
$signupsGroupedByCompetition = $signups->groupBy('competitions_id');
$signupsGroupedByCompetition->each(function($item) use ($index, $club, $invoices, $type, $teamIds){
$sender = ($type == 'App\Models\District') ? District::find($index) : Club::find($index);
dump($sender);
/**
* Collect the signups to attach to the invoice.
*/
$query = \App\Models\Signup::with('User','Competition', 'Weaponclass');
$query->orderBy('competitions_id') -> orderBy('users_id');
$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);
});
});
$signups = $query->get();
dd($signups); **//All signups are here, including non-chosen **
if(!$signups->isEmpty()):
$invoice = new \App\Models\Invoice;
$invoice->created_by = \Auth::id();
$invoice->recipient_id = $club->id;
$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_swish = $sender->swish;
$invoice->save();
$sortorder = 0;
foreach($signups as $signup): *****//Here all is present including non-chosen.
$sortorder++;
$invoice->expiration_date = (!$invoice->expiration_date || $invoice->expiration_date > $signup->Competition->signups_closing_date) ? $signup->Competition->signups_closing_date : $invoice->expiration_date;
$invoice->InvoiceRows()->create([
'description' => $signup->User->full_name.' '.$signup->Competition->name.' '.$signup->Competition->date.' ('.(($signup->Competition->championships_id) ? $signup->Weaponclass->classname_general : $signup->Weaponclass->classname).')',
'quantity' => 1,
'unit' => _('st'),
'net_unit_amount' => $signup->registration_fee,
'vat_percent' => 0,
'vat_amount' => 0,
'row_net_amount' => 1 * $signup->registration_fee,
'row_vat_amount' => 0,
'row_sum_amount' => 1 * $signup->registration_fee,
'sortorder' => $sortorder
]);
$invoice->Signups()->save($signup);
endforeach;
$amount = $invoice->InvoiceRows()->sum('row_sum_amount');
$invoice->amount = $amount;
$invoice->save();
$invoices->push($invoice);
});
});
});
return $invoices;
}
How to modify to get it to work? i heard that whereIn can be used, but how?
Here:
$query = \App\Models\Signup::with('User','Competition', 'Weaponclass');
$query->orderBy('competitions_id') -> orderBy('users_id');
$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);
});
});
$signups = $query->get();
dd($signups); **//All signups are here, including non-chosen **
all competitors are present including non-chosen. So the invoice is created with all competitors.
In this:
$query = \App\Models\Signup::with('User','Competition', 'Weaponclass');
all competitors are collected into the array. The earlier:
if($signupIds){
$query->whereIn('competitions_signups.id', $signupIds);
}
gives only the chosen competitors in the variable $signupIds So how to use that to limit?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community