HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/admin.javaapp.co.uk/app/Services/UniversalLoyaltyStampService.php
<?php

namespace App\Services;

use App\Helpers\AppPushNotification;
use App\Models\Cafe;
use App\Models\Notification;
use App\Models\Order;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;

class UniversalLoyaltyStampService
{
    private $model;
    private $order;
    private $notification;
    private $user;

    public function __construct($model)
    {
        $this->model = new $model();
        $this->order = new Order();
        $this->notification = new Notification();
        $this->user = new User();
    }

    public function create()
    {
        $model = $this->model->where('is_universal', 1)->first();
        if (empty($model)) {
            $data = [];
            $data['addPage'] = true;
            $data['title'] = __('common.addStamp');
            $data['url'] = route('admin.universal-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'] = Cafe::where('deleted_at', 0)->pluck('cafe_name', 'id');
            $data['cancelUrl'] = route('admin.universal-loyalty-stamp.index');
            return view('admin.universal-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['is_universal'] = 1;
            $create['offer_text'] = $request->offer_text;
            $create['stamp_expires_in'] = $request->stamp_expires_in;
            $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, ['cafe_id' => Auth::user()->id, 'stamp_id' => $stampId, 'item_id' => $value]);
                }
            }

            // $notifications = [];
            // $notificationData = [
            //     'fcm_token' => [],
            //     'cafe_id' => '',
            //     'group_id' => '',
            // ];
            // // $orders = $this->order->with('user')->groupBy('user_id')->get();
            // $users = $this->user->whereNotNull('fcm_token')->get();
            // // if ($orders->isNotEmpty()) {
            // if ($users->isNotEmpty()) {
            //     // foreach ($orders as $order) {
            //     foreach ($users as $user) {
            //         array_push($notifications, [
            //             'sender_id' => Auth::user()->id,
            //             // 'receiver_id' => $order->user_id,
            //             'receiver_id' => $user->id,
            //             'reference_id' => Auth::user()->id,
            //             'notification_type' => config('constants.notification_type.universalLoyaltyStamp'),
            //             'created_at' => Carbon::now()->timestamp,
            //             'updated_at' => Carbon::now()->timestamp,
            //         ]);
            //         // if ($order->user->fcm_token != '') {
            //         // array_push($notificationData['fcm_token'], $order->user->fcm_token);
            //         // }
            //         array_push($notificationData['fcm_token'], $user->fcm_token);
            //     }
            //     $this->notification->insert($notifications);
            //     AppPushNotification::sendNotification($notificationData, config('constants.notification_type.universalLoyaltyStamp'), __('common.notification.universalLoyaltyStamp.title'), __('common.notification.universalLoyaltyStamp.body'));
            // }

            DB::commit();
            session()->flash('success_message', __('common.addStampSuccessfully'));
            return redirect()->route('admin.universal-loyalty-stamp.index');
        } catch (\Throwable $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);
        $data = [];
        $data['addPage'] = false;
        $data['title'] = __('common.editStamp');
        $data['url'] = route('admin.universal-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'] = Cafe::where('deleted_at', 0)->pluck('cafe_name', 'id');
        $data['cancelUrl'] = route('admin.universal-loyalty-stamp.index');
        $data['stamp_color'] = $model->stamp_color;
        $data['stamp_no'] = $model->stamp_no;
        // $data['exclude_item'] = $model->exclude_item;
        $data['discount_type'] = $model->discount_type;
        $data['offer_text'] = $model->offer_text;
        $data['stamp_expires_in'] = $model->stamp_expires_in;
        $data['discount'] = $model->discount;
        $data['min_order_value'] = $model->min_order_value;
        return view('admin.universal-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;
            $model->update($update);
            $excludeItem = $request->exclude_item;
            $exclude = [];
            if (!empty($excludeItem)) {
                foreach ($excludeItem as $key => $value) {
                    array_push($exclude, ['cafe_id' => Auth::user()->id, 'stamp_id' => decrypt($id), 'item_id' => $value]);
                }
            }

            // $notifications = [];
            // $notificationData = [
            //     'fcm_token' => [],
            //     'cafe_id' => '',
            //     'group_id' => '',
            // ];
            // // $orders = $this->order->with('user')->groupBy('user_id')->get();
            // $users = $this->user->whereNotNull('fcm_token')->get();
            // // if ($orders->isNotEmpty()) {
            // if ($users->isNotEmpty()) {
            //     // foreach ($orders as $order) {
            //     foreach ($users as $user) {
            //         array_push($notifications, [
            //             'sender_id' => Auth::user()->id,
            //             // 'receiver_id' => $order->user_id,
            //             'receiver_id' => $user->id,
            //             'reference_id' => Auth::user()->id,
            //             'notification_type' => config('constants.notification_type.universalLoyaltyStamp'),
            //             'created_at' => Carbon::now()->timestamp,
            //             'updated_at' => Carbon::now()->timestamp,
            //         ]);
            //         // if ($order->user->fcm_token != '') {
            //         // array_push($notificationData['fcm_token'], $order->user->fcm_token);
            //         // }
            //         array_push($notificationData['fcm_token'], $user->fcm_token);
            //     }
            //     $this->notification->insert($notifications);
            //     AppPushNotification::sendNotification($notificationData, config('constants.notification_type.universalLoyaltyStamp'), __('common.notification.universalLoyaltyStamp.title'), __('common.notification.universalLoyaltyStamp.body'));
            // }

            // $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.universalLoyaltyStamp'),
            //         'created_at' => Carbon::now()->timestamp,
            //         'updated_at' => Carbon::now()->timestamp,
            //     ];
            //     $this->notification->insert($notification);
            //     $notificationData = [
            //         'fcm_token' => $order->user->fcm_token,
            //         'cafe_id' => '',
            //         'group_id' => '',
            //         'admin' => true
            //     ];
            //     AppPushNotification::sendNotification($notificationData, config('constants.notification_type.universalLoyaltyStamp'), '', '');
            // }

            DB::commit();
            session()->flash('success_message', __('common.updateStampSuccessfully'));
            return redirect()->route('admin.universal-loyalty-stamp.index');
        } catch (\Throwable $th) {
            session()->flash('error_message', __('common.somethingWentWrong'));
            return redirect()->back();
        }
    }

    public function data()
    {
        $data = $this->model->query()->where('is_universal', 1)->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' => 'admin.universal-loyalty-stamp',
                        'row' => $row,
                        'isDelete' => 0,
                        'isView' => 0,
                        'isEdit' => 1,
                    ]
                )->render();
            })
            ->rawColumns(['stamp_color', 'action'])
            ->toJson();
    }
}