pull/2/head
ajikamaludin 2 years ago
parent 217ee8bf2a
commit 767612179a

@ -0,0 +1,78 @@
<?php
namespace App\Http\Controllers;
use App\Models\Company;
use App\Models\Region;
use Illuminate\Http\Request;
class CompanyController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return inertia('Company/Index', [
'companies' => Company::with(['region'])->paginate(),
'regions' => Region::all(),
]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'short' => 'required|string|max:255',
'region_id' => 'required|exists:regions,id'
]);
Company::create([
'region_id' => $request->region_id,
'name' => $request->name,
'short' => $request->short
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Company $company
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Company $company)
{
$request->validate([
'name' => 'required|string|max:255',
'short' => 'required|string|max:255',
'region_id' => 'required|exists:regions,id'
]);
$company->update([
'region_id' => $request->region_id,
'name' => $request->name,
'short' => $request->short
]);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Company $company
* @return \Illuminate\Http\Response
*/
public function destroy(Company $company)
{
$company->users()->delete();
$company->delete();
}
}

@ -15,6 +15,11 @@ class Company extends Model
"short"
];
public function region()
{
return $this->belongsTo(Region::class);
}
public function users()
{
return $this->hasMany(User::class);

@ -21,7 +21,6 @@ class Document extends Model
"no",
"no_doc",
"name",
"company_name",
"type_id",
"category_id",
"publisher",
@ -30,10 +29,12 @@ class Document extends Model
"due_date",
"status",
"type",
"group",
"region",
"document",
"user_id",
"company_id",
// "company_name",
// "group",
// "region",
];
protected $casts = [

@ -17,7 +17,6 @@ return new class extends Migration {
$table->string("no")->nullable();
$table->string("no_doc")->nullable();
$table->string("name")->nullable();
$table->string("company_name")->nullable();
$table->foreignId("type_id")->constrained(); //select jenis
$table->foreignId("category_id")->constrained(); //select
$table->string("publisher")->nullable();
@ -26,10 +25,15 @@ return new class extends Migration {
$table->timestamp("due_date")->nullable(); //for reminder
$table->smallInteger("status")->default(1); //only 1 yes/ 0no
$table->smallInteger("type")->default(1); //only 1 tetap/ 0tidak tetap
$table->string("group")->nullable();
$table->string("region")->nullable();
$table->string("document")->nullable();
$table->foreignId("user_id")->constrained();
$table->foreignId("company_id")->constrained();
// ?
// $table->string("group")->nullable();
// $table->string("region")->nullable();
// $table->string("company_name")->nullable();
$table->timestamps();
});
}

