瀏覽代碼

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

IceToast 2 年之前
父節點
當前提交
e795b65df1
共有 2 個文件被更改,包括 4 次插入8 次删除
  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)
     public function handle(PaymentEvent $event)
     {
     {
         if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
         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
             // 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;
 namespace App\Traits;
 
 
 use App\Models\PartnerDiscount;
 use App\Models\PartnerDiscount;
+use App\Models\Payment;
 use App\Models\ShopProduct;
 use App\Models\ShopProduct;
 use App\Notifications\InvoiceNotification;
 use App\Notifications\InvoiceNotification;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\Storage;
@@ -14,9 +15,9 @@ use Symfony\Component\Intl\Currencies;
 
 
 trait Invoiceable
 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
         //create invoice
         $lastInvoiceID = \App\Models\Invoice::where("invoice_name", "like", "%" . now()->format('mY') . "%")->count("id");
         $lastInvoiceID = \App\Models\Invoice::where("invoice_name", "like", "%" . now()->format('mY') . "%")->count("id");
         $newInvoiceID = $lastInvoiceID + 1;
         $newInvoiceID = $lastInvoiceID + 1;
@@ -33,7 +34,6 @@ trait Invoiceable
             ],
             ],
         ]);
         ]);
 
 
-
         $customer = new Buyer([
         $customer = new Buyer([
             'name' => $user->name,
             'name' => $user->name,
             'custom_fields' => [
             'custom_fields' => [
@@ -78,7 +78,6 @@ trait Invoiceable
         $invoice->render();
         $invoice->render();
         Storage::disk("local")->put("invoice/" . $user->id . "/" . now()->format('Y') . "/" . $invoice->filename, $invoice->output);
         Storage::disk("local")->put("invoice/" . $user->id . "/" . now()->format('Y') . "/" . $invoice->filename, $invoice->output);
 
 
-
         \App\Models\Invoice::create([
         \App\Models\Invoice::create([
             'invoice_user' => $user->id,
             'invoice_user' => $user->id,
             'invoice_name' => $invoice->getSerialNumber(),
             'invoice_name' => $invoice->getSerialNumber(),