Add feat unsold commodity notifications
This commit is contained in:
@@ -5,9 +5,7 @@ namespace App\Console;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
|
||||
|
||||
use App\Cawangan;
|
||||
use App\Gadai;
|
||||
use Carbon\Carbon;
|
||||
use App\Jobs\WhatsappUnsoldCommodityScanner;
|
||||
|
||||
class Kernel extends ConsoleKernel
|
||||
{
|
||||
@@ -34,5 +32,7 @@ class Kernel extends ConsoleKernel
|
||||
//
|
||||
|
||||
$schedule->command('gadai:daily')->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00');
|
||||
|
||||
$schedule->job(new WhatsappUnsoldCommodityScanner)->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Jobs\WhatsappUnsoldCommodityNotification;
|
||||
use App\Jobs\WhatsappUnsoldCommodityScanner;
|
||||
use App\Setting;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SettingController extends Controller
|
||||
{
|
||||
public function getUnsoldCommodityNotifications()
|
||||
{
|
||||
$settings = Setting::where('group', 'unsold_commodity_notifications')->get();
|
||||
|
||||
return response()->json($settings);
|
||||
}
|
||||
|
||||
public function updateUnsoldCommodityNotifications(Request $request)
|
||||
{
|
||||
$request_array = $request->input();
|
||||
|
||||
$general_inputs = array_filter(
|
||||
$request_array,
|
||||
function ($value, $key) {
|
||||
return strpos($key, 'pic_') === false;
|
||||
},
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
);
|
||||
|
||||
foreach ($general_inputs as $key => $value) {
|
||||
Setting::where([
|
||||
['group', 'unsold_commodity_notifications'],
|
||||
['key', $key]
|
||||
])
|
||||
->first()
|
||||
->update(['value' => $value]);
|
||||
}
|
||||
|
||||
$pic_inputs = array_filter(
|
||||
$request_array,
|
||||
function ($value, $key) {
|
||||
return Str::startsWith($key, 'pic_');
|
||||
},
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
);
|
||||
|
||||
$unique_pic_input = array_map(function ($array, $key) {
|
||||
return $this->filterUniqueById($array, $key);
|
||||
}, $pic_inputs, array_keys($pic_inputs));
|
||||
|
||||
$transformed_unique_pic_input = [];
|
||||
|
||||
array_walk($unique_pic_input, function ($item) use (&$transformed_unique_pic_input) {
|
||||
foreach ($item as $key => $value) {
|
||||
$transformed_unique_pic_input[$key] = $value;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
foreach ($transformed_unique_pic_input as $key => $value) {
|
||||
Setting::where([
|
||||
['group', 'unsold_commodity_notifications'],
|
||||
['key', $key]
|
||||
])
|
||||
->first()
|
||||
->update(['value' => json_encode($value)]);
|
||||
}
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityScanner()));
|
||||
|
||||
return response()->json('success');
|
||||
}
|
||||
|
||||
public function filterUniqueById($array, $key)
|
||||
{
|
||||
$uniqueIds = [];
|
||||
$filteredArray = [];
|
||||
|
||||
if($array !== [] ){
|
||||
foreach ($array as $item) {
|
||||
if (!in_array($item['id'], $uniqueIds)) {
|
||||
$uniqueIds[] = $item['id'];
|
||||
$filteredArray[] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
$key => $filteredArray
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Setting;
|
||||
use Carbon\Carbon;
|
||||
use App\KomuditiPurchase;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WhatsappUnsoldCommodityNotification extends Job
|
||||
{
|
||||
public $status;
|
||||
public $start_time;
|
||||
public $end_time;
|
||||
public $message;
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
|
||||
|
||||
$this->status = $settings->where('key', 'status')->first()->value;
|
||||
|
||||
$this->start_time = $settings->where('key', 'start_time')->first()->value;
|
||||
|
||||
$this->end_time = $settings->where('key', 'end_time')->first()->value;
|
||||
|
||||
$this->message = $settings->where('key', 'message')->first()->value;
|
||||
|
||||
$isKomoditiStillAvailable = KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi === 0;
|
||||
|
||||
$pic_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek);
|
||||
$pic = json_decode($settings->where('key', $pic_key)->first()->value);
|
||||
|
||||
$phone_numbers = array_map(function ($p) {
|
||||
if(isset($p->no_telefon)){
|
||||
return $p->no_telefon;
|
||||
}
|
||||
}, $pic);
|
||||
|
||||
|
||||
if ($phone_numbers[0] === NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->status === '0') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Carbon::parse($this->start_time) > Carbon::now()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Carbon::parse($this->end_time) < Carbon::now()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$isKomoditiStillAvailable) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($phone_numbers as $phone_number) {
|
||||
Http::get(env('WABOT_API'), [
|
||||
'number' => $phone_number,
|
||||
'type' => 'text',
|
||||
'message' => $this->message,
|
||||
'instance_id' => env('WABOT_INSTANCES_ID'),
|
||||
'access_token' => env('WABOT_TOKEN_KEYS')
|
||||
]);
|
||||
}
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Setting;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class WhatsappUnsoldCommodityScanner extends Job
|
||||
{
|
||||
public $status;
|
||||
public $start_time;
|
||||
public $end_time;
|
||||
public $message;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$settings = Setting::on()->where('group', 'unsold_commodity_notifications')->get();
|
||||
|
||||
$this->status = $settings->where('key', 'status')->first()->value;
|
||||
|
||||
$this->start_time = $settings->where('key', 'start_time')->first()->value;
|
||||
|
||||
$this->end_time = $settings->where('key', 'end_time')->first()->value;
|
||||
|
||||
$this->message = $settings->where('key', 'message')->first()->value;
|
||||
|
||||
if ($this->status === '0') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Carbon::parse($this->start_time) > Carbon::now()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Carbon::parse($this->end_time) < Carbon::now()) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($this->start_time)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
|
||||
protected $fillable = [
|
||||
'key',
|
||||
'value',
|
||||
'group',
|
||||
'description',
|
||||
];
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -114,6 +114,7 @@ $app->router->group([
|
||||
], function ($router) {
|
||||
require __DIR__.'/../routes/web.php';
|
||||
require __DIR__.'/../routes/report.php';
|
||||
require __DIR__.'/../routes/setting.php';
|
||||
});
|
||||
|
||||
$app->middleware([
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateSettingsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('settings', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('key')->unique();
|
||||
$table->text('value')->nullable();
|
||||
$table->string('group')->nullable();
|
||||
$table->text('description')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('settings');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
use App\Setting;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddUnsoldCommoditiesSettings extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'key' => 'status',
|
||||
'value' => 'true',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Turn on or turn off notification for unsold commodity notifications',
|
||||
],
|
||||
[
|
||||
'key' => 'start_time',
|
||||
'value' => '20:00',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'The time the notifications start',
|
||||
],
|
||||
[
|
||||
'key' => 'end_time',
|
||||
'value' => '23:00',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'The time the notifications end',
|
||||
],
|
||||
[
|
||||
'key' => 'message',
|
||||
'value' => 'Baki komoditi masih lagi berbaki dan belum habis dijual',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Turn on or turn off notification for unsold commodity notifications',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_sunday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Sunday',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_monday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Monday',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_tuesday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Tuesday',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_wednesday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Wednesday',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_thursday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Thursday',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_friday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Friday',
|
||||
],
|
||||
[
|
||||
'key' => 'pic_saturday',
|
||||
'value' => '',
|
||||
'group' => 'unsold_commodity_notifications',
|
||||
'description' => 'Person Incharge on Saturday',
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
array_walk($data, function($setting){
|
||||
Setting::create($setting);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('settings');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
$router->group(['prefix' => 'api/settings'], function () use ($router) {
|
||||
$router->get('unsold-commodity-notifications', ['uses' => 'SettingController@getUnsoldCommodityNotifications']);
|
||||
$router->post('unsold-commodity-notifications', ['uses' => 'SettingController@updateUnsoldCommodityNotifications']);
|
||||
});
|
||||
Reference in New Issue
Block a user