From dfb27457d221dfe07cb1e714da36cee623fa7af3 Mon Sep 17 00:00:00 2001 From: ajikamaludin Date: Sat, 1 Apr 2023 06:18:19 +0700 Subject: [PATCH] change request email perregion --- app/Console/Commands/TestDoc.php | 69 +++++++++++++++++++ app/Console/Kernel.php | 2 +- app/Http/Controllers/DocumentController.php | 4 +- app/Http/Controllers/RegionController.php | 14 ++-- app/Http/Controllers/SettingController.php | 41 ++++++++++- app/Jobs/DocumentReminder.php | 46 ++++++++++++- app/Mail/DocumentRegionNotification.php | 50 ++++++++++++++ app/Models/Document.php | 6 +- app/Models/Region.php | 1 + .../2023_04_01_045717_update_regions.php | 41 +++++++++++ database/seeders/DatabaseSeeder.php | 1 + resources/js/Pages/Document/Detail.jsx | 14 ++-- resources/js/Pages/Document/Index.jsx | 4 +- resources/js/Pages/Region/FormModal.jsx | 24 ++++++- resources/js/Pages/Region/Index.jsx | 2 + .../emails/document/notification.blade.php | 4 +- 16 files changed, 295 insertions(+), 28 deletions(-) create mode 100644 app/Console/Commands/TestDoc.php create mode 100644 app/Mail/DocumentRegionNotification.php create mode 100644 database/migrations/2023_04_01_045717_update_regions.php 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 ( -
- - Download -
+ {canDownload && ( +
+ + Download +
+ )}
diff --git a/resources/js/Pages/Document/Index.jsx b/resources/js/Pages/Document/Index.jsx index ddae36e..c7c136a 100644 --- a/resources/js/Pages/Document/Index.jsx +++ b/resources/js/Pages/Document/Index.jsx @@ -138,7 +138,7 @@ export default function Document(props) { sort('type_id')}>Jenis sort('category_id')}>Ketegori No Dokumen - Nama Dokumen + {/* Nama Dokumen */} sort('publish_date')}>Tanggal Terbit sort('due_date')}>Tanggal Berakhir Catatan @@ -152,7 +152,7 @@ export default function Document(props) { {doc.variety.name} {doc.category.name} {doc.no_doc} - {doc.name} + {/* {doc.name} */} {doc.publish_date !== null ? formatDate(doc.publish_date) : ''} diff --git a/resources/js/Pages/Region/FormModal.jsx b/resources/js/Pages/Region/FormModal.jsx index 0c52452..6ec3203 100644 --- a/resources/js/Pages/Region/FormModal.jsx +++ b/resources/js/Pages/Region/FormModal.jsx @@ -8,7 +8,8 @@ export default function FormModal(props) { const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ name: '', - group_id: '' + group_id: '', + email: '', }) const handleOnChange = (event) => { @@ -54,7 +55,8 @@ export default function FormModal(props) { if (region !== null) { setData({ name: region?.name, - group_id: region?.group_id + group_id: region?.group_id, + email: region?.email, }) } }, [modalState]) @@ -116,6 +118,24 @@ export default function FormModal(props) {
+
+ + + +
Id Nama Group + Email @@ -75,6 +76,7 @@ export default function Types(props) { {region.id} {`${region.name} (${region.group.name})`} {region.group.name} + {region.email} {canUpdate && (
no_doc }} | {{ $document->name }} | {{ $document->variety->name }} | {{ $document->due_status }} | +| {{ $document->no_doc }} | {{ $document->company->name }} | {{ $document->variety->name }} | {{ $document->due_status }} | | @endforeach |@foreach($documents as $document) -| {{ $document->no_doc }} | {{ $document->name }} | {{ $document->variety->name }} | {{ $document->due_status }} | +| {{ $document->no_doc }} | {{ $document->company->name }} | {{ $document->variety->name }} | {{ $document->due_status }} | |@endforeach @endcomponent