Skip to content

Commit

Permalink
[ADD] CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
mashanz committed Sep 15, 2021
1 parent c14f98c commit da91bf8
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 22 deletions.
2 changes: 1 addition & 1 deletion keuangan/templates/keuangan/dash.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<!-- Menu Root -->
{% block navbar_root_menu %}
<li class="nav-item">
<li class="nav-item" hidden>
<a class="nav-link active" aria-current="page" href="/pemasukan">Pemasukan</a>
</li>

Expand Down
2 changes: 1 addition & 1 deletion keuangan/templates/keuangan/pengeluaran.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<div class="row">
<div class="col col-lg-5">
<a href="/pengeluaran_form?tahun={{ thisYear|safe }}&bulan={{ thisMonth|safe }}" class="btn btn-sm btn-primary">CREATE</a>
<a href="#" class="btn btn-sm btn-light border">EXPORT</a>
<a href="#" class="btn btn-sm btn-light border d-none">EXPORT</a>

</div>
<div class="col col-lg-7 text-end d-none d-sm-block">
Expand Down
82 changes: 69 additions & 13 deletions keuangan/templates/keuangan/pengeluaran_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<!-- Body Navigation -->
{% block body_navigation %}
<form action="/pengeluaran" method="POST">
<form action="/pengeluaran_form" method="POST">
{% csrf_token %}
<div class="container-fluid bg-white border-bottom pb-3">
<div class="h3 py-3">
Expand All @@ -29,7 +29,7 @@
{% if record %}
<a href="#" class="btn btn-sm btn-light border" id="menuSettings" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi-gear"></i></a>
<ul class="dropdown-menu" aria-labelledby="menuSettings">
<li><a class="dropdown-item" href="#"><i class="bi-trash text-danger"></i> Hapus</a></li>
<li><button class="dropdown-item" onclick="deleteData({{ record.id|safe }})"><i class="bi-trash text-danger"></i> Hapus</button></li>
</ul>
{% endif %}
</div>
Expand All @@ -40,9 +40,13 @@
<div class="card-body">
<div class="container">

<input type="text" name="tahun" value="{{ thisYear|safe }}" hidden>
<input type="text" name="bulan" value="{{ thisMonth|safe }}" hidden>
<input type="number" name="id" value="{{ record.id|safe }}" hidden>

<div class="row my-1">
<div class="col-4 col-lg-2">
<label class="p-sm-2" for="klasifikasi" style="font-size: small;">Tanggal</label>
<label class="p-sm-2" for="tanggal" style="font-size: small;">Tanggal</label>
</div>
<div class="col-8 col-lg-10">
<input class="form-control" type="date" name="tanggal" id="tanggal" value="{% if record %}{{ record.tanggal|safe }}{% else %}{{ thisDate|safe }}{% endif %}" required>
Expand All @@ -54,43 +58,53 @@
<label class="p-sm-2" for="klasifikasi" style="font-size: small;">Klasifikasi</label>
</div>
<div class="col-8 col-lg-10">
<input class="form-control" type="text" name="klasifikasi" id="klasifikasi" value="{% if record %}{{ record.kategory|safe }}{% endif %}" required>
<input class="form-control" type="text" name="klasifikasi" id="klasifikasi" value="{% if record %}{{ record.kategory|safe }}{% endif %}" list="kategory-list" required>
<datalist id="kategory-list">
{% for kat in category %}
<option>{{ kat.nama }}</option>
{% endfor %}
</datalist>
</div>
</div>

<div class="row my-1">
<div class="col-4 col-lg-2">
<label class="p-sm-2" for="klasifikasi" style="font-size: small;">Produk</label>
<label class="p-sm-2" for="produk" style="font-size: small;">Produk</label>
</div>
<div class="col-8 col-lg-10">
<input class="form-control" type="text" name="klasifikasi" id="produk" value="{% if record %}{{ record.produk|safe }}{% endif %}" required>
<input class="form-control" type="text" name="produk" id="produk" value="{% if record %}{{ record.produk|safe }}{% endif %}" list="product-list" required>
<datalist id="product-list">
{% for kat in product %}
<option>{{ kat.nama }}</option>
{% endfor %}
</datalist>
</div>
</div>

<div class="row my-1">
<div class="col-4 col-lg-2">
<label class="p-sm-2" for="klasifikasi" style="font-size: small;">Kuantitas</label>
<label class="p-sm-2" for="qty" style="font-size: small;">Kuantitas</label>
</div>
<div class="col-8 col-lg-10">
<input class="form-control" type="number" name="klasifikasi" id="qty" value="{% if record %}{{ record.qty|safe }}{% endif %}" required>
<input class="form-control" type="number" name="qty" id="qty" value="{% if record %}{{ record.qty|safe }}{% endif %}" required>
</div>
</div>

<div class="row my-1">
<div class="col-4 col-lg-2">
<label class="p-sm-2" for="klasifikasi" style="font-size: small;">Harga Satuan</label>
<label class="p-sm-2" for="satuan" style="font-size: small;">Harga Satuan</label>
</div>
<div class="col-8 col-lg-10">
<input class="form-control w-100" type="number" name="klasifikasi" id="satuan" value="{% if record %}{{ record.harga_satuan|safe }}{% endif %}" required>
<input class="form-control w-100" type="number" name="satuan" id="satuan" value="{% if record %}{{ record.harga_satuan|safe }}{% endif %}" required>
</div>
</div>

