Files
expense/app/Helpers/WhatsappHelper.php
T

256 lines
8.9 KiB
PHP
Raw Normal View History

2024-12-25 13:37:49 +07:00
<?php
namespace App\Helpers;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Str;
class WhatsappHelper
{
2025-01-16 15:56:22 +07:00
public static function approveExpense($phones, $expense_number, $url = null, $tanggal, $total, $name)
2024-12-25 13:37:49 +07:00
{
2025-01-04 14:09:28 +07:00
$client = new Client();
// Ensure the URL is formatted correctly
if ($url && !preg_match('/^https?:\/\//', $url)) {
$url = 'https://' . $url;
}
2025-01-16 15:56:22 +07:00
// Format the message like the email example using Markdown for WhatsApp
$message = "
Halo *{$name}*,
Pengajuan Expense Anda telah di periksa & disetujui oleh *Admin Region Anda*:
*Expense Number:* {$expense_number}
*Expense Date:* " . \Carbon\Carbon::parse($tanggal)->locale('id')->isoFormat('D MMMM Y') . "
*Total:* Rp " . number_format($total, 0, ',', '.') . "
Silahkan buka link berikut untuk melihat detail expense Anda: {$url}
Terima kasih.";
// Prepare the message for each phone number
2025-01-04 14:09:28 +07:00
$data = [];
foreach ($phones as $phone) {
$data[] = [
'phone' => $phone,
2025-01-16 15:56:22 +07:00
'message' => $message,
2025-01-04 14:09:28 +07:00
'source' => 'web'
];
}
2025-02-16 20:18:36 +07:00
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
2024-12-25 13:37:49 +07:00
}
2025-01-16 15:56:22 +07:00
public static function approve2Expense($phones, $expense_number, $url = null, $tanggal, $total, $name)
2025-01-04 14:09:28 +07:00
{
$client = new Client();
2025-01-16 15:56:22 +07:00
// Ensure the URL is formatted correctly
if ($url && !preg_match('/^https?:\/\//', $url)) {
$url = 'https://' . $url;
}
// Format the message like the email example using Markdown for WhatsApp
$message = "
Halo *{$name}*,
Pengajuan Expense Anda telah di periksa & disetujui oleh *Area Manager Anda*:
*Expense Number:* {$expense_number}
*Expense Date:* " . \Carbon\Carbon::parse($tanggal)->locale('id')->isoFormat('D MMMM Y') . "
*Total:* Rp " . number_format($total, 0, ',', '.') . "
Silahkan buka link berikut untuk melihat detail expense Anda: {$url}
Terima kasih.";
// Prepare the message for each phone number
$data = [];
foreach ($phones as $phone) {
$data[] = [
'phone' => $phone,
'message' => $message,
'source' => 'web'
];
}
2025-02-16 20:18:36 +07:00
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
2025-01-16 15:56:22 +07:00
}
public static function rejectExpense($phones, $expense_number, $url = null, $tanggal, $total, $name)
{
$client = new Client();
// Ensure the URL is formatted correctly
2025-01-04 14:09:28 +07:00
if ($url && !preg_match('/^https?:\/\//', $url)) {
$url = 'https://' . $url;
}
2025-01-16 15:56:22 +07:00
// Format the message like the email example using Markdown for WhatsApp
$message = "
Halo *{$name}*,
Pengajuan pengeluaran baru dengan detail berikut telah ditolak:
*Expense Number:* {$expense_number}
*Expense Date:* " . \Carbon\Carbon::parse($tanggal)->locale('id')->isoFormat('D MMMM Y') . "
*Total:* Rp " . number_format($total, 0, ',', '.') . "
Silahkan buka link berikut untuk melihat detail expense Anda: {$url}
Terima kasih.";
// Prepare the message for each phone number
2025-01-04 14:09:28 +07:00
$data = [];
foreach ($phones as $phone) {
$data[] = [
'phone' => $phone,
2025-01-16 15:56:22 +07:00
'message' => $message,
2025-01-04 14:09:28 +07:00
'source' => 'web'
];
}
2025-02-16 20:18:36 +07:00
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
2025-01-04 14:09:28 +07:00
}
2025-01-16 15:56:22 +07:00
public static function finalApprove($phones, $expense_number, $url = null, $tanggal, $total, $name)
2025-01-04 14:09:28 +07:00
{
$client = new Client();
2025-01-16 15:56:22 +07:00
// Ensure the URL is formatted correctly
2025-01-04 14:09:28 +07:00
if ($url && !preg_match('/^https?:\/\//', $url)) {
$url = 'https://' . $url;
}
2025-01-16 15:56:22 +07:00
// Format the message like the email example using Markdown for WhatsApp
$message = "
Halo *{$name}*,
Pengajuan Expense Anda telah di setujui *( FINAL APPROVE )* oleh *MOM Region / HOSM* anda:
*Expense Number:* {$expense_number}
*Expense Date:* " . \Carbon\Carbon::parse($tanggal)->locale('id')->isoFormat('D MMMM Y') . "
*Total:* Rp " . number_format($total, 0, ',', '.') . "
Silahkan buka link berikut untuk melihat detail expense Anda: {$url}
Terima kasih.";
// Prepare the message for each phone number
2025-01-04 14:09:28 +07:00
$data = [];
foreach ($phones as $phone) {
$data[] = [
'phone' => $phone,
2025-01-16 15:56:22 +07:00
'message' => $message,
2025-01-04 14:09:28 +07:00
'source' => 'web'
];
}
2025-02-16 20:18:36 +07:00
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
2025-01-04 14:09:28 +07:00
}
2025-01-09 13:22:56 +07:00
2025-01-16 15:56:22 +07:00
public static function newExpense($phones, $expense_number, $url = null, $tanggal, $total, $name)
2025-01-09 13:22:56 +07:00
{
$client = new Client();
2025-01-16 15:56:22 +07:00
// Ensure the URL is formatted correctly
2025-01-09 13:22:56 +07:00
if ($url && !preg_match('/^https?:\/\//', $url)) {
$url = 'https://' . $url;
}
2025-01-16 15:56:22 +07:00
// Format the message like the email example using Markdown for WhatsApp
$message = "
Halo *{$name}*,
Pengajuan pengeluaran baru dengan detail berikut telah dibuat:
*Expense Number:* {$expense_number}
*Expense Date:* " . \Carbon\Carbon::parse($tanggal)->locale('id')->isoFormat('D MMMM Y') . "
*Total:* Rp " . number_format($total, 0, ',', '.') . "
Silahkan buka link berikut untuk melihat detail expense Anda: {$url}
Terima kasih.";
// Prepare the message for each phone number
2025-01-09 13:22:56 +07:00
$data = [];
foreach ($phones as $phone) {
$data[] = [
'phone' => $phone,
2025-01-16 15:56:22 +07:00
'message' => $message,
2025-01-09 13:22:56 +07:00
'source' => 'web'
];
}
2025-02-16 20:18:36 +07:00
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
2025-03-03 21:34:16 +07:00
dd($e);
2025-02-16 20:18:36 +07:00
return null;
}
2025-01-09 13:22:56 +07:00
}
2025-02-16 20:18:36 +07:00
}