Skip to content

Commit 8687a2f

Browse files
committed
transaction
1 parent 5de180b commit 8687a2f

16 files changed

+498
-4
lines changed

app/Http/Controllers/OrderController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public function endOrder(Order $order, Product $product)
310310
$user->save();
311311

312312
$transactional_data = [
313-
"title" => "product sale",
313+
"category_id" => 1,
314314
"description" => "sales of {$order->quantity} unit of product {$product->product_name}",
315315
"income" => $order->total_price,
316316
"outcome" => null,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Category;
6+
use App\Models\Transaction;
7+
use Illuminate\Http\Request;
8+
9+
class TransactionController extends Controller
10+
{
11+
public function index()
12+
{
13+
$title = "Transaction Page";
14+
$transactions = Transaction::with("category")->get();
15+
16+
return view("/transaction/index", compact("title", "transactions"));
17+
}
18+
19+
20+
public function addOutcomeGet()
21+
{
22+
$title = "Add Outcome";
23+
$categories = Category::where("id", "!=", 1)->get();
24+
25+
return view("/transaction/add_outcome", compact("title", "categories"));
26+
}
27+
28+
29+
public function addOutcomePost(Request $request)
30+
{
31+
$validatedData = $request->validate(
32+
[
33+
"category_id" => "required|numeric|gt:0",
34+
"outcome" => "required|numeric|gt:0",
35+
"description" => "required"
36+
],
37+
[
38+
"category_id.gt" => "Please select the category"
39+
]
40+
);
41+
42+
Transaction::create($validatedData);
43+
44+
$message = "Transaction created successfully!";
45+
46+
myFlasherBuilder(message: $message, success: true);
47+
48+
return redirect("/transaction");
49+
}
50+
51+
52+
public function editOutcomeGet(Transaction $transaction)
53+
{
54+
$title = "Edit Outcome";
55+
$transaction->load("category");
56+
$categories = Category::where("id", "!=", 1)->get();
57+
58+
return view("/transaction/edit_outcome", compact("title", "transaction", "categories"));
59+
}
60+
61+
public function editOutcomePost(Request $request, Transaction $transaction)
62+
{
63+
$validatedData = $request->validate(
64+
[
65+
"category_id" => "required|numeric|gt:0",
66+
"outcome" => "required|numeric|gt:0",
67+
"description" => "required"
68+
],
69+
[
70+
"category_id.gt" => "Please select the category"
71+
]
72+
);
73+
74+
$transaction->fill($validatedData);
75+
76+
if ($transaction->isDirty()) {
77+
$transaction->save();
78+
79+
80+
$message = "Transaction updated successfully!";
81+
82+
myFlasherBuilder(message: $message, success: true);
83+
84+
return redirect("/transaction");
85+
} else {
86+
$message = "Action failed, no changes detected!";
87+
88+
myFlasherBuilder(message: $message, failed: true);
89+
90+
return back();
91+
}
92+
}
93+
}

app/Models/Category.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Category extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $guarded = ['id'];
13+
}

app/Models/Transaction.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace App\Models;
44

5-
use Illuminate\Database\Eloquent\Factories\HasFactory;
5+
use App\Models\Category;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
78

89
class Transaction extends Model
910
{
1011
use HasFactory;
1112

1213
protected $guarded = ['id'];
14+
15+
public function category()
16+
{
17+
return $this->belongsTo(Category::class);
18+
}
1319
}

database/migrations/2022_08_09_132544_create_transactions_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('transactions', function (Blueprint $table) {
1717
$table->id();
18-
$table->string("title");
18+
$table->string("category_id");
1919
$table->string("description");
2020
$table->integer("income")->nullable();
2121
$table->integer("outcome")->nullable();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('categories', function (Blueprint $table) {
17+
$table->id();
18+
$table->string("category_name");
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('categories');
31+
}
32+
};

database/seeders/CategorySeeder.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Category;
6+
use Illuminate\Database\Seeder;
7+
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
8+
9+
class CategorySeeder extends Seeder
10+
{
11+
/**
12+
* Run the database seeds.
13+
*
14+
* @return void
15+
*/
16+
public function run()
17+
{
18+
Category::create([
19+
"category_name" => "Product Sale",
20+
]);
21+
22+
Category::create([
23+
"category_name" => "Production Cost",
24+
]);
25+
26+
Category::create([
27+
"category_name" => "Marketing Cost",
28+
]);
29+
30+
Category::create([
31+
"category_name" => "Server Maintanance",
32+
]);
33+
}
34+
}

database/seeders/DatabaseSeeder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function run()
2929
ProductSeeder::class,
3030
NoteSeeder::class,
3131
PaymentSeeder::class,
32-
StatusSeeder::class
32+
StatusSeeder::class,
33+
CategorySeeder::class
3334
]);
3435
}
3536
}

