diff --git a/app/Exports/ReportExport.php b/app/Exports/ReportExport.php index 07bed6a..50f417f 100644 --- a/app/Exports/ReportExport.php +++ b/app/Exports/ReportExport.php @@ -4,6 +4,7 @@ namespace App\Exports; use App\Models\IamPrincipal; use App\Models\RedeemRestaurant; +use App\Models\Subscriptions; use Illuminate\Contracts\View\View; use Maatwebsite\Excel\Concerns\FromView; @@ -28,7 +29,25 @@ class ReportExport implements FromView { $data = collect(); - if ($this->reportType === 'Total Users') { + if ($this->reportType === 'Total Subscribed') { + $query = Subscriptions::with(['iamPrincipal.state']); + + if (!empty($this->states)) { + $query->whereHas('iamPrincipal', function ($q) { + $q->whereIn('state_xid', $this->states); + }); + } + + if ($this->startDate) { + $query->whereDate('created_at', '>=', $this->startDate); + } + + if ($this->endDate) { + $query->whereDate('created_at', '<=', $this->endDate); + } + + $data = $query->get(); + } elseif ($this->reportType === 'Total Users') { $query = IamPrincipal::query(); if (!empty($this->states)) { diff --git a/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php b/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php index c91b757..687aaae 100644 --- a/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php +++ b/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php @@ -94,7 +94,7 @@ class SubscriptionController extends Controller public function createStripeProduct(Request $request) { - + try { DB::beginTransaction(); @@ -257,98 +257,85 @@ class SubscriptionController extends Controller public function cancelSubscription(Request $request) { try { - Log::info("Razorpay Cancel subscriptions Begin: "); DB::beginTransaction(); + $stripeSecret = (config('constants.subscription.stripe_secret_key')); + // $stripeSecret = env('STRIPE_SECRET'); - $validator = $this->validateCancelSubscriptionForm($request); + $stripe = new \Stripe\StripeClient($stripeSecret); + $userId = $request->iam_principal_xid; - if ($validator->fails()) { - $validationErrors = $validator->errors()->all(); - Log::error("Razorpay Cancel subscriptions validation error: " . implode(", ", $validationErrors)); - return jsonResponseWithErrorMessageApi($validationErrors, 203); - } + // dd($request->all(),$stripeSecret); - $iamPrincipalId = $request->iam_principal_xid; - $razorpaySubscriptionId = $request->subscription_id; - $subscriptionXid = $request->subscription_xid; + $getSubscriptionData = Subscriptions::where('iam_principal_xid', $userId)->where('subscription_status', 'active')->first(); - $getSubscriptionData = RazorpaySubscriptions::where(['id' => $subscriptionXid, 'iam_principal_xid' => $iamPrincipalId, 'subscription_id' => $razorpaySubscriptionId, 'isCancelledSubscription' => 0])->first(); + $subscriptionId = $getSubscriptionData->subscription_id; + + $cancelledSubscription = $stripe->subscriptions->update( + $subscriptionId, + ['cancel_at_period_end' => true] + ); - if (!$getSubscriptionData) { - return response()->json(['status' => 502, 'message' => 'Something went wrong while cancelling Subscription']); - } - $api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret')); - $options = ['cancel_at_cycle_end' => 1]; - // Call the subscription create method with request parameters - $response = $api->subscription->fetch($razorpaySubscriptionId)->cancel($options); + $subscriptionFromDatabase = Subscriptions::where('subscription_id', $subscriptionId)->first(); + $subscriptionFromDatabase->cancelled_at = date('Y-m-d H:i:s', $cancelledSubscription->canceled_at); + + $subscriptionFromDatabase->subscription_status = $cancelledSubscription->status; + $subscriptionFromDatabase->is_cancelled_subscription = 1; //2 for cancelled + $subscriptionFromDatabase->status = "cancelled"; + + $subscriptionFromDatabase->save(); + + + $getSubscription = $stripe->subscriptions->retrieve($subscriptionFromDatabase->subscription_id, []); + + + $getSubscribeCustomer = $stripe->customers->retrieve( + $subscriptionFromDatabase->stripe_customer_id, + [] + ); - $endAt = date('Y-m-d H:i:s', $response->end_at); - $dateTime = now(); - $currentDateTime = $dateTime->format('Y-m-d H:i:s'); - $getSubscriptionData->isCancelledSubscription = 1; - $getSubscriptionData->cancelled_at = $currentDateTime; - $getSubscriptionData->save(); DB::commit(); - Log::info("Razorpay Cancel subscriptions end: "); + return redirect()->back()->with(['success' => "Your Subscription Cancelled Successfully!"]); - return response()->json(['status' => 200, 'end_date' => $endAt]); - // return response()->json(['status' => 200, 'message' => 'Your Subscription has been cancelled Sucessfully']); - - - } catch (Exception $e) { + } catch (\Exception $e) { DB::rollBack(); - Log::error("An error occurred in " . _METHOD_ . ": " . $e->getMessage(), ['exception' => $e]); - return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + + return redirect()->back()->with(['error' => "Something went wrong while cancelling subscription!" . $e->getMessage()]); + // Log::error("An error occurred in " . _METHOD_ . ": " . $e->getMessage(), ['exception' => $e]); + // return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); } } - public function validateCancelSubscriptionForm(Request $request) + + /** + * Created By : Hritik + * Created at : 05 Jult 2024 + * Use : To Apply Referral Code and return the User Id. + */ + + + + + public function applyReferralCode(Request $request) { - return Validator::make( - $request->all(), - [ - 'iam_principal_xid' => 'required', - 'subscription_id' => 'required', - 'subscription_xid' => 'required', - ] - ); + try { + + $referralCode = $request->input('referral_code'); + $currentUserId = $request->input('current_iam_principal_xid'); + $code = IamPrincipal::where('referral_code', $referralCode)->where('id', '!=', $currentUserId)->where('principal_type_xid', 3)->first(); + + if ($code) { + return response()->json(['success' => true, 'message' => 'Referral code applied successfully.', 'referralUserId' => $code->id]); + } else { + return response()->json(['success' => false, 'message' => 'Invalid referral code.']); + } + + } catch (\Exception $e) { + Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]); + return jsonResponseWithErrorMessage(__('auth.something_went_wrong')); + } } - - // public function subscriptionUpgrade(Request $request) - // { - // // dd($request->all()); - - // $razorPaysubscriptionProductXid = $request->razorpay_plan_id; - // $productTypeId = $request->product_type_xid; - // $productTierId = $request->product_tier_xid; - // $iamId = $request->iam_principal_xid; - - // $user_id = 12; // Change this to dynamic user ID - - // // Retrieve the current monthly subscription - // $currentSubscription = RazorpaySubscriptions::where('iam_principal_xid', $user_id)->first(); - - // if (!$currentSubscription) { - // return response()->json(['status' => 502, 'message' => 'Something went wrong current user Subscription not found']); - // } - - // $products = Products::where('product_type_xid', $productTypeId) - // ->where('product_tier_xid', $productTierId) - // ->where('razorpay_plan_id', $razorPaysubscriptionProductXid) - // ->where('is_active', '1')->first(); - - // if (!$products) { - // return response()->json(['status' => 502, 'message' => 'Something went wrong current product not found']); - // } - - // $api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret')); - // $plan_id = $products->razorpay_plan_id; - - // // Call the subscription create method with request parameters - - // } } diff --git a/app/Models/Subscriptions.php b/app/Models/Subscriptions.php index 349fa1e..2acea0e 100644 --- a/app/Models/Subscriptions.php +++ b/app/Models/Subscriptions.php @@ -18,6 +18,35 @@ class Subscriptions extends Model } + protected $fillable = [ + 'id', + 'subscription_product_xid', + 'iam_principal_xid', + 'amount', + 'stripe_customer_id', + 'payment_intent_id', + 'payment_intent_client_secret', + 'subscription_id', + 'subscription_status', + 'current_period_start', + 'current_period_end', + 'status', + 'next_payment_date', + 'is_cancelled_subscription', + 'cancelled_at', + 'is_active', + 'deleted_at', + 'created_by', + 'modified_by', + 'created_at', + 'updated_at' + ]; + + +public function iamPrincipal() +{ + return $this->belongsTo(IamPrincipal::class, 'iam_principal_xid', 'id'); +} } diff --git a/app/Services/APIs/CustomerAPIs/AuthServices.php b/app/Services/APIs/CustomerAPIs/AuthServices.php index d68626b..6cea0a9 100644 --- a/app/Services/APIs/CustomerAPIs/AuthServices.php +++ b/app/Services/APIs/CustomerAPIs/AuthServices.php @@ -56,9 +56,14 @@ class AuthServices do { $referral_code = \Str::random(10); } while (IamPrincipal::where('referral_code', $referral_code)->exists()); - + + if ($request->one_signal_player_id == "null") { + $playerId = null; + } else { + $playerId = $request->one_signal_player_id; + } $user = IamPrincipal::create([ - 'one_signal_player_id' => $request->one_signal_player_id, + 'one_signal_player_id' => $playerId, 'first_name' => $request->first_name, 'last_name' => $request->last_name, 'email_address' => $request->email_address, @@ -118,8 +123,13 @@ class AuthServices Log::error('Customer Login Failed'); return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403); } + if ($request->one_signal_player_id == "null") { + $playerId = null; + } else { + $playerId = $request->one_signal_player_id; + } - $isExistEmail->one_signal_player_id = $request->one_signal_player_id; + $isExistEmail->one_signal_player_id = $playerId ; $isExistEmail->save(); $response = [ 'iam_principal_xid' => $isExistEmail->id, diff --git a/resources/views/Admin/pages/manage_reports/manage_reports.blade.php b/resources/views/Admin/pages/manage_reports/manage_reports.blade.php index ddba1e3..373b538 100644 --- a/resources/views/Admin/pages/manage_reports/manage_reports.blade.php +++ b/resources/views/Admin/pages/manage_reports/manage_reports.blade.php @@ -34,7 +34,6 @@ $currentPage = 'manage-reports'; $points = [ 'Total Subscribed', 'Total Users', - 'New Subscribed', 'Redemptions', 'Redemptions for Specific Restaurants', 'Referrals Made', @@ -119,10 +118,16 @@ $currentPage = 'manage-reports'; radio.addEventListener('change', function() { document.querySelectorAll('.state-checkboxes').forEach(function(checkboxDiv) { checkboxDiv.classList.remove('show'); + checkboxDiv.querySelectorAll('.state-checkbox').forEach(function(checkbox) { + checkbox.disabled = true; + }); }); var selectedPoint = this.value; document.querySelectorAll('.state-checkboxes[data-point="' + selectedPoint + '"]').forEach(function(checkboxDiv) { checkboxDiv.classList.add('show'); + checkboxDiv.querySelectorAll('.state-checkbox').forEach(function(checkbox) { + checkbox.disabled = false; + }); }); }); }); @@ -147,7 +152,8 @@ $currentPage = 'manage-reports';