You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					210 lines
				
				7.7 KiB
			
		
		
			
		
	
	
					210 lines
				
				7.7 KiB
			| 
								 
											2 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace App\Http\Controllers\Admin;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use App\Http\Controllers\Controller;
							 | 
						||
| 
								 | 
							
								use App\Models\Order;
							 | 
						||
| 
								 | 
							
								use App\Models\User;
							 | 
						||
| 
								 | 
							
								use Brian2694\Toastr\Facades\Toastr;
							 | 
						||
| 
								 | 
							
								use Illuminate\Http\Request;
							 | 
						||
| 
								 | 
							
								use Illuminate\Support\Facades\DB;
							 | 
						||
| 
								 | 
							
								use App\CentralLogics\Helpers;
							 | 
						||
| 
								 | 
							
								use App\Models\BusinessSetting;
							 | 
						||
| 
								 | 
							
								use App\Models\Newsletter;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class CustomerController extends Controller
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    public function customer_list(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $key = [];
							 | 
						||
| 
								 | 
							
								        if($request->search)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            $key = explode(' ', $request['search']);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $customers = User::
							 | 
						||
| 
								 | 
							
								        when(count($key) > 0, function($query)use($key){
							 | 
						||
| 
								 | 
							
								            foreach ($key as $value) {
							 | 
						||
| 
								 | 
							
								                $query->orWhere('f_name', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                    ->orWhere('l_name', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                    ->orWhere('email', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                    ->orWhere('phone', 'like', "%{$value}%");
							 | 
						||
| 
								 | 
							
								            };
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								        ->orderBy('order_count','desc')->paginate(config('default_pagination'));
							 | 
						||
| 
								 | 
							
								        return view('admin-views.customer.list', compact('customers'));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function status(User $customer, Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $customer->status = $request->status;
							 | 
						||
| 
								 | 
							
								        $customer->save();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        try
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if($request->status == 0)
							 | 
						||
| 
								 | 
							
								            {   $customer->tokens->each(function ($token, $key) {
							 | 
						||
| 
								 | 
							
								                    $token->delete();
							 | 
						||
| 
								 | 
							
								                });
							 | 
						||
| 
								 | 
							
								                if(isset($customer->cm_firebase_token))
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    $data = [
							 | 
						||
| 
								 | 
							
								                        'title' => translate('messages.suspended'),
							 | 
						||
| 
								 | 
							
								                        'description' => translate('messages.your_account_has_been_blocked'),
							 | 
						||
| 
								 | 
							
								                        'order_id' => '',
							 | 
						||
| 
								 | 
							
								                        'image' => '',
							 | 
						||
| 
								 | 
							
								                        'type'=> 'block'
							 | 
						||
| 
								 | 
							
								                    ];
							 | 
						||
| 
								 | 
							
								                    Helpers::send_push_notif_to_device($customer->cm_firebase_token, $data);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								                    DB::table('user_notifications')->insert([
							 | 
						||
| 
								 | 
							
								                        'data'=> json_encode($data),
							 | 
						||
| 
								 | 
							
								                        'user_id'=>$customer->id,
							 | 
						||
| 
								 | 
							
								                        'created_at'=>now(),
							 | 
						||
| 
								 | 
							
								                        'updated_at'=>now()
							 | 
						||
| 
								 | 
							
								                    ]);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        catch (\Exception $e) {
							 | 
						||
| 
								 | 
							
								            Toastr::warning(translate('messages.push_notification_faild'));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        Toastr::success(translate('messages.customer').translate('messages.status_updated'));
							 | 
						||
| 
								 | 
							
								        return back();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function search(Request $request){
							 | 
						||
| 
								 | 
							
								        $key = explode(' ', $request['search']);
							 | 
						||
| 
								 | 
							
								        $customers=User::where(function ($q) use ($key) {
							 | 
						||
| 
								 | 
							
								            foreach ($key as $value) {
							 | 
						||
| 
								 | 
							
								                $q->orWhere('f_name', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                    ->orWhere('l_name', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                    ->orWhere('email', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                    ->orWhere('phone', 'like', "%{$value}%");
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        })->orderBy('order_count','desc')->limit(50)->get();
							 | 
						||
| 
								 | 
							
								        return response()->json([
							 | 
						||
| 
								 | 
							
								            'view'=>view('admin-views.customer.partials._table',compact('customers'))->render()
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    public function order_search(Request $request){
							 | 
						||
| 
								 | 
							
								        $key = explode(' ', $request['search']);
							 | 
						||
| 
								 | 
							
								        $customer = User::find($request->id);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $orders=Order::where(['user_id' => $customer->id])->Notpos()->
							 | 
						||
| 
								 | 
							
								        where(function ($q) use ($key) {
							 | 
						||
| 
								 | 
							
								            foreach ($key as $value) {
							 | 
						||
| 
								 | 
							
								                $q->Where('id', 'like', "%{$value}%");
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								        ->paginate(config('default_pagination'));
							 | 
						||
| 
								 | 
							
								        // ->limit(50)->get();
							 | 
						||
| 
								 | 
							
								        $total=$orders->total();
							 | 
						||
| 
								 | 
							
								        return response()->json([
							 | 
						||
| 
								 | 
							
								            'view'=>view('admin-views.customer.partials._list_table',compact('customer', 'orders'))->render() ,'total' => $total
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function view($id)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $customer = User::find($id);
							 | 
						||
| 
								 | 
							
								        if (isset($customer)) {
							 | 
						||
| 
								 | 
							
								            $orders = Order::latest()->where(['user_id' => $id])->Notpos()->paginate(config('default_pagination'));
							 | 
						||
| 
								 | 
							
								            return view('admin-views.customer.customer-view', compact('customer', 'orders'));
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        Toastr::error(translate('messages.customer_not_found'));
							 | 
						||
| 
								 | 
							
								        return back();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function get_customers(Request $request){
							 | 
						||
| 
								 | 
							
								        $key = explode(' ', $request['q']);
							 | 
						||
| 
								 | 
							
								        $data = User::
							 | 
						||
| 
								 | 
							
								        where(function ($q) use ($key) {
							 | 
						||
| 
								 | 
							
								            foreach ($key as $value) {
							 | 
						||
| 
								 | 
							
								                $q->orWhere('f_name', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                ->orWhere('l_name', 'like', "%{$value}%")
							 | 
						||
| 
								 | 
							
								                ->orWhere('phone', 'like', "%{$value}%");
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								        ->limit(8)
							 | 
						||
| 
								 | 
							
								        ->get([DB::raw('id, CONCAT(f_name, " ", l_name, " (", phone ,")") as text')]);
							 | 
						||
| 
								 | 
							
								        if($request->all) $data[]=(object)['id'=>false, 'text'=>translate('messages.all')];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($data);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function settings()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $data = BusinessSetting::where('key','like','wallet_%')
							 | 
						||
| 
								 | 
							
								            ->orWhere('key','like','loyalty_%')
							 | 
						||
| 
								 | 
							
								            ->orWhere('key','like','ref_earning_%')
							 | 
						||
| 
								 | 
							
								            ->orWhere('key','like','ref_earning_%')->get();
							 | 
						||
| 
								 | 
							
								        $data = array_column($data->toArray(), 'value','key');
							 | 
						||
| 
								 | 
							
								        //dd($data);
							 | 
						||
| 
								 | 
							
								        return view('admin-views.customer.settings', compact('data'));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function update_settings(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (env('APP_MODE') == 'demo') {
							 | 
						||
| 
								 | 
							
								            Toastr::info(translate('messages.update_option_is_disable_for_demo'));
							 | 
						||
| 
								 | 
							
								            return back();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $request->validate([
							 | 
						||
| 
								 | 
							
								            'add_fund_bonus'=>'nullable|numeric|max:100|min:0',
							 | 
						||
| 
								 | 
							
								            'loyalty_point_exchange_rate'=>'nullable|numeric',
							 | 
						||
| 
								 | 
							
								            'ref_earning_exchange_rate'=>'nullable|numeric',
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'wallet_status'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['customer_wallet']??0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'loyalty_point_status'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['customer_loyalty_point']??0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'ref_earning_status'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['ref_earning_status'] ?? 0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'wallet_add_refund'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['refund_to_wallet']??0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'loyalty_point_exchange_rate'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['loyalty_point_exchange_rate'] ?? 0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'ref_earning_exchange_rate'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['ref_earning_exchange_rate'] ?? 0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'loyalty_point_item_purchase_point'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['item_purchase_point']??0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        BusinessSetting::updateOrInsert(['key' => 'loyalty_point_minimum_point'], [
							 | 
						||
| 
								 | 
							
								            'value' => $request['minimun_transfer_point']??0
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        Toastr::success(translate('messages.customer_settings_updated_successfully'));
							 | 
						||
| 
								 | 
							
								        return back();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function subscribedCustomers()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $subscribers = Newsletter::orderBy('id', 'desc')->paginate(config('default_pagination'));
							 | 
						||
| 
								 | 
							
								        return view('admin-views.customer.subscriber.list', compact('subscribers'));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    public function subscriberMailSearch(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $key = explode(' ', $request['search']);
							 | 
						||
| 
								 | 
							
								        $customers = Newsletter::where(function ($q) use ($key) {
							 | 
						||
| 
								 | 
							
								            foreach ($key as $value) {
							 | 
						||
| 
								 | 
							
								                $q->orWhere('email', 'like', "%". $value."%");
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        })->orderBy('id', 'desc')->get();
							 | 
						||
| 
								 | 
							
								        return response()->json([
							 | 
						||
| 
								 | 
							
								            'view' => view('admin-views.customer.partials._subscriber-email-table', compact('customers'))->render()
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |