File: /var/www/admin.javaapp.co.uk/app/Services/CafeManageLoyaltyStampService.php
<?php
namespace App\Services;
use App\Helpers\AppPushNotification;
use App\Models\Cafe;
use App\Models\CafeLoyaltyStampExcludedItem;
use App\Models\CafeMenuItem;
use App\Models\Notification;
use App\Models\Order;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Session;
class CafeManageLoyaltyStampService
{
private $model;
private $cafeMenuItems;
private $cafeLoyaltyStampExcludedItem;
private $order;
private $notification;
public function __construct($model)
{
$this->model = new $model();
$this->cafeMenuItems = new CafeMenuItem();
$this->cafeLoyaltyStampExcludedItem = new CafeLoyaltyStampExcludedItem();
$this->order = new Order();
$this->notification = new Notification();
}
public function create()
{
Session::put('previous_url', url()->previous());
$model = $this->model->where('cafe_id', Auth::user()->id)->first();
if (empty($model)) {
$data = [];
$data['addPage'] = true;
$data['title'] = __('common.addStamp');
$data['url'] = route('cafe.manage-loyalty-stamp.store');
$data['colors'] = [];
foreach (config('constants.stamp_color') as $key => $value) {
$data['colors'][$key] = asset($value);
}
$data['discount_types'] = config('constants.discount_type');
$data['excludeItems'] = $this->cafeMenuItems->where('cafe_id', Auth::user()->id)->pluck('item_name', 'id');
// $data['excludeItems'] = $this->cafeMenuItems
// ->join('cafe_menus', 'cafe_menu_items.cafe_menu_id', '=', 'cafe_menus.id')
// ->where('cafe_menu_items.cafe_id', Auth::user()->id)
// ->select('cafe_menu_items.id', 'cafe_menu_items.item_name', 'cafe_menu_items.cafe_menu_id', 'cafe_menus.menu_name')
// ->orderBy('cafe_menu_items.id', 'desc')
// ->get();
$data['stampApplicableTo'] = $this->cafeMenuItems
->join('cafe_menus', 'cafe_menu_items.cafe_menu_id', '=', 'cafe_menus.id')
->where('cafe_menu_items.cafe_id', Auth::user()->id)
->select('cafe_menu_items.cafe_menu_id', 'cafe_menus.menu_name')
->distinct()
->orderBy('cafe_menu_items.cafe_menu_id', 'desc')
->get();
$data['cancelUrl'] = route('cafe.manage-loyalty-stamp.index');
if (strpos(session('previous_url'), 'set-cafe-profile') !== false) {
$data['cancelUrl'] = route('cafe.set_cafe_profile');
}
return view('cafe.manage-loyalty-stamp.stamps', $data);
}
session()->flash('error_message', __('common.moreThanOneLoyaltyStamp'));
return redirect()->back();
}
public function store($request)
{
DB::beginTransaction();
try {
$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'] = Auth::user()->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) ?? null;
$create['created_at'] = Carbon::now()->timestamp;
$create['updated_at'] = Carbon::now()->timestamp;
$stampId = $this->model->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]);
}
$this->cafeLoyaltyStampExcludedItem->insert($exclude);
}
$cafe = Auth::user();
$cafe->update(['loyalty_completed' => 1]);
// $notifications = [];
// $notificationData = [
// 'fcm_token' => [],
// 'cafe_id' => Auth::user()->id,
// 'group_id' => '',
// ];
// $orders = $this->order->with('user')->where('cafe_id', Auth::user()->id)->groupBy('user_id')->get();
// if ($orders->isNotEmpty()) {
// foreach ($orders as $order) {
// array_push($notifications, [
// 'sender_id' => Auth::user()->id,
// 'receiver_id' => $order->user_id,
// 'reference_id' => Auth::user()->id,
// 'notification_type' => config('constants.notification_type.manageLoyaltyStamp'),
// 'created_at' => Carbon::now()->timestamp,
// 'updated_at' => Carbon::now()->timestamp,
// ]);
// if ($order->user->fcm_token != '') {
// array_push($notificationData['fcm_token'], $order->user->fcm_token);
// }
// }
// $this->notification->insert($notifications);
// AppPushNotification::sendNotification($notificationData, config('constants.notification_type.manageLoyaltyStamp'), __('common.notification.manageLoyaltyStamp.title'), __('common.notification.manageLoyaltyStamp.body', ['cafe_name' => Auth::user()->cafe_name]));
// }
DB::commit();
session()->flash('success_message', __('common.addStampSuccessfully'));
if (strpos(session('previous_url'), 'set-cafe-profile') !== false) {
Session::forget('previous_url');
return redirect('/cafe/set-cafe-profile');
}
return redirect()->route('cafe.manage-loyalty-stamp.index');
} catch (\Throwable $th) {
dd($th);
session()->flash('error_message', __('common.somethingWentWrong'));
return redirect()->back();
}
}
public function edit($id)
{
abort_if(!$id, 404);
$model = $this->model->where('id', decrypt($id))->first();
abort_if(!$model, 404);
Session::put('previous_url', url()->previous());
$data = [];
$data['addPage'] = false;
$data['title'] = __('common.editStamp');
$data['url'] = route('cafe.manage-loyalty-stamp.update', $id);
$data['colors'] = [];
foreach (config('constants.stamp_color') as $key => $value) {
$data['colors'][$key] = asset($value);
}
$data['discount_types'] = config('constants.discount_type');
$data['excludeItems'] = $this->cafeMenuItems->where('cafe_id', Auth::user()->id)->pluck('item_name', 'id');
// $data['excludeItems'] = $this->cafeMenuItems
// ->join('cafe_menus', 'cafe_menu_items.cafe_menu_id', '=', 'cafe_menus.id')
// ->where('cafe_menu_items.cafe_id', Auth::user()->id)
// ->select('cafe_menu_items.id', 'cafe_menu_items.item_name', 'cafe_menu_items.cafe_menu_id', 'cafe_menus.menu_name')
// ->orderBy('cafe_menu_items.id', 'desc')
// ->get();
$data['exclude_items'] = $this->cafeLoyaltyStampExcludedItem->where('stamp_id', $model->id)->pluck('item_id')->toArray();
// $data['exclude_items'] = $this->cafeLoyaltyStampExcludedItem
// ->where('stamp_id', $model->id)
// ->pluck('cafe_menu_id')
// ->toArray();
$data['cancelUrl'] = route('cafe.manage-loyalty-stamp.index');
$data['stamp_color'] = $model->stamp_color;
$data['stamp_no'] = $model->stamp_no;
$data['offer_text'] = $model->offer_text;
$data['stamp_expires_in'] = $model->stamp_expires_in;
// $data['exclude_item'] = $model->exclude_item;
$data['discount_type'] = $model->discount_type;
$data['discount'] = $model->discount;
$data['min_order_value'] = $model->min_order_value;
$data['stampApplicableTo'] = $this->cafeMenuItems
->join('cafe_menus', 'cafe_menu_items.cafe_menu_id', '=', 'cafe_menus.id')
->where('cafe_menu_items.cafe_id', Auth::user()->id)
->select('cafe_menu_items.cafe_menu_id', 'cafe_menus.menu_name')
->distinct()
->orderBy('cafe_menu_items.cafe_menu_id', 'desc')
->get();
$data['stamp_applicable_to'] = json_decode($model->stamp_applicable_to_categories);
return view('cafe.manage-loyalty-stamp.stamps', $data);
}
public function update($request, $id)
{
abort_if(!$id, 404);
DB::beginTransaction();
try {
$model = $this->model->find(decrypt($id));
if (!$model) {
DB::rollback();
abort(404);
}
$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;
$update['updated_at'] = Carbon::now()->timestamp;
$update['stamp_applicable_to_categories'] = json_encode($request->stamp_applicable_to) ?? null;
$model->update($update);
$excludeItem = $request->exclude_item;
$exclude = [];
if (!empty($excludeItem)) {
$this->cafeLoyaltyStampExcludedItem->where('stamp_id', decrypt($id))->delete();
foreach ($excludeItem as $key => $value) {
array_push($exclude, ['stamp_id' => decrypt($id), 'item_id' => $value, 'created_at' => Carbon::now()->timestamp, 'updated_at' => Carbon::now()->timestamp]);
}
$this->cafeLoyaltyStampExcludedItem->insert($exclude);
}
// $notifications = [];
// $notificationData = [
// 'fcm_token' => [],
// 'cafe_id' => Auth::user()->id,
// 'group_id' => '',
// ];
// $orders = $this->order->with('user')->where('cafe_id', Auth::user()->id)->groupBy('user_id')->get();
// if ($orders->isNotEmpty()) {
// foreach ($orders as $order) {
// array_push($notifications, [
// 'sender_id' => $order->cafe_id,
// 'receiver_id' => $order->user_id,
// 'reference_id' => $order->cafe_id,
// 'notification_type' => config('constants.notification_type.manageLoyaltyStamp'),
// 'created_at' => Carbon::now()->timestamp,
// 'updated_at' => Carbon::now()->timestamp,
// ]);
// if ($order->user->fcm_token != '') {
// array_push($notificationData['fcm_token'], $order->user->fcm_token);
// }
// }
// $this->notification->insert($notifications);
// AppPushNotification::sendNotification($notificationData, config('constants.notification_type.manageLoyaltyStamp'), __('common.notification.manageLoyaltyStamp.title'), __('common.notification.manageLoyaltyStamp.body', ['cafe_name' => Auth::user()->cafe_name]));
// }
// $orders = $this->order->with('user')->where('cafe_id', Auth::user()->id)->groupBy('user_id')->get();
// foreach ($orders as $order) {
// $notification = [
// 'sender_id' => $order->cafe_id,
// 'receiver_id' => $order->user_id,
// 'reference_id' => $order->cafe_id,
// 'notification_type' => config('constants.notification_type.manageLoyaltyStamp'),
// 'created_at' => Carbon::now()->timestamp,
// 'updated_at' => Carbon::now()->timestamp,
// ];
// $this->notification->insert($notification);
// $notificationData = [
// 'fcm_token' => $order->user->fcm_token,
// 'cafe_id' => $order->cafe_id,
// 'group_id' => '',
// 'admin' => false
// ];
// AppPushNotification::sendNotification($notificationData, config('constants.notification_type.manageLoyaltyStamp'), '', '');
// }
DB::commit();
session()->flash('success_message', __('common.updateStampSuccessfully'));
if (strpos(session('previous_url'), 'set-cafe-profile') !== false) {
Session::forget('previous_url');
return redirect('/cafe/set-cafe-profile');
}
return redirect()->route('cafe.manage-loyalty-stamp.index');
} catch (\Throwable $th) {
session()->flash('error_message', __('common.somethingWentWrong'));
return redirect()->back();
}
}
public function destroy($id, $modelName)
{
DB::beginTransaction();
try {
$model = $this->model->find(decrypt($id));
if (!$model) {
DB::rollback();
return response()->json([
'status' => 500,
'message' => __('common.somethingWentWrong'),
]);
}
// $delete = [];
// $delete['deleted_at'] = Carbon::now()->timestamp;
// $cafe->update($delete);
$model->delete();
DB::commit();
return response()->json([
'status' => 200,
'message' => __('common.DeletedSuccessfully', ['model' => $modelName]),
]);
} catch (\Throwable $th) {
DB::rollback();
return response()->json([
'status' => 500,
'message' => __('common.somethingWentWrong'),
]);
}
}
public function data()
{
$data = $this->model->query()->where('cafe_id', Auth::user()->id)->select('id', 'stamp_color', 'discount', 'discount_type', 'min_order_value', 'stamp_no');
return DataTables::eloquent($data)
->addIndexColumn()
->editColumn('stamp_color', function ($row) {
return view(
"partials.common",
[
"isStamp" => 1,
"stamp_color" => config("constants.stamp_color." . $row->stamp_color)
]
)->render();
})
->editColumn('discount', function ($row) {
if (empty($row->discount)) {
return '-';
} else {
if ($row->discount_type == 0) {
return ' ' . $row->discount;
} else {
return $row->discount . ' %';
}
}
})
->editColumn('min_order_value', function ($row) {
if (empty($row->min_order_value)) {
return '-';
} else {
return ' ' . $row->min_order_value;
}
})
->editColumn('stamp_no', function ($row) {
if (empty($row->stamp_no)) {
return '-';
} else {
return $row->stamp_no;
}
})
->addColumn('action', function ($row) {
return view(
"partials.action",
[
'currentRoute' => 'cafe.manage-loyalty-stamp',
'row' => $row,
'isDelete' => 0,
'isView' => 0,
'isEdit' => 1,
]
)->render();
})
->rawColumns(['stamp_color', 'discount', 'min_order_value', 'action'])
->toJson();
}
}