只想在发票中包含所选的

Want to include just the chosen ones in the invoice

在我的手枪比赛系统中,我们有一个发票系统。问题是可以选择一个或多个注册,但不是全部用于创建发票,而是创建所有注册。例如,我们有两个注册,John Doe 和 Mary Doe,我只选择 John Doe 作为发票,两者都是在一张发票中创建的。

看起来像这样:https://imgur.com/wAnWYit

开始时有一些代码负责处理所选的注册,但更进一步的代码没有处理。

                if($signupIds){
                        $query->whereIn('competitions_signups.id', $signupIds);
                    }   dump($signupIds);      //Only the chosen is dumped here.   ***COMMENT***

这里是转储:

 ------------ ---------------------------------------- 
  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"
]

文件是InvoiceRepository.php php代码:

    /** 
     * 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 is dumped here.                              ***COMMENT***
                });
                $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                                           ***COMMENT***
                    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):
                            $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;
    }

如何修改才能正常工作?

我得到了解决方案。这是调整后的代码部分:

        /**
         * 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);
                    }
                });
                $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, $signupIds, $teamIds){
            $signupsGroupedBySender = $invoiceRecipientType->groupBy('invoices_recipient_id');
            $signupsGroupedBySender->each(function($signups, $index) use($club, $invoices, $type, $signupIds, $teamIds) {
                $signupsGroupedByCompetition = $signups->groupBy('competitions_id');
                $signupsGroupedByCompetition->each(function($item) use ($index, $club, $invoices, $type, $signupIds,  $teamIds){
                    $sender = ($type == 'App\Models\District') ? District::find($index) : Club::find($index);

                    /**
                     * Collect the signups to attach to the invoice.
                     */
                    if($signupIds){
                    $query = \App\Models\Signup::with('User','Competition', 'Weaponclass');
                    $query->whereIn('competitions_signups.id', $signupIds);
                    $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();
                } else {
                    $signups = null;
                }

                    if(!$signups->isEmpty()):
                        $invoice = new \App\Models\Invoice;
                        $invoice->created_by = \Auth::id();
.
.

$signupIds 在开始时丢失了三行,我不得不围绕收集注册的 $querys 执行 if($signupIds){。所以现在只收集选定的发票。它就像一个魅力。谢谢大家的提示!