diff --git a/app/Console/Commands/TestDoc.php b/app/Console/Commands/TestDoc.php
new file mode 100644
index 0000000..78a3618
--- /dev/null
+++ b/app/Console/Commands/TestDoc.php
@@ -0,0 +1,69 @@
+documents()->with(['variety'])->get() as $doc) {
+ if ($doc->is_close_due != 0) {
+ $docs[$doc->company->region->id][] = $doc;
+ }
+ }
+ }
+
+ $regions = Region::all();
+ foreach($regions as $region) {
+ $rdocs = Document::with(['variety'])
+ ->whereHas('creator', function ($query) use ($region) {
+ $query->where('region_id', $region->id);
+ })
+ ->whereDate('due_date', '<=', now()->toDateString());
+
+ if ($rdocs->count() > 0) {
+ foreach($rdocs->get() as $doc) {
+ $docs[$region->id][] = $doc;
+ }
+ }
+ }
+
+ foreach($docs as $regionId => $doc) {
+ $region = Region::find($regionId);
+ if ($region != null && $region->email != '') {
+ Mail::to($region->email)->send(new DocumentRegionNotification($doc));
+ }
+ }
+
+ return Command::SUCCESS;
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 03b6c9d..fb1290c 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -16,7 +16,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
- $schedule->job(new DocumentReminder)->daily();
+ $schedule->job(new DocumentReminder)->everyMinute();
}
/**
diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php
index b23b98f..67b31eb 100644
--- a/app/Http/Controllers/DocumentController.php
+++ b/app/Http/Controllers/DocumentController.php
@@ -74,7 +74,7 @@ class DocumentController extends Controller
{
$request->validate([
"no_doc" => "nullable|string",
- "name" => "required|string",
+ "name" => "nullable|string",
"type_id" => "required|exists:types,id",
"category_id" => "required|exists:categories,id",
"publisher" => "required|string",
@@ -149,7 +149,7 @@ class DocumentController extends Controller
{
$request->validate([
"no_doc" => "nullable|string",
- "name" => "required|string",
+ "name" => "nullable|string",
"type_id" => "required|exists:types,id",
"category_id" => "required|exists:categories,id",
"publisher" => "required|string",
diff --git a/app/Http/Controllers/RegionController.php b/app/Http/Controllers/RegionController.php
index 8ed9c9f..f000d2f 100644
--- a/app/Http/Controllers/RegionController.php
+++ b/app/Http/Controllers/RegionController.php
@@ -8,7 +8,7 @@ use Illuminate\Http\Request;
class RegionController extends Controller
{
- /**
+ /**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
@@ -31,12 +31,14 @@ class RegionController extends Controller
{
$request->validate([
'name' => 'required|string|max:255',
- 'group_id' => 'required|exists:groups,id'
+ 'group_id' => 'required|exists:groups,id',
+ 'email' => 'required|email',
]);
Region::create([
'group_id' => $request->group_id,
- 'name' => $request->name
+ 'name' => $request->name,
+ 'email' => $request->email
]);
}
@@ -51,12 +53,14 @@ class RegionController extends Controller
{
$request->validate([
'name' => 'required|string|max:255',
- 'group_id' => 'required|exists:groups,id'
+ 'group_id' => 'required|exists:groups,id',
+ 'email' => 'required|email',
]);
$region->update([
'group_id' => $request->group_id,
- 'name' => $request->name
+ 'name' => $request->name,
+ 'email' => $request->email
]);
}
diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php
index 542900c..5f792ca 100644
--- a/app/Http/Controllers/SettingController.php
+++ b/app/Http/Controllers/SettingController.php
@@ -3,7 +3,11 @@
namespace App\Http\Controllers;
use App\Mail\DocumentNotification;
+use App\Mail\DocumentRegionNotification;
+use App\Models\Category;
+use App\Models\Document;
use App\Models\Mail as ModelsMail;
+use App\Models\Region;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
@@ -21,18 +25,49 @@ class SettingController extends Controller
public function update(Request $request)
{
$request->validate([
- 'email' => 'required|email',
+ 'email' => 'nullable|email',
]);
Setting::where('key', 'DESTINATION_MAIL')->update([
- 'value' => $request->email,
+ 'value' => $request->email ?? '',
]);
- if ($request->has('test')) {
+ if ($request->has('test') && $request->email != null) {
$users = ModelsMail::all()->pluck('value')->toArray();
Mail::to($request->email)
->cc($users)
->send(new DocumentNotification());
+ $docs = [];
+ $categories = Category::all();
+ foreach($categories as $category) {
+ foreach($category->documents()->with(['variety'])->get() as $doc) {
+ if ($doc->is_close_due != 0) {
+ $docs[$doc->company->region->id][] = $doc;
+ }
+ }
+ }
+
+ $regions = Region::all();
+ foreach($regions as $region) {
+ $rdocs = Document::with(['variety'])
+ ->whereHas('company', function ($query) use ($region) {
+ $query->where('region_id', $region->id);
+ })
+ ->whereDate('due_date', '<=', now()->toDateString());
+
+ if ($rdocs->count() > 0) {
+ foreach($rdocs->get() as $doc) {
+ $docs[$region->id][] = $doc;
+ }
+ }
+ }
+
+ foreach($docs as $regionId => $doc) {
+ $region = Region::find($regionId);
+ if ($region != null && $region->email != '') {
+ Mail::to($region->email)->send(new DocumentRegionNotification($doc));
+ }
+ }
}
}
diff --git a/app/Jobs/DocumentReminder.php b/app/Jobs/DocumentReminder.php
index b7d0a44..82c5428 100644
--- a/app/Jobs/DocumentReminder.php
+++ b/app/Jobs/DocumentReminder.php
@@ -3,14 +3,19 @@
namespace App\Jobs;
use App\Mail\DocumentNotification;
+use App\Mail\DocumentRegionNotification;
+use App\Models\Category;
+use App\Models\Document;
use App\Models\Setting;
use App\Models\Mail as ModelsMail;
+use App\Models\Region;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class DocumentReminder implements ShouldQueue
@@ -34,11 +39,46 @@ class DocumentReminder implements ShouldQueue
*/
public function handle()
{
+ Log::info('staring', ['send email']);
$email = Setting::where('key', 'DESTINATION_MAIL')->first();
$users = ModelsMail::all()->pluck('value')->toArray();
- Mail::to($email->value)
- ->cc($users)
- ->send(new DocumentNotification());
+ if ($email?->value != '') {
+ Mail::to($email->value)
+ ->cc($users)
+ ->send(new DocumentNotification());
+ }
+
+ $docs = [];
+ $categories = Category::all();
+ foreach($categories as $category) {
+ foreach($category->documents()->with(['variety'])->get() as $doc) {
+ if ($doc->is_close_due != 0) {
+ $docs[$doc->company->region->id][] = $doc;
+ }
+ }
+ }
+
+ $regions = Region::all();
+ foreach($regions as $region) {
+ $rdocs = Document::with(['variety'])
+ ->whereHas('company', function ($query) use ($region) {
+ $query->where('region_id', $region->id);
+ })
+ ->whereDate('due_date', '<=', now()->toDateString());
+
+ if ($rdocs->count() > 0) {
+ foreach($rdocs->get() as $doc) {
+ $docs[$region->id][] = $doc;
+ }
+ }
+ }
+
+ foreach($docs as $regionId => $doc) {
+ $region = Region::find($regionId);
+ if ($region != null && $region->email != '') {
+ Mail::to($region->email)->send(new DocumentRegionNotification($doc));
+ }
+ }
}
}
diff --git a/app/Mail/DocumentRegionNotification.php b/app/Mail/DocumentRegionNotification.php
new file mode 100644
index 0000000..c136d04
--- /dev/null
+++ b/app/Mail/DocumentRegionNotification.php
@@ -0,0 +1,50 @@
+markdown('emails.document.notification', [
+ 'documents' => $this->docs,
+ 'dueDocuments' => []
+ ]);
+ }
+}
diff --git a/app/Models/Document.php b/app/Models/Document.php
index 913c6fd..acaf007 100644
--- a/app/Models/Document.php
+++ b/app/Models/Document.php
@@ -74,16 +74,16 @@ class Document extends Model
return '';
}
if (now()->toDateString() == $this->due_date->toDateString()) {
- return "hari ini jatuh tempo";
+ return "Hari ini jatuh tempo";
}
$date = now()->diffInDays($this->due_date, false) + 1;
if ($diff >= $date && $date > 0) {
- return $date . " hari mendekati jatuh tempo";
+ return $date . " Hari mendekati jatuh tempo";
}
if ($date <= 0) {
- return "jatuh tempo";
+ return "Jatuh tempo";
}
},
);
diff --git a/app/Models/Region.php b/app/Models/Region.php
index 99ac1d4..e35fd7e 100644
--- a/app/Models/Region.php
+++ b/app/Models/Region.php
@@ -13,6 +13,7 @@ class Region extends Model
protected $fillable = [
"name",
"group_id",
+ "email"
];
protected $cascadeDeletes = ['companies', 'users'];
diff --git a/database/migrations/2023_04_01_045717_update_regions.php b/database/migrations/2023_04_01_045717_update_regions.php
new file mode 100644
index 0000000..13e2510
--- /dev/null
+++ b/database/migrations/2023_04_01_045717_update_regions.php
@@ -0,0 +1,41 @@
+string('email')->nullable();
+ });
+
+ $permission = Permission::where('name', 'download-document')->first();
+ if ($permission == null) {
+ Permission::create([
+ 'name' => 'download-document',
+ 'label' => 'Download Documen'
+ ]);
+ }
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('regions', function ($table) {
+ $table->dropColumn('email');
+ });
+ }
+};
diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php
index f2cdf06..caa0055 100644
--- a/database/seeders/DatabaseSeeder.php
+++ b/database/seeders/DatabaseSeeder.php
@@ -51,6 +51,7 @@ class DatabaseSeeder extends Seeder
['name' => 'delete-document', 'label' => 'Hapus Dokumen'],
['name' => 'import-document', 'label' => 'Import Dokumen'],
['name' => 'export-document', 'label' => 'Export Dokumen'],
+ ['name' => 'download-document', 'label' => 'Download Documen'],
['name' => 'view-category', 'label' => 'Lihat Kategori'],
['name' => 'update-category', 'label' => 'Edit Kategori'],
['name' => 'create-category', 'label' => 'Buat Kategori'],
diff --git a/resources/js/Pages/Document/Detail.jsx b/resources/js/Pages/Document/Detail.jsx
index dfb9631..345cbe8 100644
--- a/resources/js/Pages/Document/Detail.jsx
+++ b/resources/js/Pages/Document/Detail.jsx
@@ -5,12 +5,14 @@ import DocStatusItem from './DocStatusItem'
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'
import InputLabel from '@/Components/InputLabel'
import TextInput from '@/Components/TextInput'
-import { formatDate } from '@/utils'
+import { formatDate, hasPermission } from '@/utils'
import { useModalState } from '@/Hooks'
export default function FormDocument(props) {
const { doc, doc_url }= props
+ const canDownload = hasPermission('download-document', props.auth.user)
+
return (