File: /var/www/javago-portal-updates/app/Http/Controllers/API/CafeLoyaltyController.php
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\CafeLoyaltyStampExcludedItem;
use Illuminate\Http\Request;
use App\Models\Cafe;
use App\Models\CafeManageLoyaltyStamp;
use App\Models\CafeMenuItem;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class CafeLoyaltyController extends Controller
{
//get Stamp
public function getstamp(Request $request)
{
try {
$cafe = Cafe::find($request->user->id);
$loyaltyStamp = CafeManageLoyaltyStamp::where('cafe_id', $cafe->id)->first();
if ($loyaltyStamp) {
$excludedItems = CafeLoyaltyStampExcludedItem::where('stamp_id', $loyaltyStamp->id)->get();
$loyaltyStamp->excludedItems = $excludedItems->isEmpty() ? null : $excludedItems;
}
// Return the response with data
return response()->json([
'status' => 'success',
'message' => 'Stamp Detail found Successfully!',
'loyaltyStamp' => $loyaltyStamp,
], 200);
} catch (ValidationException $validationException) {
// Handle validation exception and return custom response
return response()->json([
'status' => 'error',
'message' => 'Validation failed.',
'errors' => $validationException->errors(),
], 422);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json([
'status' => 'error',
'message' => 'An error occurred while getting Loyalty Stamp!',
'error' => $e->getMessage(),
], 500);
}
}
//get stamp suggestions
public function getStampSuggestions(Request $request)
{
try {
$cafe = Cafe::find($request->user->id);
$data['colors'] = [];
foreach (config('constants.stamp_color') as $key => $value) {
$data['colors'][$key] = asset($value);
}
$data['discount_types'] = config('constants.discount_type');
$excludeItems = CafeMenuItem::where('cafe_id', $cafe->id)->pluck('item_name', 'id');
$data['stampApplicableTo'] = CafeMenuItem::join('cafe_menus', 'cafe_menu_items.cafe_menu_id', '=', 'cafe_menus.id')
->where('cafe_menu_items.cafe_id', $cafe->id)
->select('cafe_menu_items.cafe_menu_id', 'cafe_menus.menu_name')
->distinct()
->orderBy('cafe_menu_items.cafe_menu_id', 'desc')
->get();
$data['stampApplicableTo'] = $data['stampApplicableTo']->prepend(
(object) [
'cafe_menu_id' => 'all',
'menu_name' => 'All'
]
);
$data['excludeItems'] = $excludeItems->isEmpty() ? null : $excludeItems;
return response()->json([
'status' => 'success',
'message' => 'Stamp Suggestions found Successfully!',
'data' => $data,
], 200);
} catch (ValidationException $validationException) {
// Handle validation exception and return custom response
return response()->json([
'status' => 'error',
'message' => 'Validation failed.',
'errors' => $validationException->errors(),
], 422);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json([
'status' => 'error',
'message' => 'An error occurred while getting Loyalty Stamp Suggestions!',
'error' => $e->getMessage(),
], 500);
}
}
//add stamp
public function addLoyaltyStamp(Request $request)
{
try {
// Validate the incoming request
$validator = Validator::make($request->all(), [
'stamp_color' => 'required',
'stamp_no' => 'required',
'discount_type' => 'required',
'discount' => 'required',
'min_order_value' => 'required',
'offer_text' => 'required',
'stamp_applicable_to_categories' => 'required',
'exclude_item' => 'nullable|array',
]);
// Handle validation errors
if ($validator->fails()) {
// Throw a ValidationException if validation fails
throw new ValidationException($validator);
}
DB::beginTransaction();
$cafe = Cafe::find($request->user->id);
$existingStamp = CafeManageLoyaltyStamp::where('cafe_id', $cafe->id)->first();
if ($existingStamp) {
return response()->json([
'status' => 'Error',
'message' => 'Stamp Already Added!',
], 403);
}
$create = [];
$create['stamp_color'] = $request->stamp_color;
$create['stamp_no'] = $request->stamp_no;
$create['discount_type'] = $request->discount_type;
$create['discount'] = $request->discount;
$create['cafe_id'] = $cafe->id;
$create['min_order_value'] = $request->min_order_value;
$create['offer_text'] = $request->offer_text;
$create['stamp_expires_in'] = $request->stamp_expires_in ?? null;
$create['stamp_applicable_to_categories'] = json_encode($request->stamp_applicable_to_categories) ?? null;
$create['created_at'] = Carbon::now()->timestamp;
$create['updated_at'] = Carbon::now()->timestamp;
$stampId = CafeManageLoyaltyStamp::insertGetId($create);
$excludeItem = $request->exclude_item;
$exclude = [];
if (!empty($excludeItem)) {
foreach ($excludeItem as $key => $value) {
array_push($exclude, ['stamp_id' => $stampId, 'item_id' => $value, 'created_at' => Carbon::now()->timestamp, 'updated_at' => Carbon::now()->timestamp]);
}
CafeLoyaltyStampExcludedItem::insert($exclude);
}
$cafe->update(['loyalty_completed' => 1]);
DB::commit();
return response()->json([
'status' => 'success',
'message' => 'Stamp Added Successfully!',
], 200);
} catch (ValidationException $validationException) {
// Handle validation exception and return custom response
return response()->json([
'status' => 'error',
'message' => 'Validation failed.',
'errors' => $validationException->errors(),
], 422);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json([
'status' => 'error',
'message' => 'An error occurred while adding Loyalty Stamp!',
'error' => $e->getMessage(),
], 500);
}
}
//update stamp
public function updateLoyaltyStamp(Request $request, $stampId)
{
try {
// Validate the incoming request
$validator = Validator::make($request->all(), [
'stamp_color' => 'required',
'stamp_no' => 'required',
'discount_type' => 'required',
'discount' => 'required',
'min_order_value' => 'required',
'offer_text' => 'required',
'stamp_applicable_to_categories' => 'required',
'exclude_item' => 'nullable|array',
]);
// Handle validation errors
if ($validator->fails()) {
// Throw a ValidationException if validation fails
throw new ValidationException($validator);
}
DB::beginTransaction();
// Find the cafe
$cafe = Cafe::find($request->user->id);
// Find the existing loyalty stamp by stampId
$existingStamp = CafeManageLoyaltyStamp::where('id', $stampId)->where('cafe_id', $cafe->id)->first();
if (!$existingStamp) {
return response()->json([
'status' => 'error',
'message' => 'Loyalty Stamp not found!',
], 404);
}
// Update loyalty stamp details
$update = [];
$update['stamp_color'] = $request->stamp_color;
$update['stamp_no'] = $request->stamp_no;
$update['discount_type'] = $request->discount_type;
$update['discount'] = $request->discount;
$update['min_order_value'] = $request->min_order_value;
$update['offer_text'] = $request->offer_text;
$update['stamp_expires_in'] = $request->stamp_expires_in ?? null;
$update['stamp_applicable_to_categories'] = json_encode($request->stamp_applicable_to_categories) ?? null;
$update['updated_at'] = Carbon::now()->timestamp;
// Update the stamp
$existingStamp->update($update);
// Update excluded items if provided
$excludeItem = $request->exclude_item;
if (!empty($excludeItem)) {
// Delete old excluded items for this stamp
CafeLoyaltyStampExcludedItem::where('stamp_id', $stampId)->delete();
// Add new excluded items
$exclude = [];
foreach ($excludeItem as $key => $value) {
array_push($exclude, [
'stamp_id' => $stampId,
'item_id' => $value,
'created_at' => Carbon::now()->timestamp,
'updated_at' => Carbon::now()->timestamp,
]);
}
CafeLoyaltyStampExcludedItem::insert($exclude);
}
DB::commit();
return response()->json([
'status' => 'success',
'message' => 'Loyalty Stamp Updated Successfully!',
], 200);
} catch (ValidationException $validationException) {
// Handle validation exception and return custom response
return response()->json([
'status' => 'error',
'message' => 'Validation failed.',
'errors' => $validationException->errors(),
], 422);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json([
'status' => 'error',
'message' => 'An error occurred while updating the Loyalty Stamp!',
'error' => $e->getMessage(),
], 500);
}
}
//Delete Stamp
public function deleteLoyaltyStamp(Request $request, $stampId)
{
try {
// Find the cafe
$cafe = Cafe::find($request->user->id);
// Find the existing loyalty stamp by stampId
$existingStamp = CafeManageLoyaltyStamp::where('id', $stampId)->where('cafe_id', $cafe->id)->first();
if (!$existingStamp) {
return response()->json([
'status' => 'error',
'message' => 'Loyalty Stamp not found!',
], 404);
}
$existingStamp->delete();
return response()->json([
'status' => 'success',
'message' => 'Loyalty Stamp Deleted Successfully!',
], 200);
} catch (ValidationException $validationException) {
// Handle validation exception and return custom response
return response()->json([
'status' => 'error',
'message' => 'Validation failed.',
'errors' => $validationException->errors(),
], 422);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json([
'status' => 'error',
'message' => 'An error occurred while deleting the Loyalty Stamp!',
'error' => $e->getMessage(),
], 500);
}
}
}