235 lines
6.7 KiB
PHP
235 lines
6.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\DB\Te\TableEngine;
|
||
|
|
use App\Models\Customer;
|
||
|
|
use App\Models\Message;
|
||
|
|
use App\Models\Order;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class MassageController extends Controller
|
||
|
|
{
|
||
|
|
public function send_message(Request $request)
|
||
|
|
{
|
||
|
|
// یافتن مشتریان با شمارههای داده شده
|
||
|
|
$customers = Customer::whereIn('mobile', $request->numbers)->get();
|
||
|
|
|
||
|
|
foreach ($customers as $customer) {
|
||
|
|
// جایگزین کردن نام در پیام
|
||
|
|
$message = str_replace('%name%', $customer->name, $request->message);
|
||
|
|
|
||
|
|
// ارسال پیامک
|
||
|
|
$this->send($customer->mobile, $message);
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json(true);
|
||
|
|
}
|
||
|
|
public function getFunctionDescriptions()
|
||
|
|
{
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
[
|
||
|
|
'function' => 'getAllCustomers',
|
||
|
|
'name' => 'همه کاربران',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'function' => 'getNoLoginCustomers',
|
||
|
|
'name' => 'کاربران وارد نشده',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'function' => 'getNoValidCustomers',
|
||
|
|
'name' => 'کاربران احراز نشده',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'function' => 'getActiveCustomers',
|
||
|
|
'name' => 'کاربران فعال',
|
||
|
|
],
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getCustomerList(Request $request)
|
||
|
|
{
|
||
|
|
$customers = [];
|
||
|
|
foreach ($request->functions as $action) {
|
||
|
|
if ($action === 'getAllCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getAllCustomers());
|
||
|
|
}
|
||
|
|
if ($action === 'getNoLoginCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getNoLoginCustomers());
|
||
|
|
}
|
||
|
|
if ($action === 'getNoValidCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getNoValidCustomers());
|
||
|
|
}
|
||
|
|
if ($action === 'getActiveCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getActiveCustomers());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// حذف شمارههای تکراری
|
||
|
|
$uniqueCustomers = $this->removeDuplicateNumbers($customers);
|
||
|
|
|
||
|
|
return response()->json(['customers' => $uniqueCustomers]);
|
||
|
|
}
|
||
|
|
public function getCustomerListTable(Request $request)
|
||
|
|
{
|
||
|
|
$customers = [];
|
||
|
|
foreach ($request->functions as $action) {
|
||
|
|
if ($action === 'getAllCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getAllCustomers());
|
||
|
|
}
|
||
|
|
if ($action === 'getNoLoginCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getNoLoginCustomers());
|
||
|
|
}
|
||
|
|
if ($action === 'getNoValidCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getNoValidCustomers());
|
||
|
|
}
|
||
|
|
if ($action === 'getActiveCustomers') {
|
||
|
|
$customers = array_merge($customers, $this->getActiveCustomers());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// Remove duplicate numbers
|
||
|
|
$uniqueCustomers = $this->removeDuplicateNumbers($customers);
|
||
|
|
$uniqueCustomersCollection = collect($uniqueCustomers);
|
||
|
|
|
||
|
|
$records = Customer::whereIn('id', $uniqueCustomersCollection->pluck('id'));
|
||
|
|
// Initialize TableEngine
|
||
|
|
TableEngine::create($records, $request->all())
|
||
|
|
->initSearch(['customer.name'])
|
||
|
|
->select([
|
||
|
|
'id','mobile', 'name'
|
||
|
|
])
|
||
|
|
->export($records);
|
||
|
|
|
||
|
|
return response()->json($records);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
private function getAllCustomers()
|
||
|
|
{
|
||
|
|
return Customer::where('type', 2)
|
||
|
|
->where('is_archive', false)
|
||
|
|
->get(['id', 'name', 'mobile'])
|
||
|
|
->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function getNoLoginCustomers()
|
||
|
|
{
|
||
|
|
return Customer::where('type', 2)
|
||
|
|
->where('is_archive', false)
|
||
|
|
->whereNull('last_seen_at')
|
||
|
|
->get(['id', 'name', 'mobile'])
|
||
|
|
->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function getNoValidCustomers()
|
||
|
|
{
|
||
|
|
return Customer::where('type', 2)
|
||
|
|
->where('is_archive', false)
|
||
|
|
->where('is_legal', 0)
|
||
|
|
->get(['id', 'name', 'mobile'])
|
||
|
|
->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function getActiveCustomers()
|
||
|
|
{
|
||
|
|
return Customer::where('type', 2)
|
||
|
|
->where('is_archive', false)
|
||
|
|
->whereIn('id', function ($query) {
|
||
|
|
$query->select('owner')
|
||
|
|
->from('wallets');
|
||
|
|
})
|
||
|
|
->get(['id', 'name', 'mobile'])
|
||
|
|
->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function removeDuplicateNumbers(array $customers)
|
||
|
|
{
|
||
|
|
$uniqueCustomers = [];
|
||
|
|
$seenNumbers = [];
|
||
|
|
|
||
|
|
foreach ($customers as $customer) {
|
||
|
|
if (!in_array($customer['mobile'], $seenNumbers)) {
|
||
|
|
$seenNumbers[] = $customer['mobile'];
|
||
|
|
$uniqueCustomers[] = $customer;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $uniqueCustomers;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function messagesTable(Request $request)
|
||
|
|
{
|
||
|
|
$records = Message::with(['person']);
|
||
|
|
$table = TableEngine::create($records, $request->all())
|
||
|
|
->initSearch([])
|
||
|
|
->select([
|
||
|
|
'id', 'sms', 'receiver','created_at',
|
||
|
|
]);
|
||
|
|
return response()->json($table->export());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display a listing of the messages.
|
||
|
|
*/
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
$messages = Message::all();
|
||
|
|
return response()->json($messages);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created message in storage.
|
||
|
|
*/
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
$request->validate([
|
||
|
|
'sms' => 'required|string',
|
||
|
|
'receiver' => 'required|string',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$message = Message::create($request->only(['sms', 'receiver']));
|
||
|
|
|
||
|
|
return response()->json($message, 201);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the specified message.
|
||
|
|
*/
|
||
|
|
public function show($id)
|
||
|
|
{
|
||
|
|
$message = Message::findOrFail($id);
|
||
|
|
return response()->json($message);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified message in storage.
|
||
|
|
*/
|
||
|
|
public function update(Request $request, $id)
|
||
|
|
{
|
||
|
|
$request->validate([
|
||
|
|
'sms' => 'sometimes|required|string',
|
||
|
|
'receiver' => 'sometimes|required|string',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$message = Message::findOrFail($id);
|
||
|
|
$message->update($request->only(['sms', 'receiver']));
|
||
|
|
|
||
|
|
return response()->json($message);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified message from storage.
|
||
|
|
*/
|
||
|
|
public function destroy($id)
|
||
|
|
{
|
||
|
|
$message = Message::findOrFail($id);
|
||
|
|
$message->delete();
|
||
|
|
|
||
|
|
return response()->json(null, 204);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|