llp 3 дней назад
Родитель
Сommit
9f47fb4065

+ 1 - 23
packages/Longyi/Gift/src/Http/Controllers/Shop/GiftController.php

@@ -23,12 +23,6 @@ class GiftController extends Controller
     ) {
     ) {
     }
     }
 
 
-    public function index()
-    {
-        echo 111;exit;
-        return view('giftcard::shop.index');
-    }
-
     public function lists(): JsonResponse
     public function lists(): JsonResponse
     {
     {
         $customerId = auth()->user()->id;
         $customerId = auth()->user()->id;
@@ -155,26 +149,10 @@ class GiftController extends Controller
      */
      */
     public function destroyGiftCard()
     public function destroyGiftCard()
     {
     {
-        // Get the cart instance
-        $cart = Cart::getCart();
-
-        // Remove gift card related fields from the cart
+        // 移除购物车上的礼品卡(下单时才扣余额,移除无需回补)
         GiftCards::removeGiftCardCode();
         GiftCards::removeGiftCardCode();
         Cart::collectTotals();
         Cart::collectTotals();
-        // If a gift card was applied, update GiftCardBalance accordingly
-        if ($cart->giftcard_number) {
-            $giftCards = GiftCards::where('giftcard_number', $cart->giftcard_number)
-                ->where('customer_id', auth()->user()->id)
-                ->first();
-            if ($giftCards) {
-                // Reset used and remaining amounts
-                $giftCards->used_giftcard_amount = 0;
-                $giftCards->remaining_giftcard_amount = $giftCards->giftcard_amount;
-                $giftCards->save();
-            }
-        }
 
 
-        // Return response with updated cart data
         return ApiResponse::success(new CustomCartResource(Cart::getCart()), trans('gift::app.giftcard.remove'));
         return ApiResponse::success(new CustomCartResource(Cart::getCart()), trans('gift::app.giftcard.remove'));
     }
     }
 
 

+ 7 - 6
packages/Longyi/Gift/src/Models/GiftCards.php

@@ -92,17 +92,18 @@ class GiftCards extends Model implements GiftCardsContract
     public static function removeGiftCardCode()
     public static function removeGiftCardCode()
     {
     {
         $cart = cart()->getCart();
         $cart = cart()->getCart();
-        $giftcardAmount = $cart->giftcard_amount ?? 0;
 
 
-        // 清除购物车中的礼品卡标记
+        if (! $cart) {
+            return false;
+        }
+
+        // 清除购物车中的礼品卡标记。
+        // 注意:不要在这里回补 grand_total —— 调用方随后都会 collectTotals() 重算总额,
+        // 单独回补反而会在没有 collectTotals 的路径上把金额算错。
         $cart->giftcard_number = null;
         $cart->giftcard_number = null;
         $cart->giftcard_amount = null;
         $cart->giftcard_amount = null;
         $cart->base_giftcard_amount = null;
         $cart->base_giftcard_amount = null;
 
 
-//        // 恢复购物车总额
-//        $cart->grand_total += $giftcardAmount;
-//        $cart->base_grand_total += $giftcardAmount;
-
         $cart->save();
         $cart->save();
 
 
         return true;
         return true;

+ 16 - 5
packages/Longyi/Gift/src/Repositories/CustomInvoiceRepository.php

@@ -12,10 +12,9 @@ class CustomInvoiceRepository extends BaseInvoiceRepository
         $invoice->sub_total = $invoice->base_sub_total = 0;
         $invoice->sub_total = $invoice->base_sub_total = 0;
         $invoice->sub_total_incl_tax = $invoice->base_sub_total_incl_tax = 0;
         $invoice->sub_total_incl_tax = $invoice->base_sub_total_incl_tax = 0;
         $invoice->tax_amount = $invoice->base_tax_amount = 0;
         $invoice->tax_amount = $invoice->base_tax_amount = 0;
-        $invoice->shipping_tax_amount = $invoice->shipping_tax_amount = 0;
+        $invoice->shipping_tax_amount = $invoice->base_shipping_tax_amount = 0;
         $invoice->discount_amount = $invoice->base_discount_amount = 0;
         $invoice->discount_amount = $invoice->base_discount_amount = 0;
-        $invoice->giftcard_amount = $invoice->giftcard_amount = 0;
-        $invoice->base_giftcard_amount = $invoice->base_giftcard_amount = 0;
+        $invoice->giftcard_amount = $invoice->base_giftcard_amount = 0;
         $invoice->vip_plus_amount = $invoice->base_vip_plus_amount = 0;
         $invoice->vip_plus_amount = $invoice->base_vip_plus_amount = 0;
         $invoice->vip_discount_amount = $invoice->base_vip_discount_amount = 0;
         $invoice->vip_discount_amount = $invoice->base_vip_discount_amount = 0;
         $invoice->shipping_insurance_amount = $invoice->base_shipping_insurance_amount = 0;
         $invoice->shipping_insurance_amount = $invoice->base_shipping_insurance_amount = 0;
@@ -44,10 +43,22 @@ class CustomInvoiceRepository extends BaseInvoiceRepository
         $invoice->discount_amount += $invoice->order->shipping_discount_amount;
         $invoice->discount_amount += $invoice->order->shipping_discount_amount;
         $invoice->base_discount_amount += $invoice->order->base_shipping_discount_amount;
         $invoice->base_discount_amount += $invoice->order->base_shipping_discount_amount;
 
 
-        $invoice->giftcard_amount += $invoice->order->giftcard_amount;
-        $invoice->base_giftcard_amount += $invoice->order->base_giftcard_amount;
         $invoice->giftcard_number = $invoice->order->giftcard_number;
         $invoice->giftcard_number = $invoice->order->giftcard_number;
 
 
+        // 礼品卡只记在首张发票:若订单已有其它带礼品卡的发票,本张不再重复全额抵扣
+        $giftcardAlreadyInvoiced = false;
+        foreach ($invoice->order->invoices as $prevInvoice) {
+            if ($prevInvoice->id !== $invoice->id && (float) $prevInvoice->giftcard_amount > 0) {
+                $giftcardAlreadyInvoiced = true;
+                break;
+            }
+        }
+
+        if (! $giftcardAlreadyInvoiced) {
+            $invoice->giftcard_amount += $invoice->order->giftcard_amount;
+            $invoice->base_giftcard_amount += $invoice->order->base_giftcard_amount;
+        }
+
         $invoice->vip_plus_amount += $invoice->order->vip_plus_amount;
         $invoice->vip_plus_amount += $invoice->order->vip_plus_amount;
         $invoice->base_vip_plus_amount += $invoice->order->base_vip_plus_amount;
         $invoice->base_vip_plus_amount += $invoice->order->base_vip_plus_amount;
         $invoice->vip_discount_amount += $invoice->order->vip_discount_amount;
         $invoice->vip_discount_amount += $invoice->order->vip_discount_amount;

+ 6 - 4
packages/Longyi/Gift/src/Resources/views/components/giftcard-cartsummary.blade.php

@@ -127,14 +127,16 @@
             },
             },
 
 
             mounted() {
             mounted() {
+                @if(auth()->guard('customer')->check())
                 this.autoApplyExpiringGiftCard();
                 this.autoApplyExpiringGiftCard();
+                @endif
             },
             },
 
 
             methods: {
             methods: {
                 // 自动应用即将过期的礼品卡
                 // 自动应用即将过期的礼品卡
                 autoApplyExpiringGiftCard() {
                 autoApplyExpiringGiftCard() {
                     console.log('Checking for expiring gift cards...');
                     console.log('Checking for expiring gift cards...');
-                    this.$axios.post("{{ route('shop.api.checkout.cart.giftcard.auto-apply') }}", {
+                    this.$axios.post("{{ route('shop.gift.auto-apply-expiring') }}", {
                         _token: "{{ csrf_token() }}"
                         _token: "{{ csrf_token() }}"
                     })
                     })
                         .then((response) => {
                         .then((response) => {
@@ -160,7 +162,7 @@
                 loadGiftCards() {
                 loadGiftCards() {
                     console.log('Loading gift cards...');
                     console.log('Loading gift cards...');
                     this.loading = true;
                     this.loading = true;
-                    this.$axios.get("{{ route('shop.api.checkout.cart.giftcard.list') }}")
+                    this.$axios.get("{{ route('shop.gift.my-gift-cards') }}")
                         .then((response) => {
                         .then((response) => {
                             console.log('Gift cards response:', response.data);
                             console.log('Gift cards response:', response.data);
                             this.loading = false;
                             this.loading = false;
@@ -208,7 +210,7 @@
                     }
                     }
 
 
                     this.isStoring = true;
                     this.isStoring = true;
-                    this.$axios.post("{{ route('shop.api.checkout.cart.giftcard.activate') }}", {
+                    this.$axios.post("{{ route('shop.gift.activate') }}", {
                         giftcard_number: this.giftCardNumber,
                         giftcard_number: this.giftCardNumber,
                         _token: "{{ csrf_token() }}"
                         _token: "{{ csrf_token() }}"
                     })
                     })
@@ -244,7 +246,7 @@
 
 
                 destroyGiftCard() {
                 destroyGiftCard() {
                     console.log('Removing gift card...');
                     console.log('Removing gift card...');
-                    this.$axios.delete("{{ route('shop.api.checkout.cart.giftcard.remove') }}", {
+                    this.$axios.delete("{{ route('shop.gift.remove') }}", {
                         headers: {
                         headers: {
                             'X-CSRF-TOKEN': "{{ csrf_token() }}"
                             'X-CSRF-TOKEN': "{{ csrf_token() }}"
                         }
                         }

+ 30 - 7
packages/Longyi/Gift/src/Routes/shop-routes.php

@@ -4,11 +4,34 @@ use Illuminate\Support\Facades\Route;
 use Longyi\Gift\Http\Controllers\Shop\GiftController;
 use Longyi\Gift\Http\Controllers\Shop\GiftController;
 use Longyi\RewardPoints\Http\Middleware\ApiCustomerAuthenticate;
 use Longyi\RewardPoints\Http\Middleware\ApiCustomerAuthenticate;
 
 
-Route::group(['prefix' => 'api', 'middleware' => ['api','web']], function () {
-    Route::get('/lists', [GiftController::class, 'lists'])->name('shop.api.gift.lists');
-    Route::post('/add', [GiftController::class, 'add'])->name('shop.api.gift.add');
-    Route::get('/my-gift-cards', [GiftController::class, 'getCustomerGiftCards'])->name('shop.api.checkout.cart.giftcard.list');
-    Route::post('/activate', [GiftController::class, 'activateGiftCard'])->name('shop.api.checkout.cart.giftcard.activate');
-    Route::delete('/remove', [GiftController::class, 'destroyGiftCard'])->name('shop.api.checkout.cart.giftcard.remove');
-    Route::post('/auto-apply-expiring', [GiftController::class, 'autoApplyExpiringGiftCard'])->name('shop.api.checkout.cart.giftcard.auto-apply');
+/*
+|--------------------------------------------------------------------------
+| 网站前台路由(传统 Web,session 登录)
+|--------------------------------------------------------------------------
+| 走 web + customer 中间件,未登录会跳转登录页;POST/DELETE 需带 CSRF token。
+*/
+Route::group(['middleware' => ['web', 'customer'], 'prefix' => 'gift'], function () {
+    Route::get('/lists', [GiftController::class, 'lists'])->name('shop.gift.lists');
+    Route::post('/add', [GiftController::class, 'add'])->name('shop.gift.add');
+    Route::get('/my-gift-cards', [GiftController::class, 'getCustomerGiftCards'])->name('shop.gift.my-gift-cards');
+    Route::post('/activate', [GiftController::class, 'activateGiftCard'])->name('shop.gift.activate');
+    Route::delete('/remove', [GiftController::class, 'destroyGiftCard'])->name('shop.gift.remove');
+    Route::post('/auto-apply-expiring', [GiftController::class, 'autoApplyExpiringGiftCard'])->name('shop.gift.auto-apply-expiring');
+});
+
+/*
+|--------------------------------------------------------------------------
+| API 路由(前后端分离 / 第三方客户端,token 或 storefront key)
+|--------------------------------------------------------------------------
+| 走 api + ApiCustomerAuthenticate(支持 session cookie / Sanctum token / storefront key)。
+*/
+Route::group(['prefix' => 'api'], function () {
+    Route::group(['middleware' => ['api', ApiCustomerAuthenticate::class], 'prefix' => 'gift'], function () {
+        Route::get('/lists', [GiftController::class, 'lists'])->name('shop.api.gift.lists');
+        Route::post('/add', [GiftController::class, 'add'])->name('shop.api.gift.add');
+        Route::get('/my-gift-cards', [GiftController::class, 'getCustomerGiftCards'])->name('shop.api.checkout.cart.giftcard.list');
+        Route::post('/activate', [GiftController::class, 'activateGiftCard'])->name('shop.api.checkout.cart.giftcard.activate');
+        Route::delete('/remove', [GiftController::class, 'destroyGiftCard'])->name('shop.api.checkout.cart.giftcard.remove');
+        Route::post('/auto-apply-expiring', [GiftController::class, 'autoApplyExpiringGiftCard'])->name('shop.api.checkout.cart.giftcard.auto-apply');
+    });
 });
 });

+ 9 - 2
packages/Webkul/BagistoApi/src/Dto/CartData.php

@@ -419,8 +419,15 @@ class CartData
         $data->giftcardAmount = (float) ($cart->giftcard_amount ?? 0);
         $data->giftcardAmount = (float) ($cart->giftcard_amount ?? 0);
         $data->baseGiftcardAmount = (float) ($cart->base_giftcard_amount ?? 0);
         $data->baseGiftcardAmount = (float) ($cart->base_giftcard_amount ?? 0);
         $data->formattedGiftcardAmount = core()->currency($cart->base_giftcard_amount ?? 0);
         $data->formattedGiftcardAmount = core()->currency($cart->base_giftcard_amount ?? 0);
-        $data->remainingGiftcardAmount = (float) ($cart->remaining_giftcard_amount ?? 0);
-        $data->formattedRemainingGiftcardAmount = core()->formatPrice($cart->remaining_giftcard_amount ?? 0);
+
+        // 剩余余额在 gift_cards 表,cart 上没有该字段,按卡号现查(否则前端恒拿到 0)
+        $data->remainingGiftcardAmount = 0.0;
+        $data->formattedRemainingGiftcardAmount = core()->currency(0);
+        if ($data->giftcardNumber) {
+            $baseRemaining = (float) (\Longyi\Gift\Models\GiftCards::where('giftcard_number', $data->giftcardNumber)->value('remaining_giftcard_amount') ?? 0);
+            $data->remainingGiftcardAmount = (float) core()->convertPrice($baseRemaining);
+            $data->formattedRemainingGiftcardAmount = core()->currency($baseRemaining);
+        }
 
 
         // VIP member discount
         // VIP member discount
         $data->vipPlusAmount = (float) ($cart->vip_plus_amount ?? 0);
         $data->vipPlusAmount = (float) ($cart->vip_plus_amount ?? 0);