Bläddra i källkod

refactor: ♻️ Less DB Calls & PartnerDiscount bug at invoice make()

IceToast 2 år sedan
förälder
incheckning
e795b65df1
2 ändrade filer med 4 tillägg och 8 borttagningar
  1. 1 4
      app/Listeners/CreateInvoice.php
  2. 3 4
      app/Traits/Invoiceable.php

+ 1 - 4
app/Listeners/CreateInvoice.php

@@ -19,11 +19,8 @@ class CreateInvoice
     public function handle(PaymentEvent $event)
     {
         if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
-            // get user from payment which does hold the user_id
-            $user = $event->payment->user;
-
             // create invoice using the trait
-            $this->createInvoice($user, $event->payment);
+            $this->createInvoice($event->payment, $event->shopProduct);
         }
     }
 }

+ 3 - 4
app/Traits/Invoiceable.php

@@ -3,6 +3,7 @@
 namespace App\Traits;
 
 use App\Models\PartnerDiscount;
+use App\Models\Payment;
 use App\Models\ShopProduct;
 use App\Notifications\InvoiceNotification;
 use Illuminate\Support\Facades\Storage;
@@ -14,9 +15,9 @@ use Symfony\Component\Intl\Currencies;
 
 trait Invoiceable
 {
-    public function createInvoice($user, $payment)
+    public function createInvoice(Payment $payment, ShopProduct $shopProduct)
     {
-        $shopProduct = ShopProduct::where('id', $payment->shop_item_product_id)->first();
+        $user = $payment->user;
         //create invoice
         $lastInvoiceID = \App\Models\Invoice::where("invoice_name", "like", "%" . now()->format('mY') . "%")->count("id");
         $newInvoiceID = $lastInvoiceID + 1;
@@ -33,7 +34,6 @@ trait Invoiceable
             ],
         ]);
 
-
         $customer = new Buyer([
             'name' => $user->name,
             'custom_fields' => [
@@ -78,7 +78,6 @@ trait Invoiceable
         $invoice->render();
         Storage::disk("local")->put("invoice/" . $user->id . "/" . now()->format('Y') . "/" . $invoice->filename, $invoice->output);
 
-
         \App\Models\Invoice::create([
             'invoice_user' => $user->id,
             'invoice_name' => $invoice->getSerialNumber(),