<div class="row my-1">
<div class="col-4 col-lg-2">
<label class="p-sm-2" for="klasifikasi" style="font-size: small;">Harga Total</label>
<label class="p-sm-2" for="total" style="font-size: small;">Harga Total</label>
</div>
<div class="col-8 col-lg-10">
<input class="form-control w-100" type="number" name="klasifikasi" id="total" value="{% if record %}{{ record.harga_total|safe }}{% endif %}" disabled>
<input class="form-control w-100" type="number" name="total" id="total" value="{% if record %}{{ record.harga_total|safe }}{% endif %}" disabled>
</div>
</div>

Expand All @@ -108,4 +122,46 @@
{% endblock %}

<!-- Custom JS -->
<!-- {% block custom_js %}{% endblock %} -->
{% block custom_js %}
<script>
function deleteData(dataId) {
event.preventDefault();
// console.log(dataId);

// data = new FormData();
// data.set('csrfmiddlewaretoken', '{{ csrf_token }}');
// data.set('id',1);

// let request = new XMLHttpRequest();
// request.open("DELETE", '/pengeluaran_form', true);
// request.send(data)

var details = {
'csrfmiddlewaretoken': '{{csrf_token}}',
'dataId': dataId,
};

var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");

fetch('/pengeluaran_form', {
method: 'DELETE',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'X-CSRFToken': details['csrfmiddlewaretoken']
},
body: `{"dataId": ${details['dataId']}}`
}).then(data_response => {
if (data_response.ok) {
window.location.replace("/pengeluaran?tahun={{ thisYear|safe }}&bulan={{ thisMonth|safe }}");
}
});

}
</script>
{% endblock %}
5 changes: 0 additions & 5 deletions keuangan/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.urls import path
# from .views import detail, dashboard, index, edit, delete, save

from .views.index import index
from .views.dashboard import dashboard
from .views.detail import detail
from .views.edit import edit, delete, save
from .views.category import category
Expand All @@ -19,9 +17,6 @@
path('pengeluaran', pengeluaran),
path('pengeluaran_form', pengeluaran_form),
path('pemasukan', pemasukan),

path('home', index),
path('dashboard', dashboard),
path('detail', detail),
path('edit', edit),
path('delete', delete),
Expand Down
74 changes: 72 additions & 2 deletions keuangan/views/pengeluaran_form.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from datetime import datetime
import json
from ..models import CashFlow
from ..models import CashFlowCategory, Product
from ..models import Product

def pengeluaran_form(request):
def pengeluaran_form_get(request):
html_template = 'keuangan/pengeluaran_form.html'

getYear = int(request.GET.get('tahun', datetime.now().year))
Expand All @@ -14,10 +18,76 @@ def pengeluaran_form(request):
record = CashFlow.objects.get(pk=object_id)
record.tanggal = str(record.tanggal)[0:10]

category = CashFlowCategory.objects.all()
product = Product.objects.all()

context = {
'record': record,
'category': category,
'product': product,
'thisYear': getYear,
'thisMonth': getMonth,
'thisDate': f"{getYear}-{str(getMonth).zfill(2)}-{str(datetime.now().day).zfill(2)}"
}
return render(request, html_template, context)
return render(request, html_template, context)

def pengeluaran_form_post(request):
getYear = int(request.POST.get('tahun', datetime.now().year))
getMonth = int(request.POST.get('bulan', datetime.now().month))
getId = request.POST.get('id', 0)
getTanggal = request.POST.get('tanggal', '0000-00-00')
getKlasifikasi = request.POST.get('klasifikasi', '')
getProduk = request.POST.get('produk', '')
getQty = int(request.POST.get('qty', 0))
getSatuan = float(request.POST.get('satuan', 0.0))

try:
obj_category = CashFlowCategory.objects.get(nama__exact=getKlasifikasi)
except:
obj_category = CashFlowCategory(nama=getKlasifikasi)
obj_category.save()

try:
obj_product = Product.objects.get(nama__exact=getProduk)
except:
obj_product = Product(nama=getProduk)
obj_product.save()

if getId != '':
# update
record = CashFlow.objects.get(pk=int(getId))
record.kategory = obj_category
record.produk = obj_product
record.qty = getQty
record.harga_satuan = getSatuan
record.tanggal = getTanggal
record.save()
else:
# create
obj_cashflow = CashFlow(
kategory= obj_category,
produk=obj_product,
qty=getQty,
harga_satuan=getSatuan,
tanggal=getTanggal
)
obj_cashflow.save()

return redirect(f"/pengeluaran?tahun={getYear}&bulan={getMonth}")

def pengeluaran_form_delete(request):
body = request.body
body_decode = body.decode('utf-8')
body_json = json.loads(body_decode)
record = CashFlow.objects.filter(pk=int(body_json['dataId']))
record.delete()
return JsonResponse({"status":"ok"})


def pengeluaran_form(request):
if request.method == 'GET':
return pengeluaran_form_get(request)
elif request.method == 'POST':
return pengeluaran_form_post(request)
elif request.method == 'DELETE':
return pengeluaran_form_delete(request)

0 comments on commit da91bf8

Please sign in to comment.