@ -16,7 +16,7 @@ const rs = [
{name: "Perusahaan", show: true, items: [
{name:"Group", route: "groups.index", show: true, permission: 'view-group'},
{name:"Region", route: "regions.index", show: true, permission: 'view-region'},
{name:"Perusahaan", route: "groups.index", show: true, permission: 'view-company'},
{name:"Perusahaan", route: "companies.index", show: true, permission: 'view-company'},
]},
{name: "User", show: true, items: [
{name:"User", route: "users.index", show: true, permission: 'view-user'},

@ -0,0 +1,158 @@
import React, { useEffect } from 'react'
import { useForm, usePage } from '@inertiajs/react'
import { toast } from 'react-toastify'
export default function FormModal(props) {
const { modalState } = props
const { props: { regions } } = usePage()
const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({
name: '',
short: '',
region_id: ''
})
const handleOnChange = (event) => {
setData(event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value);
}
const handleReset = () => {
reset()
clearErrors()
modalState.setData(null)
}
const handleCancel = () => {
handleReset()
modalState.toggle()
}
const handleSubmit = () => {
const company = modalState.data
if(company !== null) {
put(route('companies.update', company), {
onSuccess: () =>
Promise.all([
handleReset(),
modalState.toggle(),
toast.success('The Data has been changed'),
]),
})
return
}
post(route('companies.store'), {
onSuccess: () =>
Promise.all([
handleReset(),
modalState.toggle(),
toast.success('The Data has been saved'),
]),
})
}
useEffect(() => {
const company = modalState.data
if (company !== null) {
setData({
name: company?.name,
short: company?.short,
region_id: company?.region_id,
})
}
}, [modalState])
return (
<div
className="modal modal-bottom sm:modal-middle pb-10"
style={
modalState.isOpen
? {
opacity: 1,
pointerEvents: 'auto',
visibility: 'visible',
overflowY: 'initial',
}
: {}
}
>
<div className="modal-box overflow-y-auto max-h-screen">
<h1 className="font-bold text-2xl pb-8">Jenis</h1>
<div className="form-control">
<label className="label">
<span className="label-text font-semibold">Nama</span>
</label>
<input
type="text"
placeholder="nama"
className={`input input-bordered ${
errors.name && 'input-error'
}`}
name="name"
value={data.name}
onChange={handleOnChange}
/>
<label className="label">
<span className="label-text-alt text-red-600">{errors.name}</span>
</label>
</div>
<div className="form-control">
<label className="label">
<span className="label-text font-semibold">Inisial</span>
</label>
<input
type="text"
placeholder="inisial"
className={`input input-bordered ${
errors.short && 'input-error'
}`}
name="short"
value={data.short}
onChange={handleOnChange}
/>
<label className="label">
<span className="label-text-alt text-red-600">{errors.short}</span>
</label>
</div>
<div className="form-control">
<label className="label">
<span className="label-text">Region</span>
</label>
<select
className={`select select-bordered w-full ${
errors.region_id && 'select-error'
}`}
name='region_id'
value={data.region_id}
onChange={handleOnChange}
>
<option disabled value=""></option>
{regions.map(region => (
<option key={region.id} value={region.id}>{region.name}</option>
))}
</select>
<label className="label">
<span className="label-text-alt">
{errors.region_id}
</span>
</label>
</div>
<div className="modal-action">
<div
onClick={handleSubmit}
className="btn btn-primary"
disabled={processing}
>
Simpan
</div>
<div
onClick={handleCancel}
className="btn btn-secondary"
disabled={processing}
>
Batal
</div>
</div>
</div>
</div>
)
}

@ -0,0 +1,122 @@
import React from 'react'
import { Head } from '@inertiajs/react'
import { router } from '@inertiajs/react'
import { toast } from 'react-toastify'
import { useModalState } from '@/Hooks'
import { hasPermission } from '@/utils'
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'
import Pagination from '@/Components/Pagination'
import ModalConfirm from '@/Components/ModalConfirm'
import FormModal from './FormModal'
export default function Types(props) {
const { data: companies, links } = props.companies
const formModal = useModalState(false)
const toggle = (company = null) => {
formModal.setData(company)
formModal.toggle()
}
const confirmModal = useModalState(false)
const handleDelete = (company) => {
confirmModal.setData(company)
confirmModal.toggle()
}
const onDelete = () => {
const company = confirmModal.data
if(company != null) {
router.delete(route('companies.destroy', company), {
onSuccess: () => toast.success('The Data has been deleted'),
})
}
}
const canCreate = hasPermission('create-company', props.auth.user)
const canUpdate = hasPermission('update-company', props.auth.user)
const canDelete = hasPermission('delete-company', props.auth.user)
return (
<AuthenticatedLayout
auth={props.auth}
errors={props.errors}
flash={props.flash}
notify={props.notify}
>
<Head title="Roles" />
<div className="flex flex-col w-full sm:px-6 lg:px-8 space-y-2">
<div className="card bg-base-100 w-full">
<div className="card-body">
<div className="flex w-full mb-4 justify-between">
{canCreate && (
<div
className="btn btn-neutral"
onClick={() => toggle()}
>
Tambah
</div>
)}
</div>
<div className="overflow-x-auto">
<table className="table w-full table-zebra">
<thead>
<tr>
<th>Id</th>
<th>Nama</th>
<th>Inisial</th>
<th>Region</th>
<th></th>
</tr>
</thead>
<tbody>
{companies?.map((company) => (
<tr key={company.id}>
<th>{company.id}</th>
<td>{company.name}</td>
<td>{company.short}</td>
<td>{company.region.name}</td>
<td className="text-right">
{canUpdate && (
<div
className="btn btn-primary mx-1"
onClick={() =>
toggle(company)
}
>
Edit
</div>
)}
{canDelete && (
<div
className="btn btn-secondary mx-1"
onClick={() =>
handleDelete(company)
}
>
Delete
</div>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
<Pagination links={links} />
</div>
</div>
</div>
<FormModal
modalState={formModal}
/>
<ModalConfirm
isOpen={confirmModal.isOpen}
toggle={confirmModal.toggle}
onConfirm={onDelete}
/>
</AuthenticatedLayout>
)
}

@ -37,7 +37,7 @@ export default function Dashboard(props) {
<div className="card-body">
<div className="form-control">
<label className="label">
<span className="label-text font-semibold">Email</span>
<span className="label-text font-semibold">Email Tujuan</span>
</label>
<input
type="text"

@ -1,6 +1,7 @@
<?php
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\GeneralController;
use App\Http\Controllers\GroupController;
@ -52,6 +53,11 @@ Route::middleware(['auth'])->group(function () {
Route::put('/regions/{region}', [RegionController::class, 'update'])->name('regions.update');
Route::delete('/regions/{region}', [RegionController::class, 'destroy'])->name('regions.destroy');
Route::get('/companies', [CompanyController::class, 'index'])->name('companies.index');
Route::post('/companies', [CompanyController::class, 'store'])->name('companies.store');
Route::put('/companies/{company}', [CompanyController::class, 'update'])->name('companies.update');
Route::delete('/companies/{company}', [CompanyController::class, 'destroy'])->name('companies.destroy');
Route::get('/types', [TypeController::class, 'index'])->name('types.index');
Route::post('/types', [TypeController::class, 'store'])->name('types.store');
Route::put('/types/{type}', [TypeController::class, 'update'])->name('types.update');

Loading…
Cancel
Save