public/css/transaction.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background-color: rgb(220, 215, 215) !important;
3+
}

public/js/transaction.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$(".button_edit_transaction").click(function () {
2+
var id = $(this).attr("data-transactionId");
3+
4+
if ($(this).attr("data-isOutcome") == "0") {
5+
Swal.fire("Oops, you can not edit income data");
6+
} else {
7+
window.location = "/transaction/edit_outcome/" + id;
8+
}
9+
});

public/js/transaction_table.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
window.addEventListener("DOMContentLoaded", (event) => {
2+
$("#transaction_table").dataTable({
3+
columnDefs: [
4+
{ orderable: false, targets: 6 },
5+
{ className: "dt-center", targets: "_all" },
6+
],
7+
lengthMenu: [
8+
[5, 10, 25, 50, 100, -1],
9+
[5, 10, 25, 50, 100, "All"],
10+
],
11+
});
12+
});

resources/views/partials/sidebar.blade.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<div class="sb-nav-link-icon"><i class="fas fa-fw fa-solid fa-users"></i></div>
1212
Customers
1313
</a>
14+
<a class="nav-link" href="/transaction">
15+
<div class="sb-nav-link-icon"><i class="fa-solid fa-fw fa-dollar-sign"></i></div>
16+
Transaksi
17+
</a>
1418
@else
1519
<div class="sb-sidenav-menu-heading">Customer</div>
1620
<a class="nav-link" href="/home">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
@extends('/layouts/main')
2+
3+
@push('css-dependencies')
4+
<link href="/css/transaction.css" rel="stylesheet" />
5+
@endpush
6+
7+
@section('content')
8+
<div class="container-fluid pt-4">
9+
10+
@include('/partials/breadcumb')
11+
12+
@if(session()->has('message'))
13+
{!! session("message") !!}
14+
@endif
15+
16+
17+
<div class="row flex-lg-nowrap">
18+
19+
<div class="col">
20+
<div class="row">
21+
<div class="col mb-3">
22+
<div class="card">
23+
<div class="card-body">
24+
<div class="e-profile">
25+
<ul class="nav nav-tabs">
26+
<li class="nav-item"><a href="" class="active nav-link">Form of {{ $title }}</a></li>
27+
</ul>
28+
29+
<!-- Form -->
30+
<form action="/transaction/add_outcome" method="post">
31+
@csrf
32+
<div class="tab-content pt-3">
33+
<div class="tab-pane active">
34+
<div class="row">
35+
<div class="col">
36+
<div class="row">
37+
<div class="col-12 col-sm-8 mb-3">
38+
<div class="form-group">
39+
<label for="category">Category</label>
40+
<select class="form-control" name="category_id" id="category">
41+
<option value="0">Select Category</option>
42+
@foreach ($categories as $category)
43+
<option value="{{ $category->id }}" {{ old("category_id")==$category->id ? "selected"
44+
: "" }}>{{ $category->category_name }}</option>
45+
@endforeach
46+
</select>
47+
@error('category_id')
48+
<div class="text-danger">{{ $message }}</div>
49+
@enderror
50+
</div>
51+
</div>
52+
<div class="col-12 col-sm-4 mb-3">
53+
<div class="form-group">
54+
<label for="outcome">Total Outcome</label>
55+
<input class="form-control" type="text" id="outcome" name="outcome"
56+
placeholder="Enter outcome" value="{{ old('outcome') }}">
57+
@error('outcome')
58+
<div class="text-danger">{{ $message }}</div>
59+
@enderror
60+
</div>
61+
</div>
62+
</div>
63+
<div class="row">
64+
<div class="col mb-3">
65+
<div class="form-group">
66+
<label for="description">Description</label>
67+
<input class="form-control" id="description" name="description"
68+
placeholder="Masukkan description outcome" value="{{ old('description') }}">
69+
@error('description')
70+
<div class="text-danger">{{ $message }}</div>
71+
@enderror
72+
</div>
73+
</div>
74+
</div>
75+
</div>
76+
</div>
77+
<div class="row">
78+
<div class="col d-flex justify-content-end">
79+
<a class="btn btn-secondary mx-3" href="/transaction">Back to Transaction List</a>
80+
<button class="btn btn-dark" type="submit">Submit</button>
81+
</div>
82+
</div>
83+
</div>
84+
</div>
85+
</form>
86+
</div>
87+
</div>
88+
</div>
89+
</div>
90+
</div>
91+
</div>
92+
</div>
93+
</div>
94+
@endsection

0 commit comments

Comments
 (0)